2015-09-13 00:09:03 +00:00
|
|
|
|
package command
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"strings"
|
2017-08-22 20:13:44 +00:00
|
|
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/api/contexts"
|
|
|
|
|
"github.com/posener/complete"
|
2015-09-13 00:09:03 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type NodeDrainCommand struct {
|
2015-09-14 20:13:52 +00:00
|
|
|
|
Meta
|
2015-09-13 00:09:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *NodeDrainCommand) Help() string {
|
|
|
|
|
helpText := `
|
|
|
|
|
Usage: nomad node-drain [options] <node>
|
|
|
|
|
|
|
|
|
|
Toggles node draining on a specified node. It is required
|
|
|
|
|
that either -enable or -disable is specified, but not both.
|
2016-04-11 22:20:49 +00:00
|
|
|
|
The -self flag is useful to drain the local node.
|
2015-09-13 00:09:03 +00:00
|
|
|
|
|
2015-09-14 20:13:52 +00:00
|
|
|
|
General Options:
|
|
|
|
|
|
|
|
|
|
` + generalOptionsUsage() + `
|
|
|
|
|
|
|
|
|
|
Node Drain Options:
|
2015-09-13 00:09:03 +00:00
|
|
|
|
|
|
|
|
|
-disable
|
|
|
|
|
Disable draining for the specified node.
|
|
|
|
|
|
|
|
|
|
-enable
|
|
|
|
|
Enable draining for the specified node.
|
2016-03-24 21:43:20 +00:00
|
|
|
|
|
2016-04-11 22:20:49 +00:00
|
|
|
|
-self
|
|
|
|
|
Query the status of the local node.
|
|
|
|
|
|
2016-03-24 21:43:20 +00:00
|
|
|
|
-yes
|
|
|
|
|
Automatic yes to prompts.
|
2015-09-13 00:09:03 +00:00
|
|
|
|
`
|
|
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *NodeDrainCommand) Synopsis() string {
|
2015-09-13 18:39:49 +00:00
|
|
|
|
return "Toggle drain mode on a given node"
|
2015-09-13 00:09:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-08-22 20:13:44 +00:00
|
|
|
|
func (c *NodeDrainCommand) AutocompleteFlags() complete.Flags {
|
2017-08-23 21:56:21 +00:00
|
|
|
|
return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient),
|
|
|
|
|
complete.Flags{
|
|
|
|
|
"-disable": complete.PredictNothing,
|
|
|
|
|
"-enable": complete.PredictNothing,
|
|
|
|
|
"-self": complete.PredictNothing,
|
|
|
|
|
"-yes": complete.PredictNothing,
|
|
|
|
|
})
|
2017-08-22 20:13:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *NodeDrainCommand) AutocompleteArgs() complete.Predictor {
|
|
|
|
|
return complete.PredictFunc(func(a complete.Args) []string {
|
2017-08-29 21:29:32 +00:00
|
|
|
|
client, err := c.Meta.Client()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-28 05:17:51 +00:00
|
|
|
|
resp, _, err := client.Search().PrefixSearch(a.Last, contexts.Nodes, nil)
|
2017-08-22 20:13:44 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return []string{}
|
|
|
|
|
}
|
|
|
|
|
return resp.Matches[contexts.Nodes]
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-13 00:09:03 +00:00
|
|
|
|
func (c *NodeDrainCommand) Run(args []string) int {
|
2016-04-11 22:20:49 +00:00
|
|
|
|
var enable, disable, self, autoYes bool
|
2015-09-13 00:09:03 +00:00
|
|
|
|
|
2015-09-14 20:13:52 +00:00
|
|
|
|
flags := c.Meta.FlagSet("node-drain", FlagSetClient)
|
2015-09-13 00:09:03 +00:00
|
|
|
|
flags.Usage = func() { c.Ui.Output(c.Help()) }
|
|
|
|
|
flags.BoolVar(&enable, "enable", false, "Enable drain mode")
|
|
|
|
|
flags.BoolVar(&disable, "disable", false, "Disable drain mode")
|
2016-04-11 22:20:49 +00:00
|
|
|
|
flags.BoolVar(&self, "self", false, "")
|
2016-03-24 21:43:20 +00:00
|
|
|
|
flags.BoolVar(&autoYes, "yes", false, "Automatic yes to prompts.")
|
2015-09-13 00:09:03 +00:00
|
|
|
|
|
|
|
|
|
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) {
|
|
|
|
|
c.Ui.Error(c.Help())
|
|
|
|
|
return 1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check that we got a node ID
|
2015-09-14 20:13:52 +00:00
|
|
|
|
args = flags.Args()
|
2016-04-11 22:20:49 +00:00
|
|
|
|
if l := len(args); self && l != 0 || !self && l != 1 {
|
2015-09-13 00:09:03 +00:00
|
|
|
|
c.Ui.Error(c.Help())
|
|
|
|
|
return 1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get the HTTP client
|
2015-09-14 20:13:52 +00:00
|
|
|
|
client, err := c.Meta.Client()
|
2015-09-13 00:09:03 +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-13 00:09:03 +00:00
|
|
|
|
return 1
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-11 22:20:49 +00:00
|
|
|
|
// If -self flag is set then determine the current node.
|
2017-09-26 22:26:33 +00:00
|
|
|
|
var nodeID string
|
2016-04-11 22:20:49 +00:00
|
|
|
|
if !self {
|
|
|
|
|
nodeID = args[0]
|
|
|
|
|
} else {
|
|
|
|
|
var err error
|
|
|
|
|
if nodeID, err = getLocalNodeID(client); err != nil {
|
|
|
|
|
c.Ui.Error(err.Error())
|
|
|
|
|
return 1
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-20 17:02:10 +00:00
|
|
|
|
// Check if node exists
|
2016-03-17 23:48:45 +00:00
|
|
|
|
if len(nodeID) == 1 {
|
|
|
|
|
c.Ui.Error(fmt.Sprintf("Identifier must contain at least two characters."))
|
|
|
|
|
return 1
|
|
|
|
|
}
|
2016-01-21 19:53:05 +00:00
|
|
|
|
|
2018-03-11 18:52:59 +00:00
|
|
|
|
nodeID = sanitizeUUIDPrefix(nodeID)
|
2016-03-17 23:48:45 +00:00
|
|
|
|
nodes, _, err := client.Nodes().PrefixList(nodeID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error toggling drain mode: %s", err))
|
|
|
|
|
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 {
|
|
|
|
|
// Format the nodes list that matches the prefix so that the user
|
|
|
|
|
// can create a more specific request
|
|
|
|
|
out := make([]string, len(nodes)+1)
|
|
|
|
|
out[0] = "ID|Datacenter|Name|Class|Drain|Status"
|
|
|
|
|
for i, node := range nodes {
|
|
|
|
|
out[i+1] = fmt.Sprintf("%s|%s|%s|%s|%v|%s",
|
|
|
|
|
node.ID,
|
|
|
|
|
node.Datacenter,
|
|
|
|
|
node.Name,
|
|
|
|
|
node.NodeClass,
|
|
|
|
|
node.Drain,
|
|
|
|
|
node.Status)
|
2015-12-24 10:46:59 +00:00
|
|
|
|
}
|
2016-03-17 23:48:45 +00:00
|
|
|
|
// Dump the output
|
2017-07-21 00:31:07 +00:00
|
|
|
|
c.Ui.Error(fmt.Sprintf("Prefix matched multiple nodes\n\n%s", formatList(out)))
|
|
|
|
|
return 1
|
2016-03-17 23:48:45 +00:00
|
|
|
|
}
|
2016-03-24 21:43:20 +00:00
|
|
|
|
|
2016-03-17 23:48:45 +00:00
|
|
|
|
// Prefix lookup matched a single node
|
|
|
|
|
node, _, err := client.Nodes().Info(nodes[0].ID, nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error toggling drain mode: %s", err))
|
|
|
|
|
return 1
|
2015-12-20 17:02:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-03-24 21:43:20 +00:00
|
|
|
|
// Confirm drain if the node was a prefix match.
|
|
|
|
|
if nodeID != node.ID && !autoYes {
|
|
|
|
|
verb := "enable"
|
|
|
|
|
if disable {
|
|
|
|
|
verb = "disable"
|
|
|
|
|
}
|
|
|
|
|
question := fmt.Sprintf("Are you sure you want to %s drain mode for node %q? [y/N]", verb, node.ID)
|
|
|
|
|
answer, err := c.Ui.Ask(question)
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.Ui.Error(fmt.Sprintf("Failed to parse answer: %v", err))
|
|
|
|
|
return 1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if answer == "" || strings.ToLower(answer)[0] == 'n' {
|
|
|
|
|
// No case
|
|
|
|
|
c.Ui.Output("Canceling drain toggle")
|
|
|
|
|
return 0
|
2016-03-28 02:17:29 +00:00
|
|
|
|
} else if strings.ToLower(answer)[0] == 'y' && len(answer) > 1 {
|
2016-03-24 21:43:20 +00:00
|
|
|
|
// Non exact match yes
|
|
|
|
|
c.Ui.Output("For confirmation, an exact ‘y’ is required.")
|
|
|
|
|
return 0
|
|
|
|
|
} else if answer != "y" {
|
|
|
|
|
c.Ui.Output("No confirmation detected. For confirmation, an exact 'y' is required.")
|
|
|
|
|
return 1
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-13 00:09:03 +00:00
|
|
|
|
// Toggle node draining
|
2015-12-20 17:02:10 +00:00
|
|
|
|
if _, err := client.Nodes().ToggleDrain(node.ID, enable, nil); err != nil {
|
2015-09-14 20:13:52 +00:00
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error toggling drain mode: %s", err))
|
2015-09-13 00:09:03 +00:00
|
|
|
|
return 1
|
|
|
|
|
}
|
|
|
|
|
return 0
|
|
|
|
|
}
|