forked from hashicorp/consul
-
Notifications
You must be signed in to change notification settings - Fork 0
/
join.go
71 lines (57 loc) · 1.45 KB
/
join.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package command
import (
"fmt"
"strings"
)
// JoinCommand is a Command implementation that tells a running Consul
// agent to join another.
type JoinCommand struct {
BaseCommand
}
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.BaseCommand.Help()
return strings.TrimSpace(helpText)
}
func (c *JoinCommand) Run(args []string) int {
var wan bool
f := c.BaseCommand.NewFlagSet(c)
f.BoolVar(&wan, "wan", false, "Joins a server to another server in the WAN pool.")
if err := c.BaseCommand.Parse(args); err != nil {
return 1
}
addrs := f.Args()
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.BaseCommand.HTTPClient()
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++
}
}
if joins == 0 {
c.UI.Error("Failed to join any nodes.")
return 1
}
c.UI.Output(fmt.Sprintf(
"Successfully joined cluster by contacting %d nodes.", joins))
return 0
}
func (c *JoinCommand) Synopsis() string {
return "Tell Consul agent to join cluster"
}