forked from hashicorp/consul
-
Notifications
You must be signed in to change notification settings - Fork 0
/
operator_raft.go
99 lines (80 loc) · 2.59 KB
/
operator_raft.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
96
97
98
99
package command
import (
"flag"
"fmt"
"strings"
"github.com/hashicorp/consul/command/base"
)
type OperatorRaftCommand struct {
base.Command
}
func (c *OperatorRaftCommand) Help() string {
helpText := `
Usage: consul operator raft <subcommand> [options]
The Raft operator command is used to interact with Consul's Raft subsystem. The
command can be used to verify Raft peers or in rare cases to recover quorum by
removing invalid peers.
Subcommands:
list-peers Display the current Raft peer configuration
remove-peer Remove a Consul server from the Raft configuration
`
return strings.TrimSpace(helpText)
}
func (c *OperatorRaftCommand) Synopsis() string {
return "Provides cluster-level tools for Consul operators"
}
func (c *OperatorRaftCommand) Run(args []string) int {
if result := c.raft(args); result != nil {
c.UI.Error(result.Error())
return 1
}
return 0
}
// raft handles the raft subcommands.
func (c *OperatorRaftCommand) raft(args []string) error {
f := c.Command.NewFlagSet(c)
// Parse verb arguments.
var listPeers, removePeer bool
f.BoolVar(&listPeers, "list-peers", false,
"If this flag is provided, the current Raft peer configuration will be "+
"displayed. If the cluster is in an outage state without a leader, you may need "+
"to set -stale to 'true' to get the configuration from a non-leader server.")
f.BoolVar(&removePeer, "remove-peer", false,
"If this flag is provided, the Consul server with the given -address will be "+
"removed from the Raft configuration.")
// Parse other arguments.
var address string
f.StringVar(&address, "address", "",
"The address to remove from the Raft configuration.")
// Leave these flags for backwards compatibility, but hide them
// TODO: remove flags/behavior from this command in Consul 0.9
c.Command.HideFlags("list-peers", "remove-peer", "address")
if err := c.Command.Parse(args); err != nil {
if err == flag.ErrHelp {
return nil
}
return err
}
// Set up a client.
client, err := c.Command.HTTPClient()
if err != nil {
return fmt.Errorf("error connecting to Consul agent: %s", err)
}
// Dispatch based on the verb argument.
if listPeers {
result, err := raftListPeers(client, c.Command.HTTPStale())
if err != nil {
c.UI.Error(fmt.Sprintf("Error getting peers: %v", err))
}
c.UI.Output(result)
} else if removePeer {
if err := raftRemovePeers(address, "", client.Operator()); err != nil {
return fmt.Errorf("Error removing peer: %v", err)
}
c.UI.Output(fmt.Sprintf("Removed peer with address %q", address))
} else {
c.UI.Output(c.Help())
return nil
}
return nil
}