mirror of https://github.com/status-im/consul.git
Convert members command to use base.Command
This commit is contained in:
parent
6c4d4f9a32
commit
14415741a3
|
@ -1,21 +1,22 @@
|
||||||
package command
|
package command
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/hashicorp/consul/command/agent"
|
|
||||||
"github.com/mitchellh/cli"
|
|
||||||
"github.com/ryanuber/columnize"
|
|
||||||
"net"
|
"net"
|
||||||
"regexp"
|
"regexp"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
consulapi "github.com/hashicorp/consul/api"
|
||||||
|
"github.com/hashicorp/consul/command/base"
|
||||||
|
"github.com/hashicorp/serf/serf"
|
||||||
|
"github.com/ryanuber/columnize"
|
||||||
)
|
)
|
||||||
|
|
||||||
// MembersCommand is a Command implementation that queries a running
|
// MembersCommand is a Command implementation that queries a running
|
||||||
// Consul agent what members are part of the cluster currently.
|
// Consul agent what members are part of the cluster currently.
|
||||||
type MembersCommand struct {
|
type MembersCommand struct {
|
||||||
Ui cli.Ui
|
base.Command
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *MembersCommand) Help() string {
|
func (c *MembersCommand) Help() string {
|
||||||
|
@ -24,18 +25,8 @@ Usage: consul members [options]
|
||||||
|
|
||||||
Outputs the members of a running Consul agent.
|
Outputs the members of a running Consul agent.
|
||||||
|
|
||||||
Options:
|
` + c.Command.Help()
|
||||||
|
|
||||||
-detailed Provides detailed information about nodes
|
|
||||||
|
|
||||||
-rpc-addr=127.0.0.1:8400 RPC address of the Consul agent.
|
|
||||||
|
|
||||||
-status=<regexp> If provided, output is filtered to only nodes matching
|
|
||||||
the regular expression for status
|
|
||||||
|
|
||||||
-wan If the agent is in server mode, this can be used to return
|
|
||||||
the other peers in the WAN pool
|
|
||||||
`
|
|
||||||
return strings.TrimSpace(helpText)
|
return strings.TrimSpace(helpText)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -43,13 +34,18 @@ func (c *MembersCommand) Run(args []string) int {
|
||||||
var detailed bool
|
var detailed bool
|
||||||
var wan bool
|
var wan bool
|
||||||
var statusFilter string
|
var statusFilter string
|
||||||
cmdFlags := flag.NewFlagSet("members", flag.ContinueOnError)
|
|
||||||
cmdFlags.Usage = func() { c.Ui.Output(c.Help()) }
|
f := c.Command.NewFlagSet(c)
|
||||||
cmdFlags.BoolVar(&detailed, "detailed", false, "detailed output")
|
f.BoolVar(&detailed, "detailed", false,
|
||||||
cmdFlags.BoolVar(&wan, "wan", false, "wan members")
|
"Provides detailed information about nodes.")
|
||||||
cmdFlags.StringVar(&statusFilter, "status", ".*", "status filter")
|
f.BoolVar(&wan, "wan", false,
|
||||||
rpcAddr := RPCAddrFlag(cmdFlags)
|
"If the agent is in server mode, this can be used to return the other "+
|
||||||
if err := cmdFlags.Parse(args); err != nil {
|
"peers in the WAN pool")
|
||||||
|
f.StringVar(&statusFilter, "status", ".*",
|
||||||
|
"If provided, output is filtered to only nodes matching the regular "+
|
||||||
|
"expression for status")
|
||||||
|
|
||||||
|
if err := c.Command.Parse(args); err != nil {
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -60,19 +56,13 @@ func (c *MembersCommand) Run(args []string) int {
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
client, err := RPCClient(*rpcAddr)
|
client, err := c.Command.HTTPClient()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.Ui.Error(fmt.Sprintf("Error connecting to Consul agent: %s", err))
|
c.Ui.Error(fmt.Sprintf("Error connecting to Consul agent: %s", err))
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
defer client.Close()
|
|
||||||
|
|
||||||
var members []agent.Member
|
members, err := client.Agent().Members(wan)
|
||||||
if wan {
|
|
||||||
members, err = client.WANMembers()
|
|
||||||
} else {
|
|
||||||
members, err = client.LANMembers()
|
|
||||||
}
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.Ui.Error(fmt.Sprintf("Error retrieving members: %s", err))
|
c.Ui.Error(fmt.Sprintf("Error retrieving members: %s", err))
|
||||||
return 1
|
return 1
|
||||||
|
@ -82,7 +72,8 @@ func (c *MembersCommand) Run(args []string) int {
|
||||||
n := len(members)
|
n := len(members)
|
||||||
for i := 0; i < n; i++ {
|
for i := 0; i < n; i++ {
|
||||||
member := members[i]
|
member := members[i]
|
||||||
if !statusRe.MatchString(member.Status) {
|
statusString := serf.MemberStatus(member.Status).String()
|
||||||
|
if !statusRe.MatchString(statusString) {
|
||||||
members[i], members[n-1] = members[n-1], members[i]
|
members[i], members[n-1] = members[n-1], members[i]
|
||||||
i--
|
i--
|
||||||
n--
|
n--
|
||||||
|
@ -114,7 +105,7 @@ func (c *MembersCommand) Run(args []string) int {
|
||||||
}
|
}
|
||||||
|
|
||||||
// so we can sort members by name
|
// so we can sort members by name
|
||||||
type ByMemberName []agent.Member
|
type ByMemberName []*consulapi.AgentMember
|
||||||
|
|
||||||
func (m ByMemberName) Len() int { return len(m) }
|
func (m ByMemberName) Len() int { return len(m) }
|
||||||
func (m ByMemberName) Swap(i, j int) { m[i], m[j] = m[j], m[i] }
|
func (m ByMemberName) Swap(i, j int) { m[i], m[j] = m[j], m[i] }
|
||||||
|
@ -122,12 +113,12 @@ func (m ByMemberName) Less(i, j int) bool { return m[i].Name < m[j].Name }
|
||||||
|
|
||||||
// standardOutput is used to dump the most useful information about nodes
|
// standardOutput is used to dump the most useful information about nodes
|
||||||
// in a more human-friendly format
|
// in a more human-friendly format
|
||||||
func (c *MembersCommand) standardOutput(members []agent.Member) []string {
|
func (c *MembersCommand) standardOutput(members []*consulapi.AgentMember) []string {
|
||||||
result := make([]string, 0, len(members))
|
result := make([]string, 0, len(members))
|
||||||
header := "Node|Address|Status|Type|Build|Protocol|DC"
|
header := "Node|Address|Status|Type|Build|Protocol|DC"
|
||||||
result = append(result, header)
|
result = append(result, header)
|
||||||
for _, member := range members {
|
for _, member := range members {
|
||||||
addr := net.TCPAddr{IP: member.Addr, Port: int(member.Port)}
|
addr := net.TCPAddr{IP: net.ParseIP(member.Addr), Port: int(member.Port)}
|
||||||
protocol := member.Tags["vsn"]
|
protocol := member.Tags["vsn"]
|
||||||
build := member.Tags["build"]
|
build := member.Tags["build"]
|
||||||
if build == "" {
|
if build == "" {
|
||||||
|
@ -137,18 +128,19 @@ func (c *MembersCommand) standardOutput(members []agent.Member) []string {
|
||||||
}
|
}
|
||||||
dc := member.Tags["dc"]
|
dc := member.Tags["dc"]
|
||||||
|
|
||||||
|
statusString := serf.MemberStatus(member.Status).String()
|
||||||
switch member.Tags["role"] {
|
switch member.Tags["role"] {
|
||||||
case "node":
|
case "node":
|
||||||
line := fmt.Sprintf("%s|%s|%s|client|%s|%s|%s",
|
line := fmt.Sprintf("%s|%s|%s|client|%s|%s|%s",
|
||||||
member.Name, addr.String(), member.Status, build, protocol, dc)
|
member.Name, addr.String(), statusString, build, protocol, dc)
|
||||||
result = append(result, line)
|
result = append(result, line)
|
||||||
case "consul":
|
case "consul":
|
||||||
line := fmt.Sprintf("%s|%s|%s|server|%s|%s|%s",
|
line := fmt.Sprintf("%s|%s|%s|server|%s|%s|%s",
|
||||||
member.Name, addr.String(), member.Status, build, protocol, dc)
|
member.Name, addr.String(), statusString, build, protocol, dc)
|
||||||
result = append(result, line)
|
result = append(result, line)
|
||||||
default:
|
default:
|
||||||
line := fmt.Sprintf("%s|%s|%s|unknown|||",
|
line := fmt.Sprintf("%s|%s|%s|unknown|||",
|
||||||
member.Name, addr.String(), member.Status)
|
member.Name, addr.String(), statusString)
|
||||||
result = append(result, line)
|
result = append(result, line)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -157,7 +149,7 @@ func (c *MembersCommand) standardOutput(members []agent.Member) []string {
|
||||||
|
|
||||||
// detailedOutput is used to dump all known information about nodes in
|
// detailedOutput is used to dump all known information about nodes in
|
||||||
// their raw format
|
// their raw format
|
||||||
func (c *MembersCommand) detailedOutput(members []agent.Member) []string {
|
func (c *MembersCommand) detailedOutput(members []*consulapi.AgentMember) []string {
|
||||||
result := make([]string, 0, len(members))
|
result := make([]string, 0, len(members))
|
||||||
header := "Node|Address|Status|Tags"
|
header := "Node|Address|Status|Tags"
|
||||||
result = append(result, header)
|
result = append(result, header)
|
||||||
|
@ -177,9 +169,9 @@ func (c *MembersCommand) detailedOutput(members []agent.Member) []string {
|
||||||
|
|
||||||
tags := strings.Join(tagPairs, ",")
|
tags := strings.Join(tagPairs, ",")
|
||||||
|
|
||||||
addr := net.TCPAddr{IP: member.Addr, Port: int(member.Port)}
|
addr := net.TCPAddr{IP: net.ParseIP(member.Addr), Port: int(member.Port)}
|
||||||
line := fmt.Sprintf("%s|%s|%s|%s",
|
line := fmt.Sprintf("%s|%s|%s|%s",
|
||||||
member.Name, addr.String(), member.Status, tags)
|
member.Name, addr.String(), serf.MemberStatus(member.Status).String(), tags)
|
||||||
result = append(result, line)
|
result = append(result, line)
|
||||||
}
|
}
|
||||||
return result
|
return result
|
||||||
|
|
|
@ -2,11 +2,22 @@ package command
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/hashicorp/consul/command/base"
|
||||||
"github.com/mitchellh/cli"
|
"github.com/mitchellh/cli"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func testMembersCommand(t *testing.T) (*cli.MockUi, *MembersCommand) {
|
||||||
|
ui := new(cli.MockUi)
|
||||||
|
return ui, &MembersCommand{
|
||||||
|
Command: base.Command{
|
||||||
|
Ui: ui,
|
||||||
|
Flags: base.FlagSetClientHTTP,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestMembersCommand_implements(t *testing.T) {
|
func TestMembersCommand_implements(t *testing.T) {
|
||||||
var _ cli.Command = &MembersCommand{}
|
var _ cli.Command = &MembersCommand{}
|
||||||
}
|
}
|
||||||
|
@ -15,9 +26,8 @@ func TestMembersCommandRun(t *testing.T) {
|
||||||
a1 := testAgent(t)
|
a1 := testAgent(t)
|
||||||
defer a1.Shutdown()
|
defer a1.Shutdown()
|
||||||
|
|
||||||
ui := new(cli.MockUi)
|
ui, c := testMembersCommand(t)
|
||||||
c := &MembersCommand{Ui: ui}
|
args := []string{"-http-addr=" + a1.httpAddr}
|
||||||
args := []string{"-rpc-addr=" + a1.addr}
|
|
||||||
|
|
||||||
code := c.Run(args)
|
code := c.Run(args)
|
||||||
if code != 0 {
|
if code != 0 {
|
||||||
|
@ -44,9 +54,8 @@ func TestMembersCommandRun_WAN(t *testing.T) {
|
||||||
a1 := testAgent(t)
|
a1 := testAgent(t)
|
||||||
defer a1.Shutdown()
|
defer a1.Shutdown()
|
||||||
|
|
||||||
ui := new(cli.MockUi)
|
ui, c := testMembersCommand(t)
|
||||||
c := &MembersCommand{Ui: ui}
|
args := []string{"-http-addr=" + a1.httpAddr, "-wan"}
|
||||||
args := []string{"-rpc-addr=" + a1.addr, "-wan"}
|
|
||||||
|
|
||||||
code := c.Run(args)
|
code := c.Run(args)
|
||||||
if code != 0 {
|
if code != 0 {
|
||||||
|
@ -62,10 +71,9 @@ func TestMembersCommandRun_statusFilter(t *testing.T) {
|
||||||
a1 := testAgent(t)
|
a1 := testAgent(t)
|
||||||
defer a1.Shutdown()
|
defer a1.Shutdown()
|
||||||
|
|
||||||
ui := new(cli.MockUi)
|
ui, c := testMembersCommand(t)
|
||||||
c := &MembersCommand{Ui: ui}
|
|
||||||
args := []string{
|
args := []string{
|
||||||
"-rpc-addr=" + a1.addr,
|
"-http-addr=" + a1.httpAddr,
|
||||||
"-status=a.*e",
|
"-status=a.*e",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -83,10 +91,9 @@ func TestMembersCommandRun_statusFilter_failed(t *testing.T) {
|
||||||
a1 := testAgent(t)
|
a1 := testAgent(t)
|
||||||
defer a1.Shutdown()
|
defer a1.Shutdown()
|
||||||
|
|
||||||
ui := new(cli.MockUi)
|
ui, c := testMembersCommand(t)
|
||||||
c := &MembersCommand{Ui: ui}
|
|
||||||
args := []string{
|
args := []string{
|
||||||
"-rpc-addr=" + a1.addr,
|
"-http-addr=" + a1.httpAddr,
|
||||||
"-status=(fail|left)",
|
"-status=(fail|left)",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -145,7 +145,10 @@ func init() {
|
||||||
|
|
||||||
"members": func() (cli.Command, error) {
|
"members": func() (cli.Command, error) {
|
||||||
return &command.MembersCommand{
|
return &command.MembersCommand{
|
||||||
Ui: ui,
|
Command: base.Command{
|
||||||
|
Flags: base.FlagSetClientHTTP,
|
||||||
|
Ui: ui,
|
||||||
|
},
|
||||||
}, nil
|
}, nil
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
|
@ -22,16 +22,15 @@ that the failure is actually just a network partition.
|
||||||
|
|
||||||
Usage: `consul members [options]`
|
Usage: `consul members [options]`
|
||||||
|
|
||||||
The command-line flags are all optional. The list of available flags are:
|
#### API Options
|
||||||
|
|
||||||
|
<%= partial "docs/commands/http_api_options_client" %>
|
||||||
|
|
||||||
|
#### Command Options
|
||||||
|
|
||||||
* `-detailed` - If provided, output shows more detailed information
|
* `-detailed` - If provided, output shows more detailed information
|
||||||
about each node.
|
about each node.
|
||||||
|
|
||||||
* `-rpc-addr` - Address to the RPC server of the agent you want to contact
|
|
||||||
to send this command.If this isn't specified, the command checks the
|
|
||||||
CONSUL_RPC_ADDR env variable. If this isn't set, the default RPC
|
|
||||||
address will be set to "127.0.0.1:8400".
|
|
||||||
|
|
||||||
* `-status` - If provided, output is filtered to only nodes matching
|
* `-status` - If provided, output is filtered to only nodes matching
|
||||||
the regular expression for status
|
the regular expression for status
|
||||||
|
|
Loading…
Reference in New Issue