github.com/huiliang/nomad@v0.2.1-0.20151124023127-7a8b664699ff/command/status.go (about) 1 package command 2 3 import ( 4 "fmt" 5 "strings" 6 ) 7 8 type StatusCommand struct { 9 Meta 10 } 11 12 func (c *StatusCommand) Help() string { 13 helpText := ` 14 Usage: nomad status [options] [job] 15 16 Display status information about jobs. If no job ID is given, 17 a list of all known jobs will be dumped. 18 19 General Options: 20 21 ` + generalOptionsUsage() + ` 22 23 Status Options: 24 25 -short 26 Display short output. Used only when a single job is being 27 queried, and drops verbose information about allocations 28 and evaluations. 29 ` 30 return strings.TrimSpace(helpText) 31 } 32 33 func (c *StatusCommand) Synopsis() string { 34 return "Display status information about jobs" 35 } 36 37 func (c *StatusCommand) Run(args []string) int { 38 var short bool 39 40 flags := c.Meta.FlagSet("status", FlagSetClient) 41 flags.Usage = func() { c.Ui.Output(c.Help()) } 42 flags.BoolVar(&short, "short", false, "") 43 44 if err := flags.Parse(args); err != nil { 45 return 1 46 } 47 48 // Check that we either got no jobs or exactly one. 49 args = flags.Args() 50 if len(args) > 1 { 51 c.Ui.Error(c.Help()) 52 return 1 53 } 54 55 // Get the HTTP client 56 client, err := c.Meta.Client() 57 if err != nil { 58 c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err)) 59 return 1 60 } 61 62 // Invoke list mode if no job ID. 63 if len(args) == 0 { 64 jobs, _, err := client.Jobs().List(nil) 65 if err != nil { 66 c.Ui.Error(fmt.Sprintf("Error querying jobs: %s", err)) 67 return 1 68 } 69 70 // No output if we have no jobs 71 if len(jobs) == 0 { 72 return 0 73 } 74 75 out := make([]string, len(jobs)+1) 76 out[0] = "ID|Type|Priority|Status" 77 for i, job := range jobs { 78 out[i+1] = fmt.Sprintf("%s|%s|%d|%s", 79 job.ID, 80 job.Type, 81 job.Priority, 82 job.Status) 83 } 84 c.Ui.Output(formatList(out)) 85 return 0 86 } 87 88 // Try querying the job 89 jobID := args[0] 90 job, _, err := client.Jobs().Info(jobID, nil) 91 if err != nil { 92 c.Ui.Error(fmt.Sprintf("Error querying job: %s", err)) 93 return 1 94 } 95 96 // Format the job info 97 basic := []string{ 98 fmt.Sprintf("ID|%s", job.ID), 99 fmt.Sprintf("Name|%s", job.Name), 100 fmt.Sprintf("Type|%s", job.Type), 101 fmt.Sprintf("Priority|%d", job.Priority), 102 fmt.Sprintf("Datacenters|%s", strings.Join(job.Datacenters, ",")), 103 fmt.Sprintf("Status|%s", job.Status), 104 } 105 106 var evals, allocs []string 107 if !short { 108 // Query the evaluations 109 jobEvals, _, err := client.Jobs().Evaluations(jobID, nil) 110 if err != nil { 111 c.Ui.Error(fmt.Sprintf("Error querying job evaluations: %s", err)) 112 return 1 113 } 114 115 // Query the allocations 116 jobAllocs, _, err := client.Jobs().Allocations(jobID, nil) 117 if err != nil { 118 c.Ui.Error(fmt.Sprintf("Error querying job allocations: %s", err)) 119 return 1 120 } 121 122 // Format the evals 123 evals = make([]string, len(jobEvals)+1) 124 evals[0] = "ID|Priority|TriggeredBy|Status" 125 for i, eval := range jobEvals { 126 evals[i+1] = fmt.Sprintf("%s|%d|%s|%s", 127 eval.ID, 128 eval.Priority, 129 eval.TriggeredBy, 130 eval.Status) 131 } 132 133 // Format the allocs 134 allocs = make([]string, len(jobAllocs)+1) 135 allocs[0] = "ID|EvalID|NodeID|TaskGroup|Desired|Status" 136 for i, alloc := range jobAllocs { 137 allocs[i+1] = fmt.Sprintf("%s|%s|%s|%s|%s|%s", 138 alloc.ID, 139 alloc.EvalID, 140 alloc.NodeID, 141 alloc.TaskGroup, 142 alloc.DesiredStatus, 143 alloc.ClientStatus) 144 } 145 } 146 147 // Dump the output 148 c.Ui.Output(formatKV(basic)) 149 if !short { 150 c.Ui.Output("\n==> Evaluations") 151 c.Ui.Output(formatList(evals)) 152 c.Ui.Output("\n==> Allocations") 153 c.Ui.Output(formatList(allocs)) 154 } 155 return 0 156 }