2016-09-08 13:11:39 -04:00
|
|
|
package swarm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
|
|
|
"github.com/docker/docker/cli"
|
|
|
|
"github.com/docker/docker/cli/command"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
type leaveOptions struct {
|
|
|
|
force bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func newLeaveCommand(dockerCli *command.DockerCli) *cobra.Command {
|
|
|
|
opts := leaveOptions{}
|
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "leave [OPTIONS]",
|
2016-09-01 18:38:25 -04:00
|
|
|
Short: "Leave the swarm (workers only)",
|
2016-09-08 13:11:39 -04:00
|
|
|
Args: cli.NoArgs,
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
return runLeave(dockerCli, opts)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
flags := cmd.Flags()
|
2016-09-01 18:38:25 -04:00
|
|
|
flags.BoolVar(&opts.force, "force", false, "Force this node to leave the swarm, ignoring warnings")
|
2016-09-08 13:11:39 -04:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
func runLeave(dockerCli *command.DockerCli, opts leaveOptions) error {
|
|
|
|
client := dockerCli.Client()
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
if err := client.SwarmLeave(ctx, opts.force); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintln(dockerCli.Out(), "Node left the swarm.")
|
|
|
|
return nil
|
|
|
|
}
|