2016-06-13 22:56:23 -04:00
|
|
|
package swarm
|
|
|
|
|
|
|
|
import (
|
2016-06-30 21:07:35 -04:00
|
|
|
"errors"
|
2016-06-13 22:56:23 -04:00
|
|
|
"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-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
|
|
|
)
|
|
|
|
|
2016-07-05 18:39:26 -04:00
|
|
|
const (
|
|
|
|
generatedSecretEntropyBytes = 16
|
|
|
|
generatedSecretBase = 36
|
|
|
|
// floor(log(2^128-1, 36)) + 1
|
|
|
|
maxGeneratedSecretLength = 25
|
|
|
|
)
|
|
|
|
|
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-06-13 22:56:23 -04:00
|
|
|
flags.BoolVar(&opts.forceNewCluster, "force-new-cluster", false, "Force create a new cluster from current state.")
|
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{
|
|
|
|
ListenAddr: opts.listenAddr.String(),
|
2016-06-30 21:07:35 -04:00
|
|
|
AdvertiseAddr: opts.advertiseAddr,
|
2016-06-13 22:56:23 -04:00
|
|
|
ForceNewCluster: opts.forceNewCluster,
|
2016-08-26 00:08:53 -04:00
|
|
|
Spec: opts.swarmOptions.ToSpec(flags),
|
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")
|
|
|
|
return nil
|
2016-06-13 22:56:23 -04:00
|
|
|
}
|