github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/command/operator_autopilot_set.go (about)

     1  package command
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	flaghelper "github.com/hashicorp/nomad/helper/flags"
     8  	"github.com/posener/complete"
     9  )
    10  
    11  type OperatorAutopilotSetCommand struct {
    12  	Meta
    13  }
    14  
    15  func (c *OperatorAutopilotSetCommand) AutocompleteFlags() complete.Flags {
    16  	return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient),
    17  		complete.Flags{
    18  			"-cleanup-dead-servers":      complete.PredictAnything,
    19  			"-max-trailing-logs":         complete.PredictAnything,
    20  			"-last-contact-threshold":    complete.PredictAnything,
    21  			"-server-stabilization-time": complete.PredictAnything,
    22  			"-enable-redundancy-zones":   complete.PredictNothing,
    23  			"-disable-upgrade-migration": complete.PredictNothing,
    24  			"-enable-custom-upgrades":    complete.PredictNothing,
    25  		})
    26  }
    27  
    28  func (c *OperatorAutopilotSetCommand) AutocompleteArgs() complete.Predictor {
    29  	return complete.PredictNothing
    30  }
    31  
    32  func (c *OperatorAutopilotSetCommand) Name() string { return "operator autopilot set-config" }
    33  
    34  func (c *OperatorAutopilotSetCommand) Run(args []string) int {
    35  	// Autopilot command line flags behave differently from other commands
    36  	// in Nomad. Here, flags assume no default value. The value of the flag
    37  	// is taken into consideration if the flag is set, whether or not it contains
    38  	// the zero value when being applied to inherited configuration.
    39  	//
    40  	// This behavior was inherited from Consul.
    41  	var cleanupDeadServers flaghelper.BoolValue
    42  	var maxTrailingLogs flaghelper.UintValue
    43  	var minQuorum flaghelper.UintValue
    44  	var lastContactThreshold flaghelper.DurationValue
    45  	var serverStabilizationTime flaghelper.DurationValue
    46  	var enableRedundancyZones flaghelper.BoolValue
    47  	var disableUpgradeMigration flaghelper.BoolValue
    48  	var enableCustomUpgrades flaghelper.BoolValue
    49  
    50  	flagSet := c.Meta.FlagSet("autopilot", FlagSetClient)
    51  	flagSet.Usage = func() { c.Ui.Output(c.Help()) }
    52  
    53  	flagSet.Var(&cleanupDeadServers, "cleanup-dead-servers", "")
    54  	flagSet.Var(&maxTrailingLogs, "max-trailing-logs", "")
    55  	flagSet.Var(&lastContactThreshold, "last-contact-threshold", "")
    56  	flagSet.Var(&serverStabilizationTime, "server-stabilization-time", "")
    57  	flagSet.Var(&enableRedundancyZones, "enable-redundancy-zones", "")
    58  	flagSet.Var(&disableUpgradeMigration, "disable-upgrade-migration", "")
    59  	flagSet.Var(&enableCustomUpgrades, "enable-custom-upgrades", "")
    60  	flagSet.Var(&minQuorum, "min-quorum", "")
    61  
    62  	if err := flagSet.Parse(args); err != nil {
    63  		c.Ui.Error(fmt.Sprintf("Failed to parse args: %v", err))
    64  		return 1
    65  	}
    66  
    67  	// Set up a client.
    68  	client, err := c.Meta.Client()
    69  	if err != nil {
    70  		c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
    71  		return 1
    72  	}
    73  
    74  	// Fetch the current configuration.
    75  	operator := client.Operator()
    76  	conf, _, err := operator.AutopilotGetConfiguration(nil)
    77  	if err != nil {
    78  		c.Ui.Error(fmt.Sprintf("Error querying for Autopilot configuration: %s", err))
    79  		return 1
    80  	}
    81  
    82  	// Update the config values based on the set flags.
    83  	cleanupDeadServers.Merge(&conf.CleanupDeadServers)
    84  	enableRedundancyZones.Merge(&conf.EnableRedundancyZones)
    85  	disableUpgradeMigration.Merge(&conf.DisableUpgradeMigration)
    86  	enableCustomUpgrades.Merge(&conf.EnableCustomUpgrades)
    87  
    88  	trailing := uint(conf.MaxTrailingLogs)
    89  	maxTrailingLogs.Merge(&trailing)
    90  	conf.MaxTrailingLogs = uint64(trailing)
    91  
    92  	minQuorum.Merge(&conf.MinQuorum)
    93  
    94  	lastContactThreshold.Merge(&conf.LastContactThreshold)
    95  
    96  	serverStabilizationTime.Merge(&conf.ServerStabilizationTime)
    97  
    98  	// Check-and-set the new configuration.
    99  	result, _, err := operator.AutopilotCASConfiguration(conf, nil)
   100  	if err != nil {
   101  		c.Ui.Error(fmt.Sprintf("Error setting Autopilot configuration: %s", err))
   102  		return 1
   103  	}
   104  	if result {
   105  		c.Ui.Output("Configuration updated!")
   106  		return 0
   107  	}
   108  	c.Ui.Output("Configuration could not be atomically updated, please try again")
   109  	return 1
   110  }
   111  
   112  func (c *OperatorAutopilotSetCommand) Synopsis() string {
   113  	return "Modify the current Autopilot configuration"
   114  }
   115  
   116  func (c *OperatorAutopilotSetCommand) Help() string {
   117  	helpText := `
   118  Usage: nomad operator autopilot set-config [options]
   119  
   120    Modifies the current Autopilot configuration.
   121  
   122    If ACLs are enabled, this command requires a token with the 'operator:write'
   123    capability.
   124  
   125  General Options:
   126  
   127    ` + generalOptionsUsage(usageOptsDefault|usageOptsNoNamespace) + `
   128  
   129  Set Config Options:
   130  
   131    -cleanup-dead-servers=[true|false]
   132       Controls whether Nomad will automatically remove dead servers when
   133       new ones are successfully added. Must be one of [true|false].
   134  
   135    -disable-upgrade-migration=[true|false]
   136       (Enterprise-only) Controls whether Nomad will avoid promoting
   137       new servers until it can perform a migration. Must be one of
   138       "true|false".
   139  
   140    -last-contact-threshold=200ms
   141       Controls the maximum amount of time a server can go without contact
   142       from the leader before being considered unhealthy. Must be a
   143       duration value such as "200ms".
   144  
   145    -max-trailing-logs=<value>
   146       Controls the maximum number of log entries that a server can trail
   147       the leader by before being considered unhealthy.
   148  
   149    -min-quorum=<value>
   150        Controls the minimum number of servers required in a cluster
   151        before autopilot can prune dead servers.
   152  
   153    -redundancy-zone-tag=<value>
   154       (Enterprise-only) Controls the node_meta tag name used for
   155       separating servers into different redundancy zones.
   156  
   157    -server-stabilization-time=<10s>
   158       Controls the minimum amount of time a server must be stable in
   159       the 'healthy' state before being added to the cluster. Only takes
   160       effect if all servers are running Raft protocol version 3 or
   161       higher. Must be a duration value such as "10s".
   162  
   163    -upgrade-version-tag=<value>
   164       (Enterprise-only) The node_meta tag to use for version info when
   165       performing upgrade migrations. If left blank, the Nomad version
   166       will be used.
   167  `
   168  	return strings.TrimSpace(helpText)
   169  }