-
Notifications
You must be signed in to change notification settings - Fork 509
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
These commands allow working with build records of completed and running builds. Signed-off-by: Tonis Tiigi <[email protected]>
- Loading branch information
1 parent
206bd6c
commit 14a1153
Showing
29 changed files
with
1,252 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package history | ||
|
||
import ( | ||
"context" | ||
"log" | ||
"slices" | ||
|
||
"github.com/docker/buildx/builder" | ||
"github.com/docker/buildx/localstate" | ||
"github.com/docker/buildx/util/cobrautil/completion" | ||
"github.com/docker/buildx/util/confutil" | ||
"github.com/docker/cli/cli/command" | ||
"github.com/pkg/errors" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
type inspectOptions struct { | ||
builder string | ||
ref string | ||
} | ||
|
||
func runInspect(ctx context.Context, dockerCli command.Cli, opts inspectOptions) error { | ||
b, err := builder.New(dockerCli, builder.WithName(opts.builder)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
nodes, err := b.LoadNodes(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
for _, node := range nodes { | ||
if node.Err != nil { | ||
return node.Err | ||
} | ||
} | ||
|
||
recs, err := queryRecords(ctx, opts.ref, nodes) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if len(recs) == 0 { | ||
if opts.ref == "" { | ||
return errors.New("no records found") | ||
} | ||
return errors.Errorf("no record found for ref %q", opts.ref) | ||
} | ||
|
||
if opts.ref == "" { | ||
slices.SortFunc(recs, func(a, b historyRecord) int { | ||
return b.CreatedAt.AsTime().Compare(a.CreatedAt.AsTime()) | ||
}) | ||
} | ||
|
||
rec := &recs[0] | ||
|
||
ls, err := localstate.New(confutil.NewConfig(dockerCli)) | ||
if err != nil { | ||
return err | ||
} | ||
st, _ := ls.ReadRef(rec.node.Builder, rec.node.Name, rec.Ref) | ||
|
||
log.Printf("rec %+v", rec) | ||
log.Printf("st %+v", st) | ||
|
||
// Context | ||
// Dockerfile | ||
// Target | ||
// VCS Repo / Commit | ||
// Platform | ||
|
||
// Started | ||
// Duration | ||
// Number of steps | ||
// Cached steps | ||
// Status | ||
|
||
// build-args | ||
// exporters (image) | ||
|
||
// commands | ||
// error | ||
// materials | ||
|
||
return nil | ||
} | ||
|
||
func inspectCmd(dockerCli command.Cli, rootOpts RootOptions) *cobra.Command { | ||
var options inspectOptions | ||
|
||
cmd := &cobra.Command{ | ||
Use: "inspect [OPTIONS] [REF]", | ||
Short: "Inspect a build", | ||
Args: cobra.MaximumNArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
if len(args) > 0 { | ||
options.ref = args[0] | ||
} | ||
options.builder = *rootOpts.Builder | ||
return runInspect(cmd.Context(), dockerCli, options) | ||
}, | ||
ValidArgsFunction: completion.Disable, | ||
} | ||
|
||
// flags := cmd.Flags() | ||
|
||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
package history | ||
|
||
import ( | ||
"context" | ||
"io" | ||
"os" | ||
"slices" | ||
|
||
"github.com/docker/buildx/builder" | ||
"github.com/docker/buildx/util/cobrautil/completion" | ||
"github.com/docker/buildx/util/progress" | ||
"github.com/docker/cli/cli/command" | ||
controlapi "github.com/moby/buildkit/api/services/control" | ||
"github.com/moby/buildkit/client" | ||
"github.com/moby/buildkit/util/progress/progressui" | ||
"github.com/pkg/errors" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
type logsOptions struct { | ||
builder string | ||
ref string | ||
progress string | ||
} | ||
|
||
func runLogs(ctx context.Context, dockerCli command.Cli, opts logsOptions) error { | ||
b, err := builder.New(dockerCli, builder.WithName(opts.builder)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
nodes, err := b.LoadNodes(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
for _, node := range nodes { | ||
if node.Err != nil { | ||
return node.Err | ||
} | ||
} | ||
|
||
recs, err := queryRecords(ctx, opts.ref, nodes) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if len(recs) == 0 { | ||
if opts.ref == "" { | ||
return errors.New("no records found") | ||
} | ||
return errors.Errorf("no record found for ref %q", opts.ref) | ||
} | ||
|
||
if opts.ref == "" { | ||
slices.SortFunc(recs, func(a, b historyRecord) int { | ||
return b.CreatedAt.AsTime().Compare(a.CreatedAt.AsTime()) | ||
}) | ||
} | ||
|
||
rec := &recs[0] | ||
c, err := rec.node.Driver.Client(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
cl, err := c.ControlClient().Status(ctx, &controlapi.StatusRequest{ | ||
Ref: rec.Ref, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var mode progressui.DisplayMode = progressui.DisplayMode(opts.progress) | ||
if mode == progressui.AutoMode { | ||
mode = progressui.PlainMode | ||
} | ||
printer, err := progress.NewPrinter(context.TODO(), os.Stderr, mode) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
loop0: | ||
for { | ||
select { | ||
case <-ctx.Done(): | ||
cl.CloseSend() | ||
return context.Cause(ctx) | ||
default: | ||
ev, err := cl.Recv() | ||
if err != nil { | ||
if errors.Is(err, io.EOF) { | ||
break loop0 | ||
} | ||
return err | ||
} | ||
printer.Write(client.NewSolveStatus(ev)) | ||
} | ||
} | ||
|
||
return printer.Wait() | ||
} | ||
|
||
func logsCmd(dockerCli command.Cli, rootOpts RootOptions) *cobra.Command { | ||
var options logsOptions | ||
|
||
cmd := &cobra.Command{ | ||
Use: "logs [OPTIONS] [REF]", | ||
Short: "Print the logs of a build", | ||
Args: cobra.MaximumNArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
if len(args) > 0 { | ||
options.ref = args[0] | ||
} | ||
options.builder = *rootOpts.Builder | ||
return runLogs(cmd.Context(), dockerCli, options) | ||
}, | ||
ValidArgsFunction: completion.Disable, | ||
} | ||
|
||
flags := cmd.Flags() | ||
flags.StringVar(&options.progress, "progress", "plain", "Set type of progress output (plain, rawjson, tty)") | ||
|
||
return cmd | ||
} |
Oops, something went wrong.