Merge pull request #879 from hashicorp/f-add-allocation-output-node-status

Include number of allocations in node-status
This commit is contained in:
Jake Champlin
2016-03-03 15:37:00 -05:00
3 changed files with 60 additions and 13 deletions

View File

@@ -20,7 +20,7 @@ dev: format generate
bin: generate
@sh -c "'$(PWD)/scripts/build.sh'"
release:
release:
@$(MAKE) bin
cov:
@@ -31,7 +31,7 @@ test: generate
@sh -c "'$(PWD)/scripts/test.sh'"
@$(MAKE) vet
cover:
cover:
go list ./... | xargs -n1 go test --cover
format:

View File

@@ -2,6 +2,7 @@ package command
import (
"fmt"
"github.com/hashicorp/nomad/api"
"sort"
"strings"
)
@@ -34,6 +35,9 @@ Node Status Options:
-verbose
Display full information.
-allocs
Display a count of running allocations for each node.
`
return strings.TrimSpace(helpText)
}
@@ -43,12 +47,13 @@ func (c *NodeStatusCommand) Synopsis() string {
}
func (c *NodeStatusCommand) Run(args []string) int {
var short, verbose bool
var short, verbose, list_allocs bool
flags := c.Meta.FlagSet("node-status", FlagSetClient)
flags.Usage = func() { c.Ui.Output(c.Help()) }
flags.BoolVar(&short, "short", false, "")
flags.BoolVar(&verbose, "verbose", false, "")
flags.BoolVar(&list_allocs, "allocs", false, "")
if err := flags.Parse(args); err != nil {
return 1
@@ -90,15 +95,35 @@ func (c *NodeStatusCommand) Run(args []string) int {
// Format the nodes list
out := make([]string, len(nodes)+1)
out[0] = "ID|Datacenter|Name|Class|Drain|Status"
if list_allocs {
out[0] = "ID|DC|Name|Class|Drain|Status|Running Allocs"
} else {
out[0] = "ID|DC|Name|Class|Drain|Status"
}
for i, node := range nodes {
out[i+1] = fmt.Sprintf("%s|%s|%s|%s|%v|%s",
limit(node.ID, length),
node.Datacenter,
node.Name,
node.NodeClass,
node.Drain,
node.Status)
if list_allocs {
numAllocs, err := getRunningAllocs(client, node)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error querying node allocations: %s", err))
return 1
}
out[i+1] = fmt.Sprintf("%s|%s|%s|%s|%v|%s|%v",
limit(node.ID, length),
node.Datacenter,
node.Name,
node.NodeClass,
node.Drain,
node.Status,
numAllocs)
} else {
out[i+1] = fmt.Sprintf("%s|%s|%s|%s|%v|%s",
limit(node.ID, length),
node.Datacenter,
node.Name,
node.NodeClass,
node.Drain,
node.Status)
}
}
// Dump the output
@@ -135,7 +160,7 @@ func (c *NodeStatusCommand) Run(args []string) int {
// Format the nodes list that matches the prefix so that the user
// can create a more specific request
out := make([]string, len(nodes)+1)
out[0] = "ID|Datacenter|Name|Class|Drain|Status"
out[0] = "ID|DC|Name|Class|Drain|Status"
for i, node := range nodes {
out[i+1] = fmt.Sprintf("%s|%s|%s|%s|%v|%s",
limit(node.ID, length),
@@ -176,7 +201,7 @@ func (c *NodeStatusCommand) Run(args []string) int {
fmt.Sprintf("ID|%s", limit(node.ID, length)),
fmt.Sprintf("Name|%s", node.Name),
fmt.Sprintf("Class|%s", node.NodeClass),
fmt.Sprintf("Datacenter|%s", node.Datacenter),
fmt.Sprintf("DC|%s", node.Datacenter),
fmt.Sprintf("Drain|%v", node.Drain),
fmt.Sprintf("Status|%s", node.Status),
fmt.Sprintf("Attributes|%s", strings.Join(attributes, ", ")),
@@ -213,3 +238,15 @@ func (c *NodeStatusCommand) Run(args []string) int {
}
return 0
}
func getRunningAllocs(client *api.Client, node *api.NodeListStub) (int, error) {
// Fetch number of running allocations per node
numAllocs := 0
nodeAllocs, _, err := client.Nodes().Allocations(node.ID, nil)
for _, alloc := range nodeAllocs {
if alloc.ClientStatus == "running" {
numAllocs += 1
}
}
return numAllocs, err
}

View File

@@ -35,6 +35,7 @@ Otherwise, a list of matching nodes and information will be displayed.
* `-short`: Display short output. Used only when querying a single node. Drops
verbose information about node allocations.
* `-verbose`: Show full information.
* `-allocs`: Show running allocations per node
## Examples
@@ -47,6 +48,15 @@ a72dfba2 dc1 node1 false ready
1f3f03ea dc1 node2 false ready
```
List view, with running allocations:
```
$ nomad node-status -allocs
ID DC Name Class Drain Status Running Allocs
4d2ba53b dc1 node1 <none> false ready 1
34dfba32 dc1 node2 <none> false ready 3
```
Single-node view in short mode:
```