mirror of https://github.com/status-im/consul.git
Convert join command to use base.Command
This commit is contained in:
parent
a3d02a4cbc
commit
8775b031d3
|
@ -1,16 +1,15 @@
|
|||
package command
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"github.com/mitchellh/cli"
|
||||
"github.com/hashicorp/consul/command/base"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// JoinCommand is a Command implementation that tells a running Consul
|
||||
// agent to join another.
|
||||
type JoinCommand struct {
|
||||
Ui cli.Ui
|
||||
base.Command
|
||||
}
|
||||
|
||||
func (c *JoinCommand) Help() string {
|
||||
|
@ -20,26 +19,21 @@ Usage: consul join [options] address ...
|
|||
Tells a running Consul agent (with "consul agent") to join the cluster
|
||||
by specifying at least one existing member.
|
||||
|
||||
Options:
|
||||
` + c.Command.Help()
|
||||
|
||||
-rpc-addr=127.0.0.1:8400 RPC address of the Consul agent.
|
||||
-wan Joins a server to another server in the WAN pool
|
||||
`
|
||||
return strings.TrimSpace(helpText)
|
||||
}
|
||||
|
||||
func (c *JoinCommand) Run(args []string) int {
|
||||
var wan bool
|
||||
|
||||
cmdFlags := flag.NewFlagSet("join", flag.ContinueOnError)
|
||||
cmdFlags.Usage = func() { c.Ui.Output(c.Help()) }
|
||||
cmdFlags.BoolVar(&wan, "wan", false, "wan")
|
||||
rpcAddr := RPCAddrFlag(cmdFlags)
|
||||
if err := cmdFlags.Parse(args); err != nil {
|
||||
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 {
|
||||
return 1
|
||||
}
|
||||
|
||||
addrs := cmdFlags.Args()
|
||||
addrs := f.Args()
|
||||
if len(addrs) == 0 {
|
||||
c.Ui.Error("At least one address to join must be specified.")
|
||||
c.Ui.Error("")
|
||||
|
@ -47,21 +41,24 @@ func (c *JoinCommand) Run(args []string) int {
|
|||
return 1
|
||||
}
|
||||
|
||||
client, err := RPCClient(*rpcAddr)
|
||||
client, err := c.Command.HTTPClient()
|
||||
if err != nil {
|
||||
c.Ui.Error(fmt.Sprintf("Error connecting to Consul agent: %s", err))
|
||||
return 1
|
||||
}
|
||||
defer client.Close()
|
||||
|
||||
n, err := client.Join(addrs, wan)
|
||||
if err != nil {
|
||||
c.Ui.Error(fmt.Sprintf("Error joining the cluster: %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++
|
||||
}
|
||||
}
|
||||
|
||||
c.Ui.Output(fmt.Sprintf(
|
||||
"Successfully joined cluster by contacting %d nodes.", n))
|
||||
"Successfully joined cluster by contacting %d nodes.", joins))
|
||||
return 0
|
||||
}
|
||||
|
||||
|
|
|
@ -2,11 +2,22 @@ package command
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/hashicorp/consul/command/base"
|
||||
"github.com/mitchellh/cli"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func testJoinCommand(t *testing.T) (*cli.MockUi, *JoinCommand) {
|
||||
ui := new(cli.MockUi)
|
||||
return ui, &JoinCommand{
|
||||
Command: base.Command{
|
||||
Ui: ui,
|
||||
Flags: base.FlagSetClientHTTP,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func TestJoinCommand_implements(t *testing.T) {
|
||||
var _ cli.Command = &JoinCommand{}
|
||||
}
|
||||
|
@ -17,10 +28,9 @@ func TestJoinCommandRun(t *testing.T) {
|
|||
defer a1.Shutdown()
|
||||
defer a2.Shutdown()
|
||||
|
||||
ui := new(cli.MockUi)
|
||||
c := &JoinCommand{Ui: ui}
|
||||
ui, c := testJoinCommand(t)
|
||||
args := []string{
|
||||
"-rpc-addr=" + a1.addr,
|
||||
"-http-addr=" + a1.httpAddr,
|
||||
fmt.Sprintf("127.0.0.1:%d", a2.config.Ports.SerfLan),
|
||||
}
|
||||
|
||||
|
@ -40,10 +50,9 @@ func TestJoinCommandRun_wan(t *testing.T) {
|
|||
defer a1.Shutdown()
|
||||
defer a2.Shutdown()
|
||||
|
||||
ui := new(cli.MockUi)
|
||||
c := &JoinCommand{Ui: ui}
|
||||
ui, c := testJoinCommand(t)
|
||||
args := []string{
|
||||
"-rpc-addr=" + a1.addr,
|
||||
"-http-addr=" + a1.httpAddr,
|
||||
"-wan",
|
||||
fmt.Sprintf("127.0.0.1:%d", a2.config.Ports.SerfWan),
|
||||
}
|
||||
|
@ -59,9 +68,8 @@ func TestJoinCommandRun_wan(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestJoinCommandRun_noAddrs(t *testing.T) {
|
||||
ui := new(cli.MockUi)
|
||||
c := &JoinCommand{Ui: ui}
|
||||
args := []string{"-rpc-addr=foo"}
|
||||
ui, c := testJoinCommand(t)
|
||||
args := []string{"-http-addr=foo"}
|
||||
|
||||
code := c.Run(args)
|
||||
if code != 1 {
|
||||
|
|
15
commands.go
15
commands.go
|
@ -76,6 +76,15 @@ func init() {
|
|||
}, nil
|
||||
},
|
||||
|
||||
"join": func() (cli.Command, error) {
|
||||
return &command.JoinCommand{
|
||||
Command: base.Command{
|
||||
Ui: ui,
|
||||
Flags: base.FlagSetClientHTTP,
|
||||
},
|
||||
}, nil
|
||||
},
|
||||
|
||||
"kv": func() (cli.Command, error) {
|
||||
return &command.KVCommand{
|
||||
Ui: ui,
|
||||
|
@ -112,12 +121,6 @@ func init() {
|
|||
}, nil
|
||||
},
|
||||
|
||||
"join": func() (cli.Command, error) {
|
||||
return &command.JoinCommand{
|
||||
Ui: ui,
|
||||
}, nil
|
||||
},
|
||||
|
||||
"keygen": func() (cli.Command, error) {
|
||||
return &command.KeygenCommand{
|
||||
Ui: ui,
|
||||
|
|
|
@ -31,14 +31,14 @@ You may call join with multiple addresses if you want to try to join
|
|||
multiple clusters. Consul will attempt to join all addresses, and the join
|
||||
command will fail only if Consul was unable to join with any.
|
||||
|
||||
The command-line flags are all optional. The list of available flags are:
|
||||
#### API Options
|
||||
|
||||
<%= partial "docs/commands/http_api_options_client" %>
|
||||
|
||||
#### Command Options
|
||||
|
||||
* `-wan` - For agents running in server mode, the agent will attempt to join
|
||||
other servers gossiping in a WAN cluster. This is used to form a bridge between
|
||||
multiple datacenters.
|
||||
|
||||
* `-rpc-addr` - Address to the RPC server of the agent you want to contact
|
||||
to send this command. If this isn't specified, the command checks the
|
||||
CONSUL_RPC_ADDR env variable. If this isn't set, the default RPC
|
||||
address will be set to "127.0.0.1:8400".
|
||||
|
Loading…
Reference in New Issue