Revision a5728081 lib/utils.py
b/lib/utils.py | ||
---|---|---|
387 | 387 |
logging.warning('%s missing keys %s', logname, ', '.join(missing)) |
388 | 388 |
|
389 | 389 |
|
390 |
def ForceDictType(target, key_types, allowed_values=None): |
|
391 |
"""Force the values of a dict to have certain types. |
|
392 |
|
|
393 |
@type target: dict |
|
394 |
@param target: the dict to update |
|
395 |
@type key_types: dict |
|
396 |
@param key_types: dict mapping target dict keys to types |
|
397 |
in constants.ENFORCEABLE_TYPES |
|
398 |
@type allowed_values: list |
|
399 |
@keyword allowed_values: list of specially allowed values |
|
400 |
|
|
401 |
""" |
|
402 |
if allowed_values is None: |
|
403 |
allowed_values = [] |
|
404 |
|
|
405 |
for key in target: |
|
406 |
if key not in key_types: |
|
407 |
msg = "Unknown key '%s'" % key |
|
408 |
raise errors.TypeEnforcementError(msg) |
|
409 |
|
|
410 |
if target[key] in allowed_values: |
|
411 |
continue |
|
412 |
|
|
413 |
type = key_types[key] |
|
414 |
if type not in constants.ENFORCEABLE_TYPES: |
|
415 |
msg = "'%s' has non-enforceable type %s" % (key, type) |
|
416 |
raise errors.ProgrammerError(msg) |
|
417 |
|
|
418 |
if type == constants.VTYPE_STRING: |
|
419 |
if not isinstance(target[key], basestring): |
|
420 |
if isinstance(target[key], bool) and not target[key]: |
|
421 |
target[key] = '' |
|
422 |
else: |
|
423 |
msg = "'%s' (value %s) is not a valid string" % (key, target[key]) |
|
424 |
raise errors.TypeEnforcementError(msg) |
|
425 |
elif type == constants.VTYPE_BOOL: |
|
426 |
if isinstance(target[key], basestring) and target[key]: |
|
427 |
if target[key].lower() == constants.VALUE_FALSE: |
|
428 |
target[key] = False |
|
429 |
elif target[key].lower() == constants.VALUE_TRUE: |
|
430 |
target[key] = True |
|
431 |
else: |
|
432 |
msg = "'%s' (value %s) is not a valid boolean" % (key, target[key]) |
|
433 |
raise errors.TypeEnforcementError(msg) |
|
434 |
elif target[key]: |
|
435 |
target[key] = True |
|
436 |
else: |
|
437 |
target[key] = False |
|
438 |
elif type == constants.VTYPE_SIZE: |
|
439 |
try: |
|
440 |
target[key] = ParseUnit(target[key]) |
|
441 |
except errors.UnitParseError, err: |
|
442 |
msg = "'%s' (value %s) is not a valid size. error: %s" % \ |
|
443 |
(key, target[key], err) |
|
444 |
raise errors.TypeEnforcementError(msg) |
|
445 |
elif type == constants.VTYPE_INT: |
|
446 |
try: |
|
447 |
target[key] = int(target[key]) |
|
448 |
except (ValueError, TypeError): |
|
449 |
msg = "'%s' (value %s) is not a valid integer" % (key, target[key]) |
|
450 |
raise errors.TypeEnforcementError(msg) |
|
451 |
|
|
452 |
|
|
390 | 453 |
def IsProcessAlive(pid): |
391 | 454 |
"""Check if a given pid exists on the system. |
392 | 455 |
|
... | ... | |
558 | 621 |
return os.path.isdir("/sys/class/net/%s/bridge" % bridge) |
559 | 622 |
|
560 | 623 |
|
561 |
def CheckBEParams(beparams): |
|
562 |
"""Checks whether the user-supplied be-params are valid, |
|
563 |
and converts them from string format where appropriate. |
|
564 |
|
|
565 |
@type beparams: dict |
|
566 |
@param beparams: new params dict |
|
567 |
|
|
568 |
""" |
|
569 |
if beparams: |
|
570 |
for item in beparams: |
|
571 |
if item not in constants.BES_PARAMETERS: |
|
572 |
raise errors.OpPrereqError("Unknown backend parameter %s" % item) |
|
573 |
if item in (constants.BE_MEMORY, constants.BE_VCPUS): |
|
574 |
val = beparams[item] |
|
575 |
if val != constants.VALUE_DEFAULT: |
|
576 |
try: |
|
577 |
val = int(val) |
|
578 |
except ValueError, err: |
|
579 |
raise errors.OpPrereqError("Invalid %s size: %s" % (item, err)) |
|
580 |
beparams[item] = val |
|
581 |
if item in (constants.BE_AUTO_BALANCE): |
|
582 |
val = beparams[item] |
|
583 |
if not isinstance(val, bool): |
|
584 |
if val == constants.VALUE_TRUE: |
|
585 |
beparams[item] = True |
|
586 |
elif val == constants.VALUE_FALSE: |
|
587 |
beparams[item] = False |
|
588 |
else: |
|
589 |
raise errors.OpPrereqError("Invalid %s value: %s" % (item, val)) |
|
590 |
|
|
591 |
|
|
592 | 624 |
def NiceSort(name_list): |
593 | 625 |
"""Sort a list of strings based on digit and non-digit groupings. |
594 | 626 |
|
Also available in: Unified diff