2016-09-08 13:11:39 -04:00
|
|
|
package node
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-10-18 18:29:27 -04:00
|
|
|
"strings"
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/cli"
|
|
|
|
"github.com/docker/docker/cli/command"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
type removeOptions struct {
|
|
|
|
force bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func newRemoveCommand(dockerCli *command.DockerCli) *cobra.Command {
|
|
|
|
opts := removeOptions{}
|
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "rm [OPTIONS] NODE [NODE...]",
|
|
|
|
Aliases: []string{"remove"},
|
|
|
|
Short: "Remove one or more nodes from the swarm",
|
|
|
|
Args: cli.RequiresMinArgs(1),
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
return runRemove(dockerCli, args, opts)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
flags := cmd.Flags()
|
2016-11-02 05:22:04 -04:00
|
|
|
flags.BoolVar(&opts.force, "force", false, "Force remove a node from the swarm")
|
2016-09-08 13:11:39 -04:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
func runRemove(dockerCli *command.DockerCli, args []string, opts removeOptions) error {
|
|
|
|
client := dockerCli.Client()
|
|
|
|
ctx := context.Background()
|
2016-10-18 18:29:27 -04:00
|
|
|
|
|
|
|
var errs []string
|
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
for _, nodeID := range args {
|
|
|
|
err := client.NodeRemove(ctx, nodeID, types.NodeRemoveOptions{Force: opts.force})
|
|
|
|
if err != nil {
|
2016-10-18 18:29:27 -04:00
|
|
|
errs = append(errs, err.Error())
|
|
|
|
continue
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
fmt.Fprintf(dockerCli.Out(), "%s\n", nodeID)
|
|
|
|
}
|
2016-10-18 18:29:27 -04:00
|
|
|
|
|
|
|
if len(errs) > 0 {
|
|
|
|
return fmt.Errorf("%s", strings.Join(errs, "\n"))
|
|
|
|
}
|
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
return nil
|
|
|
|
}
|