-
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.
Allow booting builder after creation
Signed-off-by: CrazyMax <[email protected]>
- Loading branch information
Showing
7 changed files
with
180 additions
and
58 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,146 @@ | ||
package commands | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"time" | ||
|
||
"github.com/docker/buildx/build" | ||
"github.com/docker/buildx/driver" | ||
"github.com/docker/buildx/store" | ||
"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 bootstrapOptions struct { | ||
builder string | ||
} | ||
|
||
func runBootstrap(dockerCli command.Cli, in bootstrapOptions) error { | ||
ctx := appcontext.Context() | ||
|
||
txn, release, err := getStore(dockerCli) | ||
if err != nil { | ||
return err | ||
} | ||
defer release() | ||
|
||
var ng *store.NodeGroup | ||
|
||
if in.builder != "" { | ||
ng, err = getNodeGroup(txn, dockerCli, in.builder) | ||
if err != nil { | ||
return err | ||
} | ||
} else { | ||
ng, err = getCurrentInstance(txn, dockerCli) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
if ng == nil { | ||
ng = &store.NodeGroup{ | ||
Name: "default", | ||
Nodes: []store.Node{{ | ||
Name: "default", | ||
Endpoint: "default", | ||
}}, | ||
} | ||
} | ||
|
||
ngi := &nginfo{ng: ng} | ||
|
||
timeoutCtx, cancel := context.WithTimeout(ctx, 20*time.Second) | ||
defer cancel() | ||
|
||
if err = loadNodeGroupData(timeoutCtx, dockerCli, ngi); err != nil { | ||
return err | ||
} | ||
|
||
var ok bool | ||
if ok, err = boot(ctx, ngi); err != nil { | ||
return err | ||
} else if !ok { | ||
fmt.Printf("%s is already running\n", ng.Name) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func bootstrapCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command { | ||
var options bootstrapOptions | ||
|
||
cmd := &cobra.Command{ | ||
Use: "bootstrap [NAME]", | ||
Short: "Boot builder instance", | ||
Args: cli.RequiresMaxArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
options.builder = rootOpts.builder | ||
if len(args) > 0 { | ||
options.builder = args[0] | ||
} | ||
return runBootstrap(dockerCli, options) | ||
}, | ||
} | ||
|
||
return cmd | ||
} | ||
|
||
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 | ||
} |
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
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
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
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
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,11 @@ | ||
# buildx bootstrap | ||
|
||
``` | ||
docker buildx bootstrap [NAME] | ||
``` | ||
|
||
<!---MARKER_GEN_START--> | ||
Boot builder instance | ||
|
||
|
||
<!---MARKER_GEN_END--> |
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