root / scripts / gnt-node @ 66348f29
History | View | Annotate | Download (11.9 kB)
1 |
#!/usr/bin/python |
---|---|
2 |
# |
3 |
|
4 |
# Copyright (C) 2006, 2007, 2008 Google Inc. |
5 |
# |
6 |
# This program is free software; you can redistribute it and/or modify |
7 |
# it under the terms of the GNU General Public License as published by |
8 |
# the Free Software Foundation; either version 2 of the License, or |
9 |
# (at your option) any later version. |
10 |
# |
11 |
# This program is distributed in the hope that it will be useful, but |
12 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 |
# General Public License for more details. |
15 |
# |
16 |
# You should have received a copy of the GNU General Public License |
17 |
# along with this program; if not, write to the Free Software |
18 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
19 |
# 02110-1301, USA. |
20 |
|
21 |
|
22 |
# pylint: disable-msg=W0401,W0614 |
23 |
# W0401: Wildcard import ganeti.cli |
24 |
# W0614: Unused import %s from wildcard import (since we need cli) |
25 |
|
26 |
import sys |
27 |
from optparse import make_option |
28 |
|
29 |
from ganeti.cli import * |
30 |
from ganeti import opcodes |
31 |
from ganeti import utils |
32 |
from ganeti import constants |
33 |
from ganeti import errors |
34 |
from ganeti import bootstrap |
35 |
|
36 |
|
37 |
_LIST_DEF_FIELDS = [ |
38 |
"name", "dtotal", "dfree", |
39 |
"mtotal", "mnode", "mfree", |
40 |
"pinst_cnt", "sinst_cnt", |
41 |
] |
42 |
|
43 |
|
44 |
def AddNode(opts, args): |
45 |
"""Add node cli-to-processor bridge. |
46 |
|
47 |
""" |
48 |
dns_data = utils.HostInfo(args[0]) |
49 |
node = dns_data.name |
50 |
|
51 |
if not opts.readd: |
52 |
op = opcodes.OpQueryNodes(output_fields=['name'], names=[node]) |
53 |
try: |
54 |
output = SubmitOpCode(op) |
55 |
except (errors.OpPrereqError, errors.OpExecError): |
56 |
pass |
57 |
else: |
58 |
ToStderr("Node %s already in the cluster (as %s)" |
59 |
" - please use --readd", args[0], output[0][0]) |
60 |
return 1 |
61 |
|
62 |
ToStderr("-- WARNING -- \n" |
63 |
"Performing this operation is going to replace the ssh daemon" |
64 |
" keypair\n" |
65 |
"on the target machine (%s) with the ones of the" |
66 |
" current one\n" |
67 |
"and grant full intra-cluster ssh root access to/from it\n", node) |
68 |
|
69 |
bootstrap.SetupNodeDaemon(node, opts.ssh_key_check) |
70 |
|
71 |
op = opcodes.OpAddNode(node_name=args[0], secondary_ip=opts.secondary_ip, |
72 |
readd=opts.readd) |
73 |
SubmitOpCode(op) |
74 |
|
75 |
|
76 |
def ListNodes(opts, args): |
77 |
"""List nodes and their properties. |
78 |
|
79 |
""" |
80 |
if opts.output is None: |
81 |
selected_fields = _LIST_DEF_FIELDS |
82 |
elif opts.output.startswith("+"): |
83 |
selected_fields = _LIST_DEF_FIELDS + opts.output[1:].split(",") |
84 |
else: |
85 |
selected_fields = opts.output.split(",") |
86 |
|
87 |
output = GetClient().QueryNodes([], selected_fields) |
88 |
|
89 |
if not opts.no_headers: |
90 |
headers = { |
91 |
"name": "Node", "pinst_cnt": "Pinst", "sinst_cnt": "Sinst", |
92 |
"pinst_list": "PriInstances", "sinst_list": "SecInstances", |
93 |
"pip": "PrimaryIP", "sip": "SecondaryIP", |
94 |
"dtotal": "DTotal", "dfree": "DFree", |
95 |
"mtotal": "MTotal", "mnode": "MNode", "mfree": "MFree", |
96 |
"bootid": "BootID", |
97 |
"ctotal": "CTotal", |
98 |
"tags": "Tags", |
99 |
"serial_no": "SerialNo", |
100 |
} |
101 |
else: |
102 |
headers = None |
103 |
|
104 |
if opts.human_readable: |
105 |
unitfields = ["dtotal", "dfree", "mtotal", "mnode", "mfree"] |
106 |
else: |
107 |
unitfields = None |
108 |
|
109 |
numfields = ["dtotal", "dfree", |
110 |
"mtotal", "mnode", "mfree", |
111 |
"pinst_cnt", "sinst_cnt", |
112 |
"ctotal", "serial_no"] |
113 |
|
114 |
list_type_fields = ("pinst_list", "sinst_list", "tags") |
115 |
# change raw values to nicer strings |
116 |
for row in output: |
117 |
for idx, field in enumerate(selected_fields): |
118 |
val = row[idx] |
119 |
if field in list_type_fields: |
120 |
val = ",".join(val) |
121 |
elif val is None: |
122 |
val = "?" |
123 |
row[idx] = str(val) |
124 |
|
125 |
data = GenerateTable(separator=opts.separator, headers=headers, |
126 |
fields=selected_fields, unitfields=unitfields, |
127 |
numfields=numfields, data=output) |
128 |
for line in data: |
129 |
ToStdout(line) |
130 |
|
131 |
return 0 |
132 |
|
133 |
|
134 |
def EvacuateNode(opts, args): |
135 |
"""Relocate all secondary instance from a node. |
136 |
|
137 |
""" |
138 |
force = opts.force |
139 |
selected_fields = ["name", "sinst_list"] |
140 |
src_node, dst_node = args |
141 |
|
142 |
op = opcodes.OpQueryNodes(output_fields=selected_fields, names=[src_node]) |
143 |
result = SubmitOpCode(op) |
144 |
src_node, sinst = result[0] |
145 |
op = opcodes.OpQueryNodes(output_fields=["name"], names=[dst_node]) |
146 |
result = SubmitOpCode(op) |
147 |
dst_node = result[0][0] |
148 |
|
149 |
if src_node == dst_node: |
150 |
raise errors.OpPrereqError("Evacuate node needs different source and" |
151 |
" target nodes (node %s given twice)" % |
152 |
src_node) |
153 |
|
154 |
if not sinst: |
155 |
ToStderr("No secondary instances on node %s, exiting.", src_node) |
156 |
return constants.EXIT_SUCCESS |
157 |
|
158 |
sinst = utils.NiceSort(sinst) |
159 |
|
160 |
retcode = constants.EXIT_SUCCESS |
161 |
|
162 |
if not force and not AskUser("Relocate instance(s) %s from node\n" |
163 |
" %s to node\n %s?" % |
164 |
(",".join("'%s'" % name for name in sinst), |
165 |
src_node, dst_node)): |
166 |
return constants.EXIT_CONFIRMATION |
167 |
|
168 |
good_cnt = bad_cnt = 0 |
169 |
for iname in sinst: |
170 |
op = opcodes.OpReplaceDisks(instance_name=iname, |
171 |
remote_node=dst_node, |
172 |
mode=constants.REPLACE_DISK_ALL, |
173 |
disks=["sda", "sdb"]) |
174 |
try: |
175 |
ToStdout("Replacing disks for instance %s", iname) |
176 |
SubmitOpCode(op) |
177 |
ToStdout("Instance %s has been relocated", iname) |
178 |
good_cnt += 1 |
179 |
except errors.GenericError, err: |
180 |
nret, msg = FormatError(err) |
181 |
retcode |= nret |
182 |
ToStderr("Error replacing disks for instance %s: %s", iname, msg) |
183 |
bad_cnt += 1 |
184 |
|
185 |
if retcode == constants.EXIT_SUCCESS: |
186 |
ToStdout("All %d instance(s) relocated successfully.", good_cnt) |
187 |
else: |
188 |
ToStdout("There were errors during the relocation:\n" |
189 |
"%d error(s) out of %d instance(s).", bad_cnt, good_cnt + bad_cnt) |
190 |
return retcode |
191 |
|
192 |
|
193 |
def FailoverNode(opts, args): |
194 |
"""Failover all primary instance on a node. |
195 |
|
196 |
""" |
197 |
force = opts.force |
198 |
selected_fields = ["name", "pinst_list"] |
199 |
|
200 |
op = opcodes.OpQueryNodes(output_fields=selected_fields, names=args) |
201 |
result = SubmitOpCode(op) |
202 |
node, pinst = result[0] |
203 |
|
204 |
if not pinst: |
205 |
ToStderr("No primary instances on node %s, exiting.", node) |
206 |
return 0 |
207 |
|
208 |
pinst = utils.NiceSort(pinst) |
209 |
|
210 |
retcode = 0 |
211 |
|
212 |
if not force and not AskUser("Fail over instance(s) %s?" % |
213 |
(",".join("'%s'" % name for name in pinst))): |
214 |
return 2 |
215 |
|
216 |
good_cnt = bad_cnt = 0 |
217 |
for iname in pinst: |
218 |
op = opcodes.OpFailoverInstance(instance_name=iname, |
219 |
ignore_consistency=opts.ignore_consistency) |
220 |
try: |
221 |
ToStdout("Failing over instance %s", iname) |
222 |
SubmitOpCode(op) |
223 |
ToStdout("Instance %s has been failed over", iname) |
224 |
good_cnt += 1 |
225 |
except errors.GenericError, err: |
226 |
nret, msg = FormatError(err) |
227 |
retcode |= nret |
228 |
ToStderr("Error failing over instance %s: %s", iname, msg) |
229 |
bad_cnt += 1 |
230 |
|
231 |
if retcode == 0: |
232 |
ToStdout("All %d instance(s) failed over successfully.", good_cnt) |
233 |
else: |
234 |
ToStdout("There were errors during the failover:\n" |
235 |
"%d error(s) out of %d instance(s).", bad_cnt, good_cnt + bad_cnt) |
236 |
return retcode |
237 |
|
238 |
|
239 |
def ShowNodeConfig(opts, args): |
240 |
"""Show node information. |
241 |
|
242 |
""" |
243 |
op = opcodes.OpQueryNodes(output_fields=["name", "pip", "sip", |
244 |
"pinst_list", "sinst_list"], |
245 |
names=args) |
246 |
result = SubmitOpCode(op) |
247 |
|
248 |
for name, primary_ip, secondary_ip, pinst, sinst in result: |
249 |
ToStdout("Node name: %s", name) |
250 |
ToStdout(" primary ip: %s", primary_ip) |
251 |
ToStdout(" secondary ip: %s", secondary_ip) |
252 |
if pinst: |
253 |
ToStdout(" primary for instances:") |
254 |
for iname in pinst: |
255 |
ToStdout(" - %s", iname) |
256 |
else: |
257 |
ToStdout(" primary for no instances") |
258 |
if sinst: |
259 |
ToStdout(" secondary for instances:") |
260 |
for iname in sinst: |
261 |
ToStdout(" - %s", iname) |
262 |
else: |
263 |
ToStdout(" secondary for no instances") |
264 |
|
265 |
return 0 |
266 |
|
267 |
|
268 |
def RemoveNode(opts, args): |
269 |
"""Remove node cli-to-processor bridge.""" |
270 |
op = opcodes.OpRemoveNode(node_name=args[0]) |
271 |
SubmitOpCode(op) |
272 |
|
273 |
|
274 |
def ListVolumes(opts, args): |
275 |
"""List logical volumes on node(s). |
276 |
|
277 |
""" |
278 |
if opts.output is None: |
279 |
selected_fields = ["node", "phys", "vg", |
280 |
"name", "size", "instance"] |
281 |
else: |
282 |
selected_fields = opts.output.split(",") |
283 |
|
284 |
op = opcodes.OpQueryNodeVolumes(nodes=args, output_fields=selected_fields) |
285 |
output = SubmitOpCode(op) |
286 |
|
287 |
if not opts.no_headers: |
288 |
headers = {"node": "Node", "phys": "PhysDev", |
289 |
"vg": "VG", "name": "Name", |
290 |
"size": "Size", "instance": "Instance"} |
291 |
else: |
292 |
headers = None |
293 |
|
294 |
if opts.human_readable: |
295 |
unitfields = ["size"] |
296 |
else: |
297 |
unitfields = None |
298 |
|
299 |
numfields = ["size"] |
300 |
|
301 |
data = GenerateTable(separator=opts.separator, headers=headers, |
302 |
fields=selected_fields, unitfields=unitfields, |
303 |
numfields=numfields, data=output) |
304 |
|
305 |
for line in data: |
306 |
ToStdout(line) |
307 |
|
308 |
return 0 |
309 |
|
310 |
|
311 |
commands = { |
312 |
'add': (AddNode, ARGS_ONE, |
313 |
[DEBUG_OPT, |
314 |
make_option("-s", "--secondary-ip", dest="secondary_ip", |
315 |
help="Specify the secondary ip for the node", |
316 |
metavar="ADDRESS", default=None), |
317 |
make_option("--readd", dest="readd", |
318 |
default=False, action="store_true", |
319 |
help="Readd old node after replacing it"), |
320 |
make_option("--no-ssh-key-check", dest="ssh_key_check", |
321 |
default=True, action="store_false", |
322 |
help="Disable SSH key fingerprint checking"), |
323 |
], |
324 |
"[-s ip] [--readd] [--no-ssh-key-check] <node_name>", |
325 |
"Add a node to the cluster"), |
326 |
'evacuate': (EvacuateNode, ARGS_FIXED(2), |
327 |
[DEBUG_OPT, FORCE_OPT], |
328 |
"[-f] <src> <dst>", |
329 |
"Relocate the secondary instances from the first node" |
330 |
" to the second one (only for instances with drbd disk template" |
331 |
), |
332 |
'failover': (FailoverNode, ARGS_ONE, |
333 |
[DEBUG_OPT, FORCE_OPT, |
334 |
make_option("--ignore-consistency", dest="ignore_consistency", |
335 |
action="store_true", default=False, |
336 |
help="Ignore the consistency of the disks on" |
337 |
" the secondary"), |
338 |
], |
339 |
"[-f] <node>", |
340 |
"Stops the primary instances on a node and start them on their" |
341 |
" secondary node (only for instances with drbd disk template)"), |
342 |
'info': (ShowNodeConfig, ARGS_ANY, [DEBUG_OPT], |
343 |
"[<node_name>...]", "Show information about the node(s)"), |
344 |
'list': (ListNodes, ARGS_NONE, |
345 |
[DEBUG_OPT, NOHDR_OPT, SEP_OPT, USEUNITS_OPT, FIELDS_OPT, |
346 |
SUBMIT_OPT], |
347 |
"", "Lists the nodes in the cluster. The available fields" |
348 |
" are (see the man page for details): name, pinst_cnt, pinst_list," |
349 |
" sinst_cnt, sinst_list, pip, sip, dtotal, dfree, mtotal, mnode," |
350 |
" mfree, bootid, cpu_count, serial_no." |
351 |
" The default field list is" |
352 |
" (in order): %s." % ", ".join(_LIST_DEF_FIELDS), |
353 |
), |
354 |
'remove': (RemoveNode, ARGS_ONE, [DEBUG_OPT], |
355 |
"<node_name>", "Removes a node from the cluster"), |
356 |
'volumes': (ListVolumes, ARGS_ANY, |
357 |
[DEBUG_OPT, NOHDR_OPT, SEP_OPT, USEUNITS_OPT, FIELDS_OPT], |
358 |
"[<node_name>...]", "List logical volumes on node(s)"), |
359 |
'list-tags': (ListTags, ARGS_ONE, [DEBUG_OPT], |
360 |
"<node_name>", "List the tags of the given node"), |
361 |
'add-tags': (AddTags, ARGS_ATLEAST(1), [DEBUG_OPT, TAG_SRC_OPT], |
362 |
"<node_name> tag...", "Add tags to the given node"), |
363 |
'remove-tags': (RemoveTags, ARGS_ATLEAST(1), [DEBUG_OPT, TAG_SRC_OPT], |
364 |
"<node_name> tag...", "Remove tags from the given node"), |
365 |
} |
366 |
|
367 |
|
368 |
if __name__ == '__main__': |
369 |
sys.exit(GenericMain(commands, override={"tag_type": constants.TAG_NODE})) |