From af0090e4346b7cca58c0ec685609e03dab9ab5b7 Mon Sep 17 00:00:00 2001 From: CrazyMax <1951866+crazy-max@users.noreply.github.com> Date: Tue, 8 Jul 2025 10:20:49 +0200 Subject: [PATCH] history: bootstrap builder Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com> --- commands/history/export.go | 12 +--------- commands/history/inspect.go | 13 +---------- commands/history/inspect_attachment.go | 13 +---------- commands/history/logs.go | 13 +---------- commands/history/ls.go | 13 +---------- commands/history/open.go | 13 +---------- commands/history/rm.go | 13 +---------- commands/history/trace.go | 12 +--------- commands/history/utils.go | 26 +++++++++++++++++++++ tests/history.go | 31 ++++++++++++++++++++++++++ 10 files changed, 65 insertions(+), 94 deletions(-) diff --git a/commands/history/export.go b/commands/history/export.go index af8149a7f..ee56d92c1 100644 --- a/commands/history/export.go +++ b/commands/history/export.go @@ -28,21 +28,11 @@ type exportOptions struct { } func runExport(ctx context.Context, dockerCli command.Cli, opts exportOptions) error { - b, err := builder.New(dockerCli, builder.WithName(opts.builder)) + nodes, err := loadNodes(ctx, dockerCli, opts.builder) if err != nil { return err } - nodes, err := b.LoadNodes(ctx, builder.WithData()) - if err != nil { - return err - } - for _, node := range nodes { - if node.Err != nil { - return node.Err - } - } - if len(opts.refs) == 0 { opts.refs = []string{""} } diff --git a/commands/history/inspect.go b/commands/history/inspect.go index 545558d01..f42633da4 100644 --- a/commands/history/inspect.go +++ b/commands/history/inspect.go @@ -20,7 +20,6 @@ import ( "github.com/containerd/containerd/v2/core/content/proxy" "github.com/containerd/containerd/v2/core/images" "github.com/containerd/platforms" - "github.com/docker/buildx/builder" "github.com/docker/buildx/localstate" "github.com/docker/buildx/util/cobrautil/completion" "github.com/docker/buildx/util/confutil" @@ -158,21 +157,11 @@ func readAttr[T any](attrs map[string]string, k string, dest *T, f func(v string } func runInspect(ctx context.Context, dockerCli command.Cli, opts inspectOptions) error { - b, err := builder.New(dockerCli, builder.WithName(opts.builder)) + nodes, err := loadNodes(ctx, dockerCli, 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, nil) if err != nil { return err diff --git a/commands/history/inspect_attachment.go b/commands/history/inspect_attachment.go index 52fd683b9..7f7e916ee 100644 --- a/commands/history/inspect_attachment.go +++ b/commands/history/inspect_attachment.go @@ -6,7 +6,6 @@ import ( "github.com/containerd/containerd/v2/core/content/proxy" "github.com/containerd/platforms" - "github.com/docker/buildx/builder" "github.com/docker/buildx/util/cobrautil/completion" "github.com/docker/cli/cli/command" intoto "github.com/in-toto/in-toto-golang/in_toto" @@ -27,21 +26,11 @@ type attachmentOptions struct { } func runAttachment(ctx context.Context, dockerCli command.Cli, opts attachmentOptions) error { - b, err := builder.New(dockerCli, builder.WithName(opts.builder)) + nodes, err := loadNodes(ctx, dockerCli, 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, nil) if err != nil { return err diff --git a/commands/history/logs.go b/commands/history/logs.go index 458ee384f..c1b368f15 100644 --- a/commands/history/logs.go +++ b/commands/history/logs.go @@ -5,7 +5,6 @@ import ( "io" "os" - "github.com/docker/buildx/builder" "github.com/docker/buildx/util/cobrautil/completion" "github.com/docker/buildx/util/progress" "github.com/docker/cli/cli/command" @@ -23,21 +22,11 @@ type logsOptions struct { } func runLogs(ctx context.Context, dockerCli command.Cli, opts logsOptions) error { - b, err := builder.New(dockerCli, builder.WithName(opts.builder)) + nodes, err := loadNodes(ctx, dockerCli, 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, nil) if err != nil { return err diff --git a/commands/history/ls.go b/commands/history/ls.go index f6135164f..2a5195aad 100644 --- a/commands/history/ls.go +++ b/commands/history/ls.go @@ -10,7 +10,6 @@ import ( "time" "github.com/containerd/console" - "github.com/docker/buildx/builder" "github.com/docker/buildx/localstate" "github.com/docker/buildx/util/cobrautil/completion" "github.com/docker/buildx/util/confutil" @@ -47,21 +46,11 @@ type lsOptions struct { } func runLs(ctx context.Context, dockerCli command.Cli, opts lsOptions) error { - b, err := builder.New(dockerCli, builder.WithName(opts.builder)) + nodes, err := loadNodes(ctx, dockerCli, 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 - } - } - queryOptions := &queryOptions{} if opts.local { diff --git a/commands/history/open.go b/commands/history/open.go index aa047b29f..694bec35e 100644 --- a/commands/history/open.go +++ b/commands/history/open.go @@ -4,7 +4,6 @@ import ( "context" "fmt" - "github.com/docker/buildx/builder" "github.com/docker/buildx/util/cobrautil/completion" "github.com/docker/buildx/util/desktop" "github.com/docker/cli/cli/command" @@ -19,21 +18,11 @@ type openOptions struct { } func runOpen(ctx context.Context, dockerCli command.Cli, opts openOptions) error { - b, err := builder.New(dockerCli, builder.WithName(opts.builder)) + nodes, err := loadNodes(ctx, dockerCli, 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, nil) if err != nil { return err diff --git a/commands/history/rm.go b/commands/history/rm.go index 4f5338d95..8d04c9d0e 100644 --- a/commands/history/rm.go +++ b/commands/history/rm.go @@ -4,7 +4,6 @@ import ( "context" "io" - "github.com/docker/buildx/builder" "github.com/docker/buildx/util/cobrautil/completion" "github.com/docker/cli/cli/command" "github.com/hashicorp/go-multierror" @@ -21,21 +20,11 @@ type rmOptions struct { } func runRm(ctx context.Context, dockerCli command.Cli, opts rmOptions) error { - b, err := builder.New(dockerCli, builder.WithName(opts.builder)) + nodes, err := loadNodes(ctx, dockerCli, 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 - } - } - errs := make([][]error, len(opts.refs)) for i := range errs { errs[i] = make([]error, len(nodes)) diff --git a/commands/history/trace.go b/commands/history/trace.go index b841ce357..9e60765f1 100644 --- a/commands/history/trace.go +++ b/commands/history/trace.go @@ -120,21 +120,11 @@ func loadTrace(ctx context.Context, ref string, nodes []builder.Node) (string, [ } func runTrace(ctx context.Context, dockerCli command.Cli, opts traceOptions) error { - b, err := builder.New(dockerCli, builder.WithName(opts.builder)) + nodes, err := loadNodes(ctx, dockerCli, 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 - } - } - traceID, data, err := loadTrace(ctx, opts.ref, nodes) if err != nil { return err diff --git a/commands/history/utils.go b/commands/history/utils.go index b0af0bf9b..7925664cf 100644 --- a/commands/history/utils.go +++ b/commands/history/utils.go @@ -16,6 +16,7 @@ import ( "github.com/docker/buildx/build" "github.com/docker/buildx/builder" "github.com/docker/buildx/localstate" + "github.com/docker/cli/cli/command" controlapi "github.com/moby/buildkit/api/services/control" "github.com/moby/buildkit/util/gitutil" "github.com/pkg/errors" @@ -421,3 +422,28 @@ func cutAny(s string, seps ...string) (before, after, sep string, found bool) { } return s, "", "", false } + +func loadNodes(ctx context.Context, dockerCli command.Cli, builderName string) ([]builder.Node, error) { + b, err := builder.New(dockerCli, builder.WithName(builderName)) + if err != nil { + return nil, err + } + nodes, err := b.LoadNodes(ctx, builder.WithData()) + if err != nil { + return nil, err + } + if ok, err := b.Boot(ctx); err != nil { + return nil, err + } else if ok { + nodes, err = b.LoadNodes(ctx, builder.WithData()) + if err != nil { + return nil, err + } + } + for _, node := range nodes { + if node.Err != nil { + return nil, node.Err + } + } + return nodes, nil +} diff --git a/tests/history.go b/tests/history.go index 6ced64cfe..4cb100a18 100644 --- a/tests/history.go +++ b/tests/history.go @@ -19,6 +19,7 @@ var historyTests = []func(t *testing.T, sb integration.Sandbox){ testHistoryInspect, testHistoryLs, testHistoryRm, + testHistoryLsStoppedBuilder, } func testHistoryExport(t *testing.T, sb integration.Sandbox) { @@ -105,6 +106,36 @@ func testHistoryRm(t *testing.T, sb integration.Sandbox) { require.NoError(t, err, string(out)) } +func testHistoryLsStoppedBuilder(t *testing.T, sb integration.Sandbox) { + if !isDockerContainerWorker(sb) { + t.Skip("only testing with docker-container worker") + } + + var builderName string + t.Cleanup(func() { + if builderName == "" { + return + } + out, err := rmCmd(sb, withArgs(builderName)) + require.NoError(t, err, out) + }) + + out, err := createCmd(sb, withArgs("--driver", "docker-container")) + require.NoError(t, err, out) + builderName = strings.TrimSpace(out) + + ref := buildTestProject(t, sb) + require.NotEmpty(t, ref.Ref) + + cmd := buildxCmd(sb, withArgs("stop", builderName)) + bout, err := cmd.CombinedOutput() + require.NoError(t, err, string(bout)) + + cmd = buildxCmd(sb, withArgs("history", "ls", "--builder="+builderName, "--filter=ref="+ref.Ref, "--format=json")) + bout, err = cmd.CombinedOutput() + require.NoError(t, err, string(bout)) +} + type buildRef struct { Builder string Node string