github.com/blixtra/nomad@v0.7.2-0.20171221000451-da9a1d7bb050/command/operator_raft_remove.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  )
    10  
    11  type OperatorRaftRemoveCommand struct {
    12  	Meta
    13  }
    14  
    15  func (c *OperatorRaftRemoveCommand) Help() string {
    16  	helpText := `
    17  Usage: nomad operator raft remove-peer [options]
    18  
    19    Remove the Nomad server with given -peer-address from the Raft configuration.
    20  
    21    There are rare cases where a peer may be left behind in the Raft quorum even
    22    though the server is no longer present and known to the cluster. This command
    23    can be used to remove the failed server so that it is no longer affects the
    24    Raft quorum. If the server still shows in the output of the "nomad
    25    server-members" command, it is preferable to clean up by simply running "nomad
    26    server-force-leave" instead of this command.
    27  
    28  General Options:
    29  
    30    ` + generalOptionsUsage() + `
    31  
    32  Remove Peer Options:
    33  
    34    -peer-address="IP:port"
    35      Remove a Nomad server with given address from the Raft configuration.
    36  `
    37  	return strings.TrimSpace(helpText)
    38  }
    39  
    40  func (c *OperatorRaftRemoveCommand) AutocompleteFlags() complete.Flags {
    41  	return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient),
    42  		complete.Flags{
    43  			"-peer-address": complete.PredictAnything,
    44  		})
    45  }
    46  
    47  func (c *OperatorRaftRemoveCommand) AutocompleteArgs() complete.Predictor {
    48  	return complete.PredictNothing
    49  }
    50  
    51  func (c *OperatorRaftRemoveCommand) Synopsis() string {
    52  	return "Remove a Nomad server from the Raft configuration"
    53  }
    54  
    55  func (c *OperatorRaftRemoveCommand) Run(args []string) int {
    56  	var peerAddress string
    57  
    58  	flags := c.Meta.FlagSet("raft", FlagSetClient)
    59  	flags.Usage = func() { c.Ui.Output(c.Help()) }
    60  
    61  	flags.StringVar(&peerAddress, "peer-address", "", "")
    62  	if err := flags.Parse(args); err != nil {
    63  		c.Ui.Error(fmt.Sprintf("Failed to parse args: %v", err))
    64  		return 1
    65  	}
    66  
    67  	// Set up a client.
    68  	client, err := c.Meta.Client()
    69  	if err != nil {
    70  		c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
    71  		return 1
    72  	}
    73  	operator := client.Operator()
    74  
    75  	// TODO (alexdadgar) Once we expose IDs, add support for removing
    76  	// by ID, add support for that.
    77  	if len(peerAddress) == 0 {
    78  		c.Ui.Error(fmt.Sprintf("an address is required for the peer to remove"))
    79  		return 1
    80  	}
    81  
    82  	// Try to kick the peer.
    83  	w := &api.WriteOptions{}
    84  	if err := operator.RaftRemovePeerByAddress(peerAddress, w); err != nil {
    85  		c.Ui.Error(fmt.Sprintf("Failed to remove raft peer: %v", err))
    86  		return 1
    87  	}
    88  	c.Ui.Output(fmt.Sprintf("Removed peer with address %q", peerAddress))
    89  
    90  	return 0
    91  }