github.com/hhrutter/nomad@v0.6.0-rc2.0.20170723054333-80c4b03f0705/command/operator_raft_remove.go (about)

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