2015-09-11 07:38:15 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2015-12-21 22:19:53 +00:00
|
|
|
"bytes"
|
|
|
|
"encoding/gob"
|
2015-09-11 07:38:15 +00:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
2015-12-21 22:19:53 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/api"
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2015-09-11 07:38:15 +00:00
|
|
|
)
|
|
|
|
|
2016-05-26 00:06:20 +00:00
|
|
|
const (
|
|
|
|
// maxFailedTGs is the maximum number of task groups we show failure reasons
|
|
|
|
// for before defering to eval-status
|
|
|
|
maxFailedTGs = 5
|
|
|
|
)
|
|
|
|
|
2015-09-11 07:38:15 +00:00
|
|
|
type StatusCommand struct {
|
2015-09-14 20:13:52 +00:00
|
|
|
Meta
|
2016-05-26 00:06:20 +00:00
|
|
|
length int
|
|
|
|
showEvals, verbose bool
|
2015-09-11 07:38:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *StatusCommand) Help() string {
|
|
|
|
helpText := `
|
2016-01-27 20:00:31 +00:00
|
|
|
Usage: nomad status [options] <job>
|
2015-09-11 07:38:15 +00:00
|
|
|
|
2015-09-13 18:39:49 +00:00
|
|
|
Display status information about jobs. If no job ID is given,
|
2015-09-11 17:00:55 +00:00
|
|
|
a list of all known jobs will be dumped.
|
2015-09-11 07:38:15 +00:00
|
|
|
|
2015-09-14 20:13:52 +00:00
|
|
|
General Options:
|
2015-09-11 07:38:15 +00:00
|
|
|
|
2015-09-15 18:35:38 +00:00
|
|
|
` + generalOptionsUsage() + `
|
|
|
|
|
|
|
|
Status Options:
|
|
|
|
|
|
|
|
-short
|
|
|
|
Display short output. Used only when a single job is being
|
2016-05-26 00:06:20 +00:00
|
|
|
queried, and drops verbose information about allocations.
|
|
|
|
|
|
|
|
-evals
|
|
|
|
Display the evaluations associated with the job.
|
2016-01-14 20:57:43 +00:00
|
|
|
|
2016-01-15 22:32:38 +00:00
|
|
|
-verbose
|
|
|
|
Display full information.
|
2015-09-15 18:35:38 +00:00
|
|
|
`
|
2015-09-11 07:38:15 +00:00
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *StatusCommand) Synopsis() string {
|
2015-09-13 18:39:49 +00:00
|
|
|
return "Display status information about jobs"
|
2015-09-11 07:38:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *StatusCommand) Run(args []string) int {
|
2016-05-26 00:06:20 +00:00
|
|
|
var short bool
|
2015-09-15 18:35:38 +00:00
|
|
|
|
2015-09-14 20:13:52 +00:00
|
|
|
flags := c.Meta.FlagSet("status", FlagSetClient)
|
2015-09-11 07:38:15 +00:00
|
|
|
flags.Usage = func() { c.Ui.Output(c.Help()) }
|
2015-09-15 18:35:38 +00:00
|
|
|
flags.BoolVar(&short, "short", false, "")
|
2016-05-26 00:06:20 +00:00
|
|
|
flags.BoolVar(&c.showEvals, "evals", false, "")
|
|
|
|
flags.BoolVar(&c.verbose, "verbose", false, "")
|
2015-09-15 18:35:38 +00:00
|
|
|
|
2015-09-11 07:38:15 +00:00
|
|
|
if err := flags.Parse(args); err != nil {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that we either got no jobs or exactly one.
|
2015-09-14 20:13:52 +00:00
|
|
|
args = flags.Args()
|
|
|
|
if len(args) > 1 {
|
2015-09-11 07:38:15 +00:00
|
|
|
c.Ui.Error(c.Help())
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2016-01-14 20:57:43 +00:00
|
|
|
// Truncate the id unless full length is requested
|
2016-01-15 22:32:38 +00:00
|
|
|
c.length = shortId
|
2016-05-26 00:06:20 +00:00
|
|
|
if c.verbose {
|
2016-01-15 22:32:38 +00:00
|
|
|
c.length = fullId
|
2016-01-14 20:57:43 +00:00
|
|
|
}
|
|
|
|
|
2015-09-11 07:38:15 +00:00
|
|
|
// Get the HTTP client
|
2015-09-14 20:13:52 +00:00
|
|
|
client, err := c.Meta.Client()
|
2015-09-11 07:38:15 +00:00
|
|
|
if err != nil {
|
2015-09-14 20:13:52 +00:00
|
|
|
c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
|
2015-09-11 07:38:15 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Invoke list mode if no job ID.
|
2015-09-14 20:13:52 +00:00
|
|
|
if len(args) == 0 {
|
2015-09-11 07:38:15 +00:00
|
|
|
jobs, _, err := client.Jobs().List(nil)
|
|
|
|
if err != nil {
|
2015-09-14 20:13:52 +00:00
|
|
|
c.Ui.Error(fmt.Sprintf("Error querying jobs: %s", err))
|
2015-09-11 07:38:15 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2015-09-11 17:00:55 +00:00
|
|
|
// No output if we have no jobs
|
|
|
|
if len(jobs) == 0 {
|
2016-01-14 19:56:06 +00:00
|
|
|
c.Ui.Output("No running jobs")
|
2015-09-11 17:00:55 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2015-09-13 18:39:49 +00:00
|
|
|
out := make([]string, len(jobs)+1)
|
2015-09-14 03:13:55 +00:00
|
|
|
out[0] = "ID|Type|Priority|Status"
|
2015-09-13 18:39:49 +00:00
|
|
|
for i, job := range jobs {
|
2015-09-14 03:13:55 +00:00
|
|
|
out[i+1] = fmt.Sprintf("%s|%s|%d|%s",
|
2015-09-11 07:38:15 +00:00
|
|
|
job.ID,
|
|
|
|
job.Type,
|
|
|
|
job.Priority,
|
2015-09-13 18:39:49 +00:00
|
|
|
job.Status)
|
2015-09-11 07:38:15 +00:00
|
|
|
}
|
2015-09-15 23:44:38 +00:00
|
|
|
c.Ui.Output(formatList(out))
|
2015-09-11 07:38:15 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try querying the job
|
2015-09-14 20:13:52 +00:00
|
|
|
jobID := args[0]
|
2016-03-17 23:48:45 +00:00
|
|
|
jobs, _, err := client.Jobs().PrefixList(jobID)
|
2015-09-11 07:38:15 +00:00
|
|
|
if err != nil {
|
2016-03-17 23:48:45 +00:00
|
|
|
c.Ui.Error(fmt.Sprintf("Error querying job: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
if len(jobs) == 0 {
|
|
|
|
c.Ui.Error(fmt.Sprintf("No job(s) with prefix or id %q found", jobID))
|
|
|
|
return 1
|
|
|
|
}
|
2016-03-31 05:17:41 +00:00
|
|
|
if len(jobs) > 1 && strings.TrimSpace(jobID) != jobs[0].ID {
|
2016-03-17 23:48:45 +00:00
|
|
|
out := make([]string, len(jobs)+1)
|
|
|
|
out[0] = "ID|Type|Priority|Status"
|
|
|
|
for i, job := range jobs {
|
|
|
|
out[i+1] = fmt.Sprintf("%s|%s|%d|%s",
|
|
|
|
job.ID,
|
|
|
|
job.Type,
|
|
|
|
job.Priority,
|
|
|
|
job.Status)
|
2015-12-24 10:46:59 +00:00
|
|
|
}
|
2016-03-17 23:48:45 +00:00
|
|
|
c.Ui.Output(fmt.Sprintf("Prefix matched multiple jobs\n\n%s", formatList(out)))
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
// Prefix lookup matched a single job
|
|
|
|
job, _, err := client.Jobs().Info(jobs[0].ID, nil)
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error querying job: %s", err))
|
|
|
|
return 1
|
2015-09-11 07:38:15 +00:00
|
|
|
}
|
|
|
|
|
2015-12-21 22:19:53 +00:00
|
|
|
// Check if it is periodic
|
|
|
|
sJob, err := convertApiJob(job)
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error converting job: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
periodic := sJob.IsPeriodic()
|
|
|
|
|
2015-09-11 07:38:15 +00:00
|
|
|
// Format the job info
|
|
|
|
basic := []string{
|
2015-09-15 23:44:38 +00:00
|
|
|
fmt.Sprintf("ID|%s", job.ID),
|
|
|
|
fmt.Sprintf("Name|%s", job.Name),
|
|
|
|
fmt.Sprintf("Type|%s", job.Type),
|
|
|
|
fmt.Sprintf("Priority|%d", job.Priority),
|
|
|
|
fmt.Sprintf("Datacenters|%s", strings.Join(job.Datacenters, ",")),
|
|
|
|
fmt.Sprintf("Status|%s", job.Status),
|
2015-12-21 22:19:53 +00:00
|
|
|
fmt.Sprintf("Periodic|%v", periodic),
|
2015-09-11 07:38:15 +00:00
|
|
|
}
|
|
|
|
|
2015-12-21 22:19:53 +00:00
|
|
|
if periodic {
|
2016-06-15 22:26:58 +00:00
|
|
|
now := time.Now().UTC()
|
|
|
|
next := sJob.Periodic.Next(now)
|
2016-06-15 20:34:45 +00:00
|
|
|
basic = append(basic, fmt.Sprintf("Next Periodic Launch|%s",
|
2016-06-15 22:26:58 +00:00
|
|
|
fmt.Sprintf("%s (%s from now)",
|
|
|
|
formatTime(next), formatTimeDifference(now, next, time.Second))))
|
2015-12-21 22:19:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
c.Ui.Output(formatKV(basic))
|
|
|
|
|
2016-01-07 22:24:25 +00:00
|
|
|
// Exit early
|
|
|
|
if short {
|
|
|
|
return 0
|
|
|
|
}
|
2015-12-21 22:19:53 +00:00
|
|
|
|
2016-01-07 22:24:25 +00:00
|
|
|
// Print periodic job information
|
|
|
|
if periodic {
|
|
|
|
if err := c.outputPeriodicInfo(client, job); err != nil {
|
|
|
|
c.Ui.Error(err.Error())
|
2015-09-15 18:35:38 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2016-01-07 22:24:25 +00:00
|
|
|
return 0
|
|
|
|
}
|
2015-09-15 18:35:38 +00:00
|
|
|
|
2016-01-07 22:24:25 +00:00
|
|
|
if err := c.outputJobInfo(client, job); err != nil {
|
|
|
|
c.Ui.Error(err.Error())
|
|
|
|
return 1
|
|
|
|
}
|
2015-09-15 18:35:38 +00:00
|
|
|
|
2016-01-07 22:24:25 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2016-01-07 22:43:55 +00:00
|
|
|
// outputPeriodicInfo prints information about the passed periodic job. If a
|
|
|
|
// request fails, an error is returned.
|
2016-01-07 22:24:25 +00:00
|
|
|
func (c *StatusCommand) outputPeriodicInfo(client *api.Client, job *api.Job) error {
|
|
|
|
// Generate the prefix that matches launched jobs from the periodic job.
|
|
|
|
prefix := fmt.Sprintf("%s%s", job.ID, structs.PeriodicLaunchSuffix)
|
2016-01-07 22:43:55 +00:00
|
|
|
children, _, err := client.Jobs().PrefixList(prefix)
|
2016-01-07 22:24:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error querying job: %s", err)
|
|
|
|
}
|
2015-09-15 03:29:08 +00:00
|
|
|
|
2016-01-07 22:43:55 +00:00
|
|
|
if len(children) == 0 {
|
2016-01-08 18:32:08 +00:00
|
|
|
c.Ui.Output("\nNo instances of periodic job found")
|
2016-01-07 22:43:55 +00:00
|
|
|
return nil
|
2015-09-15 18:35:38 +00:00
|
|
|
}
|
2015-12-21 22:19:53 +00:00
|
|
|
|
2016-01-07 22:43:55 +00:00
|
|
|
out := make([]string, 1)
|
|
|
|
out[0] = "ID|Status"
|
|
|
|
for _, child := range children {
|
|
|
|
// Ensure that we are only showing jobs whose parent is the requested
|
|
|
|
// job.
|
|
|
|
if child.ParentID != job.ID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
out = append(out, fmt.Sprintf("%s|%s",
|
|
|
|
child.ID,
|
|
|
|
child.Status))
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Ui.Output(fmt.Sprintf("\nPreviously launched jobs:\n%s", formatList(out)))
|
2016-01-07 22:24:25 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-07 22:43:55 +00:00
|
|
|
// outputJobInfo prints information about the passed non-periodic job. If a
|
|
|
|
// request fails, an error is returned.
|
2016-01-07 22:24:25 +00:00
|
|
|
func (c *StatusCommand) outputJobInfo(client *api.Client, job *api.Job) error {
|
|
|
|
var evals, allocs []string
|
|
|
|
|
2016-05-26 00:06:20 +00:00
|
|
|
// Query the allocations
|
|
|
|
jobAllocs, _, err := client.Jobs().Allocations(job.ID, nil)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error querying job allocations: %s", err)
|
|
|
|
}
|
|
|
|
|
2016-01-07 22:24:25 +00:00
|
|
|
// Query the evaluations
|
|
|
|
jobEvals, _, err := client.Jobs().Evaluations(job.ID, nil)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error querying job evaluations: %s", err)
|
|
|
|
}
|
|
|
|
|
2016-07-21 21:08:06 +00:00
|
|
|
// Query the summary
|
|
|
|
summary, _, err := client.Jobs().Summary(job.ID, nil)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error querying job summary: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Format the summary
|
|
|
|
c.Ui.Output(c.Colorize().Color("\n[bold]Summary[reset]"))
|
|
|
|
if summary != nil {
|
|
|
|
summaries := make([]string, len(summary.Summary)+1)
|
|
|
|
summaries[0] = "Task Group|Queued|Starting|Running|Failed|Complete|Lost"
|
|
|
|
idx := 1
|
|
|
|
for tg, tgs := range summary.Summary {
|
|
|
|
summaries[idx] = fmt.Sprintf("%s|%d|%d|%d|%d|%d|%d",
|
|
|
|
tg, tgs.Queued, tgs.Starting,
|
|
|
|
tgs.Running, tgs.Failed,
|
|
|
|
tgs.Complete, tgs.Lost,
|
|
|
|
)
|
2016-07-21 21:43:21 +00:00
|
|
|
idx += 1
|
2016-07-21 21:08:06 +00:00
|
|
|
}
|
|
|
|
c.Ui.Output(formatList(summaries))
|
|
|
|
}
|
|
|
|
|
2016-05-26 00:06:20 +00:00
|
|
|
// Determine latest evaluation with failures whose follow up hasn't
|
2016-05-27 00:37:08 +00:00
|
|
|
// completed, this is done while formatting
|
2016-05-26 00:06:20 +00:00
|
|
|
var latestFailedPlacement *api.Evaluation
|
2016-05-27 00:37:08 +00:00
|
|
|
blockedEval := false
|
2016-01-07 22:24:25 +00:00
|
|
|
|
|
|
|
// Format the evals
|
|
|
|
evals = make([]string, len(jobEvals)+1)
|
2016-05-26 00:06:20 +00:00
|
|
|
evals[0] = "ID|Priority|Triggered By|Status|Placement Failures"
|
2016-01-07 22:24:25 +00:00
|
|
|
for i, eval := range jobEvals {
|
2016-06-10 18:02:15 +00:00
|
|
|
failures, _ := evalFailureStatus(eval)
|
2016-06-10 17:56:32 +00:00
|
|
|
evals[i+1] = fmt.Sprintf("%s|%d|%s|%s|%s",
|
2016-01-27 18:42:10 +00:00
|
|
|
limit(eval.ID, c.length),
|
2016-01-07 22:24:25 +00:00
|
|
|
eval.Priority,
|
|
|
|
eval.TriggeredBy,
|
2016-05-26 00:06:20 +00:00
|
|
|
eval.Status,
|
2016-06-10 17:56:32 +00:00
|
|
|
failures,
|
2016-05-26 00:06:20 +00:00
|
|
|
)
|
2016-05-27 00:37:08 +00:00
|
|
|
|
|
|
|
if eval.Status == "blocked" {
|
|
|
|
blockedEval = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(eval.FailedTGAllocs) == 0 {
|
|
|
|
// Skip evals without failures
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if latestFailedPlacement == nil || latestFailedPlacement.CreateIndex < eval.CreateIndex {
|
|
|
|
latestFailedPlacement = eval
|
|
|
|
}
|
2016-05-26 00:06:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if c.verbose || c.showEvals {
|
2016-06-12 20:16:07 +00:00
|
|
|
c.Ui.Output(c.Colorize().Color("\n[bold]Evaluations[reset]"))
|
2016-05-26 00:06:20 +00:00
|
|
|
c.Ui.Output(formatList(evals))
|
|
|
|
}
|
|
|
|
|
2016-05-27 00:37:08 +00:00
|
|
|
if blockedEval && latestFailedPlacement != nil {
|
2016-05-26 00:06:20 +00:00
|
|
|
c.outputFailedPlacements(latestFailedPlacement)
|
2016-01-07 22:24:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Format the allocs
|
2016-06-12 20:16:07 +00:00
|
|
|
c.Ui.Output(c.Colorize().Color("\n[bold]Allocations[reset]"))
|
2016-05-26 01:19:39 +00:00
|
|
|
if len(jobAllocs) > 0 {
|
|
|
|
allocs = make([]string, len(jobAllocs)+1)
|
|
|
|
allocs[0] = "ID|Eval ID|Node ID|Task Group|Desired|Status"
|
|
|
|
for i, alloc := range jobAllocs {
|
|
|
|
allocs[i+1] = fmt.Sprintf("%s|%s|%s|%s|%s|%s",
|
|
|
|
limit(alloc.ID, c.length),
|
|
|
|
limit(alloc.EvalID, c.length),
|
|
|
|
limit(alloc.NodeID, c.length),
|
|
|
|
alloc.TaskGroup,
|
|
|
|
alloc.DesiredStatus,
|
|
|
|
alloc.ClientStatus)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Ui.Output(formatList(allocs))
|
|
|
|
} else {
|
|
|
|
c.Ui.Output("No allocations placed")
|
|
|
|
}
|
2016-01-07 22:24:25 +00:00
|
|
|
return nil
|
2015-09-11 07:38:15 +00:00
|
|
|
}
|
2015-12-21 22:19:53 +00:00
|
|
|
|
2016-05-26 00:06:20 +00:00
|
|
|
func (c *StatusCommand) outputFailedPlacements(failedEval *api.Evaluation) {
|
|
|
|
if failedEval == nil || len(failedEval.FailedTGAllocs) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-06-12 20:16:07 +00:00
|
|
|
c.Ui.Output(c.Colorize().Color("\n[bold]Placement Failure[reset]"))
|
2016-05-26 00:06:20 +00:00
|
|
|
|
|
|
|
sorted := sortedTaskGroupFromMetrics(failedEval.FailedTGAllocs)
|
|
|
|
for i, tg := range sorted {
|
|
|
|
if i >= maxFailedTGs {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2016-05-27 00:37:08 +00:00
|
|
|
c.Ui.Output(fmt.Sprintf("Task Group %q:", tg))
|
2016-05-26 00:06:20 +00:00
|
|
|
metrics := failedEval.FailedTGAllocs[tg]
|
2016-05-31 21:51:23 +00:00
|
|
|
c.Ui.Output(formatAllocMetrics(metrics, false, " "))
|
2016-05-27 00:37:08 +00:00
|
|
|
if i != len(sorted)-1 {
|
|
|
|
c.Ui.Output("")
|
|
|
|
}
|
2016-05-26 00:06:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(sorted) > maxFailedTGs {
|
2016-05-27 00:37:08 +00:00
|
|
|
trunc := fmt.Sprintf("\nPlacement failures truncated. To see remainder run:\nnomad eval-status %s", failedEval.ID)
|
2016-05-26 00:06:20 +00:00
|
|
|
c.Ui.Output(trunc)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-21 22:19:53 +00:00
|
|
|
// convertApiJob is used to take a *api.Job and convert it to an *struct.Job.
|
|
|
|
// This function is just a hammer and probably needs to be revisited.
|
|
|
|
func convertApiJob(in *api.Job) (*structs.Job, error) {
|
|
|
|
gob.Register(map[string]interface{}{})
|
|
|
|
gob.Register([]interface{}{})
|
|
|
|
var structJob *structs.Job
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
if err := gob.NewEncoder(buf).Encode(in); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := gob.NewDecoder(buf).Decode(&structJob); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return structJob, nil
|
|
|
|
}
|