consul/command/forceleave/forceleave.go

98 lines
2.2 KiB
Go
Raw Normal View History

// Copyright (c) HashiCorp, Inc.
[COMPLIANCE] License changes (#18443) * Adding explicit MPL license for sub-package This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Adding explicit MPL license for sub-package This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Updating the license from MPL to Business Source License Going forward, this project will be licensed under the Business Source License v1.1. Please see our blog post for more details at <Blog URL>, FAQ at www.hashicorp.com/licensing-faq, and details of the license at www.hashicorp.com/bsl. * add missing license headers * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 --------- Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com>
2023-08-11 13:12:13 +00:00
// SPDX-License-Identifier: BUSL-1.1
package forceleave
2013-12-31 21:06:33 +00:00
import (
"flag"
2013-12-31 21:06:33 +00:00
"fmt"
"github.com/mitchellh/cli"
"github.com/hashicorp/consul/api"
"github.com/hashicorp/consul/command/flags"
2013-12-31 21:06:33 +00:00
)
func New(ui cli.Ui) *cmd {
c := &cmd{UI: ui}
c.init()
return c
}
type cmd struct {
UI cli.Ui
flags *flag.FlagSet
http *flags.HTTPFlags
help string
// flags
prune bool
wan bool
2013-12-31 21:06:33 +00:00
}
func (c *cmd) init() {
c.flags = flag.NewFlagSet("", flag.ContinueOnError)
c.flags.BoolVar(&c.prune, "prune", false,
"Remove agent completely from list of members")
c.flags.BoolVar(&c.wan, "wan", false,
"Exclusively leave the agent from the WAN serf pool.")
c.http = &flags.HTTPFlags{}
flags.Merge(c.flags, c.http.ClientFlags())
flags.Merge(c.flags, c.http.PartitionFlag())
c.help = flags.Usage(help, c.flags)
}
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
}
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
}
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
}
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
}
func (c *cmd) Synopsis() string {
return synopsis
}
func (c *cmd) Help() string {
return c.help
}
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
time before eventually reaping them.
-prune Remove agent completely from list of members
-wan Exclusively leave the agent from the WAN serf pool.
`