open-consul/command/join.go

73 lines
1.5 KiB
Go
Raw Normal View History

2013-12-31 21:06:33 +00:00
package command
import (
"fmt"
"github.com/hashicorp/consul/command/base"
2013-12-31 21:06:33 +00:00
"strings"
)
// JoinCommand is a Command implementation that tells a running Consul
// agent to join another.
type JoinCommand struct {
base.Command
2013-12-31 21:06:33 +00:00
}
func (c *JoinCommand) Help() string {
helpText := `
Usage: consul join [options] address ...
Tells a running Consul agent (with "consul agent") to join the cluster
by specifying at least one existing member.
` + c.Command.Help()
2013-12-31 21:06:33 +00:00
return strings.TrimSpace(helpText)
}
func (c *JoinCommand) Run(args []string) int {
var wan bool
f := c.Command.NewFlagSet(c)
f.BoolVar(&wan, "wan", false, "Joins a server to another server in the WAN pool.")
if err := c.Command.Parse(args); err != nil {
2013-12-31 21:06:33 +00:00
return 1
}
addrs := f.Args()
2013-12-31 21:06:33 +00:00
if len(addrs) == 0 {
c.Ui.Error("At least one address to join must be specified.")
c.Ui.Error("")
c.Ui.Error(c.Help())
return 1
}
client, err := c.Command.HTTPClient()
2013-12-31 21:06:33 +00:00
if err != nil {
c.Ui.Error(fmt.Sprintf("Error connecting to Consul agent: %s", err))
return 1
}
joins := 0
for _, addr := range addrs {
err := client.Agent().Join(addr, wan)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error joining address '%s': %s", addr, err))
} else {
joins++
}
2013-12-31 21:06:33 +00:00
}
if joins == 0 {
c.Ui.Error("Failed to join any nodes.")
return 1
}
2013-12-31 21:06:33 +00:00
c.Ui.Output(fmt.Sprintf(
"Successfully joined cluster by contacting %d nodes.", joins))
2013-12-31 21:06:33 +00:00
return 0
}
func (c *JoinCommand) Synopsis() string {
return "Tell Consul agent to join cluster"
}