2023-04-10 15:36:59 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2015-09-11 19:58:16 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-11-28 18:38:54 +00:00
|
|
|
"net"
|
2015-10-21 12:35:34 +00:00
|
|
|
"sort"
|
2015-09-11 19:58:16 +00:00
|
|
|
"strings"
|
|
|
|
|
2018-03-30 17:55:25 +00:00
|
|
|
multierror "github.com/hashicorp/go-multierror"
|
2015-09-11 19:58:16 +00:00
|
|
|
"github.com/hashicorp/nomad/api"
|
2017-08-18 15:09:52 +00:00
|
|
|
"github.com/posener/complete"
|
2015-09-11 19:58:16 +00:00
|
|
|
"github.com/ryanuber/columnize"
|
|
|
|
)
|
|
|
|
|
2015-09-22 18:01:41 +00:00
|
|
|
type ServerMembersCommand struct {
|
2015-09-14 20:13:52 +00:00
|
|
|
Meta
|
2015-09-11 19:58:16 +00:00
|
|
|
}
|
|
|
|
|
2015-09-22 18:01:41 +00:00
|
|
|
func (c *ServerMembersCommand) Help() string {
|
2015-09-11 19:58:16 +00:00
|
|
|
helpText := `
|
2018-03-21 00:37:28 +00:00
|
|
|
Usage: nomad server members [options]
|
2015-09-11 19:58:16 +00:00
|
|
|
|
2016-09-27 20:51:54 +00:00
|
|
|
Display a list of the known servers and their status. Only Nomad servers are
|
|
|
|
able to service this command.
|
2015-09-11 19:58:16 +00:00
|
|
|
|
2020-11-19 21:38:08 +00:00
|
|
|
If ACLs are enabled, this option requires a token with the 'node:read'
|
|
|
|
capability.
|
|
|
|
|
2015-09-14 20:13:52 +00:00
|
|
|
General Options:
|
|
|
|
|
2020-11-19 16:15:23 +00:00
|
|
|
` + generalOptionsUsage(usageOptsDefault|usageOptsNoNamespace) + `
|
2015-09-14 20:13:52 +00:00
|
|
|
|
2016-05-25 21:11:14 +00:00
|
|
|
Server Members Options:
|
2015-09-11 19:58:16 +00:00
|
|
|
|
2022-03-17 18:15:10 +00:00
|
|
|
-verbose
|
|
|
|
Show detailed information about each member. This dumps a raw set of tags
|
|
|
|
which shows more information than the default output format.
|
2023-03-20 09:39:24 +00:00
|
|
|
|
|
|
|
-json
|
|
|
|
Output the latest information about each member in a JSON format.
|
|
|
|
|
|
|
|
-t
|
|
|
|
Format and display latest information about each member using a Go template.
|
2015-09-11 19:58:16 +00:00
|
|
|
`
|
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|
|
|
|
|
2017-08-18 15:09:52 +00:00
|
|
|
func (c *ServerMembersCommand) AutocompleteFlags() complete.Flags {
|
2017-08-23 21:56:21 +00:00
|
|
|
return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient),
|
|
|
|
complete.Flags{
|
|
|
|
"-detailed": complete.PredictNothing,
|
2023-03-20 09:39:24 +00:00
|
|
|
"-verbose": complete.PredictNothing,
|
|
|
|
"-json": complete.PredictNothing,
|
|
|
|
"-t": complete.PredictAnything,
|
2017-08-23 21:56:21 +00:00
|
|
|
})
|
2017-08-18 15:09:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ServerMembersCommand) AutocompleteArgs() complete.Predictor {
|
|
|
|
return complete.PredictNothing
|
|
|
|
}
|
|
|
|
|
2015-09-22 18:01:41 +00:00
|
|
|
func (c *ServerMembersCommand) Synopsis() string {
|
|
|
|
return "Display a list of known servers and their status"
|
2015-09-11 19:58:16 +00:00
|
|
|
}
|
|
|
|
|
2018-04-18 16:02:11 +00:00
|
|
|
func (c *ServerMembersCommand) Name() string { return "server members" }
|
|
|
|
|
2015-09-22 18:01:41 +00:00
|
|
|
func (c *ServerMembersCommand) Run(args []string) int {
|
2023-03-20 09:39:24 +00:00
|
|
|
var detailed, verbose, json bool
|
|
|
|
var tmpl string
|
2015-09-11 19:58:16 +00:00
|
|
|
|
2018-04-18 16:02:11 +00:00
|
|
|
flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
|
2015-09-11 19:58:16 +00:00
|
|
|
flags.Usage = func() { c.Ui.Output(c.Help()) }
|
|
|
|
flags.BoolVar(&detailed, "detailed", false, "Show detailed output")
|
2022-03-17 18:15:10 +00:00
|
|
|
flags.BoolVar(&verbose, "verbose", false, "Show detailed output")
|
2023-03-20 09:39:24 +00:00
|
|
|
flags.BoolVar(&json, "json", false, "")
|
|
|
|
flags.StringVar(&tmpl, "t", "", "")
|
2015-09-11 19:58:16 +00:00
|
|
|
|
|
|
|
if err := flags.Parse(args); err != nil {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for extra 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 19:58:16 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2022-03-17 18:15:10 +00:00
|
|
|
// Keep support for previous flag name
|
|
|
|
if detailed {
|
|
|
|
verbose = true
|
|
|
|
}
|
|
|
|
|
2015-09-11 19:58:16 +00:00
|
|
|
// Get the HTTP client
|
2015-09-14 20:13:52 +00:00
|
|
|
client, err := c.Meta.Client()
|
2015-09-11 19:58:16 +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 19:58:16 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Query the members
|
2016-11-06 18:29:09 +00:00
|
|
|
srvMembers, err := client.Agent().Members()
|
2015-09-11 19:58:16 +00:00
|
|
|
if err != nil {
|
2015-09-22 18:01:41 +00:00
|
|
|
c.Ui.Error(fmt.Sprintf("Error querying servers: %s", err))
|
2015-09-11 19:58:16 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2016-11-06 18:29:09 +00:00
|
|
|
if srvMembers == nil {
|
|
|
|
c.Ui.Error("Agent doesn't know about server members")
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2015-10-21 12:35:34 +00:00
|
|
|
// Sort the members
|
2016-11-06 18:29:09 +00:00
|
|
|
sort.Sort(api.AgentMembersNameSort(srvMembers.Members))
|
2015-10-21 12:35:34 +00:00
|
|
|
|
2023-05-18 20:43:57 +00:00
|
|
|
// Determine the leaders per region.
|
|
|
|
leaders, leaderErr := regionLeaders(client, srvMembers.Members)
|
|
|
|
|
2023-03-20 09:39:24 +00:00
|
|
|
if json || len(tmpl) > 0 {
|
2023-05-18 20:43:57 +00:00
|
|
|
for _, member := range srvMembers.Members {
|
|
|
|
member.Tags["Leader"] = fmt.Sprintf("%t", isLeader(member, leaders))
|
|
|
|
}
|
2023-03-20 09:39:24 +00:00
|
|
|
out, err := Format(json, tmpl, srvMembers.Members)
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(err.Error())
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Ui.Output(out)
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2015-09-11 19:58:16 +00:00
|
|
|
// Format the list
|
|
|
|
var out []string
|
2022-03-17 18:15:10 +00:00
|
|
|
if verbose {
|
|
|
|
out = verboseOutput(srvMembers.Members, leaders)
|
2015-09-11 19:58:16 +00:00
|
|
|
} else {
|
2016-11-06 18:29:09 +00:00
|
|
|
out = standardOutput(srvMembers.Members, leaders)
|
2015-09-11 19:58:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Dump the list
|
|
|
|
c.Ui.Output(columnize.SimpleFormat(out))
|
2018-03-30 17:55:25 +00:00
|
|
|
|
|
|
|
// If there were leader errors display a warning
|
|
|
|
if leaderErr != nil {
|
|
|
|
c.Ui.Output("")
|
|
|
|
c.Ui.Warn(fmt.Sprintf("Error determining leaders: %s", leaderErr))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2015-09-11 19:58:16 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2016-03-17 23:04:09 +00:00
|
|
|
func standardOutput(mem []*api.AgentMember, leaders map[string]string) []string {
|
2015-09-11 19:58:16 +00:00
|
|
|
// Format the members list
|
|
|
|
members := make([]string, len(mem)+1)
|
2022-03-17 18:15:10 +00:00
|
|
|
members[0] = "Name|Address|Port|Status|Leader|Raft Version|Build|Datacenter|Region"
|
2015-09-11 19:58:16 +00:00
|
|
|
for i, member := range mem {
|
2022-03-17 18:15:10 +00:00
|
|
|
members[i+1] = fmt.Sprintf("%s|%s|%d|%s|%t|%s|%s|%s|%s",
|
2015-09-11 19:58:16 +00:00
|
|
|
member.Name,
|
|
|
|
member.Addr,
|
|
|
|
member.Port,
|
|
|
|
member.Status,
|
2022-03-17 18:15:10 +00:00
|
|
|
isLeader(member, leaders),
|
|
|
|
member.Tags["raft_vsn"],
|
2015-09-11 19:58:16 +00:00
|
|
|
member.Tags["build"],
|
|
|
|
member.Tags["dc"],
|
2015-09-13 18:39:49 +00:00
|
|
|
member.Tags["region"])
|
2015-09-11 19:58:16 +00:00
|
|
|
}
|
|
|
|
return members
|
|
|
|
}
|
|
|
|
|
2022-03-17 18:15:10 +00:00
|
|
|
func verboseOutput(mem []*api.AgentMember, leaders map[string]string) []string {
|
2015-09-11 19:58:16 +00:00
|
|
|
// Format the members list
|
|
|
|
members := make([]string, len(mem)+1)
|
2022-03-17 18:15:10 +00:00
|
|
|
members[0] = "Name|Address|Port|Status|Leader|Protocol|Raft Version|Build|Datacenter|Region|Tags"
|
2015-09-11 19:58:16 +00:00
|
|
|
for i, member := range mem {
|
|
|
|
// Format the tags
|
|
|
|
tagPairs := make([]string, 0, len(member.Tags))
|
|
|
|
for k, v := range member.Tags {
|
|
|
|
tagPairs = append(tagPairs, fmt.Sprintf("%s=%s", k, v))
|
|
|
|
}
|
|
|
|
tags := strings.Join(tagPairs, ",")
|
|
|
|
|
2022-03-17 18:15:10 +00:00
|
|
|
members[i+1] = fmt.Sprintf("%s|%s|%d|%s|%t|%d|%s|%s|%s|%s|%s",
|
2015-09-11 19:58:16 +00:00
|
|
|
member.Name,
|
|
|
|
member.Addr,
|
|
|
|
member.Port,
|
2022-03-17 18:15:10 +00:00
|
|
|
member.Status,
|
|
|
|
isLeader(member, leaders),
|
|
|
|
member.ProtocolCur,
|
|
|
|
member.Tags["raft_vsn"],
|
|
|
|
member.Tags["build"],
|
|
|
|
member.Tags["dc"],
|
|
|
|
member.Tags["region"],
|
|
|
|
tags,
|
|
|
|
)
|
2015-09-11 19:58:16 +00:00
|
|
|
}
|
|
|
|
return members
|
|
|
|
}
|
2016-03-17 23:04:09 +00:00
|
|
|
|
|
|
|
// regionLeaders returns a map of regions to the IP of the member that is the
|
|
|
|
// leader.
|
|
|
|
func regionLeaders(client *api.Client, mem []*api.AgentMember) (map[string]string, error) {
|
|
|
|
// Determine the unique regions.
|
|
|
|
leaders := make(map[string]string)
|
|
|
|
regions := make(map[string]struct{})
|
|
|
|
for _, m := range mem {
|
2018-03-05 22:56:36 +00:00
|
|
|
// Ignore left members
|
|
|
|
// This prevents querying for leader status on regions where all members have left
|
|
|
|
if m.Status == "left" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-03-17 23:04:09 +00:00
|
|
|
regions[m.Tags["region"]] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(regions) == 0 {
|
|
|
|
return leaders, nil
|
|
|
|
}
|
|
|
|
|
2018-03-30 17:55:25 +00:00
|
|
|
var mErr multierror.Error
|
2016-03-17 23:04:09 +00:00
|
|
|
status := client.Status()
|
|
|
|
for reg := range regions {
|
|
|
|
l, err := status.RegionLeader(reg)
|
|
|
|
if err != nil {
|
2021-01-14 20:46:35 +00:00
|
|
|
_ = multierror.Append(&mErr, fmt.Errorf("Region %q: %v", reg, err))
|
2018-03-30 17:55:25 +00:00
|
|
|
continue
|
2016-03-17 23:04:09 +00:00
|
|
|
}
|
2016-04-22 22:59:56 +00:00
|
|
|
|
2016-03-17 23:04:09 +00:00
|
|
|
leaders[reg] = l
|
|
|
|
}
|
|
|
|
|
2018-03-30 17:55:25 +00:00
|
|
|
return leaders, mErr.ErrorOrNil()
|
2016-03-17 23:04:09 +00:00
|
|
|
}
|
2022-03-17 18:15:10 +00:00
|
|
|
|
|
|
|
func isLeader(member *api.AgentMember, leaders map[string]string) bool {
|
|
|
|
addr := net.JoinHostPort(member.Addr, member.Tags["port"])
|
|
|
|
reg := member.Tags["region"]
|
|
|
|
regLeader, ok := leaders[reg]
|
|
|
|
return ok && regLeader == addr
|
|
|
|
}
|