open-nomad/command/node_drain.go

78 lines
1.6 KiB
Go
Raw Normal View History

2015-09-13 00:09:03 +00:00
package command
import (
"fmt"
"strings"
)
type NodeDrainCommand struct {
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.
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
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
args = flags.Args()
if len(args) != 1 {
2015-09-13 00:09:03 +00:00
c.Ui.Error(c.Help())
return 1
}
nodeID := args[0]
2015-09-13 00:09:03 +00:00
// Get the HTTP client
client, err := c.Meta.Client()
2015-09-13 00:09:03 +00:00
if err != nil {
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 {
c.Ui.Error(fmt.Sprintf("Error toggling drain mode: %s", err))
2015-09-13 00:09:03 +00:00
return 1
}
return 0
}