2019-04-12 08:24:24 +08:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
2019-04-13 07:39:06 +08:00
|
|
|
"context"
|
|
|
|
|
2019-04-25 10:29:56 +08:00
|
|
|
"github.com/docker/buildx/store"
|
2021-11-04 12:17:27 +08:00
|
|
|
"github.com/docker/buildx/store/storeutil"
|
2019-04-13 07:39:06 +08:00
|
|
|
"github.com/docker/cli/cli"
|
2019-04-12 08:24:24 +08:00
|
|
|
"github.com/docker/cli/cli/command"
|
2019-04-13 07:39:06 +08:00
|
|
|
"github.com/moby/buildkit/util/appcontext"
|
2019-04-12 08:24:24 +08:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
type rmOptions struct {
|
2021-07-14 00:09:30 +08:00
|
|
|
builder string
|
|
|
|
keepState bool
|
2019-04-12 08:24:24 +08:00
|
|
|
}
|
|
|
|
|
2020-04-28 05:37:17 +08:00
|
|
|
func runRm(dockerCli command.Cli, in rmOptions) error {
|
2019-04-13 07:39:06 +08:00
|
|
|
ctx := appcontext.Context()
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
defer release()
|
|
|
|
|
2020-04-28 05:37:17 +08:00
|
|
|
if in.builder != "" {
|
2021-11-04 12:17:27 +08:00
|
|
|
ng, err := storeutil.GetNodeGroup(txn, dockerCli, in.builder)
|
2019-04-13 07:39:06 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-07-14 00:09:30 +08:00
|
|
|
err1 := rm(ctx, dockerCli, ng, in.keepState)
|
2019-04-18 09:30:46 +08:00
|
|
|
if err := txn.Remove(ng.Name); err != nil {
|
2019-04-13 07:39:06 +08:00
|
|
|
return err
|
|
|
|
}
|
2019-04-18 09:30:46 +08:00
|
|
|
return err1
|
2019-04-13 07:39:06 +08:00
|
|
|
}
|
|
|
|
|
2021-11-04 12:17:27 +08:00
|
|
|
ng, err := storeutil.GetCurrentInstance(txn, dockerCli)
|
2019-04-13 07:39:06 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if ng != nil {
|
2021-07-14 00:09:30 +08:00
|
|
|
err1 := rm(ctx, dockerCli, ng, in.keepState)
|
2019-04-18 09:30:46 +08:00
|
|
|
if err := txn.Remove(ng.Name); err != nil {
|
2019-04-13 07:39:06 +08:00
|
|
|
return err
|
|
|
|
}
|
2019-04-18 09:30:46 +08:00
|
|
|
return err1
|
2019-04-13 07:39:06 +08:00
|
|
|
}
|
|
|
|
|
2019-04-12 08:24:24 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-28 05:37:17 +08:00
|
|
|
func rmCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
|
2019-04-12 08:24:24 +08:00
|
|
|
var options rmOptions
|
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "rm [NAME]",
|
|
|
|
Short: "Remove a builder instance",
|
2019-04-13 07:39:06 +08:00
|
|
|
Args: cli.RequiresMaxArgs(1),
|
2019-04-12 08:24:24 +08:00
|
|
|
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 runRm(dockerCli, options)
|
2019-04-12 08:24:24 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-07-14 00:09:30 +08:00
|
|
|
flags := cmd.Flags()
|
|
|
|
flags.BoolVar(&options.keepState, "keep-state", false, "Keep BuildKit state")
|
|
|
|
|
2019-04-13 07:39:06 +08:00
|
|
|
return cmd
|
|
|
|
}
|
2019-04-12 08:24:24 +08:00
|
|
|
|
2021-07-14 00:09:30 +08:00
|
|
|
func rm(ctx context.Context, dockerCli command.Cli, ng *store.NodeGroup, keepState bool) error {
|
2019-10-21 14:02:37 +08:00
|
|
|
dis, err := driversForNodeGroup(ctx, dockerCli, ng, "")
|
2019-04-13 07:39:06 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, di := range dis {
|
|
|
|
if di.Driver != nil {
|
|
|
|
if err := di.Driver.Stop(ctx, true); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-07-14 00:09:30 +08:00
|
|
|
if err := di.Driver.Rm(ctx, true, !keepState); err != nil {
|
2019-04-13 07:39:06 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if di.Err != nil {
|
|
|
|
err = di.Err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
2019-04-12 08:24:24 +08:00
|
|
|
}
|