command: add agent-join command

This commit is contained in:
Ryan Uber 2015-09-11 15:48:36 -07:00
parent 761aad186a
commit 3118043a4c
3 changed files with 94 additions and 0 deletions

77
command/agent_join.go Normal file
View file

@ -0,0 +1,77 @@
package command
import (
"flag"
"fmt"
"strings"
"github.com/mitchellh/cli"
)
type AgentJoinCommand struct {
Ui cli.Ui
}
func (c *AgentJoinCommand) Help() string {
helpText := `
Usage: nomad agent-join [options] <addr> [<addr>...]
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.
Options:
-help
Display this message
-http-addr
Address of the Nomad API to connect. Can also be specified
using the environment variable NOMAD_HTTP_ADDR.
Default = http://127.0.0.1:4646
`
return strings.TrimSpace(helpText)
}
func (c *AgentJoinCommand) Synopsis() string {
return "Joins server nodes together"
}
func (c *AgentJoinCommand) Run(args []string) int {
var httpAddr *string
flags := flag.NewFlagSet("agent-join", flag.ContinueOnError)
flags.Usage = func() { c.Ui.Output(c.Help()) }
httpAddr = httpAddrFlag(flags)
if err := flags.Parse(args); err != nil {
return 1
}
// Check that we got at least one node
if len(flags.Args()) < 1 {
c.Ui.Error(c.Help())
return 1
}
nodes := flags.Args()
// Get the HTTP client
client, err := httpClient(*httpAddr)
if err != nil {
c.Ui.Error(fmt.Sprintf("Failed initializing Nomad client: %s", err))
return 1
}
// Attempt the join
n, err := client.Agent().Join(nodes...)
if err != nil {
c.Ui.Error(fmt.Sprintf("Failed to join: %s", err))
return 1
}
// Success
c.Ui.Output(fmt.Sprintf("Joined %d nodes successfully", n))
return 0
}

View file

@ -0,0 +1,11 @@
package command
import (
"testing"
"github.com/mitchellh/cli"
)
func TestAgentJoinCommand_Implements(t *testing.T) {
var _ cli.Command = &AgentJoinCommand{}
}

View file

@ -46,6 +46,12 @@ func Commands(metaPtr *command.Meta) map[string]cli.CommandFactory {
}, nil
},
"agent-join": func() (cli.Command, error) {
return &command.AgentJoinCommand{
Ui: meta.Ui,
}, nil
},
"agent-members": func() (cli.Command, error) {
return &command.AgentMembersCommand{
Ui: meta.Ui,