2023-03-28 19:12:30 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2017-10-12 22:15:13 +00:00
|
|
|
package members
|
2013-12-31 21:06:33 +00:00
|
|
|
|
|
|
|
import (
|
2017-10-12 22:18:15 +00:00
|
|
|
"flag"
|
2013-12-31 21:06:33 +00:00
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"regexp"
|
2015-05-22 08:27:47 +00:00
|
|
|
"sort"
|
2013-12-31 21:06:33 +00:00
|
|
|
"strings"
|
2017-02-09 22:12:47 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/serf/serf"
|
2017-10-12 22:15:13 +00:00
|
|
|
"github.com/mitchellh/cli"
|
2017-02-09 22:12:47 +00:00
|
|
|
"github.com/ryanuber/columnize"
|
2021-10-28 21:44:38 +00:00
|
|
|
|
2022-04-05 21:10:06 +00:00
|
|
|
"github.com/hashicorp/consul/acl"
|
2021-10-28 21:44:38 +00:00
|
|
|
consulapi "github.com/hashicorp/consul/api"
|
|
|
|
"github.com/hashicorp/consul/command/flags"
|
2013-12-31 21:06:33 +00:00
|
|
|
)
|
|
|
|
|
2017-10-12 22:15:13 +00:00
|
|
|
// cmd is a Command implementation that queries a running
|
2013-12-31 21:06:33 +00:00
|
|
|
// Consul agent what members are part of the cluster currently.
|
2017-10-12 22:15:13 +00:00
|
|
|
type cmd struct {
|
|
|
|
UI cli.Ui
|
2017-10-17 13:44:20 +00:00
|
|
|
help string
|
2017-10-12 22:15:13 +00:00
|
|
|
flags *flag.FlagSet
|
|
|
|
http *flags.HTTPFlags
|
2017-10-18 00:38:26 +00:00
|
|
|
|
2017-10-11 12:51:18 +00:00
|
|
|
// flags
|
|
|
|
detailed bool
|
|
|
|
wan bool
|
|
|
|
statusFilter string
|
|
|
|
segment string
|
2023-07-25 18:57:08 +00:00
|
|
|
filter string
|
2013-12-31 21:06:33 +00:00
|
|
|
}
|
|
|
|
|
2017-10-12 22:15:13 +00:00
|
|
|
func New(ui cli.Ui) *cmd {
|
|
|
|
c := &cmd{UI: ui}
|
|
|
|
c.init()
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cmd) init() {
|
|
|
|
c.flags = flag.NewFlagSet("", flag.ContinueOnError)
|
|
|
|
c.flags.BoolVar(&c.detailed, "detailed", false,
|
2017-02-09 22:12:47 +00:00
|
|
|
"Provides detailed information about nodes.")
|
2017-10-12 22:15:13 +00:00
|
|
|
c.flags.BoolVar(&c.wan, "wan", false,
|
2017-02-09 22:12:47 +00:00
|
|
|
"If the agent is in server mode, this can be used to return the other "+
|
2017-02-10 18:02:37 +00:00
|
|
|
"peers in the WAN pool.")
|
2017-10-12 22:15:13 +00:00
|
|
|
c.flags.StringVar(&c.statusFilter, "status", ".*",
|
2017-02-09 22:12:47 +00:00
|
|
|
"If provided, output is filtered to only nodes matching the regular "+
|
2017-02-10 18:02:37 +00:00
|
|
|
"expression for status.")
|
2017-10-12 22:15:13 +00:00
|
|
|
c.flags.StringVar(&c.segment, "segment", consulapi.AllSegments,
|
2017-08-14 14:36:07 +00:00
|
|
|
"(Enterprise-only) If provided, output is filtered to only nodes in"+
|
|
|
|
"the given segment.")
|
2023-07-25 18:57:08 +00:00
|
|
|
c.flags.StringVar(&c.filter, "filter", "", "Filter to use with the request")
|
2017-10-18 00:38:26 +00:00
|
|
|
|
|
|
|
c.http = &flags.HTTPFlags{}
|
|
|
|
flags.Merge(c.flags, c.http.ClientFlags())
|
2021-10-28 21:44:38 +00:00
|
|
|
flags.Merge(c.flags, c.http.PartitionFlag())
|
2017-10-17 22:00:01 +00:00
|
|
|
c.help = flags.Usage(help, c.flags)
|
2017-10-11 12:51:18 +00:00
|
|
|
}
|
|
|
|
|
2017-10-12 22:15:13 +00:00
|
|
|
func (c *cmd) Run(args []string) int {
|
|
|
|
if err := c.flags.Parse(args); err != nil {
|
2013-12-31 21:06:33 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compile the regexp
|
2017-10-11 12:51:18 +00:00
|
|
|
statusRe, err := regexp.Compile(c.statusFilter)
|
2013-12-31 21:06:33 +00:00
|
|
|
if err != nil {
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Error(fmt.Sprintf("Failed to compile status regexp: %v", err))
|
2013-12-31 21:06:33 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2017-10-12 22:15:13 +00:00
|
|
|
client, err := c.http.APIClient()
|
2013-12-31 21:06:33 +00:00
|
|
|
if err != nil {
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Error(fmt.Sprintf("Error connecting to Consul agent: %s", err))
|
2013-12-31 21:06:33 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2017-09-05 18:46:44 +00:00
|
|
|
// Make the request.
|
|
|
|
opts := consulapi.MembersOpts{
|
2017-10-11 12:51:18 +00:00
|
|
|
Segment: c.segment,
|
|
|
|
WAN: c.wan,
|
2023-07-25 18:57:08 +00:00
|
|
|
Filter: c.filter,
|
2017-09-05 18:46:44 +00:00
|
|
|
}
|
|
|
|
members, err := client.Agent().MembersOpts(opts)
|
|
|
|
if err != nil {
|
|
|
|
c.UI.Error(fmt.Sprintf("Error retrieving members: %s", err))
|
|
|
|
return 1
|
2017-08-14 14:36:07 +00:00
|
|
|
}
|
|
|
|
|
2014-06-06 23:00:02 +00:00
|
|
|
// Filter the results
|
|
|
|
n := len(members)
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
member := members[i]
|
2021-10-28 22:27:31 +00:00
|
|
|
if member.Tags[consulapi.MemberTagKeyPartition] == "" {
|
|
|
|
member.Tags[consulapi.MemberTagKeyPartition] = "default"
|
2017-09-05 18:46:44 +00:00
|
|
|
}
|
2022-04-05 21:10:06 +00:00
|
|
|
if acl.IsDefaultPartition(member.Tags[consulapi.MemberTagKeyPartition]) {
|
2021-10-28 22:27:31 +00:00
|
|
|
if c.segment == consulapi.AllSegments && member.Tags[consulapi.MemberTagKeyRole] == consulapi.MemberTagValueRoleServer {
|
|
|
|
member.Tags[consulapi.MemberTagKeySegment] = "<all>"
|
|
|
|
} else if member.Tags[consulapi.MemberTagKeySegment] == "" {
|
|
|
|
member.Tags[consulapi.MemberTagKeySegment] = "<default>"
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
member.Tags[consulapi.MemberTagKeySegment] = ""
|
2017-08-14 14:36:07 +00:00
|
|
|
}
|
2017-02-09 22:12:47 +00:00
|
|
|
statusString := serf.MemberStatus(member.Status).String()
|
|
|
|
if !statusRe.MatchString(statusString) {
|
2014-06-06 23:00:02 +00:00
|
|
|
members[i], members[n-1] = members[n-1], members[i]
|
|
|
|
i--
|
|
|
|
n--
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
members = members[:n]
|
|
|
|
|
|
|
|
// No matching members
|
|
|
|
if len(members) == 0 {
|
|
|
|
return 2
|
|
|
|
}
|
|
|
|
|
2021-10-28 22:27:31 +00:00
|
|
|
sort.Sort(ByMemberNamePartitionAndSegment(members))
|
2015-05-22 08:37:54 +00:00
|
|
|
|
2014-06-06 23:00:02 +00:00
|
|
|
// Generate the output
|
|
|
|
var result []string
|
2017-10-11 12:51:18 +00:00
|
|
|
if c.detailed {
|
2014-06-06 23:00:02 +00:00
|
|
|
result = c.detailedOutput(members)
|
|
|
|
} else {
|
|
|
|
result = c.standardOutput(members)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate the columnized version
|
2020-03-09 10:24:56 +00:00
|
|
|
output := columnize.Format(result, &columnize.Config{Delim: string([]byte{0x1f})})
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Output(output)
|
2014-06-06 23:00:02 +00:00
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2021-10-28 22:27:31 +00:00
|
|
|
// ByMemberNamePartitionAndSegment sorts members by name with a stable sort.
|
|
|
|
//
|
|
|
|
// 1. servers go at the top
|
|
|
|
// 2. members of the default partition go next (including segments)
|
|
|
|
// 3. members of partitions follow
|
|
|
|
type ByMemberNamePartitionAndSegment []*consulapi.AgentMember
|
2017-08-14 14:36:07 +00:00
|
|
|
|
2021-10-28 22:27:31 +00:00
|
|
|
func (m ByMemberNamePartitionAndSegment) Len() int { return len(m) }
|
|
|
|
func (m ByMemberNamePartitionAndSegment) Swap(i, j int) { m[i], m[j] = m[j], m[i] }
|
|
|
|
func (m ByMemberNamePartitionAndSegment) Less(i, j int) bool {
|
|
|
|
tags_i := parseTags(m[i].Tags)
|
|
|
|
tags_j := parseTags(m[j].Tags)
|
|
|
|
|
|
|
|
// put role=consul first
|
|
|
|
switch {
|
|
|
|
case tags_i.role == consulapi.MemberTagValueRoleServer && tags_j.role != consulapi.MemberTagValueRoleServer:
|
|
|
|
return true
|
|
|
|
case tags_i.role != consulapi.MemberTagValueRoleServer && tags_j.role == consulapi.MemberTagValueRoleServer:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// then the default partitions
|
|
|
|
switch {
|
|
|
|
case isDefault(tags_i.partition) && !isDefault(tags_j.partition):
|
|
|
|
return true
|
|
|
|
case !isDefault(tags_i.partition) && isDefault(tags_j.partition):
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// then by segments within the default
|
2017-08-14 14:36:07 +00:00
|
|
|
switch {
|
2021-10-28 22:27:31 +00:00
|
|
|
case tags_i.segment < tags_j.segment:
|
2017-08-14 14:36:07 +00:00
|
|
|
return true
|
2021-10-28 22:27:31 +00:00
|
|
|
case tags_i.segment > tags_j.segment:
|
2017-08-14 14:36:07 +00:00
|
|
|
return false
|
|
|
|
}
|
2021-10-28 22:27:31 +00:00
|
|
|
|
|
|
|
// then by partitions
|
|
|
|
switch {
|
|
|
|
case tags_i.partition < tags_j.partition:
|
|
|
|
return true
|
|
|
|
case tags_i.partition > tags_j.partition:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// finally by name
|
|
|
|
return m[i].Name < m[j].Name
|
|
|
|
}
|
|
|
|
|
|
|
|
func isDefault(s string) bool {
|
|
|
|
// NOTE: we can't use structs.IsDefaultPartition since that discards the input
|
|
|
|
return s == "" || s == "default"
|
2017-08-14 14:36:07 +00:00
|
|
|
}
|
2015-05-22 08:37:54 +00:00
|
|
|
|
2014-06-06 23:00:02 +00:00
|
|
|
// standardOutput is used to dump the most useful information about nodes
|
|
|
|
// in a more human-friendly format
|
2017-10-12 22:15:13 +00:00
|
|
|
func (c *cmd) standardOutput(members []*consulapi.AgentMember) []string {
|
2014-03-06 03:52:31 +00:00
|
|
|
result := make([]string, 0, len(members))
|
2021-10-28 22:27:31 +00:00
|
|
|
header := "Node\x1fAddress\x1fStatus\x1fType\x1fBuild\x1fProtocol\x1fDC\x1fPartition\x1fSegment"
|
2014-06-06 23:00:02 +00:00
|
|
|
result = append(result, header)
|
2013-12-31 21:06:33 +00:00
|
|
|
for _, member := range members {
|
2021-10-28 22:27:31 +00:00
|
|
|
tags := parseTags(member.Tags)
|
|
|
|
|
2017-02-09 22:12:47 +00:00
|
|
|
addr := net.TCPAddr{IP: net.ParseIP(member.Addr), Port: int(member.Port)}
|
2014-06-06 23:00:02 +00:00
|
|
|
protocol := member.Tags["vsn"]
|
|
|
|
build := member.Tags["build"]
|
|
|
|
if build == "" {
|
|
|
|
build = "< 0.3"
|
|
|
|
} else if idx := strings.Index(build, ":"); idx != -1 {
|
|
|
|
build = build[:idx]
|
|
|
|
}
|
|
|
|
|
2017-02-09 22:12:47 +00:00
|
|
|
statusString := serf.MemberStatus(member.Status).String()
|
2021-10-28 22:27:31 +00:00
|
|
|
switch tags.role {
|
|
|
|
case consulapi.MemberTagValueRoleClient:
|
|
|
|
line := fmt.Sprintf("%s\x1f%s\x1f%s\x1fclient\x1f%s\x1f%s\x1f%s\x1f%s\x1f%s",
|
|
|
|
member.Name, addr.String(), statusString, build, protocol, tags.datacenter, tags.partition, tags.segment)
|
2014-06-06 23:00:02 +00:00
|
|
|
result = append(result, line)
|
2021-10-28 22:27:31 +00:00
|
|
|
|
|
|
|
case consulapi.MemberTagValueRoleServer:
|
|
|
|
line := fmt.Sprintf("%s\x1f%s\x1f%s\x1fserver\x1f%s\x1f%s\x1f%s\x1f%s\x1f%s",
|
|
|
|
member.Name, addr.String(), statusString, build, protocol, tags.datacenter, tags.partition, tags.segment)
|
2014-06-06 23:00:02 +00:00
|
|
|
result = append(result, line)
|
2021-10-28 22:27:31 +00:00
|
|
|
|
2014-06-06 23:00:02 +00:00
|
|
|
default:
|
2021-10-28 22:27:31 +00:00
|
|
|
line := fmt.Sprintf("%s\x1f%s\x1f%s\x1funknown\x1f\x1f\x1f\x1f\x1f",
|
2017-02-09 22:12:47 +00:00
|
|
|
member.Name, addr.String(), statusString)
|
2014-06-06 23:00:02 +00:00
|
|
|
result = append(result, line)
|
2013-12-31 21:06:33 +00:00
|
|
|
}
|
2014-06-06 23:00:02 +00:00
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
2013-12-31 21:06:33 +00:00
|
|
|
|
2021-10-28 22:27:31 +00:00
|
|
|
type decodedTags struct {
|
|
|
|
role string
|
|
|
|
segment string
|
|
|
|
partition string
|
|
|
|
datacenter string
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseTags(tags map[string]string) decodedTags {
|
|
|
|
return decodedTags{
|
|
|
|
role: tags[consulapi.MemberTagKeyRole],
|
|
|
|
segment: tags[consulapi.MemberTagKeySegment],
|
|
|
|
partition: tags[consulapi.MemberTagKeyPartition],
|
|
|
|
datacenter: tags[consulapi.MemberTagKeyDatacenter],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-06 23:00:02 +00:00
|
|
|
// detailedOutput is used to dump all known information about nodes in
|
|
|
|
// their raw format
|
2017-10-12 22:15:13 +00:00
|
|
|
func (c *cmd) detailedOutput(members []*consulapi.AgentMember) []string {
|
2014-06-06 23:00:02 +00:00
|
|
|
result := make([]string, 0, len(members))
|
2020-03-09 10:24:56 +00:00
|
|
|
header := "Node\x1fAddress\x1fStatus\x1fTags"
|
2014-06-06 23:00:02 +00:00
|
|
|
result = append(result, header)
|
|
|
|
for _, member := range members {
|
2015-05-22 08:27:47 +00:00
|
|
|
// Get the tags sorted by key
|
|
|
|
tagKeys := make([]string, 0, len(member.Tags))
|
|
|
|
for key := range member.Tags {
|
|
|
|
tagKeys = append(tagKeys, key)
|
|
|
|
}
|
|
|
|
sort.Strings(tagKeys)
|
|
|
|
|
2014-01-31 20:24:39 +00:00
|
|
|
// Format the tags as tag1=v1,tag2=v2,...
|
|
|
|
var tagPairs []string
|
2015-05-22 08:27:47 +00:00
|
|
|
for _, key := range tagKeys {
|
|
|
|
tagPairs = append(tagPairs, fmt.Sprintf("%s=%s", key, member.Tags[key]))
|
2014-01-31 20:24:39 +00:00
|
|
|
}
|
2015-05-22 08:27:47 +00:00
|
|
|
|
2014-01-31 20:24:39 +00:00
|
|
|
tags := strings.Join(tagPairs, ",")
|
|
|
|
|
2017-02-09 22:12:47 +00:00
|
|
|
addr := net.TCPAddr{IP: net.ParseIP(member.Addr), Port: int(member.Port)}
|
2020-03-09 10:24:56 +00:00
|
|
|
line := fmt.Sprintf("%s\x1f%s\x1f%s\x1f%s",
|
2017-02-09 22:12:47 +00:00
|
|
|
member.Name, addr.String(), serf.MemberStatus(member.Status).String(), tags)
|
2014-03-06 03:52:31 +00:00
|
|
|
result = append(result, line)
|
2013-12-31 21:06:33 +00:00
|
|
|
}
|
2014-06-06 23:00:02 +00:00
|
|
|
return result
|
2013-12-31 21:06:33 +00:00
|
|
|
}
|
|
|
|
|
2017-10-12 22:15:13 +00:00
|
|
|
func (c *cmd) Synopsis() string {
|
2017-10-17 13:44:20 +00:00
|
|
|
return synopsis
|
2013-12-31 21:06:33 +00:00
|
|
|
}
|
2017-10-12 22:15:13 +00:00
|
|
|
|
2017-10-17 13:44:20 +00:00
|
|
|
func (c *cmd) Help() string {
|
|
|
|
return c.help
|
|
|
|
}
|
|
|
|
|
|
|
|
const synopsis = "Lists the members of a Consul cluster"
|
|
|
|
const help = `
|
2017-10-12 22:15:13 +00:00
|
|
|
Usage: consul members [options]
|
|
|
|
|
|
|
|
Outputs the members of a running Consul agent.
|
|
|
|
`
|