github.com/kunnos/engine@v1.13.1/cli/command/swarm/init.go (about)

     1  package swarm
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"golang.org/x/net/context"
     8  
     9  	"github.com/docker/docker/api/types/swarm"
    10  	"github.com/docker/docker/cli"
    11  	"github.com/docker/docker/cli/command"
    12  	"github.com/pkg/errors"
    13  	"github.com/spf13/cobra"
    14  	"github.com/spf13/pflag"
    15  )
    16  
    17  type initOptions struct {
    18  	swarmOptions
    19  	listenAddr NodeAddrOption
    20  	// Not a NodeAddrOption because it has no default port.
    21  	advertiseAddr   string
    22  	forceNewCluster bool
    23  }
    24  
    25  func newInitCommand(dockerCli *command.DockerCli) *cobra.Command {
    26  	opts := initOptions{
    27  		listenAddr: NewListenAddrOption(),
    28  	}
    29  
    30  	cmd := &cobra.Command{
    31  		Use:   "init [OPTIONS]",
    32  		Short: "Initialize a swarm",
    33  		Args:  cli.NoArgs,
    34  		RunE: func(cmd *cobra.Command, args []string) error {
    35  			return runInit(dockerCli, cmd.Flags(), opts)
    36  		},
    37  	}
    38  
    39  	flags := cmd.Flags()
    40  	flags.Var(&opts.listenAddr, flagListenAddr, "Listen address (format: <ip|interface>[:port])")
    41  	flags.StringVar(&opts.advertiseAddr, flagAdvertiseAddr, "", "Advertised address (format: <ip|interface>[:port])")
    42  	flags.BoolVar(&opts.forceNewCluster, "force-new-cluster", false, "Force create a new cluster from current state")
    43  	flags.BoolVar(&opts.autolock, flagAutolock, false, "Enable manager autolocking (requiring an unlock key to start a stopped manager)")
    44  	addSwarmFlags(flags, &opts.swarmOptions)
    45  	return cmd
    46  }
    47  
    48  func runInit(dockerCli *command.DockerCli, flags *pflag.FlagSet, opts initOptions) error {
    49  	client := dockerCli.Client()
    50  	ctx := context.Background()
    51  
    52  	req := swarm.InitRequest{
    53  		ListenAddr:       opts.listenAddr.String(),
    54  		AdvertiseAddr:    opts.advertiseAddr,
    55  		ForceNewCluster:  opts.forceNewCluster,
    56  		Spec:             opts.swarmOptions.ToSpec(flags),
    57  		AutoLockManagers: opts.swarmOptions.autolock,
    58  	}
    59  
    60  	nodeID, err := client.SwarmInit(ctx, req)
    61  	if err != nil {
    62  		if strings.Contains(err.Error(), "could not choose an IP address to advertise") || strings.Contains(err.Error(), "could not find the system's IP address") {
    63  			return errors.New(err.Error() + " - specify one with --advertise-addr")
    64  		}
    65  		return err
    66  	}
    67  
    68  	fmt.Fprintf(dockerCli.Out(), "Swarm initialized: current node (%s) is now a manager.\n\n", nodeID)
    69  
    70  	if err := printJoinCommand(ctx, dockerCli, nodeID, true, false); err != nil {
    71  		return err
    72  	}
    73  
    74  	fmt.Fprint(dockerCli.Out(), "To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.\n\n")
    75  
    76  	if req.AutoLockManagers {
    77  		unlockKeyResp, err := client.SwarmGetUnlockKey(ctx)
    78  		if err != nil {
    79  			return errors.Wrap(err, "could not fetch unlock key")
    80  		}
    81  		printUnlockCommand(ctx, dockerCli, unlockKeyResp.UnlockKey)
    82  	}
    83  
    84  	return nil
    85  }