2015-09-12 20:55:51 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-10-21 01:00:00 +00:00
|
|
|
"sort"
|
2015-09-12 20:55:51 +00:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type NodeStatusCommand struct {
|
2015-09-14 20:13:52 +00:00
|
|
|
Meta
|
2015-09-12 20:55:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *NodeStatusCommand) Help() string {
|
|
|
|
helpText := `
|
2016-01-27 20:00:31 +00:00
|
|
|
Usage: nomad node-status [options] <node>
|
2015-09-12 20:55:51 +00:00
|
|
|
|
2015-09-13 18:39:49 +00:00
|
|
|
Display status information about a given node. The list of nodes
|
|
|
|
returned includes only nodes which jobs may be scheduled to, and
|
2015-09-12 20:55:51 +00:00
|
|
|
includes status and other high-level information.
|
|
|
|
|
|
|
|
If a node ID is passed, information for that specific node will
|
|
|
|
be displayed. If no node ID's are passed, then a short-hand
|
|
|
|
list of all nodes will be displayed.
|
|
|
|
|
2015-09-14 20:13:52 +00:00
|
|
|
General Options:
|
2015-09-12 20:55:51 +00:00
|
|
|
|
2015-09-15 18:20:08 +00:00
|
|
|
` + generalOptionsUsage() + `
|
|
|
|
|
|
|
|
Node Status Options:
|
|
|
|
|
|
|
|
-short
|
|
|
|
Display short output. Used only when a single node is being
|
|
|
|
queried, and drops verbose output about node allocations.
|
2016-01-14 20:57:43 +00:00
|
|
|
|
2016-01-15 22:32:38 +00:00
|
|
|
-verbose
|
|
|
|
Display full information.
|
2015-09-15 18:20:08 +00:00
|
|
|
`
|
2015-09-12 20:55:51 +00:00
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *NodeStatusCommand) Synopsis() string {
|
2015-09-13 18:39:49 +00:00
|
|
|
return "Display status information about nodes"
|
2015-09-12 20:55:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *NodeStatusCommand) Run(args []string) int {
|
2016-01-15 22:32:38 +00:00
|
|
|
var short, verbose bool
|
2015-09-15 18:20:08 +00:00
|
|
|
|
2015-09-14 20:13:52 +00:00
|
|
|
flags := c.Meta.FlagSet("node-status", FlagSetClient)
|
2015-09-12 20:55:51 +00:00
|
|
|
flags.Usage = func() { c.Ui.Output(c.Help()) }
|
2015-09-15 18:20:08 +00:00
|
|
|
flags.BoolVar(&short, "short", false, "")
|
2016-01-15 22:32:38 +00:00
|
|
|
flags.BoolVar(&verbose, "verbose", false, "")
|
2015-09-15 18:20:08 +00:00
|
|
|
|
2015-09-12 20:55:51 +00:00
|
|
|
if err := flags.Parse(args); err != nil {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that we got either a single node or none
|
2015-09-14 20:13:52 +00:00
|
|
|
args = flags.Args()
|
|
|
|
if len(args) > 1 {
|
2015-09-12 20:55:51 +00:00
|
|
|
c.Ui.Error(c.Help())
|
|
|
|
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-12 20:55:51 +00:00
|
|
|
// Get the HTTP client
|
2015-09-14 20:13:52 +00:00
|
|
|
client, err := c.Meta.Client()
|
2015-09-12 20:55:51 +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-12 20:55:51 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use list mode if no node name was provided
|
2015-09-14 20:13:52 +00:00
|
|
|
if len(args) == 0 {
|
2015-09-12 20:55:51 +00:00
|
|
|
// Query the node info
|
|
|
|
nodes, _, err := client.Nodes().List(nil)
|
|
|
|
if err != nil {
|
2015-09-14 20:13:52 +00:00
|
|
|
c.Ui.Error(fmt.Sprintf("Error querying node status: %s", err))
|
2015-09-12 20:55:51 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return nothing if no nodes found
|
|
|
|
if len(nodes) == 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// Format the nodes list
|
|
|
|
out := make([]string, len(nodes)+1)
|
2016-01-21 20:35:56 +00:00
|
|
|
out[0] = "ID|Datacenter|Name|Class|Drain|Status"
|
2015-09-12 20:55:51 +00:00
|
|
|
for i, node := range nodes {
|
|
|
|
out[i+1] = fmt.Sprintf("%s|%s|%s|%s|%v|%s",
|
2016-01-27 18:42:10 +00:00
|
|
|
limit(node.ID, length),
|
2015-09-12 20:55:51 +00:00
|
|
|
node.Datacenter,
|
|
|
|
node.Name,
|
|
|
|
node.NodeClass,
|
|
|
|
node.Drain,
|
|
|
|
node.Status)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dump the output
|
2015-09-15 23:44:38 +00:00
|
|
|
c.Ui.Output(formatList(out))
|
2015-09-12 20:55:51 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// Query the specific node
|
2015-09-14 20:13:52 +00:00
|
|
|
nodeID := args[0]
|
2015-09-12 20:55:51 +00:00
|
|
|
node, _, err := client.Nodes().Info(nodeID, nil)
|
|
|
|
if err != nil {
|
2016-01-21 21:21:35 +00:00
|
|
|
if len(nodeID) == 1 {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Identifier must contain at least two characters."))
|
2016-01-21 19:53:05 +00:00
|
|
|
return 1
|
|
|
|
}
|
2016-01-21 21:21:35 +00:00
|
|
|
if len(nodeID)%2 == 1 {
|
|
|
|
// Identifiers must be of even length, so we strip off the last byte
|
|
|
|
// to provide a consistent user experience.
|
|
|
|
nodeID = nodeID[:len(nodeID)-1]
|
|
|
|
}
|
2016-01-21 19:53:05 +00:00
|
|
|
|
2015-12-22 22:44:33 +00:00
|
|
|
// Exact lookup failed, try with prefix based search
|
2015-12-24 10:46:59 +00:00
|
|
|
nodes, _, err := client.Nodes().PrefixList(nodeID)
|
2015-12-22 22:44:33 +00:00
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error querying node info: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
// Return error if no nodes are found
|
|
|
|
if len(nodes) == 0 {
|
2015-12-24 10:46:59 +00:00
|
|
|
c.Ui.Error(fmt.Sprintf("No node(s) with prefix %q found", nodeID))
|
2015-12-22 22:44:33 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
if len(nodes) > 1 {
|
|
|
|
// Format the nodes list that matches the prefix so that the user
|
|
|
|
// can create a more specific request
|
|
|
|
out := make([]string, len(nodes)+1)
|
2016-01-21 20:35:56 +00:00
|
|
|
out[0] = "ID|Datacenter|Name|Class|Drain|Status"
|
2015-12-22 22:44:33 +00:00
|
|
|
for i, node := range nodes {
|
|
|
|
out[i+1] = fmt.Sprintf("%s|%s|%s|%s|%v|%s",
|
2016-01-27 18:42:10 +00:00
|
|
|
limit(node.ID, length),
|
2015-12-22 22:44:33 +00:00
|
|
|
node.Datacenter,
|
|
|
|
node.Name,
|
|
|
|
node.NodeClass,
|
|
|
|
node.Drain,
|
|
|
|
node.Status)
|
|
|
|
}
|
|
|
|
// Dump the output
|
2016-01-06 23:08:47 +00:00
|
|
|
c.Ui.Output(fmt.Sprintf("Prefix matched multiple nodes\n\n%s", formatList(out)))
|
2015-12-22 22:44:33 +00:00
|
|
|
return 0
|
|
|
|
}
|
2016-01-06 21:46:57 +00:00
|
|
|
// Prefix lookup matched a single node
|
2015-12-22 22:44:33 +00:00
|
|
|
node, _, err = client.Nodes().Info(nodes[0].ID, nil)
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error querying node info: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
2015-09-12 20:55:51 +00:00
|
|
|
}
|
|
|
|
|
2015-10-21 01:00:00 +00:00
|
|
|
m := node.Attributes
|
|
|
|
keys := make([]string, len(m))
|
|
|
|
for k := range m {
|
|
|
|
keys = append(keys, k)
|
|
|
|
}
|
|
|
|
sort.Strings(keys)
|
|
|
|
|
|
|
|
var attributes []string
|
|
|
|
for _, k := range keys {
|
|
|
|
if k != "" {
|
|
|
|
attributes = append(attributes, fmt.Sprintf("%s:%s", k, m[k]))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-12 20:55:51 +00:00
|
|
|
// Format the output
|
2015-09-15 18:20:08 +00:00
|
|
|
basic := []string{
|
2016-01-27 18:42:10 +00:00
|
|
|
fmt.Sprintf("ID|%s", limit(node.ID, length)),
|
2015-09-15 23:51:17 +00:00
|
|
|
fmt.Sprintf("Name|%s", node.Name),
|
|
|
|
fmt.Sprintf("Class|%s", node.NodeClass),
|
|
|
|
fmt.Sprintf("Datacenter|%s", node.Datacenter),
|
|
|
|
fmt.Sprintf("Drain|%v", node.Drain),
|
|
|
|
fmt.Sprintf("Status|%s", node.Status),
|
2015-10-21 01:00:00 +00:00
|
|
|
fmt.Sprintf("Attributes|%s", strings.Join(attributes, ", ")),
|
2015-09-12 20:55:51 +00:00
|
|
|
}
|
|
|
|
|
2015-09-15 18:20:08 +00:00
|
|
|
var allocs []string
|
|
|
|
if !short {
|
|
|
|
// Query the node allocations
|
2015-12-11 13:18:44 +00:00
|
|
|
nodeAllocs, _, err := client.Nodes().Allocations(node.ID, nil)
|
2015-09-15 18:20:08 +00:00
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error querying node allocations: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Format the allocations
|
|
|
|
allocs = make([]string, len(nodeAllocs)+1)
|
2016-01-21 20:35:56 +00:00
|
|
|
allocs[0] = "ID|Eval ID|Job ID|Task Group|Desired Status|Client Status"
|
2015-09-15 18:20:08 +00:00
|
|
|
for i, alloc := range nodeAllocs {
|
|
|
|
allocs[i+1] = fmt.Sprintf("%s|%s|%s|%s|%s|%s",
|
2016-01-27 18:42:10 +00:00
|
|
|
limit(alloc.ID, length),
|
|
|
|
limit(alloc.EvalID, length),
|
2015-09-15 18:20:08 +00:00
|
|
|
alloc.JobID,
|
|
|
|
alloc.TaskGroup,
|
|
|
|
alloc.DesiredStatus,
|
|
|
|
alloc.ClientStatus)
|
|
|
|
}
|
|
|
|
}
|
2015-09-15 03:29:08 +00:00
|
|
|
|
2015-09-12 20:55:51 +00:00
|
|
|
// Dump the output
|
2015-09-15 23:44:38 +00:00
|
|
|
c.Ui.Output(formatKV(basic))
|
2015-09-15 18:20:08 +00:00
|
|
|
if !short {
|
2016-01-21 21:13:36 +00:00
|
|
|
c.Ui.Output("\n==> Allocations")
|
2015-09-15 23:44:38 +00:00
|
|
|
c.Ui.Output(formatList(allocs))
|
2015-09-15 18:20:08 +00:00
|
|
|
}
|
2015-09-12 20:55:51 +00:00
|
|
|
return 0
|
|
|
|
}
|