2015-09-12 20:55:51 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/ryanuber/columnize"
|
|
|
|
)
|
|
|
|
|
|
|
|
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 := `
|
|
|
|
Usage: nomad node-status [options] [node]
|
|
|
|
|
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-14 20:13:52 +00:00
|
|
|
` + generalOptionsUsage()
|
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 {
|
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()) }
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
out[0] = "ID|DC|Name|Class|Drain|Status"
|
|
|
|
for i, node := range nodes {
|
|
|
|
out[i+1] = fmt.Sprintf("%s|%s|%s|%s|%v|%s",
|
|
|
|
node.ID,
|
|
|
|
node.Datacenter,
|
|
|
|
node.Name,
|
|
|
|
node.NodeClass,
|
|
|
|
node.Drain,
|
|
|
|
node.Status)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dump the output
|
|
|
|
c.Ui.Output(columnize.SimpleFormat(out))
|
|
|
|
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 {
|
2015-09-14 20:13:52 +00:00
|
|
|
c.Ui.Error(fmt.Sprintf("Error querying node info: %s", err))
|
2015-09-12 20:55:51 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Format the output
|
|
|
|
out := []string{
|
|
|
|
fmt.Sprintf("ID | %s", node.ID),
|
|
|
|
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-09-15 03:29:08 +00:00
|
|
|
// Make the column config so we can dump k = v pairs
|
|
|
|
columnConf := columnize.DefaultConfig()
|
|
|
|
columnConf.Glue = " = "
|
|
|
|
|
2015-09-12 20:55:51 +00:00
|
|
|
// Dump the output
|
2015-09-15 03:29:08 +00:00
|
|
|
c.Ui.Output(columnize.Format(out, columnConf))
|
2015-09-12 20:55:51 +00:00
|
|
|
return 0
|
|
|
|
}
|