diff --git a/cmd/fetch.go b/cmd/fetch.go index 843525f..709c3cf 100644 --- a/cmd/fetch.go +++ b/cmd/fetch.go @@ -3,7 +3,7 @@ package cmd import ( "fmt" - "github.com/paypal/gorealis/v2" + realis "github.com/paypal/gorealis/v2" "github.com/paypal/gorealis/v2/gen-go/apache/aurora" "github.com/spf13/cobra" "github.com/spf13/pflag" @@ -29,7 +29,6 @@ func init() { taskStatusCmd.Flags().StringVarP(role, "role", "r", "", "Aurora Role") taskStatusCmd.Flags().StringVarP(name, "name", "n", "", "Aurora Name") - /* Fetch Leader */ leaderCmd.Flags().String("zkPath", "/aurora/scheduler", "Zookeeper node path where leader election happens") @@ -37,9 +36,9 @@ func init() { // Hijack help function to hide unnecessary global flags help := leaderCmd.HelpFunc() - leaderCmd.SetHelpFunc(func(cmd *cobra.Command, s []string){ - if cmd.HasInheritedFlags(){ - cmd.InheritedFlags().VisitAll(func(f *pflag.Flag){ + leaderCmd.SetHelpFunc(func(cmd *cobra.Command, s []string) { + if cmd.HasInheritedFlags() { + cmd.InheritedFlags().VisitAll(func(f *pflag.Flag) { if f.Name != "logLevel" { f.Hidden = true } @@ -66,7 +65,6 @@ var fetchTaskCmd = &cobra.Command{ Short: "Task information from Aurora", } - var taskConfigCmd = &cobra.Command{ Use: "config", Short: "Fetch a list of task configurations from Aurora.", @@ -86,7 +84,7 @@ var leaderCmd = &cobra.Command{ PersistentPreRun: func(cmd *cobra.Command, args []string) {}, //We don't need a realis client for this cmd PersistentPostRun: func(cmd *cobra.Command, args []string) {}, //We don't need a realis client for this cmd PreRun: setConfig, - Args: cobra.MinimumNArgs(1), + Args: cobra.MinimumNArgs(1), Short: "Fetch current Aurora leader given Zookeeper nodes. ", Long: `Gets the current leading aurora scheduler instance using information from Zookeeper path. Pass Zookeeper nodes separated by a space as an argument to this command.`, diff --git a/cmd/force.go b/cmd/force.go index 0b2ec7d..04fc463 100644 --- a/cmd/force.go +++ b/cmd/force.go @@ -12,7 +12,7 @@ func init() { // Sub-commands forceCmd.AddCommand(forceSnapshotCmd) - forceCmd.AddCommand(forceBackupCmd) + forceCmd.AddCommand(forceBackupCmd) // Recon sub-commands forceCmd.AddCommand(reconCmd) @@ -30,7 +30,7 @@ var forceSnapshotCmd = &cobra.Command{ Short: "Force the leading scheduler to perform a Snapshot.", Long: `Takes a Snapshot of the in memory state of the Apache Aurora cluster and writes it to the Mesos replicated log. This should NOT be confused with a backup.`, - Run: snapshot, + Run: snapshot, } func snapshot(cmd *cobra.Command, args []string) { @@ -48,7 +48,7 @@ var forceBackupCmd = &cobra.Command{ Short: "Force the leading scheduler to perform a Backup.", Long: `Force the Aurora Scheduler to write a backup of the latest snapshot to the filesystem of the leading scheduler.`, - Run: backup, + Run: backup, } func backup(cmd *cobra.Command, args []string) { @@ -95,16 +95,16 @@ func explicitRecon(cmd *cobra.Command, args []string) { case 1: log.Infof("Using %v as batch size for explicit recon.\n", args[0]) - // Get batch size from args and convert it to the right format + // Get batch size from args and convert it to the right format batchInt, err := strconv.Atoi(args[0]) - if err != nil { - log.Fatalf("error: %+v\n", err) - } + if err != nil { + log.Fatalf("error: %+v\n", err) + } batchInt32 := int32(batchInt) batchSize = &batchInt32 - default: - log.Fatalln("Provide 0 arguments to use default batch size or one argument to use a custom batch size.") + default: + log.Fatalln("Provide 0 arguments to use default batch size or one argument to use a custom batch size.") } err := client.ForceExplicitTaskReconciliation(batchSize) @@ -120,7 +120,7 @@ var forceImplicitReconCmd = &cobra.Command{ Short: "Force the leading scheduler to perform an implicit recon.", Long: `Forces leading scheduler to ask Mesos Master for a list of the latest state for all currently known non-terminal tasks being run by Aurora.`, - Run: implicitRecon, + Run: implicitRecon, } func implicitRecon(cmd *cobra.Command, args []string) { diff --git a/cmd/kill.go b/cmd/kill.go index d3f9aff..1394cdc 100644 --- a/cmd/kill.go +++ b/cmd/kill.go @@ -1,7 +1,7 @@ package cmd import ( - "github.com/paypal/gorealis/v2" + realis "github.com/paypal/gorealis/v2" "github.com/spf13/cobra" ) diff --git a/cmd/rollback.go b/cmd/rollback.go index bb6dd44..c46a819 100644 --- a/cmd/rollback.go +++ b/cmd/rollback.go @@ -1,52 +1,51 @@ package cmd import ( - "fmt" + "fmt" - "github.com/paypal/gorealis/v2/gen-go/apache/aurora" - "github.com/spf13/cobra" + "github.com/paypal/gorealis/v2/gen-go/apache/aurora" + "github.com/spf13/cobra" ) func init() { - rootCmd.AddCommand(rollbackCmd) + rootCmd.AddCommand(rollbackCmd) - rollbackCmd.AddCommand(rollbackUpdateCmd) - rollbackUpdateCmd.Flags().StringVarP(env, "environment", "e", "", "Aurora Environment") - rollbackUpdateCmd.Flags().StringVarP(role, "role", "r", "", "Aurora Role") - rollbackUpdateCmd.Flags().StringVarP(name, "name", "n", "", "Aurora Name") - rollbackUpdateCmd.Flags().StringVar(&updateID, "id", "", "Update ID") - rollbackUpdateCmd.Flags().StringVar(message, "message", "", "Message to store alongside resume event") - rollbackUpdateCmd.MarkFlagRequired("environment") - rollbackUpdateCmd.MarkFlagRequired("role") - rollbackUpdateCmd.MarkFlagRequired("name") - rollbackUpdateCmd.MarkFlagRequired("id") + rollbackCmd.AddCommand(rollbackUpdateCmd) + rollbackUpdateCmd.Flags().StringVarP(env, "environment", "e", "", "Aurora Environment") + rollbackUpdateCmd.Flags().StringVarP(role, "role", "r", "", "Aurora Role") + rollbackUpdateCmd.Flags().StringVarP(name, "name", "n", "", "Aurora Name") + rollbackUpdateCmd.Flags().StringVar(&updateID, "id", "", "Update ID") + rollbackUpdateCmd.Flags().StringVar(message, "message", "", "Message to store alongside resume event") + rollbackUpdateCmd.MarkFlagRequired("environment") + rollbackUpdateCmd.MarkFlagRequired("role") + rollbackUpdateCmd.MarkFlagRequired("name") + rollbackUpdateCmd.MarkFlagRequired("id") } var rollbackCmd = &cobra.Command{ - Use: "rollback", - Short: "rollback an operation such as an Update", + Use: "rollback", + Short: "rollback an operation such as an Update", } var rollbackUpdateCmd = &cobra.Command{ - Use: "update", - Short: "rollback an update that is currently paused", - Run: rollbackUpdate, + Use: "update", + Short: "rollback an update that is currently paused", + Run: rollbackUpdate, } func rollbackUpdate(cmd *cobra.Command, args []string) { - var updateMessage string - if message != nil { - updateMessage = *message - } - err := client.RollbackJobUpdate(aurora.JobUpdateKey{ - Job: &aurora.JobKey{Environment: *env, Role: *role, Name: *name}, - ID: updateID, - }, updateMessage) + var updateMessage string + if message != nil { + updateMessage = *message + } + err := client.RollbackJobUpdate(aurora.JobUpdateKey{ + Job: &aurora.JobKey{Environment: *env, Role: *role, Name: *name}, + ID: updateID, + }, updateMessage) - if err != nil { - log.Fatal(err) - } + if err != nil { + log.Fatal(err) + } - fmt.Printf("Rollback update for update ID %v sent successfully\n", updateID) + fmt.Printf("Rollback update for update ID %v sent successfully\n", updateID) } - diff --git a/cmd/root.go b/cmd/root.go index 10fcb48..fc371a2 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -1,11 +1,12 @@ package cmd import ( - "github.com/spf13/viper" "strings" "time" - "github.com/paypal/gorealis/v2" + "github.com/spf13/viper" + + realis "github.com/paypal/gorealis/v2" "github.com/spf13/cobra" "github.com/sirupsen/logrus" @@ -46,9 +47,9 @@ func init() { rootCmd.PersistentFlags().StringVarP(&clientCert, "clientCert", "c", "", "Client certificate to use to connect to Aurora.") rootCmd.PersistentFlags().StringVarP(&caCertsPath, "caCertsPath", "a", "", "Path where CA certificates can be found.") rootCmd.PersistentFlags().BoolVarP(&skipCertVerification, "skipCertVerification", "i", false, "Skip CA certificate hostname verification.") - rootCmd.PersistentFlags().StringVar(&configFile, "config", "/etc/aurora/australis.yml", "Config file to use.") - rootCmd.PersistentFlags().BoolVar(&toJson, "toJSON", false, "Print output in JSON format.") - rootCmd.PersistentFlags().StringVarP(&logLevel, "logLevel", "l", "info", "Set logging level [" + getLoggingLevels() + "].") + rootCmd.PersistentFlags().StringVar(&configFile, "config", "/etc/aurora/australis.yml", "Config file to use.") + rootCmd.PersistentFlags().BoolVar(&toJson, "toJSON", false, "Print output in JSON format.") + rootCmd.PersistentFlags().StringVarP(&logLevel, "logLevel", "l", "info", "Set logging level ["+getLoggingLevels()+"].") } var rootCmd = &cobra.Command{ @@ -79,45 +80,44 @@ func setConfig(cmd *cobra.Command, args []string) { } func connect(cmd *cobra.Command, args []string) { - var err error + var err error + setConfig(cmd, args) - setConfig(cmd, args) + zkAddrSlice := strings.Split(zkAddr, ",") - zkAddrSlice := strings.Split(zkAddr, ",") + viper.SetConfigFile(configFile) + err = viper.ReadInConfig() + if err == nil { + // Best effort load configuration. Will only set config values when flags have not set them already. + if viper.IsSet("zk") && len(zkAddrSlice) == 1 && zkAddrSlice[0] == "" { + zkAddrSlice = viper.GetStringSlice("zk") + } - viper.SetConfigFile(configFile) - err = viper.ReadInConfig() - if err == nil { - // Best effort load configuration. Will only set config values when flags have not set them already. - if viper.IsSet("zk") && len(zkAddrSlice) == 1 && zkAddrSlice[0] == "" { - zkAddrSlice = viper.GetStringSlice("zk") - } + if viper.IsSet("username") && username == "" { + username = viper.GetString("username") + } - if viper.IsSet("username") && username == "" { - username = viper.GetString("username") - } + if viper.IsSet("password") && password == "" { + password = viper.GetString("password") + } - if viper.IsSet("password") && password == "" { - password = viper.GetString("password") - } + if viper.IsSet("clientKey") && clientKey == "" { + clientKey = viper.GetString("clientKey") + } - if viper.IsSet("clientKey") && clientKey == "" { - clientKey = viper.GetString("clientKey") - } + if viper.IsSet("clientCert") && clientCert == "" { + clientCert = viper.GetString("clientCert") + } - if viper.IsSet("clientCert") && clientCert == "" { - clientCert = viper.GetString("clientCert") - } + if viper.IsSet("caCertsPath") && caCertsPath == "" { + caCertsPath = viper.GetString("caCertsPath") + } - if viper.IsSet("caCertsPath") && caCertsPath == "" { - caCertsPath = viper.GetString("caCertsPath") - } - - if viper.IsSet("skipCertVerification") && !skipCertVerification { - skipCertVerification = viper.GetBool("skipCertVerification") - } - } + if viper.IsSet("skipCertVerification") && !skipCertVerification { + skipCertVerification = viper.GetBool("skipCertVerification") + } + } realisOptions := []realis.ClientOption{realis.BasicAuth(username, password), realis.ThriftJSON(), @@ -133,7 +133,7 @@ func connect(cmd *cobra.Command, args []string) { // Prefer zookeeper if both ways of connecting are provided if len(zkAddrSlice) > 0 && zkAddrSlice[0] != "" { // Configure Zookeeper to connect - zkOptions := []realis.ZKOpt{ realis.ZKEndpoints(zkAddrSlice...), realis.ZKPath("/aurora/scheduler")} + zkOptions := []realis.ZKOpt{realis.ZKEndpoints(zkAddrSlice...), realis.ZKPath("/aurora/scheduler")} realisOptions = append(realisOptions, realis.ZookeeperOptions(zkOptions...)) } else if schedAddr != "" { realisOptions = append(realisOptions, realis.SchedulerUrl(schedAddr)) diff --git a/cmd/set.go b/cmd/set.go index 801c30a..f09daef 100644 --- a/cmd/set.go +++ b/cmd/set.go @@ -2,10 +2,11 @@ package cmd import ( "fmt" - "github.com/pkg/errors" - "github.com/spf13/cobra" "strconv" "strings" + + "github.com/pkg/errors" + "github.com/spf13/cobra" ) func init() { @@ -32,7 +33,7 @@ var setQuotaCmd = &cobra.Command{ *role = args[0] - for i:=1; i < len(args); i++ { + for i := 1; i < len(args); i++ { resourcePair := strings.Split(args[i], ":") if len(resourcePair) != 2 { @@ -53,7 +54,7 @@ var setQuotaCmd = &cobra.Command{ return errors.Wrap(err, "unable to convert RAM value provided to a integer number") } - case "disk": + case "disk": disk, err = strconv.ParseInt(resourcePair[1], 10, 64) if err != nil { return errors.Wrap(err, "unable to convert DISK value provided to a integer number") diff --git a/cmd/start.go b/cmd/start.go index 81472a7..e7469fe 100644 --- a/cmd/start.go +++ b/cmd/start.go @@ -1,47 +1,46 @@ package cmd import ( + "time" + "github.com/paypal/gorealis/v2/gen-go/apache/aurora" "github.com/spf13/cobra" - "time" ) func init() { rootCmd.AddCommand(startCmd) - // Sub-commands startCmd.AddCommand(startDrainCmd) // Maintenance specific flags - startDrainCmd.Flags().DurationVar(&monitorInterval,"interval", time.Second * 5, "Interval at which to poll scheduler.") - startDrainCmd.Flags().DurationVar(&monitorTimeout,"timeout", time.Minute * 10, "Time after which the monitor will stop polling and throw an error.") + startDrainCmd.Flags().DurationVar(&monitorInterval, "interval", time.Second*5, "Interval at which to poll scheduler.") + startDrainCmd.Flags().DurationVar(&monitorTimeout, "timeout", time.Minute*10, "Time after which the monitor will stop polling and throw an error.") startCmd.AddCommand(startSLADrainCmd) - /* SLA Aware commands */ startSLADrainCmd.AddCommand(startSLACountDrainCmd) // SLA Maintenance specific flags - startSLACountDrainCmd.Flags().DurationVar(&monitorInterval,"interval", time.Second * 5, "Interval at which to poll scheduler.") - startSLACountDrainCmd.Flags().DurationVar(&monitorTimeout,"timeout", time.Minute * 20, "Time after which the monitor will stop polling and throw an error.") + startSLACountDrainCmd.Flags().DurationVar(&monitorInterval, "interval", time.Second*5, "Interval at which to poll scheduler.") + startSLACountDrainCmd.Flags().DurationVar(&monitorTimeout, "timeout", time.Minute*20, "Time after which the monitor will stop polling and throw an error.") startSLACountDrainCmd.Flags().Int64Var(&count, "count", 5, "Instances count that should be running to meet SLA.") - startSLACountDrainCmd.Flags().DurationVar(&duration, "duration", time.Minute * 10, "Window of time from which we derive the SLA.") + startSLACountDrainCmd.Flags().DurationVar(&duration, "duration", time.Minute*10, "Window of time from which we derive the SLA.") startSLADrainCmd.AddCommand(startSLAPercentageDrainCmd) // SLA Maintenance specific flags - startSLAPercentageDrainCmd.Flags().DurationVar(&monitorInterval,"interval", time.Second * 5, "Interval at which to poll scheduler.") - startSLAPercentageDrainCmd.Flags().DurationVar(&monitorTimeout,"timeout", time.Minute * 1, "Time after which the monitor will stop polling and throw an error.") - startSLAPercentageDrainCmd.Flags().Float64Var(&percent, "percent", 75.0, "Percentage of instances that should be running to meet SLA.") - startSLAPercentageDrainCmd.Flags().DurationVar(&duration, "duration", time.Minute * 10, "Window of time from which we derive the SLA.") + startSLAPercentageDrainCmd.Flags().DurationVar(&monitorInterval, "interval", time.Second*5, "Interval at which to poll scheduler.") + startSLAPercentageDrainCmd.Flags().DurationVar(&monitorTimeout, "timeout", time.Minute*1, "Time after which the monitor will stop polling and throw an error.") + startSLAPercentageDrainCmd.Flags().Float64Var(&percent, "percent", 75.0, "Percentage of instances that should be running to meet SLA.") + startSLAPercentageDrainCmd.Flags().DurationVar(&duration, "duration", time.Minute*10, "Window of time from which we derive the SLA.") startCmd.AddCommand(startMaintenanceCmd) // SLA Maintenance specific flags - startMaintenanceCmd.Flags().DurationVar(&monitorInterval,"interval", time.Second * 5, "Interval at which to poll scheduler.") - startMaintenanceCmd.Flags().DurationVar(&monitorTimeout,"timeout", time.Minute * 10, "Time after which the monitor will stop polling and throw an error.") + startMaintenanceCmd.Flags().DurationVar(&monitorInterval, "interval", time.Second*5, "Interval at which to poll scheduler.") + startMaintenanceCmd.Flags().DurationVar(&monitorTimeout, "timeout", time.Minute*10, "Time after which the monitor will stop polling and throw an error.") } var startCmd = &cobra.Command{ @@ -150,7 +149,7 @@ func SLACountDrain(cmd *cobra.Command, args []string) { slaDrain(&aurora.SlaPolicy{ CountSlaPolicy: &aurora.CountSlaPolicy{Count: count, DurationSecs: int64(duration.Seconds())}}, - args...) + args...) } func SLAPercentageDrain(cmd *cobra.Command, args []string) { @@ -159,7 +158,7 @@ func SLAPercentageDrain(cmd *cobra.Command, args []string) { slaDrain(&aurora.SlaPolicy{ PercentageSlaPolicy: &aurora.PercentageSlaPolicy{Percentage: percent, DurationSecs: int64(duration.Seconds())}}, - args...) + args...) } func maintenance(cmd *cobra.Command, args []string) { @@ -179,7 +178,6 @@ func maintenance(cmd *cobra.Command, args []string) { monitorInterval, monitorTimeout) - maintenanceMonitorPrint(hostResult, []aurora.MaintenanceMode{aurora.MaintenanceMode_SCHEDULED}) if err != nil { diff --git a/cmd/stop.go b/cmd/stop.go index 85e211a..39b9889 100644 --- a/cmd/stop.go +++ b/cmd/stop.go @@ -12,8 +12,8 @@ func init() { // Stop subcommands stopCmd.AddCommand(stopMaintCmd) - stopMaintCmd.Flags().DurationVar(&monitorInterval,"interval", time.Second * 5, "Interval at which to poll scheduler.") - stopMaintCmd.Flags().DurationVar(&monitorTimeout,"timeout", time.Minute * 1, "Time after which the monitor will stop polling and throw an error.") + stopMaintCmd.Flags().DurationVar(&monitorInterval, "interval", time.Second*5, "Interval at which to poll scheduler.") + stopMaintCmd.Flags().DurationVar(&monitorTimeout, "timeout", time.Minute*1, "Time after which the monitor will stop polling and throw an error.") // Stop update @@ -60,7 +60,7 @@ func endMaintenance(cmd *cobra.Command, args []string) { monitorInterval, monitorTimeout) - maintenanceMonitorPrint(hostResult,[]aurora.MaintenanceMode{aurora.MaintenanceMode_NONE}) + maintenanceMonitorPrint(hostResult, []aurora.MaintenanceMode{aurora.MaintenanceMode_NONE}) if err != nil { log.Fatalln("error: %+v", err) diff --git a/cmd/util.go b/cmd/util.go index 7d4ee58..7eb99ea 100644 --- a/cmd/util.go +++ b/cmd/util.go @@ -1,9 +1,9 @@ package cmd import ( - "bytes" - "encoding/json" - "fmt" + "bytes" + "encoding/json" + "fmt" "github.com/paypal/gorealis/v2/gen-go/apache/aurora" "github.com/sirupsen/logrus" @@ -11,38 +11,36 @@ import ( func toJSON(v interface{}) string { - output, err := json.Marshal(v) + output, err := json.Marshal(v) - if err != nil { - log.Fatalln("Unable to serialize Aurora response: %+v", v) - } + if err != nil { + log.Fatalln("Unable to serialize Aurora response: %+v", v) + } - return string(output) + return string(output) } - func getLoggingLevels() string { - var buffer bytes.Buffer + var buffer bytes.Buffer - for _, level := range logrus.AllLevels { - buffer.WriteString(level.String()) - buffer.WriteString(" ") - } + for _, level := range logrus.AllLevels { + buffer.WriteString(level.String()) + buffer.WriteString(" ") + } - buffer.Truncate(buffer.Len()-1) + buffer.Truncate(buffer.Len() - 1) - return buffer.String() + return buffer.String() } - func maintenanceMonitorPrint(hostResult map[string]bool, desiredStates []aurora.MaintenanceMode) { if len(hostResult) > 0 { // Create anonymous struct for JSON formatting - output := struct{ - DesiredStates []string `json:desired_states` - Transitioned []string `json:transitioned` + output := struct { + DesiredStates []string `json:desired_states` + Transitioned []string `json:transitioned` NonTransitioned []string `json:non-transitioned` }{ make([]string, 0), @@ -50,7 +48,7 @@ func maintenanceMonitorPrint(hostResult map[string]bool, desiredStates []aurora. make([]string, 0), } - for _,state := range desiredStates { + for _, state := range desiredStates { output.DesiredStates = append(output.DesiredStates, state.String()) }