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