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
|
|
|
)
|
|
|
|
|
|
|
|
type StatusCommand struct {
|
2015-09-14 20:13:52 +00:00
|
|
|
Meta
|
2015-09-11 07:38:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *StatusCommand) Help() string {
|
|
|
|
helpText := `
|
2015-09-11 17:00:55 +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
|
|
|
|
queried, and drops verbose information about allocations
|
|
|
|
and evaluations.
|
|
|
|
`
|
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 {
|
2015-09-15 18:35:38 +00:00
|
|
|
var short bool
|
|
|
|
|
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, "")
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
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]
|
2015-09-11 07:38:15 +00:00
|
|
|
job, _, err := client.Jobs().Info(jobID, nil)
|
|
|
|
if err != nil {
|
2015-12-24 10:46:59 +00:00
|
|
|
jobs, _, err := client.Jobs().PrefixList(jobID)
|
|
|
|
if err != nil {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
if len(jobs) > 1 {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
c.Ui.Output(formatList(out))
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
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 {
|
|
|
|
basic = append(basic, fmt.Sprintf("Next Periodic Launch|%v",
|
|
|
|
sJob.Periodic.Next(time.Now())))
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Ui.Output(formatKV(basic))
|
|
|
|
|
|
|
|
if !periodic && !short {
|
|
|
|
var evals, allocs []string
|
|
|
|
|
2015-09-15 18:35:38 +00:00
|
|
|
// Query the evaluations
|
2015-12-19 20:05:17 +00:00
|
|
|
jobEvals, _, err := client.Jobs().Evaluations(job.ID, nil)
|
2015-09-15 18:35:38 +00:00
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error querying job evaluations: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Query the allocations
|
2015-12-19 20:05:17 +00:00
|
|
|
jobAllocs, _, err := client.Jobs().Allocations(job.ID, nil)
|
2015-09-15 18:35:38 +00:00
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error querying job allocations: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Format the evals
|
|
|
|
evals = make([]string, len(jobEvals)+1)
|
2015-09-18 23:33:08 +00:00
|
|
|
evals[0] = "ID|Priority|TriggeredBy|Status"
|
2015-09-15 18:35:38 +00:00
|
|
|
for i, eval := range jobEvals {
|
2015-09-18 23:33:08 +00:00
|
|
|
evals[i+1] = fmt.Sprintf("%s|%d|%s|%s",
|
2015-09-15 18:35:38 +00:00
|
|
|
eval.ID,
|
|
|
|
eval.Priority,
|
|
|
|
eval.TriggeredBy,
|
2015-09-15 20:15:33 +00:00
|
|
|
eval.Status)
|
2015-09-15 18:35:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Format the allocs
|
|
|
|
allocs = make([]string, len(jobAllocs)+1)
|
2015-09-18 17:52:32 +00:00
|
|
|
allocs[0] = "ID|EvalID|NodeID|TaskGroup|Desired|Status"
|
2015-09-15 18:35:38 +00:00
|
|
|
for i, alloc := range jobAllocs {
|
|
|
|
allocs[i+1] = fmt.Sprintf("%s|%s|%s|%s|%s|%s",
|
|
|
|
alloc.ID,
|
|
|
|
alloc.EvalID,
|
2015-09-15 20:15:33 +00:00
|
|
|
alloc.NodeID,
|
2015-09-15 18:35:38 +00:00
|
|
|
alloc.TaskGroup,
|
|
|
|
alloc.DesiredStatus,
|
|
|
|
alloc.ClientStatus)
|
|
|
|
}
|
2015-09-15 03:29:08 +00:00
|
|
|
|
2015-09-18 17:52:32 +00:00
|
|
|
c.Ui.Output("\n==> Evaluations")
|
2015-09-15 23:44:38 +00:00
|
|
|
c.Ui.Output(formatList(evals))
|
2015-09-18 17:52:32 +00:00
|
|
|
c.Ui.Output("\n==> Allocations")
|
2015-09-15 23:44:38 +00:00
|
|
|
c.Ui.Output(formatList(allocs))
|
2015-09-15 18:35:38 +00:00
|
|
|
}
|
2015-12-21 22:19:53 +00:00
|
|
|
|
2015-09-11 07:38:15 +00:00
|
|
|
return 0
|
|
|
|
}
|
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
|
|
|
|
}
|