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