github.com/thomasobenaus/nomad@v0.11.1/command/operator_autopilot_set.go (about) 1 package command 2 3 import ( 4 "fmt" 5 "strings" 6 7 "github.com/hashicorp/consul/command/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 var cleanupDeadServers flags.BoolValue 36 var maxTrailingLogs flags.UintValue 37 var minQuorum flags.UintValue 38 var lastContactThreshold flags.DurationValue 39 var serverStabilizationTime flags.DurationValue 40 var enableRedundancyZones flags.BoolValue 41 var disableUpgradeMigration flags.BoolValue 42 var enableCustomUpgrades flags.BoolValue 43 44 f := c.Meta.FlagSet("autopilot", FlagSetClient) 45 f.Usage = func() { c.Ui.Output(c.Help()) } 46 47 f.Var(&cleanupDeadServers, "cleanup-dead-servers", "") 48 f.Var(&maxTrailingLogs, "max-trailing-logs", "") 49 f.Var(&lastContactThreshold, "last-contact-threshold", "") 50 f.Var(&serverStabilizationTime, "server-stabilization-time", "") 51 f.Var(&enableRedundancyZones, "enable-redundancy-zones", "") 52 f.Var(&disableUpgradeMigration, "disable-upgrade-migration", "") 53 f.Var(&enableCustomUpgrades, "enable-custom-upgrades", "") 54 f.Var(&minQuorum, "min-quorum", "") 55 56 if err := f.Parse(args); err != nil { 57 c.Ui.Error(fmt.Sprintf("Failed to parse args: %v", err)) 58 return 1 59 } 60 61 // Set up a client. 62 client, err := c.Meta.Client() 63 if err != nil { 64 c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err)) 65 return 1 66 } 67 68 // Fetch the current configuration. 69 operator := client.Operator() 70 conf, _, err := operator.AutopilotGetConfiguration(nil) 71 if err != nil { 72 c.Ui.Error(fmt.Sprintf("Error querying for Autopilot configuration: %s", err)) 73 return 1 74 } 75 76 // Update the config values based on the set flags. 77 cleanupDeadServers.Merge(&conf.CleanupDeadServers) 78 enableRedundancyZones.Merge(&conf.EnableRedundancyZones) 79 disableUpgradeMigration.Merge(&conf.DisableUpgradeMigration) 80 enableCustomUpgrades.Merge(&conf.EnableCustomUpgrades) 81 82 trailing := uint(conf.MaxTrailingLogs) 83 maxTrailingLogs.Merge(&trailing) 84 conf.MaxTrailingLogs = uint64(trailing) 85 minQuorum.Merge(&conf.MinQuorum) 86 lastContactThreshold.Merge(&conf.LastContactThreshold) 87 serverStabilizationTime.Merge(&conf.ServerStabilizationTime) 88 89 // Check-and-set the new configuration. 90 result, _, err := operator.AutopilotCASConfiguration(conf, nil) 91 if err != nil { 92 c.Ui.Error(fmt.Sprintf("Error setting Autopilot configuration: %s", err)) 93 return 1 94 } 95 if result { 96 c.Ui.Output("Configuration updated!") 97 return 0 98 } 99 c.Ui.Output("Configuration could not be atomically updated, please try again") 100 return 1 101 } 102 103 func (c *OperatorAutopilotSetCommand) Synopsis() string { 104 return "Modify the current Autopilot configuration" 105 } 106 107 func (c *OperatorAutopilotSetCommand) Help() string { 108 helpText := ` 109 Usage: nomad operator autopilot set-config [options] 110 111 Modifies the current Autopilot configuration. 112 113 General Options: 114 115 ` + generalOptionsUsage() + ` 116 117 Set Config Options: 118 119 -cleanup-dead-servers=[true|false] 120 Controls whether Nomad will automatically remove dead servers when 121 new ones are successfully added. Must be one of [true|false]. 122 123 -disable-upgrade-migration=[true|false] 124 (Enterprise-only) Controls whether Nomad will avoid promoting 125 new servers until it can perform a migration. Must be one of 126 "true|false". 127 128 -last-contact-threshold=200ms 129 Controls the maximum amount of time a server can go without contact 130 from the leader before being considered unhealthy. Must be a 131 duration value such as "200ms". 132 133 -max-trailing-logs=<value> 134 Controls the maximum number of log entries that a server can trail 135 the leader by before being considered unhealthy. 136 137 -min-quorum=<value> 138 Controls the minimum number of servers required in a cluster 139 before autopilot can prune dead servers. 140 141 -redundancy-zone-tag=<value> 142 (Enterprise-only) Controls the node_meta tag name used for 143 separating servers into different redundancy zones. 144 145 -server-stabilization-time=<10s> 146 Controls the minimum amount of time a server must be stable in 147 the 'healthy' state before being added to the cluster. Only takes 148 effect if all servers are running Raft protocol version 3 or 149 higher. Must be a duration value such as "10s". 150 151 -upgrade-version-tag=<value> 152 (Enterprise-only) The node_meta tag to use for version info when 153 performing upgrade migrations. If left blank, the Nomad version 154 will be used. 155 ` 156 return strings.TrimSpace(helpText) 157 }