Convert join command to use base.Command
This commit is contained in:
parent
a69f2a0faf
commit
9e156286c7
|
@ -1,16 +1,15 @@
|
||||||
package command
|
package command
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/mitchellh/cli"
|
"github.com/hashicorp/consul/command/base"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// JoinCommand is a Command implementation that tells a running Consul
|
// JoinCommand is a Command implementation that tells a running Consul
|
||||||
// agent to join another.
|
// agent to join another.
|
||||||
type JoinCommand struct {
|
type JoinCommand struct {
|
||||||
Ui cli.Ui
|
base.Command
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *JoinCommand) Help() string {
|
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
|
Tells a running Consul agent (with "consul agent") to join the cluster
|
||||||
by specifying at least one existing member.
|
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)
|
return strings.TrimSpace(helpText)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *JoinCommand) Run(args []string) int {
|
func (c *JoinCommand) Run(args []string) int {
|
||||||
var wan bool
|
var wan bool
|
||||||
|
|
||||||
cmdFlags := flag.NewFlagSet("join", flag.ContinueOnError)
|
f := c.Command.NewFlagSet(c)
|
||||||
cmdFlags.Usage = func() { c.Ui.Output(c.Help()) }
|
f.BoolVar(&wan, "wan", false, "Joins a server to another server in the WAN pool.")
|
||||||
cmdFlags.BoolVar(&wan, "wan", false, "wan")
|
if err := c.Command.Parse(args); err != nil {
|
||||||
rpcAddr := RPCAddrFlag(cmdFlags)
|
|
||||||
if err := cmdFlags.Parse(args); err != nil {
|
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
addrs := cmdFlags.Args()
|
addrs := f.Args()
|
||||||
if len(addrs) == 0 {
|
if len(addrs) == 0 {
|
||||||
c.Ui.Error("At least one address to join must be specified.")
|
c.Ui.Error("At least one address to join must be specified.")
|
||||||
c.Ui.Error("")
|
c.Ui.Error("")
|
||||||
|
@ -47,21 +41,24 @@ func (c *JoinCommand) Run(args []string) int {
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
client, err := RPCClient(*rpcAddr)
|
client, err := c.Command.HTTPClient()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.Ui.Error(fmt.Sprintf("Error connecting to Consul agent: %s", err))
|
c.Ui.Error(fmt.Sprintf("Error connecting to Consul agent: %s", err))
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
defer client.Close()
|
|
||||||
|
|
||||||
n, err := client.Join(addrs, wan)
|
joins := 0
|
||||||
|
for _, addr := range addrs {
|
||||||
|
err := client.Agent().Join(addr, wan)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.Ui.Error(fmt.Sprintf("Error joining the cluster: %s", err))
|
c.Ui.Error(fmt.Sprintf("Error joining address '%s': %s", addr, err))
|
||||||
return 1
|
} else {
|
||||||
|
joins++
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
c.Ui.Output(fmt.Sprintf(
|
c.Ui.Output(fmt.Sprintf(
|
||||||
"Successfully joined cluster by contacting %d nodes.", n))
|
"Successfully joined cluster by contacting %d nodes.", joins))
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,11 +2,22 @@ package command
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/hashicorp/consul/command/base"
|
||||||
"github.com/mitchellh/cli"
|
"github.com/mitchellh/cli"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"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) {
|
func TestJoinCommand_implements(t *testing.T) {
|
||||||
var _ cli.Command = &JoinCommand{}
|
var _ cli.Command = &JoinCommand{}
|
||||||
}
|
}
|
||||||
|
@ -17,10 +28,9 @@ func TestJoinCommandRun(t *testing.T) {
|
||||||
defer a1.Shutdown()
|
defer a1.Shutdown()
|
||||||
defer a2.Shutdown()
|
defer a2.Shutdown()
|
||||||
|
|
||||||
ui := new(cli.MockUi)
|
ui, c := testJoinCommand(t)
|
||||||
c := &JoinCommand{Ui: ui}
|
|
||||||
args := []string{
|
args := []string{
|
||||||
"-rpc-addr=" + a1.addr,
|
"-http-addr=" + a1.httpAddr,
|
||||||
fmt.Sprintf("127.0.0.1:%d", a2.config.Ports.SerfLan),
|
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 a1.Shutdown()
|
||||||
defer a2.Shutdown()
|
defer a2.Shutdown()
|
||||||
|
|
||||||
ui := new(cli.MockUi)
|
ui, c := testJoinCommand(t)
|
||||||
c := &JoinCommand{Ui: ui}
|
|
||||||
args := []string{
|
args := []string{
|
||||||
"-rpc-addr=" + a1.addr,
|
"-http-addr=" + a1.httpAddr,
|
||||||
"-wan",
|
"-wan",
|
||||||
fmt.Sprintf("127.0.0.1:%d", a2.config.Ports.SerfWan),
|
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) {
|
func TestJoinCommandRun_noAddrs(t *testing.T) {
|
||||||
ui := new(cli.MockUi)
|
ui, c := testJoinCommand(t)
|
||||||
c := &JoinCommand{Ui: ui}
|
args := []string{"-http-addr=foo"}
|
||||||
args := []string{"-rpc-addr=foo"}
|
|
||||||
|
|
||||||
code := c.Run(args)
|
code := c.Run(args)
|
||||||
if code != 1 {
|
if code != 1 {
|
||||||
|
|
15
commands.go
15
commands.go
|
@ -76,6 +76,15 @@ func init() {
|
||||||
}, nil
|
}, nil
|
||||||
},
|
},
|
||||||
|
|
||||||
|
"join": func() (cli.Command, error) {
|
||||||
|
return &command.JoinCommand{
|
||||||
|
Command: base.Command{
|
||||||
|
Ui: ui,
|
||||||
|
Flags: base.FlagSetClientHTTP,
|
||||||
|
},
|
||||||
|
}, nil
|
||||||
|
},
|
||||||
|
|
||||||
"kv": func() (cli.Command, error) {
|
"kv": func() (cli.Command, error) {
|
||||||
return &command.KVCommand{
|
return &command.KVCommand{
|
||||||
Ui: ui,
|
Ui: ui,
|
||||||
|
@ -112,12 +121,6 @@ func init() {
|
||||||
}, nil
|
}, nil
|
||||||
},
|
},
|
||||||
|
|
||||||
"join": func() (cli.Command, error) {
|
|
||||||
return &command.JoinCommand{
|
|
||||||
Ui: ui,
|
|
||||||
}, nil
|
|
||||||
},
|
|
||||||
|
|
||||||
"keygen": func() (cli.Command, error) {
|
"keygen": func() (cli.Command, error) {
|
||||||
return &command.KeygenCommand{
|
return &command.KeygenCommand{
|
||||||
Ui: ui,
|
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
|
multiple clusters. Consul will attempt to join all addresses, and the join
|
||||||
command will fail only if Consul was unable to join with any.
|
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
|
* `-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
|
other servers gossiping in a WAN cluster. This is used to form a bridge between
|
||||||
multiple datacenters.
|
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