github.com/justincormack/cli@v0.0.0-20201215022714-831ebeae9675/cli/command/swarm/join_token.go (about) 1 package swarm 2 3 import ( 4 "context" 5 "fmt" 6 7 "github.com/docker/cli/cli" 8 "github.com/docker/cli/cli/command" 9 "github.com/docker/docker/api/types/swarm" 10 "github.com/pkg/errors" 11 "github.com/spf13/cobra" 12 ) 13 14 type joinTokenOptions struct { 15 role string 16 rotate bool 17 quiet bool 18 } 19 20 func newJoinTokenCommand(dockerCli command.Cli) *cobra.Command { 21 opts := joinTokenOptions{} 22 23 cmd := &cobra.Command{ 24 Use: "join-token [OPTIONS] (worker|manager)", 25 Short: "Manage join tokens", 26 Args: cli.ExactArgs(1), 27 RunE: func(cmd *cobra.Command, args []string) error { 28 opts.role = args[0] 29 return runJoinToken(dockerCli, opts) 30 }, 31 } 32 33 flags := cmd.Flags() 34 flags.BoolVar(&opts.rotate, flagRotate, false, "Rotate join token") 35 flags.BoolVarP(&opts.quiet, flagQuiet, "q", false, "Only display token") 36 37 return cmd 38 } 39 40 func runJoinToken(dockerCli command.Cli, opts joinTokenOptions) error { 41 worker := opts.role == "worker" 42 manager := opts.role == "manager" 43 44 if !worker && !manager { 45 return errors.New("unknown role " + opts.role) 46 } 47 48 client := dockerCli.Client() 49 ctx := context.Background() 50 51 if opts.rotate { 52 flags := swarm.UpdateFlags{ 53 RotateWorkerToken: worker, 54 RotateManagerToken: manager, 55 } 56 57 sw, err := client.SwarmInspect(ctx) 58 if err != nil { 59 return err 60 } 61 62 if err := client.SwarmUpdate(ctx, sw.Version, sw.Spec, flags); err != nil { 63 return err 64 } 65 66 if !opts.quiet { 67 fmt.Fprintf(dockerCli.Out(), "Successfully rotated %s join token.\n\n", opts.role) 68 } 69 } 70 71 // second SwarmInspect in this function, 72 // this is necessary since SwarmUpdate after first changes the join tokens 73 sw, err := client.SwarmInspect(ctx) 74 if err != nil { 75 return err 76 } 77 78 if opts.quiet && worker { 79 fmt.Fprintln(dockerCli.Out(), sw.JoinTokens.Worker) 80 return nil 81 } 82 83 if opts.quiet && manager { 84 fmt.Fprintln(dockerCli.Out(), sw.JoinTokens.Manager) 85 return nil 86 } 87 88 info, err := client.Info(ctx) 89 if err != nil { 90 return err 91 } 92 93 return printJoinCommand(ctx, dockerCli, info.Swarm.NodeID, worker, manager) 94 } 95 96 func printJoinCommand(ctx context.Context, dockerCli command.Cli, nodeID string, worker bool, manager bool) error { 97 client := dockerCli.Client() 98 99 node, _, err := client.NodeInspectWithRaw(ctx, nodeID) 100 if err != nil { 101 return err 102 } 103 104 sw, err := client.SwarmInspect(ctx) 105 if err != nil { 106 return err 107 } 108 109 if node.ManagerStatus != nil { 110 if worker { 111 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.JoinTokens.Worker, node.ManagerStatus.Addr) 112 } 113 if manager { 114 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.JoinTokens.Manager, node.ManagerStatus.Addr) 115 } 116 } 117 118 return nil 119 }