mirror of https://github.com/status-im/consul.git
Convert maint command to use base.Command
This commit is contained in:
parent
ccb193f77f
commit
6c4d4f9a32
|
@ -1,18 +1,16 @@
|
||||||
package command
|
package command
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/hashicorp/consul/api"
|
"github.com/hashicorp/consul/command/base"
|
||||||
"github.com/mitchellh/cli"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// MaintCommand is a Command implementation that enables or disables
|
// MaintCommand is a Command implementation that enables or disables
|
||||||
// node or service maintenance mode.
|
// node or service maintenance mode.
|
||||||
type MaintCommand struct {
|
type MaintCommand struct {
|
||||||
Ui cli.Ui
|
base.Command
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *MaintCommand) Help() string {
|
func (c *MaintCommand) Help() string {
|
||||||
|
@ -40,15 +38,8 @@ Usage: consul maint [options]
|
||||||
If no arguments are given, the agent's maintenance status will be shown.
|
If no arguments are given, the agent's maintenance status will be shown.
|
||||||
This will return blank if nothing is currently under maintenance.
|
This will return blank if nothing is currently under maintenance.
|
||||||
|
|
||||||
Options:
|
` + c.Command.Help()
|
||||||
|
|
||||||
-enable Enable maintenance mode.
|
|
||||||
-disable Disable maintenance mode.
|
|
||||||
-reason=<string> Text string describing the maintenance reason
|
|
||||||
-service=<serviceID> Control maintenance mode for a specific service ID
|
|
||||||
-token="" ACL token to use. Defaults to that of agent.
|
|
||||||
-http-addr=127.0.0.1:8500 HTTP address of the Consul agent.
|
|
||||||
`
|
|
||||||
return strings.TrimSpace(helpText)
|
return strings.TrimSpace(helpText)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -57,19 +48,15 @@ func (c *MaintCommand) Run(args []string) int {
|
||||||
var disable bool
|
var disable bool
|
||||||
var reason string
|
var reason string
|
||||||
var serviceID string
|
var serviceID string
|
||||||
var token string
|
|
||||||
|
|
||||||
cmdFlags := flag.NewFlagSet("maint", flag.ContinueOnError)
|
f := c.Command.NewFlagSet(c)
|
||||||
cmdFlags.Usage = func() { c.Ui.Output(c.Help()) }
|
|
||||||
|
|
||||||
cmdFlags.BoolVar(&enable, "enable", false, "enable maintenance mode")
|
f.BoolVar(&enable, "enable", false, "Enable maintenance mode.")
|
||||||
cmdFlags.BoolVar(&disable, "disable", false, "disable maintenance mode")
|
f.BoolVar(&disable, "disable", false, "Disable maintenance mode.")
|
||||||
cmdFlags.StringVar(&reason, "reason", "", "maintenance reason")
|
f.StringVar(&reason, "reason", "", "Text describing the maintenance reason.")
|
||||||
cmdFlags.StringVar(&serviceID, "service", "", "service maintenance")
|
f.StringVar(&serviceID, "service", "", "Control maintenance mode for a specific service ID.")
|
||||||
cmdFlags.StringVar(&token, "token", "", "")
|
|
||||||
httpAddr := HTTPAddrFlag(cmdFlags)
|
|
||||||
|
|
||||||
if err := cmdFlags.Parse(args); err != nil {
|
if err := c.Command.Parse(args); err != nil {
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -88,10 +75,7 @@ func (c *MaintCommand) Run(args []string) int {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create and test the HTTP client
|
// Create and test the HTTP client
|
||||||
conf := api.DefaultConfig()
|
client, err := c.Command.HTTPClient()
|
||||||
conf.Address = *httpAddr
|
|
||||||
conf.Token = token
|
|
||||||
client, err := api.NewClient(conf)
|
|
||||||
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
|
||||||
|
|
|
@ -4,17 +4,27 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/hashicorp/consul/command/base"
|
||||||
"github.com/hashicorp/consul/consul/structs"
|
"github.com/hashicorp/consul/consul/structs"
|
||||||
"github.com/mitchellh/cli"
|
"github.com/mitchellh/cli"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func testMaintCommand(t *testing.T) (*cli.MockUi, *MaintCommand) {
|
||||||
|
ui := new(cli.MockUi)
|
||||||
|
return ui, &MaintCommand{
|
||||||
|
Command: base.Command{
|
||||||
|
Ui: ui,
|
||||||
|
Flags: base.FlagSetClientHTTP,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestMaintCommand_implements(t *testing.T) {
|
func TestMaintCommand_implements(t *testing.T) {
|
||||||
var _ cli.Command = &MaintCommand{}
|
var _ cli.Command = &MaintCommand{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMaintCommandRun_ConflictingArgs(t *testing.T) {
|
func TestMaintCommandRun_ConflictingArgs(t *testing.T) {
|
||||||
ui := new(cli.MockUi)
|
_, c := testMaintCommand(t)
|
||||||
c := &MaintCommand{Ui: ui}
|
|
||||||
|
|
||||||
if code := c.Run([]string{"-enable", "-disable"}); code != 1 {
|
if code := c.Run([]string{"-enable", "-disable"}); code != 1 {
|
||||||
t.Fatalf("expected return code 1, got %d", code)
|
t.Fatalf("expected return code 1, got %d", code)
|
||||||
|
@ -53,8 +63,7 @@ func TestMaintCommandRun_NoArgs(t *testing.T) {
|
||||||
a1.agent.EnableNodeMaintenance("broken 2", "")
|
a1.agent.EnableNodeMaintenance("broken 2", "")
|
||||||
|
|
||||||
// Run consul maint with no args (list mode)
|
// Run consul maint with no args (list mode)
|
||||||
ui := new(cli.MockUi)
|
ui, c := testMaintCommand(t)
|
||||||
c := &MaintCommand{Ui: ui}
|
|
||||||
|
|
||||||
args := []string{"-http-addr=" + a1.httpAddr}
|
args := []string{"-http-addr=" + a1.httpAddr}
|
||||||
code := c.Run(args)
|
code := c.Run(args)
|
||||||
|
@ -84,8 +93,7 @@ func TestMaintCommandRun_EnableNodeMaintenance(t *testing.T) {
|
||||||
a1 := testAgent(t)
|
a1 := testAgent(t)
|
||||||
defer a1.Shutdown()
|
defer a1.Shutdown()
|
||||||
|
|
||||||
ui := new(cli.MockUi)
|
ui, c := testMaintCommand(t)
|
||||||
c := &MaintCommand{Ui: ui}
|
|
||||||
|
|
||||||
args := []string{
|
args := []string{
|
||||||
"-http-addr=" + a1.httpAddr,
|
"-http-addr=" + a1.httpAddr,
|
||||||
|
@ -106,8 +114,7 @@ func TestMaintCommandRun_DisableNodeMaintenance(t *testing.T) {
|
||||||
a1 := testAgent(t)
|
a1 := testAgent(t)
|
||||||
defer a1.Shutdown()
|
defer a1.Shutdown()
|
||||||
|
|
||||||
ui := new(cli.MockUi)
|
ui, c := testMaintCommand(t)
|
||||||
c := &MaintCommand{Ui: ui}
|
|
||||||
|
|
||||||
args := []string{
|
args := []string{
|
||||||
"-http-addr=" + a1.httpAddr,
|
"-http-addr=" + a1.httpAddr,
|
||||||
|
@ -136,8 +143,7 @@ func TestMaintCommandRun_EnableServiceMaintenance(t *testing.T) {
|
||||||
t.Fatalf("err: %v", err)
|
t.Fatalf("err: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
ui := new(cli.MockUi)
|
ui, c := testMaintCommand(t)
|
||||||
c := &MaintCommand{Ui: ui}
|
|
||||||
|
|
||||||
args := []string{
|
args := []string{
|
||||||
"-http-addr=" + a1.httpAddr,
|
"-http-addr=" + a1.httpAddr,
|
||||||
|
@ -168,8 +174,7 @@ func TestMaintCommandRun_DisableServiceMaintenance(t *testing.T) {
|
||||||
t.Fatalf("err: %v", err)
|
t.Fatalf("err: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
ui := new(cli.MockUi)
|
ui, c := testMaintCommand(t)
|
||||||
c := &MaintCommand{Ui: ui}
|
|
||||||
|
|
||||||
args := []string{
|
args := []string{
|
||||||
"-http-addr=" + a1.httpAddr,
|
"-http-addr=" + a1.httpAddr,
|
||||||
|
@ -190,8 +195,7 @@ func TestMaintCommandRun_ServiceMaintenance_NoService(t *testing.T) {
|
||||||
a1 := testAgent(t)
|
a1 := testAgent(t)
|
||||||
defer a1.Shutdown()
|
defer a1.Shutdown()
|
||||||
|
|
||||||
ui := new(cli.MockUi)
|
ui, c := testMaintCommand(t)
|
||||||
c := &MaintCommand{Ui: ui}
|
|
||||||
|
|
||||||
args := []string{
|
args := []string{
|
||||||
"-http-addr=" + a1.httpAddr,
|
"-http-addr=" + a1.httpAddr,
|
||||||
|
|
|
@ -136,7 +136,10 @@ func init() {
|
||||||
|
|
||||||
"maint": func() (cli.Command, error) {
|
"maint": func() (cli.Command, error) {
|
||||||
return &command.MaintCommand{
|
return &command.MaintCommand{
|
||||||
Ui: ui,
|
Command: base.Command{
|
||||||
|
Flags: base.FlagSetClientHTTP,
|
||||||
|
Ui: ui,
|
||||||
|
},
|
||||||
}, nil
|
}, nil
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
|
@ -24,9 +24,11 @@ health check.
|
||||||
|
|
||||||
Usage: `consul maint [options]`
|
Usage: `consul maint [options]`
|
||||||
|
|
||||||
All of the command line arguments are optional.
|
#### API Options
|
||||||
|
|
||||||
The list of available flags are:
|
<%= partial "docs/commands/http_api_options_client" %>
|
||||||
|
|
||||||
|
#### Command Options
|
||||||
|
|
||||||
* `-enable` - Enable maintenance mode on a given service or node. If
|
* `-enable` - Enable maintenance mode on a given service or node. If
|
||||||
combined with the `-service` flag, we operate on a specific service ID.
|
combined with the `-service` flag, we operate on a specific service ID.
|
||||||
|
@ -44,12 +46,6 @@ The list of available flags are:
|
||||||
providing this flag, the `-enable` and `-disable` flags functionality is
|
providing this flag, the `-enable` and `-disable` flags functionality is
|
||||||
modified to operate on the given service ID.
|
modified to operate on the given service ID.
|
||||||
|
|
||||||
* `-token` - ACL token to use. Defaults to that of agent.
|
|
||||||
|
|
||||||
* `-http-addr` - Address to the HTTP server of the agent you want to contact
|
|
||||||
to send this command. If this isn't specified, the command will contact
|
|
||||||
"127.0.0.1:8500" which is the default HTTP address of a Consul agent.
|
|
||||||
|
|
||||||
## List mode
|
## List mode
|
||||||
|
|
||||||
If neither `-enable` nor `-disable` are passed, the `maint` command will
|
If neither `-enable` nor `-disable` are passed, the `maint` command will
|
Loading…
Reference in New Issue