github.com/DaoCloud/dao@v0.0.0-20161212064103-c3dbfd13ee36/api/client/swarm/leave.go (about)

     1  package swarm
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"golang.org/x/net/context"
     7  
     8  	"github.com/docker/docker/api/client"
     9  	"github.com/docker/docker/cli"
    10  	"github.com/spf13/cobra"
    11  )
    12  
    13  type leaveOptions struct {
    14  	force bool
    15  }
    16  
    17  func newLeaveCommand(dockerCli *client.DockerCli) *cobra.Command {
    18  	opts := leaveOptions{}
    19  
    20  	cmd := &cobra.Command{
    21  		Use:   "leave [OPTIONS]",
    22  		Short: "脱离Swarm集群",
    23  		Args:  cli.NoArgs,
    24  		RunE: func(cmd *cobra.Command, args []string) error {
    25  			return runLeave(dockerCli, opts)
    26  		},
    27  	}
    28  
    29  	flags := cmd.Flags()
    30  
    31  	flags.BoolVar(&opts.force, "force", false, "强制脱离Swarm集群,忽略所有警告。")
    32  
    33  	return cmd
    34  }
    35  
    36  func runLeave(dockerCli *client.DockerCli, opts leaveOptions) error {
    37  	client := dockerCli.Client()
    38  	ctx := context.Background()
    39  
    40  	if err := client.SwarmLeave(ctx, opts.force); err != nil {
    41  		return err
    42  	}
    43  
    44  	fmt.Fprintln(dockerCli.Out(), "节点成功脱离Swarm集群")
    45  	return nil
    46  }