github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/command/server_force_leave.go (about)

     1  package command
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/posener/complete"
     8  )
     9  
    10  type ServerForceLeaveCommand struct {
    11  	Meta
    12  }
    13  
    14  func (c *ServerForceLeaveCommand) Help() string {
    15  	helpText := `
    16  Usage: nomad server force-leave [options] <node>
    17  
    18    Forces an server to enter the "left" state. This can be used to
    19    eject nodes which have failed and will not rejoin the cluster.
    20    Note that if the member is actually still alive, it will
    21    eventually rejoin the cluster again.
    22  
    23    If ACLs are enabled, this option requires a token with the 'agent:write'
    24    capability.
    25  
    26  General Options:
    27  
    28    ` + generalOptionsUsage(usageOptsDefault|usageOptsNoNamespace)
    29  	return strings.TrimSpace(helpText)
    30  }
    31  
    32  func (c *ServerForceLeaveCommand) Synopsis() string {
    33  	return "Force a server into the 'left' state"
    34  }
    35  
    36  func (c *ServerForceLeaveCommand) AutocompleteFlags() complete.Flags {
    37  	return c.Meta.AutocompleteFlags(FlagSetClient)
    38  }
    39  
    40  func (c *ServerForceLeaveCommand) AutocompleteArgs() complete.Predictor {
    41  	return complete.PredictNothing
    42  }
    43  
    44  func (c *ServerForceLeaveCommand) Name() string { return "server force-leave" }
    45  
    46  func (c *ServerForceLeaveCommand) Run(args []string) int {
    47  	flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
    48  	flags.Usage = func() { c.Ui.Output(c.Help()) }
    49  	if err := flags.Parse(args); err != nil {
    50  		return 1
    51  	}
    52  
    53  	// Check that we got exactly one node
    54  	args = flags.Args()
    55  	if len(args) != 1 {
    56  		c.Ui.Error("This command takes one argument: <node>")
    57  		c.Ui.Error(commandErrorText(c))
    58  		return 1
    59  	}
    60  	node := args[0]
    61  
    62  	// Get the HTTP client
    63  	client, err := c.Meta.Client()
    64  	if err != nil {
    65  		c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
    66  		return 1
    67  	}
    68  
    69  	// Call force-leave on the node
    70  	if err := client.Agent().ForceLeave(node); err != nil {
    71  		c.Ui.Error(fmt.Sprintf("Error force-leaving server %s: %s", node, err))
    72  		return 1
    73  	}
    74  
    75  	return 0
    76  }