2019-04-12 08:24:24 +08:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
2021-09-29 21:33:19 +08:00
|
|
|
"bytes"
|
2024-01-05 20:11:53 +08:00
|
|
|
"context"
|
2019-04-12 08:24:24 +08:00
|
|
|
"fmt"
|
|
|
|
|
2022-12-06 02:57:35 +08:00
|
|
|
"github.com/docker/buildx/builder"
|
2019-04-25 10:29:56 +08:00
|
|
|
"github.com/docker/buildx/driver"
|
2021-11-04 12:17:27 +08:00
|
|
|
"github.com/docker/buildx/store/storeutil"
|
2021-11-05 01:52:11 +08:00
|
|
|
"github.com/docker/buildx/util/cobrautil"
|
2023-04-10 03:20:18 +08:00
|
|
|
"github.com/docker/buildx/util/cobrautil/completion"
|
2019-04-12 08:24:24 +08:00
|
|
|
"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
|
2019-04-13 07:39:06 +08:00
|
|
|
use bool
|
2019-07-09 06:06:07 +08:00
|
|
|
flags string
|
2019-08-01 01:54:20 +08:00
|
|
|
configFile string
|
2019-08-01 08:25:25 +08:00
|
|
|
driverOpts []string
|
2021-08-12 14:45:40 +08:00
|
|
|
bootstrap bool
|
2019-04-12 08:24:24 +08:00
|
|
|
// upgrade bool // perform upgrade of the driver
|
|
|
|
}
|
|
|
|
|
2024-01-05 20:11:53 +08:00
|
|
|
func runCreate(ctx context.Context, dockerCli command.Cli, in createOptions, args []string) error {
|
2021-11-04 12:17:27 +08:00
|
|
|
txn, release, err := storeutil.GetStore(dockerCli)
|
2019-04-13 07:39:06 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-09-30 04:53:38 +08:00
|
|
|
// Ensure the file lock gets released no matter what happens.
|
2019-04-13 07:39:06 +08:00
|
|
|
defer release()
|
|
|
|
|
|
|
|
if in.actionLeave {
|
2023-12-04 20:50:37 +08:00
|
|
|
return builder.Leave(ctx, txn, dockerCli, builder.LeaveOpts{
|
|
|
|
Name: in.name,
|
|
|
|
NodeName: in.nodeName,
|
|
|
|
})
|
2019-04-13 07:39:06 +08:00
|
|
|
}
|
|
|
|
|
2023-12-04 20:50:37 +08:00
|
|
|
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,
|
|
|
|
Flags: in.flags,
|
|
|
|
ConfigFile: in.configFile,
|
|
|
|
DriverOpts: in.driverOpts,
|
|
|
|
Use: in.use,
|
|
|
|
Endpoint: ep,
|
|
|
|
Append: in.actionAppend,
|
|
|
|
})
|
2022-12-06 02:57:35 +08:00
|
|
|
if err != nil {
|
2021-08-12 14:45:40 +08:00
|
|
|
return err
|
|
|
|
}
|
2022-12-06 02:57:35 +08:00
|
|
|
|
2023-09-30 04:53:38 +08:00
|
|
|
// The store is no longer used from this point.
|
|
|
|
// Release it so we aren't holding the file lock during the boot.
|
|
|
|
release()
|
|
|
|
|
2021-08-12 14:45:40 +08:00
|
|
|
if in.bootstrap {
|
2022-12-06 02:57:35 +08:00
|
|
|
if _, err = b.Boot(ctx); err != nil {
|
2021-08-12 14:45:40 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-04 20:50:37 +08:00
|
|
|
fmt.Printf("%s\n", b.Name)
|
2019-04-12 08:24:24 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func createCmd(dockerCli command.Cli) *cobra.Command {
|
|
|
|
var options createOptions
|
|
|
|
|
2021-09-29 21:33:19 +08:00
|
|
|
var drivers bytes.Buffer
|
2022-08-16 17:58:23 +08:00
|
|
|
for _, d := range driver.GetFactories(true) {
|
2021-09-29 21:33:19 +08:00
|
|
|
if len(drivers.String()) > 0 {
|
|
|
|
drivers.WriteString(", ")
|
|
|
|
}
|
2021-11-22 17:51:54 +08:00
|
|
|
drivers.WriteString(fmt.Sprintf(`"%s"`, d.Name()))
|
2019-05-26 06:45:20 +08:00
|
|
|
}
|
|
|
|
|
2019-04-12 08:24:24 +08:00
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "create [OPTIONS] [CONTEXT|ENDPOINT]",
|
|
|
|
Short: "Create a new builder instance",
|
|
|
|
Args: cli.RequiresMaxArgs(1),
|
2024-01-20 08:44:30 +08:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2024-01-05 20:11:53 +08:00
|
|
|
return runCreate(cmd.Context(), dockerCli, options, args)
|
2024-01-20 08:44:30 +08:00
|
|
|
},
|
2023-04-10 03:20:18 +08:00
|
|
|
ValidArgsFunction: completion.Disable,
|
2019-04-12 08:24:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
flags := cmd.Flags()
|
|
|
|
|
|
|
|
flags.StringVar(&options.name, "name", "", "Builder instance name")
|
2021-09-29 21:33:19 +08:00
|
|
|
flags.StringVar(&options.driver, "driver", "", fmt.Sprintf("Driver to use (available: %s)", drivers.String()))
|
2019-04-12 08:24:24 +08:00
|
|
|
flags.StringVar(&options.nodeName, "node", "", "Create/modify node with given name")
|
2019-07-09 06:06:07 +08:00
|
|
|
flags.StringVar(&options.flags, "buildkitd-flags", "", "Flags for buildkitd daemon")
|
2019-08-01 01:54:20 +08:00
|
|
|
flags.StringVar(&options.configFile, "config", "", "BuildKit config file")
|
2019-04-12 08:24:24 +08:00
|
|
|
flags.StringArrayVar(&options.platform, "platform", []string{}, "Fixed platforms for current node")
|
2019-08-01 08:25:25 +08:00
|
|
|
flags.StringArrayVar(&options.driverOpts, "driver-opt", []string{}, "Options for the driver")
|
2021-08-12 14:45:40 +08:00
|
|
|
flags.BoolVar(&options.bootstrap, "bootstrap", false, "Boot builder after creation")
|
2019-04-12 08:24:24 +08:00
|
|
|
|
|
|
|
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")
|
2019-04-13 07:39:06 +08:00
|
|
|
flags.BoolVar(&options.use, "use", false, "Set the current builder instance")
|
2019-04-12 08:24:24 +08:00
|
|
|
|
2021-11-05 01:52:11 +08:00
|
|
|
// hide builder persistent flag for this command
|
|
|
|
cobrautil.HideInheritedFlags(cmd, "builder")
|
2019-04-12 08:24:24 +08:00
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|