Package ganeti :: Module cli
[hide private]
[frames] | no frames]

Source Code for Module ganeti.cli

   1  # 
   2  # 
   3   
   4  # Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Google Inc. 
   5  # All rights reserved. 
   6  # 
   7  # Redistribution and use in source and binary forms, with or without 
   8  # modification, are permitted provided that the following conditions are 
   9  # met: 
  10  # 
  11  # 1. Redistributions of source code must retain the above copyright notice, 
  12  # this list of conditions and the following disclaimer. 
  13  # 
  14  # 2. Redistributions in binary form must reproduce the above copyright 
  15  # notice, this list of conditions and the following disclaimer in the 
  16  # documentation and/or other materials provided with the distribution. 
  17  # 
  18  # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 
  19  # IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 
  20  # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
  21  # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 
  22  # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
  23  # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
  24  # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
  25  # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
  26  # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
  27  # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
  28  # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
  29   
  30   
  31  """Module dealing with command line parsing""" 
  32   
  33   
  34  import sys 
  35  import textwrap 
  36  import os.path 
  37  import time 
  38  import logging 
  39  import errno 
  40  import itertools 
  41  import shlex 
  42  from cStringIO import StringIO 
  43   
  44  from ganeti import utils 
  45  from ganeti import errors 
  46  from ganeti import constants 
  47  from ganeti import opcodes 
  48  import ganeti.rpc.errors as rpcerr 
  49  import ganeti.rpc.node as rpc 
  50  from ganeti import ssh 
  51  from ganeti import compat 
  52  from ganeti import netutils 
  53  from ganeti import qlang 
  54  from ganeti import objects 
  55  from ganeti import pathutils 
  56   
  57  from ganeti.runtime import (GetClient) 
  58   
  59  from optparse import (OptionParser, TitledHelpFormatter, 
  60                        Option, OptionValueError) 
  61   
  62   
  63  __all__ = [ 
  64    # Command line options 
  65    "ABSOLUTE_OPT", 
  66    "ADD_UIDS_OPT", 
  67    "ADD_RESERVED_IPS_OPT", 
  68    "ALLOCATABLE_OPT", 
  69    "ALLOC_POLICY_OPT", 
  70    "ALL_OPT", 
  71    "ALLOW_FAILOVER_OPT", 
  72    "AUTO_PROMOTE_OPT", 
  73    "AUTO_REPLACE_OPT", 
  74    "BACKEND_OPT", 
  75    "BLK_OS_OPT", 
  76    "CAPAB_MASTER_OPT", 
  77    "CAPAB_VM_OPT", 
  78    "CLEANUP_OPT", 
  79    "CLUSTER_DOMAIN_SECRET_OPT", 
  80    "CONFIRM_OPT", 
  81    "CP_SIZE_OPT", 
  82    "DEBUG_OPT", 
  83    "DEBUG_SIMERR_OPT", 
  84    "DISKIDX_OPT", 
  85    "DISK_OPT", 
  86    "DISK_PARAMS_OPT", 
  87    "DISK_TEMPLATE_OPT", 
  88    "DRAINED_OPT", 
  89    "DRY_RUN_OPT", 
  90    "DRBD_HELPER_OPT", 
  91    "DST_NODE_OPT", 
  92    "EARLY_RELEASE_OPT", 
  93    "ENABLED_HV_OPT", 
  94    "ENABLED_DISK_TEMPLATES_OPT", 
  95    "ENABLED_USER_SHUTDOWN_OPT", 
  96    "ERROR_CODES_OPT", 
  97    "FAILURE_ONLY_OPT", 
  98    "FIELDS_OPT", 
  99    "FILESTORE_DIR_OPT", 
 100    "FILESTORE_DRIVER_OPT", 
 101    "FORCE_FAILOVER_OPT", 
 102    "FORCE_FILTER_OPT", 
 103    "FORCE_OPT", 
 104    "FORCE_VARIANT_OPT", 
 105    "GATEWAY_OPT", 
 106    "GATEWAY6_OPT", 
 107    "GLOBAL_FILEDIR_OPT", 
 108    "HID_OS_OPT", 
 109    "GLOBAL_GLUSTER_FILEDIR_OPT", 
 110    "GLOBAL_SHARED_FILEDIR_OPT", 
 111    "HOTPLUG_OPT", 
 112    "HOTPLUG_IF_POSSIBLE_OPT", 
 113    "HVLIST_OPT", 
 114    "HVOPTS_OPT", 
 115    "HYPERVISOR_OPT", 
 116    "IALLOCATOR_OPT", 
 117    "DEFAULT_IALLOCATOR_OPT", 
 118    "DEFAULT_IALLOCATOR_PARAMS_OPT", 
 119    "IDENTIFY_DEFAULTS_OPT", 
 120    "IGNORE_CONSIST_OPT", 
 121    "IGNORE_ERRORS_OPT", 
 122    "IGNORE_FAILURES_OPT", 
 123    "IGNORE_OFFLINE_OPT", 
 124    "IGNORE_REMOVE_FAILURES_OPT", 
 125    "IGNORE_SECONDARIES_OPT", 
 126    "IGNORE_SIZE_OPT", 
 127    "INCLUDEDEFAULTS_OPT", 
 128    "INTERVAL_OPT", 
 129    "MAC_PREFIX_OPT", 
 130    "MAINTAIN_NODE_HEALTH_OPT", 
 131    "MASTER_NETDEV_OPT", 
 132    "MASTER_NETMASK_OPT", 
 133    "MC_OPT", 
 134    "MIGRATION_MODE_OPT", 
 135    "MODIFY_ETCHOSTS_OPT", 
 136    "NET_OPT", 
 137    "NETWORK_OPT", 
 138    "NETWORK6_OPT", 
 139    "NEW_CLUSTER_CERT_OPT", 
 140    "NEW_NODE_CERT_OPT", 
 141    "NEW_CLUSTER_DOMAIN_SECRET_OPT", 
 142    "NEW_CONFD_HMAC_KEY_OPT", 
 143    "NEW_RAPI_CERT_OPT", 
 144    "NEW_PRIMARY_OPT", 
 145    "NEW_SECONDARY_OPT", 
 146    "NEW_SPICE_CERT_OPT", 
 147    "NIC_PARAMS_OPT", 
 148    "NOCONFLICTSCHECK_OPT", 
 149    "NODE_FORCE_JOIN_OPT", 
 150    "NODE_LIST_OPT", 
 151    "NODE_PLACEMENT_OPT", 
 152    "NODEGROUP_OPT", 
 153    "NODE_PARAMS_OPT", 
 154    "NODE_POWERED_OPT", 
 155    "NOHDR_OPT", 
 156    "NOIPCHECK_OPT", 
 157    "NO_INSTALL_OPT", 
 158    "NONAMECHECK_OPT", 
 159    "NOMODIFY_ETCHOSTS_OPT", 
 160    "NOMODIFY_SSH_SETUP_OPT", 
 161    "NONICS_OPT", 
 162    "NONLIVE_OPT", 
 163    "NONPLUS1_OPT", 
 164    "NORUNTIME_CHGS_OPT", 
 165    "NOSHUTDOWN_OPT", 
 166    "NOSTART_OPT", 
 167    "NOSSH_KEYCHECK_OPT", 
 168    "NOVOTING_OPT", 
 169    "NO_REMEMBER_OPT", 
 170    "NWSYNC_OPT", 
 171    "OFFLINE_INST_OPT", 
 172    "ONLINE_INST_OPT", 
 173    "ON_PRIMARY_OPT", 
 174    "ON_SECONDARY_OPT", 
 175    "OFFLINE_OPT", 
 176    "OSPARAMS_OPT", 
 177    "OS_OPT", 
 178    "OS_SIZE_OPT", 
 179    "OOB_TIMEOUT_OPT", 
 180    "POWER_DELAY_OPT", 
 181    "PREALLOC_WIPE_DISKS_OPT", 
 182    "PRIMARY_IP_VERSION_OPT", 
 183    "PRIMARY_ONLY_OPT", 
 184    "PRINT_JOBID_OPT", 
 185    "PRIORITY_OPT", 
 186    "RAPI_CERT_OPT", 
 187    "READD_OPT", 
 188    "REASON_OPT", 
 189    "REBOOT_TYPE_OPT", 
 190    "REMOVE_INSTANCE_OPT", 
 191    "REMOVE_RESERVED_IPS_OPT", 
 192    "REMOVE_UIDS_OPT", 
 193    "RESERVED_LVS_OPT", 
 194    "RQL_OPT", 
 195    "RUNTIME_MEM_OPT", 
 196    "ROMAN_OPT", 
 197    "SECONDARY_IP_OPT", 
 198    "SECONDARY_ONLY_OPT", 
 199    "SELECT_OS_OPT", 
 200    "SEP_OPT", 
 201    "SHOWCMD_OPT", 
 202    "SHOW_MACHINE_OPT", 
 203    "COMPRESS_OPT", 
 204    "TRANSPORT_COMPRESSION_OPT", 
 205    "SHUTDOWN_TIMEOUT_OPT", 
 206    "SINGLE_NODE_OPT", 
 207    "SPECS_CPU_COUNT_OPT", 
 208    "SPECS_DISK_COUNT_OPT", 
 209    "SPECS_DISK_SIZE_OPT", 
 210    "SPECS_MEM_SIZE_OPT", 
 211    "SPECS_NIC_COUNT_OPT", 
 212    "SPLIT_ISPECS_OPTS", 
 213    "IPOLICY_STD_SPECS_OPT", 
 214    "IPOLICY_DISK_TEMPLATES", 
 215    "IPOLICY_VCPU_RATIO", 
 216    "SEQUENTIAL_OPT", 
 217    "SPICE_CACERT_OPT", 
 218    "SPICE_CERT_OPT", 
 219    "SRC_DIR_OPT", 
 220    "SRC_NODE_OPT", 
 221    "SUBMIT_OPT", 
 222    "SUBMIT_OPTS", 
 223    "STARTUP_PAUSED_OPT", 
 224    "STATIC_OPT", 
 225    "SYNC_OPT", 
 226    "TAG_ADD_OPT", 
 227    "TAG_SRC_OPT", 
 228    "TIMEOUT_OPT", 
 229    "TO_GROUP_OPT", 
 230    "UIDPOOL_OPT", 
 231    "USEUNITS_OPT", 
 232    "USE_EXTERNAL_MIP_SCRIPT", 
 233    "USE_REPL_NET_OPT", 
 234    "VERBOSE_OPT", 
 235    "VG_NAME_OPT", 
 236    "WFSYNC_OPT", 
 237    "YES_DOIT_OPT", 
 238    "DISK_STATE_OPT", 
 239    "HV_STATE_OPT", 
 240    "IGNORE_IPOLICY_OPT", 
 241    "INSTANCE_POLICY_OPTS", 
 242    # Generic functions for CLI programs 
 243    "ConfirmOperation", 
 244    "CreateIPolicyFromOpts", 
 245    "GenericMain", 
 246    "GenericInstanceCreate", 
 247    "GenericList", 
 248    "GenericListFields", 
 249    "GetClient", 
 250    "GetOnlineNodes", 
 251    "GetNodesSshPorts", 
 252    "JobExecutor", 
 253    "JobSubmittedException", 
 254    "ParseTimespec", 
 255    "RunWhileClusterStopped", 
 256    "SubmitOpCode", 
 257    "SubmitOpCodeToDrainedQueue", 
 258    "SubmitOrSend", 
 259    "UsesRPC", 
 260    # Formatting functions 
 261    "ToStderr", "ToStdout", 
 262    "FormatError", 
 263    "FormatQueryResult", 
 264    "FormatParamsDictInfo", 
 265    "FormatPolicyInfo", 
 266    "PrintIPolicyCommand", 
 267    "PrintGenericInfo", 
 268    "GenerateTable", 
 269    "AskUser", 
 270    "FormatTimestamp", 
 271    "FormatLogMessage", 
 272    # Tags functions 
 273    "ListTags", 
 274    "AddTags", 
 275    "RemoveTags", 
 276    # command line options support infrastructure 
 277    "ARGS_MANY_INSTANCES", 
 278    "ARGS_MANY_NODES", 
 279    "ARGS_MANY_GROUPS", 
 280    "ARGS_MANY_NETWORKS", 
 281    "ARGS_NONE", 
 282    "ARGS_ONE_INSTANCE", 
 283    "ARGS_ONE_NODE", 
 284    "ARGS_ONE_GROUP", 
 285    "ARGS_ONE_OS", 
 286    "ARGS_ONE_NETWORK", 
 287    "ArgChoice", 
 288    "ArgCommand", 
 289    "ArgFile", 
 290    "ArgGroup", 
 291    "ArgHost", 
 292    "ArgInstance", 
 293    "ArgJobId", 
 294    "ArgNetwork", 
 295    "ArgNode", 
 296    "ArgOs", 
 297    "ArgExtStorage", 
 298    "ArgSuggest", 
 299    "ArgUnknown", 
 300    "OPT_COMPL_INST_ADD_NODES", 
 301    "OPT_COMPL_MANY_NODES", 
 302    "OPT_COMPL_ONE_IALLOCATOR", 
 303    "OPT_COMPL_ONE_INSTANCE", 
 304    "OPT_COMPL_ONE_NODE", 
 305    "OPT_COMPL_ONE_NODEGROUP", 
 306    "OPT_COMPL_ONE_NETWORK", 
 307    "OPT_COMPL_ONE_OS", 
 308    "OPT_COMPL_ONE_EXTSTORAGE", 
 309    "cli_option", 
 310    "FixHvParams", 
 311    "SplitNodeOption", 
 312    "CalculateOSNames", 
 313    "ParseFields", 
 314    "COMMON_CREATE_OPTS", 
 315    ] 
 316   
 317  NO_PREFIX = "no_" 
 318  UN_PREFIX = "-" 
 319   
 320  #: Priorities (sorted) 
 321  _PRIORITY_NAMES = [ 
 322    ("low", constants.OP_PRIO_LOW), 
 323    ("normal", constants.OP_PRIO_NORMAL), 
 324    ("high", constants.OP_PRIO_HIGH), 
 325    ] 
 326   
 327  #: Priority dictionary for easier lookup 
 328  # TODO: Replace this and _PRIORITY_NAMES with a single sorted dictionary once 
 329  # we migrate to Python 2.6 
 330  _PRIONAME_TO_VALUE = dict(_PRIORITY_NAMES) 
 331   
 332  # Query result status for clients 
 333  (QR_NORMAL, 
 334   QR_UNKNOWN, 
 335   QR_INCOMPLETE) = range(3) 
 336   
 337  #: Maximum batch size for ChooseJob 
 338  _CHOOSE_BATCH = 25 
 339   
 340   
 341  # constants used to create InstancePolicy dictionary 
 342  TISPECS_GROUP_TYPES = { 
 343    constants.ISPECS_MIN: constants.VTYPE_INT, 
 344    constants.ISPECS_MAX: constants.VTYPE_INT, 
 345    } 
 346   
 347  TISPECS_CLUSTER_TYPES = { 
 348    constants.ISPECS_MIN: constants.VTYPE_INT, 
 349    constants.ISPECS_MAX: constants.VTYPE_INT, 
 350    constants.ISPECS_STD: constants.VTYPE_INT, 
 351    } 
 352   
 353  #: User-friendly names for query2 field types 
 354  _QFT_NAMES = { 
 355    constants.QFT_UNKNOWN: "Unknown", 
 356    constants.QFT_TEXT: "Text", 
 357    constants.QFT_BOOL: "Boolean", 
 358    constants.QFT_NUMBER: "Number", 
 359    constants.QFT_UNIT: "Storage size", 
 360    constants.QFT_TIMESTAMP: "Timestamp", 
 361    constants.QFT_OTHER: "Custom", 
 362    } 
