Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow booting builder after creation #692

Merged
merged 1 commit into from
Aug 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions commands/create.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package commands

import (
"context"
"encoding/csv"
"fmt"
"net/url"
"os"
"strings"
"time"

"github.com/docker/buildx/driver"
"github.com/docker/buildx/store"
Expand All @@ -29,6 +31,7 @@ type createOptions struct {
flags string
configFile string
driverOpts []string
bootstrap bool
// upgrade bool // perform upgrade of the driver
}

Expand Down Expand Up @@ -179,6 +182,21 @@ func runCreate(dockerCli command.Cli, in createOptions, args []string) error {
}
}

ngi := &nginfo{ng: ng}

timeoutCtx, cancel := context.WithTimeout(ctx, 20*time.Second)
defer cancel()

if err = loadNodeGroupData(timeoutCtx, dockerCli, ngi); err != nil {
return err
}

if in.bootstrap {
if _, err = boot(ctx, ngi); err != nil {
return err
}
}

fmt.Printf("%s\n", ng.Name)
return nil
}
Expand Down Expand Up @@ -209,6 +227,7 @@ func createCmd(dockerCli command.Cli) *cobra.Command {
flags.StringVar(&options.configFile, "config", "", "BuildKit config file")
flags.StringArrayVar(&options.platform, "platform", []string{}, "Fixed platforms for current node")
flags.StringArrayVar(&options.driverOpts, "driver-opt", []string{}, "Options for the driver")
flags.BoolVar(&options.bootstrap, "bootstrap", false, "Boot builder after creation")

flags.BoolVar(&options.actionAppend, "append", false, "Append a node to builder instead of changing it")
flags.BoolVar(&options.actionLeave, "leave", false, "Remove a node from builder instead of changing it")
Expand Down
59 changes: 1 addition & 58 deletions commands/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,19 @@ import (
"text/tabwriter"
"time"

"github.com/docker/buildx/build"
"github.com/docker/buildx/driver"
"github.com/docker/buildx/store"
"github.com/docker/buildx/util/platformutil"
"github.com/docker/buildx/util/progress"
"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
"github.com/moby/buildkit/util/appcontext"
specs "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/spf13/cobra"
"golang.org/x/sync/errgroup"
)

type inspectOptions struct {
bootstrap bool
builder string
}

type dinfo struct {
di *build.DriverInfo
info *driver.Info
platforms []specs.Platform
err error
}

type nginfo struct {
ng *store.NodeGroup
drivers []dinfo
err error
}

func runInspect(dockerCli command.Cli, in inspectOptions) error {
ctx := appcontext.Context()

Expand Down Expand Up @@ -82,7 +64,7 @@ func runInspect(dockerCli command.Cli, in inspectOptions) error {
var bootNgi *nginfo
if in.bootstrap {
var ok bool
ok, err = boot(ctx, ngi, dockerCli)
ok, err = boot(ctx, ngi)
if err != nil {
return err
}
Expand Down Expand Up @@ -156,42 +138,3 @@ func inspectCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {

return cmd
}

func boot(ctx context.Context, ngi *nginfo, dockerCli command.Cli) (bool, error) {
toBoot := make([]int, 0, len(ngi.drivers))
for i, d := range ngi.drivers {
if d.err != nil || d.di.Err != nil || d.di.Driver == nil || d.info == nil {
continue
}
if d.info.Status != driver.Running {
toBoot = append(toBoot, i)
}
}
if len(toBoot) == 0 {
return false, nil
}

printer := progress.NewPrinter(context.TODO(), os.Stderr, "auto")

eg, _ := errgroup.WithContext(ctx)
for _, idx := range toBoot {
func(idx int) {
eg.Go(func() error {
pw := progress.WithPrefix(printer, ngi.ng.Nodes[idx].Name, len(toBoot) > 1)
_, err := driver.Boot(ctx, ngi.drivers[idx].di.Driver, pw)
if err != nil {
ngi.drivers[idx].err = err
}
return nil
})
}(idx)
}

err := eg.Wait()
err1 := printer.Wait()
if err == nil {
err = err1
}

return true, err
}
54 changes: 54 additions & 0 deletions commands/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ import (
"github.com/docker/buildx/driver"
"github.com/docker/buildx/store"
"github.com/docker/buildx/util/platformutil"
"github.com/docker/buildx/util/progress"
"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/context/docker"
"github.com/docker/cli/cli/context/kubernetes"
ctxstore "github.com/docker/cli/cli/context/store"
dopts "github.com/docker/cli/opts"
dockerclient "github.com/docker/docker/client"
specs "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"golang.org/x/sync/errgroup"
Expand Down Expand Up @@ -475,3 +477,55 @@ func (a *api) DockerAPI(name string) (dockerclient.APIClient, error) {
}
return clientForEndpoint(a.dockerCli, name)
}

type dinfo struct {
di *build.DriverInfo
info *driver.Info
platforms []specs.Platform
err error
}

type nginfo struct {
ng *store.NodeGroup
drivers []dinfo
err error
}

func boot(ctx context.Context, ngi *nginfo) (bool, error) {
toBoot := make([]int, 0, len(ngi.drivers))
for i, d := range ngi.drivers {
if d.err != nil || d.di.Err != nil || d.di.Driver == nil || d.info == nil {
continue
}
if d.info.Status != driver.Running {
toBoot = append(toBoot, i)
}
}
if len(toBoot) == 0 {
return false, nil
}

printer := progress.NewPrinter(context.TODO(), os.Stderr, "auto")

eg, _ := errgroup.WithContext(ctx)
for _, idx := range toBoot {
func(idx int) {
eg.Go(func() error {
pw := progress.WithPrefix(printer, ngi.ng.Nodes[idx].Name, len(toBoot) > 1)
_, err := driver.Boot(ctx, ngi.drivers[idx].di.Driver, pw)
if err != nil {
ngi.drivers[idx].err = err
}
return nil
})
}(idx)
}

err := eg.Wait()
err1 := printer.Wait()
if err == nil {
err = err1
}

return true, err
}
1 change: 1 addition & 0 deletions docs/reference/buildx_create.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Create a new builder instance
| Name | Description |
| --- | --- |
| [`--append`](#append) | Append a node to builder instead of changing it |
| `--bootstrap` | Boot builder after creation |
| `--builder string` | Override the configured builder instance |
| [`--buildkitd-flags string`](#buildkitd-flags) | Flags for buildkitd daemon |
| [`--config string`](#config) | BuildKit config file |
Expand Down