github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/command/server_join.go (about) 1 package command 2 3 import ( 4 "fmt" 5 "strings" 6 7 "github.com/posener/complete" 8 ) 9 10 type ServerJoinCommand struct { 11 Meta 12 } 13 14 func (c *ServerJoinCommand) Help() string { 15 helpText := ` 16 Usage: nomad server join [options] <addr> [<addr>...] 17 18 Joins the local server to one or more Nomad servers. Joining is 19 only required for server nodes, and only needs to succeed 20 against one or more of the provided addresses. Once joined, the 21 gossip layer will handle discovery of the other server nodes in 22 the cluster. 23 24 General Options: 25 26 ` + generalOptionsUsage(usageOptsDefault|usageOptsNoNamespace) 27 return strings.TrimSpace(helpText) 28 } 29 30 func (c *ServerJoinCommand) Synopsis() string { 31 return "Join server nodes together" 32 } 33 34 func (c *ServerJoinCommand) AutocompleteFlags() complete.Flags { 35 return c.Meta.AutocompleteFlags(FlagSetClient) 36 } 37 38 func (c *ServerJoinCommand) AutocompleteArgs() complete.Predictor { 39 return complete.PredictNothing 40 } 41 42 func (c *ServerJoinCommand) Name() string { return "server join" } 43 44 func (c *ServerJoinCommand) Run(args []string) int { 45 flags := c.Meta.FlagSet(c.Name(), FlagSetClient) 46 flags.Usage = func() { c.Ui.Output(c.Help()) } 47 if err := flags.Parse(args); err != nil { 48 return 1 49 } 50 51 // Check that we got at least one node 52 args = flags.Args() 53 if len(args) < 1 { 54 c.Ui.Error("One or more node addresses must be given as arguments") 55 c.Ui.Error(commandErrorText(c)) 56 return 1 57 } 58 nodes := args 59 60 // Get the HTTP client 61 client, err := c.Meta.Client() 62 if err != nil { 63 c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err)) 64 return 1 65 } 66 67 // Attempt the join 68 n, err := client.Agent().Join(nodes...) 69 if err != nil { 70 c.Ui.Error(fmt.Sprintf("Error joining: %s", err)) 71 return 1 72 } 73 74 // Success 75 c.Ui.Output(fmt.Sprintf("Joined %d servers successfully", n)) 76 return 0 77 }