2023-04-10 15:36:59 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2015-09-11 17:50:39 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-12-12 12:39:16 +00:00
|
|
|
"sort"
|
2015-09-11 17:50:39 +00:00
|
|
|
"strings"
|
2018-05-11 19:19:16 +00:00
|
|
|
|
|
|
|
"github.com/posener/complete"
|
2015-09-11 17:50:39 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type AgentInfoCommand struct {
|
2015-09-14 20:13:52 +00:00
|
|
|
Meta
|
2015-09-11 17:50:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *AgentInfoCommand) Help() string {
|
|
|
|
helpText := `
|
|
|
|
Usage: nomad agent-info [options]
|
|
|
|
|
2015-09-13 18:39:49 +00:00
|
|
|
Display status information about the local agent.
|
2015-09-11 17:50:39 +00:00
|
|
|
|
2020-11-19 21:38:08 +00:00
|
|
|
When ACLs are enabled, this command requires a token with the 'agent:read'
|
|
|
|
capability.
|
|
|
|
|
2015-09-14 20:13:52 +00:00
|
|
|
General Options:
|
2015-09-11 17:50:39 +00:00
|
|
|
|
2021-01-13 14:42:46 +00:00
|
|
|
` + generalOptionsUsage(usageOptsDefault|usageOptsNoNamespace) + `
|
|
|
|
|
|
|
|
Agent Info Options:
|
|
|
|
|
|
|
|
-json
|
|
|
|
Output the node in its JSON format.
|
|
|
|
|
2021-06-11 19:39:28 +00:00
|
|
|
-t
|
2021-01-13 14:42:46 +00:00
|
|
|
Format and display node using a Go template.
|
|
|
|
`
|
2015-09-11 17:50:39 +00:00
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *AgentInfoCommand) Synopsis() string {
|
2015-09-14 20:13:52 +00:00
|
|
|
return "Display status information about the local agent"
|
2015-09-11 17:50:39 +00:00
|
|
|
}
|
|
|
|
|
2018-05-11 19:19:16 +00:00
|
|
|
func (c *AgentInfoCommand) AutocompleteFlags() complete.Flags {
|
2021-01-13 14:42:46 +00:00
|
|
|
return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient),
|
|
|
|
complete.Flags{
|
|
|
|
"-json": complete.PredictNothing,
|
|
|
|
"-t": complete.PredictAnything,
|
|
|
|
})
|
2018-05-11 19:19:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *AgentInfoCommand) AutocompleteArgs() complete.Predictor {
|
|
|
|
return complete.PredictNothing
|
|
|
|
}
|
|
|
|
|
2018-04-18 16:02:11 +00:00
|
|
|
func (c *AgentInfoCommand) Name() string { return "agent-info" }
|
|
|
|
|
2015-09-11 17:50:39 +00:00
|
|
|
func (c *AgentInfoCommand) Run(args []string) int {
|
2021-01-13 14:42:46 +00:00
|
|
|
var json bool
|
|
|
|
var tmpl string
|
|
|
|
|
2018-04-18 16:02:11 +00:00
|
|
|
flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
|
2015-09-11 17:50:39 +00:00
|
|
|
flags.Usage = func() { c.Ui.Output(c.Help()) }
|
2021-01-13 14:42:46 +00:00
|
|
|
flags.BoolVar(&json, "json", false, "")
|
|
|
|
flags.StringVar(&tmpl, "t", "", "")
|
|
|
|
|
2015-09-11 17:50:39 +00:00
|
|
|
if err := flags.Parse(args); err != nil {
|
2021-01-13 14:42:46 +00:00
|
|
|
c.Ui.Error(fmt.Sprintf("Error parsing flags: %s", err))
|
2015-09-11 17:50:39 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2018-04-18 16:02:11 +00:00
|
|
|
// Check that we got no arguments
|
2015-09-14 20:13:52 +00:00
|
|
|
args = flags.Args()
|
|
|
|
if len(args) > 0 {
|
2018-04-18 16:02:11 +00:00
|
|
|
c.Ui.Error("This command takes no arguments")
|
|
|
|
c.Ui.Error(commandErrorText(c))
|
2015-09-11 17:50:39 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the HTTP client
|
2015-09-14 20:13:52 +00:00
|
|
|
client, err := c.Meta.Client()
|
2015-09-11 17:50:39 +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-11 17:50:39 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Query the agent info
|
|
|
|
info, err := client.Agent().Self()
|
|
|
|
if err != nil {
|
2015-09-14 20:13:52 +00:00
|
|
|
c.Ui.Error(fmt.Sprintf("Error querying agent info: %s", err))
|
2015-09-11 17:50:39 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2021-01-13 14:42:46 +00:00
|
|
|
// If output format is specified, format and output the agent info
|
|
|
|
if json || len(tmpl) > 0 {
|
|
|
|
out, err := Format(json, tmpl, info)
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error formatting output: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Ui.Output(out)
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2015-12-12 12:39:16 +00:00
|
|
|
// Sort and output agent info
|
2017-03-08 14:50:54 +00:00
|
|
|
statsKeys := make([]string, 0, len(info.Stats))
|
|
|
|
for key := range info.Stats {
|
2015-12-17 22:53:33 +00:00
|
|
|
statsKeys = append(statsKeys, key)
|
2015-12-12 12:39:16 +00:00
|
|
|
}
|
2015-12-17 22:53:33 +00:00
|
|
|
sort.Strings(statsKeys)
|
2015-12-12 12:39:16 +00:00
|
|
|
|
2015-12-17 22:53:33 +00:00
|
|
|
for _, key := range statsKeys {
|
2015-12-12 12:39:16 +00:00
|
|
|
c.Ui.Output(key)
|
2020-12-09 19:05:18 +00:00
|
|
|
statsData := info.Stats[key]
|
2015-12-21 23:10:49 +00:00
|
|
|
statsDataKeys := make([]string, len(statsData))
|
|
|
|
i := 0
|
2015-12-17 22:53:33 +00:00
|
|
|
for key := range statsData {
|
2015-12-21 23:10:49 +00:00
|
|
|
statsDataKeys[i] = key
|
|
|
|
i++
|
2015-12-12 12:39:16 +00:00
|
|
|
}
|
2015-12-17 22:53:33 +00:00
|
|
|
sort.Strings(statsDataKeys)
|
2015-09-11 17:50:39 +00:00
|
|
|
|
2015-12-17 22:53:33 +00:00
|
|
|
for _, key := range statsDataKeys {
|
|
|
|
c.Ui.Output(fmt.Sprintf(" %s = %v", key, statsData[key]))
|
2015-09-11 17:50:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|