consul/command/operator_autopilot_set.go

79 lines
1.8 KiB
Go
Raw Normal View History

2017-02-24 23:54:49 +00:00
package command
import (
"flag"
"fmt"
"strings"
"github.com/hashicorp/consul/command/base"
)
type OperatorAutopilotSetCommand struct {
base.Command
}
func (c *OperatorAutopilotSetCommand) Help() string {
helpText := `
Usage: consul operator autopilot set-config [options]
Modifies the current Autopilot configuration.
` + c.Command.Help()
return strings.TrimSpace(helpText)
}
func (c *OperatorAutopilotSetCommand) Synopsis() string {
return "Modify the current Autopilot configuration"
}
func (c *OperatorAutopilotSetCommand) Run(args []string) int {
2017-02-25 00:00:39 +00:00
var deadServerCleanup base.BoolValue
2017-02-24 23:54:49 +00:00
f := c.Command.NewFlagSet(c)
2017-02-25 00:00:39 +00:00
f.Var(&deadServerCleanup, "dead-server-cleanup",
2017-02-24 23:54:49 +00:00
"Controls whether Consul will automatically remove dead servers "+
"when new ones are successfully added. Must be one of `true|false`.")
if err := c.Command.Parse(args); err != nil {
if err == flag.ErrHelp {
return 0
}
c.Ui.Error(fmt.Sprintf("Failed to parse args: %v", err))
return 1
}
// Set up a client.
client, err := c.Command.HTTPClient()
if err != nil {
c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
return 1
}
// Fetch the current configuration.
operator := client.Operator()
conf, err := operator.AutopilotGetConfiguration(nil)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error querying Autopilot configuration: %s", err))
return 1
}
2017-02-25 00:00:39 +00:00
// Update the config values.
deadServerCleanup.Merge(&conf.DeadServerCleanup)
2017-02-24 23:54:49 +00:00
2017-02-25 00:00:39 +00:00
// Check-and-set the new configuration.
2017-02-24 23:54:49 +00:00
result, err := operator.AutopilotCASConfiguration(conf, nil)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error setting Autopilot configuration: %s", err))
return 1
}
if result {
c.Ui.Output("Configuration updated!")
return 0
2017-02-24 23:54:49 +00:00
} else {
c.Ui.Output("Configuration could not be atomically updated")
return 1
2017-02-24 23:54:49 +00:00
}
}