2015-09-13 00:09:03 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
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.
|
|
|
|
`
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func (c *NodeDrainCommand) Run(args []string) int {
|
|
|
|
var enable, disable bool
|
|
|
|
|
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")
|
|
|
|
|
|
|
|
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()
|
|
|
|
if len(args) != 1 {
|
2015-09-13 00:09:03 +00:00
|
|
|
c.Ui.Error(c.Help())
|
|
|
|
return 1
|
|
|
|
}
|
2015-09-14 20:13:52 +00:00
|
|
|
nodeID := args[0]
|
2015-09-13 00:09:03 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
// Toggle node draining
|
|
|
|
if _, err := client.Nodes().ToggleDrain(nodeID, 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
|
|
|
|
}
|