github.com/emate/nomad@v0.8.2-wo-binpacking/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) Name() string { return "operator raft list-peers" }
    52  
    53  func (c *OperatorRaftListCommand) Run(args []string) int {
    54  	var stale bool
    55  
    56  	flags := c.Meta.FlagSet("raft", FlagSetClient)
    57  	flags.Usage = func() { c.Ui.Output(c.Help()) }
    58  
    59  	flags.BoolVar(&stale, "stale", false, "")
    60  	if err := flags.Parse(args); err != nil {
    61  		c.Ui.Error(fmt.Sprintf("Failed to parse args: %v", err))
    62  		return 1
    63  	}
    64  
    65  	// Set up a client.
    66  	client, err := c.Meta.Client()
    67  	if err != nil {
    68  		c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
    69  		return 1
    70  	}
    71  	operator := client.Operator()
    72  
    73  	// Fetch the current configuration.
    74  	q := &api.QueryOptions{
    75  		AllowStale: stale,
    76  	}
    77  	reply, err := operator.RaftGetConfiguration(q)
    78  	if err != nil {
    79  		c.Ui.Error(fmt.Sprintf("Failed to retrieve raft configuration: %v", err))
    80  		return 1
    81  	}
    82  
    83  	// Format it as a nice table.
    84  	result := []string{"Node|ID|Address|State|Voter|RaftProtocol"}
    85  	for _, s := range reply.Servers {
    86  		state := "follower"
    87  		if s.Leader {
    88  			state = "leader"
    89  		}
    90  		result = append(result, fmt.Sprintf("%s|%s|%s|%s|%v|%s",
    91  			s.Node, s.ID, s.Address, state, s.Voter, s.RaftProtocol))
    92  	}
    93  	c.Ui.Output(columnize.SimpleFormat(result))
    94  
    95  	return 0
    96  }