2017-06-13 08:09:14 +00:00
|
|
|
package debug
|
2017-06-12 09:52:25 +00:00
|
|
|
|
|
|
|
import (
|
2017-06-21 21:46:48 +00:00
|
|
|
"encoding/json"
|
2017-07-17 21:24:19 +00:00
|
|
|
"fmt"
|
2017-06-12 09:52:25 +00:00
|
|
|
"io"
|
|
|
|
"os"
|
2017-07-17 21:24:19 +00:00
|
|
|
"strings"
|
2017-06-12 09:52:25 +00:00
|
|
|
|
2017-06-22 20:15:46 +00:00
|
|
|
"github.com/moby/buildkit/client/llb"
|
|
|
|
"github.com/moby/buildkit/solver/pb"
|
2017-06-12 09:52:25 +00:00
|
|
|
digest "github.com/opencontainers/go-digest"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/urfave/cli"
|
|
|
|
)
|
|
|
|
|
2017-07-05 03:38:48 +00:00
|
|
|
var DumpLLBCommand = cli.Command{
|
|
|
|
Name: "dump-llb",
|
|
|
|
Usage: "dump LLB in human-readable format. LLB can be also passed via stdin. This command does not require the daemon to be running.",
|
|
|
|
ArgsUsage: "<llbfile>",
|
|
|
|
Action: dumpLLB,
|
2017-07-17 21:24:19 +00:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "dot",
|
|
|
|
Usage: "Output dot format",
|
|
|
|
},
|
|
|
|
},
|
2017-06-12 09:52:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func dumpLLB(clicontext *cli.Context) error {
|
2017-07-05 03:38:48 +00:00
|
|
|
var r io.Reader
|
|
|
|
if llbFile := clicontext.Args().First(); llbFile != "" && llbFile != "-" {
|
|
|
|
f, err := os.Open(llbFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
r = f
|
|
|
|
} else {
|
|
|
|
r = os.Stdin
|
|
|
|
}
|
|
|
|
ops, err := loadLLB(r)
|
2017-06-12 09:52:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-07-17 21:24:19 +00:00
|
|
|
if clicontext.Bool("dot") {
|
|
|
|
writeDot(ops, os.Stdout)
|
|
|
|
} else {
|
|
|
|
enc := json.NewEncoder(os.Stdout)
|
|
|
|
for _, op := range ops {
|
|
|
|
if err := enc.Encode(op); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-06-21 21:46:48 +00:00
|
|
|
}
|
|
|
|
}
|
2017-06-12 09:52:25 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type llbOp struct {
|
2017-10-02 04:59:34 +00:00
|
|
|
Op pb.Op
|
|
|
|
Digest digest.Digest
|
|
|
|
OpMetadata pb.OpMetadata
|
2017-06-12 09:52:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func loadLLB(r io.Reader) ([]llbOp, error) {
|
2017-10-02 04:59:34 +00:00
|
|
|
def, err := llb.ReadFrom(r)
|
2017-06-12 09:52:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var ops []llbOp
|
2017-10-02 04:59:34 +00:00
|
|
|
for _, dt := range def.Def {
|
2017-06-12 09:52:25 +00:00
|
|
|
var op pb.Op
|
|
|
|
if err := (&op).Unmarshal(dt); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to parse op")
|
|
|
|
}
|
|
|
|
dgst := digest.FromBytes(dt)
|
2017-10-02 04:59:34 +00:00
|
|
|
ent := llbOp{Op: op, Digest: dgst, OpMetadata: def.Metadata[dgst].OpMetadata}
|
|
|
|
ops = append(ops, ent)
|
2017-06-12 09:52:25 +00:00
|
|
|
}
|
|
|
|
return ops, nil
|
|
|
|
}
|
2017-07-17 21:24:19 +00:00
|
|
|
|
|
|
|
func writeDot(ops []llbOp, w io.Writer) {
|
2017-10-02 04:59:34 +00:00
|
|
|
// TODO: print OpMetadata
|
2017-07-17 21:24:19 +00:00
|
|
|
fmt.Fprintln(w, "digraph {")
|
|
|
|
defer fmt.Fprintln(w, "}")
|
|
|
|
for _, op := range ops {
|
|
|
|
name, shape := attr(op.Digest, op.Op)
|
|
|
|
fmt.Fprintf(w, " \"%s\" [label=\"%s\" shape=\"%s\"];\n", op.Digest, name, shape)
|
|
|
|
}
|
|
|
|
for _, op := range ops {
|
|
|
|
for i, inp := range op.Op.Inputs {
|
|
|
|
label := ""
|
|
|
|
if eo, ok := op.Op.Op.(*pb.Op_Exec); ok {
|
|
|
|
for _, m := range eo.Exec.Mounts {
|
|
|
|
if int(m.Input) == i && m.Dest != "/" {
|
|
|
|
label = m.Dest
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fmt.Fprintf(w, " \"%s\" -> \"%s\" [label=\"%s\"];\n", inp.Digest, op.Digest, label)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func attr(dgst digest.Digest, op pb.Op) (string, string) {
|
|
|
|
switch op := op.Op.(type) {
|
|
|
|
case *pb.Op_Source:
|
|
|
|
return op.Source.Identifier, "ellipse"
|
|
|
|
case *pb.Op_Exec:
|
|
|
|
return strings.Join(op.Exec.Meta.Args, " "), "box"
|
2017-07-21 17:58:24 +00:00
|
|
|
case *pb.Op_Build:
|
|
|
|
return "build", "box3d"
|
2017-07-17 21:24:19 +00:00
|
|
|
default:
|
|
|
|
return dgst.String(), "plaintext"
|
|
|
|
}
|
|
|
|
}
|