Adding flags to take in JSON from the command line.

This commit is contained in:
Renan DelValle 2020-02-06 18:49:21 -08:00
parent 1497aca2bb
commit e0d400e85c
No known key found for this signature in database
GPG key ID: C240AD6D6F443EC9
9 changed files with 45 additions and 20 deletions

View file

@ -15,6 +15,7 @@
package cmd
import (
"errors"
"time"
"github.com/paypal/gorealis/v2/gen-go/apache/aurora"
@ -23,6 +24,8 @@ import (
const countFlag = "count"
const percentageFlag = "percentage"
const jsonFlag = "json"
const jsonFileFlag = "json-file"
func init() {
rootCmd.AddCommand(startCmd)
@ -34,6 +37,8 @@ func init() {
// Maintenance specific flags
startDrainCmd.cmd.Flags().DurationVar(&startDrainCmd.monitorInterval, "interval", time.Second*5, "Interval at which to poll scheduler.")
startDrainCmd.cmd.Flags().DurationVar(&startDrainCmd.monitorTimeout, "timeout", time.Minute*10, "Time after which the monitor will stop polling and throw an error.")
startDrainCmd.cmd.Flags().StringVar(&fromJsonFile, jsonFileFlag, "", "JSON file to read list of agents from.")
startDrainCmd.cmd.Flags().BoolVar(&fromJson, jsonFlag, false, "Read JSON list of agents from the STDIN.")
/* SLA Aware commands */
startCmd.AddCommand(startSLADrainCmd.cmd)
@ -46,6 +51,8 @@ func init() {
startSLADrainCmd.cmd.Flags().DurationVar(&forceDrainTimeout, "sla-limit", time.Minute*60, "Time limit after which SLA-Aware drain sheds SLA Awareness.")
startSLADrainCmd.cmd.Flags().DurationVar(&startSLADrainCmd.monitorInterval, "interval", time.Second*10, "Interval at which to poll scheduler.")
startSLADrainCmd.cmd.Flags().DurationVar(&startSLADrainCmd.monitorTimeout, "timeout", time.Minute*20, "Time after which the monitor will stop polling and throw an error.")
startSLADrainCmd.cmd.Flags().StringVar(&fromJsonFile, jsonFileFlag, "", "JSON file to read list of agents from.")
startSLADrainCmd.cmd.Flags().BoolVar(&fromJson, jsonFlag, false, "Read JSON list of agents from the STDIN.")
startCmd.AddCommand(startMaintenanceCmd.cmd)
startMaintenanceCmd.cmd.Run = maintenance
@ -53,6 +60,8 @@ func init() {
// SLA Maintenance specific flags
startMaintenanceCmd.cmd.Flags().DurationVar(&startMaintenanceCmd.monitorInterval, "interval", time.Second*5, "Interval at which to poll scheduler.")
startMaintenanceCmd.cmd.Flags().DurationVar(&startMaintenanceCmd.monitorTimeout, "timeout", time.Minute*10, "Time after which the monitor will stop polling and throw an error.")
startMaintenanceCmd.cmd.Flags().StringVar(&fromJsonFile, jsonFileFlag, "", "JSON file to read list of agents from.")
startMaintenanceCmd.cmd.Flags().BoolVar(&fromJson, jsonFlag, false, "Read JSON list of agents from the STDIN.")
}
var startCmd = &cobra.Command{
@ -62,19 +71,19 @@ var startCmd = &cobra.Command{
var startDrainCmd = monitorCmdConfig{
cmd: &cobra.Command{
Use: "drain [space separated host list]",
Use: "drain [space separated host list or use JSON flags]",
Short: "Place a list of space separated Mesos Agents into draining mode.",
Long: `Adds a Mesos Agent to Aurora's Drain list. Agents in this list
are not allowed to schedule new tasks and any tasks already running on this Agent
are killed and rescheduled in an Agent that is not in maintenance mode. Command
expects a space separated list of hosts to place into maintenance mode.`,
Args: cobra.MinimumNArgs(1),
Args: argsValidateJSONFlags,
},
}
var startSLADrainCmd = monitorCmdConfig{
cmd: &cobra.Command{
Use: "sla-drain [space separated host list]",
Use: "sla-drain [space separated host list or use JSON flags]",
Short: "Place a list of space separated Mesos Agents into maintenance mode using SLA aware strategies.",
Long: `Adds a Mesos Agent to Aurora's Drain list. Agents in this list
are not allowed to schedule new tasks and any tasks already running on this Agent
@ -84,21 +93,36 @@ If the --count argument is passed, tasks will be drained using the count SLA pol
when a Job does not have a defined SLA policy.
If the --percentage argument is passed, tasks will be drained using the percentage SLA policy as a fallback
when a Job does not have a defined SLA policy.`,
Args: cobra.MinimumNArgs(1),
Args: argsValidateJSONFlags,
},
}
var startMaintenanceCmd = monitorCmdConfig{
cmd: &cobra.Command{
Use: "maintenance [space separated host list]",
Use: "maintenance [space separated host list or use JSON flags]",
Short: "Place a list of space separated Mesos Agents into maintenance mode.",
Long: `Places Mesos Agent into Maintenance mode. Agents in this list
are de-prioritized for scheduling a task. Command
expects a space separated list of hosts to place into maintenance mode.`,
Args: cobra.MinimumNArgs(1),
Args: argsValidateJSONFlags,
},
}
func argsValidateJSONFlags(cmd *cobra.Command, args []string) error {
if cmd.Flags().Changed(jsonFlag) && cmd.Flags().Changed(jsonFileFlag) {
return errors.New("only json file or json stdin must be set")
}
// These two flags are mutually exclusive
if cmd.Flags().Changed(jsonFlag) != cmd.Flags().Changed(jsonFileFlag) {
return nil
}
if len(args) < 1 {
return errors.New("at least one host must be specified")
}
return nil
}
func drain(cmd *cobra.Command, args []string) {
log.Infoln("Setting hosts to DRAINING")
log.Infoln(args)
@ -111,7 +135,7 @@ func drain(cmd *cobra.Command, args []string) {
log.Infof("Monitoring for %v at %v intervals", monitorHostCmd.monitorTimeout, monitorHostCmd.monitorInterval)
// Monitor change to DRAINING and DRAINED mode
hostResult, err := client.HostMaintenanceMonitor(
hostResult, err := client.MonitorHostMaintenance(
args,
[]aurora.MaintenanceMode{aurora.MaintenanceMode_DRAINED},
startDrainCmd.monitorInterval,
@ -135,7 +159,7 @@ func slaDrainHosts(policy *aurora.SlaPolicy, interval, timeout time.Duration, ho
log.Infof("Monitoring for %v at %v intervals", timeout, interval)
// Monitor change to DRAINING and DRAINED mode
hostResult, err := client.HostMaintenanceMonitor(
hostResult, err := client.MonitorHostMaintenance(
hosts,
[]aurora.MaintenanceMode{aurora.MaintenanceMode_DRAINED},
interval,
@ -178,7 +202,7 @@ func maintenance(cmd *cobra.Command, args []string) {
log.Infoln(args)
result, err := client.StartMaintenance(args...)
if err != nil {
log.Fatalf("error: %+v\n", err)
log.Fatalf("error: %+v", err)
}
log.Debugln(result)
@ -186,7 +210,7 @@ func maintenance(cmd *cobra.Command, args []string) {
log.Infof("Monitoring for %v at %v intervals", monitorHostCmd.monitorTimeout, monitorHostCmd.monitorInterval)
// Monitor change to DRAINING and DRAINED mode
hostResult, err := client.HostMaintenanceMonitor(
hostResult, err := client.MonitorHostMaintenance(
args,
[]aurora.MaintenanceMode{aurora.MaintenanceMode_SCHEDULED},
startMaintenanceCmd.monitorInterval,