2013-12-31 21:06:33 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-02-09 21:41:17 +00:00
|
|
|
"github.com/hashicorp/consul/command/base"
|
2013-12-31 21:06:33 +00:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// LeaveCommand is a Command implementation that instructs
|
|
|
|
// the Consul agent to gracefully leave the cluster
|
|
|
|
type LeaveCommand struct {
|
2017-02-09 21:41:17 +00:00
|
|
|
base.Command
|
2013-12-31 21:06:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *LeaveCommand) Help() string {
|
|
|
|
helpText := `
|
2017-02-09 21:41:17 +00:00
|
|
|
Usage: consul leave [options]
|
2013-12-31 21:06:33 +00:00
|
|
|
|
|
|
|
Causes the agent to gracefully leave the Consul cluster and shutdown.
|
|
|
|
|
2017-02-09 21:41:17 +00:00
|
|
|
` + c.Command.Help()
|
2013-12-31 21:06:33 +00:00
|
|
|
|
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *LeaveCommand) Run(args []string) int {
|
2017-02-09 21:41:17 +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-09 21:41:17 +00:00
|
|
|
nonFlagArgs := f.Args()
|
2016-07-23 14:36:59 +00:00
|
|
|
if len(nonFlagArgs) > 0 {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error found unexpected args: %v", nonFlagArgs))
|
|
|
|
c.Ui.Output(c.Help())
|
|
|
|
return 1
|
|
|
|
}
|
2013-12-31 21:06:33 +00:00
|
|
|
|
2017-02-09 21:41:17 +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-09 21:41:17 +00:00
|
|
|
if err := client.Agent().Leave(); err != nil {
|
2013-12-31 21:06:33 +00:00
|
|
|
c.Ui.Error(fmt.Sprintf("Error leaving: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Ui.Output("Graceful leave complete")
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *LeaveCommand) Synopsis() string {
|
|
|
|
return "Gracefully leaves the Consul cluster and shuts down"
|
|
|
|
}
|