2016-06-13 22:56:23 -04:00
|
|
|
package swarm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-06-30 21:07:35 -04:00
|
|
|
"strings"
|
2016-06-13 22:56:23 -04:00
|
|
|
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
2016-09-06 14:18:12 -04:00
|
|
|
"github.com/docker/docker/api/types/swarm"
|
2016-06-13 22:56:23 -04:00
|
|
|
"github.com/docker/docker/cli"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/docker/docker/cli/command"
|
2016-10-27 21:50:49 -04:00
|
|
|
"github.com/pkg/errors"
|
2016-06-13 22:56:23 -04:00
|
|
|
"github.com/spf13/cobra"
|
2016-06-15 17:30:54 -04:00
|
|
|
"github.com/spf13/pflag"
|
2016-06-13 22:56:23 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type initOptions struct {
|
2016-06-21 17:27:04 -04:00
|
|
|
swarmOptions
|
2016-06-30 21:07:35 -04:00
|
|
|
listenAddr NodeAddrOption
|
|
|
|
// Not a NodeAddrOption because it has no default port.
|
|
|
|
advertiseAddr string
|
2016-06-13 22:56:23 -04:00
|
|
|
forceNewCluster bool
|
|
|
|
}
|
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
func newInitCommand(dockerCli *command.DockerCli) *cobra.Command {
|
2016-06-13 22:56:23 -04:00
|
|
|
opts := initOptions{
|
2016-06-17 15:42:16 -04:00
|
|
|
listenAddr: NewListenAddrOption(),
|
2016-06-13 22:56:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
2016-07-16 10:44:10 -04:00
|
|
|
Use: "init [OPTIONS]",
|
2016-07-20 14:15:08 -04:00
|
|
|
Short: "Initialize a swarm",
|
2016-06-13 22:56:23 -04:00
|
|
|
Args: cli.NoArgs,
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2016-06-17 11:19:51 -04:00
|
|
|
return runInit(dockerCli, cmd.Flags(), opts)
|
2016-06-13 22:56:23 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-06-17 11:19:51 -04:00
|
|
|
flags := cmd.Flags()
|
2016-07-21 13:40:19 -04:00
|
|
|
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)")
|
2016-06-21 17:27:04 -04:00
|
|
|
addSwarmFlags(flags, &opts.swarmOptions)
|
2016-06-13 22:56:23 -04:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
func runInit(dockerCli *command.DockerCli, flags *pflag.FlagSet, opts initOptions) error {
|
2016-06-13 22:56:23 -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-06-13 22:56:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
nodeID, err := client.SwarmInit(ctx, req)
|
|
|
|
if err != nil {
|
2016-06-30 21:07:35 -04:00
|
|
|
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")
|
|
|
|
}
|
2016-06-13 22:56:23 -04:00
|
|
|
return err
|
|
|
|
}
|
2016-07-04 00:02:56 -04:00
|
|
|
|
2016-07-05 18:39:26 -04:00
|
|
|
fmt.Fprintf(dockerCli.Out(), "Swarm initialized: current node (%s) is now a manager.\n\n", nodeID)
|
|
|
|
|
2016-07-27 10:14:29 -04:00
|
|
|
if err := printJoinCommand(ctx, dockerCli, nodeID, true, false); err != nil {
|
|
|
|
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
|
|
|
}
|