forked from hashicorp/consul
-
Notifications
You must be signed in to change notification settings - Fork 0
/
operator_raft_remove.go
95 lines (77 loc) · 2.4 KB
/
operator_raft_remove.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package command
import (
"flag"
"fmt"
"strings"
"github.com/hashicorp/consul/api"
)
type OperatorRaftRemoveCommand struct {
BaseCommand
}
func (c *OperatorRaftRemoveCommand) Help() string {
helpText := `
Usage: consul operator raft remove-peer [options]
Remove the Consul server with given -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 "consul members" command,
it is preferable to clean up by simply running "consul force-leave" instead of
this command.
` + c.BaseCommand.Help()
return strings.TrimSpace(helpText)
}
func (c *OperatorRaftRemoveCommand) Synopsis() string {
return "Remove a Consul server from the Raft configuration"
}
func (c *OperatorRaftRemoveCommand) Run(args []string) int {
f := c.BaseCommand.NewFlagSet(c)
var address, id string
f.StringVar(&address, "address", "",
"The address to remove from the Raft configuration.")
f.StringVar(&id, "id", "",
"The ID to remove from the Raft configuration.")
if err := c.BaseCommand.Parse(args); err != nil {
if err == flag.ErrHelp {
return 0
}
c.UI.Error(fmt.Sprintf("Failed to parse args: %v", err))
return 1
}
// Set up a client.
client, err := c.BaseCommand.HTTPClient()
if err != nil {
c.UI.Error(fmt.Sprintf("Error initializing client: %s", err))
return 1
}
// Fetch the current configuration.
if err := raftRemovePeers(address, id, client.Operator()); err != nil {
c.UI.Error(fmt.Sprintf("Error removing peer: %v", err))
return 1
}
if address != "" {
c.UI.Output(fmt.Sprintf("Removed peer with address %q", address))
} else {
c.UI.Output(fmt.Sprintf("Removed peer with id %q", id))
}
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")
}
// Try to kick the peer.
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
}
}
return nil
}