diff --git a/cmd/create.go b/cmd/create.go index 54033bd..d78d724 100644 --- a/cmd/create.go +++ b/cmd/create.go @@ -189,7 +189,7 @@ func createJob(cmd *cobra.Command, args []string) { log.Fatal("unable to create Aurora job: ", err) } - if ok, monitorErr := client.InstancesMonitor(auroraJob.JobKey(), + if ok, monitorErr := client.MonitorInstances(auroraJob.JobKey(), auroraJob.GetInstanceCount(), 5, 50); !ok || monitorErr != nil { diff --git a/cmd/kill.go b/cmd/kill.go index 5fb4086..fc79c0d 100644 --- a/cmd/kill.go +++ b/cmd/kill.go @@ -58,7 +58,7 @@ func killJob(cmd *cobra.Command, args []string) { log.Fatalln(err) } - if ok, err := client.InstancesMonitor(job.JobKey(), 0, 5, 50); !ok || err != nil { + if ok, err := client.MonitorInstances(job.JobKey(), 0, 5, 50); !ok || err != nil { log.Fatalln("Unable to kill all instances of job") } } diff --git a/cmd/monitor.go b/cmd/monitor.go index 55a1863..d1c0016 100644 --- a/cmd/monitor.go +++ b/cmd/monitor.go @@ -61,7 +61,7 @@ func monitorHost(cmd *cobra.Command, args []string) { } log.Infof("Monitoring for %v at %v intervals", monitorHostCmd.monitorTimeout, monitorHostCmd.monitorInterval) - hostResult, err := client.HostMaintenanceMonitor(args, maintenanceModes, monitorHostCmd.monitorInterval, monitorHostCmd.monitorTimeout) + hostResult, err := client.MonitorHostMaintenance(args, maintenanceModes, monitorHostCmd.monitorInterval, monitorHostCmd.monitorTimeout) maintenanceMonitorPrint(hostResult, maintenanceModes) diff --git a/cmd/pulse.go b/cmd/pulse.go index 7a3c2f9..38db2b7 100644 --- a/cmd/pulse.go +++ b/cmd/pulse.go @@ -36,7 +36,7 @@ var pulseJobUpdateCmd = &cobra.Command{ func pulseJobUpdate(cmd *cobra.Command, args []string) { _, err := client.PulseJobUpdate( - &aurora.JobUpdateKey{ + aurora.JobUpdateKey{ Job: &aurora.JobKey{Environment: *env, Role: *role, Name: *name}, ID: updateID, }) diff --git a/cmd/resume.go b/cmd/resume.go index 9c597c7..2b43497 100644 --- a/cmd/resume.go +++ b/cmd/resume.go @@ -37,7 +37,7 @@ var resumeJobUpdateCmd = &cobra.Command{ func resumeJobUpdate(cmd *cobra.Command, args []string) { err := client.ResumeJobUpdate( - &aurora.JobUpdateKey{ + aurora.JobUpdateKey{ Job: &aurora.JobKey{Environment: *env, Role: *role, Name: *name}, ID: updateID, }, diff --git a/cmd/root.go b/cmd/root.go index 25e19c9..84dbb02 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -36,6 +36,8 @@ var caCertsPath string var clientKey, clientCert string var configFile string var toJson bool +var fromJson bool +var fromJsonFile string var logLevel string var duration time.Duration var percent float64 @@ -43,10 +45,9 @@ var count int64 var filename string var message = new(string) var updateID string -var statusList = make([]string, 0, 0) var log = logrus.New() -const australisVer = "v0.0.10" +const australisVer = "v0.1.0" var forceDrainTimeout time.Duration diff --git a/cmd/start.go b/cmd/start.go index f32ddb7..20fb0a4 100644 --- a/cmd/start.go +++ b/cmd/start.go @@ -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, diff --git a/cmd/stop.go b/cmd/stop.go index 0ff6cee..c170e1e 100644 --- a/cmd/stop.go +++ b/cmd/stop.go @@ -66,13 +66,13 @@ func endMaintenance(cmd *cobra.Command, args []string) { log.Println(args) result, err := client.EndMaintenance(args...) if err != nil { - log.Fatalf("error: %+v\n", err) + log.Fatalf("error: %+v", err) } log.Debugln(result) // Monitor change to NONE mode - hostResult, err := client.HostMaintenanceMonitor( + hostResult, err := client.MonitorHostMaintenance( args, []aurora.MaintenanceMode{aurora.MaintenanceMode_NONE}, stopMaintenanceConfig.monitorInterval, diff --git a/go.mod b/go.mod index bcc57ae..8e10a03 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ require ( github.com/BurntSushi/toml v0.3.1 // indirect github.com/cpuguy83/go-md2man v1.0.10 // indirect github.com/inconshreveable/mousetrap v1.0.0 // indirect - github.com/paypal/gorealis/v2 v2.0.1 + github.com/paypal/gorealis/v2 v2.21.0 github.com/pkg/errors v0.0.0-20171216070316-e881fd58d78e github.com/sirupsen/logrus v1.2.0 github.com/spf13/cobra v0.0.0-20180115160933-0c34d16c3123