(ctx context.Context, dockerCLI command.Cli, opts joinTokenOptions)
| 43 | } |
| 44 | |
| 45 | func runJoinToken(ctx context.Context, dockerCLI command.Cli, opts joinTokenOptions) error { |
| 46 | worker := opts.role == "worker" |
| 47 | manager := opts.role == "manager" |
| 48 | |
| 49 | if !worker && !manager { |
| 50 | return errors.New("unknown role " + opts.role) |
| 51 | } |
| 52 | |
| 53 | apiClient := dockerCLI.Client() |
| 54 | |
| 55 | if opts.rotate { |
| 56 | res, err := apiClient.SwarmInspect(ctx, client.SwarmInspectOptions{}) |
| 57 | if err != nil { |
| 58 | return err |
| 59 | } |
| 60 | |
| 61 | _, err = apiClient.SwarmUpdate(ctx, client.SwarmUpdateOptions{ |
| 62 | Version: res.Swarm.Version, |
| 63 | Spec: res.Swarm.Spec, |
| 64 | RotateWorkerToken: worker, |
| 65 | RotateManagerToken: manager, |
| 66 | }) |
| 67 | if err != nil { |
| 68 | return err |
| 69 | } |
| 70 | |
| 71 | if !opts.quiet { |
| 72 | _, _ = fmt.Fprintf(dockerCLI.Out(), "Successfully rotated %s join token.\n\n", opts.role) |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | // second SwarmInspect in this function, |
| 77 | // this is necessary since SwarmUpdate after first changes the join tokens |
| 78 | res, err := apiClient.SwarmInspect(ctx, client.SwarmInspectOptions{}) |
| 79 | if err != nil { |
| 80 | return err |
| 81 | } |
| 82 | |
| 83 | if opts.quiet && worker { |
| 84 | _, _ = fmt.Fprintln(dockerCLI.Out(), res.Swarm.JoinTokens.Worker) |
| 85 | return nil |
| 86 | } |
| 87 | |
| 88 | if opts.quiet && manager { |
| 89 | _, _ = fmt.Fprintln(dockerCLI.Out(), res.Swarm.JoinTokens.Manager) |
| 90 | return nil |
| 91 | } |
| 92 | |
| 93 | infoResp, err := apiClient.Info(ctx, client.InfoOptions{}) |
| 94 | if err != nil { |
| 95 | return err |
| 96 | } |
| 97 | |
| 98 | return printJoinCommand(ctx, dockerCLI, infoResp.Info.Swarm.NodeID, worker, manager) |
| 99 | } |
| 100 | |
| 101 | func printJoinCommand(ctx context.Context, dockerCLI command.Cli, nodeID string, worker bool, manager bool) error { |
| 102 | apiClient := dockerCLI.Client() |
no test coverage detected
searching dependent graphs…