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:
|
|
|
|
|
|
|
|
-role=<regexp> If provided, output is filtered to only nodes matching
|
|
|
|
the regular expression for role
|
|
|
|
|
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-03-09 22:46:43 +00:00
|
|
|
var wan bool
|
2013-12-31 21:06:33 +00:00
|
|
|
var roleFilter, statusFilter string
|
|
|
|
cmdFlags := flag.NewFlagSet("members", flag.ContinueOnError)
|
|
|
|
cmdFlags.Usage = func() { c.Ui.Output(c.Help()) }
|
|
|
|
cmdFlags.BoolVar(&wan, "wan", false, "wan members")
|
|
|
|
cmdFlags.StringVar(&roleFilter, "role", ".*", "role filter")
|
|
|
|
cmdFlags.StringVar(&statusFilter, "status", ".*", "status filter")
|
|
|
|
rpcAddr := RPCAddrFlag(cmdFlags)
|
|
|
|
if err := cmdFlags.Parse(args); err != nil {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compile the regexp
|
|
|
|
roleRe, err := regexp.Compile(roleFilter)
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Failed to compile role regexp: %v", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
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-03-06 03:52:31 +00:00
|
|
|
result := make([]string, 0, len(members))
|
2013-12-31 21:06:33 +00:00
|
|
|
for _, member := range members {
|
|
|
|
// Skip the non-matching members
|
2014-01-31 20:24:39 +00:00
|
|
|
if !roleRe.MatchString(member.Tags["role"]) || !statusRe.MatchString(member.Status) {
|
2013-12-31 21:06:33 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
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-05-04 09:54:45 +00:00
|
|
|
// No matching members
|
|
|
|
if len(result) == 0 {
|
|
|
|
return 2
|
|
|
|
}
|
|
|
|
|
2014-03-06 03:52:31 +00:00
|
|
|
// Generate the columnized version
|
2014-04-16 23:21:05 +00:00
|
|
|
output := columnize.SimpleFormat(result)
|
2014-03-06 03:52:31 +00:00
|
|
|
c.Ui.Output(string(output))
|
|
|
|
|
2013-12-31 21:06:33 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *MembersCommand) Synopsis() string {
|
|
|
|
return "Lists the members of a Consul cluster"
|
|
|
|
}
|