1package forceleave
2
3import (
4	"flag"
5	"fmt"
6
7	"github.com/hashicorp/consul/command/flags"
8	"github.com/mitchellh/cli"
9)
10
11func New(ui cli.Ui) *cmd {
12	c := &cmd{UI: ui}
13	c.init()
14	return c
15}
16
17type cmd struct {
18	UI    cli.Ui
19	flags *flag.FlagSet
20	http  *flags.HTTPFlags
21	help  string
22
23	//flags
24	prune bool
25}
26
27func (c *cmd) init() {
28	c.flags = flag.NewFlagSet("", flag.ContinueOnError)
29	c.flags.BoolVar(&c.prune, "prune", false,
30		"Remove agent completely from list of members")
31	c.http = &flags.HTTPFlags{}
32	flags.Merge(c.flags, c.http.ClientFlags())
33	c.help = flags.Usage(help, c.flags)
34}
35
36func (c *cmd) Run(args []string) int {
37	if err := c.flags.Parse(args); err != nil {
38		return 1
39	}
40
41	nodes := c.flags.Args()
42	if len(nodes) != 1 {
43		c.UI.Error("A single node name must be specified to force leave.")
44		c.UI.Error("")
45		c.UI.Error(c.Help())
46		return 1
47	}
48
49	client, err := c.http.APIClient()
50	if err != nil {
51		c.UI.Error(fmt.Sprintf("Error connecting to Consul agent: %s", err))
52		return 1
53	}
54
55	if c.prune {
56		err = client.Agent().ForceLeavePrune(nodes[0])
57	} else {
58		err = client.Agent().ForceLeave(nodes[0])
59	}
60
61	if err != nil {
62		c.UI.Error(fmt.Sprintf("Error force leaving: %s", err))
63		return 1
64	}
65
66	return 0
67}
68
69func (c *cmd) Synopsis() string {
70	return synopsis
71}
72
73func (c *cmd) Help() string {
74	return c.help
75}
76
77const synopsis = "Forces a member of the cluster to enter the \"left\" state"
78const help = `
79Usage: consul force-leave [options] name
80
81  Forces a member of a Consul cluster to enter the "left" state. Note
82  that if the member is still actually alive, it will eventually rejoin
83  the cluster. This command is most useful for cleaning out "failed" nodes
84  that are never coming back. If you do not force leave a failed node,
85  Consul will attempt to reconnect to those failed nodes for some period of
86  time before eventually reaping them.
87
88  -prune    Remove agent completely from list of members
89`
90