2017-06-06 18:01:18 +00:00
|
|
|
package solver
|
|
|
|
|
|
|
|
import (
|
2017-07-06 04:25:51 +00:00
|
|
|
"strings"
|
|
|
|
|
2017-06-22 20:15:46 +00:00
|
|
|
"github.com/moby/buildkit/solver/pb"
|
2017-06-06 18:01:18 +00:00
|
|
|
digest "github.com/opencontainers/go-digest"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2017-07-06 04:25:51 +00:00
|
|
|
func LoadLLB(ops [][]byte) (Vertex, error) {
|
2017-06-09 01:16:19 +00:00
|
|
|
if len(ops) == 0 {
|
|
|
|
return nil, errors.New("invalid empty definition")
|
|
|
|
}
|
|
|
|
|
2017-07-06 04:25:51 +00:00
|
|
|
allOps := make(map[digest.Digest]*pb.Op)
|
2017-06-06 18:01:18 +00:00
|
|
|
|
|
|
|
var lastOp *pb.Op
|
2017-07-06 04:25:51 +00:00
|
|
|
var lastDigest digest.Digest
|
2017-06-06 18:01:18 +00:00
|
|
|
|
2017-07-06 04:25:51 +00:00
|
|
|
for _, dt := range ops {
|
2017-06-06 18:01:18 +00:00
|
|
|
var op pb.Op
|
|
|
|
if err := (&op).Unmarshal(dt); err != nil {
|
2017-07-06 04:25:51 +00:00
|
|
|
return nil, errors.Wrap(err, "failed to parse llb proto op")
|
2017-06-06 18:01:18 +00:00
|
|
|
}
|
|
|
|
lastOp = &op
|
2017-07-06 04:25:51 +00:00
|
|
|
lastDigest = digest.FromBytes(dt)
|
|
|
|
allOps[lastDigest] = &op
|
2017-06-06 18:01:18 +00:00
|
|
|
}
|
|
|
|
|
2017-07-06 04:25:51 +00:00
|
|
|
delete(allOps, lastDigest) // avoid loops
|
|
|
|
|
|
|
|
cache := make(map[digest.Digest]*vertex)
|
2017-06-06 18:01:18 +00:00
|
|
|
|
|
|
|
// TODO: validate the connections
|
2017-07-06 04:25:51 +00:00
|
|
|
return loadLLBVertexRecursive(lastDigest, lastOp, allOps, cache)
|
|
|
|
}
|
2017-06-06 18:01:18 +00:00
|
|
|
|
2017-07-06 04:25:51 +00:00
|
|
|
func toInternalVertex(v Vertex) *vertex {
|
|
|
|
cache := make(map[digest.Digest]*vertex)
|
|
|
|
return loadInternalVertexHelper(v, cache)
|
2017-06-06 18:01:18 +00:00
|
|
|
}
|
|
|
|
|
2017-07-06 04:25:51 +00:00
|
|
|
func loadInternalVertexHelper(v Vertex, cache map[digest.Digest]*vertex) *vertex {
|
|
|
|
if v, ok := cache[v.Digest()]; ok {
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
vtx := &vertex{sys: v.Sys(), digest: v.Digest(), name: v.Name()}
|
|
|
|
for _, in := range v.Inputs() {
|
|
|
|
vv := loadInternalVertexHelper(in.Vertex, cache)
|
|
|
|
vtx.inputs = append(vtx.inputs, &input{index: in.Index, vertex: vv})
|
|
|
|
}
|
|
|
|
vtx.initClientVertex()
|
|
|
|
cache[v.Digest()] = vtx
|
|
|
|
return vtx
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadLLBVertexRecursive(dgst digest.Digest, op *pb.Op, all map[digest.Digest]*pb.Op, cache map[digest.Digest]*vertex) (*vertex, error) {
|
2017-06-06 18:01:18 +00:00
|
|
|
if v, ok := cache[dgst]; ok {
|
|
|
|
return v, nil
|
|
|
|
}
|
2017-07-06 04:25:51 +00:00
|
|
|
vtx := &vertex{sys: op.Op, digest: dgst, name: llbOpName(op)}
|
2017-06-06 18:01:18 +00:00
|
|
|
for _, in := range op.Inputs {
|
2017-06-13 21:42:51 +00:00
|
|
|
dgst := digest.Digest(in.Digest)
|
2017-07-06 04:25:51 +00:00
|
|
|
op, ok := all[dgst]
|
2017-06-06 18:01:18 +00:00
|
|
|
if !ok {
|
|
|
|
return nil, errors.Errorf("failed to find %s", in)
|
|
|
|
}
|
2017-07-06 04:25:51 +00:00
|
|
|
sub, err := loadLLBVertexRecursive(dgst, op, all, cache)
|
2017-06-06 18:01:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-07-31 22:06:59 +00:00
|
|
|
vtx.inputs = append(vtx.inputs, &input{index: Index(in.Index), vertex: sub})
|
2017-06-13 21:42:51 +00:00
|
|
|
}
|
2017-07-06 04:25:51 +00:00
|
|
|
vtx.initClientVertex()
|
2017-06-06 18:01:18 +00:00
|
|
|
cache[dgst] = vtx
|
|
|
|
return vtx, nil
|
|
|
|
}
|
2017-07-06 04:25:51 +00:00
|
|
|
|
|
|
|
func llbOpName(op *pb.Op) string {
|
|
|
|
switch op := op.Op.(type) {
|
|
|
|
case *pb.Op_Source:
|
|
|
|
return op.Source.Identifier
|
|
|
|
case *pb.Op_Exec:
|
|
|
|
return strings.Join(op.Exec.Meta.Args, " ")
|
2017-07-21 17:58:24 +00:00
|
|
|
case *pb.Op_Build:
|
|
|
|
return "build"
|
2017-07-06 04:25:51 +00:00
|
|
|
default:
|
|
|
|
return "unknown"
|
|
|
|
}
|
|
|
|
}
|