hub.fastgit.org/hashicorp/consul.git@v1.4.5/command/operator/autopilot/set/operator_autopilot_set.go (about)

     1  package set
     2  
     3  import (
     4  	"flag"
     5  	"fmt"
     6  	"time"
     7  
     8  	"github.com/hashicorp/consul/api"
     9  	"github.com/hashicorp/consul/command/flags"
    10  	"github.com/mitchellh/cli"
    11  )
    12  
    13  func New(ui cli.Ui) *cmd {
    14  	c := &cmd{UI: ui}
    15  	c.init()
    16  	return c
    17  }
    18  
    19  type cmd struct {
    20  	UI    cli.Ui
    21  	flags *flag.FlagSet
    22  	http  *flags.HTTPFlags
    23  	help  string
    24  
    25  	// flags
    26  	cleanupDeadServers      flags.BoolValue
    27  	maxTrailingLogs         flags.UintValue
    28  	lastContactThreshold    flags.DurationValue
    29  	serverStabilizationTime flags.DurationValue
    30  	redundancyZoneTag       flags.StringValue
    31  	disableUpgradeMigration flags.BoolValue
    32  	upgradeVersionTag       flags.StringValue
    33  }
    34  
    35  func (c *cmd) init() {
    36  	c.flags = flag.NewFlagSet("", flag.ContinueOnError)
    37  	c.flags.Var(&c.cleanupDeadServers, "cleanup-dead-servers",
    38  		"Controls whether Consul will automatically remove dead servers "+
    39  			"when new ones are successfully added. Must be one of `true|false`.")
    40  	c.flags.Var(&c.maxTrailingLogs, "max-trailing-logs",
    41  		"Controls the maximum number of log entries that a server can trail the "+
    42  			"leader by before being considered unhealthy.")
    43  	c.flags.Var(&c.lastContactThreshold, "last-contact-threshold",
    44  		"Controls the maximum amount of time a server can go without contact "+
    45  			"from the leader before being considered unhealthy. Must be a duration value "+
    46  			"such as `200ms`.")
    47  	c.flags.Var(&c.serverStabilizationTime, "server-stabilization-time",
    48  		"Controls the minimum amount of time a server must be stable in the "+
    49  			"'healthy' state before being added to the cluster. Only takes effect if all "+
    50  			"servers are running Raft protocol version 3 or higher. Must be a duration "+
    51  			"value such as `10s`.")
    52  	c.flags.Var(&c.redundancyZoneTag, "redundancy-zone-tag",
    53  		"(Enterprise-only) Controls the node_meta tag name used for separating servers into "+
    54  			"different redundancy zones.")
    55  	c.flags.Var(&c.disableUpgradeMigration, "disable-upgrade-migration",
    56  		"(Enterprise-only) Controls whether Consul will avoid promoting new servers until "+
    57  			"it can perform a migration. Must be one of `true|false`.")
    58  	c.flags.Var(&c.upgradeVersionTag, "upgrade-version-tag",
    59  		"(Enterprise-only) The node_meta tag to use for version info when performing upgrade "+
    60  			"migrations. If left blank, the Consul version will be used.")
    61  
    62  	c.http = &flags.HTTPFlags{}
    63  	flags.Merge(c.flags, c.http.ClientFlags())
    64  	flags.Merge(c.flags, c.http.ServerFlags())
    65  	c.help = flags.Usage(help, c.flags)
    66  }
    67  
    68  func (c *cmd) Run(args []string) int {
    69  	if err := c.flags.Parse(args); err != nil {
    70  		if err == flag.ErrHelp {
    71  			return 0
    72  		}
    73  		c.UI.Error(fmt.Sprintf("Failed to parse args: %v", err))
    74  		return 1
    75  	}
    76  
    77  	// Set up a client.
    78  	client, err := c.http.APIClient()
    79  	if err != nil {
    80  		c.UI.Error(fmt.Sprintf("Error initializing client: %s", err))
    81  		return 1
    82  	}
    83  
    84  	// Fetch the current configuration.
    85  	operator := client.Operator()
    86  	conf, err := operator.AutopilotGetConfiguration(nil)
    87  	if err != nil {
    88  		c.UI.Error(fmt.Sprintf("Error querying Autopilot configuration: %s", err))
    89  		return 1
    90  	}
    91  
    92  	// Update the config values based on the set flags.
    93  	c.cleanupDeadServers.Merge(&conf.CleanupDeadServers)
    94  	c.redundancyZoneTag.Merge(&conf.RedundancyZoneTag)
    95  	c.disableUpgradeMigration.Merge(&conf.DisableUpgradeMigration)
    96  	c.upgradeVersionTag.Merge(&conf.UpgradeVersionTag)
    97  
    98  	trailing := uint(conf.MaxTrailingLogs)
    99  	c.maxTrailingLogs.Merge(&trailing)
   100  	conf.MaxTrailingLogs = uint64(trailing)
   101  
   102  	last := time.Duration(*conf.LastContactThreshold)
   103  	c.lastContactThreshold.Merge(&last)
   104  	conf.LastContactThreshold = api.NewReadableDuration(last)
   105  
   106  	stablization := time.Duration(*conf.ServerStabilizationTime)
   107  	c.serverStabilizationTime.Merge(&stablization)
   108  	conf.ServerStabilizationTime = api.NewReadableDuration(stablization)
   109  
   110  	// Check-and-set the new configuration.
   111  	result, err := operator.AutopilotCASConfiguration(conf, nil)
   112  	if err != nil {
   113  		c.UI.Error(fmt.Sprintf("Error setting Autopilot configuration: %s", err))
   114  		return 1
   115  	}
   116  	if result {
   117  		c.UI.Output("Configuration updated!")
   118  		return 0
   119  	}
   120  	c.UI.Output("Configuration could not be atomically updated, please try again")
   121  	return 1
   122  }
   123  
   124  func (c *cmd) Synopsis() string {
   125  	return synopsis
   126  }
   127  
   128  func (c *cmd) Help() string {
   129  	return c.help
   130  }
   131  
   132  const synopsis = "Modify the current Autopilot configuration"
   133  const help = `
   134  Usage: consul operator autopilot set-config [options]
   135  
   136    Modifies the current Autopilot configuration.
   137  `