Commit 703fa9ab authored by Iustin Pop's avatar Iustin Pop
Browse files

Move some ipolicy functions from objects to cli



These are only used in CLI programs to parse command line options and
such they do not belong in object.py (I will change them more and I
don't want to add more code in objects.py).
Signed-off-by: default avatarIustin Pop <iustin@google.com>
Reviewed-by: default avatarMichael Hanselmann <hansmi@google.com>
parent ea0f78c8
......@@ -43,6 +43,7 @@ from ganeti import ssh
from ganeti import compat
from ganeti import netutils
from ganeti import qlang
from ganeti import objects
from optparse import (OptionParser, TitledHelpFormatter,
Option, OptionValueError)
......@@ -201,6 +202,7 @@ __all__ = [
"INSTANCE_POLICY_OPTS",
# Generic functions for CLI programs
"ConfirmOperation",
"CreateIPolicyFromOpts",
"GenericMain",
"GenericInstanceCreate",
"GenericList",
......@@ -285,6 +287,19 @@ _PRIONAME_TO_VALUE = dict(_PRIORITY_NAMES)
_CHOOSE_BATCH = 25
# constants used to create InstancePolicy dictionary
TISPECS_GROUP_TYPES = {
constants.ISPECS_MIN: constants.VTYPE_INT,
constants.ISPECS_MAX: constants.VTYPE_INT,
}
TISPECS_CLUSTER_TYPES = {
constants.ISPECS_MIN: constants.VTYPE_INT,
constants.ISPECS_MAX: constants.VTYPE_INT,
constants.ISPECS_STD: constants.VTYPE_INT,
}
class _Argument:
def __init__(self, min=0, max=None): # pylint: disable=W0622
self.min = min
......@@ -3393,3 +3408,62 @@ def ConfirmOperation(names, list_type, text, extra=""):
choices.pop(1)
choice = AskUser(msg + affected, choices)
return choice
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,
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
"""
# 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_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
assert not (frozenset(ipolicy_out.keys()) - constants.IPOLICY_ALL_KEYS)
return ipolicy_out
......@@ -143,7 +143,7 @@ def InitCluster(opts, args):
utils.ForceDictType(diskparams[templ], constants.DISK_DT_TYPES)
# prepare ipolicy dict
ipolicy_raw = objects.CreateIPolicyFromOpts(
ipolicy_raw = CreateIPolicyFromOpts(
ispecs_mem_size=opts.ispecs_mem_size,
ispecs_cpu_count=opts.ispecs_cpu_count,
ispecs_disk_count=opts.ispecs_disk_count,
......@@ -1002,7 +1002,7 @@ def SetClusterParams(opts, args):
if ndparams is not None:
utils.ForceDictType(ndparams, constants.NDS_PARAMETER_TYPES)
ipolicy = objects.CreateIPolicyFromOpts(
ipolicy = CreateIPolicyFromOpts(
ispecs_mem_size=opts.ispecs_mem_size,
ispecs_cpu_count=opts.ispecs_cpu_count,
ispecs_disk_count=opts.ispecs_disk_count,
......
......@@ -26,7 +26,6 @@
from ganeti.cli import *
from ganeti import constants
from ganeti import objects
from ganeti import opcodes
from ganeti import utils
from cStringIO import StringIO
......@@ -49,13 +48,13 @@ def AddGroup(opts, args):
@return: the desired exit code
"""
ipolicy = \
objects.CreateIPolicyFromOpts(ispecs_mem_size=opts.ispecs_mem_size,
ispecs_cpu_count=opts.ispecs_cpu_count,
ispecs_disk_count=opts.ispecs_disk_count,
ispecs_disk_size=opts.ispecs_disk_size,
ispecs_nic_count=opts.ispecs_nic_count,
group_ipolicy=True)
ipolicy = CreateIPolicyFromOpts(
ispecs_mem_size=opts.ispecs_mem_size,
ispecs_cpu_count=opts.ispecs_cpu_count,
ispecs_disk_count=opts.ispecs_disk_count,
ispecs_disk_size=opts.ispecs_disk_size,
ispecs_nic_count=opts.ispecs_nic_count,
group_ipolicy=True)
(group_name,) = args
diskparams = dict(opts.diskparams)
......@@ -185,7 +184,7 @@ def SetGroupParams(opts, args):
if ispec[param].lower() == "default":
ispec[param] = constants.VALUE_DEFAULT
# create ipolicy object
ipolicy = objects.CreateIPolicyFromOpts(\
ipolicy = CreateIPolicyFromOpts(
ispecs_mem_size=opts.ispecs_mem_size,
ispecs_cpu_count=opts.ispecs_cpu_count,
ispecs_disk_count=opts.ispecs_disk_count,
......
......@@ -55,18 +55,6 @@ __all__ = ["ConfigObject", "ConfigData", "NIC", "Disk", "Instance",
_TIMESTAMPS = ["ctime", "mtime"]
_UUID = ["uuid"]
# constants used to create InstancePolicy dictionary
TISPECS_GROUP_TYPES = {
constants.ISPECS_MIN: constants.VTYPE_INT,
constants.ISPECS_MAX: constants.VTYPE_INT,
}
TISPECS_CLUSTER_TYPES = {
constants.ISPECS_MIN: constants.VTYPE_INT,
constants.ISPECS_MAX: constants.VTYPE_INT,
constants.ISPECS_STD: constants.VTYPE_INT,
}
def FillDict(defaults_dict, custom_dict, skip_keys=None):
"""Basic function to apply settings on top a default dict.
......@@ -199,65 +187,6 @@ def MakeEmptyIPolicy():
])
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,
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
"""
# 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 = 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_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
assert not (frozenset(ipolicy_out.keys()) - constants.IPOLICY_ALL_KEYS)
return ipolicy_out
class ConfigObject(object):
"""A generic config object.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment