-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathjoin_token.go
More file actions
130 lines (107 loc) · 3.13 KB
/
join_token.go
File metadata and controls
130 lines (107 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package swarm
import (
"context"
"errors"
"fmt"
"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
"github.com/moby/moby/client"
"github.com/spf13/cobra"
)
type joinTokenOptions struct {
role string
rotate bool
quiet bool
}
func newJoinTokenCommand(dockerCLI command.Cli) *cobra.Command {
opts := joinTokenOptions{}
cmd := &cobra.Command{
Use: "join-token [OPTIONS] (worker|manager)",
Short: "Manage join tokens",
Args: cli.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
opts.role = args[0]
return runJoinToken(cmd.Context(), dockerCLI, opts)
},
Annotations: map[string]string{
"version": "1.24",
"swarm": "manager",
},
DisableFlagsInUseLine: true,
}
flags := cmd.Flags()
flags.BoolVar(&opts.rotate, flagRotate, false, "Rotate join token")
flags.BoolVarP(&opts.quiet, flagQuiet, "q", false, "Only display token")
return cmd
}
func runJoinToken(ctx context.Context, dockerCLI command.Cli, opts joinTokenOptions) error {
worker := opts.role == "worker"
manager := opts.role == "manager"
if !worker && !manager {
return errors.New("unknown role " + opts.role)
}
apiClient := dockerCLI.Client()
if opts.rotate {
res, err := apiClient.SwarmInspect(ctx, client.SwarmInspectOptions{})
if err != nil {
return err
}
_, err = apiClient.SwarmUpdate(ctx, client.SwarmUpdateOptions{
Version: res.Swarm.Version,
Spec: res.Swarm.Spec,
RotateWorkerToken: worker,
RotateManagerToken: manager,
})
if err != nil {
return err
}
if !opts.quiet {
_, _ = fmt.Fprintf(dockerCLI.Out(), "Successfully rotated %s join token.\n\n", opts.role)
}
}
// second SwarmInspect in this function,
// this is necessary since SwarmUpdate after first changes the join tokens
res, err := apiClient.SwarmInspect(ctx, client.SwarmInspectOptions{})
if err != nil {
return err
}
if opts.quiet && worker {
_, _ = fmt.Fprintln(dockerCLI.Out(), res.Swarm.JoinTokens.Worker)
return nil
}
if opts.quiet && manager {
_, _ = fmt.Fprintln(dockerCLI.Out(), res.Swarm.JoinTokens.Manager)
return nil
}
infoResp, err := apiClient.Info(ctx, client.InfoOptions{})
if err != nil {
return err
}
return printJoinCommand(ctx, dockerCLI, infoResp.Info.Swarm.NodeID, worker, manager)
}
func printJoinCommand(ctx context.Context, dockerCLI command.Cli, nodeID string, worker bool, manager bool) error {
apiClient := dockerCLI.Client()
res, err := apiClient.NodeInspect(ctx, nodeID, client.NodeInspectOptions{})
if err != nil {
return err
}
sw, err := apiClient.SwarmInspect(ctx, client.SwarmInspectOptions{})
if err != nil {
return err
}
if res.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 --token %s %s\n\n",
sw.Swarm.JoinTokens.Worker, res.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 --token %s %s\n\n",
sw.Swarm.JoinTokens.Manager, res.Node.ManagerStatus.Addr,
)
}
}
return nil
}