Merge pull request #246 from cpuguy83/override_instance

Add option to build/bake to override instance
pull/270/head
Tõnis Tiigi 5 years ago committed by GitHub
commit af9edb6ba4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -74,7 +74,7 @@ func runBake(dockerCli command.Cli, targets []string, in bakeOptions) error {
contextPathHash, _ := os.Getwd() contextPathHash, _ := os.Getwd()
return buildTargets(ctx, dockerCli, bo, in.progress, contextPathHash) return buildTargets(ctx, dockerCli, bo, in.progress, contextPathHash, in.builder)
} }
func defaultFiles() ([]string, error) { func defaultFiles() ([]string, error) {
@ -99,7 +99,7 @@ func defaultFiles() ([]string, error) {
return out, nil return out, nil
} }
func bakeCmd(dockerCli command.Cli) *cobra.Command { func bakeCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
var options bakeOptions var options bakeOptions
cmd := &cobra.Command{ cmd := &cobra.Command{
@ -119,7 +119,7 @@ func bakeCmd(dockerCli command.Cli) *cobra.Command {
flags.BoolVar(&options.exportPush, "push", false, "Shorthand for --set=*.output=type=registry") flags.BoolVar(&options.exportPush, "push", false, "Shorthand for --set=*.output=type=registry")
flags.BoolVar(&options.exportLoad, "load", false, "Shorthand for --set=*.output=type=docker") flags.BoolVar(&options.exportLoad, "load", false, "Shorthand for --set=*.output=type=docker")
commonFlags(&options.commonOptions, flags) commonBuildFlags(&options.commonOptions, flags)
return cmd return cmd
} }

@ -63,6 +63,7 @@ type buildOptions struct {
} }
type commonOptions struct { type commonOptions struct {
builder string
noCache *bool noCache *bool
progress string progress string
pull *bool pull *bool
@ -191,11 +192,11 @@ func runBuild(dockerCli command.Cli, in buildOptions) error {
contextPathHash = in.contextPath contextPathHash = in.contextPath
} }
return buildTargets(ctx, dockerCli, map[string]build.Options{"default": opts}, in.progress, contextPathHash) return buildTargets(ctx, dockerCli, map[string]build.Options{"default": opts}, in.progress, contextPathHash, in.builder)
} }
func buildTargets(ctx context.Context, dockerCli command.Cli, opts map[string]build.Options, progressMode, contextPathHash string) error { func buildTargets(ctx context.Context, dockerCli command.Cli, opts map[string]build.Options, progressMode, contextPathHash, instance string) error {
dis, err := getDefaultDrivers(ctx, dockerCli, contextPathHash) dis, err := getInstanceOrDefault(ctx, dockerCli, instance, contextPathHash)
if err != nil { if err != nil {
return err return err
} }
@ -208,7 +209,7 @@ func buildTargets(ctx context.Context, dockerCli command.Cli, opts map[string]bu
return err return err
} }
func buildCmd(dockerCli command.Cli) *cobra.Command { func buildCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
var options buildOptions var options buildOptions
cmd := &cobra.Command{ cmd := &cobra.Command{
@ -218,6 +219,7 @@ func buildCmd(dockerCli command.Cli) *cobra.Command {
Args: cli.ExactArgs(1), Args: cli.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
options.contextPath = args[0] options.contextPath = args[0]
options.builder = rootOpts.builder
return runBuild(dockerCli, options) return runBuild(dockerCli, options)
}, },
} }
@ -297,12 +299,12 @@ func buildCmd(dockerCli command.Cli) *cobra.Command {
flags.StringArrayVarP(&options.outputs, "output", "o", []string{}, "Output destination (format: type=local,dest=path)") flags.StringArrayVarP(&options.outputs, "output", "o", []string{}, "Output destination (format: type=local,dest=path)")
commonFlags(&options.commonOptions, flags) commonBuildFlags(&options.commonOptions, flags)
return cmd return cmd
} }
func commonFlags(options *commonOptions, flags *pflag.FlagSet) { func commonBuildFlags(options *commonOptions, flags *pflag.FlagSet) {
flags.Var(flagutil.Tristate(options.noCache), "no-cache", "Do not use cache when building the image") flags.Var(flagutil.Tristate(options.noCache), "no-cache", "Do not use cache when building the image")
flags.StringVar(&options.progress, "progress", "auto", "Set type of progress output (auto, plain, tty). Use plain to show container output") flags.StringVar(&options.progress, "progress", "auto", "Set type of progress output (auto, plain, tty). Use plain to show container output")
flags.Var(flagutil.Tristate(options.pull), "pull", "Always attempt to pull a newer version of the image") flags.Var(flagutil.Tristate(options.pull), "pull", "Always attempt to pull a newer version of the image")

@ -18,6 +18,7 @@ import (
) )
type duOptions struct { type duOptions struct {
builder string
filter opts.FilterOpt filter opts.FilterOpt
verbose bool verbose bool
} }
@ -30,7 +31,7 @@ func runDiskUsage(dockerCli command.Cli, opts duOptions) error {
return err return err
} }
dis, err := getDefaultDrivers(ctx, dockerCli, "") dis, err := getInstanceOrDefault(ctx, dockerCli, opts.builder, "")
if err != nil { if err != nil {
return err return err
} }
@ -97,7 +98,7 @@ func runDiskUsage(dockerCli command.Cli, opts duOptions) error {
return nil return nil
} }
func duCmd(dockerCli command.Cli) *cobra.Command { func duCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
options := duOptions{filter: opts.NewFilterOpt()} options := duOptions{filter: opts.NewFilterOpt()}
cmd := &cobra.Command{ cmd := &cobra.Command{
@ -105,6 +106,7 @@ func duCmd(dockerCli command.Cli) *cobra.Command {
Short: "Disk usage", Short: "Disk usage",
Args: cli.NoArgs, Args: cli.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
options.builder = rootOpts.builder
return runDiskUsage(dockerCli, options) return runDiskUsage(dockerCli, options)
}, },
Annotations: map[string]string{"version": "1.00"}, Annotations: map[string]string{"version": "1.00"},

@ -23,6 +23,7 @@ import (
type inspectOptions struct { type inspectOptions struct {
bootstrap bool bootstrap bool
builder string
} }
type dinfo struct { type dinfo struct {
@ -38,7 +39,7 @@ type nginfo struct {
err error err error
} }
func runInspect(dockerCli command.Cli, in inspectOptions, args []string) error { func runInspect(dockerCli command.Cli, in inspectOptions) error {
ctx := appcontext.Context() ctx := appcontext.Context()
txn, release, err := getStore(dockerCli) txn, release, err := getStore(dockerCli)
@ -49,8 +50,8 @@ func runInspect(dockerCli command.Cli, in inspectOptions, args []string) error {
var ng *store.NodeGroup var ng *store.NodeGroup
if len(args) > 0 { if in.builder != "" {
ng, err = getNodeGroup(txn, dockerCli, args[0]) ng, err = getNodeGroup(txn, dockerCli, in.builder)
if err != nil { if err != nil {
return err return err
} }
@ -127,7 +128,7 @@ func runInspect(dockerCli command.Cli, in inspectOptions, args []string) error {
return nil return nil
} }
func inspectCmd(dockerCli command.Cli) *cobra.Command { func inspectCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
var options inspectOptions var options inspectOptions
cmd := &cobra.Command{ cmd := &cobra.Command{
@ -135,7 +136,11 @@ func inspectCmd(dockerCli command.Cli) *cobra.Command {
Short: "Inspect current builder instance", Short: "Inspect current builder instance",
Args: cli.RequiresMaxArgs(1), Args: cli.RequiresMaxArgs(1),
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
return runInspect(dockerCli, options, args) options.builder = rootOpts.builder
if len(args) > 0 {
options.builder = args[0]
}
return runInspect(dockerCli, options)
}, },
} }

@ -21,6 +21,7 @@ import (
) )
type pruneOptions struct { type pruneOptions struct {
builder string
all bool all bool
filter opts.FilterOpt filter opts.FilterOpt
keepStorage opts.MemBytes keepStorage opts.MemBytes
@ -53,7 +54,7 @@ func runPrune(dockerCli command.Cli, opts pruneOptions) error {
return nil return nil
} }
dis, err := getDefaultDrivers(ctx, dockerCli, "") dis, err := getInstanceOrDefault(ctx, dockerCli, opts.builder, "")
if err != nil { if err != nil {
return err return err
} }
@ -123,7 +124,7 @@ func runPrune(dockerCli command.Cli, opts pruneOptions) error {
return nil return nil
} }
func pruneCmd(dockerCli command.Cli) *cobra.Command { func pruneCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
options := pruneOptions{filter: opts.NewFilterOpt()} options := pruneOptions{filter: opts.NewFilterOpt()}
cmd := &cobra.Command{ cmd := &cobra.Command{
@ -131,6 +132,7 @@ func pruneCmd(dockerCli command.Cli) *cobra.Command {
Short: "Remove build cache ", Short: "Remove build cache ",
Args: cli.NoArgs, Args: cli.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
options.builder = rootOpts.builder
return runPrune(dockerCli, options) return runPrune(dockerCli, options)
}, },
Annotations: map[string]string{"version": "1.00"}, Annotations: map[string]string{"version": "1.00"},

@ -11,9 +11,10 @@ import (
) )
type rmOptions struct { type rmOptions struct {
builder string
} }
func runRm(dockerCli command.Cli, in rmOptions, args []string) error { func runRm(dockerCli command.Cli, in rmOptions) error {
ctx := appcontext.Context() ctx := appcontext.Context()
txn, release, err := getStore(dockerCli) txn, release, err := getStore(dockerCli)
@ -22,8 +23,8 @@ func runRm(dockerCli command.Cli, in rmOptions, args []string) error {
} }
defer release() defer release()
if len(args) > 0 { if in.builder != "" {
ng, err := getNodeGroup(txn, dockerCli, args[0]) ng, err := getNodeGroup(txn, dockerCli, in.builder)
if err != nil { if err != nil {
return err return err
} }
@ -49,7 +50,7 @@ func runRm(dockerCli command.Cli, in rmOptions, args []string) error {
return nil return nil
} }
func rmCmd(dockerCli command.Cli) *cobra.Command { func rmCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
var options rmOptions var options rmOptions
cmd := &cobra.Command{ cmd := &cobra.Command{
@ -57,7 +58,11 @@ func rmCmd(dockerCli command.Cli) *cobra.Command {
Short: "Remove a builder instance", Short: "Remove a builder instance",
Args: cli.RequiresMaxArgs(1), Args: cli.RequiresMaxArgs(1),
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
return runRm(dockerCli, options, args) options.builder = rootOpts.builder
if len(args) > 0 {
options.builder = args[0]
}
return runRm(dockerCli, options)
}, },
} }

@ -1,10 +1,13 @@
package commands package commands
import ( import (
"os"
imagetoolscmd "github.com/docker/buildx/commands/imagetools" imagetoolscmd "github.com/docker/buildx/commands/imagetools"
"github.com/docker/cli/cli-plugins/plugin" "github.com/docker/cli/cli-plugins/plugin"
"github.com/docker/cli/cli/command" "github.com/docker/cli/cli/command"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/pflag"
) )
func NewRootCmd(name string, isPlugin bool, dockerCli command.Cli) *cobra.Command { func NewRootCmd(name string, isPlugin bool, dockerCli command.Cli) *cobra.Command {
@ -22,21 +25,32 @@ func NewRootCmd(name string, isPlugin bool, dockerCli command.Cli) *cobra.Comman
return cmd return cmd
} }
type rootOptions struct {
builder string
}
func addCommands(cmd *cobra.Command, dockerCli command.Cli) { func addCommands(cmd *cobra.Command, dockerCli command.Cli) {
opts := &rootOptions{}
rootFlags(opts, cmd.PersistentFlags())
cmd.AddCommand( cmd.AddCommand(
buildCmd(dockerCli), buildCmd(dockerCli, opts),
bakeCmd(dockerCli), bakeCmd(dockerCli, opts),
createCmd(dockerCli), createCmd(dockerCli),
rmCmd(dockerCli), rmCmd(dockerCli, opts),
lsCmd(dockerCli), lsCmd(dockerCli),
useCmd(dockerCli), useCmd(dockerCli, opts),
inspectCmd(dockerCli), inspectCmd(dockerCli, opts),
stopCmd(dockerCli), stopCmd(dockerCli, opts),
installCmd(dockerCli), installCmd(dockerCli),
uninstallCmd(dockerCli), uninstallCmd(dockerCli),
versionCmd(dockerCli), versionCmd(dockerCli),
pruneCmd(dockerCli), pruneCmd(dockerCli, opts),
duCmd(dockerCli), duCmd(dockerCli, opts),
imagetoolscmd.RootCmd(dockerCli), imagetoolscmd.RootCmd(dockerCli),
) )
} }
func rootFlags(options *rootOptions, flags *pflag.FlagSet) {
flags.StringVar(&options.builder, "builder", os.Getenv("BUILDX_BUILDER"), "Override the configured builder instance")
}

@ -8,9 +8,10 @@ import (
) )
type stopOptions struct { type stopOptions struct {
builder string
} }
func runStop(dockerCli command.Cli, in stopOptions, args []string) error { func runStop(dockerCli command.Cli, in stopOptions) error {
ctx := appcontext.Context() ctx := appcontext.Context()
txn, release, err := getStore(dockerCli) txn, release, err := getStore(dockerCli)
@ -19,8 +20,8 @@ func runStop(dockerCli command.Cli, in stopOptions, args []string) error {
} }
defer release() defer release()
if len(args) > 0 { if in.builder != "" {
ng, err := getNodeGroup(txn, dockerCli, args[0]) ng, err := getNodeGroup(txn, dockerCli, in.builder)
if err != nil { if err != nil {
return err return err
} }
@ -41,7 +42,7 @@ func runStop(dockerCli command.Cli, in stopOptions, args []string) error {
return stopCurrent(ctx, dockerCli, false) return stopCurrent(ctx, dockerCli, false)
} }
func stopCmd(dockerCli command.Cli) *cobra.Command { func stopCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
var options stopOptions var options stopOptions
cmd := &cobra.Command{ cmd := &cobra.Command{
@ -49,7 +50,11 @@ func stopCmd(dockerCli command.Cli) *cobra.Command {
Short: "Stop builder instance", Short: "Stop builder instance",
Args: cli.RequiresMaxArgs(1), Args: cli.RequiresMaxArgs(1),
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
return runStop(dockerCli, options, args) options.builder = rootOpts.builder
if len(args) > 0 {
options.builder = args[0]
}
return runStop(dockerCli, options)
}, },
} }

@ -12,21 +12,22 @@ import (
type useOptions struct { type useOptions struct {
isGlobal bool isGlobal bool
isDefault bool isDefault bool
builder string
} }
func runUse(dockerCli command.Cli, in useOptions, name string) error { func runUse(dockerCli command.Cli, in useOptions) error {
txn, release, err := getStore(dockerCli) txn, release, err := getStore(dockerCli)
if err != nil { if err != nil {
return err return err
} }
defer release() defer release()
if _, err := txn.NodeGroupByName(name); err != nil { if _, err := txn.NodeGroupByName(in.builder); err != nil {
if os.IsNotExist(errors.Cause(err)) { if os.IsNotExist(errors.Cause(err)) {
if name == "default" && name != dockerCli.CurrentContext() { if in.builder == "default" && in.builder != dockerCli.CurrentContext() {
return errors.Errorf("run `docker context use default` to switch to default context") return errors.Errorf("run `docker context use default` to switch to default context")
} }
if name == "default" || name == dockerCli.CurrentContext() { if in.builder == "default" || in.builder == dockerCli.CurrentContext() {
ep, err := getCurrentEndpoint(dockerCli) ep, err := getCurrentEndpoint(dockerCli)
if err != nil { if err != nil {
return err return err
@ -41,35 +42,39 @@ func runUse(dockerCli command.Cli, in useOptions, name string) error {
return err return err
} }
for _, l := range list { for _, l := range list {
if l.Name == name { if l.Name == in.builder {
return errors.Errorf("run `docker context use %s` to switch to context %s", name, name) return errors.Errorf("run `docker context use %s` to switch to context %s", in.builder, in.builder)
} }
} }
} }
return errors.Wrapf(err, "failed to find instance %q", name) return errors.Wrapf(err, "failed to find instance %q", in.builder)
} }
ep, err := getCurrentEndpoint(dockerCli) ep, err := getCurrentEndpoint(dockerCli)
if err != nil { if err != nil {
return err return err
} }
if err := txn.SetCurrent(ep, name, in.isGlobal, in.isDefault); err != nil { if err := txn.SetCurrent(ep, in.builder, in.isGlobal, in.isDefault); err != nil {
return err return err
} }
return nil return nil
} }
func useCmd(dockerCli command.Cli) *cobra.Command { func useCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
var options useOptions var options useOptions
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "use [OPTIONS] NAME", Use: "use [OPTIONS] NAME",
Short: "Set the current builder instance", Short: "Set the current builder instance",
Args: cli.ExactArgs(1), Args: cli.RequiresMaxArgs(1),
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
return runUse(dockerCli, options, args[0]) options.builder = rootOpts.builder
if len(args) > 0 {
options.builder = args[0]
}
return runUse(dockerCli, options)
}, },
} }

@ -247,6 +247,27 @@ func clientForEndpoint(dockerCli command.Cli, name string) (dockerclient.APIClie
return dockerclient.NewClientWithOpts(clientOpts...) return dockerclient.NewClientWithOpts(clientOpts...)
} }
func getInstanceOrDefault(ctx context.Context, dockerCli command.Cli, instance, contextPathHash string) ([]build.DriverInfo, error) {
if instance != "" {
return getInstanceByName(ctx, dockerCli, instance, contextPathHash)
}
return getDefaultDrivers(ctx, dockerCli, contextPathHash)
}
func getInstanceByName(ctx context.Context, dockerCli command.Cli, instance, contextPathHash string) ([]build.DriverInfo, error) {
txn, release, err := getStore(dockerCli)
if err != nil {
return nil, err
}
defer release()
ng, err := txn.NodeGroupByName(instance)
if err != nil {
return nil, err
}
return driversForNodeGroup(ctx, dockerCli, ng, contextPathHash)
}
// getDefaultDrivers returns drivers based on current cli config // getDefaultDrivers returns drivers based on current cli config
func getDefaultDrivers(ctx context.Context, dockerCli command.Cli, contextPathHash string) ([]build.DriverInfo, error) { func getDefaultDrivers(ctx context.Context, dockerCli command.Cli, contextPathHash string) ([]build.DriverInfo, error) {
txn, release, err := getStore(dockerCli) txn, release, err := getStore(dockerCli)

Loading…
Cancel
Save