2018-01-25 15:31:43 -08:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2018-03-13 17:00:38 -07:00
|
|
|
"github.com/paypal/gorealis"
|
2018-01-25 15:31:43 -08:00
|
|
|
"github.com/paypal/gorealis/gen-go/apache/aurora"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
rootCmd.AddCommand(fetchCmd)
|
|
|
|
|
|
|
|
// Sub-commands
|
2018-03-13 17:00:38 -07:00
|
|
|
|
|
|
|
// Fetch Task Config
|
2018-01-25 15:31:43 -08:00
|
|
|
fetchCmd.AddCommand(taskConfigCmd)
|
2018-09-16 21:23:36 -07:00
|
|
|
taskConfigCmd.Flags().StringVarP(env, "environment", "e", "", "Aurora Environment")
|
|
|
|
taskConfigCmd.Flags().StringVarP(role, "role", "r", "", "Aurora Role")
|
|
|
|
taskConfigCmd.Flags().StringVarP(name, "name", "n", "", "Aurora Name")
|
2018-03-13 17:00:38 -07:00
|
|
|
|
2018-10-23 17:42:52 -07:00
|
|
|
/* Fetch Leader */
|
|
|
|
|
|
|
|
leaderCmd.Flags().String("zkPath", "/aurora/scheduler", "Zookeeper node path where leader election happens")
|
|
|
|
|
|
|
|
// Override usage template to hide global flags
|
|
|
|
leaderCmd.SetUsageTemplate(`Usage:{{if .Runnable}}
|
|
|
|
{{.UseLine}}{{end}}{{if .HasAvailableSubCommands}}
|
|
|
|
{{.CommandPath}} [command]{{end}}{{if gt (len .Aliases) 0}}
|
|
|
|
|
|
|
|
Aliases:
|
|
|
|
{{.NameAndAliases}}{{end}}{{if .HasExample}}
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
{{.Example}}{{end}}{{if .HasAvailableSubCommands}}
|
|
|
|
|
|
|
|
Available Commands:{{range .Commands}}{{if (or .IsAvailableCommand (eq .Name "help"))}}
|
|
|
|
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableLocalFlags}}
|
|
|
|
|
|
|
|
Flags:
|
|
|
|
{{.LocalFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .HasAvailableInheritedFlags}}
|
|
|
|
|
|
|
|
Use "{{.CommandPath}} [command] --help" for more information about a command.{{end}}
|
|
|
|
`)
|
2018-03-13 17:00:38 -07:00
|
|
|
fetchCmd.AddCommand(leaderCmd)
|
2018-06-15 16:12:35 -07:00
|
|
|
|
|
|
|
// Fetch jobs
|
2018-09-16 21:23:36 -07:00
|
|
|
fetchJobsCmd.Flags().StringVarP(role, "role", "r", "", "Aurora Role")
|
2018-10-23 17:42:52 -07:00
|
|
|
fetchCmd.AddCommand(fetchJobsCmd)
|
2018-09-21 14:25:12 +05:30
|
|
|
|
|
|
|
// Fetch Status
|
|
|
|
fetchCmd.AddCommand(fetchStatusCmd)
|
2018-01-25 15:31:43 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
var fetchCmd = &cobra.Command{
|
|
|
|
Use: "fetch",
|
|
|
|
Short: "Fetch information from Aurora",
|
|
|
|
}
|
|
|
|
|
|
|
|
var taskConfigCmd = &cobra.Command{
|
2018-03-13 17:00:38 -07:00
|
|
|
Use: "config",
|
2018-01-25 15:31:43 -08:00
|
|
|
Short: "Fetch a list of task configurations from Aurora.",
|
2018-03-13 17:00:38 -07:00
|
|
|
Long: `To be written.`,
|
|
|
|
Run: fetchTasks,
|
|
|
|
}
|
|
|
|
|
|
|
|
var leaderCmd = &cobra.Command{
|
2018-10-23 17:42:52 -07:00
|
|
|
Use: "leader [zkNode0, zkNode1, ...zkNodeN]",
|
2018-03-13 17:00:38 -07:00
|
|
|
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
|
2018-10-23 17:42:52 -07:00
|
|
|
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.`,
|
2018-03-13 17:00:38 -07:00
|
|
|
Run: fetchLeader,
|
2018-01-25 15:31:43 -08:00
|
|
|
}
|
|
|
|
|
2018-06-15 16:12:35 -07:00
|
|
|
var fetchJobsCmd = &cobra.Command{
|
|
|
|
Use: "jobs",
|
|
|
|
Short: "Fetch a list of task Aurora running under a role.",
|
|
|
|
Long: `To be written.`,
|
|
|
|
Run: fetchJobs,
|
|
|
|
}
|
|
|
|
|
2018-09-21 14:25:12 +05:30
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
2018-01-25 15:31:43 -08:00
|
|
|
func fetchTasks(cmd *cobra.Command, args []string) {
|
|
|
|
fmt.Printf("Fetching job configuration for [%s/%s/%s] \n", env, role, name)
|
|
|
|
|
2018-09-16 21:23:36 -07:00
|
|
|
// 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
|
|
|
|
}
|
2018-01-25 15:31:43 -08:00
|
|
|
//TODO: Add filtering down by status
|
|
|
|
taskQuery := &aurora.TaskQuery{Environment: env, Role: role, JobName: name}
|
|
|
|
|
|
|
|
tasks, err := client.GetTasksWithoutConfigs(taskQuery)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("error: %+v\n", err.Error())
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, t := range tasks {
|
|
|
|
fmt.Println(t)
|
|
|
|
}
|
|
|
|
}
|
2018-03-13 17:00:38 -07:00
|
|
|
|
2018-09-21 14:25:12 +05:30
|
|
|
func fetchStatus(cmd *cobra.Command, args []string) {
|
|
|
|
fmt.Printf("Fetching maintenance status for %v \n", args)
|
|
|
|
_, result, err := client.MaintenanceStatus(args...)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("error: %+v\n", err.Error())
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
for k := range result.Statuses {
|
|
|
|
fmt.Printf("Result: %s:%s\n", k.Host, k.Mode)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-13 17:00:38 -07:00
|
|
|
func fetchLeader(cmd *cobra.Command, args []string) {
|
|
|
|
fmt.Printf("Fetching leader from %v \n", args)
|
|
|
|
|
|
|
|
if len(args) < 1 {
|
|
|
|
fmt.Println("At least one Zookeper node address must be passed in.")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2018-10-23 17:42:52 -07:00
|
|
|
url, err := realis.LeaderFromZKOpts(realis.ZKEndpoints(args...), realis.ZKPath(cmd.Flag("zkPath").Value.String()))
|
2018-03-13 17:00:38 -07:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("error: %+v\n", err.Error())
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2018-10-23 12:55:46 -07:00
|
|
|
fmt.Println(url)
|
2018-03-13 17:00:38 -07:00
|
|
|
}
|
2018-06-15 16:12:35 -07:00
|
|
|
|
|
|
|
// TODO: Expand this to be able to filter by job name and environment.
|
|
|
|
func fetchJobs(cmd *cobra.Command, args []string) {
|
2018-09-16 21:23:36 -07:00
|
|
|
fmt.Printf("Fetching tasks under role: %s \n", *role)
|
2018-06-15 16:12:35 -07:00
|
|
|
|
2018-09-16 21:23:36 -07:00
|
|
|
if *role == "" {
|
2018-06-15 16:12:35 -07:00
|
|
|
fmt.Println("Role must be specified.")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2018-09-16 21:23:36 -07:00
|
|
|
if *role == "*" {
|
2018-06-15 16:12:35 -07:00
|
|
|
fmt.Println("Warning: This is an expensive operation.")
|
2018-09-16 21:23:36 -07:00
|
|
|
*role = ""
|
2018-06-15 16:12:35 -07:00
|
|
|
}
|
|
|
|
|
2018-09-16 21:23:36 -07:00
|
|
|
_, result, err := client.GetJobs(*role)
|
2018-06-15 16:12:35 -07:00
|
|
|
|
|
|
|
if err != nil {
|
2018-09-16 21:23:36 -07:00
|
|
|
fmt.Printf("error: %+v\n", err.Error())
|
2018-06-15 16:12:35 -07:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
for jobConfig, _ := range result.GetConfigs() {
|
|
|
|
fmt.Println(jobConfig)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|