2019-04-13 07:39:06 +08:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
2021-07-14 00:09:30 +08:00
|
|
|
"context"
|
|
|
|
|
2022-12-06 02:57:35 +08:00
|
|
|
"github.com/docker/buildx/builder"
|
2023-04-10 03:20:18 +08:00
|
|
|
"github.com/docker/buildx/util/cobrautil/completion"
|
2019-04-13 07:39:06 +08:00
|
|
|
"github.com/docker/cli/cli"
|
|
|
|
"github.com/docker/cli/cli/command"
|
|
|
|
"github.com/moby/buildkit/util/appcontext"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
type stopOptions struct {
|
2020-04-28 05:37:17 +08:00
|
|
|
builder string
|
2019-04-13 07:39:06 +08:00
|
|
|
}
|
|
|
|
|
2020-04-28 05:37:17 +08:00
|
|
|
func runStop(dockerCli command.Cli, in stopOptions) error {
|
2019-04-13 07:39:06 +08:00
|
|
|
ctx := appcontext.Context()
|
|
|
|
|
2022-12-06 02:57:35 +08:00
|
|
|
b, err := builder.New(dockerCli,
|
|
|
|
builder.WithName(in.builder),
|
|
|
|
builder.WithSkippedValidation(),
|
|
|
|
)
|
2019-04-13 07:39:06 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-08-23 04:11:50 +08:00
|
|
|
nodes, err := b.LoadNodes(ctx)
|
2019-04-13 07:39:06 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-12-06 02:57:35 +08:00
|
|
|
return stop(ctx, nodes)
|
2019-04-13 07:39:06 +08:00
|
|
|
}
|
|
|
|
|
2020-04-28 05:37:17 +08:00
|
|
|
func stopCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
|
2019-04-13 07:39:06 +08:00
|
|
|
var options stopOptions
|
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "stop [NAME]",
|
|
|
|
Short: "Stop builder instance",
|
|
|
|
Args: cli.RequiresMaxArgs(1),
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2020-04-28 05:37:17 +08:00
|
|
|
options.builder = rootOpts.builder
|
|
|
|
if len(args) > 0 {
|
|
|
|
options.builder = args[0]
|
|
|
|
}
|
|
|
|
return runStop(dockerCli, options)
|
2019-04-13 07:39:06 +08:00
|
|
|
},
|
2023-04-11 17:45:59 +08:00
|
|
|
ValidArgsFunction: completion.BuilderNames(dockerCli),
|
2019-04-13 07:39:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
2021-07-14 00:09:30 +08:00
|
|
|
|
2022-12-06 02:57:35 +08:00
|
|
|
func stop(ctx context.Context, nodes []builder.Node) (err error) {
|
|
|
|
for _, node := range nodes {
|
|
|
|
if node.Driver != nil {
|
|
|
|
if err := node.Driver.Stop(ctx, true); err != nil {
|
2021-07-14 00:09:30 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2022-12-06 02:57:35 +08:00
|
|
|
if node.Err != nil {
|
|
|
|
err = node.Err
|
2021-07-14 00:09:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|