2013-12-31 21:06:33 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-02-08 00:16:33 +00:00
|
|
|
"github.com/hashicorp/consul/command/base"
|
2013-12-31 21:06:33 +00:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ForceLeaveCommand is a Command implementation that tells a running Consul
|
|
|
|
// to force a member to enter the "left" state.
|
|
|
|
type ForceLeaveCommand struct {
|
2017-02-08 00:16:33 +00:00
|
|
|
base.Command
|
2013-12-31 21:06:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ForceLeaveCommand) Run(args []string) int {
|
2017-02-08 00:16:33 +00:00
|
|
|
f := c.Command.NewFlagSet(c)
|
|
|
|
if err := c.Command.Parse(args); err != nil {
|
2013-12-31 21:06:33 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2017-02-07 01:50:51 +00:00
|
|
|
nodes := f.Args()
|
2013-12-31 21:06:33 +00:00
|
|
|
if len(nodes) != 1 {
|
2017-02-07 01:50:51 +00:00
|
|
|
c.Ui.Error("A single node name must be specified to force leave.")
|
2013-12-31 21:06:33 +00:00
|
|
|
c.Ui.Error("")
|
|
|
|
c.Ui.Error(c.Help())
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2017-02-08 00:16:33 +00:00
|
|
|
client, err := c.Command.HTTPClient()
|
2013-12-31 21:06:33 +00:00
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error connecting to Consul agent: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2017-02-07 01:50:51 +00:00
|
|
|
err = client.Agent().ForceLeave(nodes[0])
|
2013-12-31 21:06:33 +00:00
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error force leaving: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ForceLeaveCommand) Synopsis() string {
|
|
|
|
return "Forces a member of the cluster to enter the \"left\" state"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ForceLeaveCommand) Help() string {
|
|
|
|
helpText := `
|
|
|
|
Usage: consul force-leave [options] name
|
|
|
|
|
|
|
|
Forces a member of a Consul cluster to enter the "left" state. Note
|
|
|
|
that if the member is still actually alive, it will eventually rejoin
|
|
|
|
the cluster. This command is most useful for cleaning out "failed" nodes
|
|
|
|
that are never coming back. If you do not force leave a failed node,
|
|
|
|
Consul will attempt to reconnect to those failed nodes for some period of
|
|
|
|
time before eventually reaping them.
|
|
|
|
|
2017-02-08 00:16:33 +00:00
|
|
|
` + c.Command.Help()
|
2013-12-31 21:06:33 +00:00
|
|
|
|
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|