363 364 365 -class _Argument(object):
366 - def __init__(self, min=0, max=None): # pylint: disable=W0622
367 self.min = min 368 self.max = max
369
370 - def __repr__(self):
371 return ("<%s min=%s max=%s>" % 372 (self.__class__.__name__, self.min, self.max))
373
374 375 -class ArgSuggest(_Argument):
376 """Suggesting argument. 377 378 Value can be any of the ones passed to the constructor. 379 380 """ 381 # pylint: disable=W0622
382 - def __init__(self, min=0, max=None, choices=None):
383 _Argument.__init__(self, min=min, max=max) 384 self.choices = choices
385
386 - def __repr__(self):
387 return ("<%s min=%s max=%s choices=%r>" % 388 (self.__class__.__name__, self.min, self.max, self.choices))
389
390 391 -class ArgChoice(ArgSuggest):
392 """Choice argument. 393 394 Value can be any of the ones passed to the constructor. Like L{ArgSuggest}, 395 but value must be one of the choices. 396 397 """
398
399 400 -class ArgUnknown(_Argument):
401 """Unknown argument to program (e.g. determined at runtime). 402 403 """
404
405 406 -class ArgInstance(_Argument):
407 """Instances argument. 408 409 """
410
411 412 -class ArgNode(_Argument):
413 """Node argument. 414 415 """
416
417 418 -class ArgNetwork(_Argument):
419 """Network argument. 420 421 """
422
423 424 -class ArgGroup(_Argument):
425 """Node group argument. 426 427 """
428
429 430 -class ArgJobId(_Argument):
431 """Job ID argument. 432 433 """
434
435 436 -class ArgFile(_Argument):
437 """File path argument. 438 439 """
440
441 442 -class ArgCommand(_Argument):
443 """Command argument. 444 445 """
446
447 448 -class ArgHost(_Argument):
449 """Host argument. 450 451 """
452
453 454 -class ArgOs(_Argument):
455 """OS argument. 456 457 """
458
459 460 -class ArgExtStorage(_Argument):
461 """ExtStorage argument. 462 463 """
464 465 466 ARGS_NONE = [] 467 ARGS_MANY_INSTANCES = [ArgInstance()] 468 ARGS_MANY_NETWORKS = [ArgNetwork()] 469 ARGS_MANY_NODES = [ArgNode()] 470 ARGS_MANY_GROUPS = [ArgGroup()] 471 ARGS_ONE_INSTANCE = [ArgInstance(min=1, max=1)] 472 ARGS_ONE_NETWORK = [ArgNetwork(min=1, max=1)] 473 ARGS_ONE_NODE = [ArgNode(min=1, max=1)] 474 # TODO 475 ARGS_ONE_GROUP = [ArgGroup(min=1, max=1)] 476 ARGS_ONE_OS = [ArgOs(min=1, max=1)]
477 478 479 -def _ExtractTagsObject(opts, args):
480 """Extract the tag type object. 481 482 Note that this function will modify its args parameter. 483 484 """ 485 if not hasattr(opts, "tag_type"): 486 raise errors.ProgrammerError("tag_type not passed to _ExtractTagsObject") 487 kind = opts.tag_type 488 if kind == constants.TAG_CLUSTER: 489 retval = kind, "" 490 elif kind in (constants.TAG_NODEGROUP, 491 constants.TAG_NODE, 492 constants.TAG_NETWORK, 493 constants.TAG_INSTANCE): 494 if not args: 495 raise errors.OpPrereqError("no arguments passed to the command", 496 errors.ECODE_INVAL) 497 name = args.pop(0) 498 retval = kind, name 499 else: 500 raise errors.ProgrammerError("Unhandled tag type '%s'" % kind) 501 return retval
502
503 504 -def _ExtendTags(opts, args):
505 """Extend the args if a source file has been given. 506 507 This function will extend the tags with the contents of the file 508 passed in the 'tags_source' attribute of the opts parameter. A file 509 named '-' will be replaced by stdin. 510 511 """ 512 fname = opts.tags_source 513 if fname is None: 514 return 515 if fname == "-": 516 new_fh = sys.stdin 517 else: 518 new_fh = open(fname, "r") 519 new_data = [] 520 try: 521 # we don't use the nice 'new_data = [line.strip() for line in fh]' 522 # because of python bug 1633941 523 while True: 524 line = new_fh.readline() 525 if not line: 526 break 527 new_data.append(line.strip()) 528 finally: 529 new_fh.close() 530 args.extend(new_data)
531
532 533 -def ListTags(opts, args):
534 """List the tags on a given object. 535 536 This is a generic implementation that knows how to deal with all 537 three cases of tag objects (cluster, node, instance). The opts 538 argument is expected to contain a tag_type field denoting what 539 object type we work on. 540 541 """ 542 kind, name = _ExtractTagsObject(opts, args) 543 cl = GetClient(query=True) 544 result = cl.QueryTags(kind, name) 545 result = list(result) 546 result.sort() 547 for tag in result: 548 ToStdout(tag)
549
550 551 -def AddTags(opts, args):
552 """Add tags on a given object. 553 554 This is a generic implementation that knows how to deal with all 555 three cases of tag objects (cluster, node, instance). The opts 556 argument is expected to contain a tag_type field denoting what 557 object type we work on. 558 559 """ 560 kind, name = _ExtractTagsObject(opts, args) 561 _ExtendTags(opts, args) 562 if not args: 563 raise errors.OpPrereqError("No tags to be added", errors.ECODE_INVAL) 564 op = opcodes.OpTagsSet(kind=kind, name=name, tags=args) 565 SubmitOrSend(op, opts)
566
567 568 -def RemoveTags(opts, args):
569 """Remove tags from a given object. 570 571 This is a generic implementation that knows how to deal with all 572 three cases of tag objects (cluster, node, instance). The opts 573 argument is expected to contain a tag_type field denoting what 574 object type we work on. 575 576 """ 577 kind, name = _ExtractTagsObject(opts, args) 578 _ExtendTags(opts, args) 579 if not args: 580 raise errors.OpPrereqError("No tags to be removed", errors.ECODE_INVAL) 581 op = opcodes.OpTagsDel(kind=kind, name=name, tags=args) 582 SubmitOrSend(op, opts)
583
584 585 -def check_unit(option, opt, value): # pylint: disable=W0613
586 """OptParsers custom converter for units. 587 588 """ 589 try: 590 return utils.ParseUnit(value) 591 except errors.UnitParseError, err: 592 raise OptionValueError("option %s: %s" % (opt, err)) 593
594 595 -def _SplitKeyVal(opt, data, parse_prefixes):
596 """Convert a KeyVal string into a dict. 597 598 This function will convert a key=val[,...] string into a dict. Empty 599 values will be converted specially: keys which have the prefix 'no_' 600 will have the value=False and the prefix stripped, keys with the prefix 601 "-" will have value=None and the prefix stripped, and the others will 602 have value=True. 603 604 @type opt: string 605 @param opt: a string holding the option name for which we process the 606 data, used in building error messages 607 @type data: string 608 @param data: a string of the format key=val,key=val,... 609 @type parse_prefixes: bool 610 @param parse_prefixes: whether to handle prefixes specially 611 @rtype: dict 612 @return: {key=val, key=val} 613 @raises errors.ParameterError: if there are duplicate keys 614 615 """ 616 kv_dict = {} 617 if data: 618 for elem in utils.UnescapeAndSplit(data, sep=","): 619 if "=" in elem: 620 key, val = elem.split("=", 1) 621 elif parse_prefixes: 622 if elem.startswith(NO_PREFIX): 623 key, val = elem[len(NO_PREFIX):], False 624 elif elem.startswith(UN_PREFIX): 625 key, val = elem[len(UN_PREFIX):], None 626 else: 627 key, val = elem, True 628 else: 629 raise errors.ParameterError("Missing value for key '%s' in option %s" % 630 (elem, opt)) 631 if key in kv_dict: 632 raise errors.ParameterError("Duplicate key '%s' in option %s" % 633 (key, opt)) 634 kv_dict[key] = val 635 return kv_dict
636
637 638 -def _SplitIdentKeyVal(opt, value, parse_prefixes):
639 """Helper function to parse "ident:key=val,key=val" options. 640 641 @type opt: string 642 @param opt: option name, used in error messages 643 @type value: string 644 @param value: expected to be in the format "ident:key=val,key=val,..." 645 @type parse_prefixes: bool 646 @param parse_prefixes: whether to handle prefixes specially (see 647 L{_SplitKeyVal}) 648 @rtype: tuple 649 @return: (ident, {key=val, key=val}) 650 @raises errors.ParameterError: in case of duplicates or other parsing errors 651 652 """ 653 if ":" not in value: 654 ident, rest = value, "" 655 else: 656 ident, rest = value.split(":", 1) 657 658 if parse_prefixes and ident.startswith(NO_PREFIX): 659 if rest: 660 msg = "Cannot pass options when removing parameter groups: %s" % value 661 raise errors.ParameterError(msg) 662 retval = (ident[len(NO_PREFIX):], False) 663 elif (parse_prefixes and ident.startswith(UN_PREFIX) and 664 (len(ident) <= len(UN_PREFIX) or not ident[len(UN_PREFIX)].isdigit())): 665 if rest: 666 msg = "Cannot pass options when removing parameter groups: %s" % value 667 raise errors.ParameterError(msg) 668 retval = (ident[len(UN_PREFIX):], None) 669 else: 670 kv_dict = _SplitKeyVal(opt, rest, parse_prefixes) 671 retval = (ident, kv_dict) 672 return retval
673
674 675 -def check_ident_key_val(option, opt, value): # pylint: disable=W0613
676 """Custom parser for ident:key=val,key=val options. 677 678 This will store the parsed values as a tuple (ident, {key: val}). As such, 679 multiple uses of this option via action=append is possible. 680 681 """ 682 return _SplitIdentKeyVal(opt, value, True) 683
684 685 -def check_key_val(option, opt, value): # pylint: disable=W0613
686 """Custom parser class for key=val,key=val options. 687 688 This will store the parsed values as a dict {key: val}. 689 690 """ 691 return _SplitKeyVal(opt, value, True) 692
693 694 -def _SplitListKeyVal(opt, value):
695 retval = {} 696 for elem in value.split("/"): 697 if not elem: 698 raise errors.ParameterError("Empty section in option '%s'" % opt) 699 (ident, valdict) = _SplitIdentKeyVal(opt, elem, False) 700 if ident in retval: 701 msg = ("Duplicated parameter '%s' in parsing %s: %s" % 702 (ident, opt, elem)) 703 raise errors.ParameterError(msg) 704 retval[ident] = valdict 705 return retval
706
707 708 -def check_multilist_ident_key_val(_, opt, value):
709 """Custom parser for "ident:key=val,key=val/ident:key=val//ident:.." options. 710 711 @rtype: list of dictionary 712 @return: [{ident: {key: val, key: val}, ident: {key: val}}, {ident:..}] 713 714 """ 715 retval = [] 716 for line in value.split("//"): 717 retval.append(_SplitListKeyVal(opt, line)) 718 return retval
719
720 721 -def check_bool(option, opt, value): # pylint: disable=W0613
722 """Custom parser for yes/no options. 723 724 This will store the parsed value as either True or False. 725 726 """ 727 value = value.lower() 728 if value == constants.VALUE_FALSE or value == "no": 729 return False 730 elif value == constants.VALUE_TRUE or value == "yes": 731 return True 732 else: 733 raise errors.ParameterError("Invalid boolean value '%s'" % value) 734
735 736 -def check_list(option, opt, value): # pylint: disable=W0613
737 """Custom parser for comma-separated lists. 738 739 """ 740 # we have to make this explicit check since "".split(",") is [""], 741 # not an empty list :( 742 if not value: 743 return [] 744 else: 745 return utils.UnescapeAndSplit(value) 746
747 748 -def check_maybefloat(option, opt, value): # pylint: disable=W0613
749 """Custom parser for float numbers which might be also defaults. 750 751 """ 752 value = value.lower() 753 754 if value == constants.VALUE_DEFAULT: 755 return value 756 else: 757 return float(value) 758 759 760 # completion_suggestion is normally a list. Using numeric values not evaluating 761 # to False for dynamic completion. 762 (OPT_COMPL_MANY_NODES, 763 OPT_COMPL_ONE_NODE, 764 OPT_COMPL_ONE_INSTANCE, 765 OPT_COMPL_ONE_OS, 766 OPT_COMPL_ONE_EXTSTORAGE, 767 OPT_COMPL_ONE_IALLOCATOR, 768 OPT_COMPL_ONE_NETWORK, 769 OPT_COMPL_INST_ADD_NODES, 770 OPT_COMPL_ONE_NODEGROUP) = range(100, 109) 771 772 OPT_COMPL_ALL = compat.UniqueFrozenset([ 773 OPT_COMPL_MANY_NODES, 774 OPT_COMPL_ONE_NODE, 775 OPT_COMPL_ONE_INSTANCE, 776 OPT_COMPL_ONE_OS, 777 OPT_COMPL_ONE_EXTSTORAGE, 778 OPT_COMPL_ONE_IALLOCATOR, 779 OPT_COMPL_ONE_NETWORK, 780 OPT_COMPL_INST_ADD_NODES, 781 OPT_COMPL_ONE_NODEGROUP, 782 ])
783 784 785 -class CliOption(Option):
786 """Custom option class for optparse. 787 788 """ 789 ATTRS = Option.ATTRS + [ 790 "completion_suggest", 791 ] 792 TYPES = Option.TYPES + ( 793 "multilistidentkeyval", 794 "identkeyval", 795 "keyval", 796 "unit", 797 "bool", 798 "list", 799 "maybefloat", 800 ) 801 TYPE_CHECKER = Option.TYPE_CHECKER.copy() 802 TYPE_CHECKER["multilistidentkeyval"] = check_multilist_ident_key_val 803 TYPE_CHECKER["identkeyval"] = check_ident_key_val 804 TYPE_CHECKER["keyval"] = check_key_val 805 TYPE_CHECKER["unit"] = check_unit 806 TYPE_CHECKER["bool"] = check_bool 807 TYPE_CHECKER["list"] = check_list 808 TYPE_CHECKER["maybefloat"] = check_maybefloat
809 810 811 # optparse.py sets make_option, so we do it for our own option class, too 812 cli_option = CliOption 813 814 815 _YORNO = "yes|no" 816 817 DEBUG_OPT = cli_option("-d", "--debug", default=0, action="count", 818 help="Increase debugging level") 819 820 NOHDR_OPT = cli_option("--no-headers", default=False, 821 action="store_true", dest="no_headers", 822 help="Don't display column headers") 823 824 SEP_OPT = cli_option("--separator", default=None, 825 action="store", dest="separator", 826 help=("Separator between output fields" 827 " (defaults to one space)")) 828 829 USEUNITS_OPT = cli_option("--units", default=None, 830 dest="units", choices=("h", "m", "g", "t"), 831 help="Specify units for output (one of h/m/g/t)") 832 833 FIELDS_OPT = cli_option("-o", "--output", dest="output", action="store", 834 type="string", metavar="FIELDS", 835 help="Comma separated list of output fields") 836 837 FORCE_OPT = cli_option("-f", "--force", dest="force", action="store_true", 838 default=False, help="Force the operation") 839 840 CONFIRM_OPT = cli_option("--yes", dest="confirm", action="store_true", 841 default=False, help="Do not require confirmation") 842 843 IGNORE_OFFLINE_OPT = cli_option("--ignore-offline", dest="ignore_offline", 844 action="store_true", default=False, 845 help=("Ignore offline nodes and do as much" 846 " as possible")) 847 848 TAG_ADD_OPT = cli_option("--tags", dest="tags", 849 default=None, help="Comma-separated list of instance" 850 " tags") 851 852 TAG_SRC_OPT = cli_option("--from", dest="tags_source", 853 default=None, help="File with tag names") 854 855 SUBMIT_OPT = cli_option("--submit", dest="submit_only", 856 default=False, action="store_true", 857 help=("Submit the job and return the job ID, but" 858 " don't wait for the job to finish")) 859 860 PRINT_JOBID_OPT = cli_option("--print-jobid", dest="print_jobid", 861 default=False, action="store_true", 862 help=("Additionally print the job as first line" 863 " on stdout (for scripting).")) 864 865 SEQUENTIAL_OPT = cli_option("--sequential", dest="sequential", 866 default=False, action="store_true", 867 help=("Execute all resulting jobs sequentially")) 868 869 SYNC_OPT = cli_option("--sync", dest="do_locking", 870 default=False, action="store_true", 871 help=("Grab locks while doing the queries" 872 " in order to ensure more consistent results")) 873 874 DRY_RUN_OPT = cli_option("--dry-run", default=False, 875 action="store_true", 876 help=("Do not execute the operation, just run the" 877 " check steps and verify if it could be" 878 " executed")) 879 880 VERBOSE_OPT = cli_option("-v", "--verbose", default=False, 881 action="store_true", 882 help="Increase the verbosity of the operation") 883 884 DEBUG_SIMERR_OPT = cli_option("--debug-simulate-errors", default=False, 885 action="store_true", dest="simulate_errors", 886 help="Debugging option that makes the operation" 887 " treat most runtime checks as failed") 888 889 NWSYNC_OPT = cli_option("--no-wait-for-sync", dest="wait_for_sync", 890 default=True, action="store_false", 891 help="Don't wait for sync (DANGEROUS!)") 892 893 WFSYNC_OPT = cli_option("--wait-for-sync", dest="wait_for_sync", 894 default=False, action="store_true", 895 help="Wait for disks to sync") 896 897 ONLINE_INST_OPT = cli_option("--online", dest="online_inst", 898 action="store_true", default=False, 899 help="Enable offline instance") 900 901 OFFLINE_INST_OPT = cli_option("--offline", dest="offline_inst", 902 action="store_true", default=False, 903 help="Disable down instance") 904 905 DISK_TEMPLATE_OPT = cli_option("-t", "--disk-template", dest="disk_template", 906 help=("Custom disk setup (%s)" % 907 utils.CommaJoin(constants.DISK_TEMPLATES)), 908 default=None, metavar="TEMPL", 909 choices=list(constants.DISK_TEMPLATES)) 910 911 NONICS_OPT = cli_option("--no-nics", default=False, action="store_true", 912 help="Do not create any network cards for" 913 " the instance") 914 915 FILESTORE_DIR_OPT = cli_option("--file-storage-dir", dest="file_storage_dir", 916 help="Relative path under default cluster-wide" 917 " file storage dir to store file-based disks", 918 default=None, metavar="<DIR>") 919 920 FILESTORE_DRIVER_OPT = cli_option("--file-driver", dest="file_driver", 921 help="Driver to use for image files", 922 default=None, metavar="<DRIVER>", 923 choices=list(constants.FILE_DRIVER)) 924 925 IALLOCATOR_OPT = cli_option("-I", "--iallocator", metavar="<NAME>", 926 help="Select nodes for the instance automatically" 927 " using the <NAME> iallocator plugin", 928 default=None, type="string", 929 completion_suggest=OPT_COMPL_ONE_IALLOCATOR) 930 931 DEFAULT_IALLOCATOR_OPT = cli_option("-I", "--default-iallocator", 932 metavar="<NAME>", 933 help="Set the default instance" 934 " allocator plugin", 935 default=None, type="string", 936 completion_suggest=OPT_COMPL_ONE_IALLOCATOR) 937 938 DEFAULT_IALLOCATOR_PARAMS_OPT = cli_option("--default-iallocator-params", 939 dest="default_iallocator_params", 940 help="iallocator template" 941 " parameters, in the format" 942 " template:option=value," 943 " option=value,...", 944 type="keyval", 945 default=None) 946 947 OS_OPT = cli_option("-o", "--os-type", dest="os", help="What OS to run", 948 metavar="<os>", 949 completion_suggest=OPT_COMPL_ONE_OS) 950 951 OSPARAMS_OPT = cli_option("-O", "--os-parameters", dest="osparams", 952 type="keyval", default={}, 953 help="OS parameters") 954 955 FORCE_VARIANT_OPT = cli_option("--force-variant", dest="force_variant", 956 action="store_true", default=False, 957 help="Force an unknown variant") 958 959 NO_INSTALL_OPT = cli_option("--no-install", dest="no_install", 960 action="store_true", default=False, 961 help="Do not install the OS (will" 962 " enable no-start)") 963 964 NORUNTIME_CHGS_OPT = cli_option("--no-runtime-changes", 965 dest="allow_runtime_chgs", 966 default=True, action="store_false", 967 help="Don't allow runtime changes") 968 969 BACKEND_OPT = cli_option("-B", "--backend-parameters", dest="beparams", 970 type="keyval", default={}, 971 help="Backend parameters") 972 973 HVOPTS_OPT = cli_option("-H", "--hypervisor-parameters", type="keyval", 974 default={}, dest="hvparams", 975 help="Hypervisor parameters") 976 977 DISK_PARAMS_OPT = cli_option("-D", "--disk-parameters", dest="diskparams", 978 help="Disk template parameters, in the format" 979 " template:option=value,option=value,...", 980 type="identkeyval", action="append", default=[]) 981 982 SPECS_MEM_SIZE_OPT = cli_option("--specs-mem-size", dest="ispecs_mem_size", 983 type="keyval", default={}, 984 help="Memory size specs: list of key=value," 985 " where key is one of min, max, std" 986 " (in MB or using a unit)") 987 988 SPECS_CPU_COUNT_OPT = cli_option("--specs-cpu-count", dest="ispecs_cpu_count", 989 type="keyval", default={}, 990 help="CPU count specs: list of key=value," 991 " where key is one of min, max, std") 992 993 SPECS_DISK_COUNT_OPT = cli_option("--specs-disk-count", 994 dest="ispecs_disk_count", 995 type="keyval", default={}, 996 help="Disk count specs: list of key=value," 997 " where key is one of min, max, std") 998 999 SPECS_DISK_SIZE_OPT = cli_option("--specs-disk-size", dest="ispecs_disk_size", 1000 type="keyval", default={}, 1001 help="Disk size specs: list of key=value," 1002 " where key is one of min, max, std" 1003 " (in MB or using a unit)") 1004 1005 SPECS_NIC_COUNT_OPT = cli_option("--specs-nic-count", dest="ispecs_nic_count", 1006 type="keyval", default={}, 1007 help="NIC count specs: list of key=value," 1008 " where key is one of min, max, std") 1009 1010 IPOLICY_BOUNDS_SPECS_STR = "--ipolicy-bounds-specs" 1011 IPOLICY_BOUNDS_SPECS_OPT = cli_option(IPOLICY_BOUNDS_SPECS_STR, 1012 dest="ipolicy_bounds_specs", 1013 type="multilistidentkeyval", default=None, 1014 help="Complete instance specs limits") 1015 1016 IPOLICY_STD_SPECS_STR = "--ipolicy-std-specs" 1017 IPOLICY_STD_SPECS_OPT = cli_option(IPOLICY_STD_SPECS_STR, 1018 dest="ipolicy_std_specs", 1019 type="keyval", default=None, 1020 help="Complete standard instance specs") 1021 1022 IPOLICY_DISK_TEMPLATES = cli_option("--ipolicy-disk-templates", 1023 dest="ipolicy_disk_templates", 1024 type="list", default=None, 1025 help="Comma-separated list of" 1026 " enabled disk templates") 1027 1028 IPOLICY_VCPU_RATIO = cli_option("--ipolicy-vcpu-ratio", 1029 dest="ipolicy_vcpu_ratio", 1030 type="maybefloat", default=None, 1031 help="The maximum allowed vcpu-to-cpu ratio") 1032 1033 IPOLICY_SPINDLE_RATIO = cli_option("--ipolicy-spindle-ratio", 1034 dest="ipolicy_spindle_ratio", 1035 type="maybefloat", default=None, 1036 help=("The maximum allowed instances to" 1037 " spindle ratio")) 1038 1039 HYPERVISOR_OPT = cli_option("-H", "--hypervisor-parameters", dest="hypervisor", 1040 help="Hypervisor and hypervisor options, in the" 1041 " format hypervisor:option=value,option=value,...", 1042 default=None, type="identkeyval") 1043 1044 HVLIST_OPT = cli_option("-H", "--hypervisor-parameters", dest="hvparams", 1045 help="Hypervisor and hypervisor options, in the" 1046 " format hypervisor:option=value,option=value,...", 1047 default=[], action="append", type="identkeyval") 1048 1049 NOIPCHECK_OPT = cli_option("--no-ip-check", dest="ip_check", default=True, 1050 action="store_false", 1051 help="Don't check that the instance's IP" 1052 " is alive") 1053 1054 NONAMECHECK_OPT = cli_option("--no-name-check", dest="name_check", 1055 default=True, action="store_false", 1056 help="Don't check that the instance's name" 1057 " is resolvable") 1058 1059 NET_OPT = cli_option("--net", 1060 help="NIC parameters", default=[], 1061 dest="nics", action="append", type="identkeyval") 1062 1063 DISK_OPT = cli_option("--disk", help="Disk parameters", default=[], 1064 dest="disks", action="append", type="identkeyval") 1065 1066 DISKIDX_OPT = cli_option("--disks", dest="disks", default=None, 1067 help="Comma-separated list of disks" 1068 " indices to act on (e.g. 0,2) (optional," 1069 " defaults to all disks)") 1070 1071 OS_SIZE_OPT = cli_option("-s", "--os-size", dest="sd_size", 1072 help="Enforces a single-disk configuration using the" 1073 " given disk size, in MiB unless a suffix is used", 1074 default=None, type="unit", metavar="<size>") 1075 1076 IGNORE_CONSIST_OPT = cli_option("--ignore-consistency", 1077 dest="ignore_consistency", 1078 action="store_true", default=False, 1079 help="Ignore the consistency of the disks on" 1080 " the secondary") 1081 1082 ALLOW_FAILOVER_OPT = cli_option("--allow-failover", 1083 dest="allow_failover", 1084 action="store_true", default=False, 1085 help="If migration is not possible fallback to" 1086 " failover") 1087 1088 FORCE_FAILOVER_OPT = cli_option("--force-failover", 1089 dest="force_failover", 1090 action="store_true", default=False, 1091 help="Do not use migration, always use" 1092 " failover") 1093 1094 NONLIVE_OPT = cli_option("--non-live", dest="live", 1095 default=True, action="store_false", 1096 help="Do a non-live migration (this usually means" 1097 " freeze the instance, save the state, transfer and" 1098 " only then resume running on the secondary node)") 1099 1100 MIGRATION_MODE_OPT = cli_option("--migration-mode", dest="migration_mode", 1101 default=None, 1102 choices=list(constants.HT_MIGRATION_MODES), 1103 help="Override default migration mode (choose" 1104 " either live or non-live") 1105 1106 NODE_PLACEMENT_OPT = cli_option("-n", "--node", dest="node", 1107 help="Target node and optional secondary node", 1108 metavar="<pnode>[:<snode>]", 1109 completion_suggest=OPT_COMPL_INST_ADD_NODES) 1110 1111 NODE_LIST_OPT = cli_option("-n", "--node", dest="nodes", default=[], 1112 action="append", metavar="<node>", 1113 help="Use only this node (can be used multiple" 1114 " times, if not given defaults to all nodes)", 1115 completion_suggest=OPT_COMPL_ONE_NODE) 1116 1117 NODEGROUP_OPT_NAME = "--node-group" 1118 NODEGROUP_OPT = cli_option("-g", NODEGROUP_OPT_NAME, 1119 dest="nodegroup", 1120 help="Node group (name or uuid)", 1121 metavar="<nodegroup>", 1122 default=None, type="string", 1123 completion_suggest=OPT_COMPL_ONE_NODEGROUP) 1124 1125 SINGLE_NODE_OPT = cli_option("-n", "--node", dest="node", help="Target node", 1126 metavar="<node>", 1127 completion_suggest=OPT_COMPL_ONE_NODE) 1128 1129 NOSTART_OPT = cli_option("--no-start", dest="start", default=True, 1130 action="store_false", 1131 help="Don't start the instance after creation") 1132 1133 SHOWCMD_OPT = cli_option("--show-cmd", dest="show_command", 1134 action="store_true", default=False, 1135 help="Show command instead of executing it") 1136 1137 CLEANUP_OPT = cli_option("--cleanup", dest="cleanup", 1138 default=False, action="store_true", 1139 help="Instead of performing the migration/failover," 1140 " try to recover from a failed cleanup. This is safe" 1141 " to run even if the instance is healthy, but it" 1142 " will create extra replication traffic and " 1143 " disrupt briefly the replication (like during the" 1144 " migration/failover") 1145 1146 STATIC_OPT = cli_option("-s", "--static", dest="static", 1147 action="store_true", default=False, 1148 help="Only show configuration data, not runtime data") 1149 1150 ALL_OPT = cli_option("--all", dest="show_all", 1151 default=False, action="store_true", 1152 help="Show info on all instances on the cluster." 1153 " This can take a long time to run, use wisely") 1154 1155 SELECT_OS_OPT = cli_option("--select-os", dest="select_os", 1156 action="store_true", default=False, 1157 help="Interactive OS reinstall, lists available" 1158 " OS templates for selection") 1159 1160 IGNORE_FAILURES_OPT = cli_option("--ignore-failures", dest="ignore_failures", 1161 action="store_true", default=False, 1162 help="Remove the instance from the cluster" 1163 " configuration even if there are failures" 1164 " during the removal process") 1165 1166 IGNORE_REMOVE_FAILURES_OPT = cli_option("--ignore-remove-failures", 1167 dest="ignore_remove_failures", 1168 action="store_true", default=False, 1169 help="Remove the instance from the" 1170 " cluster configuration even if there" 1171 " are failures during the removal" 1172 " process") 1173 1174 REMOVE_INSTANCE_OPT = cli_option("--remove-instance", dest="remove_instance", 1175 action="store_true", default=False, 1176 help="Remove the instance from the cluster") 1177 1178 DST_NODE_OPT = cli_option("-n", "--target-node", dest="dst_node", 1179 help="Specifies the new node for the instance", 1180 metavar="NODE", default=None, 1181 completion_suggest=OPT_COMPL_ONE_NODE) 1182 1183 NEW_SECONDARY_OPT = cli_option("-n", "--new-secondary", dest="dst_node", 1184 help="Specifies the new secondary node", 1185 metavar="NODE", default=None, 1186 completion_suggest=OPT_COMPL_ONE_NODE) 1187 1188 NEW_PRIMARY_OPT = cli_option("--new-primary", dest="new_primary_node", 1189 help="Specifies the new primary node", 1190 metavar="<node>", default=None, 1191 completion_suggest=OPT_COMPL_ONE_NODE) 1192 1193 ON_PRIMARY_OPT = cli_option("-p", "--on-primary", dest="on_primary", 1194 default=False, action="store_true", 1195 help="Replace the disk(s) on the primary" 1196 " node (applies only to internally mirrored" 1197 " disk templates, e.g. %s)" % 1198 utils.CommaJoin(constants.DTS_INT_MIRROR)) 1199 1200 ON_SECONDARY_OPT = cli_option("-s", "--on-secondary", dest="on_secondary", 1201 default=False, action="store_true", 1202 help="Replace the disk(s) on the secondary" 1203 " node (applies only to internally mirrored" 1204 " disk templates, e.g. %s)" % 1205 utils.CommaJoin(constants.DTS_INT_MIRROR)) 1206 1207 AUTO_PROMOTE_OPT = cli_option("--auto-promote", dest="auto_promote", 1208 default=False, action="store_true", 1209 help="Lock all nodes and auto-promote as needed" 1210 " to MC status") 1211 1212 AUTO_REPLACE_OPT = cli_option("-a", "--auto", dest="auto", 1213 default=False, action="store_true", 1214 help="Automatically replace faulty disks" 1215 " (applies only to internally mirrored" 1216 " disk templates, e.g. %s)" % 1217 utils.CommaJoin(constants.DTS_INT_MIRROR)) 1218 1219 IGNORE_SIZE_OPT = cli_option("--ignore-size", dest="ignore_size", 1220 default=False, action="store_true", 1221 help="Ignore current recorded size" 1222 " (useful for forcing activation when" 1223 " the recorded size is wrong)") 1224 1225 SRC_NODE_OPT = cli_option("--src-node", dest="src_node", help="Source node", 1226 metavar="<node>", 1227 completion_suggest=OPT_COMPL_ONE_NODE) 1228 1229 SRC_DIR_OPT = cli_option("--src-dir", dest="src_dir", help="Source directory", 1230 metavar="<dir>") 1231 1232 SECONDARY_IP_OPT = cli_option("-s", "--secondary-ip", dest="secondary_ip", 1233 help="Specify the secondary ip for the node", 1234 metavar="ADDRESS", default=None) 1235 1236 READD_OPT = cli_option("--readd", dest="readd", 1237 default=False, action="store_true", 1238 help="Readd old node after replacing it") 1239 1240 NOSSH_KEYCHECK_OPT = cli_option("--no-ssh-key-check", dest="ssh_key_check", 1241 default=True, action="store_false", 1242 help="Disable SSH key fingerprint checking") 1243 1244 NODE_FORCE_JOIN_OPT = cli_option("--force-join", dest="force_join", 1245 default=False, action="store_true", 1246 help="Force the joining of a node") 1247 1248 MC_OPT = cli_option("-C", "--master-candidate", dest="master_candidate", 1249 type="bool", default=None, metavar=_YORNO, 1250 help="Set the master_candidate flag on the node") 1251 1252 OFFLINE_OPT = cli_option("-O", "--offline", dest="offline", metavar=_YORNO, 1253 type="bool", default=None, 1254 help=("Set the offline flag on the node" 1255 " (cluster does not communicate with offline" 1256 " nodes)")) 1257 1258 DRAINED_OPT = cli_option("-D", "--drained", dest="drained", metavar=_YORNO, 1259 type="bool", default=None, 1260 help=("Set the drained flag on the node" 1261 " (excluded from allocation operations)")) 1262 1263 CAPAB_MASTER_OPT = cli_option("--master-capable", dest="master_capable", 1264 type="bool", default=None, metavar=_YORNO, 1265 help="Set the master_capable flag on the node") 1266 1267 CAPAB_VM_OPT = cli_option("--vm-capable", dest="vm_capable", 1268 type="bool", default=None, metavar=_YORNO, 1269 help="Set the vm_capable flag on the node") 1270 1271 ALLOCATABLE_OPT = cli_option("--allocatable", dest="allocatable", 1272 type="bool", default=None, metavar=_YORNO, 1273 help="Set the allocatable flag on a volume") 1274 1275 ENABLED_HV_OPT = cli_option("--enabled-hypervisors", 1276 dest="enabled_hypervisors", 1277 help="Comma-separated list of hypervisors", 1278 type="string", default=None) 1279 1280 ENABLED_DISK_TEMPLATES_OPT = cli_option("--enabled-disk-templates", 1281 dest="enabled_disk_templates", 1282 help="Comma-separated list of " 1283 "disk templates", 1284 type="string", default=None) 1285 1286 ENABLED_USER_SHUTDOWN_OPT = cli_option("--user-shutdown", 1287 default=None, 1288 dest="enabled_user_shutdown", 1289 help="Whether user shutdown is enabled", 1290 type="bool") 1291 1292 NIC_PARAMS_OPT = cli_option("-N", "--nic-parameters", dest="nicparams", 1293 type="keyval", default={}, 1294 help="NIC parameters") 1295 1296 CP_SIZE_OPT = cli_option("-C", "--candidate-pool-size", default=None, 1297 dest="candidate_pool_size", type="int", 1298 help="Set the candidate pool size") 1299 1300 RQL_OPT = cli_option("--max-running-jobs", dest="max_running_jobs", 1301 type="int", help="Set the maximal number of jobs to " 1302 "run simultaneously") 1303 1304 VG_NAME_OPT = cli_option("--vg-name", dest="vg_name", 1305 help=("Enables LVM and specifies the volume group" 1306 " name (cluster-wide) for disk allocation" 1307 " [%s]" % constants.DEFAULT_VG), 1308 metavar="VG", default=None) 1309 1310 YES_DOIT_OPT = cli_option("--yes-do-it", "--ya-rly", dest="yes_do_it", 1311 help="Destroy cluster", action="store_true") 1312 1313 NOVOTING_OPT = cli_option("--no-voting", dest="no_voting", 1314 help="Skip node agreement check (dangerous)", 1315 action="store_true", default=False) 1316 1317 MAC_PREFIX_OPT = cli_option("-m", "--mac-prefix", dest="mac_prefix", 1318 help="Specify the mac prefix for the instance IP" 1319 " addresses, in the format XX:XX:XX", 1320 metavar="PREFIX", 1321 default=None) 1322 1323 MASTER_NETDEV_OPT = cli_option("--master-netdev", dest="master_netdev", 1324 help="Specify the node interface (cluster-wide)" 1325 " on which the master IP address will be added" 1326 " (cluster init default: %s)" % 1327 constants.DEFAULT_BRIDGE, 1328 metavar="NETDEV", 1329 default=None) 1330 1331 MASTER_NETMASK_OPT = cli_option("--master-netmask", dest="master_netmask", 1332 help="Specify the netmask of the master IP", 1333 metavar="NETMASK", 1334 default=None) 1335 1336 USE_EXTERNAL_MIP_SCRIPT = cli_option("--use-external-mip-script", 1337 dest="use_external_mip_script", 1338 help="Specify whether to run a" 1339 " user-provided script for the master" 1340 " IP address turnup and" 1341 " turndown operations", 1342 type="bool", metavar=_YORNO, default=None) 1343 1344 GLOBAL_FILEDIR_OPT = cli_option("--file-storage-dir", dest="file_storage_dir", 1345 help="Specify the default directory (cluster-" 1346 "wide) for storing the file-based disks [%s]" % 1347 pathutils.DEFAULT_FILE_STORAGE_DIR, 1348 metavar="DIR", 1349 default=None) 1350 1351 GLOBAL_SHARED_FILEDIR_OPT = cli_option( 1352 "--shared-file-storage-dir", 1353 dest="shared_file_storage_dir", 1354 help="Specify the default directory (cluster-wide) for storing the" 1355 " shared file-based disks [%s]" % 1356 pathutils.DEFAULT_SHARED_FILE_STORAGE_DIR, 1357 metavar="SHAREDDIR", default=None) 1358 1359 GLOBAL_GLUSTER_FILEDIR_OPT = cli_option( 1360 "--gluster-storage-dir", 1361 dest="gluster_storage_dir", 1362 help="Specify the default directory (cluster-wide) for mounting Gluster" 1363 " file systems [%s]" % 1364 pathutils.DEFAULT_GLUSTER_STORAGE_DIR, 1365 metavar="GLUSTERDIR", 1366 default=pathutils.DEFAULT_GLUSTER_STORAGE_DIR) 1367 1368 NOMODIFY_ETCHOSTS_OPT = cli_option("--no-etc-hosts", dest="modify_etc_hosts", 1369 help="Don't modify %s" % pathutils.ETC_HOSTS, 1370 action="store_false", default=True) 1371 1372 MODIFY_ETCHOSTS_OPT = \ 1373 cli_option("--modify-etc-hosts", dest="modify_etc_hosts", metavar=_YORNO, 1374 default=None, type="bool", 1375 help="Defines whether the cluster should autonomously modify" 1376 " and keep in sync the /etc/hosts file of the nodes") 1377 1378 NOMODIFY_SSH_SETUP_OPT = cli_option("--no-ssh-init", dest="modify_ssh_setup", 1379 help="Don't initialize SSH keys", 1380 action="store_false", default=True) 1381 1382 ERROR_CODES_OPT = cli_option("--error-codes", dest="error_codes", 1383 help="Enable parseable error messages", 1384 action="store_true", default=False) 1385 1386 NONPLUS1_OPT = cli_option("--no-nplus1-mem", dest="skip_nplusone_mem", 1387 help="Skip N+1 memory redundancy tests", 1388 action="store_true", default=False) 1389 1390 REBOOT_TYPE_OPT = cli_option("-t", "--type", dest="reboot_type", 1391 help="Type of reboot: soft/hard/full", 1392 default=constants.INSTANCE_REBOOT_HARD, 1393 metavar="<REBOOT>", 1394 choices=list(constants.REBOOT_TYPES)) 1395 1396 IGNORE_SECONDARIES_OPT = cli_option("--ignore-secondaries", 1397 dest="ignore_secondaries", 1398 default=False, action="store_true", 1399 help="Ignore errors from secondaries") 1400 1401 NOSHUTDOWN_OPT = cli_option("--noshutdown", dest="shutdown", 1402 action="store_false", default=True, 1403 help="Don't shutdown the instance (unsafe)") 1404 1405 TIMEOUT_OPT = cli_option("--timeout", dest="timeout", type="int", 1406 default=constants.DEFAULT_SHUTDOWN_TIMEOUT, 1407 help="Maximum time to wait") 1408 1409 COMPRESS_OPT = cli_option("--compress", dest="compress", 1410 default=constants.IEC_NONE, 1411 help="The compression mode to use", 1412 choices=list(constants.IEC_ALL)) 1413 1414 TRANSPORT_COMPRESSION_OPT = \ 1415 cli_option("--transport-compression", dest="transport_compression", 1416 default=constants.IEC_NONE, choices=list(constants.IEC_ALL), 1417 help="The compression mode to use during transport") 1418 1419 SHUTDOWN_TIMEOUT_OPT = cli_option("--shutdown-timeout", 1420 dest="shutdown_timeout", type="int", 1421 default=constants.DEFAULT_SHUTDOWN_TIMEOUT, 1422 help="Maximum time to wait for instance" 1423 " shutdown") 1424 1425 INTERVAL_OPT = cli_option("--interval", dest="interval", type="int", 1426 default=None, 1427 help=("Number of seconds between repetions of the" 1428 " command")) 1429 1430 EARLY_RELEASE_OPT = cli_option("--early-release", 1431 dest="early_release", default=False, 1432 action="store_true", 1433 help="Release the locks on the secondary" 1434 " node(s) early") 1435 1436 NEW_CLUSTER_CERT_OPT = cli_option("--new-cluster-certificate", 1437 dest="new_cluster_cert", 1438 default=False, action="store_true", 1439 help="Generate a new cluster certificate") 1440 1441 NEW_NODE_CERT_OPT = cli_option( 1442 "--new-node-certificates", dest="new_node_cert", default=False, 1443 action="store_true", help="Generate new node certificates (for all nodes)") 1444 1445 RAPI_CERT_OPT = cli_option("--rapi-certificate", dest="rapi_cert", 1446 default=None, 1447 help="File containing new RAPI certificate") 1448 1449 NEW_RAPI_CERT_OPT = cli_option("--new-rapi-certificate", dest="new_rapi_cert", 1450 default=None, action="store_true", 1451 help=("Generate a new self-signed RAPI" 1452 " certificate")) 1453 1454 SPICE_CERT_OPT = cli_option("--spice-certificate", dest="spice_cert", 1455 default=None, 1456 help="File containing new SPICE certificate") 1457 1458 SPICE_CACERT_OPT = cli_option("--spice-ca-certificate", dest="spice_cacert", 1459 default=None, 1460 help="File containing the certificate of the CA" 1461 " which signed the SPICE certificate") 1462 1463 NEW_SPICE_CERT_OPT = cli_option("--new-spice-certificate", 1464 dest="new_spice_cert", default=None, 1465 action="store_true", 1466 help=("Generate a new self-signed SPICE" 1467 " certificate")) 1468 1469 NEW_CONFD_HMAC_KEY_OPT = cli_option("--new-confd-hmac-key", 1470 dest="new_confd_hmac_key", 1471 default=False, action="store_true", 1472 help=("Create a new HMAC key for %s" % 1473 constants.CONFD)) 1474 1475 CLUSTER_DOMAIN_SECRET_OPT = cli_option("--cluster-domain-secret", 1476 dest="cluster_domain_secret", 1477 default=None, 1478 help=("Load new new cluster domain" 1479 " secret from file")) 1480 1481 NEW_CLUSTER_DOMAIN_SECRET_OPT = cli_option("--new-cluster-domain-secret", 1482 dest="new_cluster_domain_secret", 1483 default=False, action="store_true", 1484 help=("Create a new cluster domain" 1485 " secret")) 1486 1487 USE_REPL_NET_OPT = cli_option("--use-replication-network", 1488 dest="use_replication_network", 1489 help="Whether to use the replication network" 1490 " for talking to the nodes", 1491 action="store_true", default=False) 1492 1493 MAINTAIN_NODE_HEALTH_OPT = \ 1494 cli_option("--maintain-node-health", dest="maintain_node_health", 1495 metavar=_YORNO, default=None, type="bool", 1496 help="Configure the cluster to automatically maintain node" 1497 " health, by shutting down unknown instances, shutting down" 1498 " unknown DRBD devices, etc.") 1499 1500 IDENTIFY_DEFAULTS_OPT = \ 1501 cli_option("--identify-defaults", dest="identify_defaults", 1502 default=False, action="store_true", 1503 help="Identify which saved instance parameters are equal to" 1504 " the current cluster defaults and set them as such, instead" 1505 " of marking them as overridden") 1506 1507 UIDPOOL_OPT = cli_option("--uid-pool", default=None, 1508 action="store", dest="uid_pool", 1509 help=("A list of user-ids or user-id" 1510 " ranges separated by commas")) 1511 1512 ADD_UIDS_OPT = cli_option("--add-uids", default=None, 1513 action="store", dest="add_uids", 1514 help=("A list of user-ids or user-id" 1515 " ranges separated by commas, to be" 1516 " added to the user-id pool")) 1517 1518 REMOVE_UIDS_OPT = cli_option("--remove-uids", default=None, 1519 action="store", dest="remove_uids", 1520 help=("A list of user-ids or user-id" 1521 " ranges separated by commas, to be" 1522 " removed from the user-id pool")) 1523 1524 RESERVED_LVS_OPT = cli_option("--reserved-lvs", default=None, 1525 action="store", dest="reserved_lvs", 1526 help=("A comma-separated list of reserved" 1527 " logical volumes names, that will be" 1528 " ignored by cluster verify")) 1529 1530 ROMAN_OPT = cli_option("--roman", 1531 dest="roman_integers", default=False, 1532 action="store_true", 1533 help="Use roman numbers for positive integers") 1534 1535 DRBD_HELPER_OPT = cli_option("--drbd-usermode-helper", dest="drbd_helper", 1536 action="store", default=None, 1537 help="Specifies usermode helper for DRBD") 1538 1539 PRIMARY_IP_VERSION_OPT = \ 1540 cli_option("--primary-ip-version", default=constants.IP4_VERSION, 1541 action="store", dest="primary_ip_version", 1542 metavar="%d|%d" % (constants.IP4_VERSION, 1543 constants.IP6_VERSION), 1544 help="Cluster-wide IP version for primary IP") 1545 1546 SHOW_MACHINE_OPT = cli_option("-M", "--show-machine-names", default=False, 1547 action="store_true", 1548 help="Show machine name for every line in output") 1549 1550 FAILURE_ONLY_OPT = cli_option("--failure-only", default=False, 1551 action="store_true", 1552 help=("Hide successful results and show failures" 1553 " only (determined by the exit code)")) 1554 1555 REASON_OPT = cli_option("--reason", default=None, 1556 help="The reason for executing the command")
1557 1558 1559 -def _PriorityOptionCb(option, _, value, parser):
1560 """Callback for processing C{--priority} option. 1561 1562 """ 1563 value = _PRIONAME_TO_VALUE[value] 1564 1565 setattr(parser.values, option.dest, value)
1566 1567 1568 PRIORITY_OPT = cli_option("--priority", default=None, dest="priority", 1569 metavar="|".join(name for name, _ in _PRIORITY_NAMES), 1570 choices=_PRIONAME_TO_VALUE.keys(), 1571 action="callback", type="choice", 1572 callback=_PriorityOptionCb, 1573 help="Priority for opcode processing") 1574 1575 HID_OS_OPT = cli_option("--hidden", dest="hidden", 1576 type="bool", default=None, metavar=_YORNO, 1577 help="Sets the hidden flag on the OS") 1578 1579 BLK_OS_OPT = cli_option("--blacklisted", dest="blacklisted", 1580 type="bool", default=None, metavar=_YORNO, 1581 help="Sets the blacklisted flag on the OS") 1582 1583 PREALLOC_WIPE_DISKS_OPT = cli_option("--prealloc-wipe-disks", default=None, 1584 type="bool", metavar=_YORNO, 1585 dest="prealloc_wipe_disks", 1586 help=("Wipe disks prior to instance" 1587 " creation")) 1588 1589 NODE_PARAMS_OPT = cli_option("--node-parameters", dest="ndparams", 1590 type="keyval", default=None, 1591 help="Node parameters") 1592 1593 ALLOC_POLICY_OPT = cli_option("--alloc-policy", dest="alloc_policy", 1594 action="store", metavar="POLICY", default=None, 1595 help="Allocation policy for the node group") 1596 1597 NODE_POWERED_OPT = cli_option("--node-powered", default=None, 1598 type="bool", metavar=_YORNO, 1599 dest="node_powered", 1600 help="Specify if the SoR for node is powered") 1601 1602 OOB_TIMEOUT_OPT = cli_option("--oob-timeout", dest="oob_timeout", type="int", 1603 default=constants.OOB_TIMEOUT, 1604 help="Maximum time to wait for out-of-band helper") 1605 1606 POWER_DELAY_OPT = cli_option("--power-delay", dest="power_delay", type="float", 1607 default=constants.OOB_POWER_DELAY, 1608 help="Time in seconds to wait between power-ons") 1609 1610 FORCE_FILTER_OPT = cli_option("-F", "--filter", dest="force_filter", 1611 action="store_true", default=False, 1612 help=("Whether command argument should be treated" 1613 " as filter")) 1614 1615 NO_REMEMBER_OPT = cli_option("--no-remember", 1616 dest="no_remember", 1617 action="store_true", default=False, 1618 help="Perform but do not record the change" 1619 " in the configuration") 1620 1621 PRIMARY_ONLY_OPT = cli_option("-p", "--primary-only", 1622 default=False, action="store_true", 1623 help="Evacuate primary instances only") 1624 1625 SECONDARY_ONLY_OPT = cli_option("-s", "--secondary-only", 1626 default=False, action="store_true", 1627 help="Evacuate secondary instances only" 1628 " (applies only to internally mirrored" 1629 " disk templates, e.g. %s)" % 1630 utils.CommaJoin(constants.DTS_INT_MIRROR)) 1631 1632 STARTUP_PAUSED_OPT = cli_option("--paused", dest="startup_paused", 1633 action="store_true", default=False, 1634 help="Pause instance at startup") 1635 1636 TO_GROUP_OPT = cli_option("--to", dest="to", metavar="<group>", 1637 help="Destination node group (name or uuid)", 1638 default=None, action="append", 1639 completion_suggest=OPT_COMPL_ONE_NODEGROUP) 1640 1641 IGNORE_ERRORS_OPT = cli_option("-I", "--ignore-errors", default=[], 1642 action="append", dest="ignore_errors", 1643 choices=list(constants.CV_ALL_ECODES_STRINGS), 1644 help="Error code to be ignored") 1645 1646 DISK_STATE_OPT = cli_option("--disk-state", default=[], dest="disk_state", 1647 action="append", 1648 help=("Specify disk state information in the" 1649 " format" 1650 " storage_type/identifier:option=value,...;" 1651 " note this is unused for now"), 1652 type="identkeyval") 1653 1654 HV_STATE_OPT = cli_option("--hypervisor-state", default=[], dest="hv_state", 1655 action="append", 1656 help=("Specify hypervisor state information in the" 1657 " format hypervisor:option=value,...;" 1658 " note this is unused for now"), 1659 type="identkeyval") 1660 1661 IGNORE_IPOLICY_OPT = cli_option("--ignore-ipolicy", dest="ignore_ipolicy", 1662 action="store_true", default=False, 1663 help="Ignore instance policy violations") 1664 1665 RUNTIME_MEM_OPT = cli_option("-m", "--runtime-memory", dest="runtime_mem", 1666 help="Sets the instance's runtime memory," 1667 " ballooning it up or down to the new value", 1668 default=None, type="unit", metavar="<size>") 1669 1670 ABSOLUTE_OPT = cli_option("--absolute", dest="absolute", 1671 action="store_true", default=False, 1672 help="Marks the grow as absolute instead of the" 1673 " (default) relative mode") 1674 1675 NETWORK_OPT = cli_option("--network", 1676 action="store", default=None, dest="network", 1677 help="IP network in CIDR notation") 1678 1679 GATEWAY_OPT = cli_option("--gateway", 1680 action="store", default=None, dest="gateway", 1681 help="IP address of the router (gateway)") 1682 1683 ADD_RESERVED_IPS_OPT = cli_option("--add-reserved-ips", 1684 action="store", default=None, 1685 dest="add_reserved_ips", 1686 help="Comma-separated list of" 1687 " reserved IPs to add") 1688 1689 REMOVE_RESERVED_IPS_OPT = cli_option("--remove-reserved-ips", 1690 action="store", default=None, 1691 dest="remove_reserved_ips", 1692 help="Comma-delimited list of" 1693 " reserved IPs to remove") 1694 1695 NETWORK6_OPT = cli_option("--network6", 1696 action="store", default=None, dest="network6", 1697 help="IP network in CIDR notation") 1698 1699 GATEWAY6_OPT = cli_option("--gateway6", 1700 action="store", default=None, dest="gateway6", 1701 help="IP6 address of the router (gateway)") 1702 1703 NOCONFLICTSCHECK_OPT = cli_option("--no-conflicts-check", 1704 dest="conflicts_check", 1705 default=True, 1706 action="store_false", 1707 help="Don't check for conflicting IPs") 1708 1709 INCLUDEDEFAULTS_OPT = cli_option("--include-defaults", dest="include_defaults", 1710 default=False, action="store_true", 1711 help="Include default values") 1712 1713 HOTPLUG_OPT = cli_option("--hotplug", dest="hotplug", 1714 action="store_true", default=False, 1715 help="Hotplug supported devices (NICs and Disks)") 1716 1717 HOTPLUG_IF_POSSIBLE_OPT = cli_option("--hotplug-if-possible", 1718 dest="hotplug_if_possible", 1719 action="store_true", default=False, 1720 help="Hotplug devices in case" 1721 " hotplug is supported") 1722 1723 #: Options provided by all commands 1724 COMMON_OPTS = [DEBUG_OPT, REASON_OPT] 1725 1726 # options related to asynchronous job handling 1727 1728 SUBMIT_OPTS = [ 1729 SUBMIT_OPT, 1730 PRINT_JOBID_OPT, 1731 ] 1732 1733 # common options for creating instances. add and import then add their own 1734 # specific ones. 1735 COMMON_CREATE_OPTS = [ 1736 BACKEND_OPT, 1737 DISK_OPT, 1738 DISK_TEMPLATE_OPT, 1739 FILESTORE_DIR_OPT, 1740 FILESTORE_DRIVER_OPT, 1741 HYPERVISOR_OPT, 1742 IALLOCATOR_OPT, 1743 NET_OPT, 1744 NODE_PLACEMENT_OPT, 1745 NOIPCHECK_OPT, 1746 NOCONFLICTSCHECK_OPT, 1747 NONAMECHECK_OPT, 1748 NONICS_OPT, 1749 NWSYNC_OPT, 1750 OSPARAMS_OPT, 1751 OS_SIZE_OPT, 1752 SUBMIT_OPT, 1753 PRINT_JOBID_OPT, 1754 TAG_ADD_OPT, 1755 DRY_RUN_OPT, 1756 PRIORITY_OPT, 1757 ] 1758 1759 # common instance policy options 1760 INSTANCE_POLICY_OPTS = [ 1761 IPOLICY_BOUNDS_SPECS_OPT, 1762 IPOLICY_DISK_TEMPLATES, 1763 IPOLICY_VCPU_RATIO, 1764 IPOLICY_SPINDLE_RATIO, 1765 ] 1766 1767 # instance policy split specs options 1768 SPLIT_ISPECS_OPTS = [ 1769 SPECS_CPU_COUNT_OPT, 1770 SPECS_DISK_COUNT_OPT, 1771 SPECS_DISK_SIZE_OPT, 1772 SPECS_MEM_SIZE_OPT, 1773 SPECS_NIC_COUNT_OPT, 1774 ]
1775 1776 1777 -class _ShowUsage(Exception):
1778 """Exception class for L{_ParseArgs}. 1779 1780 """
1781 - def __init__(self, exit_error):
1782 """Initializes instances of this class. 1783 1784 @type exit_error: bool 1785 @param exit_error: Whether to report failure on exit 1786 1787 """ 1788 Exception.__init__(self) 1789 self.exit_error = exit_error
1790
1791 1792 -class _ShowVersion(Exception):
1793 """Exception class for L{_ParseArgs}. 1794 1795 """
1796
1797 1798 -def _ParseArgs(binary, argv, commands, aliases, env_override):
1799 """Parser for the command line arguments. 1800 1801 This function parses the arguments and returns the function which 1802 must be executed together with its (modified) arguments. 1803 1804 @param binary: Script name 1805 @param argv: Command line arguments 1806 @param commands: Dictionary containing command definitions 1807 @param aliases: dictionary with command aliases {"alias": "target", ...} 1808 @param env_override: list of env variables allowed for default args 1809 @raise _ShowUsage: If usage description should be shown 1810 @raise _ShowVersion: If version should be shown 1811 1812 """ 1813 assert not (env_override - set(commands)) 1814 assert not (set(aliases.keys()) & set(commands.keys())) 1815 1816 if len(argv) > 1: 1817 cmd = argv[1] 1818 else: 1819 # No option or command given 1820 raise _ShowUsage(exit_error=True) 1821 1822 if cmd == "--version": 1823 raise _ShowVersion() 1824 elif cmd == "--help": 1825 raise _ShowUsage(exit_error=False) 1826 elif not (cmd in commands or cmd in aliases): 1827 raise _ShowUsage(exit_error=True) 1828 1829 # get command, unalias it, and look it up in commands 1830 if cmd in aliases: 1831 if aliases[cmd] not in commands: 1832 raise errors.ProgrammerError("Alias '%s' maps to non-existing" 1833 " command '%s'" % (cmd, aliases[cmd])) 1834 1835 cmd = aliases[cmd] 1836 1837 if cmd in env_override: 1838 args_env_name = ("%s_%s" % (binary.replace("-", "_"), cmd)).upper() 1839 env_args = os.environ.get(args_env_name) 1840 if env_args: 1841 argv = utils.InsertAtPos(argv, 2, shlex.split(env_args)) 1842 1843 func, args_def, parser_opts, usage, description = commands[cmd] 1844 parser = OptionParser(option_list=parser_opts + COMMON_OPTS, 1845 description=description, 1846 formatter=TitledHelpFormatter(), 1847 usage="%%prog %s %s" % (cmd, usage)) 1848 parser.disable_interspersed_args() 1849 options, args = parser.parse_args(args=argv[2:]) 1850 1851 if not _CheckArguments(cmd, args_def, args): 1852 return None, None, None 1853 1854 return func, options, args
1855
1856 1857 -def _FormatUsage(binary, commands):
1858 """Generates a nice description of all commands. 1859 1860 @param binary: Script name 1861 @param commands: Dictionary containing command definitions 1862 1863 """ 1864 # compute the max line length for cmd + usage 1865 mlen = min(60, max(map(len, commands))) 1866 1867 yield "Usage: %s {command} [options...] [argument...]" % binary 1868 yield "%s <command> --help to see details, or man %s" % (binary, binary) 1869 yield "" 1870 yield "Commands:" 1871 1872 # and format a nice command list 1873 for (cmd, (_, _, _, _, help_text)) in sorted(commands.items()): 1874 help_lines = textwrap.wrap(help_text, 79 - 3 - mlen) 1875 yield " %-*s - %s" % (mlen, cmd, help_lines.pop(0)) 1876 for line in help_lines: 1877 yield " %-*s %s" % (mlen, "", line) 1878 1879 yield ""
1880
1881 1882 -def _CheckArguments(cmd, args_def, args):
1883 """Verifies the arguments using the argument definition. 1884 1885 Algorithm: 1886 1887 1. Abort with error if values specified by user but none expected. 1888 1889 1. For each argument in definition 1890 1891 1. Keep running count of minimum number of values (min_count) 1892 1. Keep running count of maximum number of values (max_count) 1893 1. If it has an unlimited number of values 1894 1895 1. Abort with error if it's not the last argument in the definition 1896 1897 1. If last argument has limited number of values 1898 1899 1. Abort with error if number of values doesn't match or is too large 1900 1901 1. Abort with error if user didn't pass enough values (min_count) 1902 1903 """ 1904 if args and not args_def: 1905 ToStderr("Error: Command %s expects no arguments", cmd) 1906 return False 1907 1908 min_count = None 1909 max_count = None 1910 check_max = None 1911 1912 last_idx = len(args_def) - 1 1913 1914 for idx, arg in enumerate(args_def): 1915 if min_count is None: 1916 min_count = arg.min 1917 elif arg.min is not None: 1918 min_count += arg.min 1919 1920 if max_count is None: 1921 max_count = arg.max 1922 elif arg.max is not None: 1923 max_count += arg.max 1924 1925 if idx == last_idx: 1926 check_max = (arg.max is not None) 1927 1928 elif arg.max is None: 1929 raise errors.ProgrammerError("Only the last argument can have max=None") 1930 1931 if check_max: 1932 # Command with exact number of arguments 1933 if (min_count is not None and max_count is not None and 1934 min_count == max_count and len(args) != min_count): 1935 ToStderr("Error: Command %s expects %d argument(s)", cmd, min_count) 1936 return False 1937 1938 # Command with limited number of arguments 1939 if max_count is not None and len(args) > max_count: 1940 ToStderr("Error: Command %s expects only %d argument(s)", 1941 cmd, max_count) 1942 return False 1943 1944 # Command with some required arguments 1945 if min_count is not None and len(args) < min_count: 1946 ToStderr("Error: Command %s expects at least %d argument(s)", 1947 cmd, min_count) 1948 return False 1949 1950 return True
1951
1952 1953 -def SplitNodeOption(value):
1954 """Splits the value of a --node option. 1955 1956 """ 1957 if value and ":" in value: 1958 return value.split(":", 1) 1959 else: 1960 return (value, None)
1961
1962 1963 -def CalculateOSNames(os_name, os_variants):
1964 """Calculates all the names an OS can be called, according to its variants. 1965 1966 @type os_name: string 1967 @param os_name: base name of the os 1968 @type os_variants: list or None 1969 @param os_variants: list of supported variants 1970 @rtype: list 1971 @return: list of valid names 1972 1973 """ 1974 if os_variants: 1975 return ["%s+%s" % (os_name, v) for v in os_variants] 1976 else: 1977 return [os_name]
1978
1979 1980 -def ParseFields(selected, default):
1981 """Parses the values of "--field"-like options. 1982 1983 @type selected: string or None 1984 @param selected: User-selected options 1985 @type default: list 1986 @param default: Default fields 1987 1988 """ 1989 if selected is None: 1990 return default 1991 1992 if selected.startswith("+"): 1993 return default + selected[1:].split(",") 1994 1995 return selected.split(",")
1996 1997 1998 UsesRPC = rpc.RunWithRPC
1999 2000 2001 -def AskUser(text, choices=None):
2002 """Ask the user a question. 2003 2004 @param text: the question to ask 2005 2006 @param choices: list with elements tuples (input_char, return_value, 2007 description); if not given, it will default to: [('y', True, 2008 'Perform the operation'), ('n', False, 'Do no do the operation')]; 2009 note that the '?' char is reserved for help 2010 2011 @return: one of the return values from the choices list; if input is 2012 not possible (i.e. not running with a tty, we return the last 2013 entry from the list 2014 2015 """ 2016 if choices is None: 2017 choices = [("y", True, "Perform the operation"), 2018 ("n", False, "Do not perform the operation")] 2019 if not choices or not isinstance(choices, list): 2020 raise errors.ProgrammerError("Invalid choices argument to AskUser") 2021 for entry in choices: 2022 if not isinstance(entry, tuple) or len(entry) < 3 or entry[0] == "?": 2023 raise errors.ProgrammerError("Invalid choices element to AskUser") 2024 2025 answer = choices[-1][1] 2026 new_text = [] 2027 for line in text.splitlines(): 2028 new_text.append(textwrap.fill(line, 70, replace_whitespace=False)) 2029 text = "\n".join(new_text) 2030 try: 2031 f = file("/dev/tty", "a+") 2032 except IOError: 2033 return answer 2034 try: 2035 chars = [entry[0] for entry in choices] 2036 chars[-1] = "[%s]" % chars[-1] 2037 chars.append("?") 2038 maps = dict([(entry[0], entry[1]) for entry in choices]) 2039 while True: 2040 f.write(text) 2041 f.write("\n") 2042 f.write("/".join(chars)) 2043 f.write(": ") 2044 line = f.readline(2).strip().lower() 2045 if line in maps: 2046 answer = maps[line] 2047 break 2048 elif line == "?": 2049 for entry in choices: 2050 f.write(" %s - %s\n" % (entry[0], entry[2])) 2051 f.write("\n") 2052 continue 2053 finally: 2054 f.close() 2055 return answer
2056
2057 2058 -class JobSubmittedException(Exception):
2059 """Job was submitted, client should exit. 2060 2061 This exception has one argument, the ID of the job that was 2062 submitted. The handler should print this ID. 2063 2064 This is not an error, just a structured way to exit from clients. 2065 2066 """
2067
2068 2069 -def SendJob(ops, cl=None):
2070 """Function to submit an opcode without waiting for the results. 2071 2072 @type ops: list 2073 @param ops: list of opcodes 2074 @type cl: luxi.Client 2075 @param cl: the luxi client to use for communicating with the master; 2076 if None, a new client will be created 2077 2078 """ 2079 if cl is None: 2080 cl = GetClient() 2081 2082 job_id = cl.SubmitJob(ops) 2083 2084 return job_id
2085
2086 2087 -def GenericPollJob(job_id, cbs, report_cbs):
2088 """Generic job-polling function. 2089 2090 @type job_id: number 2091 @param job_id: Job ID 2092 @type cbs: Instance of L{JobPollCbBase} 2093 @param cbs: Data callbacks 2094 @type report_cbs: Instance of L{JobPollReportCbBase} 2095 @param report_cbs: Reporting callbacks 2096 2097 """ 2098 prev_job_info = None 2099 prev_logmsg_serial = None 2100 2101 status = None 2102 2103 while True: 2104 result = cbs.WaitForJobChangeOnce(job_id, ["status"], prev_job_info, 2105 prev_logmsg_serial) 2106 if not result: 2107 # job not found, go away! 2108 raise errors.JobLost("Job with id %s lost" % job_id) 2109 2110 if result == constants.JOB_NOTCHANGED: 2111 report_cbs.ReportNotChanged(job_id, status) 2112 2113 # Wait again 2114 continue 2115 2116 # Split result, a tuple of (field values, log entries) 2117 (job_info, log_entries) = result 2118 (status, ) = job_info 2119 2120 if log_entries: 2121 for log_entry in log_entries: 2122 (serial, timestamp, log_type, message) = log_entry 2123 report_cbs.ReportLogMessage(job_id, serial, timestamp, 2124 log_type, message) 2125 prev_logmsg_serial = max(prev_logmsg_serial, serial) 2126 2127 # TODO: Handle canceled and archived jobs 2128 elif status in (constants.JOB_STATUS_SUCCESS, 2129 constants.JOB_STATUS_ERROR, 2130 constants.JOB_STATUS_CANCELING, 2131 constants.JOB_STATUS_CANCELED): 2132 break 2133 2134 prev_job_info = job_info 2135 2136 jobs = cbs.QueryJobs([job_id], ["status", "opstatus", "opresult"]) 2137 if not jobs: 2138 raise errors.JobLost("Job with id %s lost" % job_id) 2139 2140 status, opstatus, result = jobs[0] 2141 2142 if status == constants.JOB_STATUS_SUCCESS: 2143 return result 2144 2145 if status in (constants.JOB_STATUS_CANCELING, constants.JOB_STATUS_CANCELED): 2146 raise errors.OpExecError("Job was canceled") 2147 2148 has_ok = False 2149 for idx, (status, msg) in enumerate(zip(opstatus, result)): 2150 if status == constants.OP_STATUS_SUCCESS: 2151 has_ok = True 2152 elif status == constants.OP_STATUS_ERROR: 2153 errors.MaybeRaise(msg) 2154 2155 if has_ok: 2156 raise errors.OpExecError("partial failure (opcode %d): %s" % 2157 (idx, msg)) 2158 2159 raise errors.OpExecError(str(msg)) 2160 2161 # default failure mode 2162 raise errors.OpExecError(result)
2163
2164 2165 -class JobPollCbBase(object):
2166 """Base class for L{GenericPollJob} callbacks. 2167 2168 """
2169 - def __init__(self):
2170 """Initializes this class. 2171 2172 """
2173
2174 - def WaitForJobChangeOnce(self, job_id, fields, 2175 prev_job_info, prev_log_serial):
2176 """Waits for changes on a job. 2177 2178 """ 2179 raise NotImplementedError()
2180
2181 - def QueryJobs(self, job_ids, fields):
2182 """Returns the selected fields for the selected job IDs. 2183 2184 @type job_ids: list of numbers 2185 @param job_ids: Job IDs 2186 @type fields: list of strings 2187 @param fields: Fields 2188 2189 """ 2190 raise NotImplementedError()
2191
2192 2193 -class JobPollReportCbBase(object):
2194 """Base class for L{GenericPollJob} reporting callbacks. 2195 2196 """
2197 - def __init__(self):
2198 """Initializes this class. 2199 2200 """
2201
2202 - def ReportLogMessage(self, job_id, serial, timestamp, log_type, log_msg):
2203 """Handles a log message. 2204 2205 """ 2206 raise NotImplementedError()
2207
2208 - def ReportNotChanged(self, job_id, status):
2209 """Called for if a job hasn't changed in a while. 2210 2211 @type job_id: number 2212 @param job_id: Job ID 2213 @type status: string or None 2214 @param status: Job status if available 2215 2216 """ 2217 raise NotImplementedError()
2218
2219 2220 -class _LuxiJobPollCb(JobPollCbBase):
2221 - def __init__(self, cl):
2222 """Initializes this class. 2223 2224 """ 2225 JobPollCbBase.__init__(self) 2226 self.cl = cl
2227
2228 - def WaitForJobChangeOnce(self, job_id, fields, 2229 prev_job_info, prev_log_serial):
2230 """Waits for changes on a job. 2231 2232 """ 2233 return self.cl.WaitForJobChangeOnce(job_id, fields, 2234 prev_job_info, prev_log_serial)
2235
2236 - def QueryJobs(self, job_ids, fields):
2237 """Returns the selected fields for the selected job IDs. 2238 2239 """ 2240 return self.cl.QueryJobs(job_ids, fields)
2241
2242 2243 -class FeedbackFnJobPollReportCb(JobPollReportCbBase):
2244 - def __init__(self, feedback_fn):
2245 """Initializes this class. 2246 2247 """ 2248 JobPollReportCbBase.__init__(self) 2249 2250 self.feedback_fn = feedback_fn 2251 2252 assert callable(feedback_fn)
2253
2254 - def ReportLogMessage(self, job_id, serial, timestamp, log_type, log_msg):
2255 """Handles a log message. 2256 2257 """ 2258 self.feedback_fn((timestamp, log_type, log_msg))
2259
2260 - def ReportNotChanged(self, job_id, status):
2261 """Called if a job hasn't changed in a while. 2262 2263 """
2264 # Ignore
2265 2266 2267 -class StdioJobPollReportCb(JobPollReportCbBase):
2268 - def __init__(self):
2269 """Initializes this class. 2270 2271 """ 2272 JobPollReportCbBase.__init__(self) 2273 2274 self.notified_queued = False 2275 self.notified_waitlock = False
2276
2277 - def ReportLogMessage(self, job_id, serial, timestamp, log_type, log_msg):
2278 """Handles a log message. 2279 2280 """ 2281 ToStdout("%s %s", time.ctime(utils.MergeTime(timestamp)), 2282 FormatLogMessage(log_type, log_msg))
2283
2284 - def ReportNotChanged(self, job_id, status):
2285 """Called if a job hasn't changed in a while. 2286 2287 """ 2288 if status is None: 2289 return 2290 2291 if status == constants.JOB_STATUS_QUEUED and not self.notified_queued: 2292 ToStderr("Job %s is waiting in queue", job_id) 2293 self.notified_queued = True 2294 2295 elif status == constants.JOB_STATUS_WAITING and not self.notified_waitlock: 2296 ToStderr("Job %s is trying to acquire all necessary locks", job_id) 2297 self.notified_waitlock = True
2298
2299 2300 -def FormatLogMessage(log_type, log_msg):
2301 """Formats a job message according to its type. 2302 2303 """ 2304 if log_type != constants.ELOG_MESSAGE: 2305 log_msg = str(log_msg) 2306 2307 return utils.SafeEncode(log_msg)
2308
2309 2310 -def PollJob(job_id, cl=None, feedback_fn=None, reporter=None):
2311 """Function to poll for the result of a job. 2312 2313 @type job_id: job identified 2314 @param job_id: the job to poll for results 2315 @type cl: luxi.Client 2316 @param cl: the luxi client to use for communicating with the master; 2317 if None, a new client will be created 2318 2319 """ 2320 if cl is None: 2321 cl = GetClient() 2322 2323 if reporter is None: 2324 if feedback_fn: 2325 reporter = FeedbackFnJobPollReportCb(feedback_fn) 2326 else: 2327 reporter = StdioJobPollReportCb() 2328 elif feedback_fn: 2329 raise errors.ProgrammerError("Can't specify reporter and feedback function") 2330 2331 return GenericPollJob(job_id, _LuxiJobPollCb(cl), reporter)
2332
2333 2334 -def SubmitOpCode(op, cl=None, feedback_fn=None, opts=None, reporter=None):
2335 """Legacy function to submit an opcode. 2336 2337 This is just a simple wrapper over the construction of the processor 2338 instance. It should be extended to better handle feedback and 2339 interaction functions. 2340 2341 """ 2342 if cl is None: 2343 cl = GetClient() 2344 2345 SetGenericOpcodeOpts([op], opts) 2346 2347 job_id = SendJob([op], cl=cl) 2348 if hasattr(opts, "print_jobid") and opts.print_jobid: 2349 ToStdout("%d" % job_id) 2350 2351 op_results = PollJob(job_id, cl=cl, feedback_fn=feedback_fn, 2352 reporter=reporter) 2353 2354 return op_results[0]
2355
2356 2357 -def SubmitOpCodeToDrainedQueue(op):
2358 """Forcefully insert a job in the queue, even if it is drained. 2359 2360 """ 2361 cl = GetClient() 2362 job_id = cl.SubmitJobToDrainedQueue([op]) 2363 op_results = PollJob(job_id, cl=cl) 2364 return op_results[0]
2365
2366 2367 -def SubmitOrSend(op, opts, cl=None, feedback_fn=None):
2368 """Wrapper around SubmitOpCode or SendJob. 2369 2370 This function will decide, based on the 'opts' parameter, whether to 2371 submit and wait for the result of the opcode (and return it), or 2372 whether to just send the job and print its identifier. It is used in 2373 order to simplify the implementation of the '--submit' option. 2374 2375 It will also process the opcodes if we're sending the via SendJob 2376 (otherwise SubmitOpCode does it). 2377 2378 """ 2379 if opts and opts.submit_only: 2380 job = [op] 2381 SetGenericOpcodeOpts(job, opts) 2382 job_id = SendJob(job, cl=cl) 2383 if opts.print_jobid: 2384 ToStdout("%d" % job_id) 2385 raise JobSubmittedException(job_id) 2386 else: 2387 return SubmitOpCode(op, cl=cl, feedback_fn=feedback_fn, opts=opts)
2388
2389 2390 -def _InitReasonTrail(op, opts):
2391 """Builds the first part of the reason trail 2392 2393 Builds the initial part of the reason trail, adding the user provided reason 2394 (if it exists) and the name of the command starting the operation. 2395 2396 @param op: the opcode the reason trail will be added to 2397 @param opts: the command line options selected by the user 2398 2399 """ 2400 assert len(sys.argv) >= 2 2401 trail = [] 2402 2403 if opts.reason: 2404 trail.append((constants.OPCODE_REASON_SRC_USER, 2405 opts.reason, 2406 utils.EpochNano())) 2407 2408 binary = os.path.basename(sys.argv[0]) 2409 source = "%s:%s" % (constants.OPCODE_REASON_SRC_CLIENT, binary) 2410 command = sys.argv[1] 2411 trail.append((source, command, utils.EpochNano())) 2412 op.reason = trail
2413
2414 2415 -def SetGenericOpcodeOpts(opcode_list, options):
2416 """Processor for generic options. 2417 2418 This function updates the given opcodes based on generic command 2419 line options (like debug, dry-run, etc.). 2420 2421 @param opcode_list: list of opcodes 2422 @param options: command line options or None 2423 @return: None (in-place modification) 2424 2425 """ 2426 if not options: 2427 return 2428 for op in opcode_list: 2429 op.debug_level = options.debug 2430 if hasattr(options, "dry_run"): 2431 op.dry_run = options.dry_run 2432 if getattr(options, "priority", None) is not None: 2433 op.priority = options.priority 2434 _InitReasonTrail(op, options)
2435
2436 2437 -def FormatError(err):
2438 """Return a formatted error message for a given error. 2439 2440 This function takes an exception instance and returns a tuple 2441 consisting of two values: first, the recommended exit code, and 2442 second, a string describing the error message (not 2443 newline-terminated). 2444 2445 """ 2446 retcode = 1 2447 obuf = StringIO() 2448 msg = str(err) 2449 if isinstance(err, errors.ConfigurationError): 2450 txt = "Corrupt configuration file: %s" % msg 2451 logging.error(txt) 2452 obuf.write(txt + "\n") 2453 obuf.write("Aborting.") 2454 retcode = 2 2455 elif isinstance(err, errors.HooksAbort): 2456 obuf.write("Failure: hooks execution failed:\n") 2457 for node, script, out in err.args[0]: 2458 if out: 2459 obuf.write(" node: %s, script: %s, output: %s\n" % 2460 (node, script, out)) 2461 else: 2462 obuf.write(" node: %s, script: %s (no output)\n" % 2463 (node, script)) 2464 elif isinstance(err, errors.HooksFailure): 2465 obuf.write("Failure: hooks general failure: %s" % msg) 2466 elif isinstance(err, errors.ResolverError): 2467 this_host = netutils.Hostname.GetSysName() 2468 if err.args[0] == this_host: 2469 msg = "Failure: can't resolve my own hostname ('%s')" 2470 else: 2471 msg = "Failure: can't resolve hostname '%s'" 2472 obuf.write(msg % err.args[0]) 2473 elif isinstance(err, errors.OpPrereqError): 2474 if len(err.args) == 2: 2475 obuf.write("Failure: prerequisites not met for this" 2476 " operation:\nerror type: %s, error details:\n%s" % 2477 (err.args[1], err.args[0])) 2478 else: 2479 obuf.write("Failure: prerequisites not met for this" 2480 " operation:\n%s" % msg) 2481 elif isinstance(err, errors.OpExecError): 2482 obuf.write("Failure: command execution error:\n%s" % msg) 2483 elif isinstance(err, errors.TagError): 2484 obuf.write("Failure: invalid tag(s) given:\n%s" % msg) 2485 elif isinstance(err, errors.JobQueueDrainError): 2486 obuf.write("Failure: the job queue is marked for drain and doesn't" 2487 " accept new requests\n") 2488 elif isinstance(err, errors.JobQueueFull): 2489 obuf.write("Failure: the job queue is full and doesn't accept new" 2490 " job submissions until old jobs are archived\n") 2491 elif isinstance(err, errors.TypeEnforcementError): 2492 obuf.write("Parameter Error: %s" % msg) 2493 elif isinstance(err, errors.ParameterError): 2494 obuf.write("Failure: unknown/wrong parameter name '%s'" % msg) 2495 elif isinstance(err, rpcerr.NoMasterError): 2496 if err.args[0] == pathutils.MASTER_SOCKET: 2497 daemon = "the master daemon" 2498 elif err.args[0] == pathutils.QUERY_SOCKET: 2499 daemon = "the config daemon" 2500 else: 2501 daemon = "socket '%s'" % str(err.args[0]) 2502 obuf.write("Cannot communicate with %s.\nIs the process running" 2503 " and listening for connections?" % daemon) 2504 elif isinstance(err, rpcerr.TimeoutError): 2505 obuf.write("Timeout while talking to the master daemon. Jobs might have" 2506 " been submitted and will continue to run even if the call" 2507 " timed out. Useful commands in this situation are \"gnt-job" 2508 " list\", \"gnt-job cancel\" and \"gnt-job watch\". Error:\n") 2509 obuf.write(msg) 2510 elif isinstance(err, rpcerr.PermissionError): 2511 obuf.write("It seems you don't have permissions to connect to the" 2512 " master daemon.\nPlease retry as a different user.") 2513 elif isinstance(err, rpcerr.ProtocolError): 2514 obuf.write("Unhandled protocol error while talking to the master daemon:\n" 2515 "%s" % msg) 2516 elif isinstance(err, errors.JobLost): 2517 obuf.write("Error checking job status: %s" % msg) 2518 elif isinstance(err, errors.QueryFilterParseError): 2519 obuf.write("Error while parsing query filter: %s\n" % err.args[0]) 2520 obuf.write("\n".join(err.GetDetails())) 2521 elif isinstance(err, errors.GenericError): 2522 obuf.write("Unhandled Ganeti error: %s" % msg) 2523 elif isinstance(err, JobSubmittedException): 2524 obuf.write("JobID: %s\n" % err.args[0]) 2525 retcode = 0 2526 else: 2527 obuf.write("Unhandled exception: %s" % msg) 2528 return retcode, obuf.getvalue().rstrip("\n")
2529
2530 2531 -def GenericMain(commands, override=None, aliases=None, 2532 env_override=frozenset()):
2533 """Generic main function for all the gnt-* commands. 2534 2535 @param commands: a dictionary with a special structure, see the design doc 2536 for command line handling. 2537 @param override: if not None, we expect a dictionary with keys that will 2538 override command line options; this can be used to pass 2539 options from the scripts to generic functions 2540 @param aliases: dictionary with command aliases {'alias': 'target, ...} 2541 @param env_override: list of environment names which are allowed to submit 2542 default args for commands 2543 2544 """ 2545 # save the program name and the entire command line for later logging 2546 if sys.argv: 2547 binary = os.path.basename(sys.argv[0]) 2548 if not binary: 2549 binary = sys.argv[0] 2550 2551 if len(sys.argv) >= 2: 2552 logname = utils.ShellQuoteArgs([binary, sys.argv[1]]) 2553 else: 2554 logname = binary 2555 2556 cmdline = utils.ShellQuoteArgs([binary] + sys.argv[1:]) 2557 else: 2558 binary = "<unknown program>" 2559 cmdline = "<unknown>" 2560 2561 if aliases is None: 2562 aliases = {} 2563 2564 try: 2565 (func, options, args) = _ParseArgs(binary, sys.argv, commands, aliases, 2566 env_override) 2567 except _ShowVersion: 2568 ToStdout("%s (ganeti %s) %s", binary, constants.VCS_VERSION, 2569 constants.RELEASE_VERSION) 2570 return constants.EXIT_SUCCESS 2571 except _ShowUsage, err: 2572 for line in _FormatUsage(binary, commands): 2573 ToStdout(line) 2574 2575 if err.exit_error: 2576 return constants.EXIT_FAILURE 2577 else: 2578 return constants.EXIT_SUCCESS 2579 except errors.ParameterError, err: 2580 result, err_msg = FormatError(err) 2581 ToStderr(err_msg) 2582 return 1 2583 2584 if func is None: # parse error 2585 return 1 2586 2587 if override is not None: 2588 for key, val in override.iteritems(): 2589 setattr(options, key, val) 2590 2591 utils.SetupLogging(pathutils.LOG_COMMANDS, logname, debug=options.debug, 2592 stderr_logging=True) 2593 2594 logging.info("Command line: %s", cmdline) 2595 2596 try: 2597 result = func(options, args) 2598 except (errors.GenericError, rpcerr.ProtocolError, 2599 JobSubmittedException), err: 2600 result, err_msg = FormatError(err) 2601 logging.exception("Error during command processing") 2602 ToStderr(err_msg) 2603 except KeyboardInterrupt: 2604 result = constants.EXIT_FAILURE 2605 ToStderr("Aborted. Note that if the operation created any jobs, they" 2606 " might have been submitted and" 2607 " will continue to run in the background.") 2608 except IOError, err: 2609 if err.errno == errno.EPIPE: 2610 # our terminal went away, we'll exit 2611 sys.exit(constants.EXIT_FAILURE) 2612 else: 2613 raise 2614 2615 return result
2616
2617 2618 -def ParseNicOption(optvalue):
2619 """Parses the value of the --net option(s). 2620 2621 """ 2622 try: 2623 nic_max = max(int(nidx[0]) + 1 for nidx in optvalue) 2624 except (TypeError, ValueError), err: 2625 raise errors.OpPrereqError("Invalid NIC index passed: %s" % str(err), 2626 errors.ECODE_INVAL) 2627 2628 nics = [{}] * nic_max 2629 for nidx, ndict in optvalue: 2630 nidx = int(nidx) 2631 2632 if not isinstance(ndict, dict): 2633 raise errors.OpPrereqError("Invalid nic/%d value: expected dict," 2634 " got %s" % (nidx, ndict), errors.ECODE_INVAL) 2635 2636 utils.ForceDictType(ndict, constants.INIC_PARAMS_TYPES) 2637 2638 nics[nidx] = ndict 2639 2640 return nics
2641
2642 2643 -def FixHvParams(hvparams):
2644 # In Ganeti 2.8.4 the separator for the usb_devices hvparam was changed from 2645 # comma to space because commas cannot be accepted on the command line 2646 # (they already act as the separator between different hvparams). Still, 2647 # RAPI should be able to accept commas for backwards compatibility. 2648 # Therefore, we convert spaces into commas here, and we keep the old 2649 # parsing logic everywhere else. 2650 try: 2651 new_usb_devices = hvparams[constants.HV_USB_DEVICES].replace(" ", ",") 2652 hvparams[constants.HV_USB_DEVICES] = new_usb_devices 2653 except KeyError: 2654 #No usb_devices, no modification required 2655 pass
2656
2657 2658 -def GenericInstanceCreate(mode, opts, args):
2659 """Add an instance to the cluster via either creation or import. 2660 2661 @param mode: constants.INSTANCE_CREATE or constants.INSTANCE_IMPORT 2662 @param opts: the command line options selected by the user 2663 @type args: list 2664 @param args: should contain only one element, the new instance name 2665 @rtype: int 2666 @return: the desired exit code 2667 2668 """ 2669 instance = args[0] 2670 2671 (pnode, snode) = SplitNodeOption(opts.node) 2672 2673 hypervisor = None 2674 hvparams = {} 2675 if opts.hypervisor: 2676 hypervisor, hvparams = opts.hypervisor 2677 2678 if opts.nics: 2679 nics = ParseNicOption(opts.nics) 2680 elif opts.no_nics: 2681 # no nics 2682 nics = [] 2683 elif mode == constants.INSTANCE_CREATE: 2684 # default of one nic, all auto 2685 nics = [{}] 2686 else: 2687 # mode == import 2688 nics = [] 2689 2690 if opts.disk_template == constants.DT_DISKLESS: 2691 if opts.disks or opts.sd_size is not None: 2692 raise errors.OpPrereqError("Diskless instance but disk" 2693 " information passed", errors.ECODE_INVAL) 2694 disks = [] 2695 else: 2696 if (not opts.disks and not opts.sd_size 2697 and mode == constants.INSTANCE_CREATE): 2698 raise errors.OpPrereqError("No disk information specified", 2699 errors.ECODE_INVAL) 2700 if opts.disks and opts.sd_size is not None: 2701 raise errors.OpPrereqError("Please use either the '--disk' or" 2702 " '-s' option", errors.ECODE_INVAL) 2703 if opts.sd_size is not None: 2704 opts.disks = [(0, {constants.IDISK_SIZE: opts.sd_size})] 2705 2706 if opts.disks: 2707 try: 2708 disk_max = max(int(didx[0]) + 1 for didx in opts.disks) 2709 except ValueError, err: 2710 raise errors.OpPrereqError("Invalid disk index passed: %s" % str(err), 2711 errors.ECODE_INVAL) 2712 disks = [{}] * disk_max 2713 else: 2714 disks = [] 2715 for didx, ddict in opts.disks: 2716 didx = int(didx) 2717 if not isinstance(ddict, dict): 2718 msg = "Invalid disk/%d value: expected dict, got %s" % (didx, ddict) 2719 raise errors.OpPrereqError(msg, errors.ECODE_INVAL) 2720 elif constants.IDISK_SIZE in ddict: 2721 if constants.IDISK_ADOPT in ddict: 2722 raise errors.OpPrereqError("Only one of 'size' and 'adopt' allowed" 2723 " (disk %d)" % didx, errors.ECODE_INVAL) 2724 try: 2725 ddict[constants.IDISK_SIZE] = \ 2726 utils.ParseUnit(ddict[constants.IDISK_SIZE]) 2727 except ValueError, err: 2728 raise errors.OpPrereqError("Invalid disk size for disk %d: %s" % 2729 (didx, err), errors.ECODE_INVAL) 2730 elif constants.IDISK_ADOPT in ddict: 2731 if constants.IDISK_SPINDLES in ddict: 2732 raise errors.OpPrereqError("spindles is not a valid option when" 2733 " adopting a disk", errors.ECODE_INVAL) 2734 if mode == constants.INSTANCE_IMPORT: 2735 raise errors.OpPrereqError("Disk adoption not allowed for instance" 2736 " import", errors.ECODE_INVAL) 2737 ddict[constants.IDISK_SIZE] = 0 2738 else: 2739 raise errors.OpPrereqError("Missing size or adoption source for" 2740 " disk %d" % didx, errors.ECODE_INVAL) 2741 if constants.IDISK_SPINDLES in ddict: 2742 ddict[constants.IDISK_SPINDLES] = int(ddict[constants.IDISK_SPINDLES]) 2743 2744 disks[didx] = ddict 2745 2746 if opts.tags is not None: 2747 tags = opts.tags.split(",") 2748 else: 2749 tags = [] 2750 2751 utils.ForceDictType(opts.beparams, constants.BES_PARAMETER_COMPAT) 2752 utils.ForceDictType(hvparams, constants.HVS_PARAMETER_TYPES) 2753 FixHvParams(hvparams) 2754 2755 if mode == constants.INSTANCE_CREATE: 2756 start = opts.start 2757 os_type = opts.os 2758 force_variant = opts.force_variant 2759 src_node = None 2760 src_path = None 2761 no_install = opts.no_install 2762 identify_defaults = False 2763 compress = constants.IEC_NONE 2764 elif mode == constants.INSTANCE_IMPORT: 2765 start = False 2766 os_type = None 2767 force_variant = False 2768 src_node = opts.src_node 2769 src_path = opts.src_dir 2770 no_install = None 2771 identify_defaults = opts.identify_defaults 2772 compress = opts.compress 2773 else: 2774 raise errors.ProgrammerError("Invalid creation mode %s" % mode) 2775 2776 op = opcodes.OpInstanceCreate(instance_name=instance, 2777 disks=disks, 2778 disk_template=opts.disk_template, 2779 nics=nics, 2780 conflicts_check=opts.conflicts_check, 2781 pnode=pnode, snode=snode, 2782 ip_check=opts.ip_check, 2783 name_check=opts.name_check, 2784 wait_for_sync=opts.wait_for_sync, 2785 file_storage_dir=opts.file_storage_dir, 2786 file_driver=opts.file_driver, 2787 iallocator=opts.iallocator, 2788 hypervisor=hypervisor, 2789 hvparams=hvparams, 2790 beparams=opts.beparams, 2791 osparams=opts.osparams, 2792 mode=mode, 2793 start=start, 2794 os_type=os_type, 2795 force_variant=force_variant, 2796 src_node=src_node, 2797 src_path=src_path, 2798 compress=compress, 2799 tags=tags, 2800 no_install=no_install, 2801 identify_defaults=identify_defaults, 2802 ignore_ipolicy=opts.ignore_ipolicy) 2803 2804 SubmitOrSend(op, opts) 2805 return 0
2806
2807 2808 -class _RunWhileClusterStoppedHelper(object):
2809 """Helper class for L{RunWhileClusterStopped} to simplify state management 2810 2811 """
2812 - def __init__(self, feedback_fn, cluster_name, master_node, 2813 online_nodes, ssh_ports):
2814 """Initializes this class. 2815 2816 @type feedback_fn: callable 2817 @param feedback_fn: Feedback function 2818 @type cluster_name: string 2819 @param cluster_name: Cluster name 2820 @type master_node: string 2821 @param master_node Master node name 2822 @type online_nodes: list 2823 @param online_nodes: List of names of online nodes 2824 @type ssh_ports: list 2825 @param ssh_ports: List of SSH ports of online nodes 2826 2827 """ 2828 self.feedback_fn = feedback_fn 2829 self.cluster_name = cluster_name 2830 self.master_node = master_node 2831 self.online_nodes = online_nodes 2832 self.ssh_ports = dict(zip(online_nodes, ssh_ports)) 2833 2834 self.ssh = ssh.SshRunner(self.cluster_name) 2835 2836 self.nonmaster_nodes = [name for name in online_nodes 2837 if name != master_node] 2838 2839 assert self.master_node not in self.nonmaster_nodes
2840
2841 - def _RunCmd(self, node_name, cmd):
2842 """Runs a command on the local or a remote machine. 2843 2844 @type node_name: string 2845 @param node_name: Machine name 2846 @type cmd: list 2847 @param cmd: Command 2848 2849 """ 2850 if node_name is None or node_name == self.master_node: 2851 # No need to use SSH 2852 result = utils.RunCmd(cmd) 2853 else: 2854 result = self.ssh.Run(node_name, constants.SSH_LOGIN_USER, 2855 utils.ShellQuoteArgs(cmd), 2856 port=self.ssh_ports[node_name]) 2857 2858 if result.failed: 2859 errmsg = ["Failed to run command %s" % result.cmd] 2860 if node_name: 2861 errmsg.append("on node %s" % node_name) 2862 errmsg.append(": exitcode %s and error %s" % 2863 (result.exit_code, result.output)) 2864 raise errors.OpExecError(" ".join(errmsg))
2865
2866 - def Call(self, fn, *args):
2867 """Call function while all daemons are stopped. 2868 2869 @type fn: callable 2870 @param fn: Function to be called 2871 2872 """ 2873 # Pause watcher by acquiring an exclusive lock on watcher state file 2874 self.feedback_fn("Blocking watcher") 2875 watcher_block = utils.FileLock.Open(pathutils.WATCHER_LOCK_FILE) 2876 try: 2877 # TODO: Currently, this just blocks. There's no timeout. 2878 # TODO: Should it be a shared lock? 2879 watcher_block.Exclusive(blocking=True) 2880 2881 # Stop master daemons, so that no new jobs can come in and all running 2882 # ones are finished 2883 self.feedback_fn("Stopping master daemons") 2884 self._RunCmd(None, [pathutils.DAEMON_UTIL, "stop-master"]) 2885 try: 2886 # Stop daemons on all nodes 2887 for node_name in self.online_nodes: 2888 self.feedback_fn("Stopping daemons on %s" % node_name) 2889 self._RunCmd(node_name, [pathutils.DAEMON_UTIL, "stop-all"]) 2890 2891 # All daemons are shut down now 2892 try: 2893 return fn(self, *args) 2894 except Exception, err: 2895 _, errmsg = FormatError(err) 2896 logging.exception("Caught exception") 2897 self.feedback_fn(errmsg) 2898 raise 2899 finally: 2900 # Start cluster again, master node last 2901 for node_name in self.nonmaster_nodes + [self.master_node]: 2902 self.feedback_fn("Starting daemons on %s" % node_name) 2903 self._RunCmd(node_name, [pathutils.DAEMON_UTIL, "start-all"]) 2904 finally: 2905 # Resume watcher 2906 watcher_block.Close()
2907
2908 2909 -def RunWhileClusterStopped(feedback_fn, fn, *args):
2910 """Calls a function while all cluster daemons are stopped. 2911 2912 @type feedback_fn: callable 2913 @param feedback_fn: Feedback function 2914 @type fn: callable 2915 @param fn: Function to be called when daemons are stopped 2916 2917 """ 2918 feedback_fn("Gathering cluster information") 2919 2920 # This ensures we're running on the master daemon 2921 cl = GetClient() 2922 # Query client 2923 qcl = GetClient(query=True) 2924 2925 (cluster_name, master_node) = \ 2926 cl.QueryConfigValues(["cluster_name", "master_node"]) 2927 2928 online_nodes = GetOnlineNodes([], cl=qcl) 2929 ssh_ports = GetNodesSshPorts(online_nodes, qcl) 2930 2931 # Don't keep a reference to the client. The master daemon will go away. 2932 del cl 2933 del qcl 2934 2935 assert master_node in online_nodes 2936 2937 return _RunWhileClusterStoppedHelper(feedback_fn, cluster_name, master_node, 2938 online_nodes, ssh_ports).Call(fn, *args)
2939
2940 2941 -def GenerateTable(headers, fields, separator, data, 2942 numfields=None, unitfields=None, 2943 units=None):
2944 """Prints a table with headers and different fields. 2945 2946 @type headers: dict 2947 @param headers: dictionary mapping field names to headers for 2948 the table 2949 @type fields: list 2950 @param fields: the field names corresponding to each row in 2951 the data field 2952 @param separator: the separator to be used; if this is None, 2953 the default 'smart' algorithm is used which computes optimal 2954 field width, otherwise just the separator is used between 2955 each field 2956 @type data: list 2957 @param data: a list of lists, each sublist being one row to be output 2958 @type numfields: list 2959 @param numfields: a list with the fields that hold numeric 2960 values and thus should be right-aligned 2961 @type unitfields: list 2962 @param unitfields: a list with the fields that hold numeric 2963 values that should be formatted with the units field 2964 @type units: string or None 2965 @param units: the units we should use for formatting, or None for 2966 automatic choice (human-readable for non-separator usage, otherwise 2967 megabytes); this is a one-letter string 2968 2969 """ 2970 if units is None: 2971 if separator: 2972 units = "m" 2973 else: 2974 units = "h" 2975 2976 if numfields is None: 2977 numfields = [] 2978 if unitfields is None: 2979 unitfields = [] 2980 2981 numfields = utils.FieldSet(*numfields) # pylint: disable=W0142 2982 unitfields = utils.FieldSet(*unitfields) # pylint: disable=W0142 2983 2984 format_fields = [] 2985 for field in fields: 2986 if headers and field not in headers: 2987 # TODO: handle better unknown fields (either revert to old 2988 # style of raising exception, or deal more intelligently with 2989 # variable fields) 2990 headers[field] = field 2991 if separator is not None: 2992 format_fields.append("%s") 2993 elif numfields.Matches(field): 2994 format_fields.append("%*s") 2995 else: 2996 format_fields.append("%-*s") 2997 2998 if separator is None: 2999 mlens = [0 for name in fields] 3000 format_str = " ".join(format_fields) 3001 else: 3002 format_str = separator.replace("%", "%%").join(format_fields) 3003 3004 for row in data: 3005 if row is None: 3006 continue 3007 for idx, val in enumerate(row): 3008 if unitfields.Matches(fields[idx]): 3009 try: 3010 val = int(val) 3011 except (TypeError, ValueError): 3012 pass 3013 else: 3014 val = row[idx] = utils.FormatUnit(val, units) 3015 val = row[idx] = str(val) 3016 if separator is None: 3017 mlens[idx] = max(mlens[idx], len(val)) 3018 3019 result = [] 3020 if headers: 3021 args = [] 3022 for idx, name in enumerate(fields): 3023 hdr = headers[name] 3024 if separator is None: 3025 mlens[idx] = max(mlens[idx], len(hdr)) 3026 args.append(mlens[idx]) 3027 args.append(hdr) 3028 result.append(format_str % tuple(args)) 3029 3030 if separator is None: 3031 assert len(mlens) == len(fields) 3032 3033 if fields and not numfields.Matches(fields[-1]): 3034 mlens[-1] = 0 3035 3036 for line in data: 3037 args = [] 3038 if line is None: 3039 line = ["-" for _ in fields] 3040 for idx in range(len(fields)): 3041 if separator is None: 3042 args.append(mlens[idx]) 3043 args.append(line[idx]) 3044 result.append(format_str % tuple(args)) 3045 3046 return result
3047
3048 3049 -def _FormatBool(value):
3050 """Formats a boolean value as a string. 3051 3052 """ 3053 if value: 3054 return "Y" 3055 return "N"
3056 3057 3058 #: Default formatting for query results; (callback, align right) 3059 _DEFAULT_FORMAT_QUERY = { 3060 constants.QFT_TEXT: (str, False), 3061 constants.QFT_BOOL: (_FormatBool, False), 3062 constants.QFT_NUMBER: (str, True), 3063 constants.QFT_TIMESTAMP: (utils.FormatTime, False), 3064 constants.QFT_OTHER: (str, False), 3065 constants.QFT_UNKNOWN: (str, False), 3066 }
3067 3068 3069 -def _GetColumnFormatter(fdef, override, unit):
3070 """Returns formatting function for a field. 3071 3072 @type fdef: L{objects.QueryFieldDefinition} 3073 @type override: dict 3074 @param override: Dictionary for overriding field formatting functions, 3075 indexed by field name, contents like L{_DEFAULT_FORMAT_QUERY} 3076 @type unit: string 3077 @param unit: Unit used for formatting fields of type L{constants.QFT_UNIT} 3078 @rtype: tuple; (callable, bool) 3079 @return: Returns the function to format a value (takes one parameter) and a 3080 boolean for aligning the value on the right-hand side 3081 3082 """ 3083 fmt = override.get(fdef.name, None) 3084 if fmt is not None: 3085 return fmt 3086 3087 assert constants.QFT_UNIT not in _DEFAULT_FORMAT_QUERY 3088 3089 if fdef.kind == constants.QFT_UNIT: 3090 # Can't keep this information in the static dictionary 3091 return (lambda value: utils.FormatUnit(value, unit), True) 3092 3093 fmt = _DEFAULT_FORMAT_QUERY.get(fdef.kind, None) 3094 if fmt is not None: 3095 return fmt 3096 3097 raise NotImplementedError("Can't format column type '%s'" % fdef.kind)
3098
3099 3100 -class _QueryColumnFormatter(object):
3101 """Callable class for formatting fields of a query. 3102 3103 """
3104 - def __init__(self, fn, status_fn, verbose):
3105 """Initializes this class. 3106 3107 @type fn: callable 3108 @param fn: Formatting function 3109 @type status_fn: callable 3110 @param status_fn: Function to report fields' status 3111 @type verbose: boolean 3112 @param verbose: whether to use verbose field descriptions or not 3113 3114 """ 3115 self._fn = fn 3116 self._status_fn = status_fn 3117 self._verbose = verbose
3118
3119 - def __call__(self, data):
3120 """Returns a field's string representation. 3121 3122 """ 3123 (status, value) = data 3124 3125 # Report status 3126 self._status_fn(status) 3127 3128 if status == constants.RS_NORMAL: 3129 return self._fn(value) 3130 3131 assert value is None, \ 3132 "Found value %r for abnormal status %s" % (value, status) 3133 3134 return FormatResultError(status, self._verbose)
3135
3136 3137 -def FormatResultError(status, verbose):
3138 """Formats result status other than L{constants.RS_NORMAL}. 3139 3140 @param status: The result status 3141 @type verbose: boolean 3142 @param verbose: Whether to return the verbose text 3143 @return: Text of result status 3144 3145 """ 3146 assert status != constants.RS_NORMAL, \ 3147 "FormatResultError called with status equal to constants.RS_NORMAL" 3148 try: 3149 (verbose_text, normal_text) = constants.RSS_DESCRIPTION[status] 3150 except KeyError: 3151 raise NotImplementedError("Unknown status %s" % status) 3152 else: 3153 if verbose: 3154 return verbose_text 3155 return normal_text
3156
3157 3158 -def FormatQueryResult(result, unit=None, format_override=None, separator=None, 3159 header=False, verbose=False):
3160 """Formats data in L{objects.QueryResponse}. 3161 3162 @type result: L{objects.QueryResponse} 3163 @param result: result of query operation 3164 @type unit: string 3165 @param unit: Unit used for formatting fields of type L{constants.QFT_UNIT}, 3166 see L{utils.text.FormatUnit} 3167 @type format_override: dict 3168 @param format_override: Dictionary for overriding field formatting functions, 3169 indexed by field name, contents like L{_DEFAULT_FORMAT_QUERY} 3170 @type separator: string or None 3171 @param separator: String used to separate fields 3172 @type header: bool 3173 @param header: Whether to output header row 3174 @type verbose: boolean 3175 @param verbose: whether to use verbose field descriptions or not 3176 3177 """ 3178 if unit is None: 3179 if separator: 3180 unit = "m" 3181 else: 3182 unit = "h" 3183 3184 if format_override is None: 3185 format_override = {} 3186 3187 stats = dict.fromkeys(constants.RS_ALL, 0) 3188 3189 def _RecordStatus(status): 3190 if status in stats: 3191 stats[status] += 1
3192 3193 columns = [] 3194 for fdef in result.fields: 3195 assert fdef.title and fdef.name 3196 (fn, align_right) = _GetColumnFormatter(fdef, format_override, unit) 3197 columns.append(TableColumn(fdef.title, 3198 _QueryColumnFormatter(fn, _RecordStatus, 3199 verbose), 3200 align_right)) 3201 3202 table = FormatTable(result.data, columns, header, separator) 3203 3204 # Collect statistics 3205 assert len(stats) == len(constants.RS_ALL) 3206 assert compat.all(count >= 0 for count in stats.values()) 3207 3208 # Determine overall status. If there was no data, unknown fields must be 3209 # detected via the field definitions. 3210 if (stats[constants.RS_UNKNOWN] or 3211 (not result.data and _GetUnknownFields(result.fields))): 3212 status = QR_UNKNOWN 3213 elif compat.any(count > 0 for key, count in stats.items() 3214 if key != constants.RS_NORMAL): 3215 status = QR_INCOMPLETE 3216 else: 3217 status = QR_NORMAL 3218 3219 return (status, table) 3220
3221 3222 -def _GetUnknownFields(fdefs):
3223 """Returns list of unknown fields included in C{fdefs}. 3224 3225 @type fdefs: list of L{objects.QueryFieldDefinition} 3226 3227 """ 3228 return [fdef for fdef in fdefs 3229 if fdef.kind == constants.QFT_UNKNOWN]
3230
3231 3232 -def _WarnUnknownFields(fdefs):
3233 """Prints a warning to stderr if a query included unknown fields. 3234 3235 @type fdefs: list of L{objects.QueryFieldDefinition} 3236 3237 """ 3238 unknown = _GetUnknownFields(fdefs) 3239 if unknown: 3240 ToStderr("Warning: Queried for unknown fields %s", 3241 utils.CommaJoin(fdef.name for fdef in unknown)) 3242 return True 3243 3244 return False
3245
3246 3247 -def GenericList(resource, fields, names, unit, separator, header, cl=None, 3248 format_override=None, verbose=False, force_filter=False, 3249 namefield=None, qfilter=None, isnumeric=False):
3250 """Generic implementation for listing all items of a resource. 3251 3252 @param resource: One of L{constants.QR_VIA_LUXI} 3253 @type fields: list of strings 3254 @param fields: List of fields to query for 3255 @type names: list of strings 3256 @param names: Names of items to query for 3257 @type unit: string or None 3258 @param unit: Unit used for formatting fields of type L{constants.QFT_UNIT} or 3259 None for automatic choice (human-readable for non-separator usage, 3260 otherwise megabytes); this is a one-letter string 3261 @type separator: string or None 3262 @param separator: String used to separate fields 3263 @type header: bool 3264 @param header: Whether to show header row 3265 @type force_filter: bool 3266 @param force_filter: Whether to always treat names as filter 3267 @type format_override: dict 3268 @param format_override: Dictionary for overriding field formatting functions, 3269 indexed by field name, contents like L{_DEFAULT_FORMAT_QUERY} 3270 @type verbose: boolean 3271 @param verbose: whether to use verbose field descriptions or not 3272 @type namefield: string 3273 @param namefield: Name of field to use for simple filters (see 3274 L{qlang.MakeFilter} for details) 3275 @type qfilter: list or None 3276 @param qfilter: Query filter (in addition to names) 3277 @param isnumeric: bool 3278 @param isnumeric: Whether the namefield's type is numeric, and therefore 3279 any simple filters built by namefield should use integer values to 3280 reflect that 3281 3282 """ 3283 if not names: 3284 names = None 3285 3286 namefilter = qlang.MakeFilter(names, force_filter, namefield=namefield, 3287 isnumeric=isnumeric) 3288 3289 if qfilter is None: 3290 qfilter = namefilter 3291 elif namefilter is not None: 3292 qfilter = [qlang.OP_AND, namefilter, qfilter] 3293 3294 if cl is None: 3295 cl = GetClient() 3296 3297 response = cl.Query(resource, fields, qfilter) 3298 3299 found_unknown = _WarnUnknownFields(response.fields) 3300 3301 (status, data) = FormatQueryResult(response, unit=unit, separator=separator, 3302 header=header, 3303 format_override=format_override, 3304 verbose=verbose) 3305 3306 for line in data: 3307 ToStdout(line) 3308 3309 assert ((found_unknown and status == QR_UNKNOWN) or 3310 (not found_unknown and status != QR_UNKNOWN)) 3311 3312 if status == QR_UNKNOWN: 3313 return constants.EXIT_UNKNOWN_FIELD 3314 3315 # TODO: Should the list command fail if not all data could be collected? 3316 return constants.EXIT_SUCCESS
3317
3318 3319 -def _FieldDescValues(fdef):
3320 """Helper function for L{GenericListFields} to get query field description. 3321 3322 @type fdef: L{objects.QueryFieldDefinition} 3323 @rtype: list 3324 3325 """ 3326 return [ 3327 fdef.name, 3328 _QFT_NAMES.get(fdef.kind, fdef.kind), 3329 fdef.title, 3330 fdef.doc, 3331 ]
3332
3333 3334 -def GenericListFields(resource, fields, separator, header, cl=None):
3335 """Generic implementation for listing fields for a resource. 3336 3337 @param resource: One of L{constants.QR_VIA_LUXI} 3338 @type fields: list of strings 3339 @param fields: List of fields to query for 3340 @type separator: string or None 3341 @param separator: String used to separate fields 3342 @type header: bool 3343 @param header: Whether to show header row 3344 3345 """ 3346 if cl is None: 3347 cl = GetClient() 3348 3349 if not fields: 3350 fields = None 3351 3352 response = cl.QueryFields(resource, fields) 3353 3354 found_unknown = _WarnUnknownFields(response.fields) 3355 3356 columns = [ 3357 TableColumn("Name", str, False), 3358 TableColumn("Type", str, False), 3359 TableColumn("Title", str, False), 3360 TableColumn("Description", str, False), 3361 ] 3362 3363 rows = map(_FieldDescValues, response.fields) 3364 3365 for line in FormatTable(rows, columns, header, separator): 3366 ToStdout(line) 3367 3368 if found_unknown: 3369 return constants.EXIT_UNKNOWN_FIELD 3370 3371 return constants.EXIT_SUCCESS
3372
3373 3374 -class TableColumn(object):
3375 """Describes a column for L{FormatTable}. 3376 3377 """
3378 - def __init__(self, title, fn, align_right):
3379 """Initializes this class. 3380 3381 @type title: string 3382 @param title: Column title 3383 @type fn: callable 3384 @param fn: Formatting function 3385 @type align_right: bool 3386 @param align_right: Whether to align values on the right-hand side 3387 3388 """ 3389 self.title = title 3390 self.format = fn 3391 self.align_right = align_right
3392
3393 3394 -def _GetColFormatString(width, align_right):
3395 """Returns the format string for a field. 3396 3397 """ 3398 if align_right: 3399 sign = "" 3400 else: 3401 sign = "-" 3402 3403 return "%%%s%ss" % (sign, width)
3404
3405 3406 -def FormatTable(rows, columns, header, separator):
3407 """Formats data as a table. 3408 3409 @type rows: list of lists 3410 @param rows: Row data, one list per row 3411 @type columns: list of L{TableColumn} 3412 @param columns: Column descriptions 3413 @type header: bool 3414 @param header: Whether to show header row 3415 @type separator: string or None 3416 @param separator: String used to separate columns 3417 3418 """ 3419 if header: 3420 data = [[col.title for col in columns]] 3421 colwidth = [len(col.title) for col in columns] 3422 else: 3423 data = [] 3424 colwidth = [0 for _ in columns] 3425 3426 # Format row data 3427 for row in rows: 3428 assert len(row) == len(columns) 3429 3430 formatted = [col.format(value) for value, col in zip(row, columns)] 3431 3432 if separator is None: 3433 # Update column widths 3434 for idx, (oldwidth, value) in enumerate(zip(colwidth, formatted)): 3435 # Modifying a list's items while iterating is fine 3436 colwidth[idx] = max(oldwidth, len(value)) 3437 3438 data.append(formatted) 3439 3440 if separator is not None: 3441 # Return early if a separator is used 3442 return [separator.join(row) for row in data] 3443 3444 if columns and not columns[-1].align_right: 3445 # Avoid unnecessary spaces at end of line 3446 colwidth[-1] = 0 3447 3448 # Build format string 3449 fmt = " ".join([_GetColFormatString(width, col.align_right) 3450 for col, width in zip(columns, colwidth)]) 3451 3452 return [fmt % tuple(row) for row in data]
3453
3454 3455 -def FormatTimestamp(ts):
3456 """Formats a given timestamp. 3457 3458 @type ts: timestamp 3459 @param ts: a timeval-type timestamp, a tuple of seconds and microseconds 3460 3461 @rtype: string 3462 @return: a string with the formatted timestamp 3463 3464 """ 3465 if not isinstance(ts, (tuple, list)) or len(ts) != 2: 3466 return "?" 3467 3468 (sec, usecs) = ts 3469 return utils.FormatTime(sec, usecs=usecs)
3470
3471 3472 -def ParseTimespec(value):
3473 """Parse a time specification. 3474 3475 The following suffixed will be recognized: 3476 3477 - s: seconds 3478 - m: minutes 3479 - h: hours 3480 - d: day 3481 - w: weeks 3482 3483 Without any suffix, the value will be taken to be in seconds. 3484 3485 """ 3486 value = str(value) 3487 if not value: 3488 raise errors.OpPrereqError("Empty time specification passed", 3489 errors.ECODE_INVAL) 3490 suffix_map = { 3491 "s": 1, 3492 "m": 60, 3493 "h": 3600, 3494 "d": 86400, 3495 "w": 604800, 3496 } 3497 if value[-1] not in suffix_map: 3498 try: 3499 value = int(value) 3500 except (TypeError, ValueError): 3501 raise errors.OpPrereqError("Invalid time specification '%s'" % value, 3502 errors.ECODE_INVAL) 3503 else: 3504 multiplier = suffix_map[value[-1]] 3505 value = value[:-1] 3506 if not value: # no data left after stripping the suffix 3507 raise errors.OpPrereqError("Invalid time specification (only" 3508 " suffix passed)", errors.ECODE_INVAL) 3509 try: 3510 value = int(value) * multiplier 3511 except (TypeError, ValueError): 3512 raise errors.OpPrereqError("Invalid time specification '%s'" % value, 3513 errors.ECODE_INVAL) 3514 return value
3515
3516 3517 -def GetOnlineNodes(nodes, cl=None, nowarn=False, secondary_ips=False, 3518 filter_master=False, nodegroup=None):
3519 """Returns the names of online nodes. 3520 3521 This function will also log a warning on stderr with the names of 3522 the online nodes. 3523 3524 @param nodes: if not empty, use only this subset of nodes (minus the 3525 offline ones) 3526 @param cl: if not None, luxi client to use 3527 @type nowarn: boolean 3528 @param nowarn: by default, this function will output a note with the 3529 offline nodes that are skipped; if this parameter is True the 3530 note is not displayed 3531 @type secondary_ips: boolean 3532 @param secondary_ips: if True, return the secondary IPs instead of the 3533 names, useful for doing network traffic over the replication interface 3534 (if any) 3535 @type filter_master: boolean 3536 @param filter_master: if True, do not return the master node in the list 3537 (useful in coordination with secondary_ips where we cannot check our 3538 node name against the list) 3539 @type nodegroup: string 3540 @param nodegroup: If set, only return nodes in this node group 3541 3542 """ 3543 if cl is None: 3544 cl = GetClient(query=True) 3545 3546 qfilter = [] 3547 3548 if nodes: 3549 qfilter.append(qlang.MakeSimpleFilter("name", nodes)) 3550 3551 if nodegroup is not None: 3552 qfilter.append([qlang.OP_OR, [qlang.OP_EQUAL, "group", nodegroup], 3553 [qlang.OP_EQUAL, "group.uuid", nodegroup]]) 3554 3555 if filter_master: 3556 qfilter.append([qlang.OP_NOT, [qlang.OP_TRUE, "master"]]) 3557 3558 if qfilter: 3559 if len(qfilter) > 1: 3560 final_filter = [qlang.OP_AND] + qfilter 3561 else: 3562 assert len(qfilter) == 1 3563 final_filter = qfilter[0] 3564 else: 3565 final_filter = None 3566 3567 result = cl.Query(constants.QR_NODE, ["name", "offline", "sip"], final_filter) 3568 3569 def _IsOffline(row): 3570 (_, (_, offline), _) = row 3571 return offline
3572 3573 def _GetName(row): 3574 ((_, name), _, _) = row 3575 return name 3576 3577 def _GetSip(row): 3578 (_, _, (_, sip)) = row 3579 return sip 3580 3581 (offline, online) = compat.partition(result.data, _IsOffline) 3582 3583 if offline and not nowarn: 3584 ToStderr("Note: skipping offline node(s): %s" % 3585 utils.CommaJoin(map(_GetName, offline))) 3586 3587 if secondary_ips: 3588 fn = _GetSip 3589 else: 3590 fn = _GetName 3591 3592 return map(fn, online) 3593
3594 3595 -def GetNodesSshPorts(nodes, cl):
3596 """Retrieves SSH ports of given nodes. 3597 3598 @param nodes: the names of nodes 3599 @type nodes: a list of strings 3600 @param cl: a client to use for the query 3601 @type cl: L{Client} 3602 @return: the list of SSH ports corresponding to the nodes 3603 @rtype: a list of tuples 3604 """ 3605 return map(lambda t: t[0], 3606 cl.QueryNodes(names=nodes, 3607 fields=["ndp/ssh_port"], 3608 use_locking=False))
3609
3610 3611 -def _ToStream(stream, txt, *args):
3612 """Write a message to a stream, bypassing the logging system 3613 3614 @type stream: file object 3615 @param stream: the file to which we should write 3616 @type txt: str 3617 @param txt: the message 3618 3619 """ 3620 try: 3621 if args: 3622 args = tuple(args) 3623 stream.write(txt % args) 3624 else: 3625 stream.write(txt) 3626 stream.write("\n") 3627 stream.flush() 3628 except IOError, err: 3629 if err.errno == errno.EPIPE: 3630 # our terminal went away, we'll exit 3631 sys.exit(constants.EXIT_FAILURE) 3632 else: 3633 raise
3634
3635 3636 -def ToStdout(txt, *args):
3637 """Write a message to stdout only, bypassing the logging system 3638 3639 This is just a wrapper over _ToStream. 3640 3641 @type txt: str 3642 @param txt: the message 3643 3644 """ 3645 _ToStream(sys.stdout, txt, *args)
3646
3647 3648 -def ToStderr(txt, *args):
3649 """Write a message to stderr only, bypassing the logging system 3650 3651 This is just a wrapper over _ToStream. 3652 3653 @type txt: str 3654 @param txt: the message 3655 3656 """ 3657 _ToStream(sys.stderr, txt, *args)
3658
3659 3660 -class JobExecutor(object):
3661 """Class which manages the submission and execution of multiple jobs. 3662 3663 Note that instances of this class should not be reused between 3664 GetResults() calls. 3665 3666 """
3667 - def __init__(self, cl=None, verbose=True, opts=None, feedback_fn=None):
3668 self.queue = [] 3669 if cl is None: 3670 cl = GetClient() 3671 self.cl = cl 3672 self.verbose = verbose 3673 self.jobs = [] 3674 self.opts = opts 3675 self.feedback_fn = feedback_fn 3676 self._counter = itertools.count()
3677 3678 @staticmethod
3679 - def _IfName(name, fmt):
3680 """Helper function for formatting name. 3681 3682 """ 3683 if name: 3684 return fmt % name 3685 3686 return ""
3687
3688 - def QueueJob(self, name, *ops):
3689 """Record a job for later submit. 3690 3691 @type name: string 3692 @param name: a description of the job, will be used in WaitJobSet 3693 3694 """ 3695 SetGenericOpcodeOpts(ops, self.opts) 3696 self.queue.append((self._counter.next(), name, ops))
3697
3698 - def AddJobId(self, name, status, job_id):
3699 """Adds a job ID to the internal queue. 3700 3701 """ 3702 self.jobs.append((self._counter.next(), status, job_id, name))
3703
3704 - def SubmitPending(self, each=False):
3705 """Submit all pending jobs. 3706 3707 """ 3708 if each: 3709 results = [] 3710 for (_, _, ops) in self.queue: 3711 # SubmitJob will remove the success status, but raise an exception if 3712 # the submission fails, so we'll notice that anyway. 3713 results.append([True, self.cl.SubmitJob(ops)[0]]) 3714 else: 3715 results = self.cl.SubmitManyJobs([ops for (_, _, ops) in self.queue]) 3716 for ((status, data), (idx, name, _)) in zip(results, self.queue): 3717 self.jobs.append((idx, status, data, name))
3718
3719 - def _ChooseJob(self):
3720 """Choose a non-waiting/queued job to poll next. 3721 3722 """ 3723 assert self.jobs, "_ChooseJob called with empty job list" 3724 3725 result = self.cl.QueryJobs([i[2] for i in self.jobs[:_CHOOSE_BATCH]], 3726 ["status"]) 3727 assert result 3728 3729 for job_data, status in zip(self.jobs, result): 3730 if (isinstance(status, list) and status and 3731 status[0] in (constants.JOB_STATUS_QUEUED, 3732 constants.JOB_STATUS_WAITING, 3733 constants.JOB_STATUS_CANCELING)): 3734 # job is still present and waiting 3735 continue 3736 # good candidate found (either running job or lost job) 3737 self.jobs.remove(job_data) 3738 return job_data 3739 3740 # no job found 3741 return self.jobs.pop(0)
3742
3743 - def GetResults(self):
3744 """Wait for and return the results of all jobs. 3745 3746 @rtype: list 3747 @return: list of tuples (success, job results), in the same order 3748 as the submitted jobs; if a job has failed, instead of the result 3749 there will be the error message 3750 3751 """ 3752 if not self.jobs: 3753 self.SubmitPending() 3754 results = [] 3755 if self.verbose: 3756 ok_jobs = [row[2] for row in self.jobs if row[1]] 3757 if ok_jobs: 3758 ToStdout("Submitted jobs %s", utils.CommaJoin(ok_jobs)) 3759 3760 # first, remove any non-submitted jobs 3761 self.jobs, failures = compat.partition(self.jobs, lambda x: x[1]) 3762 for idx, _, jid, name in failures: 3763 ToStderr("Failed to submit job%s: %s", self._IfName(name, " for %s"), jid) 3764 results.append((idx, False, jid)) 3765 3766 while self.jobs: 3767 (idx, _, jid, name) = self._ChooseJob() 3768 ToStdout("Waiting for job %s%s ...", jid, self._IfName(name, " for %s")) 3769 try: 3770 job_result = PollJob(jid, cl=self.cl, feedback_fn=self.feedback_fn) 3771 success = True 3772 except errors.JobLost, err: 3773 _, job_result = FormatError(err) 3774 ToStderr("Job %s%s has been archived, cannot check its result", 3775 jid, self._IfName(name, " for %s")) 3776 success = False 3777 except (errors.GenericError, rpcerr.ProtocolError), err: 3778 _, job_result = FormatError(err) 3779 success = False 3780 # the error message will always be shown, verbose or not 3781 ToStderr("Job %s%s has failed: %s", 3782 jid, self._IfName(name, " for %s"), job_result) 3783 3784 results.append((idx, success, job_result)) 3785 3786 # sort based on the index, then drop it 3787 results.sort() 3788 results = [i[1:] for i in results] 3789 3790 return results
3791
3792 - def WaitOrShow(self, wait):
3793 """Wait for job results or only print the job IDs. 3794 3795 @type wait: boolean 3796 @param wait: whether to wait or not 3797 3798 """ 3799 if wait: 3800 return self.GetResults() 3801 else: 3802 if not self.jobs: 3803 self.SubmitPending() 3804 for _, status, result, name in self.jobs: 3805 if status: 3806 ToStdout("%s: %s", result, name) 3807 else: 3808 ToStderr("Failure for %s: %s", name, result) 3809 return [row[1:3] for row in self.jobs]
3810
3811 3812 -def FormatParamsDictInfo(param_dict, actual):
3813 """Formats a parameter dictionary. 3814 3815 @type param_dict: dict 3816 @param param_dict: the own parameters 3817 @type actual: dict 3818 @param actual: the current parameter set (including defaults) 3819 @rtype: dict 3820 @return: dictionary where the value of each parameter is either a fully 3821 formatted string or a dictionary containing formatted strings 3822 3823 """ 3824 ret = {} 3825 for (key, data) in actual.items(): 3826 if isinstance(data, dict) and data: 3827 ret[key] = FormatParamsDictInfo(param_dict.get(key, {}), data) 3828 else: 3829 ret[key] = str(param_dict.get(key, "default (%s)" % data)) 3830 return ret
3831
3832 3833 -def _FormatListInfoDefault(data, def_data):
3834 if data is not None: 3835 ret = utils.CommaJoin(data) 3836 else: 3837 ret = "default (%s)" % utils.CommaJoin(def_data) 3838 return ret
3839
3840 3841 -def FormatPolicyInfo(custom_ipolicy, eff_ipolicy, iscluster):
3842 """Formats an instance policy. 3843 3844 @type custom_ipolicy: dict 3845 @param custom_ipolicy: own policy 3846 @type eff_ipolicy: dict 3847 @param eff_ipolicy: effective policy (including defaults); ignored for 3848 cluster 3849 @type iscluster: bool 3850 @param iscluster: the policy is at cluster level 3851 @rtype: list of pairs 3852 @return: formatted data, suitable for L{PrintGenericInfo} 3853 3854 """ 3855 if iscluster: 3856 eff_ipolicy = custom_ipolicy 3857 3858 minmax_out = [] 3859 custom_minmax = custom_ipolicy.get(constants.ISPECS_MINMAX) 3860 if custom_minmax: 3861 for (k, minmax) in enumerate(custom_minmax): 3862 minmax_out.append([ 3863 ("%s/%s" % (key, k), 3864 FormatParamsDictInfo(minmax[key], minmax[key])) 3865 for key in constants.ISPECS_MINMAX_KEYS 3866 ]) 3867 else: 3868 for (k, minmax) in enumerate(eff_ipolicy[constants.ISPECS_MINMAX]): 3869 minmax_out.append([ 3870 ("%s/%s" % (key, k), 3871 FormatParamsDictInfo({}, minmax[key])) 3872 for key in constants.ISPECS_MINMAX_KEYS 3873 ]) 3874 ret = [("bounds specs", minmax_out)] 3875 3876 if iscluster: 3877 stdspecs = custom_ipolicy[constants.ISPECS_STD] 3878 ret.append( 3879 (constants.ISPECS_STD, 3880 FormatParamsDictInfo(stdspecs, stdspecs)) 3881 ) 3882 3883 ret.append( 3884 ("allowed disk templates", 3885 _FormatListInfoDefault(custom_ipolicy.get(constants.IPOLICY_DTS), 3886 eff_ipolicy[constants.IPOLICY_DTS])) 3887 ) 3888 ret.extend([ 3889 (key, str(custom_ipolicy.get(key, "default (%s)" % eff_ipolicy[key]))) 3890 for key in constants.IPOLICY_PARAMETERS 3891 ]) 3892 return ret
3893
3894 3895 -def _PrintSpecsParameters(buf, specs):
3896 values = ("%s=%s" % (par, val) for (par, val) in sorted(specs.items())) 3897 buf.write(",".join(values))
3898
3899 3900 -def PrintIPolicyCommand(buf, ipolicy, isgroup):
3901 """Print the command option used to generate the given instance policy. 3902 3903 Currently only the parts dealing with specs are supported. 3904 3905 @type buf: StringIO 3906 @param buf: stream to write into 3907 @type ipolicy: dict 3908 @param ipolicy: instance policy 3909 @type isgroup: bool 3910 @param isgroup: whether the policy is at group level 3911 3912 """ 3913 if not isgroup: 3914 stdspecs = ipolicy.get("std") 3915 if stdspecs: 3916 buf.write(" %s " % IPOLICY_STD_SPECS_STR) 3917 _PrintSpecsParameters(buf, stdspecs) 3918 minmaxes = ipolicy.get("minmax", []) 3919 first = True 3920 for minmax in minmaxes: 3921 minspecs = minmax.get("min") 3922 maxspecs = minmax.get("max") 3923 if minspecs and maxspecs: 3924 if first: 3925 buf.write(" %s " % IPOLICY_BOUNDS_SPECS_STR) 3926 first = False 3927 else: 3928 buf.write("//") 3929 buf.write("min:") 3930 _PrintSpecsParameters(buf, minspecs) 3931 buf.write("/max:") 3932 _PrintSpecsParameters(buf, maxspecs)
3933
3934 3935 -def ConfirmOperation(names, list_type, text, extra=""):
3936 """Ask the user to confirm an operation on a list of list_type. 3937 3938 This function is used to request confirmation for doing an operation 3939 on a given list of list_type. 3940 3941 @type names: list 3942 @param names: the list of names that we display when 3943 we ask for confirmation 3944 @type list_type: str 3945 @param list_type: Human readable name for elements in the list (e.g. nodes) 3946 @type text: str 3947 @param text: the operation that the user should confirm 3948 @rtype: boolean 3949 @return: True or False depending on user's confirmation. 3950 3951 """ 3952 count = len(names) 3953 msg = ("The %s will operate on %d %s.\n%s" 3954 "Do you want to continue?" % (text, count, list_type, extra)) 3955 affected = (("\nAffected %s:\n" % list_type) + 3956 "\n".join([" %s" % name for name in names])) 3957 3958 choices = [("y", True, "Yes, execute the %s" % text), 3959 ("n", False, "No, abort the %s" % text)] 3960 3961 if count > 20: 3962 choices.insert(1, ("v", "v", "View the list of affected %s" % list_type)) 3963 question = msg 3964 else: 3965 question = msg + affected 3966 3967 choice = AskUser(question, choices) 3968 if choice == "v": 3969 choices.pop(1) 3970 choice = AskUser(msg + affected, choices) 3971 return choice
3972
3973 3974 -def _MaybeParseUnit(elements):
3975 """Parses and returns an array of potential values with units. 3976 3977 """ 3978 parsed = {} 3979 for k, v in elements.items(): 3980 if v == constants.VALUE_DEFAULT: 3981 parsed[k] = v 3982 else: 3983 parsed[k] = utils.ParseUnit(v) 3984 return parsed
3985
3986 3987 -def _InitISpecsFromSplitOpts(ipolicy, ispecs_mem_size, ispecs_cpu_count, 3988 ispecs_disk_count, ispecs_disk_size, 3989 ispecs_nic_count, group_ipolicy, fill_all):
3990 try: 3991 if ispecs_mem_size: 3992 ispecs_mem_size = _MaybeParseUnit(ispecs_mem_size) 3993 if ispecs_disk_size: 3994 ispecs_disk_size = _MaybeParseUnit(ispecs_disk_size) 3995 except (TypeError, ValueError, errors.UnitParseError), err: 3996 raise errors.OpPrereqError("Invalid disk (%s) or memory (%s) size" 3997 " in policy: %s" % 3998 (ispecs_disk_size, ispecs_mem_size, err), 3999 errors.ECODE_INVAL) 4000 4001 # prepare ipolicy dict 4002 ispecs_transposed = { 4003 constants.ISPEC_MEM_SIZE: ispecs_mem_size, 4004 constants.ISPEC_CPU_COUNT: ispecs_cpu_count, 4005 constants.ISPEC_DISK_COUNT: ispecs_disk_count, 4006 constants.ISPEC_DISK_SIZE: ispecs_disk_size, 4007 constants.ISPEC_NIC_COUNT: ispecs_nic_count, 4008 } 4009 4010 # first, check that the values given are correct 4011 if group_ipolicy: 4012 forced_type = TISPECS_GROUP_TYPES 4013 else: 4014 forced_type = TISPECS_CLUSTER_TYPES 4015 for specs in ispecs_transposed.values(): 4016 assert type(specs) is dict 4017 utils.ForceDictType(specs, forced_type) 4018 4019 # then transpose 4020 ispecs = { 4021 constants.ISPECS_MIN: {}, 4022 constants.ISPECS_MAX: {}, 4023 constants.ISPECS_STD: {}, 4024 } 4025 for (name, specs) in ispecs_transposed.iteritems(): 4026 assert name in constants.ISPECS_PARAMETERS 4027 for key, val in specs.items(): # {min: .. ,max: .., std: ..} 4028 assert key in ispecs 4029 ispecs[key][name] = val 4030 minmax_out = {} 4031 for key in constants.ISPECS_MINMAX_KEYS: 4032 if fill_all: 4033 minmax_out[key] = \ 4034 objects.FillDict(constants.ISPECS_MINMAX_DEFAULTS[key], ispecs[key]) 4035 else: 4036 minmax_out[key] = ispecs[key] 4037 ipolicy[constants.ISPECS_MINMAX] = [minmax_out] 4038 if fill_all: 4039 ipolicy[constants.ISPECS_STD] = \ 4040 objects.FillDict(constants.IPOLICY_DEFAULTS[constants.ISPECS_STD], 4041 ispecs[constants.ISPECS_STD]) 4042 else: 4043 ipolicy[constants.ISPECS_STD] = ispecs[constants.ISPECS_STD]
4044
4045 4046 -def _ParseSpecUnit(spec, keyname):
4047 ret = spec.copy() 4048 for k in [constants.ISPEC_DISK_SIZE, constants.ISPEC_MEM_SIZE]: 4049 if k in ret: 4050 try: 4051 ret[k] = utils.ParseUnit(ret[k]) 4052 except (TypeError, ValueError, errors.UnitParseError), err: 4053 raise errors.OpPrereqError(("Invalid parameter %s (%s) in %s instance" 4054 " specs: %s" % (k, ret[k], keyname, err)), 4055 errors.ECODE_INVAL) 4056 return ret
4057
4058 4059 -def _ParseISpec(spec, keyname, required):
4060 ret = _ParseSpecUnit(spec, keyname) 4061 utils.ForceDictType(ret, constants.ISPECS_PARAMETER_TYPES) 4062 missing = constants.ISPECS_PARAMETERS - frozenset(ret.keys()) 4063 if required and missing: 4064 raise errors.OpPrereqError("Missing parameters in ipolicy spec %s: %s" % 4065 (keyname, utils.CommaJoin(missing)), 4066 errors.ECODE_INVAL) 4067 return ret
4068
4069 4070 -def _GetISpecsInAllowedValues(minmax_ispecs, allowed_values):
4071 ret = None 4072 if (minmax_ispecs and allowed_values and len(minmax_ispecs) == 1 and 4073 len(minmax_ispecs[0]) == 1): 4074 for (key, spec) in minmax_ispecs[0].items(): 4075 # This loop is executed exactly once 4076 if key in allowed_values and not spec: 4077 ret = key 4078 return ret
4079
4080 4081 -def _InitISpecsFromFullOpts(ipolicy_out, minmax_ispecs, std_ispecs, 4082 group_ipolicy, allowed_values):
4083 found_allowed = _GetISpecsInAllowedValues(minmax_ispecs, allowed_values) 4084 if found_allowed is not None: 4085 ipolicy_out[constants.ISPECS_MINMAX] = found_allowed 4086 elif minmax_ispecs is not None: 4087 minmax_out = [] 4088 for mmpair in minmax_ispecs: 4089 mmpair_out = {} 4090 for (key, spec) in mmpair.items(): 4091 if key not in constants.ISPECS_MINMAX_KEYS: 4092 msg = "Invalid key in bounds instance specifications: %s" % key 4093 raise errors.OpPrereqError(msg, errors.ECODE_INVAL) 4094 mmpair_out[key] = _ParseISpec(spec, key, True) 4095 minmax_out.append(mmpair_out) 4096 ipolicy_out[constants.ISPECS_MINMAX] = minmax_out 4097 if std_ispecs is not None: 4098 assert not group_ipolicy # This is not an option for gnt-group 4099 ipolicy_out[constants.ISPECS_STD] = _ParseISpec(std_ispecs, "std", False)
4100
4101 4102 -def CreateIPolicyFromOpts(ispecs_mem_size=None, 4103 ispecs_cpu_count=None, 4104 ispecs_disk_count=None, 4105 ispecs_disk_size=None, 4106 ispecs_nic_count=None, 4107 minmax_ispecs=None, 4108 std_ispecs=None, 4109 ipolicy_disk_templates=None, 4110 ipolicy_vcpu_ratio=None, 4111 ipolicy_spindle_ratio=None, 4112 group_ipolicy=False, 4113 allowed_values=None, 4114 fill_all=False):
4115 """Creation of instance policy based on command line options. 4116 4117 @param fill_all: whether for cluster policies we should ensure that 4118 all values are filled 4119 4120 """ 4121 assert not (fill_all and allowed_values) 4122 4123 split_specs = (ispecs_mem_size or ispecs_cpu_count or ispecs_disk_count or 4124 ispecs_disk_size or ispecs_nic_count) 4125 if (split_specs and (minmax_ispecs is not None or std_ispecs is not None)): 4126 raise errors.OpPrereqError("A --specs-xxx option cannot be specified" 4127 " together with any --ipolicy-xxx-specs option", 4128 errors.ECODE_INVAL) 4129 4130 ipolicy_out = objects.MakeEmptyIPolicy() 4131 if split_specs: 4132 assert fill_all 4133 _InitISpecsFromSplitOpts(ipolicy_out, ispecs_mem_size, ispecs_cpu_count, 4134 ispecs_disk_count, ispecs_disk_size, 4135 ispecs_nic_count, group_ipolicy, fill_all) 4136 elif (minmax_ispecs is not None or std_ispecs is not None): 4137 _InitISpecsFromFullOpts(ipolicy_out, minmax_ispecs, std_ispecs, 4138 group_ipolicy, allowed_values) 4139 4140 if ipolicy_disk_templates is not None: 4141 if allowed_values and ipolicy_disk_templates in allowed_values: 4142 ipolicy_out[constants.IPOLICY_DTS] = ipolicy_disk_templates 4143 else: 4144 ipolicy_out[constants.IPOLICY_DTS] = list(ipolicy_disk_templates) 4145 if ipolicy_vcpu_ratio is not None: 4146 ipolicy_out[constants.IPOLICY_VCPU_RATIO] = ipolicy_vcpu_ratio 4147 if ipolicy_spindle_ratio is not None: 4148 ipolicy_out[constants.IPOLICY_SPINDLE_RATIO] = ipolicy_spindle_ratio 4149 4150 assert not (frozenset(ipolicy_out.keys()) - constants.IPOLICY_ALL_KEYS) 4151 4152 if not group_ipolicy and fill_all: 4153 ipolicy_out = objects.FillIPolicy(constants.IPOLICY_DEFAULTS, ipolicy_out) 4154 4155 return ipolicy_out
4156
4157 4158 -def _SerializeGenericInfo(buf, data, level, afterkey=False):
4159 """Formatting core of L{PrintGenericInfo}. 4160 4161 @param buf: (string) stream to accumulate the result into 4162 @param data: data to format 4163 @type level: int 4164 @param level: depth in the data hierarchy, used for indenting 4165 @type afterkey: bool 4166 @param afterkey: True when we are in the middle of a line after a key (used 4167 to properly add newlines or indentation) 4168 4169 """ 4170 baseind = " " 4171 if isinstance(data, dict): 4172 if not data: 4173 buf.write("\n") 4174 else: 4175 if afterkey: 4176 buf.write("\n") 4177 doindent = True 4178 else: 4179 doindent = False 4180 for key in sorted(data): 4181 if doindent: 4182 buf.write(baseind * level) 4183 else: 4184 doindent = True 4185 buf.write(key) 4186 buf.write(": ") 4187 _SerializeGenericInfo(buf, data[key], level + 1, afterkey=True) 4188 elif isinstance(data, list) and len(data) > 0 and isinstance(data[0], tuple): 4189 # list of tuples (an ordered dictionary) 4190 if afterkey: 4191 buf.write("\n") 4192 doindent = True 4193 else: 4194 doindent = False 4195 for (key, val) in data: 4196 if doindent: 4197 buf.write(baseind * level) 4198 else: 4199 doindent = True 4200 buf.write(key) 4201 buf.write(": ") 4202 _SerializeGenericInfo(buf, val, level + 1, afterkey=True) 4203 elif isinstance(data, list): 4204 if not data: 4205 buf.write("\n") 4206 else: 4207 if afterkey: 4208 buf.write("\n") 4209 doindent = True 4210 else: 4211 doindent = False 4212 for item in data: 4213 if doindent: 4214 buf.write(baseind * level) 4215 else: 4216 doindent = True 4217 buf.write("-") 4218 buf.write(baseind[1:]) 4219 _SerializeGenericInfo(buf, item, level + 1) 4220 else: 4221 # This branch should be only taken for strings, but it's practically 4222 # impossible to guarantee that no other types are produced somewhere 4223 buf.write(str(data)) 4224 buf.write("\n")
4225
4226 4227 -def PrintGenericInfo(data):
4228 """Print information formatted according to the hierarchy. 4229 4230 The output is a valid YAML string. 4231 4232 @param data: the data to print. It's a hierarchical structure whose elements 4233 can be: 4234 - dictionaries, where keys are strings and values are of any of the 4235 types listed here 4236 - lists of pairs (key, value), where key is a string and value is of 4237 any of the types listed here; it's a way to encode ordered 4238 dictionaries 4239 - lists of any of the types listed here 4240 - strings 4241 4242 """ 4243 buf = StringIO() 4244 _SerializeGenericInfo(buf, data, 0) 4245 ToStdout(buf.getvalue().rstrip("\n"))
4246