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()
return buildTargets(ctx, dockerCli, bo, in.progress, contextPathHash)
return buildTargets(ctx, dockerCli, bo, in.progress, contextPathHash, in.builder)
}
func defaultFiles() ([]string, error) {
@ -99,7 +99,7 @@ func defaultFiles() ([]string, error) {
return out, nil
}
func bakeCmd(dockerCli command.Cli) *cobra.Command {
func bakeCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
var options bakeOptions
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.exportLoad, "load", false, "Shorthand for --set=*.output=type=docker")
commonFlags(&options.commonOptions, flags)
commonBuildFlags(&options.commonOptions, flags)
return cmd
}

@ -63,6 +63,7 @@ type buildOptions struct {
}
type commonOptions struct {
builder string
noCache *bool
progress string
pull *bool
@ -191,11 +192,11 @@ func runBuild(dockerCli command.Cli, in buildOptions) error {
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 {
dis, err := getDefaultDrivers(ctx, dockerCli, contextPathHash)
func buildTargets(ctx context.Context, dockerCli command.Cli, opts map[string]build.Options, progressMode, contextPathHash, instance string) error {
dis, err := getInstanceOrDefault(ctx, dockerCli, instance, contextPathHash)
if err != nil {
return err
}
@ -208,7 +209,7 @@ func buildTargets(ctx context.Context, dockerCli command.Cli, opts map[string]bu
return err
}
func buildCmd(dockerCli command.Cli) *cobra.Command {
func buildCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
var options buildOptions
cmd := &cobra.Command{
@ -218,6 +219,7 @@ func buildCmd(dockerCli command.Cli) *cobra.Command {
Args: cli.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
options.contextPath = args[0]
options.builder = rootOpts.builder
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)")
commonFlags(&options.commonOptions, flags)
commonBuildFlags(&options.commonOptions, flags)
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.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")

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

@ -23,6 +23,7 @@ import (
type inspectOptions struct {
bootstrap bool
builder string
}
type dinfo struct {
@ -38,7 +39,7 @@ type nginfo struct {
err error
}
func runInspect(dockerCli command.Cli, in inspectOptions, args []string) error {
func runInspect(dockerCli command.Cli, in inspectOptions) error {
ctx := appcontext.Context()
txn, release, err := getStore(dockerCli)
@ -49,8 +50,8 @@ func runInspect(dockerCli command.Cli, in inspectOptions, args []string) error {
var ng *store.NodeGroup
if len(args) > 0 {
ng, err = getNodeGroup(txn, dockerCli, args[0])
if in.builder != "" {
ng, err = getNodeGroup(txn, dockerCli, in.builder)
if err != nil {
return err
}
@ -127,7 +128,7 @@ func runInspect(dockerCli command.Cli, in inspectOptions, args []string) error {
return nil
}
func inspectCmd(dockerCli command.Cli) *cobra.Command {
func inspectCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
var options inspectOptions
cmd := &cobra.Command{
@ -135,7 +136,11 @@ func inspectCmd(dockerCli command.Cli) *cobra.Command {
Short: "Inspect current builder instance",
Args: cli.RequiresMaxArgs(1),
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 {
builder string
all bool
filter opts.FilterOpt
keepStorage opts.MemBytes
@ -53,7 +54,7 @@ func runPrune(dockerCli command.Cli, opts pruneOptions) error {
return nil
}
dis, err := getDefaultDrivers(ctx, dockerCli, "")
dis, err := getInstanceOrDefault(ctx, dockerCli, opts.builder, "")
if err != nil {
return err
}
@ -123,7 +124,7 @@ func runPrune(dockerCli command.Cli, opts pruneOptions) error {
return nil
}
func pruneCmd(dockerCli command.Cli) *cobra.Command {
func pruneCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
options := pruneOptions{filter: opts.NewFilterOpt()}
cmd := &cobra.Command{
@ -131,6 +132,7 @@ func pruneCmd(dockerCli command.Cli) *cobra.Command {
Short: "Remove build cache ",
Args: cli.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
options.builder = rootOpts.builder
return runPrune(dockerCli, options)
},
Annotations: map[string]string{"version": "1.00"},

@ -11,9 +11,10 @@ import (
)
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()
txn, release, err := getStore(dockerCli)
@ -22,8 +23,8 @@ func runRm(dockerCli command.Cli, in rmOptions, args []string) error {
}
defer release()
if len(args) > 0 {
ng, err := getNodeGroup(txn, dockerCli, args[0])
if in.builder != "" {
ng, err := getNodeGroup(txn, dockerCli, in.builder)
if err != nil {
return err
}
@ -49,7 +50,7 @@ func runRm(dockerCli command.Cli, in rmOptions, args []string) error {
return nil
}
func rmCmd(dockerCli command.Cli) *cobra.Command {
func rmCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
var options rmOptions
cmd := &cobra.Command{
@ -57,7 +58,11 @@ func rmCmd(dockerCli command.Cli) *cobra.Command {
Short: "Remove a builder instance",
Args: cli.RequiresMaxArgs(1),
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
import (
"os"
imagetoolscmd "github.com/docker/buildx/commands/imagetools"
"github.com/docker/cli/cli-plugins/plugin"
"github.com/docker/cli/cli/command"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
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
}
type rootOptions struct {
builder string
}
func addCommands(cmd *cobra.Command, dockerCli command.Cli) {
opts := &rootOptions{}
rootFlags(opts, cmd.PersistentFlags())
cmd.AddCommand(
buildCmd(dockerCli),
bakeCmd(dockerCli),
buildCmd(dockerCli, opts),
bakeCmd(dockerCli, opts),
createCmd(dockerCli),
rmCmd(dockerCli),
rmCmd(dockerCli, opts),
lsCmd(dockerCli),
useCmd(dockerCli),
inspectCmd(dockerCli),
stopCmd(dockerCli),
useCmd(dockerCli, opts),
inspectCmd(dockerCli, opts),
stopCmd(dockerCli, opts),
installCmd(dockerCli),
uninstallCmd(dockerCli),
versionCmd(dockerCli),
pruneCmd(dockerCli),
duCmd(dockerCli),
pruneCmd(dockerCli, opts),
duCmd(dockerCli, opts),
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 {
builder string
}
func runStop(dockerCli command.Cli, in stopOptions, args []string) error {
func runStop(dockerCli command.Cli, in stopOptions) error {
ctx := appcontext.Context()
txn, release, err := getStore(dockerCli)
@ -19,8 +20,8 @@ func runStop(dockerCli command.Cli, in stopOptions, args []string) error {
}
defer release()
if len(args) > 0 {
ng, err := getNodeGroup(txn, dockerCli, args[0])
if in.builder != "" {
ng, err := getNodeGroup(txn, dockerCli, in.builder)
if err != nil {
return err
}
@ -41,7 +42,7 @@ func runStop(dockerCli command.Cli, in stopOptions, args []string) error {
return stopCurrent(ctx, dockerCli, false)
}
func stopCmd(dockerCli command.Cli) *cobra.Command {
func stopCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
var options stopOptions
cmd := &cobra.Command{
@ -49,7 +50,11 @@ func stopCmd(dockerCli command.Cli) *cobra.Command {
Short: "Stop builder instance",
Args: cli.RequiresMaxArgs(1),
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 {
isGlobal 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)
if err != nil {
return err
}
defer release()
if _, err := txn.NodeGroupByName(name); err != nil {
if _, err := txn.NodeGroupByName(in.builder); err != nil {
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")
}
if name == "default" || name == dockerCli.CurrentContext() {
if in.builder == "default" || in.builder == dockerCli.CurrentContext() {
ep, err := getCurrentEndpoint(dockerCli)
if err != nil {
return err
@ -41,35 +42,39 @@ func runUse(dockerCli command.Cli, in useOptions, name string) error {
return err
}
for _, l := range list {
if l.Name == name {
return errors.Errorf("run `docker context use %s` to switch to context %s", name, name)
if l.Name == in.builder {
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)
if err != nil {
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 nil
}
func useCmd(dockerCli command.Cli) *cobra.Command {
func useCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
var options useOptions
cmd := &cobra.Command{
Use: "use [OPTIONS] NAME",
Short: "Set the current builder instance",
Args: cli.ExactArgs(1),
Args: cli.RequiresMaxArgs(1),
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...)
}
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
func getDefaultDrivers(ctx context.Context, dockerCli command.Cli, contextPathHash string) ([]build.DriverInfo, error) {
txn, release, err := getStore(dockerCli)

Loading…
Cancel
Save