2013-12-31 21:06:33 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"github.com/hashicorp/consul/command/agent"
|
|
|
|
"github.com/mitchellh/cli"
|
2014-03-06 03:52:31 +00:00
|
|
|
"github.com/ryanuber/columnize"
|
2013-12-31 21:06:33 +00:00
|
|
|
"net"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// MembersCommand is a Command implementation that queries a running
|
|
|
|
// Consul agent what members are part of the cluster currently.
|
|
|
|
type MembersCommand struct {
|
|
|
|
Ui cli.Ui
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *MembersCommand) Help() string {
|
|
|
|
helpText := `
|
|
|
|
Usage: consul members [options]
|
|
|
|
|
|
|
|
Outputs the members of a running Consul agent.
|
|
|
|
|
|
|
|
Options:
|
|
|
|
|
2014-06-06 23:00:02 +00:00
|
|
|
-detailed Provides detailed information about nodes
|
2013-12-31 21:06:33 +00:00
|
|
|
|
2014-02-19 01:32:13 +00:00
|
|
|
-rpc-addr=127.0.0.1:8400 RPC address of the Consul agent.
|
2013-12-31 21:06:33 +00:00
|
|
|
|
2014-02-19 01:32:13 +00:00
|
|
|
-status=<regexp> If provided, output is filtered to only nodes matching
|
2013-12-31 21:06:33 +00:00
|
|
|
the regular expression for status
|
|
|
|
|
2014-02-19 01:32:13 +00:00
|
|
|
-wan If the agent is in server mode, this can be used to return
|
2013-12-31 21:06:33 +00:00
|
|
|
the other peers in the WAN pool
|
|
|
|
`
|
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *MembersCommand) Run(args []string) int {
|
2014-06-06 23:00:02 +00:00
|
|
|
var detailed bool
|
2014-03-09 22:46:43 +00:00
|
|
|
var wan bool
|
2014-06-06 23:00:02 +00:00
|
|
|
var statusFilter string
|
2013-12-31 21:06:33 +00:00
|
|
|
cmdFlags := flag.NewFlagSet("members", flag.ContinueOnError)
|
|
|
|
cmdFlags.Usage = func() { c.Ui.Output(c.Help()) }
|
2014-06-06 23:00:02 +00:00
|
|
|
cmdFlags.BoolVar(&detailed, "detailed", false, "detailed output")
|
2013-12-31 21:06:33 +00:00
|
|
|
cmdFlags.BoolVar(&wan, "wan", false, "wan members")
|
|
|
|
cmdFlags.StringVar(&statusFilter, "status", ".*", "status filter")
|
|
|
|
rpcAddr := RPCAddrFlag(cmdFlags)
|
|
|
|
if err := cmdFlags.Parse(args); err != nil {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compile the regexp
|
|
|
|
statusRe, err := regexp.Compile(statusFilter)
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Failed to compile status regexp: %v", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
client, err := RPCClient(*rpcAddr)
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error connecting to Consul agent: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
defer client.Close()
|
|
|
|
|
|
|
|
var members []agent.Member
|
|
|
|
if wan {
|
|
|
|
members, err = client.WANMembers()
|
|
|
|
} else {
|
|
|
|
members, err = client.LANMembers()
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error retrieving members: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2014-06-06 23:00:02 +00:00
|
|
|
// Filter the results
|
|
|
|
n := len(members)
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
member := members[i]
|
|
|
|
if !statusRe.MatchString(member.Status) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate the output
|
|
|
|
var result []string
|
|
|
|
if detailed {
|
|
|
|
result = c.detailedOutput(members)
|
|
|
|
} else {
|
|
|
|
result = c.standardOutput(members)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate the columnized version
|
|
|
|
output := columnize.SimpleFormat(result)
|
|
|
|
c.Ui.Output(string(output))
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// standardOutput is used to dump the most useful information about nodes
|
|
|
|
// in a more human-friendly format
|
|
|
|
func (c *MembersCommand) standardOutput(members []agent.Member) []string {
|
2014-03-06 03:52:31 +00:00
|
|
|
result := make([]string, 0, len(members))
|
2014-06-06 23:00:02 +00:00
|
|
|
header := "Node|Address|Status|Type|Build|Protocol"
|
|
|
|
result = append(result, header)
|
2013-12-31 21:06:33 +00:00
|
|
|
for _, member := range members {
|
2014-06-06 23:00:02 +00:00
|
|
|
addr := net.TCPAddr{IP: member.Addr, Port: int(member.Port)}
|
|
|
|
protocol := member.Tags["vsn"]
|
|
|
|
build := member.Tags["build"]
|
|
|
|
if build == "" {
|
|
|
|
build = "< 0.3"
|
|
|
|
} else if idx := strings.Index(build, ":"); idx != -1 {
|
|
|
|
build = build[:idx]
|
|
|
|
}
|
|
|
|
|
|
|
|
switch member.Tags["role"] {
|
|
|
|
case "node":
|
|
|
|
line := fmt.Sprintf("%s|%s|%s|client|%s|%s",
|
|
|
|
member.Name, addr.String(), member.Status, build, protocol)
|
|
|
|
result = append(result, line)
|
|
|
|
case "consul":
|
|
|
|
line := fmt.Sprintf("%s|%s|%s|server|%s|%s",
|
|
|
|
member.Name, addr.String(), member.Status, build, protocol)
|
|
|
|
result = append(result, line)
|
|
|
|
default:
|
|
|
|
line := fmt.Sprintf("%s|%s|%s|unknown||",
|
|
|
|
member.Name, addr.String(), member.Status)
|
|
|
|
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
|
|
|
|
2014-06-06 23:00:02 +00:00
|
|
|
// detailedOutput is used to dump all known information about nodes in
|
|
|
|
// their raw format
|
|
|
|
func (c *MembersCommand) detailedOutput(members []agent.Member) []string {
|
|
|
|
result := make([]string, 0, len(members))
|
|
|
|
header := "Node|Address|Status|Tags"
|
|
|
|
result = append(result, header)
|
|
|
|
for _, member := range members {
|
2014-01-31 20:24:39 +00:00
|
|
|
// Format the tags as tag1=v1,tag2=v2,...
|
|
|
|
var tagPairs []string
|
|
|
|
for name, value := range member.Tags {
|
|
|
|
tagPairs = append(tagPairs, fmt.Sprintf("%s=%s", name, value))
|
|
|
|
}
|
|
|
|
tags := strings.Join(tagPairs, ",")
|
|
|
|
|
2013-12-31 21:06:33 +00:00
|
|
|
addr := net.TCPAddr{IP: member.Addr, Port: int(member.Port)}
|
2014-03-06 03:52:31 +00:00
|
|
|
line := fmt.Sprintf("%s|%s|%s|%s",
|
|
|
|
member.Name, addr.String(), member.Status, tags)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func (c *MembersCommand) Synopsis() string {
|
|
|
|
return "Lists the members of a Consul cluster"
|
|
|
|
}
|