github.com/hernad/nomad@v1.6.112/command/server_force_leave.go (about)

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