From 45512316446a571af7a5e47abd0b709b4e7bff34 Mon Sep 17 00:00:00 2001 From: Renan DelValle Date: Tue, 19 Mar 2019 15:27:20 -0700 Subject: [PATCH] Adding fetch task status. Changed how logrus is used to be able to be passed down to gorealis. --- cmd/create.go | 2 -- cmd/fetch.go | 70 ++++++++++++++++++++++++++++++++++++++++++------- cmd/force.go | 2 -- cmd/kill.go | 2 -- cmd/rollback.go | 1 - cmd/root.go | 10 ++++--- cmd/set.go | 1 - cmd/start.go | 2 -- cmd/stop.go | 5 ++-- cmd/util.go | 6 ++--- 10 files changed, 72 insertions(+), 29 deletions(-) diff --git a/cmd/create.go b/cmd/create.go index f7e9d40..9cf3d9f 100644 --- a/cmd/create.go +++ b/cmd/create.go @@ -2,8 +2,6 @@ package cmd import ( "github.com/spf13/cobra" - - log "github.com/sirupsen/logrus" ) func init() { diff --git a/cmd/fetch.go b/cmd/fetch.go index 49e6d04..843525f 100644 --- a/cmd/fetch.go +++ b/cmd/fetch.go @@ -2,13 +2,11 @@ package cmd import ( "fmt" - "github.com/spf13/pflag" "github.com/paypal/gorealis/v2" "github.com/paypal/gorealis/v2/gen-go/apache/aurora" "github.com/spf13/cobra" - - log "github.com/sirupsen/logrus" + "github.com/spf13/pflag" ) func init() { @@ -17,11 +15,21 @@ func init() { // Sub-commands // Fetch Task Config - fetchCmd.AddCommand(taskConfigCmd) + fetchCmd.AddCommand(fetchTaskCmd) + + // Fetch Task Config + fetchTaskCmd.AddCommand(taskConfigCmd) taskConfigCmd.Flags().StringVarP(env, "environment", "e", "", "Aurora Environment") taskConfigCmd.Flags().StringVarP(role, "role", "r", "", "Aurora Role") taskConfigCmd.Flags().StringVarP(name, "name", "n", "", "Aurora Name") + // Fetch Task Status + fetchTaskCmd.AddCommand(taskStatusCmd) + taskStatusCmd.Flags().StringVarP(env, "environment", "e", "", "Aurora Environment") + 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") @@ -53,11 +61,24 @@ var fetchCmd = &cobra.Command{ Short: "Fetch information from Aurora", } +var fetchTaskCmd = &cobra.Command{ + Use: "task", + Short: "Task information from Aurora", +} + + var taskConfigCmd = &cobra.Command{ Use: "config", Short: "Fetch a list of task configurations from Aurora.", Long: `To be written.`, - Run: fetchTasks, + Run: fetchTasksConfig, +} + +var taskStatusCmd = &cobra.Command{ + Use: "status", + Short: "Fetch task status for a Job key.", + Long: `To be written.`, + Run: fetchTasksStatus, } var leaderCmd = &cobra.Command{ @@ -83,11 +104,11 @@ var fetchStatusCmd = &cobra.Command{ Use: "status", Short: "Fetch the maintenance status of a node from Aurora", Long: `This command will print the actual status of the mesos agent nodes in Aurora server`, - Run: fetchStatus, + Run: fetchHostStatus, } -func fetchTasks(cmd *cobra.Command, args []string) { - fmt.Printf("Fetching job configuration for [%s/%s/%s] \n", env, role, name) +func fetchTasksConfig(cmd *cobra.Command, args []string) { + log.Infof("Fetching job configuration for [%s/%s/%s] \n", *env, *role, *name) // Task Query takes nil for values it shouldn't need to match against. // This allows us to potentially more expensive calls for specific environments, roles, or job names. @@ -117,7 +138,38 @@ func fetchTasks(cmd *cobra.Command, args []string) { } } -func fetchStatus(cmd *cobra.Command, args []string) { +func fetchTasksStatus(cmd *cobra.Command, args []string) { + log.Infof("Fetching task status for [%s/%s/%s] \n", *env, *role, *name) + + // Task Query takes nil for values it shouldn't need to match against. + // This allows us to potentially more expensive calls for specific environments, roles, or job names. + if *env == "" { + env = nil + } + if *role == "" { + role = nil + } + if *role == "" { + role = nil + } + //TODO: Add filtering down by status + taskQuery := &aurora.TaskQuery{Environment: env, Role: role, JobName: name} + + tasks, err := client.GetTaskStatus(taskQuery) + if err != nil { + log.Fatalf("error: %+v\n", err) + } + + if toJson { + fmt.Println(toJSON(tasks)) + } else { + for _, t := range tasks { + fmt.Println(t) + } + } +} + +func fetchHostStatus(cmd *cobra.Command, args []string) { log.Infof("Fetching maintenance status for %v \n", args) result, err := client.MaintenanceStatus(args...) if err != nil { diff --git a/cmd/force.go b/cmd/force.go index 839000b..0b2ec7d 100644 --- a/cmd/force.go +++ b/cmd/force.go @@ -5,8 +5,6 @@ import ( "strconv" "github.com/spf13/cobra" - - log "github.com/sirupsen/logrus" ) func init() { diff --git a/cmd/kill.go b/cmd/kill.go index db59a67..d3f9aff 100644 --- a/cmd/kill.go +++ b/cmd/kill.go @@ -3,8 +3,6 @@ package cmd import ( "github.com/paypal/gorealis/v2" "github.com/spf13/cobra" - - log "github.com/sirupsen/logrus" ) func init() { diff --git a/cmd/rollback.go b/cmd/rollback.go index 8c8d515..bb6dd44 100644 --- a/cmd/rollback.go +++ b/cmd/rollback.go @@ -2,7 +2,6 @@ package cmd import ( "fmt" - "log" "github.com/paypal/gorealis/v2/gen-go/apache/aurora" "github.com/spf13/cobra" diff --git a/cmd/root.go b/cmd/root.go index 7d98a2e..10fcb48 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -8,7 +8,7 @@ import ( "github.com/paypal/gorealis/v2" "github.com/spf13/cobra" - log "github.com/sirupsen/logrus" + "github.com/sirupsen/logrus" ) var username, password, zkAddr, schedAddr string @@ -28,8 +28,9 @@ var count int64 var filename string var message = new(string) var updateID string +var log = logrus.New() -const australisVer = "v0.0.7" +const australisVer = "v0.0.8" var monitorInterval, monitorTimeout time.Duration @@ -68,7 +69,7 @@ func Execute() { // TODO(rdelvalle): Move more from connect into this function func setConfig(cmd *cobra.Command, args []string) { - lvl, err := log.ParseLevel(logLevel) + lvl, err := logrus.ParseLevel(logLevel) if err != nil { log.Fatalf("Log level %v is not valid\n", logLevel) @@ -126,7 +127,8 @@ func connect(cmd *cobra.Command, args []string) { Duration: 10 * time.Second, Factor: 2.0, Jitter: 0.1, - })} + }), + realis.SetLogger(log)} // Prefer zookeeper if both ways of connecting are provided if len(zkAddrSlice) > 0 && zkAddrSlice[0] != "" { diff --git a/cmd/set.go b/cmd/set.go index 07d397f..801c30a 100644 --- a/cmd/set.go +++ b/cmd/set.go @@ -3,7 +3,6 @@ package cmd import ( "fmt" "github.com/pkg/errors" - log "github.com/sirupsen/logrus" "github.com/spf13/cobra" "strconv" "strings" diff --git a/cmd/start.go b/cmd/start.go index eb642bc..81472a7 100644 --- a/cmd/start.go +++ b/cmd/start.go @@ -4,8 +4,6 @@ import ( "github.com/paypal/gorealis/v2/gen-go/apache/aurora" "github.com/spf13/cobra" "time" - - log "github.com/sirupsen/logrus" ) func init() { diff --git a/cmd/stop.go b/cmd/stop.go index 7bd7d2f..85e211a 100644 --- a/cmd/stop.go +++ b/cmd/stop.go @@ -1,11 +1,10 @@ package cmd import ( - "github.com/paypal/gorealis/v2/gen-go/apache/aurora" - "github.com/spf13/cobra" "time" - log "github.com/sirupsen/logrus" + "github.com/paypal/gorealis/v2/gen-go/apache/aurora" + "github.com/spf13/cobra" ) func init() { diff --git a/cmd/util.go b/cmd/util.go index dd58aee..7d4ee58 100644 --- a/cmd/util.go +++ b/cmd/util.go @@ -4,9 +4,9 @@ import ( "bytes" "encoding/json" "fmt" - "github.com/paypal/gorealis/v2/gen-go/apache/aurora" - log "github.com/sirupsen/logrus" + "github.com/paypal/gorealis/v2/gen-go/apache/aurora" + "github.com/sirupsen/logrus" ) func toJSON(v interface{}) string { @@ -25,7 +25,7 @@ func getLoggingLevels() string { var buffer bytes.Buffer - for _, level := range log.AllLevels { + for _, level := range logrus.AllLevels { buffer.WriteString(level.String()) buffer.WriteString(" ") }