2015-09-11 19:58:16 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-10-21 12:35:34 +00:00
|
|
|
"sort"
|
2015-09-11 19:58:16 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/api"
|
|
|
|
"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 := `
|
2015-09-22 18:01:41 +00:00
|
|
|
Usage: nomad server-members [options]
|
2015-09-11 19:58:16 +00:00
|
|
|
|
2015-09-22 18:01:41 +00:00
|
|
|
Display a list of the known servers and their status.
|
2015-09-11 19:58:16 +00:00
|
|
|
|
2015-09-14 20:13:52 +00:00
|
|
|
General Options:
|
|
|
|
|
|
|
|
` + generalOptionsUsage() + `
|
|
|
|
|
|
|
|
Agent Members Options:
|
2015-09-11 19:58:16 +00:00
|
|
|
|
|
|
|
-detailed
|
|
|
|
Show detailed information about each member. This dumps
|
|
|
|
a raw set of tags which shows more information than the
|
|
|
|
default output format.
|
|
|
|
`
|
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-09-22 18:01:41 +00:00
|
|
|
func (c *ServerMembersCommand) Run(args []string) int {
|
2015-09-11 19:58:16 +00:00
|
|
|
var detailed bool
|
|
|
|
|
2015-09-22 18:01:41 +00:00
|
|
|
flags := c.Meta.FlagSet("server-members", 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")
|
|
|
|
|
|
|
|
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 {
|
2015-09-11 19:58:16 +00:00
|
|
|
c.Ui.Error(c.Help())
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
mem, err := client.Agent().Members()
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-10-21 12:35:34 +00:00
|
|
|
// Sort the members
|
|
|
|
sort.Sort(api.AgentMembersNameSort(mem))
|
|
|
|
|
2015-09-11 19:58:16 +00:00
|
|
|
// Format the list
|
|
|
|
var out []string
|
|
|
|
if detailed {
|
|
|
|
out = detailedOutput(mem)
|
|
|
|
} else {
|
|
|
|
out = standardOutput(mem)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dump the list
|
|
|
|
c.Ui.Output(columnize.SimpleFormat(out))
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func standardOutput(mem []*api.AgentMember) []string {
|
|
|
|
// Format the members list
|
|
|
|
members := make([]string, len(mem)+1)
|
2016-01-21 20:35:56 +00:00
|
|
|
members[0] = "Name|Address|Port|Status|Protocol|Build|Datacenter|Region"
|
2015-09-11 19:58:16 +00:00
|
|
|
for i, member := range mem {
|
2015-09-13 18:39:49 +00:00
|
|
|
members[i+1] = fmt.Sprintf("%s|%s|%d|%s|%d|%s|%s|%s",
|
2015-09-11 19:58:16 +00:00
|
|
|
member.Name,
|
|
|
|
member.Addr,
|
|
|
|
member.Port,
|
|
|
|
member.Status,
|
|
|
|
member.ProtocolCur,
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func detailedOutput(mem []*api.AgentMember) []string {
|
|
|
|
// Format the members list
|
|
|
|
members := make([]string, len(mem)+1)
|
2016-01-21 20:35:56 +00:00
|
|
|
members[0] = "Name|Address|Port|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, ",")
|
|
|
|
|
2015-09-13 18:39:49 +00:00
|
|
|
members[i+1] = fmt.Sprintf("%s|%s|%d|%s",
|
2015-09-11 19:58:16 +00:00
|
|
|
member.Name,
|
|
|
|
member.Addr,
|
|
|
|
member.Port,
|
2015-09-13 18:39:49 +00:00
|
|
|
tags)
|
2015-09-11 19:58:16 +00:00
|
|
|
}
|
|
|
|
return members
|
|
|
|
}
|