commands: move operator list remove-peer command to separate pkg

This commit is contained in:
Frank Schroeder 2017-10-17 08:28:35 +02:00 committed by Frank Schröder
parent 2a824f0575
commit c50e43a4ea
3 changed files with 55 additions and 56 deletions

View File

@ -30,6 +30,7 @@ import (
"github.com/hashicorp/consul/command/oper" "github.com/hashicorp/consul/command/oper"
"github.com/hashicorp/consul/command/operraft" "github.com/hashicorp/consul/command/operraft"
"github.com/hashicorp/consul/command/operraftlist" "github.com/hashicorp/consul/command/operraftlist"
"github.com/hashicorp/consul/command/operraftremove"
"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"
@ -184,12 +185,7 @@ func init() {
}, },
"operator raft remove-peer": func() (cli.Command, error) { "operator raft remove-peer": func() (cli.Command, error) {
return &OperatorRaftRemoveCommand{ return operraftremove.New(ui), nil
BaseCommand: BaseCommand{
Flags: FlagSetHTTP,
UI: ui,
},
}, nil
}, },
"reload": func() (cli.Command, error) { "reload": func() (cli.Command, error) {

View File

@ -1,52 +1,54 @@
package command package operraftremove
import ( import (
"flag" "flag"
"fmt" "fmt"
"github.com/hashicorp/consul/api" "github.com/hashicorp/consul/api"
"github.com/hashicorp/consul/command/flags"
"github.com/mitchellh/cli"
) )
type OperatorRaftRemoveCommand struct { func New(ui cli.Ui) *cmd {
BaseCommand c := &cmd{UI: ui}
c.init()
return c
}
type cmd struct {
UI cli.Ui
flags *flag.FlagSet
http *flags.HTTPFlags
usage string
// flags // flags
address string address string
id string id string
} }
func (c *OperatorRaftRemoveCommand) initFlags() { func (c *cmd) init() {
c.InitFlagSet() c.flags = flag.NewFlagSet("", flag.ContinueOnError)
c.FlagSet.StringVar(&c.address, "address", "", c.flags.StringVar(&c.address, "address", "",
"The address to remove from the Raft configuration.") "The address to remove from the Raft configuration.")
c.FlagSet.StringVar(&c.id, "id", "", c.flags.StringVar(&c.id, "id", "",
"The ID to remove from the Raft configuration.") "The ID to remove from the Raft configuration.")
c.http = &flags.HTTPFlags{}
flags.Merge(c.flags, c.http.ClientFlags())
flags.Merge(c.flags, c.http.ServerFlags())
c.usage = flags.Usage(usage, c.flags, c.http.ClientFlags(), c.http.ServerFlags())
} }
func (c *OperatorRaftRemoveCommand) Help() string { func (c *cmd) Synopsis() string {
c.initFlags()
return c.HelpCommand(`
Usage: consul operator raft remove-peer [options]
Remove the Consul server with given -address from the Raft configuration.
There are rare cases where a peer may be left behind in the Raft quorum even
though the server is no longer present and known to the cluster. This command
can be used to remove the failed server so that it is no longer affects the Raft
quorum. If the server still shows in the output of the "consul members" command,
it is preferable to clean up by simply running "consul force-leave" instead of
this command.
`)
}
func (c *OperatorRaftRemoveCommand) Synopsis() string {
return "Remove a Consul server from the Raft configuration" return "Remove a Consul server from the Raft configuration"
} }
func (c *OperatorRaftRemoveCommand) Run(args []string) int { func (c *cmd) Help() string {
c.initFlags() return c.usage
if err := c.FlagSet.Parse(args); err != nil { }
func (c *cmd) Run(args []string) int {
if err := c.flags.Parse(args); err != nil {
if err == flag.ErrHelp { if err == flag.ErrHelp {
return 0 return 0
} }
@ -55,7 +57,7 @@ func (c *OperatorRaftRemoveCommand) Run(args []string) int {
} }
// Set up a client. // Set up a client.
client, err := c.HTTPClient() client, err := c.http.APIClient()
if err != nil { if err != nil {
c.UI.Error(fmt.Sprintf("Error initializing client: %s", err)) c.UI.Error(fmt.Sprintf("Error initializing client: %s", err))
return 1 return 1
@ -96,3 +98,14 @@ func raftRemovePeers(address, id string, operator *api.Operator) error {
return nil return nil
} }
const usage = `Usage: consul operator raft remove-peer [options]
Remove the Consul server with given -address from the Raft configuration.
There are rare cases where a peer may be left behind in the Raft quorum even
though the server is no longer present and known to the cluster. This command
can be used to remove the failed server so that it is no longer affects the Raft
quorum. If the server still shows in the output of the "consul members" command,
it is preferable to clean up by simply running "consul force-leave" instead of
this command.`

View File

@ -1,4 +1,4 @@
package command package operraftremove
import ( import (
"strings" "strings"
@ -8,9 +8,11 @@ import (
"github.com/mitchellh/cli" "github.com/mitchellh/cli"
) )
func TestOperator_Raft_RemovePeer_Implements(t *testing.T) { func TestOperatorRaftRemovePeerCommand_noTabs(t *testing.T) {
t.Parallel() t.Parallel()
var _ cli.Command = &OperatorRaftRemoveCommand{} if strings.ContainsRune(New(cli.NewMockUi()).Help(), '\t') {
t.Fatal("usage has tabs")
}
} }
func TestOperator_Raft_RemovePeer(t *testing.T) { func TestOperator_Raft_RemovePeer(t *testing.T) {
@ -18,15 +20,9 @@ func TestOperator_Raft_RemovePeer(t *testing.T) {
a := agent.NewTestAgent(t.Name(), ``) a := agent.NewTestAgent(t.Name(), ``)
defer a.Shutdown() defer a.Shutdown()
// Test the remove-peer subcommand directly t.Run("Test the remove-peer subcommand directly", func(t *testing.T) {
{
ui := cli.NewMockUi() ui := cli.NewMockUi()
c := OperatorRaftRemoveCommand{ c := New(ui)
BaseCommand: BaseCommand{
UI: ui,
Flags: FlagSetHTTP,
},
}
args := []string{"-http-addr=" + a.HTTPAddr(), "-address=nope"} args := []string{"-http-addr=" + a.HTTPAddr(), "-address=nope"}
code := c.Run(args) code := c.Run(args)
@ -39,17 +35,11 @@ func TestOperator_Raft_RemovePeer(t *testing.T) {
if !strings.Contains(output, "address \"nope\" was not found in the Raft configuration") { if !strings.Contains(output, "address \"nope\" was not found in the Raft configuration") {
t.Fatalf("bad: %s", output) t.Fatalf("bad: %s", output)
} }
} })
// Test the remove-peer subcommand with -id t.Run("Test the remove-peer subcommand with -id", func(t *testing.T) {
{
ui := cli.NewMockUi() ui := cli.NewMockUi()
c := OperatorRaftRemoveCommand{ c := New(ui)
BaseCommand: BaseCommand{
UI: ui,
Flags: FlagSetHTTP,
},
}
args := []string{"-http-addr=" + a.HTTPAddr(), "-id=nope"} args := []string{"-http-addr=" + a.HTTPAddr(), "-id=nope"}
code := c.Run(args) code := c.Run(args)
@ -62,5 +52,5 @@ func TestOperator_Raft_RemovePeer(t *testing.T) {
if !strings.Contains(output, "id \"nope\" was not found in the Raft configuration") { if !strings.Contains(output, "id \"nope\" was not found in the Raft configuration") {
t.Fatalf("bad: %s", output) t.Fatalf("bad: %s", output)
} }
} })
} }