mirror of
https://github.com/status-im/consul.git
synced 2025-01-09 21:35:52 +00:00
commands: move join command to separate pkg
This commit is contained in:
parent
e0551b80a6
commit
d870c6289b
@ -8,6 +8,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
|
"github.com/hashicorp/consul/command/join"
|
||||||
"github.com/hashicorp/consul/command/validate"
|
"github.com/hashicorp/consul/command/validate"
|
||||||
"github.com/hashicorp/consul/version"
|
"github.com/hashicorp/consul/version"
|
||||||
"github.com/mitchellh/cli"
|
"github.com/mitchellh/cli"
|
||||||
@ -108,12 +109,7 @@ func init() {
|
|||||||
},
|
},
|
||||||
|
|
||||||
"join": func() (cli.Command, error) {
|
"join": func() (cli.Command, error) {
|
||||||
return &JoinCommand{
|
return join.New(ui), nil
|
||||||
BaseCommand: BaseCommand{
|
|
||||||
Flags: FlagSetClientHTTP,
|
|
||||||
UI: ui,
|
|
||||||
},
|
|
||||||
}, nil
|
|
||||||
},
|
},
|
||||||
|
|
||||||
"keygen": func() (cli.Command, error) {
|
"keygen": func() (cli.Command, error) {
|
||||||
|
@ -1,75 +0,0 @@
|
|||||||
package command
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
)
|
|
||||||
|
|
||||||
// JoinCommand is a Command implementation that tells a running Consul
|
|
||||||
// agent to join another.
|
|
||||||
type JoinCommand struct {
|
|
||||||
BaseCommand
|
|
||||||
|
|
||||||
// flags
|
|
||||||
wan bool
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *JoinCommand) initFlags() {
|
|
||||||
c.InitFlagSet()
|
|
||||||
c.FlagSet.BoolVar(&c.wan, "wan", false,
|
|
||||||
"Joins a server to another server in the WAN pool.")
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *JoinCommand) Run(args []string) int {
|
|
||||||
c.initFlags()
|
|
||||||
if err := c.FlagSet.Parse(args); err != nil {
|
|
||||||
return 1
|
|
||||||
}
|
|
||||||
|
|
||||||
addrs := c.FlagSet.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.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, c.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) Help() string {
|
|
||||||
c.initFlags()
|
|
||||||
return c.HelpCommand(`
|
|
||||||
Usage: consul join [options] address ...
|
|
||||||
|
|
||||||
Tells a running Consul agent (with "consul agent") to join the cluster
|
|
||||||
by specifying at least one existing member.
|
|
||||||
|
|
||||||
`)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *JoinCommand) Synopsis() string {
|
|
||||||
return "Tell Consul agent to join cluster"
|
|
||||||
}
|
|
82
command/join/join.go
Normal file
82
command/join/join.go
Normal file
@ -0,0 +1,82 @@
|
|||||||
|
package join
|
||||||
|
|
||||||
|
import (
|
||||||
|
"flag"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/hashicorp/consul/command/flags"
|
||||||
|
"github.com/mitchellh/cli"
|
||||||
|
)
|
||||||
|
|
||||||
|
func New(ui cli.Ui) *cmd {
|
||||||
|
c := &cmd{UI: ui}
|
||||||
|
c.initFlags()
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
type cmd struct {
|
||||||
|
UI cli.Ui
|
||||||
|
flags *flag.FlagSet
|
||||||
|
http *flags.HTTPFlags
|
||||||
|
wan bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *cmd) initFlags() {
|
||||||
|
c.flags = flag.NewFlagSet("", flag.ContinueOnError)
|
||||||
|
c.flags.BoolVar(&c.wan, "wan", false,
|
||||||
|
"Joins a server to another server in the WAN pool.")
|
||||||
|
|
||||||
|
c.http = &flags.HTTPFlags{}
|
||||||
|
flags.Merge(c.flags, c.http.ClientFlags())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *cmd) Run(args []string) int {
|
||||||
|
if err := c.flags.Parse(args); err != nil {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
|
addrs := c.flags.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.http.APIClient()
|
||||||
|
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, c.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 *cmd) Synopsis() string {
|
||||||
|
return "Tell Consul agent to join cluster"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *cmd) Help() string {
|
||||||
|
s := `Usage: consul join [options] address ...
|
||||||
|
|
||||||
|
Tells a running Consul agent (with "consul agent") to join the cluster
|
||||||
|
by specifying at least one existing member.`
|
||||||
|
|
||||||
|
return flags.Usage(s, c.flags, c.http.ClientFlags(), nil)
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package command
|
package join
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"strings"
|
"strings"
|
||||||
@ -8,35 +8,27 @@ import (
|
|||||||
"github.com/mitchellh/cli"
|
"github.com/mitchellh/cli"
|
||||||
)
|
)
|
||||||
|
|
||||||
func testJoinCommand(t *testing.T) (*cli.MockUi, *JoinCommand) {
|
func TestJoinCommand_noTabs(t *testing.T) {
|
||||||
ui := cli.NewMockUi()
|
if strings.ContainsRune(New(nil).Help(), '\t') {
|
||||||
return ui, &JoinCommand{
|
t.Fatal("usage has tabs")
|
||||||
BaseCommand: BaseCommand{
|
|
||||||
UI: ui,
|
|
||||||
Flags: FlagSetClientHTTP,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestJoinCommand_implements(t *testing.T) {
|
func TestJoinCommandJoinRun_lan(t *testing.T) {
|
||||||
t.Parallel()
|
|
||||||
var _ cli.Command = &JoinCommand{}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestJoinCommandRun(t *testing.T) {
|
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
a1 := agent.NewTestAgent(t.Name(), ``)
|
a1 := agent.NewTestAgent(t.Name(), ``)
|
||||||
a2 := agent.NewTestAgent(t.Name(), ``)
|
a2 := agent.NewTestAgent(t.Name(), ``)
|
||||||
defer a1.Shutdown()
|
defer a1.Shutdown()
|
||||||
defer a2.Shutdown()
|
defer a2.Shutdown()
|
||||||
|
|
||||||
ui, c := testJoinCommand(t)
|
ui := cli.NewMockUi()
|
||||||
|
cmd := New(ui)
|
||||||
args := []string{
|
args := []string{
|
||||||
"-http-addr=" + a1.HTTPAddr(),
|
"-http-addr=" + a1.HTTPAddr(),
|
||||||
a2.Config.SerfBindAddrLAN.String(),
|
a2.Config.SerfBindAddrLAN.String(),
|
||||||
}
|
}
|
||||||
|
|
||||||
code := c.Run(args)
|
code := cmd.Run(args)
|
||||||
if code != 0 {
|
if code != 0 {
|
||||||
t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String())
|
t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String())
|
||||||
}
|
}
|
||||||
@ -53,14 +45,15 @@ func TestJoinCommandRun_wan(t *testing.T) {
|
|||||||
defer a1.Shutdown()
|
defer a1.Shutdown()
|
||||||
defer a2.Shutdown()
|
defer a2.Shutdown()
|
||||||
|
|
||||||
ui, c := testJoinCommand(t)
|
ui := cli.NewMockUi()
|
||||||
|
cmd := New(ui)
|
||||||
args := []string{
|
args := []string{
|
||||||
"-http-addr=" + a1.HTTPAddr(),
|
"-http-addr=" + a1.HTTPAddr(),
|
||||||
"-wan",
|
"-wan",
|
||||||
a2.Config.SerfBindAddrWAN.String(),
|
a2.Config.SerfBindAddrWAN.String(),
|
||||||
}
|
}
|
||||||
|
|
||||||
code := c.Run(args)
|
code := cmd.Run(args)
|
||||||
if code != 0 {
|
if code != 0 {
|
||||||
t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String())
|
t.Fatalf("bad: %d. %#v", code, ui.ErrorWriter.String())
|
||||||
}
|
}
|
||||||
@ -72,10 +65,11 @@ func TestJoinCommandRun_wan(t *testing.T) {
|
|||||||
|
|
||||||
func TestJoinCommandRun_noAddrs(t *testing.T) {
|
func TestJoinCommandRun_noAddrs(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
ui, c := testJoinCommand(t)
|
ui := cli.NewMockUi()
|
||||||
|
cmd := New(ui)
|
||||||
args := []string{"-http-addr=foo"}
|
args := []string{"-http-addr=foo"}
|
||||||
|
|
||||||
code := c.Run(args)
|
code := cmd.Run(args)
|
||||||
if code != 1 {
|
if code != 1 {
|
||||||
t.Fatalf("bad: %d", code)
|
t.Fatalf("bad: %d", code)
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user