From 40baa97ab10a8e372c460a95dc93e38db957f3a6 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Wed, 27 Jul 2016 16:14:29 +0200 Subject: [PATCH] Improve swarm join-token instructions 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 (cherry picked from commit ebebd4176940bc907ba4e8f5fbe62f6a050f8ed4) Signed-off-by: Tibor Vass --- api/client/swarm/init.go | 7 +++- api/client/swarm/join_token.go | 33 ++++++++----------- api/client/swarm/opts.go | 2 ++ docs/reference/commandline/swarm_init.md | 6 ++-- .../reference/commandline/swarm_join_token.md | 6 ++++ docs/swarm/join-nodes.md | 2 ++ docs/swarm/swarm-mode.md | 11 ++++--- docs/swarm/swarm-tutorial/add-nodes.md | 1 + docs/swarm/swarm-tutorial/create-swarm.md | 6 ++-- 9 files changed, 41 insertions(+), 33 deletions(-) diff --git a/api/client/swarm/init.go b/api/client/swarm/init.go index 0555243f0d..6a59019067 100644 --- a/api/client/swarm/init.go +++ b/api/client/swarm/init.go @@ -72,5 +72,10 @@ func runInit(dockerCli *client.DockerCli, flags *pflag.FlagSet, opts initOptions fmt.Fprintf(dockerCli.Out(), "Swarm initialized: current node (%s) is now a manager.\n\n", nodeID) - return printJoinCommand(ctx, dockerCli, nodeID, true, true) + 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 } diff --git a/api/client/swarm/join_token.go b/api/client/swarm/join_token.go index 8e6f3fa2de..130b55f40b 100644 --- a/api/client/swarm/join_token.go +++ b/api/client/swarm/join_token.go @@ -12,11 +12,6 @@ import ( "golang.org/x/net/context" ) -const ( - flagRotate = "rotate" - flagQuiet = "quiet" -) - func newJoinTokenCommand(dockerCli *client.DockerCli) *cobra.Command { var rotate, quiet bool @@ -25,7 +20,10 @@ func newJoinTokenCommand(dockerCli *client.DockerCli) *cobra.Command { Short: "Manage join tokens", Args: cli.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { - if args[0] != "worker" && args[0] != "manager" { + worker := args[0] == "worker" + manager := args[0] == "manager" + + if !worker && !manager { return errors.New("unknown role " + args[0]) } @@ -40,16 +38,16 @@ func newJoinTokenCommand(dockerCli *client.DockerCli) *cobra.Command { return err } - if args[0] == "worker" { - flags.RotateWorkerToken = true - } else if args[0] == "manager" { - flags.RotateManagerToken = true - } + 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) @@ -58,9 +56,9 @@ func newJoinTokenCommand(dockerCli *client.DockerCli) *cobra.Command { } if quiet { - if args[0] == "worker" { + if worker { fmt.Fprintln(dockerCli.Out(), swarm.JoinTokens.Worker) - } else if args[0] == "manager" { + } else { fmt.Fprintln(dockerCli.Out(), swarm.JoinTokens.Manager) } } else { @@ -68,7 +66,7 @@ func newJoinTokenCommand(dockerCli *client.DockerCli) *cobra.Command { if err != nil { return err } - return printJoinCommand(ctx, dockerCli, info.Swarm.NodeID, args[0] == "worker", args[0] == "manager") + return printJoinCommand(ctx, dockerCli, info.Swarm.NodeID, worker, manager) } return nil }, @@ -96,13 +94,10 @@ func printJoinCommand(ctx context.Context, dockerCli *client.DockerCli, nodeID s if node.ManagerStatus != nil { if worker { - fmt.Fprintf(dockerCli.Out(), "To add a worker to this swarm, run the following command:\n docker swarm join \\\n --token %s \\\n %s\n", swarm.JoinTokens.Worker, node.ManagerStatus.Addr) + 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 { - if worker { - fmt.Fprintln(dockerCli.Out()) - } - fmt.Fprintf(dockerCli.Out(), "To add a manager to this swarm, run the following command:\n docker swarm join \\\n --token %s \\\n %s\n", swarm.JoinTokens.Manager, node.ManagerStatus.Addr) + 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) } } diff --git a/api/client/swarm/opts.go b/api/client/swarm/opts.go index c57a07dfbc..ae91b8530e 100644 --- a/api/client/swarm/opts.go +++ b/api/client/swarm/opts.go @@ -19,6 +19,8 @@ const ( flagDispatcherHeartbeat = "dispatcher-heartbeat" flagListenAddr = "listen-addr" flagAdvertiseAddr = "advertise-addr" + flagQuiet = "quiet" + flagRotate = "rotate" flagToken = "token" flagTaskHistoryLimit = "task-history-limit" flagExternalCA = "external-ca" diff --git a/docs/reference/commandline/swarm_init.md b/docs/reference/commandline/swarm_init.md index 5f57c8191c..dd72f6050d 100644 --- a/docs/reference/commandline/swarm_init.md +++ b/docs/reference/commandline/swarm_init.md @@ -37,14 +37,12 @@ $ docker swarm init --advertise-addr 192.168.99.121 Swarm initialized: current node (bvz81updecsj6wjz393c09vti) is now a manager. To add a worker to this swarm, run the following command: + docker swarm join \ --token SWMTKN-1-3pu6hszjas19xyp7ghgosyx9k8atbfcr8p2is99znpy26u2lkl-1awxwuwd3z9j1z3puu7rcgdbx \ 172.17.0.2:2377 -To add a manager to this swarm, run the following command: - docker swarm join \ - --token SWMTKN-1-3pu6hszjas19xyp7ghgosyx9k8atbfcr8p2is99znpy26u2lkl-7p73s1dx5in4tatdymyhg9hu2 \ - 172.17.0.2:2377 +To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions. ``` `docker swarm init` generates two random tokens, a worker token and a manager token. When you join diff --git a/docs/reference/commandline/swarm_join_token.md b/docs/reference/commandline/swarm_join_token.md index 996ca01fee..4b4d652bf3 100644 --- a/docs/reference/commandline/swarm_join_token.md +++ b/docs/reference/commandline/swarm_join_token.md @@ -36,12 +36,14 @@ the swarm: ```bash $ docker swarm join-token worker To add a worker to this swarm, run the following command: + docker swarm join \ --token SWMTKN-1-3pu6hszjas19xyp7ghgosyx9k8atbfcr8p2is99znpy26u2lkl-1awxwuwd3z9j1z3puu7rcgdbx \ 172.17.0.2:2377 $ docker swarm join-token manager To add a manager to this swarm, run the following command: + docker swarm join \ --token SWMTKN-1-3pu6hszjas19xyp7ghgosyx9k8atbfcr8p2is99znpy26u2lkl-7p73s1dx5in4tatdymyhg9hu2 \ 172.17.0.2:2377 @@ -51,7 +53,10 @@ Use the `--rotate` flag to generate a new join token for the specified role: ```bash $ docker swarm join-token --rotate worker +Succesfully rotated worker join token. + To add a worker to this swarm, run the following command: + docker swarm join \ --token SWMTKN-1-3pu6hszjas19xyp7ghgosyx9k8atbfcr8p2is99znpy26u2lkl-b30ljddcqhef9b9v4rs7mel7t \ 172.17.0.2:2377 @@ -63,6 +68,7 @@ The `-q` (or `--quiet`) flag only prints the token: ```bash $ docker swarm join-token -q worker + SWMTKN-1-3pu6hszjas19xyp7ghgosyx9k8atbfcr8p2is99znpy26u2lkl-b30ljddcqhef9b9v4rs7mel7t ``` diff --git a/docs/swarm/join-nodes.md b/docs/swarm/join-nodes.md index b7fd313486..3a8cde98eb 100644 --- a/docs/swarm/join-nodes.md +++ b/docs/swarm/join-nodes.md @@ -43,6 +43,7 @@ following command on a manager node: $ docker swarm join-token worker To add a worker to this swarm, run the following command: + docker swarm join \ --token SWMTKN-1-49nj1cmql0jkz5s954yi3oex3nedyz0fb0xx14ie39trti4wxv-8vxv8rssmk743ojnwacrr2e7c \ 192.168.99.100:2377 @@ -90,6 +91,7 @@ following command on a manager node: $ docker swarm join-token manager To add a manager to this swarm, run the following command: + docker swarm join \ --token SWMTKN-1-61ztec5kyafptydic6jfc1i33t37flcl4nuipzcusor96k7kby-5vy9t8u35tuqm7vh67lrz9xp6 \ 192.168.99.100:2377 diff --git a/docs/swarm/swarm-mode.md b/docs/swarm/swarm-mode.md index b602026a44..6f2a09cee5 100644 --- a/docs/swarm/swarm-mode.md +++ b/docs/swarm/swarm-mode.md @@ -56,21 +56,19 @@ swarm. external to the swarm. The output for `docker swarm init` provides the connection command to use when -you join new worker or manager nodes to the swarm: +you join new worker nodes to the swarm: ```bash $ docker swarm init Swarm initialized: current node (dxn1zf6l61qsb1josjja83ngz) is now a manager. To add a worker to this swarm, run the following command: + docker swarm join \ --token SWMTKN-1-49nj1cmql0jkz5s954yi3oex3nedyz0fb0xx14ie39trti4wxv-8vxv8rssmk743ojnwacrr2e7c \ 192.168.99.100:2377 -To add a manager to this swarm, run the following command: - docker swarm join \ - --token SWMTKN-1-61ztec5kyafptydic6jfc1i33t37flcl4nuipzcusor96k7kby-5vy9t8u35tuqm7vh67lrz9xp6 \ - 192.168.99.100:2377 +To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions. ``` ### Configure the advertise address @@ -115,6 +113,7 @@ To retrieve the join command including the join token for worker nodes, run: $ docker swarm join-token worker To add a worker to this swarm, run the following command: + docker swarm join \ --token SWMTKN-1-49nj1cmql0jkz5s954yi3oex3nedyz0fb0xx14ie39trti4wxv-8vxv8rssmk743ojnwacrr2e7c \ 192.168.99.100:2377 @@ -128,6 +127,7 @@ To view the join command and token for manager nodes, run: $ docker swarm join-token manager To add a worker to this swarm, run the following command: + docker swarm join \ --token SWMTKN-1-49nj1cmql0jkz5s954yi3oex3nedyz0fb0xx14ie39trti4wxv-8vxv8rssmk743ojnwacrr2e7c \ 192.168.99.100:2377 @@ -167,6 +167,7 @@ nodes: $docker swarm join-token --rotate worker To add a worker to this swarm, run the following command: + docker swarm join \ --token SWMTKN-1-2kscvs0zuymrsc9t0ocyy1rdns9dhaodvpl639j2bqx55uptag-ebmn5u927reawo27s3azntd44 \ 172.17.0.2:2377 diff --git a/docs/swarm/swarm-tutorial/add-nodes.md b/docs/swarm/swarm-tutorial/add-nodes.md index 3d4fd89bfb..364d3f40a0 100644 --- a/docs/swarm/swarm-tutorial/add-nodes.md +++ b/docs/swarm/swarm-tutorial/add-nodes.md @@ -36,6 +36,7 @@ This tutorial uses the name `worker1`. $ docker swarm join-token worker To add a worker to this swarm, run the following command: + docker swarm join \ --token SWMTKN-1-49nj1cmql0jkz5s954yi3oex3nedyz0fb0xx14ie39trti4wxv-8vxv8rssmk743ojnwacrr2e7c \ 192.168.99.100:2377 diff --git a/docs/swarm/swarm-tutorial/create-swarm.md b/docs/swarm/swarm-tutorial/create-swarm.md index f4c6223b0f..b6d6222169 100644 --- a/docs/swarm/swarm-tutorial/create-swarm.md +++ b/docs/swarm/swarm-tutorial/create-swarm.md @@ -33,14 +33,12 @@ node. For example, the tutorial uses a machine named `manager1`. Swarm initialized: current node (dxn1zf6l61qsb1josjja83ngz) is now a manager. To add a worker to this swarm, run the following command: + docker swarm join \ --token SWMTKN-1-49nj1cmql0jkz5s954yi3oex3nedyz0fb0xx14ie39trti4wxv-8vxv8rssmk743ojnwacrr2e7c \ 192.168.99.100:2377 - To add a manager to this swarm, run the following command: - docker swarm join \ - --token SWMTKN-1-61ztec5kyafptydic6jfc1i33t37flcl4nuipzcusor96k7kby-5vy9t8u35tuqm7vh67lrz9xp6 \ - 192.168.99.100:2377 + To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions. ``` The `--advertise-addr` flag configures the manager node to publish its