@@ -15,6 +15,7 @@ Usage: nomad node-drain [options] <node>
15
15
16
16
Toggles node draining on a specified node. It is required
17
17
that either -enable or -disable is specified, but not both.
18
+ The -self flag is useful to drain the local node.
18
19
19
20
General Options:
20
21
@@ -28,6 +29,9 @@ Node Drain Options:
28
29
-enable
29
30
Enable draining for the specified node.
30
31
32
+ -self
33
+ Query the status of the local node.
34
+
31
35
-yes
32
36
Automatic yes to prompts.
33
37
`
@@ -39,12 +43,13 @@ func (c *NodeDrainCommand) Synopsis() string {
39
43
}
40
44
41
45
func (c * NodeDrainCommand ) Run (args []string ) int {
42
- var enable , disable , autoYes bool
46
+ var enable , disable , self , autoYes bool
43
47
44
48
flags := c .Meta .FlagSet ("node-drain" , FlagSetClient )
45
49
flags .Usage = func () { c .Ui .Output (c .Help ()) }
46
50
flags .BoolVar (& enable , "enable" , false , "Enable drain mode" )
47
51
flags .BoolVar (& disable , "disable" , false , "Disable drain mode" )
52
+ flags .BoolVar (& self , "self" , false , "" )
48
53
flags .BoolVar (& autoYes , "yes" , false , "Automatic yes to prompts." )
49
54
50
55
if err := flags .Parse (args ); err != nil {
@@ -59,11 +64,10 @@ func (c *NodeDrainCommand) Run(args []string) int {
59
64
60
65
// Check that we got a node ID
61
66
args = flags .Args ()
62
- if len (args ) != 1 {
67
+ if l := len (args ); self && l != 0 || ! self && l != 1 {
63
68
c .Ui .Error (c .Help ())
64
69
return 1
65
70
}
66
- nodeID := args [0 ]
67
71
68
72
// Get the HTTP client
69
73
client , err := c .Meta .Client ()
@@ -72,6 +76,18 @@ func (c *NodeDrainCommand) Run(args []string) int {
72
76
return 1
73
77
}
74
78
79
+ // If -self flag is set then determine the current node.
80
+ nodeID := ""
81
+ if ! self {
82
+ nodeID = args [0 ]
83
+ } else {
84
+ var err error
85
+ if nodeID , err = getLocalNodeID (client ); err != nil {
86
+ c .Ui .Error (err .Error ())
87
+ return 1
88
+ }
89
+ }
90
+
75
91
// Check if node exists
76
92
if len (nodeID ) == 1 {
77
93
c .Ui .Error (fmt .Sprintf ("Identifier must contain at least two characters." ))
@@ -83,7 +99,6 @@ func (c *NodeDrainCommand) Run(args []string) int {
83
99
nodeID = nodeID [:len (nodeID )- 1 ]
84
100
}
85
101
86
- // Exact lookup failed, try with prefix based search
87
102
nodes , _ , err := client .Nodes ().PrefixList (nodeID )
88
103
if err != nil {
89
104
c .Ui .Error (fmt .Sprintf ("Error toggling drain mode: %s" , err ))
0 commit comments