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"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/client"
|
|
|
|
"github.com/docker/docker/cli"
|
|
|
|
"github.com/docker/engine-api/types/swarm"
|
|
|
|
"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
|
|
|
|
}
|
|
|
|
|
|
|
|
func newInitCommand(dockerCli *client.DockerCli) *cobra.Command {
|
|
|
|
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-06-15 17:30:54 -04:00
|
|
|
func runInit(dockerCli *client.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-06-21 17:27:04 -04:00
|
|
|
Spec: opts.swarmOptions.ToSpec(),
|
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-20 14:15:08 -04:00
|
|
|
return printJoinCommand(ctx, dockerCli, nodeID, true, true)
|
2016-06-13 22:56:23 -04:00
|
|
|
}
|