2016-09-08 13:11:39 -04:00
|
|
|
package swarm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli"
|
|
|
|
"github.com/docker/cli/cli/command"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/docker/docker/api/types/swarm"
|
2016-10-27 21:50:49 -04:00
|
|
|
"github.com/pkg/errors"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/pflag"
|
|
|
|
)
|
|
|
|
|
|
|
|
type initOptions struct {
|
|
|
|
swarmOptions
|
|
|
|
listenAddr NodeAddrOption
|
|
|
|
// Not a NodeAddrOption because it has no default port.
|
|
|
|
advertiseAddr string
|
|
|
|
forceNewCluster bool
|
2016-12-21 21:13:31 -05:00
|
|
|
availability string
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
2016-12-25 16:23:35 -05:00
|
|
|
func newInitCommand(dockerCli command.Cli) *cobra.Command {
|
2016-09-08 13:11:39 -04:00
|
|
|
opts := initOptions{
|
|
|
|
listenAddr: NewListenAddrOption(),
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "init [OPTIONS]",
|
|
|
|
Short: "Initialize a swarm",
|
|
|
|
Args: cli.NoArgs,
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
return runInit(dockerCli, cmd.Flags(), opts)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
flags := cmd.Flags()
|
|
|
|
flags.Var(&opts.listenAddr, flagListenAddr, "Listen address (format: <ip|interface>[:port])")
|
|
|
|
flags.StringVar(&opts.advertiseAddr, flagAdvertiseAddr, "", "Advertised address (format: <ip|interface>[:port])")
|
2016-11-01 15:11:38 -04:00
|
|
|
flags.BoolVar(&opts.forceNewCluster, "force-new-cluster", false, "Force create a new cluster from current state")
|
2016-11-10 15:05:19 -05:00
|
|
|
flags.BoolVar(&opts.autolock, flagAutolock, false, "Enable manager autolocking (requiring an unlock key to start a stopped manager)")
|
2017-02-08 03:31:16 -05:00
|
|
|
flags.StringVar(&opts.availability, flagAvailability, "active", `Availability of the node ("active"|"pause"|"drain")`)
|
2016-09-08 13:11:39 -04:00
|
|
|
addSwarmFlags(flags, &opts.swarmOptions)
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2016-12-25 16:23:35 -05:00
|
|
|
func runInit(dockerCli command.Cli, flags *pflag.FlagSet, opts initOptions) error {
|
2016-09-08 13:11:39 -04:00
|
|
|
client := dockerCli.Client()
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
req := swarm.InitRequest{
|
2016-10-27 21:50:49 -04:00
|
|
|
ListenAddr: opts.listenAddr.String(),
|
|
|
|
AdvertiseAddr: opts.advertiseAddr,
|
|
|
|
ForceNewCluster: opts.forceNewCluster,
|
|
|
|
Spec: opts.swarmOptions.ToSpec(flags),
|
|
|
|
AutoLockManagers: opts.swarmOptions.autolock,
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
2016-12-21 21:13:31 -05:00
|
|
|
if flags.Changed(flagAvailability) {
|
|
|
|
availability := swarm.NodeAvailability(strings.ToLower(opts.availability))
|
|
|
|
switch availability {
|
|
|
|
case swarm.NodeAvailabilityActive, swarm.NodeAvailabilityPause, swarm.NodeAvailabilityDrain:
|
|
|
|
req.Availability = availability
|
|
|
|
default:
|
2017-03-09 13:23:45 -05:00
|
|
|
return errors.Errorf("invalid availability %q, only active, pause and drain are supported", opts.availability)
|
2016-12-21 21:13:31 -05:00
|
|
|
}
|
|
|
|
}
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
nodeID, err := client.SwarmInit(ctx, req)
|
|
|
|
if err != nil {
|
|
|
|
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") {
|
|
|
|
return errors.New(err.Error() + " - specify one with --advertise-addr")
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintf(dockerCli.Out(), "Swarm initialized: current node (%s) is now a manager.\n\n", nodeID)
|
|
|
|
|
2017-02-09 17:54:05 -05:00
|
|
|
if err := printJoinCommand(ctx, dockerCli, nodeID, true, false); err != nil {
|
2016-09-08 13:11:39 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprint(dockerCli.Out(), "To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.\n\n")
|
2016-10-21 21:07:55 -04:00
|
|
|
|
2016-10-27 21:50:49 -04:00
|
|
|
if req.AutoLockManagers {
|
|
|
|
unlockKeyResp, err := client.SwarmGetUnlockKey(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "could not fetch unlock key")
|
2016-10-21 21:07:55 -04:00
|
|
|
}
|
2016-10-27 21:50:49 -04:00
|
|
|
printUnlockCommand(ctx, dockerCli, unlockKeyResp.UnlockKey)
|
2016-10-21 21:07:55 -04:00
|
|
|
}
|
2016-10-27 21:50:49 -04:00
|
|
|
|
|
|
|
return nil
|
2016-10-21 21:07:55 -04:00
|
|
|
}
|