2017-10-17 06:19:25 +00:00
|
|
|
package operraftlist
|
2017-02-15 21:30:07 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/api"
|
2017-10-17 06:19:25 +00:00
|
|
|
"github.com/hashicorp/consul/command/flags"
|
|
|
|
"github.com/mitchellh/cli"
|
2017-02-15 21:30:07 +00:00
|
|
|
"github.com/ryanuber/columnize"
|
|
|
|
)
|
|
|
|
|
2017-10-17 06:19:25 +00:00
|
|
|
func New(ui cli.Ui) *cmd {
|
|
|
|
c := &cmd{UI: ui}
|
|
|
|
c.init()
|
|
|
|
return c
|
2017-02-15 21:30:07 +00:00
|
|
|
}
|
|
|
|
|
2017-10-17 06:19:25 +00:00
|
|
|
type cmd struct {
|
|
|
|
UI cli.Ui
|
|
|
|
flags *flag.FlagSet
|
|
|
|
http *flags.HTTPFlags
|
|
|
|
usage string
|
|
|
|
}
|
2017-02-15 21:30:07 +00:00
|
|
|
|
2017-10-17 06:19:25 +00:00
|
|
|
func (c *cmd) init() {
|
|
|
|
c.flags = flag.NewFlagSet("", flag.ContinueOnError)
|
|
|
|
c.http = &flags.HTTPFlags{}
|
|
|
|
flags.Merge(c.flags, c.http.ClientFlags())
|
|
|
|
flags.Merge(c.flags, c.http.ServerFlags())
|
|
|
|
c.usage = flags.Usage(usage, c.flags, c.http.ClientFlags(), c.http.ServerFlags())
|
2017-02-15 21:30:07 +00:00
|
|
|
}
|
|
|
|
|
2017-10-17 06:19:25 +00:00
|
|
|
func (c *cmd) Synopsis() string {
|
2017-02-15 21:30:07 +00:00
|
|
|
return "Display the current Raft peer configuration"
|
|
|
|
}
|
|
|
|
|
2017-10-17 06:19:25 +00:00
|
|
|
func (c *cmd) Help() string {
|
|
|
|
return c.usage
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cmd) Run(args []string) int {
|
|
|
|
if err := c.flags.Parse(args); err != nil {
|
2017-02-15 21:30:07 +00:00
|
|
|
if err == flag.ErrHelp {
|
|
|
|
return 0
|
|
|
|
}
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Error(fmt.Sprintf("Failed to parse args: %v", err))
|
2017-02-15 21:30:07 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set up a client.
|
2017-10-17 06:19:25 +00:00
|
|
|
client, err := c.http.APIClient()
|
2017-02-15 21:30:07 +00:00
|
|
|
if err != nil {
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Error(fmt.Sprintf("Error initializing client: %s", err))
|
2017-02-15 21:30:07 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch the current configuration.
|
2017-10-17 06:19:25 +00:00
|
|
|
result, err := raftListPeers(client, c.http.Stale())
|
2017-02-15 21:30:07 +00:00
|
|
|
if err != nil {
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Error(fmt.Sprintf("Error getting peers: %v", err))
|
2017-09-28 22:37:59 +00:00
|
|
|
return 1
|
2017-02-15 21:30:07 +00:00
|
|
|
}
|
|
|
|
|
2017-09-28 22:37:59 +00:00
|
|
|
c.UI.Output(result)
|
2017-02-15 21:30:07 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2017-04-19 22:01:40 +00:00
|
|
|
func raftListPeers(client *api.Client, stale bool) (string, error) {
|
2017-02-15 21:30:07 +00:00
|
|
|
q := &api.QueryOptions{
|
|
|
|
AllowStale: stale,
|
|
|
|
}
|
2017-04-19 22:01:40 +00:00
|
|
|
reply, err := client.Operator().RaftGetConfiguration(q)
|
2017-02-15 21:30:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("Failed to retrieve raft configuration: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Format it as a nice table.
|
2017-04-19 22:01:40 +00:00
|
|
|
result := []string{"Node|ID|Address|State|Voter|RaftProtocol"}
|
2017-02-15 21:30:07 +00:00
|
|
|
for _, s := range reply.Servers {
|
2017-09-26 14:34:12 +00:00
|
|
|
raftProtocol := s.ProtocolVersion
|
|
|
|
|
2017-04-19 22:01:40 +00:00
|
|
|
if raftProtocol == "" {
|
|
|
|
raftProtocol = "<=1"
|
|
|
|
}
|
2017-02-15 21:30:07 +00:00
|
|
|
state := "follower"
|
|
|
|
if s.Leader {
|
|
|
|
state = "leader"
|
|
|
|
}
|
2017-04-19 22:01:40 +00:00
|
|
|
result = append(result, fmt.Sprintf("%s|%s|%s|%s|%v|%s",
|
|
|
|
s.Node, s.ID, s.Address, state, s.Voter, raftProtocol))
|
2017-02-15 21:30:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return columnize.SimpleFormat(result), nil
|
|
|
|
}
|
2017-10-17 06:19:25 +00:00
|
|
|
|
|
|
|
const usage = `Usage: consul operator raft list-peers [options]
|
|
|
|
|
|
|
|
Displays the current Raft peer configuration.`
|