File: create.go

package info (click to toggle)
docker-buildx 0.19.3%2Bds1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,852 kB
  • sloc: sh: 318; makefile: 73
file content (127 lines) | stat: -rw-r--r-- 3,937 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package commands

import (
	"bytes"
	"context"
	"fmt"

	"github.com/docker/buildx/builder"
	"github.com/docker/buildx/driver"
	"github.com/docker/buildx/store/storeutil"
	"github.com/docker/buildx/util/cobrautil"
	"github.com/docker/buildx/util/cobrautil/completion"
	"github.com/docker/cli/cli"
	"github.com/docker/cli/cli/command"
	"github.com/spf13/cobra"
)

type createOptions struct {
	name                string
	driver              string
	nodeName            string
	platform            []string
	actionAppend        bool
	actionLeave         bool
	use                 bool
	driverOpts          []string
	buildkitdFlags      string
	buildkitdConfigFile string
	bootstrap           bool
	// upgrade      bool // perform upgrade of the driver
}

func runCreate(ctx context.Context, dockerCli command.Cli, in createOptions, args []string) error {
	txn, release, err := storeutil.GetStore(dockerCli)
	if err != nil {
		return err
	}
	// Ensure the file lock gets released no matter what happens.
	defer release()

	if in.actionLeave {
		return builder.Leave(ctx, txn, dockerCli, builder.LeaveOpts{
			Name:     in.name,
			NodeName: in.nodeName,
		})
	}

	var ep string
	if len(args) > 0 {
		ep = args[0]
	}

	b, err := builder.Create(ctx, txn, dockerCli, builder.CreateOpts{
		Name:                in.name,
		Driver:              in.driver,
		NodeName:            in.nodeName,
		Platforms:           in.platform,
		DriverOpts:          in.driverOpts,
		BuildkitdFlags:      in.buildkitdFlags,
		BuildkitdConfigFile: in.buildkitdConfigFile,
		Use:                 in.use,
		Endpoint:            ep,
		Append:              in.actionAppend,
	})
	if err != nil {
		return err
	}

	// The store is no longer used from this point.
	// Release it so we aren't holding the file lock during the boot.
	release()

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

	fmt.Printf("%s\n", b.Name)
	return nil
}

func createCmd(dockerCli command.Cli) *cobra.Command {
	var options createOptions

	var drivers bytes.Buffer
	for _, d := range driver.GetFactories(true) {
		if len(drivers.String()) > 0 {
			drivers.WriteString(", ")
		}
		drivers.WriteString(fmt.Sprintf(`"%s"`, d.Name()))
	}

	cmd := &cobra.Command{
		Use:   "create [OPTIONS] [CONTEXT|ENDPOINT]",
		Short: "Create a new builder instance",
		Args:  cli.RequiresMaxArgs(1),
		RunE: func(cmd *cobra.Command, args []string) error {
			return runCreate(cmd.Context(), dockerCli, options, args)
		},
		ValidArgsFunction: completion.Disable,
	}

	flags := cmd.Flags()

	flags.StringVar(&options.name, "name", "", "Builder instance name")
	flags.StringVar(&options.driver, "driver", "", fmt.Sprintf("Driver to use (available: %s)", drivers.String()))
	flags.StringVar(&options.nodeName, "node", "", "Create/modify node with given name")
	flags.StringArrayVar(&options.platform, "platform", []string{}, "Fixed platforms for current node")
	flags.StringArrayVar(&options.driverOpts, "driver-opt", []string{}, "Options for the driver")
	flags.StringVar(&options.buildkitdFlags, "buildkitd-flags", "", "BuildKit daemon flags")

	// we allow for both "--config" and "--buildkitd-config", although the latter is the recommended way to avoid ambiguity.
	flags.StringVar(&options.buildkitdConfigFile, "buildkitd-config", "", "BuildKit daemon config file")
	flags.StringVar(&options.buildkitdConfigFile, "config", "", "BuildKit daemon config file")
	flags.MarkHidden("config")

	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")
	flags.BoolVar(&options.use, "use", false, "Set the current builder instance")

	// hide builder persistent flag for this command
	cobrautil.HideInheritedFlags(cmd, "builder")

	return cmd
}