github.com/blixtra/nomad@v0.7.2-0.20171221000451-da9a1d7bb050/command/server_join.go (about)

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