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