+ data = actual[key]
+ buf.write("%s- %s:" % (indent, key))
+
+ if isinstance(data, dict) and data:
+ buf.write("\n")
+ FormatParameterDict(buf, param_dict.get(key, {}), data,
+ level=level + 1)
+ else:
+ val = param_dict.get(key, "default (%s)" % data)
+ buf.write(" %s\n" % val)
+
+
+def ConfirmOperation(names, list_type, text, extra=""):
+ """Ask the user to confirm an operation on a list of list_type.
+
+ This function is used to request confirmation for doing an operation
+ on a given list of list_type.
+
+ @type names: list
+ @param names: the list of names that we display when
+ we ask for confirmation
+ @type list_type: str
+ @param list_type: Human readable name for elements in the list (e.g. nodes)
+ @type text: str
+ @param text: the operation that the user should confirm
+ @rtype: boolean
+ @return: True or False depending on user's confirmation.
+
+ """
+ count = len(names)
+ msg = ("The %s will operate on %d %s.\n%s"
+ "Do you want to continue?" % (text, count, list_type, extra))
+ affected = (("\nAffected %s:\n" % list_type) +
+ "\n".join([" %s" % name for name in names]))
+
+ choices = [("y", True, "Yes, execute the %s" % text),
+ ("n", False, "No, abort the %s" % text)]
+
+ if count > 20:
+ choices.insert(1, ("v", "v", "View the list of affected %s" % list_type))
+ question = msg
+ else:
+ question = msg + affected
+
+ choice = AskUser(question, choices)
+ if choice == "v":
+ choices.pop(1)
+ choice = AskUser(msg + affected, choices)
+ return choice
+
+
+def _MaybeParseUnit(elements):
+ """Parses and returns an array of potential values with units.
+
+ """
+ parsed = {}
+ for k, v in elements.items():
+ if v == constants.VALUE_DEFAULT:
+ parsed[k] = v
+ else:
+ parsed[k] = utils.ParseUnit(v)
+ return parsed
+
+
+def CreateIPolicyFromOpts(ispecs_mem_size=None,
+ ispecs_cpu_count=None,
+ ispecs_disk_count=None,
+ ispecs_disk_size=None,
+ ispecs_nic_count=None,
+ ipolicy_disk_templates=None,
+ ipolicy_vcpu_ratio=None,
+ ipolicy_spindle_ratio=None,
+ group_ipolicy=False,
+ allowed_values=None,
+ fill_all=False):
+ """Creation of instance policy based on command line options.
+
+ @param fill_all: whether for cluster policies we should ensure that
+ all values are filled
+
+
+ """
+ try:
+ if ispecs_mem_size:
+ ispecs_mem_size = _MaybeParseUnit(ispecs_mem_size)
+ if ispecs_disk_size:
+ ispecs_disk_size = _MaybeParseUnit(ispecs_disk_size)
+ except (TypeError, ValueError, errors.UnitParseError), err:
+ raise errors.OpPrereqError("Invalid disk (%s) or memory (%s) size"
+ " in policy: %s" %
+ (ispecs_disk_size, ispecs_mem_size, err),
+ errors.ECODE_INVAL)
+
+ # prepare ipolicy dict
+ ipolicy_transposed = {
+ constants.ISPEC_MEM_SIZE: ispecs_mem_size,
+ constants.ISPEC_CPU_COUNT: ispecs_cpu_count,
+ constants.ISPEC_DISK_COUNT: ispecs_disk_count,
+ constants.ISPEC_DISK_SIZE: ispecs_disk_size,
+ constants.ISPEC_NIC_COUNT: ispecs_nic_count,
+ }
+
+ # first, check that the values given are correct
+ if group_ipolicy:
+ forced_type = TISPECS_GROUP_TYPES
+ else:
+ forced_type = TISPECS_CLUSTER_TYPES
+
+ for specs in ipolicy_transposed.values():
+ utils.ForceDictType(specs, forced_type, allowed_values=allowed_values)
+
+ # then transpose
+ ipolicy_out = objects.MakeEmptyIPolicy()
+ for name, specs in ipolicy_transposed.iteritems():
+ assert name in constants.ISPECS_PARAMETERS
+ for key, val in specs.items(): # {min: .. ,max: .., std: ..}
+ ipolicy_out[key][name] = val
+
+ # no filldict for non-dicts
+ if not group_ipolicy and fill_all:
+ if ipolicy_disk_templates is None:
+ ipolicy_disk_templates = constants.DISK_TEMPLATES
+ if ipolicy_vcpu_ratio is None:
+ ipolicy_vcpu_ratio = \
+ constants.IPOLICY_DEFAULTS[constants.IPOLICY_VCPU_RATIO]
+ if ipolicy_spindle_ratio is None:
+ ipolicy_spindle_ratio = \
+ constants.IPOLICY_DEFAULTS[constants.IPOLICY_SPINDLE_RATIO]
+ if ipolicy_disk_templates is not None:
+ ipolicy_out[constants.IPOLICY_DTS] = list(ipolicy_disk_templates)
+ if ipolicy_vcpu_ratio is not None:
+ ipolicy_out[constants.IPOLICY_VCPU_RATIO] = ipolicy_vcpu_ratio
+ if ipolicy_spindle_ratio is not None:
+ ipolicy_out[constants.IPOLICY_SPINDLE_RATIO] = ipolicy_spindle_ratio
+
+ assert not (frozenset(ipolicy_out.keys()) - constants.IPOLICY_ALL_KEYS)
+
+ return ipolicy_out