github.com/blixtra/nomad@v0.7.2-0.20171221000451-da9a1d7bb050/command/operator_raft_list.go (about) 1 package command 2 3 import ( 4 "fmt" 5 "strings" 6 7 "github.com/hashicorp/nomad/api" 8 "github.com/posener/complete" 9 "github.com/ryanuber/columnize" 10 ) 11 12 type OperatorRaftListCommand struct { 13 Meta 14 } 15 16 func (c *OperatorRaftListCommand) Help() string { 17 helpText := ` 18 Usage: nomad operator raft list-peers [options] 19 20 Displays the current Raft peer configuration. 21 22 General Options: 23 24 ` + generalOptionsUsage() + ` 25 26 List Peers Options: 27 28 -stale=[true|false] 29 The -stale argument defaults to "false" which means the leader provides the 30 result. If the cluster is in an outage state without a leader, you may need 31 to set -stale to "true" to get the configuration from a non-leader server. 32 ` 33 return strings.TrimSpace(helpText) 34 } 35 36 func (c *OperatorRaftListCommand) AutocompleteFlags() complete.Flags { 37 return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient), 38 complete.Flags{ 39 "-stale": complete.PredictAnything, 40 }) 41 } 42 43 func (c *OperatorRaftListCommand) AutocompleteArgs() complete.Predictor { 44 return complete.PredictNothing 45 } 46 47 func (c *OperatorRaftListCommand) Synopsis() string { 48 return "Display the current Raft peer configuration" 49 } 50 51 func (c *OperatorRaftListCommand) Run(args []string) int { 52 var stale bool 53 54 flags := c.Meta.FlagSet("raft", FlagSetClient) 55 flags.Usage = func() { c.Ui.Output(c.Help()) } 56 57 flags.BoolVar(&stale, "stale", false, "") 58 if err := flags.Parse(args); err != nil { 59 c.Ui.Error(fmt.Sprintf("Failed to parse args: %v", err)) 60 return 1 61 } 62 63 // Set up a client. 64 client, err := c.Meta.Client() 65 if err != nil { 66 c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err)) 67 return 1 68 } 69 operator := client.Operator() 70 71 // Fetch the current configuration. 72 q := &api.QueryOptions{ 73 AllowStale: stale, 74 } 75 reply, err := operator.RaftGetConfiguration(q) 76 if err != nil { 77 c.Ui.Error(fmt.Sprintf("Failed to retrieve raft configuration: %v", err)) 78 return 1 79 } 80 81 // Format it as a nice table. 82 result := []string{"Node|ID|Address|State|Voter"} 83 for _, s := range reply.Servers { 84 state := "follower" 85 if s.Leader { 86 state = "leader" 87 } 88 result = append(result, fmt.Sprintf("%s|%s|%s|%s|%v", 89 s.Node, s.ID, s.Address, state, s.Voter)) 90 } 91 c.Ui.Output(columnize.SimpleFormat(result)) 92 93 return 0 94 }