init.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package swarm
  2. import (
  3. "fmt"
  4. "strings"
  5. "golang.org/x/net/context"
  6. "github.com/docker/docker/api/types/swarm"
  7. "github.com/docker/docker/cli"
  8. "github.com/docker/docker/cli/command"
  9. "github.com/pkg/errors"
  10. "github.com/spf13/cobra"
  11. "github.com/spf13/pflag"
  12. )
  13. type initOptions struct {
  14. swarmOptions
  15. listenAddr NodeAddrOption
  16. // Not a NodeAddrOption because it has no default port.
  17. advertiseAddr string
  18. dataPathAddr string
  19. forceNewCluster bool
  20. availability string
  21. }
  22. func newInitCommand(dockerCli command.Cli) *cobra.Command {
  23. opts := initOptions{
  24. listenAddr: NewListenAddrOption(),
  25. }
  26. cmd := &cobra.Command{
  27. Use: "init [OPTIONS]",
  28. Short: "Initialize a swarm",
  29. Args: cli.NoArgs,
  30. RunE: func(cmd *cobra.Command, args []string) error {
  31. return runInit(dockerCli, cmd.Flags(), opts)
  32. },
  33. }
  34. flags := cmd.Flags()
  35. flags.Var(&opts.listenAddr, flagListenAddr, "Listen address (format: <ip|interface>[:port])")
  36. flags.StringVar(&opts.advertiseAddr, flagAdvertiseAddr, "", "Advertised address (format: <ip|interface>[:port])")
  37. flags.StringVar(&opts.dataPathAddr, flagDataPathAddr, "", "Address or interface to use for data path traffic (format: <ip|interface>)")
  38. flags.BoolVar(&opts.forceNewCluster, "force-new-cluster", false, "Force create a new cluster from current state")
  39. flags.BoolVar(&opts.autolock, flagAutolock, false, "Enable manager autolocking (requiring an unlock key to start a stopped manager)")
  40. flags.StringVar(&opts.availability, flagAvailability, "active", `Availability of the node ("active"|"pause"|"drain")`)
  41. addSwarmFlags(flags, &opts.swarmOptions)
  42. return cmd
  43. }
  44. func runInit(dockerCli command.Cli, flags *pflag.FlagSet, opts initOptions) error {
  45. client := dockerCli.Client()
  46. ctx := context.Background()
  47. req := swarm.InitRequest{
  48. ListenAddr: opts.listenAddr.String(),
  49. AdvertiseAddr: opts.advertiseAddr,
  50. DataPathAddr: opts.dataPathAddr,
  51. ForceNewCluster: opts.forceNewCluster,
  52. Spec: opts.swarmOptions.ToSpec(flags),
  53. AutoLockManagers: opts.swarmOptions.autolock,
  54. }
  55. if flags.Changed(flagAvailability) {
  56. availability := swarm.NodeAvailability(strings.ToLower(opts.availability))
  57. switch availability {
  58. case swarm.NodeAvailabilityActive, swarm.NodeAvailabilityPause, swarm.NodeAvailabilityDrain:
  59. req.Availability = availability
  60. default:
  61. return errors.Errorf("invalid availability %q, only active, pause and drain are supported", opts.availability)
  62. }
  63. }
  64. nodeID, err := client.SwarmInit(ctx, req)
  65. if err != nil {
  66. if strings.Contains(err.Error(), "could not choose an IP address to advertise") || strings.Contains(err.Error(), "could not find the system's IP address") {
  67. return errors.New(err.Error() + " - specify one with --advertise-addr")
  68. }
  69. return err
  70. }
  71. fmt.Fprintf(dockerCli.Out(), "Swarm initialized: current node (%s) is now a manager.\n\n", nodeID)
  72. if err := printJoinCommand(ctx, dockerCli, nodeID, true, false); err != nil {
  73. return err
  74. }
  75. fmt.Fprint(dockerCli.Out(), "To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.\n\n")
  76. if req.AutoLockManagers {
  77. unlockKeyResp, err := client.SwarmGetUnlockKey(ctx)
  78. if err != nil {
  79. return errors.Wrap(err, "could not fetch unlock key")
  80. }
  81. printUnlockCommand(ctx, dockerCli, unlockKeyResp.UnlockKey)
  82. }
  83. return nil
  84. }