this change improves the instructions for
swarm join-token and swarm init;
- only print the join-token command for workers
instead of for both managers and workers, to
prevent users from copying the wrong command.
An extra line is added to explain how to obtain
the manager token.
- print a message that a token was rotated
sucesfully if '--rotate' is used.
- add some extra white-space before / after
the join commands, to make copy/pasting
easier.
this change also does some refactoring of join-token;
- move flagname-constants together with other constants
- use variables for selected role ("worker" / "manager")
to prevent checking for them multiple times, and to
keep the "worker" / "manager" sting centralized
- add an extra blank line after "join-token" instructions
this makes it easier to copy, and cleans up the
code a tiny bit
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Upstream-commit: ebebd4176940bc907ba4e8f5fbe62f6a050f8ed4
Component: engine
106 lines
2.5 KiB
Go
106 lines
2.5 KiB
Go
package swarm
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/docker/docker/api/client"
|
|
"github.com/docker/docker/cli"
|
|
"github.com/docker/engine-api/types/swarm"
|
|
"golang.org/x/net/context"
|
|
)
|
|
|
|
func newJoinTokenCommand(dockerCli *client.DockerCli) *cobra.Command {
|
|
var rotate, quiet bool
|
|
|
|
cmd := &cobra.Command{
|
|
Use: "join-token [-q] [--rotate] (worker|manager)",
|
|
Short: "Manage join tokens",
|
|
Args: cli.ExactArgs(1),
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
worker := args[0] == "worker"
|
|
manager := args[0] == "manager"
|
|
|
|
if !worker && !manager {
|
|
return errors.New("unknown role " + args[0])
|
|
}
|
|
|
|
client := dockerCli.Client()
|
|
ctx := context.Background()
|
|
|
|
if rotate {
|
|
var flags swarm.UpdateFlags
|
|
|
|
swarm, err := client.SwarmInspect(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
flags.RotateWorkerToken = worker
|
|
flags.RotateManagerToken = manager
|
|
|
|
err = client.SwarmUpdate(ctx, swarm.Version, swarm.Spec, flags)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !quiet {
|
|
fmt.Fprintf(dockerCli.Out(), "Succesfully rotated %s join token.\n\n", args[0])
|
|
}
|
|
}
|
|
|
|
swarm, err := client.SwarmInspect(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if quiet {
|
|
if worker {
|
|
fmt.Fprintln(dockerCli.Out(), swarm.JoinTokens.Worker)
|
|
} else {
|
|
fmt.Fprintln(dockerCli.Out(), swarm.JoinTokens.Manager)
|
|
}
|
|
} else {
|
|
info, err := client.Info(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return printJoinCommand(ctx, dockerCli, info.Swarm.NodeID, worker, manager)
|
|
}
|
|
return nil
|
|
},
|
|
}
|
|
|
|
flags := cmd.Flags()
|
|
flags.BoolVar(&rotate, flagRotate, false, "Rotate join token")
|
|
flags.BoolVarP(&quiet, flagQuiet, "q", false, "Only display token")
|
|
|
|
return cmd
|
|
}
|
|
|
|
func printJoinCommand(ctx context.Context, dockerCli *client.DockerCli, nodeID string, worker bool, manager bool) error {
|
|
client := dockerCli.Client()
|
|
|
|
swarm, err := client.SwarmInspect(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
node, _, err := client.NodeInspectWithRaw(ctx, nodeID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if node.ManagerStatus != nil {
|
|
if worker {
|
|
fmt.Fprintf(dockerCli.Out(), "To add a worker to this swarm, run the following command:\n\n docker swarm join \\\n --token %s \\\n %s\n\n", swarm.JoinTokens.Worker, node.ManagerStatus.Addr)
|
|
}
|
|
if manager {
|
|
fmt.Fprintf(dockerCli.Out(), "To add a manager to this swarm, run the following command:\n\n docker swarm join \\\n --token %s \\\n %s\n\n", swarm.JoinTokens.Manager, node.ManagerStatus.Addr)
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|