2023-04-10 15:36:59 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2017-02-10 01:58:20 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-03-02 21:16:30 +00:00
|
|
|
"sort"
|
2017-02-10 01:58:20 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/api"
|
2017-08-18 15:09:52 +00:00
|
|
|
"github.com/posener/complete"
|
2017-02-10 01:58:20 +00:00
|
|
|
"github.com/ryanuber/columnize"
|
|
|
|
)
|
|
|
|
|
|
|
|
type OperatorRaftListCommand struct {
|
|
|
|
Meta
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *OperatorRaftListCommand) Help() string {
|
|
|
|
helpText := `
|
|
|
|
Usage: nomad operator raft list-peers [options]
|
|
|
|
|
2017-10-13 21:36:02 +00:00
|
|
|
Displays the current Raft peer configuration.
|
2017-02-10 01:58:20 +00:00
|
|
|
|
2020-11-19 21:38:08 +00:00
|
|
|
If ACLs are enabled, this command requires a management token.
|
|
|
|
|
2017-02-10 01:58:20 +00:00
|
|
|
General Options:
|
|
|
|
|
2020-11-19 16:15:23 +00:00
|
|
|
` + generalOptionsUsage(usageOptsDefault|usageOptsNoNamespace) + `
|
2017-02-10 01:58:20 +00:00
|
|
|
|
|
|
|
List Peers Options:
|
|
|
|
|
|
|
|
-stale=[true|false]
|
|
|
|
The -stale argument defaults to "false" which means the leader provides the
|
|
|
|
result. If the cluster is in an outage state without a leader, you may need
|
|
|
|
to set -stale to "true" to get the configuration from a non-leader server.
|
|
|
|
`
|
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|
|
|
|
|
2017-08-18 15:09:52 +00:00
|
|
|
func (c *OperatorRaftListCommand) AutocompleteFlags() complete.Flags {
|
2017-08-23 21:56:21 +00:00
|
|
|
return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient),
|
|
|
|
complete.Flags{
|
|
|
|
"-stale": complete.PredictAnything,
|
|
|
|
})
|
2017-08-18 15:09:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *OperatorRaftListCommand) AutocompleteArgs() complete.Predictor {
|
|
|
|
return complete.PredictNothing
|
|
|
|
}
|
|
|
|
|
2017-02-10 01:58:20 +00:00
|
|
|
func (c *OperatorRaftListCommand) Synopsis() string {
|
|
|
|
return "Display the current Raft peer configuration"
|
|
|
|
}
|
|
|
|
|
2018-04-18 16:02:11 +00:00
|
|
|
func (c *OperatorRaftListCommand) Name() string { return "operator raft list-peers" }
|
|
|
|
|
2017-02-10 01:58:20 +00:00
|
|
|
func (c *OperatorRaftListCommand) Run(args []string) int {
|
|
|
|
var stale bool
|
|
|
|
|
|
|
|
flags := c.Meta.FlagSet("raft", FlagSetClient)
|
|
|
|
flags.Usage = func() { c.Ui.Output(c.Help()) }
|
|
|
|
|
|
|
|
flags.BoolVar(&stale, "stale", false, "")
|
|
|
|
if err := flags.Parse(args); err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Failed to parse args: %v", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set up a client.
|
|
|
|
client, err := c.Meta.Client()
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
operator := client.Operator()
|
|
|
|
|
|
|
|
// Fetch the current configuration.
|
|
|
|
q := &api.QueryOptions{
|
|
|
|
AllowStale: stale,
|
|
|
|
}
|
|
|
|
reply, err := operator.RaftGetConfiguration(q)
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Failed to retrieve raft configuration: %v", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Format it as a nice table.
|
2017-11-22 00:29:11 +00:00
|
|
|
result := []string{"Node|ID|Address|State|Voter|RaftProtocol"}
|
2023-03-02 21:16:30 +00:00
|
|
|
sort.Slice(reply.Servers, func(i, j int) bool {
|
|
|
|
return reply.Servers[i].Node < reply.Servers[j].Node
|
|
|
|
})
|
|
|
|
|
2017-02-10 01:58:20 +00:00
|
|
|
for _, s := range reply.Servers {
|
|
|
|
state := "follower"
|
|
|
|
if s.Leader {
|
|
|
|
state = "leader"
|
|
|
|
}
|
2017-11-22 00:29:11 +00:00
|
|
|
result = append(result, fmt.Sprintf("%s|%s|%s|%s|%v|%s",
|
|
|
|
s.Node, s.ID, s.Address, state, s.Voter, s.RaftProtocol))
|
2017-02-10 01:58:20 +00:00
|
|
|
}
|
|
|
|
c.Ui.Output(columnize.SimpleFormat(result))
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|