mirror of
				https://github.com/moby/moby.git
				synced 2022-11-09 12:21:53 -05:00 
			
		
		
		
	Implement the proposal from https://github.com/docker/docker/issues/24430#issuecomment-233100121 Removes acceptance policy and secret in favor of an automatically generated join token that combines the secret, CA hash, and manager/worker role into a single opaque string. Adds a docker swarm join-token subcommand to inspect and rotate the tokens. Signed-off-by: Aaron Lehmann <aaron.lehmann@docker.com>
		
			
				
	
	
		
			67 lines
		
	
	
	
		
			1.6 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			67 lines
		
	
	
	
		
			1.6 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
package swarm
 | 
						|
 | 
						|
import (
 | 
						|
	"fmt"
 | 
						|
 | 
						|
	"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"
 | 
						|
	"github.com/spf13/pflag"
 | 
						|
)
 | 
						|
 | 
						|
const (
 | 
						|
	generatedSecretEntropyBytes = 16
 | 
						|
	generatedSecretBase         = 36
 | 
						|
	// floor(log(2^128-1, 36)) + 1
 | 
						|
	maxGeneratedSecretLength = 25
 | 
						|
)
 | 
						|
 | 
						|
type initOptions struct {
 | 
						|
	swarmOptions
 | 
						|
	listenAddr      NodeAddrOption
 | 
						|
	forceNewCluster bool
 | 
						|
}
 | 
						|
 | 
						|
func newInitCommand(dockerCli *client.DockerCli) *cobra.Command {
 | 
						|
	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, "listen-addr", "Listen address")
 | 
						|
	flags.BoolVar(&opts.forceNewCluster, "force-new-cluster", false, "Force create a new cluster from current state.")
 | 
						|
	addSwarmFlags(flags, &opts.swarmOptions)
 | 
						|
	return cmd
 | 
						|
}
 | 
						|
 | 
						|
func runInit(dockerCli *client.DockerCli, flags *pflag.FlagSet, opts initOptions) error {
 | 
						|
	client := dockerCli.Client()
 | 
						|
	ctx := context.Background()
 | 
						|
 | 
						|
	req := swarm.InitRequest{
 | 
						|
		ListenAddr:      opts.listenAddr.String(),
 | 
						|
		ForceNewCluster: opts.forceNewCluster,
 | 
						|
		Spec:            opts.swarmOptions.ToSpec(),
 | 
						|
	}
 | 
						|
 | 
						|
	nodeID, err := client.SwarmInit(ctx, req)
 | 
						|
	if err != nil {
 | 
						|
		return err
 | 
						|
	}
 | 
						|
 | 
						|
	fmt.Fprintf(dockerCli.Out(), "Swarm initialized: current node (%s) is now a manager.\n\n", nodeID)
 | 
						|
 | 
						|
	return printJoinCommand(ctx, dockerCli, nodeID, true, true)
 | 
						|
}
 |