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