1package command
2
3import (
4	"fmt"
5	"strings"
6
7	"github.com/posener/complete"
8)
9
10type ServerJoinCommand struct {
11	Meta
12}
13
14func (c *ServerJoinCommand) Help() string {
15	helpText := `
16Usage: 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
24General Options:
25
26  ` + generalOptionsUsage()
27	return strings.TrimSpace(helpText)
28}
29
30func (c *ServerJoinCommand) Synopsis() string {
31	return "Join server nodes together"
32}
33
34func (c *ServerJoinCommand) AutocompleteFlags() complete.Flags {
35	return c.Meta.AutocompleteFlags(FlagSetClient)
36}
37
38func (c *ServerJoinCommand) AutocompleteArgs() complete.Predictor {
39	return complete.PredictNothing
40}
41
42func (c *ServerJoinCommand) Name() string { return "server join" }
43
44func (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}
78