2016-06-13 22:56:23 -04:00
|
|
|
package node
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
2016-09-06 14:18:12 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2016-06-13 22:56:23 -04:00
|
|
|
"github.com/docker/docker/cli"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/docker/docker/cli/command"
|
2016-06-13 22:56:23 -04:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2016-07-28 00:17:00 -04:00
|
|
|
type removeOptions struct {
|
|
|
|
force bool
|
|
|
|
}
|
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
func newRemoveCommand(dockerCli *command.DockerCli) *cobra.Command {
|
2016-07-28 00:17:00 -04:00
|
|
|
opts := removeOptions{}
|
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "rm [OPTIONS] NODE [NODE...]",
|
2016-06-13 22:56:23 -04:00
|
|
|
Aliases: []string{"remove"},
|
2016-07-29 13:41:52 -04:00
|
|
|
Short: "Remove one or more nodes from the swarm",
|
2016-06-13 22:56:23 -04:00
|
|
|
Args: cli.RequiresMinArgs(1),
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2016-07-28 00:17:00 -04:00
|
|
|
return runRemove(dockerCli, args, opts)
|
2016-06-13 22:56:23 -04:00
|
|
|
},
|
|
|
|
}
|
2016-07-28 00:17:00 -04:00
|
|
|
flags := cmd.Flags()
|
|
|
|
flags.BoolVar(&opts.force, "force", false, "Force remove an active node")
|
|
|
|
return cmd
|
2016-06-13 22:56:23 -04:00
|
|
|
}
|
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
func runRemove(dockerCli *command.DockerCli, args []string, opts removeOptions) error {
|
2016-06-13 22:56:23 -04:00
|
|
|
client := dockerCli.Client()
|
|
|
|
ctx := context.Background()
|
|
|
|
for _, nodeID := range args {
|
2016-07-28 00:17:00 -04:00
|
|
|
err := client.NodeRemove(ctx, nodeID, types.NodeRemoveOptions{Force: opts.force})
|
2016-06-13 22:56:23 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Fprintf(dockerCli.Out(), "%s\n", nodeID)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|