2019-04-12 08:24:24 +08:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
2019-04-13 07:39:06 +08:00
|
|
|
"context"
|
|
|
|
|
|
|
|
"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"
|
2019-04-13 07:39:06 +08:00
|
|
|
"github.com/tonistiigi/buildx/store"
|
2019-04-12 08:24:24 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type rmOptions struct {
|
|
|
|
}
|
|
|
|
|
2019-04-13 07:39:06 +08:00
|
|
|
func runRm(dockerCli command.Cli, in rmOptions, args []string) error {
|
|
|
|
ctx := appcontext.Context()
|
|
|
|
|
|
|
|
txn, release, err := getStore(dockerCli)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer release()
|
|
|
|
|
|
|
|
if len(args) > 0 {
|
|
|
|
ng, err := getNodeGroup(txn, dockerCli, args[0])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-04-18 09:30:46 +08:00
|
|
|
err1 := stop(ctx, dockerCli, ng, true)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
ng, err := getCurrentInstance(txn, dockerCli)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if ng != nil {
|
2019-04-18 09:30:46 +08:00
|
|
|
err1 := stop(ctx, dockerCli, ng, true)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func rmCmd(dockerCli command.Cli) *cobra.Command {
|
|
|
|
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 {
|
2019-04-13 07:39:06 +08:00
|
|
|
return runRm(dockerCli, options, args)
|
2019-04-12 08:24:24 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-04-13 07:39:06 +08:00
|
|
|
return cmd
|
|
|
|
}
|
2019-04-12 08:24:24 +08:00
|
|
|
|
2019-04-13 07:39:06 +08:00
|
|
|
func stop(ctx context.Context, dockerCli command.Cli, ng *store.NodeGroup, rm bool) error {
|
|
|
|
dis, err := driversForNodeGroup(ctx, dockerCli, ng)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, di := range dis {
|
|
|
|
if di.Driver != nil {
|
|
|
|
if err := di.Driver.Stop(ctx, true); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if rm {
|
|
|
|
if err := di.Driver.Rm(ctx, true); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if di.Err != nil {
|
|
|
|
err = di.Err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2019-04-12 08:24:24 +08:00
|
|
|
|
2019-04-13 07:39:06 +08:00
|
|
|
func stopCurrent(ctx context.Context, dockerCli command.Cli, rm bool) error {
|
|
|
|
dis, err := getDefaultDrivers(ctx, dockerCli)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, di := range dis {
|
|
|
|
if di.Driver != nil {
|
|
|
|
if err := di.Driver.Stop(ctx, true); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if rm {
|
|
|
|
if err := di.Driver.Rm(ctx, true); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if di.Err != nil {
|
|
|
|
err = di.Err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
2019-04-12 08:24:24 +08:00
|
|
|
}
|