Browse code

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 <github@gone.nl>

Sebastiaan van Stijn authored on 2016/07/27 23:14:29
Showing 9 changed files
... ...
@@ -72,5 +72,10 @@ func runInit(dockerCli *client.DockerCli, flags *pflag.FlagSet, opts initOptions
72 72
 
73 73
 	fmt.Fprintf(dockerCli.Out(), "Swarm initialized: current node (%s) is now a manager.\n\n", nodeID)
74 74
 
75
-	return printJoinCommand(ctx, dockerCli, nodeID, true, true)
75
+	if err := printJoinCommand(ctx, dockerCli, nodeID, true, false); err != nil {
76
+		return err
77
+	}
78
+
79
+	fmt.Fprint(dockerCli.Out(), "To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.\n\n")
80
+	return nil
76 81
 }
... ...
@@ -12,11 +12,6 @@ import (
12 12
 	"golang.org/x/net/context"
13 13
 )
14 14
 
15
-const (
16
-	flagRotate = "rotate"
17
-	flagQuiet  = "quiet"
18
-)
19
-
20 15
 func newJoinTokenCommand(dockerCli *client.DockerCli) *cobra.Command {
21 16
 	var rotate, quiet bool
22 17
 
... ...
@@ -25,7 +20,10 @@ func newJoinTokenCommand(dockerCli *client.DockerCli) *cobra.Command {
25 25
 		Short: "Manage join tokens",
26 26
 		Args:  cli.ExactArgs(1),
27 27
 		RunE: func(cmd *cobra.Command, args []string) error {
28
-			if args[0] != "worker" && args[0] != "manager" {
28
+			worker := args[0] == "worker"
29
+			manager := args[0] == "manager"
30
+
31
+			if !worker && !manager {
29 32
 				return errors.New("unknown role " + args[0])
30 33
 			}
31 34
 
... ...
@@ -40,16 +38,16 @@ func newJoinTokenCommand(dockerCli *client.DockerCli) *cobra.Command {
40 40
 					return err
41 41
 				}
42 42
 
43
-				if args[0] == "worker" {
44
-					flags.RotateWorkerToken = true
45
-				} else if args[0] == "manager" {
46
-					flags.RotateManagerToken = true
47
-				}
43
+				flags.RotateWorkerToken = worker
44
+				flags.RotateManagerToken = manager
48 45
 
49 46
 				err = client.SwarmUpdate(ctx, swarm.Version, swarm.Spec, flags)
50 47
 				if err != nil {
51 48
 					return err
52 49
 				}
50
+				if !quiet {
51
+					fmt.Fprintf(dockerCli.Out(), "Succesfully rotated %s join token.\n\n", args[0])
52
+				}
53 53
 			}
54 54
 
55 55
 			swarm, err := client.SwarmInspect(ctx)
... ...
@@ -58,9 +56,9 @@ func newJoinTokenCommand(dockerCli *client.DockerCli) *cobra.Command {
58 58
 			}
59 59
 
60 60
 			if quiet {
61
-				if args[0] == "worker" {
61
+				if worker {
62 62
 					fmt.Fprintln(dockerCli.Out(), swarm.JoinTokens.Worker)
63
-				} else if args[0] == "manager" {
63
+				} else {
64 64
 					fmt.Fprintln(dockerCli.Out(), swarm.JoinTokens.Manager)
65 65
 				}
66 66
 			} else {
... ...
@@ -68,7 +66,7 @@ func newJoinTokenCommand(dockerCli *client.DockerCli) *cobra.Command {
68 68
 				if err != nil {
69 69
 					return err
70 70
 				}
71
-				return printJoinCommand(ctx, dockerCli, info.Swarm.NodeID, args[0] == "worker", args[0] == "manager")
71
+				return printJoinCommand(ctx, dockerCli, info.Swarm.NodeID, worker, manager)
72 72
 			}
73 73
 			return nil
74 74
 		},
... ...
@@ -96,13 +94,10 @@ func printJoinCommand(ctx context.Context, dockerCli *client.DockerCli, nodeID s
96 96
 
97 97
 	if node.ManagerStatus != nil {
98 98
 		if worker {
99
-			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)
99
+			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)
100 100
 		}
101 101
 		if manager {
102
-			if worker {
103
-				fmt.Fprintln(dockerCli.Out())
104
-			}
105
-			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)
102
+			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)
106 103
 		}
107 104
 	}
108 105
 
... ...
@@ -19,6 +19,8 @@ const (
19 19
 	flagDispatcherHeartbeat = "dispatcher-heartbeat"
20 20
 	flagListenAddr          = "listen-addr"
21 21
 	flagAdvertiseAddr       = "advertise-addr"
22
+	flagQuiet               = "quiet"
23
+	flagRotate              = "rotate"
22 24
 	flagToken               = "token"
23 25
 	flagTaskHistoryLimit    = "task-history-limit"
24 26
 	flagExternalCA          = "external-ca"
... ...
@@ -35,14 +35,12 @@ $ docker swarm init --advertise-addr 192.168.99.121
35 35
 Swarm initialized: current node (bvz81updecsj6wjz393c09vti) is now a manager.
36 36
 
37 37
 To add a worker to this swarm, run the following command:
38
+
38 39
     docker swarm join \
39 40
     --token SWMTKN-1-3pu6hszjas19xyp7ghgosyx9k8atbfcr8p2is99znpy26u2lkl-1awxwuwd3z9j1z3puu7rcgdbx \
40 41
     172.17.0.2:2377
41 42
 
42
-To add a manager to this swarm, run the following command:
43
-    docker swarm join \
44
-    --token SWMTKN-1-3pu6hszjas19xyp7ghgosyx9k8atbfcr8p2is99znpy26u2lkl-7p73s1dx5in4tatdymyhg9hu2 \
45
-    172.17.0.2:2377
43
+To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
46 44
 ```
47 45
 
48 46
 `docker swarm init` generates two random tokens, a worker token and a manager token. When you join
... ...
@@ -36,12 +36,14 @@ the swarm:
36 36
 ```bash
37 37
 $ docker swarm join-token worker
38 38
 To add a worker to this swarm, run the following command:
39
+
39 40
     docker swarm join \
40 41
     --token SWMTKN-1-3pu6hszjas19xyp7ghgosyx9k8atbfcr8p2is99znpy26u2lkl-1awxwuwd3z9j1z3puu7rcgdbx \
41 42
     172.17.0.2:2377
42 43
 
43 44
 $ docker swarm join-token manager
44 45
 To add a manager to this swarm, run the following command:
46
+
45 47
     docker swarm join \
46 48
     --token SWMTKN-1-3pu6hszjas19xyp7ghgosyx9k8atbfcr8p2is99znpy26u2lkl-7p73s1dx5in4tatdymyhg9hu2 \
47 49
     172.17.0.2:2377
... ...
@@ -51,7 +53,10 @@ Use the `--rotate` flag to generate a new join token for the specified role:
51 51
 
52 52
 ```bash
53 53
 $ docker swarm join-token --rotate worker
54
+Succesfully rotated worker join token.
55
+
54 56
 To add a worker to this swarm, run the following command:
57
+
55 58
     docker swarm join \
56 59
     --token SWMTKN-1-3pu6hszjas19xyp7ghgosyx9k8atbfcr8p2is99znpy26u2lkl-b30ljddcqhef9b9v4rs7mel7t \
57 60
     172.17.0.2:2377
... ...
@@ -63,6 +68,7 @@ The `-q` (or `--quiet`) flag only prints the token:
63 63
 
64 64
 ```bash
65 65
 $ docker swarm join-token -q worker
66
+
66 67
 SWMTKN-1-3pu6hszjas19xyp7ghgosyx9k8atbfcr8p2is99znpy26u2lkl-b30ljddcqhef9b9v4rs7mel7t
67 68
 ```
68 69
 
... ...
@@ -43,6 +43,7 @@ following command on a manager node:
43 43
 $ docker swarm join-token worker
44 44
 
45 45
 To add a worker to this swarm, run the following command:
46
+
46 47
     docker swarm join \
47 48
     --token SWMTKN-1-49nj1cmql0jkz5s954yi3oex3nedyz0fb0xx14ie39trti4wxv-8vxv8rssmk743ojnwacrr2e7c \
48 49
     192.168.99.100:2377
... ...
@@ -90,6 +91,7 @@ following command on a manager node:
90 90
 $ docker swarm join-token manager
91 91
 
92 92
 To add a manager to this swarm, run the following command:
93
+
93 94
     docker swarm join \
94 95
     --token SWMTKN-1-61ztec5kyafptydic6jfc1i33t37flcl4nuipzcusor96k7kby-5vy9t8u35tuqm7vh67lrz9xp6 \
95 96
     192.168.99.100:2377
... ...
@@ -56,21 +56,19 @@ swarm.
56 56
 external to the swarm.
57 57
 
58 58
 The output for `docker swarm init` provides the connection command to use when
59
-you join new worker or manager nodes to the swarm:
59
+you join new worker nodes to the swarm:
60 60
 
61 61
 ```bash
62 62
 $ docker swarm init
63 63
 Swarm initialized: current node (dxn1zf6l61qsb1josjja83ngz) is now a manager.
64 64
 
65 65
 To add a worker to this swarm, run the following command:
66
+
66 67
     docker swarm join \
67 68
     --token SWMTKN-1-49nj1cmql0jkz5s954yi3oex3nedyz0fb0xx14ie39trti4wxv-8vxv8rssmk743ojnwacrr2e7c \
68 69
     192.168.99.100:2377
69 70
 
70
-To add a manager to this swarm, run the following command:
71
-    docker swarm join \
72
-    --token SWMTKN-1-61ztec5kyafptydic6jfc1i33t37flcl4nuipzcusor96k7kby-5vy9t8u35tuqm7vh67lrz9xp6 \
73
-    192.168.99.100:2377
71
+To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
74 72
 ```
75 73
 
76 74
 ### Configure the advertise address
... ...
@@ -115,6 +113,7 @@ To retrieve the join command including the join token for worker nodes, run:
115 115
 $ docker swarm join-token worker
116 116
 
117 117
 To add a worker to this swarm, run the following command:
118
+
118 119
     docker swarm join \
119 120
     --token SWMTKN-1-49nj1cmql0jkz5s954yi3oex3nedyz0fb0xx14ie39trti4wxv-8vxv8rssmk743ojnwacrr2e7c \
120 121
     192.168.99.100:2377
... ...
@@ -128,6 +127,7 @@ To view the join command and token for manager nodes, run:
128 128
 $ docker swarm join-token manager
129 129
 
130 130
 To add a worker to this swarm, run the following command:
131
+
131 132
     docker swarm join \
132 133
     --token SWMTKN-1-49nj1cmql0jkz5s954yi3oex3nedyz0fb0xx14ie39trti4wxv-8vxv8rssmk743ojnwacrr2e7c \
133 134
     192.168.99.100:2377
... ...
@@ -167,6 +167,7 @@ nodes:
167 167
 $docker swarm join-token  --rotate worker
168 168
 
169 169
 To add a worker to this swarm, run the following command:
170
+
170 171
     docker swarm join \
171 172
     --token SWMTKN-1-2kscvs0zuymrsc9t0ocyy1rdns9dhaodvpl639j2bqx55uptag-ebmn5u927reawo27s3azntd44 \
172 173
     172.17.0.2:2377
... ...
@@ -36,6 +36,7 @@ This tutorial uses the name `worker1`.
36 36
     $ docker swarm join-token worker
37 37
 
38 38
     To add a worker to this swarm, run the following command:
39
+
39 40
         docker swarm join \
40 41
         --token SWMTKN-1-49nj1cmql0jkz5s954yi3oex3nedyz0fb0xx14ie39trti4wxv-8vxv8rssmk743ojnwacrr2e7c \
41 42
         192.168.99.100:2377
... ...
@@ -33,14 +33,12 @@ node. For example, the tutorial uses a machine named `manager1`.
33 33
     Swarm initialized: current node (dxn1zf6l61qsb1josjja83ngz) is now a manager.
34 34
 
35 35
     To add a worker to this swarm, run the following command:
36
+
36 37
         docker swarm join \
37 38
         --token SWMTKN-1-49nj1cmql0jkz5s954yi3oex3nedyz0fb0xx14ie39trti4wxv-8vxv8rssmk743ojnwacrr2e7c \
38 39
         192.168.99.100:2377
39 40
 
40
-    To add a manager to this swarm, run the following command:
41
-        docker swarm join \
42
-        --token SWMTKN-1-61ztec5kyafptydic6jfc1i33t37flcl4nuipzcusor96k7kby-5vy9t8u35tuqm7vh67lrz9xp6 \
43
-        192.168.99.100:2377
41
+    To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
44 42
     ```
45 43
 
46 44
     The `--advertise-addr` flag configures the manager node to publish its