Revision 703fa9ab
b/lib/cli.py | ||
---|---|---|
43 | 43 |
from ganeti import compat |
44 | 44 |
from ganeti import netutils |
45 | 45 |
from ganeti import qlang |
46 |
from ganeti import objects |
|
46 | 47 |
|
47 | 48 |
from optparse import (OptionParser, TitledHelpFormatter, |
48 | 49 |
Option, OptionValueError) |
... | ... | |
201 | 202 |
"INSTANCE_POLICY_OPTS", |
202 | 203 |
# Generic functions for CLI programs |
203 | 204 |
"ConfirmOperation", |
205 |
"CreateIPolicyFromOpts", |
|
204 | 206 |
"GenericMain", |
205 | 207 |
"GenericInstanceCreate", |
206 | 208 |
"GenericList", |
... | ... | |
285 | 287 |
_CHOOSE_BATCH = 25 |
286 | 288 |
|
287 | 289 |
|
290 |
# constants used to create InstancePolicy dictionary |
|
291 |
TISPECS_GROUP_TYPES = { |
|
292 |
constants.ISPECS_MIN: constants.VTYPE_INT, |
|
293 |
constants.ISPECS_MAX: constants.VTYPE_INT, |
|
294 |
} |
|
295 |
|
|
296 |
TISPECS_CLUSTER_TYPES = { |
|
297 |
constants.ISPECS_MIN: constants.VTYPE_INT, |
|
298 |
constants.ISPECS_MAX: constants.VTYPE_INT, |
|
299 |
constants.ISPECS_STD: constants.VTYPE_INT, |
|
300 |
} |
|
301 |
|
|
302 |
|
|
288 | 303 |
class _Argument: |
289 | 304 |
def __init__(self, min=0, max=None): # pylint: disable=W0622 |
290 | 305 |
self.min = min |
... | ... | |
3393 | 3408 |
choices.pop(1) |
3394 | 3409 |
choice = AskUser(msg + affected, choices) |
3395 | 3410 |
return choice |
3411 |
|
|
3412 |
|
|
3413 |
def CreateIPolicyFromOpts(ispecs_mem_size=None, |
|
3414 |
ispecs_cpu_count=None, |
|
3415 |
ispecs_disk_count=None, |
|
3416 |
ispecs_disk_size=None, |
|
3417 |
ispecs_nic_count=None, |
|
3418 |
ipolicy_disk_templates=None, |
|
3419 |
ipolicy_vcpu_ratio=None, |
|
3420 |
group_ipolicy=False, |
|
3421 |
allowed_values=None, |
|
3422 |
fill_all=False): |
|
3423 |
"""Creation of instance policy based on command line options. |
|
3424 |
|
|
3425 |
@param fill_all: whether for cluster policies we should ensure that |
|
3426 |
all values are filled |
|
3427 |
|
|
3428 |
|
|
3429 |
""" |
|
3430 |
# prepare ipolicy dict |
|
3431 |
ipolicy_transposed = { |
|
3432 |
constants.ISPEC_MEM_SIZE: ispecs_mem_size, |
|
3433 |
constants.ISPEC_CPU_COUNT: ispecs_cpu_count, |
|
3434 |
constants.ISPEC_DISK_COUNT: ispecs_disk_count, |
|
3435 |
constants.ISPEC_DISK_SIZE: ispecs_disk_size, |
|
3436 |
constants.ISPEC_NIC_COUNT: ispecs_nic_count, |
|
3437 |
} |
|
3438 |
|
|
3439 |
# first, check that the values given are correct |
|
3440 |
if group_ipolicy: |
|
3441 |
forced_type = TISPECS_GROUP_TYPES |
|
3442 |
else: |
|
3443 |
forced_type = TISPECS_CLUSTER_TYPES |
|
3444 |
|
|
3445 |
for specs in ipolicy_transposed.values(): |
|
3446 |
utils.ForceDictType(specs, forced_type, allowed_values=allowed_values) |
|
3447 |
|
|
3448 |
# then transpose |
|
3449 |
ipolicy_out = objects.MakeEmptyIPolicy() |
|
3450 |
for name, specs in ipolicy_transposed.iteritems(): |
|
3451 |
assert name in constants.ISPECS_PARAMETERS |
|
3452 |
for key, val in specs.items(): # {min: .. ,max: .., std: ..} |
|
3453 |
ipolicy_out[key][name] = val |
|
3454 |
|
|
3455 |
# no filldict for non-dicts |
|
3456 |
if not group_ipolicy and fill_all: |
|
3457 |
if ipolicy_disk_templates is None: |
|
3458 |
ipolicy_disk_templates = constants.DISK_TEMPLATES |
|
3459 |
if ipolicy_vcpu_ratio is None: |
|
3460 |
ipolicy_vcpu_ratio = \ |
|
3461 |
constants.IPOLICY_DEFAULTS[constants.IPOLICY_VCPU_RATIO] |
|
3462 |
if ipolicy_disk_templates is not None: |
|
3463 |
ipolicy_out[constants.IPOLICY_DTS] = list(ipolicy_disk_templates) |
|
3464 |
if ipolicy_vcpu_ratio is not None: |
|
3465 |
ipolicy_out[constants.IPOLICY_VCPU_RATIO] = ipolicy_vcpu_ratio |
|
3466 |
|
|
3467 |
assert not (frozenset(ipolicy_out.keys()) - constants.IPOLICY_ALL_KEYS) |
|
3468 |
|
|
3469 |
return ipolicy_out |
b/lib/client/gnt_cluster.py | ||
---|---|---|
143 | 143 |
utils.ForceDictType(diskparams[templ], constants.DISK_DT_TYPES) |
144 | 144 |
|
145 | 145 |
# prepare ipolicy dict |
146 |
ipolicy_raw = objects.CreateIPolicyFromOpts(
|
|
146 |
ipolicy_raw = CreateIPolicyFromOpts( |
|
147 | 147 |
ispecs_mem_size=opts.ispecs_mem_size, |
148 | 148 |
ispecs_cpu_count=opts.ispecs_cpu_count, |
149 | 149 |
ispecs_disk_count=opts.ispecs_disk_count, |
... | ... | |
1002 | 1002 |
if ndparams is not None: |
1003 | 1003 |
utils.ForceDictType(ndparams, constants.NDS_PARAMETER_TYPES) |
1004 | 1004 |
|
1005 |
ipolicy = objects.CreateIPolicyFromOpts(
|
|
1005 |
ipolicy = CreateIPolicyFromOpts( |
|
1006 | 1006 |
ispecs_mem_size=opts.ispecs_mem_size, |
1007 | 1007 |
ispecs_cpu_count=opts.ispecs_cpu_count, |
1008 | 1008 |
ispecs_disk_count=opts.ispecs_disk_count, |
b/lib/client/gnt_group.py | ||
---|---|---|
26 | 26 |
|
27 | 27 |
from ganeti.cli import * |
28 | 28 |
from ganeti import constants |
29 |
from ganeti import objects |
|
30 | 29 |
from ganeti import opcodes |
31 | 30 |
from ganeti import utils |
32 | 31 |
from cStringIO import StringIO |
... | ... | |
49 | 48 |
@return: the desired exit code |
50 | 49 |
|
51 | 50 |
""" |
52 |
ipolicy = \
|
|
53 |
objects.CreateIPolicyFromOpts(ispecs_mem_size=opts.ispecs_mem_size,
|
|
54 |
ispecs_cpu_count=opts.ispecs_cpu_count,
|
|
55 |
ispecs_disk_count=opts.ispecs_disk_count,
|
|
56 |
ispecs_disk_size=opts.ispecs_disk_size,
|
|
57 |
ispecs_nic_count=opts.ispecs_nic_count,
|
|
58 |
group_ipolicy=True)
|
|
51 |
ipolicy = CreateIPolicyFromOpts(
|
|
52 |
ispecs_mem_size=opts.ispecs_mem_size, |
|
53 |
ispecs_cpu_count=opts.ispecs_cpu_count, |
|
54 |
ispecs_disk_count=opts.ispecs_disk_count, |
|
55 |
ispecs_disk_size=opts.ispecs_disk_size, |
|
56 |
ispecs_nic_count=opts.ispecs_nic_count, |
|
57 |
group_ipolicy=True) |
|
59 | 58 |
|
60 | 59 |
(group_name,) = args |
61 | 60 |
diskparams = dict(opts.diskparams) |
... | ... | |
185 | 184 |
if ispec[param].lower() == "default": |
186 | 185 |
ispec[param] = constants.VALUE_DEFAULT |
187 | 186 |
# create ipolicy object |
188 |
ipolicy = objects.CreateIPolicyFromOpts(\
|
|
187 |
ipolicy = CreateIPolicyFromOpts(
|
|
189 | 188 |
ispecs_mem_size=opts.ispecs_mem_size, |
190 | 189 |
ispecs_cpu_count=opts.ispecs_cpu_count, |
191 | 190 |
ispecs_disk_count=opts.ispecs_disk_count, |
b/lib/objects.py | ||
---|---|---|
55 | 55 |
_TIMESTAMPS = ["ctime", "mtime"] |
56 | 56 |
_UUID = ["uuid"] |
57 | 57 |
|
58 |
# constants used to create InstancePolicy dictionary |
|
59 |
TISPECS_GROUP_TYPES = { |
|
60 |
constants.ISPECS_MIN: constants.VTYPE_INT, |
|
61 |
constants.ISPECS_MAX: constants.VTYPE_INT, |
|
62 |
} |
|
63 |
|
|
64 |
TISPECS_CLUSTER_TYPES = { |
|
65 |
constants.ISPECS_MIN: constants.VTYPE_INT, |
|
66 |
constants.ISPECS_MAX: constants.VTYPE_INT, |
|
67 |
constants.ISPECS_STD: constants.VTYPE_INT, |
|
68 |
} |
|
69 |
|
|
70 | 58 |
|
71 | 59 |
def FillDict(defaults_dict, custom_dict, skip_keys=None): |
72 | 60 |
"""Basic function to apply settings on top a default dict. |
... | ... | |
199 | 187 |
]) |
200 | 188 |
|
201 | 189 |
|
202 |
def CreateIPolicyFromOpts(ispecs_mem_size=None, |
|
203 |
ispecs_cpu_count=None, |
|
204 |
ispecs_disk_count=None, |
|
205 |
ispecs_disk_size=None, |
|
206 |
ispecs_nic_count=None, |
|
207 |
ipolicy_disk_templates=None, |
|
208 |
ipolicy_vcpu_ratio=None, |
|
209 |
group_ipolicy=False, |
|
210 |
allowed_values=None, |
|
211 |
fill_all=False): |
|
212 |
"""Creation of instance policy based on command line options. |
|
213 |
|
|
214 |
@param fill_all: whether for cluster policies we should ensure that |
|
215 |
all values are filled |
|
216 |
|
|
217 |
|
|
218 |
""" |
|
219 |
# prepare ipolicy dict |
|
220 |
ipolicy_transposed = { |
|
221 |
constants.ISPEC_MEM_SIZE: ispecs_mem_size, |
|
222 |
constants.ISPEC_CPU_COUNT: ispecs_cpu_count, |
|
223 |
constants.ISPEC_DISK_COUNT: ispecs_disk_count, |
|
224 |
constants.ISPEC_DISK_SIZE: ispecs_disk_size, |
|
225 |
constants.ISPEC_NIC_COUNT: ispecs_nic_count, |
|
226 |
} |
|
227 |
|
|
228 |
# first, check that the values given are correct |
|
229 |
if group_ipolicy: |
|
230 |
forced_type = TISPECS_GROUP_TYPES |
|
231 |
else: |
|
232 |
forced_type = TISPECS_CLUSTER_TYPES |
|
233 |
|
|
234 |
for specs in ipolicy_transposed.values(): |
|
235 |
utils.ForceDictType(specs, forced_type, allowed_values=allowed_values) |
|
236 |
|
|
237 |
# then transpose |
|
238 |
ipolicy_out = MakeEmptyIPolicy() |
|
239 |
for name, specs in ipolicy_transposed.iteritems(): |
|
240 |
assert name in constants.ISPECS_PARAMETERS |
|
241 |
for key, val in specs.items(): # {min: .. ,max: .., std: ..} |
|
242 |
ipolicy_out[key][name] = val |
|
243 |
|
|
244 |
# no filldict for non-dicts |
|
245 |
if not group_ipolicy and fill_all: |
|
246 |
if ipolicy_disk_templates is None: |
|
247 |
ipolicy_disk_templates = constants.DISK_TEMPLATES |
|
248 |
if ipolicy_vcpu_ratio is None: |
|
249 |
ipolicy_vcpu_ratio = \ |
|
250 |
constants.IPOLICY_DEFAULTS[constants.IPOLICY_VCPU_RATIO] |
|
251 |
if ipolicy_disk_templates is not None: |
|
252 |
ipolicy_out[constants.IPOLICY_DTS] = list(ipolicy_disk_templates) |
|
253 |
if ipolicy_vcpu_ratio is not None: |
|
254 |
ipolicy_out[constants.IPOLICY_VCPU_RATIO] = ipolicy_vcpu_ratio |
|
255 |
|
|
256 |
assert not (frozenset(ipolicy_out.keys()) - constants.IPOLICY_ALL_KEYS) |
|
257 |
|
|
258 |
return ipolicy_out |
|
259 |
|
|
260 |
|
|
261 | 190 |
class ConfigObject(object): |
262 | 191 |
"""A generic config object. |
263 | 192 |
|
Also available in: Unified diff