join_token.go 2.8 KB

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