4 # Copyright (C) 2006, 2007, 2008 Google Inc.
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.
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.
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
22 # pylint: disable-msg=W0401,W0614
23 # W0401: Wildcard import ganeti.cli
24 # W0614: Unused import %s from wildcard import (since we need cli)
27 from optparse import make_option
29 from ganeti.cli import *
30 from ganeti import cli
31 from ganeti import opcodes
32 from ganeti import utils
33 from ganeti import constants
34 from ganeti import errors
35 from ganeti import bootstrap
38 #: default list of field for L{ListNodes}
40 "name", "dtotal", "dfree",
41 "mtotal", "mnode", "mfree",
42 "pinst_cnt", "sinst_cnt",
45 #: headers (and full field list for L{ListNodes}
47 "name": "Node", "pinst_cnt": "Pinst", "sinst_cnt": "Sinst",
48 "pinst_list": "PriInstances", "sinst_list": "SecInstances",
49 "pip": "PrimaryIP", "sip": "SecondaryIP",
50 "dtotal": "DTotal", "dfree": "DFree",
51 "mtotal": "MTotal", "mnode": "MNode", "mfree": "MFree",
53 "ctotal": "CTotal", "cnodes": "CNodes", "csockets": "CSockets",
55 "serial_no": "SerialNo",
56 "master_candidate": "MasterC",
58 "offline": "Offline", "drained": "Drained",
64 def AddNode(opts, args):
65 """Add a node to the cluster.
67 @param opts: the command line options selected by the user
69 @param args: should contain only one element, the new node name
71 @return: the desired exit code
75 dns_data = utils.HostInfo(args[0])
80 output = cl.QueryNodes(names=[node], fields=['name', 'sip'],
82 node_exists, sip = output[0]
83 except (errors.OpPrereqError, errors.OpExecError):
89 ToStderr("Node %s not in the cluster"
90 " - please retry without '--readd'", node)
94 ToStderr("Node %s already in the cluster (as %s)"
95 " - please retry with '--readd'", node, node_exists)
97 sip = opts.secondary_ip
99 # read the cluster name from the master
100 output = cl.QueryConfigValues(['cluster_name'])
101 cluster_name = output[0]
104 ToStderr("-- WARNING -- \n"
105 "Performing this operation is going to replace the ssh daemon"
107 "on the target machine (%s) with the ones of the"
109 "and grant full intra-cluster ssh root access to/from it\n", node)
111 bootstrap.SetupNodeDaemon(cluster_name, node, opts.ssh_key_check)
113 op = opcodes.OpAddNode(node_name=args[0], secondary_ip=sip,
118 def ListNodes(opts, args):
119 """List nodes and their properties.
121 @param opts: the command line options selected by the user
123 @param args: should be an empty list
125 @return: the desired exit code
128 if opts.output is None:
129 selected_fields = _LIST_DEF_FIELDS
130 elif opts.output.startswith("+"):
131 selected_fields = _LIST_DEF_FIELDS + opts.output[1:].split(",")
133 selected_fields = opts.output.split(",")
135 output = GetClient().QueryNodes(args, selected_fields, opts.do_locking)
137 if not opts.no_headers:
138 headers = _LIST_HEADERS
142 unitfields = ["dtotal", "dfree", "mtotal", "mnode", "mfree"]
144 numfields = ["dtotal", "dfree",
145 "mtotal", "mnode", "mfree",
146 "pinst_cnt", "sinst_cnt",
147 "ctotal", "serial_no"]
149 list_type_fields = ("pinst_list", "sinst_list", "tags")
150 # change raw values to nicer strings
152 for idx, field in enumerate(selected_fields):
154 if field in list_type_fields:
156 elif field in ('master', 'master_candidate', 'offline', 'drained'):
165 data = GenerateTable(separator=opts.separator, headers=headers,
166 fields=selected_fields, unitfields=unitfields,
167 numfields=numfields, data=output, units=opts.units)
174 def EvacuateNode(opts, args):
175 """Relocate all secondary instance from a node.
177 @param opts: the command line options selected by the user
179 @param args: should be an empty list
181 @return: the desired exit code
187 dst_node = opts.dst_node
188 iallocator = opts.iallocator
190 cnt = [dst_node, iallocator].count(None)
192 raise errors.OpPrereqError("One and only one of the -n and -I"
193 " options must be passed")
195 selected_fields = ["name", "sinst_list"]
198 result = cl.QueryNodes(names=[src_node], fields=selected_fields,
200 src_node, sinst = result[0]
203 ToStderr("No secondary instances on node %s, exiting.", src_node)
204 return constants.EXIT_SUCCESS
206 if dst_node is not None:
207 result = cl.QueryNodes(names=[dst_node], fields=["name"],
209 dst_node = result[0][0]
211 if src_node == dst_node:
212 raise errors.OpPrereqError("Evacuate node needs different source and"
213 " target nodes (node %s given twice)" %
215 txt_msg = "to node %s" % dst_node
217 txt_msg = "using iallocator %s" % iallocator
219 sinst = utils.NiceSort(sinst)
221 if not force and not AskUser("Relocate instance(s) %s from node\n"
223 (",".join("'%s'" % name for name in sinst),
225 return constants.EXIT_CONFIRMATION
229 op = opcodes.OpReplaceDisks(instance_name=iname,
230 remote_node=dst_node,
231 mode=constants.REPLACE_DISK_CHG,
232 iallocator=iallocator,
236 job_id = cli.SendJob(ops, cl=cl)
237 cli.PollJob(job_id, cl=cl)
240 def FailoverNode(opts, args):
241 """Failover all primary instance on a node.
243 @param opts: the command line options selected by the user
245 @param args: should be an empty list
247 @return: the desired exit code
252 selected_fields = ["name", "pinst_list"]
254 # these fields are static data anyway, so it doesn't matter, but
255 # locking=True should be safer
256 result = cl.QueryNodes(names=args, fields=selected_fields,
258 node, pinst = result[0]
261 ToStderr("No primary instances on node %s, exiting.", node)
264 pinst = utils.NiceSort(pinst)
268 if not force and not AskUser("Fail over instance(s) %s?" %
269 (",".join("'%s'" % name for name in pinst))):
272 jex = JobExecutor(cl=cl)
274 op = opcodes.OpFailoverInstance(instance_name=iname,
275 ignore_consistency=opts.ignore_consistency)
276 jex.QueueJob(iname, op)
277 results = jex.GetResults()
278 bad_cnt = len([row for row in results if not row[0]])
280 ToStdout("All %d instance(s) failed over successfully.", len(results))
282 ToStdout("There were errors during the failover:\n"
283 "%d error(s) out of %d instance(s).", bad_cnt, len(results))
287 def MigrateNode(opts, args):
288 """Migrate all primary instance on a node.
293 selected_fields = ["name", "pinst_list"]
295 result = cl.QueryNodes(names=args, fields=selected_fields, use_locking=False)
296 node, pinst = result[0]
299 ToStdout("No primary instances on node %s, exiting." % node)
302 pinst = utils.NiceSort(pinst)
306 if not force and not AskUser("Migrate instance(s) %s?" %
307 (",".join("'%s'" % name for name in pinst))):
310 jex = JobExecutor(cl=cl)
312 op = opcodes.OpMigrateInstance(instance_name=iname, live=opts.live,
314 jex.QueueJob(iname, op)
316 results = jex.GetResults()
317 bad_cnt = len([row for row in results if not row[0]])
319 ToStdout("All %d instance(s) migrated successfully.", len(results))
321 ToStdout("There were errors during the migration:\n"
322 "%d error(s) out of %d instance(s).", bad_cnt, len(results))
326 def ShowNodeConfig(opts, args):
327 """Show node information.
329 @param opts: the command line options selected by the user
331 @param args: should either be an empty list, in which case
332 we show information about all nodes, or should contain
333 a list of nodes to be queried for information
335 @return: the desired exit code
339 result = cl.QueryNodes(fields=["name", "pip", "sip",
340 "pinst_list", "sinst_list",
341 "master_candidate", "drained", "offline"],
342 names=args, use_locking=False)
344 for (name, primary_ip, secondary_ip, pinst, sinst,
345 is_mc, drained, offline) in result:
346 ToStdout("Node name: %s", name)
347 ToStdout(" primary ip: %s", primary_ip)
348 ToStdout(" secondary ip: %s", secondary_ip)
349 ToStdout(" master candidate: %s", is_mc)
350 ToStdout(" drained: %s", drained)
351 ToStdout(" offline: %s", offline)
353 ToStdout(" primary for instances:")
354 for iname in utils.NiceSort(pinst):
355 ToStdout(" - %s", iname)
357 ToStdout(" primary for no instances")
359 ToStdout(" secondary for instances:")
360 for iname in utils.NiceSort(sinst):
361 ToStdout(" - %s", iname)
363 ToStdout(" secondary for no instances")
368 def RemoveNode(opts, args):
369 """Remove a node from the cluster.
371 @param opts: the command line options selected by the user
373 @param args: should contain only one element, the name of
374 the node to be removed
376 @return: the desired exit code
379 op = opcodes.OpRemoveNode(node_name=args[0])
384 def ListVolumes(opts, args):
385 """List logical volumes on node(s).
387 @param opts: the command line options selected by the user
389 @param args: should either be an empty list, in which case
390 we list data for all nodes, or contain a list of nodes
391 to display data only for those
393 @return: the desired exit code
396 if opts.output is None:
397 selected_fields = ["node", "phys", "vg",
398 "name", "size", "instance"]
400 selected_fields = opts.output.split(",")
402 op = opcodes.OpQueryNodeVolumes(nodes=args, output_fields=selected_fields)
403 output = SubmitOpCode(op)
405 if not opts.no_headers:
406 headers = {"node": "Node", "phys": "PhysDev",
407 "vg": "VG", "name": "Name",
408 "size": "Size", "instance": "Instance"}
412 unitfields = ["size"]
416 data = GenerateTable(separator=opts.separator, headers=headers,
417 fields=selected_fields, unitfields=unitfields,
418 numfields=numfields, data=output, units=opts.units)
426 def SetNodeParams(opts, args):
429 @param opts: the command line options selected by the user
431 @param args: should contain only one element, the node name
433 @return: the desired exit code
436 if [opts.master_candidate, opts.drained, opts.offline].count(None) == 3:
437 ToStderr("Please give at least one of the parameters.")
440 if opts.master_candidate is not None:
441 candidate = opts.master_candidate == 'yes'
444 if opts.offline is not None:
445 offline = opts.offline == 'yes'
449 if opts.drained is not None:
450 drained = opts.drained == 'yes'
453 op = opcodes.OpSetNodeParams(node_name=args[0],
454 master_candidate=candidate,
459 # even if here we process the result, we allow submit only
460 result = SubmitOrSend(op, opts)
463 ToStdout("Modified node %s", args[0])
464 for param, data in result:
465 ToStdout(" - %-5s -> %s", param, data)
470 'add': (AddNode, ARGS_ONE,
472 make_option("-s", "--secondary-ip", dest="secondary_ip",
473 help="Specify the secondary ip for the node",
474 metavar="ADDRESS", default=None),
475 make_option("--readd", dest="readd",
476 default=False, action="store_true",
477 help="Readd old node after replacing it"),
478 make_option("--no-ssh-key-check", dest="ssh_key_check",
479 default=True, action="store_false",
480 help="Disable SSH key fingerprint checking"),
482 "[-s ip] [--readd] [--no-ssh-key-check] <node_name>",
483 "Add a node to the cluster"),
484 'evacuate': (EvacuateNode, ARGS_ONE,
485 [DEBUG_OPT, FORCE_OPT,
486 make_option("-n", "--new-secondary", dest="dst_node",
487 help="New secondary node", metavar="NODE",
489 make_option("-I", "--iallocator", metavar="<NAME>",
490 help="Select new secondary for the instance"
491 " automatically using the"
492 " <NAME> iallocator plugin",
493 default=None, type="string"),
495 "[-f] {-I <iallocator> | -n <dst>} <node>",
496 "Relocate the secondary instances from a node"
497 " to other nodes (only for instances with drbd disk template)"),
498 'failover': (FailoverNode, ARGS_ONE,
499 [DEBUG_OPT, FORCE_OPT,
500 make_option("--ignore-consistency", dest="ignore_consistency",
501 action="store_true", default=False,
502 help="Ignore the consistency of the disks on"
506 "Stops the primary instances on a node and start them on their"
507 " secondary node (only for instances with drbd disk template)"),
508 'migrate': (MigrateNode, ARGS_ONE,
509 [DEBUG_OPT, FORCE_OPT,
510 make_option("--non-live", dest="live",
511 default=True, action="store_false",
512 help="Do a non-live migration (this usually means"
513 " freeze the instance, save the state,"
514 " transfer and only then resume running on the"
518 "Migrate all the primary instance on a node away from it"
519 " (only for instances of type drbd)"),
520 'info': (ShowNodeConfig, ARGS_ANY, [DEBUG_OPT],
521 "[<node_name>...]", "Show information about the node(s)"),
522 'list': (ListNodes, ARGS_ANY,
523 [DEBUG_OPT, NOHDR_OPT, SEP_OPT, USEUNITS_OPT, FIELDS_OPT, SYNC_OPT],
525 "Lists the nodes in the cluster. The available fields"
526 " are (see the man page for details): %s"
527 " The default field list is (in order): %s." %
528 (", ".join(_LIST_HEADERS), ", ".join(_LIST_DEF_FIELDS))),
529 'modify': (SetNodeParams, ARGS_ONE,
530 [DEBUG_OPT, FORCE_OPT,
532 make_option("-C", "--master-candidate", dest="master_candidate",
533 choices=('yes', 'no'), default=None,
535 help="Set the master_candidate flag on the node"),
537 make_option("-O", "--offline", dest="offline", metavar="yes|no",
538 choices=('yes', 'no'), default=None,
539 help="Set the offline flag on the node"),
540 make_option("-D", "--drained", dest="drained", metavar="yes|no",
541 choices=('yes', 'no'), default=None,
542 help="Set the drained flag on the node"),
544 "<instance>", "Alters the parameters of an instance"),
545 'remove': (RemoveNode, ARGS_ONE, [DEBUG_OPT],
546 "<node_name>", "Removes a node from the cluster"),
547 'volumes': (ListVolumes, ARGS_ANY,
548 [DEBUG_OPT, NOHDR_OPT, SEP_OPT, USEUNITS_OPT, FIELDS_OPT],
549 "[<node_name>...]", "List logical volumes on node(s)"),
550 'list-tags': (ListTags, ARGS_ONE, [DEBUG_OPT],
551 "<node_name>", "List the tags of the given node"),
552 'add-tags': (AddTags, ARGS_ATLEAST(1), [DEBUG_OPT, TAG_SRC_OPT],
553 "<node_name> tag...", "Add tags to the given node"),
554 'remove-tags': (RemoveTags, ARGS_ATLEAST(1), [DEBUG_OPT, TAG_SRC_OPT],
555 "<node_name> tag...", "Remove tags from the given node"),
559 if __name__ == '__main__':
560 sys.exit(GenericMain(commands, override={"tag_type": constants.TAG_NODE}))