2013-12-31 21:06:33 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-02-08 22:14:02 +00:00
|
|
|
"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 {
|
2017-02-08 22:14:02 +00:00
|
|
|
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.
|
|
|
|
|
2017-02-08 22:14:02 +00:00
|
|
|
` + 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
|
|
|
|
|
2017-02-08 22:14:02 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-02-08 22:14:02 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-02-08 22:14:02 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-02-08 22:14:02 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-02-09 00:45:13 +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(
|
2017-02-08 22:14:02 +00:00
|
|
|
"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"
|
|
|
|
}
|