root / scripts / gnt-node @ 9a033156
History | View | Annotate | Download (11.2 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 |
import sys |
23 |
from optparse import make_option |
24 |
|
25 |
from ganeti.cli import * |
26 |
from ganeti import opcodes |
27 |
from ganeti import logger |
28 |
from ganeti import utils |
29 |
from ganeti import constants |
30 |
from ganeti import errors |
31 |
|
32 |
|
33 |
_LIST_DEF_FIELDS = [ |
34 |
"name", "dtotal", "dfree", |
35 |
"mtotal", "mnode", "mfree", |
36 |
"pinst_cnt", "sinst_cnt", |
37 |
] |
38 |
|
39 |
def AddNode(opts, args): |
40 |
"""Add node cli-to-processor bridge.""" |
41 |
logger.ToStderr("-- WARNING -- \n" |
42 |
"Performing this operation is going to replace the ssh daemon keypair\n" |
43 |
"on the target machine (%s) with the ones of the current one\n" |
44 |
"and grant full intra-cluster ssh root access to/from it\n" % args[0]) |
45 |
op = opcodes.OpAddNode(node_name=args[0], secondary_ip=opts.secondary_ip, |
46 |
readd=opts.readd) |
47 |
SubmitOpCode(op) |
48 |
|
49 |
|
50 |
def ListNodes(opts, args): |
51 |
"""List nodes and their properties. |
52 |
|
53 |
""" |
54 |
if opts.output is None: |
55 |
selected_fields = _LIST_DEF_FIELDS |
56 |
elif opts.output.startswith("+"): |
57 |
selected_fields = _LIST_DEF_FIELDS + opts.output[1:].split(",") |
58 |
else: |
59 |
selected_fields = opts.output.split(",") |
60 |
|
61 |
op = opcodes.OpQueryNodes(output_fields=selected_fields, names=[]) |
62 |
output = SubmitOpCode(op) |
63 |
|
64 |
if not opts.no_headers: |
65 |
headers = { |
66 |
"name": "Node", "pinst_cnt": "Pinst", "sinst_cnt": "Sinst", |
67 |
"pinst_list": "PriInstances", "sinst_list": "SecInstances", |
68 |
"pip": "PrimaryIP", "sip": "SecondaryIP", |
69 |
"dtotal": "DTotal", "dfree": "DFree", |
70 |
"mtotal": "MTotal", "mnode": "MNode", "mfree": "MFree", |
71 |
"bootid": "BootID", |
72 |
"ctotal": "CTotal", |
73 |
} |
74 |
else: |
75 |
headers = None |
76 |
|
77 |
if opts.human_readable: |
78 |
unitfields = ["dtotal", "dfree", "mtotal", "mnode", "mfree"] |
79 |
else: |
80 |
unitfields = None |
81 |
|
82 |
numfields = ["dtotal", "dfree", |
83 |
"mtotal", "mnode", "mfree", |
84 |
"pinst_cnt", "sinst_cnt", |
85 |
"ctotal"] |
86 |
|
87 |
# change raw values to nicer strings |
88 |
for row in output: |
89 |
for idx, field in enumerate(selected_fields): |
90 |
val = row[idx] |
91 |
if field == "pinst_list": |
92 |
val = ",".join(val) |
93 |
elif field == "sinst_list": |
94 |
val = ",".join(val) |
95 |
elif val is None: |
96 |
val = "?" |
97 |
row[idx] = str(val) |
98 |
|
99 |
data = GenerateTable(separator=opts.separator, headers=headers, |
100 |
fields=selected_fields, unitfields=unitfields, |
101 |
numfields=numfields, data=output) |
102 |
for line in data: |
103 |
logger.ToStdout(line) |
104 |
|
105 |
return 0 |
106 |
|
107 |
|
108 |
def EvacuateNode(opts, args): |
109 |
"""Relocate all secondary instance from a node. |
110 |
|
111 |
""" |
112 |
force = opts.force |
113 |
selected_fields = ["name", "sinst_list"] |
114 |
src_node, dst_node = args |
115 |
|
116 |
op = opcodes.OpQueryNodes(output_fields=selected_fields, names=[src_node]) |
117 |
result = SubmitOpCode(op) |
118 |
src_node, sinst = result[0] |
119 |
op = opcodes.OpQueryNodes(output_fields=["name"], names=[dst_node]) |
120 |
result = SubmitOpCode(op) |
121 |
dst_node = result[0][0] |
122 |
|
123 |
if src_node == dst_node: |
124 |
raise errors.OpPrereqError("Evacuate node needs different source and" |
125 |
" target nodes (node %s given twice)" % |
126 |
src_node) |
127 |
|
128 |
if not sinst: |
129 |
logger.ToStderr("No secondary instances on node %s, exiting." % src_node) |
130 |
return constants.EXIT_SUCCESS |
131 |
|
132 |
sinst = utils.NiceSort(sinst) |
133 |
|
134 |
retcode = constants.EXIT_SUCCESS |
135 |
|
136 |
if not force and not AskUser("Relocate instance(s) %s from node\n" |
137 |
" %s to node\n %s?" % |
138 |
(",".join("'%s'" % name for name in sinst), |
139 |
src_node, dst_node)): |
140 |
return constants.EXIT_CONFIRMATION |
141 |
|
142 |
good_cnt = bad_cnt = 0 |
143 |
for iname in sinst: |
144 |
op = opcodes.OpReplaceDisks(instance_name=iname, |
145 |
remote_node=dst_node, |
146 |
mode=constants.REPLACE_DISK_ALL, |
147 |
disks=["sda", "sdb"]) |
148 |
try: |
149 |
logger.ToStdout("Replacing disks for instance %s" % iname) |
150 |
SubmitOpCode(op) |
151 |
logger.ToStdout("Instance %s has been relocated" % iname) |
152 |
good_cnt += 1 |
153 |
except errors.GenericError, err: |
154 |
nret, msg = FormatError(err) |
155 |
retcode |= nret |
156 |
logger.ToStderr("Error replacing disks for instance %s: %s" % |
157 |
(iname, msg)) |
158 |
bad_cnt += 1 |
159 |
|
160 |
if retcode == constants.EXIT_SUCCESS: |
161 |
logger.ToStdout("All %d instance(s) relocated successfully." % good_cnt) |
162 |
else: |
163 |
logger.ToStdout("There were errors during the relocation:\n" |
164 |
"%d error(s) out of %d instance(s)." % |
165 |
(bad_cnt, good_cnt + bad_cnt)) |
166 |
return retcode |
167 |
|
168 |
|
169 |
def FailoverNode(opts, args): |
170 |
"""Failover all primary instance on a node. |
171 |
|
172 |
""" |
173 |
force = opts.force |
174 |
selected_fields = ["name", "pinst_list"] |
175 |
|
176 |
op = opcodes.OpQueryNodes(output_fields=selected_fields, names=args) |
177 |
result = SubmitOpCode(op) |
178 |
node, pinst = result[0] |
179 |
|
180 |
if not pinst: |
181 |
logger.ToStderr("No primary instances on node %s, exiting." % node) |
182 |
return 0 |
183 |
|
184 |
pinst = utils.NiceSort(pinst) |
185 |
|
186 |
retcode = 0 |
187 |
|
188 |
if not force and not AskUser("Fail over instance(s) %s?" % |
189 |
(",".join("'%s'" % name for name in pinst))): |
190 |
return 2 |
191 |
|
192 |
good_cnt = bad_cnt = 0 |
193 |
for iname in pinst: |
194 |
op = opcodes.OpFailoverInstance(instance_name=iname, |
195 |
ignore_consistency=opts.ignore_consistency) |
196 |
try: |
197 |
logger.ToStdout("Failing over instance %s" % iname) |
198 |
SubmitOpCode(op) |
199 |
logger.ToStdout("Instance %s has been failed over" % iname) |
200 |
good_cnt += 1 |
201 |
except errors.GenericError, err: |
202 |
nret, msg = FormatError(err) |
203 |
retcode |= nret |
204 |
logger.ToStderr("Error failing over instance %s: %s" % (iname, msg)) |
205 |
bad_cnt += 1 |
206 |
|
207 |
if retcode == 0: |
208 |
logger.ToStdout("All %d instance(s) failed over successfully." % good_cnt) |
209 |
else: |
210 |
logger.ToStdout("There were errors during the failover:\n" |
211 |
"%d error(s) out of %d instance(s)." % |
212 |
(bad_cnt, good_cnt + bad_cnt)) |
213 |
return retcode |
214 |
|
215 |
|
216 |
def ShowNodeConfig(opts, args): |
217 |
"""Show node information. |
218 |
|
219 |
""" |
220 |
op = opcodes.OpQueryNodes(output_fields=["name", "pip", "sip", |
221 |
"pinst_list", "sinst_list"], |
222 |
names=args) |
223 |
result = SubmitOpCode(op) |
224 |
|
225 |
for name, primary_ip, secondary_ip, pinst, sinst in result: |
226 |
logger.ToStdout("Node name: %s" % name) |
227 |
logger.ToStdout(" primary ip: %s" % primary_ip) |
228 |
logger.ToStdout(" secondary ip: %s" % secondary_ip) |
229 |
if pinst: |
230 |
logger.ToStdout(" primary for instances:") |
231 |
for iname in pinst: |
232 |
logger.ToStdout(" - %s" % iname) |
233 |
else: |
234 |
logger.ToStdout(" primary for no instances") |
235 |
if sinst: |
236 |
logger.ToStdout(" secondary for instances:") |
237 |
for iname in sinst: |
238 |
logger.ToStdout(" - %s" % iname) |
239 |
else: |
240 |
logger.ToStdout(" secondary for no instances") |
241 |
|
242 |
return 0 |
243 |
|
244 |
|
245 |
def RemoveNode(opts, args): |
246 |
"""Remove node cli-to-processor bridge.""" |
247 |
op = opcodes.OpRemoveNode(node_name=args[0]) |
248 |
SubmitOpCode(op) |
249 |
|
250 |
|
251 |
def ListVolumes(opts, args): |
252 |
"""List logical volumes on node(s). |
253 |
|
254 |
""" |
255 |
if opts.output is None: |
256 |
selected_fields = ["node", "phys", "vg", |
257 |
"name", "size", "instance"] |
258 |
else: |
259 |
selected_fields = opts.output.split(",") |
260 |
|
261 |
op = opcodes.OpQueryNodeVolumes(nodes=args, output_fields=selected_fields) |
262 |
output = SubmitOpCode(op) |
263 |
|
264 |
if not opts.no_headers: |
265 |
headers = {"node": "Node", "phys": "PhysDev", |
266 |
"vg": "VG", "name": "Name", |
267 |
"size": "Size", "instance": "Instance"} |
268 |
else: |
269 |
headers = None |
270 |
|
271 |
if opts.human_readable: |
272 |
unitfields = ["size"] |
273 |
else: |
274 |
unitfields = None |
275 |
|
276 |
numfields = ["size"] |
277 |
|
278 |
data = GenerateTable(separator=opts.separator, headers=headers, |
279 |
fields=selected_fields, unitfields=unitfields, |
280 |
numfields=numfields, data=output) |
281 |
|
282 |
for line in data: |
283 |
logger.ToStdout(line) |
284 |
|
285 |
return 0 |
286 |
|
287 |
|
288 |
commands = { |
289 |
'add': (AddNode, ARGS_ONE, |
290 |
[DEBUG_OPT, |
291 |
make_option("-s", "--secondary-ip", dest="secondary_ip", |
292 |
help="Specify the secondary ip for the node", |
293 |
metavar="ADDRESS", default=None), |
294 |
make_option("--readd", dest="readd", |
295 |
default=False, action="store_true", |
296 |
help="Readd old node after replacing it"), |
297 |
], |
298 |
"[-s ip] <node_name>", "Add a node to the cluster"), |
299 |
'evacuate': (EvacuateNode, ARGS_FIXED(2), |
300 |
[DEBUG_OPT, FORCE_OPT], |
301 |
"[-f] <src> <dst>", |
302 |
"Relocate the secondary instances from the first node" |
303 |
" to the second one (only for instances of type remote_raid1" |
304 |
" drbd)"), |
305 |
'failover': (FailoverNode, ARGS_ONE, |
306 |
[DEBUG_OPT, FORCE_OPT, |
307 |
make_option("--ignore-consistency", dest="ignore_consistency", |
308 |
action="store_true", default=False, |
309 |
help="Ignore the consistency of the disks on" |
310 |
" the secondary"), |
311 |
], |
312 |
"[-f] <node>", |
313 |
"Stops the primary instances on a node and start them on their" |
314 |
" secondary node (only for instances of type remote_raid1)"), |
315 |
'info': (ShowNodeConfig, ARGS_ANY, [DEBUG_OPT], |
316 |
"[<node_name>...]", "Show information about the node(s)"), |
317 |
'list': (ListNodes, ARGS_NONE, |
318 |
[DEBUG_OPT, NOHDR_OPT, SEP_OPT, USEUNITS_OPT, FIELDS_OPT], |
319 |
"", "Lists the nodes in the cluster. The available fields" |
320 |
" are (see the man page for details): name, pinst_cnt, pinst_list," |
321 |
" sinst_cnt, sinst_list, pip, sip, dtotal, dfree, mtotal, mnode," |
322 |
" mfree, bootid, cpu_count. The default field list is" |
323 |
" (in order): %s." % ", ".join(_LIST_DEF_FIELDS), |
324 |
), |
325 |
'remove': (RemoveNode, ARGS_ONE, [DEBUG_OPT], |
326 |
"<node_name>", "Removes a node from the cluster"), |
327 |
'volumes': (ListVolumes, ARGS_ANY, |
328 |
[DEBUG_OPT, NOHDR_OPT, SEP_OPT, USEUNITS_OPT, FIELDS_OPT], |
329 |
"[<node_name>...]", "List logical volumes on node(s)"), |
330 |
'list-tags': (ListTags, ARGS_ONE, [DEBUG_OPT], |
331 |
"<node_name>", "List the tags of the given node"), |
332 |
'add-tags': (AddTags, ARGS_ATLEAST(1), [DEBUG_OPT, TAG_SRC_OPT], |
333 |
"<node_name> tag...", "Add tags to the given node"), |
334 |
'remove-tags': (RemoveTags, ARGS_ATLEAST(1), [DEBUG_OPT, TAG_SRC_OPT], |
335 |
"<node_name> tag...", "Remove tags from the given node"), |
336 |
} |
337 |
|
338 |
|
339 |
if __name__ == '__main__': |
340 |
sys.exit(GenericMain(commands, override={"tag_type": constants.TAG_NODE})) |