From c5b1e2dcc183b98d6cf6ce6921e88cd90c340f85 Mon Sep 17 00:00:00 2001 From: Alex Dadgar Date: Thu, 6 Jul 2017 21:51:13 -0700 Subject: [PATCH] alloc-list shows version --- api/allocations.go | 1 + command/alloc_status.go | 16 +------- command/fs.go | 15 +------- command/logs.go | 15 +------- command/node_status.go | 28 ++------------ command/status.go | 82 +++++++++++++++++++++++++++++++++------- nomad/structs/structs.go | 2 + 7 files changed, 80 insertions(+), 79 deletions(-) diff --git a/api/allocations.go b/api/allocations.go index a6efe4790..203fdc064 100644 --- a/api/allocations.go +++ b/api/allocations.go @@ -138,6 +138,7 @@ type AllocationListStub struct { Name string NodeID string JobID string + JobVersion uint64 TaskGroup string DesiredStatus string DesiredDescription string diff --git a/command/alloc_status.go b/command/alloc_status.go index c9d420c37..8ab5866fa 100644 --- a/command/alloc_status.go +++ b/command/alloc_status.go @@ -135,20 +135,8 @@ func (c *AllocStatusCommand) Run(args []string) int { return 1 } if len(allocs) > 1 { - // Format the allocs - out := make([]string, len(allocs)+1) - out[0] = "ID|Eval ID|Job ID|Task Group|Desired Status|Client Status" - for i, alloc := range allocs { - out[i+1] = fmt.Sprintf("%s|%s|%s|%s|%s|%s", - limit(alloc.ID, length), - limit(alloc.EvalID, length), - alloc.JobID, - alloc.TaskGroup, - alloc.DesiredStatus, - alloc.ClientStatus, - ) - } - c.Ui.Output(fmt.Sprintf("Prefix matched multiple allocations\n\n%s", formatList(out))) + out := formatAllocListStubs(allocs, verbose, length) + c.Ui.Output(fmt.Sprintf("Prefix matched multiple allocations\n\n%s", out)) return 0 } // Prefix lookup matched a single allocation diff --git a/command/fs.go b/command/fs.go index 16fa5a44a..01c33e8fb 100644 --- a/command/fs.go +++ b/command/fs.go @@ -159,19 +159,8 @@ func (f *FSCommand) Run(args []string) int { } if len(allocs) > 1 { // Format the allocs - out := make([]string, len(allocs)+1) - out[0] = "ID|Eval ID|Job ID|Task Group|Desired Status|Client Status" - for i, alloc := range allocs { - out[i+1] = fmt.Sprintf("%s|%s|%s|%s|%s|%s", - limit(alloc.ID, length), - limit(alloc.EvalID, length), - alloc.JobID, - alloc.TaskGroup, - alloc.DesiredStatus, - alloc.ClientStatus, - ) - } - f.Ui.Output(fmt.Sprintf("Prefix matched multiple allocations\n\n%s", formatList(out))) + out := formatAllocListStubs(allocs, verbose, length) + f.Ui.Output(fmt.Sprintf("Prefix matched multiple allocations\n\n%s", out)) return 0 } // Prefix lookup matched a single allocation diff --git a/command/logs.go b/command/logs.go index 6b3d2bb4e..7a86476b2 100644 --- a/command/logs.go +++ b/command/logs.go @@ -135,19 +135,8 @@ func (l *LogsCommand) Run(args []string) int { } if len(allocs) > 1 { // Format the allocs - out := make([]string, len(allocs)+1) - out[0] = "ID|Eval ID|Job ID|Task Group|Desired Status|Client Status" - for i, alloc := range allocs { - out[i+1] = fmt.Sprintf("%s|%s|%s|%s|%s|%s", - limit(alloc.ID, length), - limit(alloc.EvalID, length), - alloc.JobID, - alloc.TaskGroup, - alloc.DesiredStatus, - alloc.ClientStatus, - ) - } - l.Ui.Output(fmt.Sprintf("Prefix matched multiple allocations\n\n%s", formatList(out))) + out := formatAllocListStubs(allocs, verbose, length) + l.Ui.Output(fmt.Sprintf("Prefix matched multiple allocations\n\n%s", out)) return 0 } // Prefix lookup matched a single allocation diff --git a/command/node_status.go b/command/node_status.go index d3cba2bbd..6f9a66ee2 100644 --- a/command/node_status.go +++ b/command/node_status.go @@ -340,16 +340,14 @@ func (c *NodeStatusCommand) formatNode(client *api.Client, node *api.Node) int { } } - allocs, err := getAllocs(client, node, c.length) + nodeAllocs, _, err := client.Nodes().Allocations(node.ID, nil) if err != nil { c.Ui.Error(fmt.Sprintf("Error querying node allocations: %s", err)) return 1 } - if len(allocs) > 1 { - c.Ui.Output(c.Colorize().Color("\n[bold]Allocations[reset]")) - c.Ui.Output(formatList(allocs)) - } + c.Ui.Output(c.Colorize().Color("\n[bold]Allocations[reset]")) + c.Ui.Output(formatAllocList(nodeAllocs, c.verbose, c.length)) if c.verbose { c.formatAttributes(node) @@ -453,26 +451,6 @@ func getRunningAllocs(client *api.Client, nodeID string) ([]*api.Allocation, err return allocs, err } -// getAllocs returns information about every running allocation on the node -func getAllocs(client *api.Client, node *api.Node, length int) ([]string, error) { - var allocs []string - // Query the node allocations - nodeAllocs, _, err := client.Nodes().Allocations(node.ID, nil) - // Format the allocations - allocs = make([]string, len(nodeAllocs)+1) - allocs[0] = "ID|Eval ID|Job ID|Task Group|Desired Status|Client Status" - for i, alloc := range nodeAllocs { - allocs[i+1] = fmt.Sprintf("%s|%s|%s|%s|%s|%s", - limit(alloc.ID, length), - limit(alloc.EvalID, length), - alloc.JobID, - alloc.TaskGroup, - alloc.DesiredStatus, - alloc.ClientStatus) - } - return allocs, err -} - // getAllocatedResources returns the resource usage of the node. func getAllocatedResources(client *api.Client, runningAllocs []*api.Allocation, node *api.Node) []string { // Compute the total diff --git a/command/status.go b/command/status.go index 9dffc6ff8..a970abc14 100644 --- a/command/status.go +++ b/command/status.go @@ -279,7 +279,6 @@ func (c *StatusCommand) outputParameterizedInfo(client *api.Client, job *api.Job // outputJobInfo prints information about the passed non-periodic job. If a // request fails, an error is returned. func (c *StatusCommand) outputJobInfo(client *api.Client, job *api.Job) error { - var evals, allocs []string // Query the allocations jobAllocs, _, err := client.Jobs().Allocations(*job.ID, c.allAllocs, nil) @@ -304,7 +303,7 @@ func (c *StatusCommand) outputJobInfo(client *api.Client, job *api.Job) error { blockedEval := false // Format the evals - evals = make([]string, len(jobEvals)+1) + evals := make([]string, len(jobEvals)+1) evals[0] = "ID|Priority|Triggered By|Status|Placement Failures" for i, eval := range jobEvals { failures, _ := evalFailureStatus(eval) @@ -341,25 +340,80 @@ func (c *StatusCommand) outputJobInfo(client *api.Client, job *api.Job) error { // Format the allocs c.Ui.Output(c.Colorize().Color("\n[bold]Allocations[reset]")) - if len(jobAllocs) > 0 { - allocs = make([]string, len(jobAllocs)+1) - allocs[0] = "ID|Eval ID|Node ID|Task Group|Desired|Status|Created At" - for i, alloc := range jobAllocs { - allocs[i+1] = fmt.Sprintf("%s|%s|%s|%s|%s|%s|%s", - limit(alloc.ID, c.length), - limit(alloc.EvalID, c.length), - limit(alloc.NodeID, c.length), + c.Ui.Output(formatAllocListStubs(jobAllocs, c.verbose, c.length)) + return nil +} + +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 { + allocs[0] = "ID|Eval ID|Node ID|Task Group|Version|Desired|Status|Created At" + for i, alloc := range stubs { + allocs[i+1] = fmt.Sprintf("%s|%s|%s|%s|%d|%s|%s|%s", + limit(alloc.ID, uuidLength), + limit(alloc.EvalID, uuidLength), + limit(alloc.NodeID, uuidLength), alloc.TaskGroup, + alloc.JobVersion, alloc.DesiredStatus, alloc.ClientStatus, formatUnixNanoTime(alloc.CreateTime)) } - - c.Ui.Output(formatList(allocs)) } else { - c.Ui.Output("No allocations placed") + allocs[0] = "ID|Node ID|Task Group|Version|Desired|Status|Created At" + for i, alloc := range stubs { + allocs[i+1] = fmt.Sprintf("%s|%s|%s|%d|%s|%s|%s", + limit(alloc.ID, uuidLength), + limit(alloc.NodeID, uuidLength), + alloc.TaskGroup, + alloc.JobVersion, + alloc.DesiredStatus, + alloc.ClientStatus, + formatUnixNanoTime(alloc.CreateTime)) + } } - return nil + + return formatList(allocs) +} + +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 { + allocs[0] = "ID|Eval ID|Node ID|Task Group|Version|Desired|Status|Created At" + for i, alloc := range allocations { + allocs[i+1] = fmt.Sprintf("%s|%s|%s|%s|%d|%s|%s|%s", + limit(alloc.ID, uuidLength), + limit(alloc.EvalID, uuidLength), + limit(alloc.NodeID, uuidLength), + alloc.TaskGroup, + *alloc.Job.Version, + alloc.DesiredStatus, + alloc.ClientStatus, + formatUnixNanoTime(alloc.CreateTime)) + } + } else { + allocs[0] = "ID|Node ID|Task Group|Version|Desired|Status|Created At" + for i, alloc := range allocations { + allocs[i+1] = fmt.Sprintf("%s|%s|%s|%d|%s|%s|%s", + limit(alloc.ID, uuidLength), + limit(alloc.NodeID, uuidLength), + alloc.TaskGroup, + *alloc.Job.Version, + alloc.DesiredStatus, + alloc.ClientStatus, + formatUnixNanoTime(alloc.CreateTime)) + } + } + + return formatList(allocs) } // outputJobSummary displays the given jobs summary and children job summary diff --git a/nomad/structs/structs.go b/nomad/structs/structs.go index 90459b39a..01f53a041 100644 --- a/nomad/structs/structs.go +++ b/nomad/structs/structs.go @@ -4375,6 +4375,7 @@ func (a *Allocation) Stub() *AllocListStub { Name: a.Name, NodeID: a.NodeID, JobID: a.JobID, + JobVersion: a.Job.Version, TaskGroup: a.TaskGroup, DesiredStatus: a.DesiredStatus, DesiredDescription: a.DesiredDescription, @@ -4395,6 +4396,7 @@ type AllocListStub struct { Name string NodeID string JobID string + JobVersion uint64 TaskGroup string DesiredStatus string DesiredDescription string