2018-04-13 21:03:55 +00:00
|
|
|
package ops
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/containerd/continuity/fs"
|
|
|
|
"github.com/moby/buildkit/client/llb"
|
|
|
|
"github.com/moby/buildkit/frontend"
|
2020-06-30 01:06:02 +00:00
|
|
|
"github.com/moby/buildkit/session"
|
2018-04-13 21:03:55 +00:00
|
|
|
"github.com/moby/buildkit/snapshot"
|
2018-05-11 05:58:41 +00:00
|
|
|
"github.com/moby/buildkit/solver"
|
2019-06-15 00:26:27 +00:00
|
|
|
"github.com/moby/buildkit/solver/llbsolver"
|
2018-04-13 21:03:55 +00:00
|
|
|
"github.com/moby/buildkit/solver/pb"
|
|
|
|
"github.com/moby/buildkit/worker"
|
|
|
|
digest "github.com/opencontainers/go-digest"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
const buildCacheType = "buildkit.build.v0"
|
|
|
|
|
|
|
|
type buildOp struct {
|
|
|
|
op *pb.BuildOp
|
|
|
|
b frontend.FrontendLLBBridge
|
|
|
|
v solver.Vertex
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewBuildOp(v solver.Vertex, op *pb.Op_Build, b frontend.FrontendLLBBridge, _ worker.Worker) (solver.Op, error) {
|
2019-06-15 00:26:27 +00:00
|
|
|
if err := llbsolver.ValidateOp(&pb.Op{Op: op}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-04-13 21:03:55 +00:00
|
|
|
return &buildOp{
|
|
|
|
op: op.Build,
|
|
|
|
b: b,
|
|
|
|
v: v,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-06-30 01:06:02 +00:00
|
|
|
func (b *buildOp) CacheMap(ctx context.Context, g session.Group, index int) (*solver.CacheMap, bool, error) {
|
2018-04-13 21:03:55 +00:00
|
|
|
dt, err := json.Marshal(struct {
|
|
|
|
Type string
|
|
|
|
Exec *pb.BuildOp
|
|
|
|
}{
|
|
|
|
Type: buildCacheType,
|
|
|
|
Exec: b.op,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2018-04-24 21:00:58 +00:00
|
|
|
return nil, false, err
|
2018-04-13 21:03:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &solver.CacheMap{
|
|
|
|
Digest: digest.FromBytes(dt),
|
|
|
|
Deps: make([]struct {
|
|
|
|
Selector digest.Digest
|
|
|
|
ComputeDigestFunc solver.ResultBasedCacheFunc
|
2020-10-31 03:55:33 +00:00
|
|
|
PreprocessFunc solver.PreprocessFunc
|
2018-04-13 21:03:55 +00:00
|
|
|
}, len(b.v.Inputs())),
|
2018-04-24 21:00:58 +00:00
|
|
|
}, true, nil
|
2018-04-13 21:03:55 +00:00
|
|
|
}
|
|
|
|
|
2020-06-30 01:06:02 +00:00
|
|
|
func (b *buildOp) Exec(ctx context.Context, g session.Group, inputs []solver.Result) (outputs []solver.Result, retErr error) {
|
2018-04-13 21:03:55 +00:00
|
|
|
if b.op.Builder != pb.LLBBuilder {
|
|
|
|
return nil, errors.Errorf("only LLB builder is currently allowed")
|
|
|
|
}
|
|
|
|
|
|
|
|
builderInputs := b.op.Inputs
|
|
|
|
llbDef, ok := builderInputs[pb.LLBDefinitionInput]
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.Errorf("no llb definition input %s found", pb.LLBDefinitionInput)
|
|
|
|
}
|
|
|
|
|
|
|
|
i := int(llbDef.Input)
|
|
|
|
if i >= len(inputs) {
|
|
|
|
return nil, errors.Errorf("invalid index %v", i) // TODO: this should be validated before
|
|
|
|
}
|
|
|
|
inp := inputs[i]
|
|
|
|
|
|
|
|
ref, ok := inp.Sys().(*worker.WorkerRef)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.Errorf("invalid reference for build %T", inp.Sys())
|
|
|
|
}
|
|
|
|
|
2020-10-27 06:13:39 +00:00
|
|
|
mount, err := ref.ImmutableRef.Mount(ctx, true, g)
|
2018-04-13 21:03:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
lm := snapshot.LocalMounter(mount)
|
|
|
|
|
|
|
|
root, err := lm.Mount()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if retErr != nil && lm != nil {
|
|
|
|
lm.Unmount()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
fn := pb.LLBDefaultDefinitionFile
|
|
|
|
if override, ok := b.op.Attrs[pb.AttrLLBDefinitionFilename]; ok {
|
|
|
|
fn = override
|
|
|
|
}
|
|
|
|
|
|
|
|
newfn, err := fs.RootPath(root, fn)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "working dir %s points to invalid target", fn)
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := os.Open(newfn)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "failed to open %s", newfn)
|
|
|
|
}
|
|
|
|
|
|
|
|
def, err := llb.ReadFrom(f)
|
|
|
|
if err != nil {
|
|
|
|
f.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
f.Close()
|
|
|
|
lm.Unmount()
|
|
|
|
lm = nil
|
|
|
|
|
2018-07-11 23:51:41 +00:00
|
|
|
newRes, err := b.b.Solve(ctx, frontend.SolveRequest{
|
2018-04-13 21:03:55 +00:00
|
|
|
Definition: def.ToPB(),
|
2020-06-30 01:06:02 +00:00
|
|
|
}, g.SessionIterator().NextSession())
|
2018-04-13 21:03:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-07-11 23:51:41 +00:00
|
|
|
for _, r := range newRes.Refs {
|
|
|
|
r.Release(context.TODO())
|
2018-07-05 23:32:01 +00:00
|
|
|
}
|
|
|
|
|
2020-02-04 23:00:44 +00:00
|
|
|
r, err := newRes.Ref.Result(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return []solver.Result{r}, err
|
2018-04-13 21:03:55 +00:00
|
|
|
}
|