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