2023-04-10 15:36:59 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2015-09-11 19:31:13 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
2018-05-11 19:19:16 +00:00
|
|
|
|
2023-09-15 13:16:38 +00:00
|
|
|
"github.com/hashicorp/nomad/api"
|
2018-05-11 19:19:16 +00:00
|
|
|
"github.com/posener/complete"
|
2015-09-11 19:31:13 +00:00
|
|
|
)
|
|
|
|
|
2015-09-22 18:01:41 +00:00
|
|
|
type ServerForceLeaveCommand struct {
|
2015-09-14 20:13:52 +00:00
|
|
|
Meta
|
2015-09-11 19:31:13 +00:00
|
|
|
}
|
|
|
|
|
2015-09-22 18:01:41 +00:00
|
|
|
func (c *ServerForceLeaveCommand) Help() string {
|
2015-09-11 19:31:13 +00:00
|
|
|
helpText := `
|
2018-03-21 00:37:28 +00:00
|
|
|
Usage: nomad server force-leave [options] <node>
|
2015-09-11 19:31:13 +00:00
|
|
|
|
2015-09-22 18:01:41 +00:00
|
|
|
Forces an server to enter the "left" state. This can be used to
|
2015-09-11 19:31:13 +00:00
|
|
|
eject nodes which have failed and will not rejoin the cluster.
|
|
|
|
Note that if the member is actually still alive, it will
|
2023-09-15 13:16:38 +00:00
|
|
|
eventually rejoin the cluster again. The failed or left server will
|
|
|
|
be garbage collected after 24h.
|
2015-09-11 19:31:13 +00:00
|
|
|
|
2020-11-19 21:38:08 +00:00
|
|
|
If ACLs are enabled, this option requires a token with the 'agent:write'
|
|
|
|
capability.
|
|
|
|
|
2015-09-14 20:13:52 +00:00
|
|
|
General Options:
|
2015-09-11 19:31:13 +00:00
|
|
|
|
2023-09-15 13:16:38 +00:00
|
|
|
` + generalOptionsUsage(usageOptsDefault|usageOptsNoNamespace) + `
|
|
|
|
|
|
|
|
Server Force-Leave Options:
|
|
|
|
|
|
|
|
-prune
|
|
|
|
Removes failed or left server from the Serf member list immediately.
|
|
|
|
If member is actually still alive, it will eventually rejoin the cluster again.
|
|
|
|
`
|
2015-09-11 19:31:13 +00:00
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|
|
|
|
|
2015-09-22 18:01:41 +00:00
|
|
|
func (c *ServerForceLeaveCommand) Synopsis() string {
|
|
|
|
return "Force a server into the 'left' state"
|
2015-09-11 19:31:13 +00:00
|
|
|
}
|
|
|
|
|
2018-05-11 19:19:16 +00:00
|
|
|
func (c *ServerForceLeaveCommand) AutocompleteFlags() complete.Flags {
|
2023-09-15 13:16:38 +00:00
|
|
|
return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient),
|
|
|
|
complete.Flags{
|
|
|
|
"-prune": complete.PredictNothing,
|
|
|
|
})
|
2018-05-11 19:19:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ServerForceLeaveCommand) AutocompleteArgs() complete.Predictor {
|
|
|
|
return complete.PredictNothing
|
|
|
|
}
|
|
|
|
|
2018-04-18 16:02:11 +00:00
|
|
|
func (c *ServerForceLeaveCommand) Name() string { return "server force-leave" }
|
|
|
|
|
2015-09-22 18:01:41 +00:00
|
|
|
func (c *ServerForceLeaveCommand) Run(args []string) int {
|
2023-09-15 13:16:38 +00:00
|
|
|
var prune bool
|
2018-04-18 16:02:11 +00:00
|
|
|
flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
|
2015-09-11 19:31:13 +00:00
|
|
|
flags.Usage = func() { c.Ui.Output(c.Help()) }
|
2023-09-15 13:16:38 +00:00
|
|
|
flags.BoolVar(&prune, "prune", false, "Remove server completely from list of members")
|
|
|
|
|
2015-09-11 19:31:13 +00:00
|
|
|
if err := flags.Parse(args); err != nil {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that we got exactly one node
|
2015-09-14 20:13:52 +00:00
|
|
|
args = flags.Args()
|
|
|
|
if len(args) != 1 {
|
2018-04-18 17:55:51 +00:00
|
|
|
c.Ui.Error("This command takes one argument: <node>")
|
2018-04-18 16:02:11 +00:00
|
|
|
c.Ui.Error(commandErrorText(c))
|
2015-09-11 19:31:13 +00:00
|
|
|
return 1
|
|
|
|
}
|
2015-09-14 20:13:52 +00:00
|
|
|
node := args[0]
|
2015-09-11 19:31:13 +00:00
|
|
|
|
|
|
|
// Get the HTTP client
|
2015-09-14 20:13:52 +00:00
|
|
|
client, err := c.Meta.Client()
|
2015-09-11 19:31:13 +00:00
|
|
|
if err != nil {
|
2015-09-14 20:13:52 +00:00
|
|
|
c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
|
2015-09-11 19:31:13 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Call force-leave on the node
|
2023-09-15 13:16:38 +00:00
|
|
|
forceLeaveOpts := api.ForceLeaveOpts{
|
|
|
|
Prune: prune,
|
|
|
|
}
|
|
|
|
if err := client.Agent().ForceLeaveWithOptions(node, forceLeaveOpts); err != nil {
|
2015-09-22 18:01:41 +00:00
|
|
|
c.Ui.Error(fmt.Sprintf("Error force-leaving server %s: %s", node, err))
|
2015-09-11 19:31:13 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|