2023-04-10 15:36:59 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2017-02-10 01:58:20 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/api"
|
2017-08-18 15:09:52 +00:00
|
|
|
"github.com/posener/complete"
|
2017-02-10 01:58:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type OperatorRaftRemoveCommand struct {
|
|
|
|
Meta
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *OperatorRaftRemoveCommand) Help() string {
|
|
|
|
helpText := `
|
|
|
|
Usage: nomad operator raft remove-peer [options]
|
|
|
|
|
2017-10-13 21:36:02 +00:00
|
|
|
Remove the Nomad server with given -peer-address from the Raft configuration.
|
|
|
|
|
|
|
|
There are rare cases where a peer may be left behind in the Raft quorum even
|
|
|
|
though the server is no longer present and known to the cluster. This command
|
|
|
|
can be used to remove the failed server so that it is no longer affects the
|
|
|
|
Raft quorum. If the server still shows in the output of the "nomad
|
|
|
|
server-members" command, it is preferable to clean up by simply running "nomad
|
|
|
|
server-force-leave" instead of this command.
|
2017-02-10 01:58:20 +00:00
|
|
|
|
2020-11-19 21:38:08 +00:00
|
|
|
If ACLs are enabled, this command requires a management token.
|
|
|
|
|
2017-02-10 01:58:20 +00:00
|
|
|
General Options:
|
|
|
|
|
2020-11-19 16:15:23 +00:00
|
|
|
` + generalOptionsUsage(usageOptsDefault|usageOptsNoNamespace) + `
|
2017-02-10 01:58:20 +00:00
|
|
|
|
|
|
|
Remove Peer Options:
|
|
|
|
|
|
|
|
-peer-address="IP:port"
|
2018-01-16 21:35:32 +00:00
|
|
|
Remove a Nomad server with given address from the Raft configuration.
|
2018-04-18 16:02:11 +00:00
|
|
|
|
2018-01-16 21:35:32 +00:00
|
|
|
-peer-id="id"
|
|
|
|
Remove a Nomad server with the given ID from the Raft configuration.
|
2017-02-10 01:58:20 +00:00
|
|
|
`
|
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|
|
|
|
|
2017-08-18 15:09:52 +00:00
|
|
|
func (c *OperatorRaftRemoveCommand) AutocompleteFlags() complete.Flags {
|
2017-08-23 21:56:21 +00:00
|
|
|
return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient),
|
|
|
|
complete.Flags{
|
|
|
|
"-peer-address": complete.PredictAnything,
|
2018-01-16 21:35:32 +00:00
|
|
|
"-peer-id": complete.PredictAnything,
|
2017-08-23 21:56:21 +00:00
|
|
|
})
|
2017-08-18 15:09:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *OperatorRaftRemoveCommand) AutocompleteArgs() complete.Predictor {
|
|
|
|
return complete.PredictNothing
|
|
|
|
}
|
|
|
|
|
2017-02-10 01:58:20 +00:00
|
|
|
func (c *OperatorRaftRemoveCommand) Synopsis() string {
|
|
|
|
return "Remove a Nomad server from the Raft configuration"
|
|
|
|
}
|
|
|
|
|
2018-04-18 16:02:11 +00:00
|
|
|
func (c *OperatorRaftRemoveCommand) Name() string { return "operator raft remove-peer" }
|
|
|
|
|
2017-02-10 01:58:20 +00:00
|
|
|
func (c *OperatorRaftRemoveCommand) Run(args []string) int {
|
|
|
|
var peerAddress string
|
2018-01-16 21:35:32 +00:00
|
|
|
var peerID string
|
2017-02-10 01:58:20 +00:00
|
|
|
|
|
|
|
flags := c.Meta.FlagSet("raft", FlagSetClient)
|
|
|
|
flags.Usage = func() { c.Ui.Output(c.Help()) }
|
|
|
|
|
|
|
|
flags.StringVar(&peerAddress, "peer-address", "", "")
|
2018-01-16 21:35:32 +00:00
|
|
|
flags.StringVar(&peerID, "peer-id", "", "")
|
2017-02-10 01:58:20 +00:00
|
|
|
if err := flags.Parse(args); err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Failed to parse args: %v", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set up a client.
|
|
|
|
client, err := c.Meta.Client()
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
operator := client.Operator()
|
|
|
|
|
2018-01-16 21:35:32 +00:00
|
|
|
if err := raftRemovePeers(peerAddress, peerID, operator); err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error removing peer: %v", err))
|
2017-02-10 01:58:20 +00:00
|
|
|
return 1
|
|
|
|
}
|
2018-01-16 21:35:32 +00:00
|
|
|
if peerAddress != "" {
|
|
|
|
c.Ui.Output(fmt.Sprintf("Removed peer with address %q", peerAddress))
|
|
|
|
} else {
|
|
|
|
c.Ui.Output(fmt.Sprintf("Removed peer with id %q", peerID))
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func raftRemovePeers(address, id string, operator *api.Operator) error {
|
|
|
|
if len(address) == 0 && len(id) == 0 {
|
|
|
|
return fmt.Errorf("an address or id is required for the peer to remove")
|
|
|
|
}
|
|
|
|
if len(address) > 0 && len(id) > 0 {
|
|
|
|
return fmt.Errorf("cannot give both an address and id")
|
|
|
|
}
|
2017-02-10 01:58:20 +00:00
|
|
|
|
|
|
|
// Try to kick the peer.
|
2018-01-16 21:35:32 +00:00
|
|
|
if len(address) > 0 {
|
|
|
|
if err := operator.RaftRemovePeerByAddress(address, nil); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err := operator.RaftRemovePeerByID(id, nil); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-02-10 01:58:20 +00:00
|
|
|
}
|
|
|
|
|
2018-01-16 21:35:32 +00:00
|
|
|
return nil
|
2017-02-10 01:58:20 +00:00
|
|
|
}
|