2023-04-10 15:36:59 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2018-02-27 21:54:27 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/api/contexts"
|
|
|
|
"github.com/posener/complete"
|
|
|
|
)
|
|
|
|
|
|
|
|
type NodeEligibilityCommand struct {
|
|
|
|
Meta
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *NodeEligibilityCommand) Help() string {
|
|
|
|
helpText := `
|
|
|
|
Usage: nomad node eligibility [options] <node>
|
|
|
|
|
|
|
|
Toggles the nodes scheduling eligibility. When a node is marked as ineligible,
|
|
|
|
no new allocations will be placed on it but existing allocations will remain.
|
|
|
|
To remove existing allocations, use the node drain command.
|
|
|
|
|
|
|
|
It is required that either -enable or -disable is specified, but not both.
|
2018-02-27 22:43:35 +00:00
|
|
|
The -self flag is useful to set the scheduling eligibility of the local node.
|
2018-02-27 21:54:27 +00:00
|
|
|
|
2020-11-19 21:38:08 +00:00
|
|
|
If ACLs are enabled, this option requires a token with the 'node:write'
|
|
|
|
capability.
|
|
|
|
|
2018-02-27 21:54:27 +00:00
|
|
|
General Options:
|
|
|
|
|
2020-11-19 16:15:23 +00:00
|
|
|
` + generalOptionsUsage(usageOptsDefault|usageOptsNoNamespace) + `
|
2018-02-27 21:54:27 +00:00
|
|
|
|
|
|
|
Node Eligibility Options:
|
|
|
|
|
|
|
|
-disable
|
|
|
|
Mark the specified node as ineligible for new allocations.
|
|
|
|
|
|
|
|
-enable
|
|
|
|
Mark the specified node as eligible for new allocations.
|
|
|
|
|
|
|
|
-self
|
|
|
|
Set the eligibility of the local node.
|
|
|
|
`
|
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *NodeEligibilityCommand) Synopsis() string {
|
|
|
|
return "Toggle scheduling eligibility for a given node"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *NodeEligibilityCommand) AutocompleteFlags() complete.Flags {
|
|
|
|
return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient),
|
|
|
|
complete.Flags{
|
|
|
|
"-disable": complete.PredictNothing,
|
|
|
|
"-enable": complete.PredictNothing,
|
|
|
|
"-self": complete.PredictNothing,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *NodeEligibilityCommand) AutocompleteArgs() complete.Predictor {
|
|
|
|
return complete.PredictFunc(func(a complete.Args) []string {
|
|
|
|
client, err := c.Meta.Client()
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, _, err := client.Search().PrefixSearch(a.Last, contexts.Nodes, nil)
|
|
|
|
if err != nil {
|
|
|
|
return []string{}
|
|
|
|
}
|
|
|
|
return resp.Matches[contexts.Nodes]
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-08-29 14:37:58 +00:00
|
|
|
func (c *NodeEligibilityCommand) Name() string { return "node eligibility" }
|
2018-04-18 16:02:11 +00:00
|
|
|
|
2018-02-27 21:54:27 +00:00
|
|
|
func (c *NodeEligibilityCommand) Run(args []string) int {
|
|
|
|
var enable, disable, self bool
|
|
|
|
|
2018-04-18 16:02:11 +00:00
|
|
|
flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
|
2018-02-27 21:54:27 +00:00
|
|
|
flags.Usage = func() { c.Ui.Output(c.Help()) }
|
|
|
|
flags.BoolVar(&enable, "enable", false, "Mark node as eligibile for scheduling")
|
|
|
|
flags.BoolVar(&disable, "disable", false, "Mark node as ineligibile for scheduling")
|
|
|
|
flags.BoolVar(&self, "self", false, "")
|
|
|
|
|
|
|
|
if err := flags.Parse(args); err != nil {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that we got either enable or disable, but not both.
|
|
|
|
if (enable && disable) || (!enable && !disable) {
|
2020-01-02 22:42:27 +00:00
|
|
|
c.Ui.Error("Either the '-enable' or '-disable' flag must be set")
|
2018-04-18 16:02:11 +00:00
|
|
|
c.Ui.Error(commandErrorText(c))
|
2018-02-27 21:54:27 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that we got a node ID
|
|
|
|
args = flags.Args()
|
|
|
|
if l := len(args); self && l != 0 || !self && l != 1 {
|
|
|
|
c.Ui.Error("Node ID must be specified if -self isn't being used")
|
2018-04-18 16:02:11 +00:00
|
|
|
c.Ui.Error(commandErrorText(c))
|
2018-02-27 21:54:27 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the HTTP client
|
|
|
|
client, err := c.Meta.Client()
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// If -self flag is set then determine the current node.
|
|
|
|
var nodeID string
|
|
|
|
if !self {
|
|
|
|
nodeID = args[0]
|
|
|
|
} else {
|
|
|
|
var err error
|
|
|
|
if nodeID, err = getLocalNodeID(client); err != nil {
|
|
|
|
c.Ui.Error(err.Error())
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if node exists
|
|
|
|
if len(nodeID) == 1 {
|
2020-12-09 19:05:18 +00:00
|
|
|
c.Ui.Error("Identifier must contain at least two characters.")
|
2018-02-27 21:54:27 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2018-02-27 22:46:40 +00:00
|
|
|
nodeID = sanitizeUUIDPrefix(nodeID)
|
2018-02-27 21:54:27 +00:00
|
|
|
nodes, _, err := client.Nodes().PrefixList(nodeID)
|
|
|
|
if err != nil {
|
2018-02-27 22:43:35 +00:00
|
|
|
c.Ui.Error(fmt.Sprintf("Error updating scheduling eligibility: %s", err))
|
2018-02-27 21:54:27 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
// Return error if no nodes are found
|
|
|
|
if len(nodes) == 0 {
|
|
|
|
c.Ui.Error(fmt.Sprintf("No node(s) with prefix or id %q found", nodeID))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
if len(nodes) > 1 {
|
2018-02-27 22:43:35 +00:00
|
|
|
c.Ui.Error(fmt.Sprintf("Prefix matched multiple nodes\n\n%s",
|
|
|
|
formatNodeStubList(nodes, true)))
|
2018-02-27 21:54:27 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prefix lookup matched a single node
|
|
|
|
node, _, err := client.Nodes().Info(nodes[0].ID, nil)
|
|
|
|
if err != nil {
|
2018-02-27 22:43:35 +00:00
|
|
|
c.Ui.Error(fmt.Sprintf("Error updating scheduling eligibility: %s", err))
|
2018-02-27 21:54:27 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Toggle node eligibility
|
|
|
|
if _, err := client.Nodes().ToggleEligibility(node.ID, enable, nil); err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error updating scheduling eligibility: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2018-03-30 00:05:06 +00:00
|
|
|
if enable {
|
|
|
|
c.Ui.Output(fmt.Sprintf("Node %q scheduling eligibility set: eligible for scheduling", node.ID))
|
|
|
|
} else {
|
|
|
|
c.Ui.Output(fmt.Sprintf("Node %q scheduling eligibility set: ineligible for scheduling", node.ID))
|
|
|
|
}
|
2018-02-27 21:54:27 +00:00
|
|
|
return 0
|
|
|
|
}
|