2015-09-27 19:02:14 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-11-17 02:09:00 +00:00
|
|
|
"sort"
|
2015-09-27 19:02:14 +00:00
|
|
|
"strings"
|
2015-11-17 02:09:00 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/api"
|
2015-09-27 19:02:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type AllocStatusCommand struct {
|
|
|
|
Meta
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *AllocStatusCommand) Help() string {
|
|
|
|
helpText := `
|
2015-09-29 02:41:00 +00:00
|
|
|
Usage: nomad alloc-status [options] <allocation>
|
2015-09-27 19:02:14 +00:00
|
|
|
|
2015-11-17 02:09:00 +00:00
|
|
|
Display information about existing allocations and its tasks. This command can
|
|
|
|
be used to inspect the current status of all allocation, including its running
|
|
|
|
status, metadata, and verbose failure messages reported by internal
|
|
|
|
subsystems.
|
2015-09-27 19:02:14 +00:00
|
|
|
|
|
|
|
General Options:
|
|
|
|
|
2015-11-17 02:09:00 +00:00
|
|
|
` + generalOptionsUsage() + `
|
|
|
|
|
|
|
|
|
|
|
|
-short
|
2015-11-17 02:36:13 +00:00
|
|
|
Display short output. Shows only the most recent task event.
|
2016-01-14 20:57:43 +00:00
|
|
|
|
2016-01-15 22:32:38 +00:00
|
|
|
-verbose
|
|
|
|
Show full information.
|
2015-11-17 02:09:00 +00:00
|
|
|
`
|
|
|
|
|
2015-09-27 19:02:14 +00:00
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *AllocStatusCommand) Synopsis() string {
|
|
|
|
return "Display allocation status information and metadata"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *AllocStatusCommand) Run(args []string) int {
|
2016-01-15 22:32:38 +00:00
|
|
|
var short, verbose bool
|
2015-11-17 02:09:00 +00:00
|
|
|
|
2015-09-27 19:02:14 +00:00
|
|
|
flags := c.Meta.FlagSet("alloc-status", FlagSetClient)
|
|
|
|
flags.Usage = func() { c.Ui.Output(c.Help()) }
|
2015-11-17 02:09:00 +00:00
|
|
|
flags.BoolVar(&short, "short", false, "")
|
2016-01-15 22:32:38 +00:00
|
|
|
flags.BoolVar(&verbose, "verbose", false, "")
|
2015-11-17 02:09:00 +00:00
|
|
|
|
2015-09-27 19:02:14 +00:00
|
|
|
if err := flags.Parse(args); err != nil {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that we got exactly one allocation ID
|
|
|
|
args = flags.Args()
|
2015-11-17 02:44:37 +00:00
|
|
|
if len(args) != 1 {
|
2015-09-27 19:02:14 +00:00
|
|
|
c.Ui.Error(c.Help())
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
allocID := args[0]
|
|
|
|
|
|
|
|
// Get the HTTP client
|
|
|
|
client, err := c.Meta.Client()
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
|
|
|
|
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
|
|
|
length := shortId
|
|
|
|
if verbose {
|
|
|
|
length = fullId
|
2016-01-14 20:57:43 +00:00
|
|
|
}
|
|
|
|
|
2015-09-27 19:02:14 +00:00
|
|
|
// Query the allocation info
|
|
|
|
alloc, _, err := client.Allocations().Info(allocID, nil)
|
|
|
|
if err != nil {
|
2015-12-24 10:46:59 +00:00
|
|
|
allocs, _, err := client.Allocations().PrefixList(allocID)
|
|
|
|
if err != nil {
|
2016-01-06 21:46:57 +00:00
|
|
|
c.Ui.Error(fmt.Sprintf("Error querying allocation: %v", err))
|
2015-12-24 10:46:59 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
if len(allocs) == 0 {
|
|
|
|
c.Ui.Error(fmt.Sprintf("No allocation(s) with prefix or id %q found", allocID))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
if len(allocs) > 1 {
|
|
|
|
// Format the allocs
|
|
|
|
out := make([]string, len(allocs)+1)
|
|
|
|
out[0] = "ID|EvalID|JobID|TaskGroup|DesiredStatus|ClientStatus"
|
|
|
|
for i, alloc := range allocs {
|
|
|
|
out[i+1] = fmt.Sprintf("%s|%s|%s|%s|%s|%s",
|
2016-01-14 20:57:43 +00:00
|
|
|
alloc.ID[:length],
|
|
|
|
alloc.EvalID[:length],
|
2015-12-24 10:46:59 +00:00
|
|
|
alloc.JobID,
|
|
|
|
alloc.TaskGroup,
|
|
|
|
alloc.DesiredStatus,
|
2016-01-14 20:57:43 +00:00
|
|
|
alloc.ClientStatus,
|
|
|
|
)
|
2015-12-24 10:46:59 +00:00
|
|
|
}
|
2016-01-06 23:08:47 +00:00
|
|
|
c.Ui.Output(fmt.Sprintf("Prefix matched multiple allocations\n\n%s", formatList(out)))
|
2015-12-24 10:46:59 +00:00
|
|
|
return 0
|
|
|
|
}
|
2016-01-06 21:46:57 +00:00
|
|
|
// Prefix lookup matched a single allocation
|
2015-12-24 10:46:59 +00:00
|
|
|
alloc, _, err = client.Allocations().Info(allocs[0].ID, nil)
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error querying allocation: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
2015-09-27 19:02:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Format the allocation data
|
|
|
|
basic := []string{
|
2016-01-14 20:57:43 +00:00
|
|
|
fmt.Sprintf("ID|%s", alloc.ID[:length]),
|
|
|
|
fmt.Sprintf("EvalID|%s", alloc.EvalID[:length]),
|
2015-09-27 19:02:14 +00:00
|
|
|
fmt.Sprintf("Name|%s", alloc.Name),
|
2016-01-14 20:57:43 +00:00
|
|
|
fmt.Sprintf("NodeID|%s", alloc.NodeID[:length]),
|
2015-09-27 19:02:14 +00:00
|
|
|
fmt.Sprintf("JobID|%s", alloc.JobID),
|
|
|
|
fmt.Sprintf("ClientStatus|%s", alloc.ClientStatus),
|
|
|
|
fmt.Sprintf("NodesEvaluated|%d", alloc.Metrics.NodesEvaluated),
|
|
|
|
fmt.Sprintf("NodesFiltered|%d", alloc.Metrics.NodesFiltered),
|
|
|
|
fmt.Sprintf("NodesExhausted|%d", alloc.Metrics.NodesExhausted),
|
|
|
|
fmt.Sprintf("AllocationTime|%s", alloc.Metrics.AllocationTime),
|
2015-09-27 20:59:27 +00:00
|
|
|
fmt.Sprintf("CoalescedFailures|%d", alloc.Metrics.CoalescedFailures),
|
2015-09-27 19:02:14 +00:00
|
|
|
}
|
|
|
|
c.Ui.Output(formatKV(basic))
|
|
|
|
|
2015-11-17 02:09:00 +00:00
|
|
|
// Print the state of each task.
|
|
|
|
if short {
|
|
|
|
c.shortTaskStatus(alloc)
|
|
|
|
} else {
|
|
|
|
c.taskStatus(alloc)
|
|
|
|
}
|
|
|
|
|
2015-09-27 20:59:27 +00:00
|
|
|
// Format the detailed status
|
|
|
|
c.Ui.Output("\n==> Status")
|
2016-01-14 20:57:43 +00:00
|
|
|
dumpAllocStatus(c.Ui, alloc, length)
|
2015-09-27 19:02:14 +00:00
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
2015-11-17 02:09:00 +00:00
|
|
|
|
|
|
|
// shortTaskStatus prints out the current state of each task.
|
|
|
|
func (c *AllocStatusCommand) shortTaskStatus(alloc *api.Allocation) {
|
|
|
|
tasks := make([]string, 0, len(alloc.TaskStates)+1)
|
|
|
|
tasks = append(tasks, "Name|State|LastEvent|Time")
|
|
|
|
for task := range c.sortedTaskStateIterator(alloc.TaskStates) {
|
|
|
|
fmt.Println(task)
|
|
|
|
state := alloc.TaskStates[task]
|
|
|
|
lastState := state.State
|
|
|
|
var lastEvent, lastTime string
|
|
|
|
|
|
|
|
l := len(state.Events)
|
|
|
|
if l != 0 {
|
|
|
|
last := state.Events[l-1]
|
|
|
|
lastEvent = last.Type
|
2015-11-17 02:37:38 +00:00
|
|
|
lastTime = c.formatUnixNanoTime(last.Time)
|
2015-11-17 02:09:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tasks = append(tasks, fmt.Sprintf("%s|%s|%s|%s",
|
|
|
|
task, lastState, lastEvent, lastTime))
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Ui.Output("\n==> Tasks")
|
|
|
|
c.Ui.Output(formatList(tasks))
|
|
|
|
}
|
|
|
|
|
|
|
|
// taskStatus prints out the most recent events for each task.
|
|
|
|
func (c *AllocStatusCommand) taskStatus(alloc *api.Allocation) {
|
|
|
|
for task := range c.sortedTaskStateIterator(alloc.TaskStates) {
|
|
|
|
state := alloc.TaskStates[task]
|
|
|
|
events := make([]string, len(state.Events)+1)
|
|
|
|
events[0] = "Time|Type|Description"
|
|
|
|
|
|
|
|
size := len(state.Events)
|
|
|
|
for i, event := range state.Events {
|
2015-11-17 02:37:38 +00:00
|
|
|
formatedTime := c.formatUnixNanoTime(event.Time)
|
2015-11-17 02:09:00 +00:00
|
|
|
|
|
|
|
// Build up the description based on the event type.
|
|
|
|
var desc string
|
|
|
|
switch event.Type {
|
|
|
|
case api.TaskDriverFailure:
|
|
|
|
desc = event.DriverError
|
|
|
|
case api.TaskKilled:
|
|
|
|
desc = event.KillError
|
|
|
|
case api.TaskTerminated:
|
|
|
|
var parts []string
|
|
|
|
parts = append(parts, fmt.Sprintf("Exit Code: %d", event.ExitCode))
|
|
|
|
|
|
|
|
if event.Signal != 0 {
|
|
|
|
parts = append(parts, fmt.Sprintf("Signal: %d", event.Signal))
|
|
|
|
}
|
|
|
|
|
|
|
|
if event.Message != "" {
|
|
|
|
parts = append(parts, fmt.Sprintf("Exit Message: %q", event.Message))
|
|
|
|
}
|
|
|
|
desc = strings.Join(parts, ", ")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reverse order so we are sorted by time
|
|
|
|
events[size-i] = fmt.Sprintf("%s|%s|%s", formatedTime, event.Type, desc)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Ui.Output(fmt.Sprintf("\n==> Task %q is %q\nRecent Events:", task, state.State))
|
|
|
|
c.Ui.Output(formatList(events))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-17 02:37:38 +00:00
|
|
|
// formatUnixNanoTime is a helper for formating time for output.
|
|
|
|
func (c *AllocStatusCommand) formatUnixNanoTime(nano int64) string {
|
2015-11-17 02:09:00 +00:00
|
|
|
t := time.Unix(0, nano)
|
|
|
|
return t.Format("15:04:05 01/02/06")
|
|
|
|
}
|
|
|
|
|
|
|
|
// sortedTaskStateIterator is a helper that takes the task state map and returns a
|
|
|
|
// channel that returns the keys in a sorted order.
|
|
|
|
func (c *AllocStatusCommand) sortedTaskStateIterator(m map[string]*api.TaskState) <-chan string {
|
|
|
|
output := make(chan string, len(m))
|
|
|
|
keys := make([]string, len(m))
|
|
|
|
i := 0
|
2015-11-17 02:37:38 +00:00
|
|
|
for k := range m {
|
2015-11-17 02:09:00 +00:00
|
|
|
keys[i] = k
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
sort.Strings(keys)
|
|
|
|
|
|
|
|
for _, key := range keys {
|
|
|
|
output <- key
|
|
|
|
}
|
|
|
|
|
|
|
|
close(output)
|
|
|
|
return output
|
|
|
|
}
|