open-nomad/command/server_join.go

78 lines
1.7 KiB
Go
Raw Normal View History

2015-09-11 22:48:36 +00:00
package command
import (
"fmt"
"strings"
2018-05-11 19:19:16 +00:00
"github.com/posener/complete"
2015-09-11 22:48:36 +00:00
)
type ServerJoinCommand struct {
Meta
2015-09-11 22:48:36 +00:00
}
func (c *ServerJoinCommand) Help() string {
2015-09-11 22:48:36 +00:00
helpText := `
2018-03-21 00:37:28 +00:00
Usage: nomad server join [options] <addr> [<addr>...]
2015-09-11 22:48:36 +00:00
Joins the local server to one or more Nomad servers. Joining is
only required for server nodes, and only needs to succeed
against one or more of the provided addresses. Once joined, the
gossip layer will handle discovery of the other server nodes in
the cluster.
General Options:
2015-09-11 22:48:36 +00:00
` + generalOptionsUsage()
2015-09-11 22:48:36 +00:00
return strings.TrimSpace(helpText)
}
func (c *ServerJoinCommand) Synopsis() string {
return "Join server nodes together"
2015-09-11 22:48:36 +00:00
}
2018-05-11 19:19:16 +00:00
func (c *ServerJoinCommand) AutocompleteFlags() complete.Flags {
return c.Meta.AutocompleteFlags(FlagSetClient)
}
func (c *ServerJoinCommand) AutocompleteArgs() complete.Predictor {
return complete.PredictNothing
}
func (c *ServerJoinCommand) Name() string { return "server join" }
func (c *ServerJoinCommand) Run(args []string) int {
flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
2015-09-11 22:48:36 +00:00
flags.Usage = func() { c.Ui.Output(c.Help()) }
if err := flags.Parse(args); err != nil {
return 1
}
// Check that we got at least one node
args = flags.Args()
if len(args) < 1 {
c.Ui.Error("One or more node addresses must be given as arguments")
c.Ui.Error(commandErrorText(c))
2015-09-11 22:48:36 +00:00
return 1
}
nodes := args
2015-09-11 22:48:36 +00:00
// Get the HTTP client
client, err := c.Meta.Client()
2015-09-11 22:48:36 +00:00
if err != nil {
c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
2015-09-11 22:48:36 +00:00
return 1
}
// Attempt the join
n, err := client.Agent().Join(nodes...)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error joining: %s", err))
2015-09-11 22:48:36 +00:00
return 1
}
// Success
c.Ui.Output(fmt.Sprintf("Joined %d servers successfully", n))
2015-09-11 22:48:36 +00:00
return 0
}