2023-03-28 19:12:30 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2023-08-11 13:12:13 +00:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-03-28 19:12:30 +00:00
|
|
|
|
2017-10-11 13:47:35 +00:00
|
|
|
package forceleave
|
2013-12-31 21:06:33 +00:00
|
|
|
|
|
|
|
import (
|
2017-10-11 13:47:35 +00:00
|
|
|
"flag"
|
2013-12-31 21:06:33 +00:00
|
|
|
"fmt"
|
2017-10-11 13:47:35 +00:00
|
|
|
|
|
|
|
"github.com/mitchellh/cli"
|
2021-10-28 21:44:38 +00:00
|
|
|
|
2021-12-02 23:15:10 +00:00
|
|
|
"github.com/hashicorp/consul/api"
|
2021-10-28 21:44:38 +00:00
|
|
|
"github.com/hashicorp/consul/command/flags"
|
2013-12-31 21:06:33 +00:00
|
|
|
)
|
|
|
|
|
2017-10-11 13:47:35 +00:00
|
|
|
func New(ui cli.Ui) *cmd {
|
|
|
|
c := &cmd{UI: ui}
|
2017-10-11 21:44:01 +00:00
|
|
|
c.init()
|
2017-10-11 13:47:35 +00:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
type cmd struct {
|
|
|
|
UI cli.Ui
|
|
|
|
flags *flag.FlagSet
|
|
|
|
http *flags.HTTPFlags
|
2017-10-17 13:44:20 +00:00
|
|
|
help string
|
2019-10-04 21:10:02 +00:00
|
|
|
|
2021-12-02 23:15:10 +00:00
|
|
|
// flags
|
2019-10-04 21:10:02 +00:00
|
|
|
prune bool
|
2021-12-02 23:15:10 +00:00
|
|
|
wan bool
|
2013-12-31 21:06:33 +00:00
|
|
|
}
|
|
|
|
|
2017-10-11 21:44:01 +00:00
|
|
|
func (c *cmd) init() {
|
2017-10-11 13:47:35 +00:00
|
|
|
c.flags = flag.NewFlagSet("", flag.ContinueOnError)
|
2019-10-04 21:10:02 +00:00
|
|
|
c.flags.BoolVar(&c.prune, "prune", false,
|
|
|
|
"Remove agent completely from list of members")
|
2021-12-02 23:15:10 +00:00
|
|
|
c.flags.BoolVar(&c.wan, "wan", false,
|
|
|
|
"Exclusively leave the agent from the WAN serf pool.")
|
2017-10-11 13:47:35 +00:00
|
|
|
c.http = &flags.HTTPFlags{}
|
|
|
|
flags.Merge(c.flags, c.http.ClientFlags())
|
2021-10-28 21:44:38 +00:00
|
|
|
flags.Merge(c.flags, c.http.PartitionFlag())
|
2017-10-17 22:00:01 +00:00
|
|
|
c.help = flags.Usage(help, c.flags)
|
2017-10-11 13:47:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cmd) Run(args []string) int {
|
|
|
|
if err := c.flags.Parse(args); err != nil {
|
2013-12-31 21:06:33 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2017-10-11 13:47:35 +00:00
|
|
|
nodes := c.flags.Args()
|
2013-12-31 21:06:33 +00:00
|
|
|
if len(nodes) != 1 {
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Error("A single node name must be specified to force leave.")
|
|
|
|
c.UI.Error("")
|
|
|
|
c.UI.Error(c.Help())
|
2013-12-31 21:06:33 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2017-10-11 13:47:35 +00:00
|
|
|
client, err := c.http.APIClient()
|
2013-12-31 21:06:33 +00:00
|
|
|
if err != nil {
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Error(fmt.Sprintf("Error connecting to Consul agent: %s", err))
|
2013-12-31 21:06:33 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2021-12-02 23:15:10 +00:00
|
|
|
err = client.Agent().ForceLeaveOpts(nodes[0], api.ForceLeaveOpts{
|
|
|
|
Prune: c.prune,
|
|
|
|
WAN: c.wan,
|
|
|
|
})
|
2013-12-31 21:06:33 +00:00
|
|
|
if err != nil {
|
2017-04-21 00:02:42 +00:00
|
|
|
c.UI.Error(fmt.Sprintf("Error force leaving: %s", err))
|
2013-12-31 21:06:33 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2017-10-11 13:47:35 +00:00
|
|
|
func (c *cmd) Synopsis() string {
|
2017-10-17 13:44:20 +00:00
|
|
|
return synopsis
|
2017-10-11 13:47:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cmd) Help() string {
|
2017-10-17 13:44:20 +00:00
|
|
|
return c.help
|
2017-10-11 21:44:01 +00:00
|
|
|
}
|
|
|
|
|
2017-10-17 13:44:20 +00:00
|
|
|
const synopsis = "Forces a member of the cluster to enter the \"left\" state"
|
|
|
|
const help = `
|
|
|
|
Usage: consul force-leave [options] name
|
2013-12-31 21:06:33 +00:00
|
|
|
|
|
|
|
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
|
2017-10-17 13:44:20 +00:00
|
|
|
time before eventually reaping them.
|
2019-10-04 21:10:02 +00:00
|
|
|
|
|
|
|
-prune Remove agent completely from list of members
|
2021-12-02 23:15:10 +00:00
|
|
|
-wan Exclusively leave the agent from the WAN serf pool.
|
2017-10-17 13:44:20 +00:00
|
|
|
`
|