2015-09-11 07:38:15 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-08-04 17:42:53 +00:00
|
|
|
"sort"
|
2015-09-11 07:38:15 +00:00
|
|
|
"strings"
|
2015-12-21 22:19:53 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/api"
|
2017-08-14 14:24:35 +00:00
|
|
|
"github.com/hashicorp/nomad/api/contexts"
|
2015-12-21 22:19:53 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2017-08-14 14:24:35 +00:00
|
|
|
"github.com/posener/complete"
|
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
|
2018-03-11 17:53:08 +00:00
|
|
|
// for before deferring to eval-status
|
2016-05-26 00:06:20 +00:00
|
|
|
maxFailedTGs = 5
|
|
|
|
)
|
|
|
|
|
2017-08-09 15:05:55 +00:00
|
|
|
type JobStatusCommand struct {
|
2015-09-14 20:13:52 +00:00
|
|
|
Meta
|
2016-11-24 12:20:52 +00:00
|
|
|
length int
|
|
|
|
evals bool
|
|
|
|
allAllocs bool
|
|
|
|
verbose bool
|
2015-09-11 07:38:15 +00:00
|
|
|
}
|
|
|
|
|
2017-08-09 15:05:55 +00:00
|
|
|
func (c *JobStatusCommand) Help() string {
|
2015-09-11 07:38:15 +00:00
|
|
|
helpText := `
|
2016-01-27 20:00:31 +00:00
|
|
|
Usage: nomad status [options] <job>
|
2015-09-11 07:38:15 +00:00
|
|
|
|
2017-08-28 23:05:58 +00:00
|
|
|
Display status information about a job. If no job ID is given, a list of all
|
|
|
|
known jobs will be displayed.
|
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-11-24 12:20:52 +00:00
|
|
|
-all-allocs
|
2016-12-20 02:10:02 +00:00
|
|
|
Display all allocations matching the job ID, including those from an older
|
|
|
|
instance of the job.
|
2016-11-24 12:20:52 +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)
|
|
|
|
}
|
|
|
|
|
2017-08-09 15:05:55 +00:00
|
|
|
func (c *JobStatusCommand) Synopsis() string {
|
2017-08-28 23:05:58 +00:00
|
|
|
return "Display status information about a job"
|
2015-09-11 07:38:15 +00:00
|
|
|
}
|
|
|
|
|
2017-08-22 20:41:42 +00:00
|
|
|
func (c *JobStatusCommand) AutocompleteFlags() complete.Flags {
|
2017-08-23 19:53:15 +00:00
|
|
|
return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient),
|
|
|
|
complete.Flags{
|
|
|
|
"-all-allocs": complete.PredictNothing,
|
|
|
|
"-evals": complete.PredictNothing,
|
|
|
|
"-short": complete.PredictNothing,
|
|
|
|
"-verbose": complete.PredictNothing,
|
|
|
|
})
|
2017-08-22 20:41:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *JobStatusCommand) AutocompleteArgs() complete.Predictor {
|
|
|
|
return complete.PredictFunc(func(a complete.Args) []string {
|
2017-08-29 21:29:32 +00:00
|
|
|
client, err := c.Meta.Client()
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-08-28 05:17:51 +00:00
|
|
|
resp, _, err := client.Search().PrefixSearch(a.Last, contexts.Jobs, nil)
|
2017-08-22 20:41:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return []string{}
|
|
|
|
}
|
|
|
|
return resp.Matches[contexts.Jobs]
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-04-18 16:02:11 +00:00
|
|
|
func (c *JobStatusCommand) Name() string { return "status" }
|
|
|
|
|
2017-08-09 15:05:55 +00:00
|
|
|
func (c *JobStatusCommand) Run(args []string) int {
|
2016-05-26 00:06:20 +00:00
|
|
|
var short bool
|
2015-09-15 18:35:38 +00:00
|
|
|
|
2018-04-18 16:02:11 +00:00
|
|
|
flags := c.Meta.FlagSet(c.Name(), 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-07-29 03:49:19 +00:00
|
|
|
flags.BoolVar(&c.evals, "evals", false, "")
|
2016-11-24 12:20:52 +00:00
|
|
|
flags.BoolVar(&c.allAllocs, "all-allocs", false, "")
|
2016-05-26 00:06:20 +00:00
|
|
|
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 {
|
2018-04-18 17:55:51 +00:00
|
|
|
c.Ui.Error("This command takes either no arguments or one: <job>")
|
2018-04-18 16:02:11 +00:00
|
|
|
c.Ui.Error(commandErrorText(c))
|
2015-09-11 07:38:15 +00:00
|
|
|
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
|
|
|
if len(jobs) == 0 {
|
2016-07-29 03:49:19 +00:00
|
|
|
// No output if we have no jobs
|
2016-01-14 19:56:06 +00:00
|
|
|
c.Ui.Output("No running jobs")
|
2016-07-29 03:49:19 +00:00
|
|
|
} else {
|
|
|
|
c.Ui.Output(createStatusListOutput(jobs))
|
2015-09-11 17:00:55 +00:00
|
|
|
}
|
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]
|
2017-08-14 17:35:13 +00:00
|
|
|
|
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 {
|
2017-07-21 00:31:07 +00:00
|
|
|
c.Ui.Error(fmt.Sprintf("Prefix matched multiple jobs\n\n%s", createStatusListOutput(jobs)))
|
|
|
|
return 1
|
2016-03-17 23:48:45 +00:00
|
|
|
}
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2017-02-16 00:56:36 +00:00
|
|
|
periodic := job.IsPeriodic()
|
|
|
|
parameterized := job.IsParameterized()
|
2015-12-21 22:19:53 +00:00
|
|
|
|
2015-09-11 07:38:15 +00:00
|
|
|
// Format the job info
|
|
|
|
basic := []string{
|
2017-02-16 00:49:01 +00:00
|
|
|
fmt.Sprintf("ID|%s", *job.ID),
|
|
|
|
fmt.Sprintf("Name|%s", *job.Name),
|
2017-07-28 17:20:47 +00:00
|
|
|
fmt.Sprintf("Submit Date|%s", formatTime(getSubmitTime(job))),
|
2017-02-16 00:49:01 +00:00
|
|
|
fmt.Sprintf("Type|%s", *job.Type),
|
|
|
|
fmt.Sprintf("Priority|%d", *job.Priority),
|
2015-09-15 23:44:38 +00:00
|
|
|
fmt.Sprintf("Datacenters|%s", strings.Join(job.Datacenters, ",")),
|
2017-07-28 17:20:47 +00:00
|
|
|
fmt.Sprintf("Status|%s", getStatusString(*job.Status, job.Stop)),
|
2015-12-21 22:19:53 +00:00
|
|
|
fmt.Sprintf("Periodic|%v", periodic),
|
2017-01-20 18:33:52 +00:00
|
|
|
fmt.Sprintf("Parameterized|%v", parameterized),
|
2015-09-11 07:38:15 +00:00
|
|
|
}
|
|
|
|
|
2017-03-01 19:45:20 +00:00
|
|
|
if periodic && !parameterized {
|
2017-04-15 23:47:19 +00:00
|
|
|
if *job.Stop {
|
|
|
|
basic = append(basic, fmt.Sprintf("Next Periodic Launch|none (job stopped)"))
|
|
|
|
} else {
|
|
|
|
location, err := job.Periodic.GetLocation()
|
|
|
|
if err == nil {
|
|
|
|
now := time.Now().In(location)
|
|
|
|
next := job.Periodic.Next(now)
|
|
|
|
basic = append(basic, fmt.Sprintf("Next Periodic Launch|%s",
|
|
|
|
fmt.Sprintf("%s (%s from now)",
|
|
|
|
formatTime(next), formatTimeDifference(now, next, time.Second))))
|
|
|
|
}
|
2017-02-20 23:13:10 +00:00
|
|
|
}
|
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
|
2017-03-01 19:45:20 +00:00
|
|
|
if periodic && !parameterized {
|
2016-01-07 22:24:25 +00:00
|
|
|
if err := c.outputPeriodicInfo(client, job); err != nil {
|
|
|
|
c.Ui.Error(err.Error())
|
2015-09-15 18:35:38 +00:00
|
|
|
return 1
|
|
|
|
}
|
2017-01-20 18:33:52 +00:00
|
|
|
} else if parameterized {
|
|
|
|
if err := c.outputParameterizedInfo(client, job); err != nil {
|
2016-12-15 00:58:54 +00:00
|
|
|
c.Ui.Error(err.Error())
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err := c.outputJobInfo(client, job); err != nil {
|
|
|
|
c.Ui.Error(err.Error())
|
|
|
|
return 1
|
|
|
|
}
|
2016-01-07 22:24:25 +00:00
|
|
|
}
|
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.
|
2017-08-09 15:05:55 +00:00
|
|
|
func (c *JobStatusCommand) outputPeriodicInfo(client *api.Client, job *api.Job) error {
|
2016-12-15 00:58:54 +00:00
|
|
|
// Output the summary
|
|
|
|
if err := c.outputJobSummary(client, job); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-01-07 22:24:25 +00:00
|
|
|
// Generate the prefix that matches launched jobs from the periodic job.
|
2017-02-16 22:29:34 +00:00
|
|
|
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.
|
2017-02-06 19:48:28 +00:00
|
|
|
if child.ParentID != *job.ID {
|
2016-01-07 22:43:55 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
out = append(out, fmt.Sprintf("%s|%s",
|
|
|
|
child.ID,
|
|
|
|
child.Status))
|
|
|
|
}
|
|
|
|
|
2016-12-15 00:58:54 +00:00
|
|
|
c.Ui.Output(c.Colorize().Color("\n[bold]Previously Launched Jobs[reset]"))
|
|
|
|
c.Ui.Output(formatList(out))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-01-20 18:33:52 +00:00
|
|
|
// outputParameterizedInfo prints information about a parameterized job. If a
|
2016-12-15 00:58:54 +00:00
|
|
|
// request fails, an error is returned.
|
2017-08-09 15:05:55 +00:00
|
|
|
func (c *JobStatusCommand) outputParameterizedInfo(client *api.Client, job *api.Job) error {
|
2017-01-20 18:33:52 +00:00
|
|
|
// Output parameterized job details
|
|
|
|
c.Ui.Output(c.Colorize().Color("\n[bold]Parameterized Job[reset]"))
|
|
|
|
parameterizedJob := make([]string, 3)
|
|
|
|
parameterizedJob[0] = fmt.Sprintf("Payload|%s", job.ParameterizedJob.Payload)
|
|
|
|
parameterizedJob[1] = fmt.Sprintf("Required Metadata|%v", strings.Join(job.ParameterizedJob.MetaRequired, ", "))
|
|
|
|
parameterizedJob[2] = fmt.Sprintf("Optional Metadata|%v", strings.Join(job.ParameterizedJob.MetaOptional, ", "))
|
|
|
|
c.Ui.Output(formatKV(parameterizedJob))
|
2016-12-15 00:58:54 +00:00
|
|
|
|
|
|
|
// Output the summary
|
|
|
|
if err := c.outputJobSummary(client, job); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-02-16 22:29:34 +00:00
|
|
|
// Generate the prefix that matches launched jobs from the parameterized job.
|
|
|
|
prefix := fmt.Sprintf("%s%s", *job.ID, structs.DispatchLaunchSuffix)
|
2016-12-15 00:58:54 +00:00
|
|
|
children, _, err := client.Jobs().PrefixList(prefix)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error querying job: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(children) == 0 {
|
2017-01-20 18:33:52 +00:00
|
|
|
c.Ui.Output("\nNo dispatched instances of parameterized job found")
|
2016-12-15 00:58:54 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
2017-02-06 19:48:28 +00:00
|
|
|
if child.ParentID != *job.ID {
|
2016-12-15 00:58:54 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
out = append(out, fmt.Sprintf("%s|%s",
|
|
|
|
child.ID,
|
|
|
|
child.Status))
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Ui.Output(c.Colorize().Color("\n[bold]Dispatched Jobs[reset]"))
|
|
|
|
c.Ui.Output(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.
|
2017-08-09 15:05:55 +00:00
|
|
|
func (c *JobStatusCommand) outputJobInfo(client *api.Client, job *api.Job) error {
|
2016-01-07 22:24:25 +00:00
|
|
|
|
2016-05-26 00:06:20 +00:00
|
|
|
// Query the allocations
|
2017-02-06 19:48:28 +00:00
|
|
|
jobAllocs, _, err := client.Jobs().Allocations(*job.ID, c.allAllocs, nil)
|
2016-05-26 00:06:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error querying job allocations: %s", err)
|
|
|
|
}
|
|
|
|
|
2016-01-07 22:24:25 +00:00
|
|
|
// Query the evaluations
|
2017-02-06 19:48:28 +00:00
|
|
|
jobEvals, _, err := client.Jobs().Evaluations(*job.ID, nil)
|
2016-01-07 22:24:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error querying job evaluations: %s", err)
|
|
|
|
}
|
|
|
|
|
2017-07-07 05:44:05 +00:00
|
|
|
latestDeployment, _, err := client.Jobs().LatestDeployment(*job.ID, nil)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error querying latest job deployment: %s", err)
|
|
|
|
}
|
|
|
|
|
2016-12-15 00:58:54 +00:00
|
|
|
// Output the summary
|
|
|
|
if err := c.outputJobSummary(client, job); err != nil {
|
|
|
|
return err
|
2016-07-21 21:08:06 +00:00
|
|
|
}
|
|
|
|
|
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
|
2017-07-07 04:51:13 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-07-29 03:49:19 +00:00
|
|
|
if c.verbose || c.evals {
|
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
|
|
|
}
|
|
|
|
|
2018-03-20 18:34:29 +00:00
|
|
|
c.outputReschedulingEvals(client, job, jobAllocs, c.length)
|
2018-03-20 02:42:37 +00:00
|
|
|
|
2017-07-07 06:04:32 +00:00
|
|
|
if latestDeployment != nil {
|
2017-07-07 05:44:05 +00:00
|
|
|
c.Ui.Output(c.Colorize().Color("\n[bold]Latest Deployment[reset]"))
|
|
|
|
c.Ui.Output(c.Colorize().Color(c.formatDeployment(latestDeployment)))
|
|
|
|
}
|
|
|
|
|
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]"))
|
2017-07-07 04:51:13 +00:00
|
|
|
c.Ui.Output(formatAllocListStubs(jobAllocs, c.verbose, c.length))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-08-09 15:05:55 +00:00
|
|
|
func (c *JobStatusCommand) formatDeployment(d *api.Deployment) string {
|
2017-07-07 05:44:05 +00:00
|
|
|
// Format the high-level elements
|
|
|
|
high := []string{
|
|
|
|
fmt.Sprintf("ID|%s", limit(d.ID, c.length)),
|
|
|
|
fmt.Sprintf("Status|%s", d.Status),
|
|
|
|
fmt.Sprintf("Description|%s", d.StatusDescription),
|
|
|
|
}
|
|
|
|
|
|
|
|
base := formatKV(high)
|
|
|
|
if len(d.TaskGroups) == 0 {
|
|
|
|
return base
|
|
|
|
}
|
|
|
|
base += "\n\n[bold]Deployed[reset]\n"
|
|
|
|
base += formatDeploymentGroups(d, c.length)
|
|
|
|
return base
|
|
|
|
}
|
|
|
|
|
2017-07-07 04:51:13 +00:00
|
|
|
func formatAllocListStubs(stubs []*api.AllocationListStub, verbose bool, uuidLength int) string {
|
|
|
|
if len(stubs) == 0 {
|
|
|
|
return "No allocations placed"
|
|
|
|
}
|
|
|
|
|
|
|
|
allocs := make([]string, len(stubs)+1)
|
|
|
|
if verbose {
|
2017-10-27 22:24:42 +00:00
|
|
|
allocs[0] = "ID|Eval ID|Node ID|Task Group|Version|Desired|Status|Created|Modified"
|
2017-07-07 04:51:13 +00:00
|
|
|
for i, alloc := range stubs {
|
2017-10-26 14:21:05 +00:00
|
|
|
allocs[i+1] = fmt.Sprintf("%s|%s|%s|%s|%d|%s|%s|%s|%s",
|
2017-07-07 04:51:13 +00:00
|
|
|
limit(alloc.ID, uuidLength),
|
|
|
|
limit(alloc.EvalID, uuidLength),
|
|
|
|
limit(alloc.NodeID, uuidLength),
|
|
|
|
alloc.TaskGroup,
|
|
|
|
alloc.JobVersion,
|
|
|
|
alloc.DesiredStatus,
|
|
|
|
alloc.ClientStatus,
|
2017-10-26 14:21:05 +00:00
|
|
|
formatUnixNanoTime(alloc.CreateTime),
|
|
|
|
formatUnixNanoTime(alloc.ModifyTime))
|
2017-07-07 04:51:13 +00:00
|
|
|
}
|
|
|
|
} else {
|
2017-10-26 14:21:05 +00:00
|
|
|
allocs[0] = "ID|Node ID|Task Group|Version|Desired|Status|Created|Modified"
|
2017-07-07 04:51:13 +00:00
|
|
|
for i, alloc := range stubs {
|
2017-12-12 00:08:25 +00:00
|
|
|
now := time.Now()
|
|
|
|
createTimePretty := prettyTimeDiff(time.Unix(0, alloc.CreateTime), now)
|
|
|
|
modTimePretty := prettyTimeDiff(time.Unix(0, alloc.ModifyTime), now)
|
2017-10-26 14:21:05 +00:00
|
|
|
allocs[i+1] = fmt.Sprintf("%s|%s|%s|%d|%s|%s|%s|%s",
|
2017-07-07 04:51:13 +00:00
|
|
|
limit(alloc.ID, uuidLength),
|
|
|
|
limit(alloc.NodeID, uuidLength),
|
2016-05-26 01:19:39 +00:00
|
|
|
alloc.TaskGroup,
|
2017-07-07 04:51:13 +00:00
|
|
|
alloc.JobVersion,
|
2016-05-26 01:19:39 +00:00
|
|
|
alloc.DesiredStatus,
|
2016-08-09 02:15:24 +00:00
|
|
|
alloc.ClientStatus,
|
2017-10-26 14:21:05 +00:00
|
|
|
createTimePretty,
|
|
|
|
modTimePretty)
|
2016-05-26 01:19:39 +00:00
|
|
|
}
|
2017-07-07 04:51:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return formatList(allocs)
|
|
|
|
}
|
2016-05-26 01:19:39 +00:00
|
|
|
|
2017-07-07 04:51:13 +00:00
|
|
|
func formatAllocList(allocations []*api.Allocation, verbose bool, uuidLength int) string {
|
|
|
|
if len(allocations) == 0 {
|
|
|
|
return "No allocations placed"
|
|
|
|
}
|
|
|
|
|
|
|
|
allocs := make([]string, len(allocations)+1)
|
|
|
|
if verbose {
|
2017-12-12 00:08:25 +00:00
|
|
|
allocs[0] = "ID|Eval ID|Node ID|Task Group|Version|Desired|Status|Created|Modified"
|
2017-07-07 04:51:13 +00:00
|
|
|
for i, alloc := range allocations {
|
2017-12-12 00:14:03 +00:00
|
|
|
allocs[i+1] = fmt.Sprintf("%s|%s|%s|%s|%d|%s|%s|%s|%s",
|
2017-07-07 04:51:13 +00:00
|
|
|
limit(alloc.ID, uuidLength),
|
|
|
|
limit(alloc.EvalID, uuidLength),
|
|
|
|
limit(alloc.NodeID, uuidLength),
|
|
|
|
alloc.TaskGroup,
|
2017-07-28 17:20:47 +00:00
|
|
|
getVersion(alloc.Job),
|
2017-07-07 04:51:13 +00:00
|
|
|
alloc.DesiredStatus,
|
|
|
|
alloc.ClientStatus,
|
2017-12-12 00:08:25 +00:00
|
|
|
formatUnixNanoTime(alloc.CreateTime),
|
|
|
|
formatUnixNanoTime(alloc.ModifyTime))
|
2017-07-07 04:51:13 +00:00
|
|
|
}
|
2016-05-26 01:19:39 +00:00
|
|
|
} else {
|
2017-12-12 00:08:25 +00:00
|
|
|
allocs[0] = "ID|Node ID|Task Group|Version|Desired|Status|Created|Modified"
|
2017-07-07 04:51:13 +00:00
|
|
|
for i, alloc := range allocations {
|
2017-12-12 00:08:25 +00:00
|
|
|
now := time.Now()
|
|
|
|
createTimePretty := prettyTimeDiff(time.Unix(0, alloc.CreateTime), now)
|
|
|
|
modTimePretty := prettyTimeDiff(time.Unix(0, alloc.ModifyTime), now)
|
|
|
|
allocs[i+1] = fmt.Sprintf("%s|%s|%s|%d|%s|%s|%s|%s",
|
2017-07-07 04:51:13 +00:00
|
|
|
limit(alloc.ID, uuidLength),
|
|
|
|
limit(alloc.NodeID, uuidLength),
|
|
|
|
alloc.TaskGroup,
|
2017-07-28 17:20:47 +00:00
|
|
|
getVersion(alloc.Job),
|
2017-07-07 04:51:13 +00:00
|
|
|
alloc.DesiredStatus,
|
|
|
|
alloc.ClientStatus,
|
2017-12-12 00:08:25 +00:00
|
|
|
createTimePretty,
|
|
|
|
modTimePretty)
|
2017-07-07 04:51:13 +00:00
|
|
|
}
|
2016-05-26 01:19:39 +00:00
|
|
|
}
|
2017-07-07 04:51:13 +00:00
|
|
|
|
|
|
|
return formatList(allocs)
|
2015-09-11 07:38:15 +00:00
|
|
|
}
|
2015-12-21 22:19:53 +00:00
|
|
|
|
2016-12-15 00:58:54 +00:00
|
|
|
// outputJobSummary displays the given jobs summary and children job summary
|
|
|
|
// where appropriate
|
2017-08-09 15:05:55 +00:00
|
|
|
func (c *JobStatusCommand) outputJobSummary(client *api.Client, job *api.Job) error {
|
2016-12-15 00:58:54 +00:00
|
|
|
// Query the summary
|
2017-02-06 19:48:28 +00:00
|
|
|
summary, _, err := client.Jobs().Summary(*job.ID, nil)
|
2016-12-15 00:58:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error querying job summary: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if summary == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-02-16 00:56:36 +00:00
|
|
|
periodic := job.IsPeriodic()
|
|
|
|
parameterizedJob := job.IsParameterized()
|
2016-12-15 00:58:54 +00:00
|
|
|
|
|
|
|
// Print the summary
|
2017-01-20 18:33:52 +00:00
|
|
|
if !periodic && !parameterizedJob {
|
2016-12-15 00:58:54 +00:00
|
|
|
c.Ui.Output(c.Colorize().Color("\n[bold]Summary[reset]"))
|
|
|
|
summaries := make([]string, len(summary.Summary)+1)
|
|
|
|
summaries[0] = "Task Group|Queued|Starting|Running|Failed|Complete|Lost"
|
|
|
|
taskGroups := make([]string, 0, len(summary.Summary))
|
|
|
|
for taskGroup := range summary.Summary {
|
|
|
|
taskGroups = append(taskGroups, taskGroup)
|
|
|
|
}
|
|
|
|
sort.Strings(taskGroups)
|
|
|
|
for idx, taskGroup := range taskGroups {
|
|
|
|
tgs := summary.Summary[taskGroup]
|
|
|
|
summaries[idx+1] = fmt.Sprintf("%s|%d|%d|%d|%d|%d|%d",
|
|
|
|
taskGroup, tgs.Queued, tgs.Starting,
|
|
|
|
tgs.Running, tgs.Failed,
|
|
|
|
tgs.Complete, tgs.Lost,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
c.Ui.Output(formatList(summaries))
|
|
|
|
}
|
|
|
|
|
2017-01-20 18:33:52 +00:00
|
|
|
// Always display the summary if we are periodic or parameterized, but
|
|
|
|
// only display if the summary is non-zero on normal jobs
|
|
|
|
if summary.Children != nil && (parameterizedJob || periodic || summary.Children.Sum() > 0) {
|
|
|
|
if parameterizedJob {
|
|
|
|
c.Ui.Output(c.Colorize().Color("\n[bold]Parameterized Job Summary[reset]"))
|
2016-12-15 00:58:54 +00:00
|
|
|
} else {
|
|
|
|
c.Ui.Output(c.Colorize().Color("\n[bold]Children Job Summary[reset]"))
|
|
|
|
}
|
|
|
|
summaries := make([]string, 2)
|
|
|
|
summaries[0] = "Pending|Running|Dead"
|
|
|
|
summaries[1] = fmt.Sprintf("%d|%d|%d",
|
|
|
|
summary.Children.Pending, summary.Children.Running, summary.Children.Dead)
|
|
|
|
c.Ui.Output(formatList(summaries))
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-03-20 02:42:37 +00:00
|
|
|
// outputReschedulingEvals displays eval IDs and time for any
|
|
|
|
// delayed evaluations by task group
|
2018-03-20 18:34:29 +00:00
|
|
|
func (c *JobStatusCommand) outputReschedulingEvals(client *api.Client, job *api.Job, allocListStubs []*api.AllocationListStub, uuidLength int) error {
|
2018-03-20 02:42:37 +00:00
|
|
|
// Get the most recent alloc ID by task group
|
|
|
|
|
|
|
|
mostRecentAllocs := make(map[string]*api.AllocationListStub)
|
|
|
|
for _, alloc := range allocListStubs {
|
|
|
|
a, ok := mostRecentAllocs[alloc.TaskGroup]
|
|
|
|
if !ok || alloc.ModifyTime > a.ModifyTime {
|
|
|
|
mostRecentAllocs[alloc.TaskGroup] = alloc
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
followUpEvalIds := make(map[string]string)
|
|
|
|
for tg, alloc := range mostRecentAllocs {
|
|
|
|
if alloc.FollowupEvalID != "" {
|
|
|
|
followUpEvalIds[tg] = alloc.FollowupEvalID
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(followUpEvalIds) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// Print the reschedule info section
|
|
|
|
var delayedEvalInfos []string
|
|
|
|
|
|
|
|
taskGroups := make([]string, 0, len(followUpEvalIds))
|
2018-03-20 03:11:07 +00:00
|
|
|
for taskGroup := range followUpEvalIds {
|
2018-03-20 02:42:37 +00:00
|
|
|
taskGroups = append(taskGroups, taskGroup)
|
|
|
|
}
|
|
|
|
sort.Strings(taskGroups)
|
|
|
|
var evalDetails []string
|
|
|
|
for _, taskGroup := range taskGroups {
|
|
|
|
evalID := followUpEvalIds[taskGroup]
|
|
|
|
evaluation, _, err := client.Evaluations().Info(evalID, nil)
|
|
|
|
// Eval time is not critical output,
|
|
|
|
// so don't return it on errors, if its not set, or its already in the past
|
|
|
|
if err != nil || evaluation.WaitUntil.IsZero() || time.Now().After(evaluation.WaitUntil) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
evalTime := prettyTimeDiff(evaluation.WaitUntil, time.Now())
|
2018-03-21 14:15:29 +00:00
|
|
|
if c.verbose {
|
|
|
|
delayedEvalInfos = append(delayedEvalInfos, "Task Group|Reschedule Policy|Eval ID|Eval Time")
|
|
|
|
rp := job.LookupTaskGroup(taskGroup).ReschedulePolicy
|
|
|
|
evalDetails = append(evalDetails, fmt.Sprintf("%s|%s|%s|%s", taskGroup, rp.String(), limit(evalID, uuidLength), evalTime))
|
|
|
|
} else {
|
|
|
|
delayedEvalInfos = append(delayedEvalInfos, "Task Group|Eval ID|Eval Time")
|
|
|
|
evalDetails = append(evalDetails, fmt.Sprintf("%s|%s|%s", taskGroup, limit(evalID, uuidLength), evalTime))
|
|
|
|
}
|
2018-03-20 02:42:37 +00:00
|
|
|
}
|
2018-03-21 14:15:29 +00:00
|
|
|
if len(evalDetails) == 0 {
|
|
|
|
return nil
|
2018-03-20 02:42:37 +00:00
|
|
|
}
|
2018-03-21 14:15:29 +00:00
|
|
|
// Only show this section if there is pending evals
|
|
|
|
delayedEvalInfos = append(delayedEvalInfos, evalDetails...)
|
2018-03-21 19:28:14 +00:00
|
|
|
c.Ui.Output(c.Colorize().Color("\n[bold]Future Rescheduling Attempts[reset]"))
|
2018-03-21 14:15:29 +00:00
|
|
|
c.Ui.Output(formatList(delayedEvalInfos))
|
2018-03-20 02:42:37 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-08-09 15:05:55 +00:00
|
|
|
func (c *JobStatusCommand) outputFailedPlacements(failedEval *api.Evaluation) {
|
2016-05-26 00:06:20 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-29 03:49:19 +00:00
|
|
|
// list general information about a list of jobs
|
2016-08-05 01:36:22 +00:00
|
|
|
func createStatusListOutput(jobs []*api.JobListStub) string {
|
2016-07-29 03:49:19 +00:00
|
|
|
out := make([]string, len(jobs)+1)
|
2017-06-30 02:08:25 +00:00
|
|
|
out[0] = "ID|Type|Priority|Status|Submit Date"
|
2016-07-29 03:49:19 +00:00
|
|
|
for i, job := range jobs {
|
2017-06-30 02:08:25 +00:00
|
|
|
out[i+1] = fmt.Sprintf("%s|%s|%d|%s|%s",
|
2016-07-29 03:49:19 +00:00
|
|
|
job.ID,
|
2017-04-16 00:05:52 +00:00
|
|
|
getTypeString(job),
|
2016-07-29 03:49:19 +00:00
|
|
|
job.Priority,
|
2017-07-28 17:20:47 +00:00
|
|
|
getStatusString(job.Status, &job.Stop),
|
2017-06-30 02:08:25 +00:00
|
|
|
formatTime(time.Unix(0, job.SubmitTime)))
|
2016-07-29 03:49:19 +00:00
|
|
|
}
|
|
|
|
return formatList(out)
|
2016-08-05 01:36:22 +00:00
|
|
|
}
|
2017-04-15 03:54:30 +00:00
|
|
|
|
2017-04-16 00:05:52 +00:00
|
|
|
func getTypeString(job *api.JobListStub) string {
|
|
|
|
t := job.Type
|
|
|
|
|
|
|
|
if job.Periodic {
|
|
|
|
t += "/periodic"
|
|
|
|
}
|
|
|
|
|
|
|
|
if job.ParameterizedJob {
|
|
|
|
t += "/parameterized"
|
|
|
|
}
|
|
|
|
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2017-07-28 17:20:47 +00:00
|
|
|
func getStatusString(status string, stop *bool) string {
|
|
|
|
if stop != nil && *stop {
|
2017-04-15 03:54:30 +00:00
|
|
|
return fmt.Sprintf("%s (stopped)", status)
|
|
|
|
}
|
|
|
|
return status
|
|
|
|
}
|