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",
62 #: User-facing storage unit types
63 _USER_STORAGE_TYPE = {
64 constants.ST_FILE: "file",
65 constants.ST_LVM_PV: "lvm-pv",
66 constants.ST_LVM_VG: "lvm-vg",
70 def ConvertStorageType(user_storage_type):
71 """Converts a user storage type to its internal name.
75 return _USER_STORAGE_TYPE[user_storage_type]
77 raise errors.OpPrereqError("Unknown storage type: %s" % user_storage_type)
81 def AddNode(opts, args):
82 """Add a node to the cluster.
84 @param opts: the command line options selected by the user
86 @param args: should contain only one element, the new node name
88 @return: the desired exit code
92 dns_data = utils.HostInfo(args[0])
97 output = cl.QueryNodes(names=[node], fields=['name', 'sip'],
99 node_exists, sip = output[0]
100 except (errors.OpPrereqError, errors.OpExecError):
106 ToStderr("Node %s not in the cluster"
107 " - please retry without '--readd'", node)
111 ToStderr("Node %s already in the cluster (as %s)"
112 " - please retry with '--readd'", node, node_exists)
114 sip = opts.secondary_ip
116 # read the cluster name from the master
117 output = cl.QueryConfigValues(['cluster_name'])
118 cluster_name = output[0]
121 ToStderr("-- WARNING -- \n"
122 "Performing this operation is going to replace the ssh daemon"
124 "on the target machine (%s) with the ones of the"
126 "and grant full intra-cluster ssh root access to/from it\n", node)
128 bootstrap.SetupNodeDaemon(cluster_name, node, opts.ssh_key_check)
130 op = opcodes.OpAddNode(node_name=args[0], secondary_ip=sip,
135 def ListNodes(opts, args):
136 """List nodes and their properties.
138 @param opts: the command line options selected by the user
140 @param args: should be an empty list
142 @return: the desired exit code
145 if opts.output is None:
146 selected_fields = _LIST_DEF_FIELDS
147 elif opts.output.startswith("+"):
148 selected_fields = _LIST_DEF_FIELDS + opts.output[1:].split(",")
150 selected_fields = opts.output.split(",")
152 output = GetClient().QueryNodes(args, selected_fields, opts.do_locking)
154 if not opts.no_headers:
155 headers = _LIST_HEADERS
159 unitfields = ["dtotal", "dfree", "mtotal", "mnode", "mfree"]
161 numfields = ["dtotal", "dfree",
162 "mtotal", "mnode", "mfree",
163 "pinst_cnt", "sinst_cnt",
164 "ctotal", "serial_no"]
166 list_type_fields = ("pinst_list", "sinst_list", "tags")
167 # change raw values to nicer strings
169 for idx, field in enumerate(selected_fields):
171 if field in list_type_fields:
173 elif field in ('master', 'master_candidate', 'offline', 'drained'):
182 data = GenerateTable(separator=opts.separator, headers=headers,
183 fields=selected_fields, unitfields=unitfields,
184 numfields=numfields, data=output, units=opts.units)
191 def EvacuateNode(opts, args):
192 """Relocate all secondary instance from a node.
194 @param opts: the command line options selected by the user
196 @param args: should be an empty list
198 @return: the desired exit code
204 dst_node = opts.dst_node
205 iallocator = opts.iallocator
207 cnt = [dst_node, iallocator].count(None)
209 raise errors.OpPrereqError("One and only one of the -n and -i"
210 " options must be passed")
212 selected_fields = ["name", "sinst_list"]
215 result = cl.QueryNodes(names=[src_node], fields=selected_fields,
217 src_node, sinst = result[0]
220 ToStderr("No secondary instances on node %s, exiting.", src_node)
221 return constants.EXIT_SUCCESS
223 if dst_node is not None:
224 result = cl.QueryNodes(names=[dst_node], fields=["name"],
226 dst_node = result[0][0]
228 if src_node == dst_node:
229 raise errors.OpPrereqError("Evacuate node needs different source and"
230 " target nodes (node %s given twice)" %
232 txt_msg = "to node %s" % dst_node
234 txt_msg = "using iallocator %s" % iallocator
236 sinst = utils.NiceSort(sinst)
238 if not force and not AskUser("Relocate instance(s) %s from node\n"
240 (",".join("'%s'" % name for name in sinst),
242 return constants.EXIT_CONFIRMATION
244 op = opcodes.OpEvacuateNode(node_name=args[0], remote_node=dst_node,
245 iallocator=iallocator)
246 SubmitOpCode(op, cl=cl)
249 def FailoverNode(opts, args):
250 """Failover all primary instance on a node.
252 @param opts: the command line options selected by the user
254 @param args: should be an empty list
256 @return: the desired exit code
261 selected_fields = ["name", "pinst_list"]
263 # these fields are static data anyway, so it doesn't matter, but
264 # locking=True should be safer
265 result = cl.QueryNodes(names=args, fields=selected_fields,
267 node, pinst = result[0]
270 ToStderr("No primary instances on node %s, exiting.", node)
273 pinst = utils.NiceSort(pinst)
277 if not force and not AskUser("Fail over instance(s) %s?" %
278 (",".join("'%s'" % name for name in pinst))):
281 jex = JobExecutor(cl=cl)
283 op = opcodes.OpFailoverInstance(instance_name=iname,
284 ignore_consistency=opts.ignore_consistency)
285 jex.QueueJob(iname, op)
286 results = jex.GetResults()
287 bad_cnt = len([row for row in results if not row[0]])
289 ToStdout("All %d instance(s) failed over successfully.", len(results))
291 ToStdout("There were errors during the failover:\n"
292 "%d error(s) out of %d instance(s).", bad_cnt, len(results))
296 def MigrateNode(opts, args):
297 """Migrate all primary instance on a node.
302 selected_fields = ["name", "pinst_list"]
304 result = cl.QueryNodes(names=args, fields=selected_fields, use_locking=False)
305 node, pinst = result[0]
308 ToStdout("No primary instances on node %s, exiting." % node)
311 pinst = utils.NiceSort(pinst)
315 if not force and not AskUser("Migrate instance(s) %s?" %
316 (",".join("'%s'" % name for name in pinst))):
319 op = opcodes.OpMigrateNode(node_name=args[0], live=opts.live)
320 SubmitOpCode(op, cl=cl)
323 def ShowNodeConfig(opts, args):
324 """Show node information.
326 @param opts: the command line options selected by the user
328 @param args: should either be an empty list, in which case
329 we show information about all nodes, or should contain
330 a list of nodes to be queried for information
332 @return: the desired exit code
336 result = cl.QueryNodes(fields=["name", "pip", "sip",
337 "pinst_list", "sinst_list",
338 "master_candidate", "drained", "offline"],
339 names=args, use_locking=False)
341 for (name, primary_ip, secondary_ip, pinst, sinst,
342 is_mc, drained, offline) in result:
343 ToStdout("Node name: %s", name)
344 ToStdout(" primary ip: %s", primary_ip)
345 ToStdout(" secondary ip: %s", secondary_ip)
346 ToStdout(" master candidate: %s", is_mc)
347 ToStdout(" drained: %s", drained)
348 ToStdout(" offline: %s", offline)
350 ToStdout(" primary for instances:")
351 for iname in utils.NiceSort(pinst):
352 ToStdout(" - %s", iname)
354 ToStdout(" primary for no instances")
356 ToStdout(" secondary for instances:")
357 for iname in utils.NiceSort(sinst):
358 ToStdout(" - %s", iname)
360 ToStdout(" secondary for no instances")
365 def RemoveNode(opts, args):
366 """Remove a node from the cluster.
368 @param opts: the command line options selected by the user
370 @param args: should contain only one element, the name of
371 the node to be removed
373 @return: the desired exit code
376 op = opcodes.OpRemoveNode(node_name=args[0])
381 def PowercycleNode(opts, args):
382 """Remove a node from the cluster.
384 @param opts: the command line options selected by the user
386 @param args: should contain only one element, the name of
387 the node to be removed
389 @return: the desired exit code
393 if (not opts.confirm and
394 not AskUser("Are you sure you want to hard powercycle node %s?" % node)):
397 op = opcodes.OpPowercycleNode(node_name=node, force=opts.force)
398 result = SubmitOpCode(op)
403 def ListVolumes(opts, args):
404 """List logical volumes on node(s).
406 @param opts: the command line options selected by the user
408 @param args: should either be an empty list, in which case
409 we list data for all nodes, or contain a list of nodes
410 to display data only for those
412 @return: the desired exit code
415 if opts.output is None:
416 selected_fields = ["node", "phys", "vg",
417 "name", "size", "instance"]
419 selected_fields = opts.output.split(",")
421 op = opcodes.OpQueryNodeVolumes(nodes=args, output_fields=selected_fields)
422 output = SubmitOpCode(op)
424 if not opts.no_headers:
425 headers = {"node": "Node", "phys": "PhysDev",
426 "vg": "VG", "name": "Name",
427 "size": "Size", "instance": "Instance"}
431 unitfields = ["size"]
435 data = GenerateTable(separator=opts.separator, headers=headers,
436 fields=selected_fields, unitfields=unitfields,
437 numfields=numfields, data=output, units=opts.units)
445 def ListPhysicalVolumes(opts, args):
446 """List physical volumes on node(s).
448 @param opts: the command line options selected by the user
450 @param args: should either be an empty list, in which case
451 we list data for all nodes, or contain a list of nodes
452 to display data only for those
454 @return: the desired exit code
457 # TODO: Default to ST_FILE if LVM is disabled on the cluster
458 if opts.user_storage_type is None:
459 opts.user_storage_type = constants.ST_LVM_PV
461 storage_type = ConvertStorageType(opts.user_storage_type)
469 constants.ST_LVM_PV: [
475 constants.ST_LVM_VG: [
481 if opts.output is None:
482 selected_fields = ["node"]
483 selected_fields.extend(default_fields[storage_type])
485 selected_fields = opts.output.split(",")
487 op = opcodes.OpQueryNodeStorage(nodes=args,
488 storage_type=storage_type,
489 output_fields=selected_fields)
490 output = SubmitOpCode(op)
492 if not opts.no_headers:
495 constants.SF_NAME: "Name",
496 constants.SF_SIZE: "Size",
497 constants.SF_USED: "Used",
498 constants.SF_FREE: "Free",
499 constants.SF_ALLOCATABLE: "Allocatable",
504 unitfields = [constants.SF_SIZE, constants.SF_USED, constants.SF_FREE]
505 numfields = [constants.SF_SIZE, constants.SF_USED, constants.SF_FREE]
507 data = GenerateTable(separator=opts.separator, headers=headers,
508 fields=selected_fields, unitfields=unitfields,
509 numfields=numfields, data=output, units=opts.units)
517 def ModifyVolume(opts, args):
518 """Modify storage volume on a node.
520 @param opts: the command line options selected by the user
522 @param args: should contain 3 items: node name, storage type and volume name
524 @return: the desired exit code
527 (node_name, user_storage_type, volume_name) = args
529 storage_type = ConvertStorageType(user_storage_type)
533 if opts.allocatable is not None:
534 changes[constants.SF_ALLOCATABLE] = (opts.allocatable == "yes")
537 op = opcodes.OpModifyNodeStorage(node_name=node_name,
538 storage_type=storage_type,
544 def RepairVolume(opts, args):
545 """Repairs a storage volume on a node.
547 @param opts: the command line options selected by the user
549 @param args: should contain 3 items: node name, storage type and volume name
551 @return: the desired exit code
554 (node_name, user_storage_type, volume_name) = args
556 storage_type = ConvertStorageType(user_storage_type)
558 op = opcodes.OpRepairNodeStorage(node_name=node_name,
559 storage_type=storage_type,
564 def SetNodeParams(opts, args):
567 @param opts: the command line options selected by the user
569 @param args: should contain only one element, the node name
571 @return: the desired exit code
574 if [opts.master_candidate, opts.drained, opts.offline].count(None) == 3:
575 ToStderr("Please give at least one of the parameters.")
578 if opts.master_candidate is not None:
579 candidate = opts.master_candidate == 'yes'
582 if opts.offline is not None:
583 offline = opts.offline == 'yes'
587 if opts.drained is not None:
588 drained = opts.drained == 'yes'
591 op = opcodes.OpSetNodeParams(node_name=args[0],
592 master_candidate=candidate,
597 # even if here we process the result, we allow submit only
598 result = SubmitOrSend(op, opts)
601 ToStdout("Modified node %s", args[0])
602 for param, data in result:
603 ToStdout(" - %-5s -> %s", param, data)
608 'add': (AddNode, ARGS_ONE,
610 make_option("-s", "--secondary-ip", dest="secondary_ip",
611 help="Specify the secondary ip for the node",
612 metavar="ADDRESS", default=None),
613 make_option("--readd", dest="readd",
614 default=False, action="store_true",
615 help="Readd old node after replacing it"),
616 make_option("--no-ssh-key-check", dest="ssh_key_check",
617 default=True, action="store_false",
618 help="Disable SSH key fingerprint checking"),
620 "[-s ip] [--readd] [--no-ssh-key-check] <node_name>",
621 "Add a node to the cluster"),
622 'evacuate': (EvacuateNode, ARGS_ONE,
623 [DEBUG_OPT, FORCE_OPT,
624 make_option("-n", "--new-secondary", dest="dst_node",
625 help="New secondary node", metavar="NODE",
627 make_option("-I", "--iallocator", metavar="<NAME>",
628 help="Select new secondary for the instance"
629 " automatically using the"
630 " <NAME> iallocator plugin",
631 default=None, type="string"),
633 "[-f] {-I <iallocator> | -n <dst>} <node>",
634 "Relocate the secondary instances from a node"
635 " to other nodes (only for instances with drbd disk template)"),
636 'failover': (FailoverNode, ARGS_ONE,
637 [DEBUG_OPT, FORCE_OPT,
638 make_option("--ignore-consistency", dest="ignore_consistency",
639 action="store_true", default=False,
640 help="Ignore the consistency of the disks on"
644 "Stops the primary instances on a node and start them on their"
645 " secondary node (only for instances with drbd disk template)"),
646 'migrate': (MigrateNode, ARGS_ONE,
647 [DEBUG_OPT, FORCE_OPT,
648 make_option("--non-live", dest="live",
649 default=True, action="store_false",
650 help="Do a non-live migration (this usually means"
651 " freeze the instance, save the state,"
652 " transfer and only then resume running on the"
656 "Migrate all the primary instance on a node away from it"
657 " (only for instances of type drbd)"),
658 'info': (ShowNodeConfig, ARGS_ANY, [DEBUG_OPT],
659 "[<node_name>...]", "Show information about the node(s)"),
660 'list': (ListNodes, ARGS_ANY,
661 [DEBUG_OPT, NOHDR_OPT, SEP_OPT, USEUNITS_OPT, FIELDS_OPT, SYNC_OPT],
663 "Lists the nodes in the cluster. The available fields"
664 " are (see the man page for details): %s"
665 " The default field list is (in order): %s." %
666 (", ".join(_LIST_HEADERS), ", ".join(_LIST_DEF_FIELDS))),
667 'modify': (SetNodeParams, ARGS_ONE,
668 [DEBUG_OPT, FORCE_OPT,
670 make_option("-C", "--master-candidate", dest="master_candidate",
671 choices=('yes', 'no'), default=None,
673 help="Set the master_candidate flag on the node"),
674 make_option("-O", "--offline", dest="offline", metavar="yes|no",
675 choices=('yes', 'no'), default=None,
676 help="Set the offline flag on the node"),
677 make_option("-D", "--drained", dest="drained", metavar="yes|no",
678 choices=('yes', 'no'), default=None,
679 help="Set the drained flag on the node"),
681 "<instance>", "Alters the parameters of an instance"),
682 'powercycle': (PowercycleNode, ARGS_ONE, [DEBUG_OPT, FORCE_OPT, CONFIRM_OPT],
683 "<node_name>", "Tries to forcefully powercycle a node"),
684 'remove': (RemoveNode, ARGS_ONE, [DEBUG_OPT],
685 "<node_name>", "Removes a node from the cluster"),
686 'volumes': (ListVolumes, ARGS_ANY,
687 [DEBUG_OPT, NOHDR_OPT, SEP_OPT, USEUNITS_OPT, FIELDS_OPT],
688 "[<node_name>...]", "List logical volumes on node(s)"),
689 'physical-volumes': (ListPhysicalVolumes, ARGS_ANY,
690 [DEBUG_OPT, NOHDR_OPT, SEP_OPT, USEUNITS_OPT,
692 make_option("--storage-type",
693 dest="user_storage_type",
694 choices=_USER_STORAGE_TYPE.keys(),
696 metavar="STORAGE_TYPE",
697 help=("Storage type (%s)" %
698 utils.CommaJoin(_USER_STORAGE_TYPE.keys()))),
701 "List physical volumes on node(s)"),
702 'modify-volume': (ModifyVolume, ARGS_FIXED(3),
704 make_option("--allocatable", dest="allocatable",
705 choices=["yes", "no"], default=None,
707 help="Set the allocatable flag on a volume"),
709 "<node_name> <storage_type> <name>",
710 "Modify storage volume on a node"),
711 'repair-volume': (RepairVolume, ARGS_FIXED(3),
713 "<node_name> <storage_type> <name>",
714 "Repairs a storage volume on a node"),
715 'list-tags': (ListTags, ARGS_ONE, [DEBUG_OPT],
716 "<node_name>", "List the tags of the given node"),
717 'add-tags': (AddTags, ARGS_ATLEAST(1), [DEBUG_OPT, TAG_SRC_OPT],
718 "<node_name> tag...", "Add tags to the given node"),
719 'remove-tags': (RemoveTags, ARGS_ATLEAST(1), [DEBUG_OPT, TAG_SRC_OPT],
720 "<node_name> tag...", "Remove tags from the given node"),
724 if __name__ == '__main__':
725 sys.exit(GenericMain(commands, override={"tag_type": constants.TAG_NODE}))