Revision 5eacbcae lib/cmdlib/instance_utils.py
b/lib/cmdlib/instance_utils.py | ||
---|---|---|
31 | 31 |
from ganeti import objects |
32 | 32 |
from ganeti import pathutils |
33 | 33 |
from ganeti import utils |
34 |
from ganeti.cmdlib.common import _AnnotateDiskParams, \
|
|
35 |
_ComputeIPolicyInstanceViolation
|
|
34 |
from ganeti.cmdlib.common import AnnotateDiskParams, \ |
|
35 |
ComputeIPolicyInstanceViolation |
|
36 | 36 |
|
37 | 37 |
|
38 |
def _BuildInstanceHookEnv(name, primary_node, secondary_nodes, os_type, status,
|
|
39 |
minmem, maxmem, vcpus, nics, disk_template, disks,
|
|
40 |
bep, hvp, hypervisor_name, tags):
|
|
38 |
def BuildInstanceHookEnv(name, primary_node, secondary_nodes, os_type, status, |
|
39 |
minmem, maxmem, vcpus, nics, disk_template, disks, |
|
40 |
bep, hvp, hypervisor_name, tags): |
|
41 | 41 |
"""Builds instance related env variables for hooks |
42 | 42 |
|
43 | 43 |
This builds the hook environment from individual variables. |
... | ... | |
140 | 140 |
return env |
141 | 141 |
|
142 | 142 |
|
143 |
def _BuildInstanceHookEnvByObject(lu, instance, override=None):
|
|
143 |
def BuildInstanceHookEnvByObject(lu, instance, override=None): |
|
144 | 144 |
"""Builds instance related env variables for hooks from an object. |
145 | 145 |
|
146 | 146 |
@type lu: L{LogicalUnit} |
... | ... | |
167 | 167 |
"maxmem": bep[constants.BE_MAXMEM], |
168 | 168 |
"minmem": bep[constants.BE_MINMEM], |
169 | 169 |
"vcpus": bep[constants.BE_VCPUS], |
170 |
"nics": _NICListToTuple(lu, instance.nics),
|
|
170 |
"nics": NICListToTuple(lu, instance.nics), |
|
171 | 171 |
"disk_template": instance.disk_template, |
172 | 172 |
"disks": [(disk.name, disk.size, disk.mode) |
173 | 173 |
for disk in instance.disks], |
... | ... | |
178 | 178 |
} |
179 | 179 |
if override: |
180 | 180 |
args.update(override) |
181 |
return _BuildInstanceHookEnv(**args) # pylint: disable=W0142
|
|
181 |
return BuildInstanceHookEnv(**args) # pylint: disable=W0142 |
|
182 | 182 |
|
183 | 183 |
|
184 |
def _GetClusterDomainSecret():
|
|
184 |
def GetClusterDomainSecret(): |
|
185 | 185 |
"""Reads the cluster domain secret. |
186 | 186 |
|
187 | 187 |
""" |
... | ... | |
189 | 189 |
strict=True) |
190 | 190 |
|
191 | 191 |
|
192 |
def _CheckNodeNotDrained(lu, node):
|
|
192 |
def CheckNodeNotDrained(lu, node): |
|
193 | 193 |
"""Ensure that a given node is not drained. |
194 | 194 |
|
195 | 195 |
@param lu: the LU on behalf of which we make the check |
... | ... | |
202 | 202 |
errors.ECODE_STATE) |
203 | 203 |
|
204 | 204 |
|
205 |
def _CheckNodeVmCapable(lu, node):
|
|
205 |
def CheckNodeVmCapable(lu, node): |
|
206 | 206 |
"""Ensure that a given node is vm capable. |
207 | 207 |
|
208 | 208 |
@param lu: the LU on behalf of which we make the check |
... | ... | |
215 | 215 |
errors.ECODE_STATE) |
216 | 216 |
|
217 | 217 |
|
218 |
def _RemoveInstance(lu, feedback_fn, instance, ignore_failures):
|
|
218 |
def RemoveInstance(lu, feedback_fn, instance, ignore_failures): |
|
219 | 219 |
"""Utility function to remove an instance. |
220 | 220 |
|
221 | 221 |
""" |
222 | 222 |
logging.info("Removing block devices for instance %s", instance.name) |
223 | 223 |
|
224 |
if not _RemoveDisks(lu, instance, ignore_failures=ignore_failures):
|
|
224 |
if not RemoveDisks(lu, instance, ignore_failures=ignore_failures): |
|
225 | 225 |
if not ignore_failures: |
226 | 226 |
raise errors.OpExecError("Can't remove instance's disks") |
227 | 227 |
feedback_fn("Warning: can't remove instance's disks") |
... | ... | |
237 | 237 |
lu.remove_locks[locking.LEVEL_INSTANCE] = instance.name |
238 | 238 |
|
239 | 239 |
|
240 |
def _RemoveDisks(lu, instance, target_node=None, ignore_failures=False):
|
|
240 |
def RemoveDisks(lu, instance, target_node=None, ignore_failures=False): |
|
241 | 241 |
"""Remove all disks for an instance. |
242 | 242 |
|
243 | 243 |
This abstracts away some work from `AddInstance()` and |
... | ... | |
258 | 258 |
|
259 | 259 |
all_result = True |
260 | 260 |
ports_to_release = set() |
261 |
anno_disks = _AnnotateDiskParams(instance, instance.disks, lu.cfg)
|
|
261 |
anno_disks = AnnotateDiskParams(instance, instance.disks, lu.cfg) |
|
262 | 262 |
for (idx, device) in enumerate(anno_disks): |
263 | 263 |
if target_node: |
264 | 264 |
edata = [(target_node, device)] |
... | ... | |
296 | 296 |
return all_result |
297 | 297 |
|
298 | 298 |
|
299 |
def _NICToTuple(lu, nic):
|
|
299 |
def NICToTuple(lu, nic): |
|
300 | 300 |
"""Build a tupple of nic information. |
301 | 301 |
|
302 | 302 |
@type lu: L{LogicalUnit} |
... | ... | |
316 | 316 |
return (nic.name, nic.uuid, nic.ip, nic.mac, mode, link, nic.network, netinfo) |
317 | 317 |
|
318 | 318 |
|
319 |
def _NICListToTuple(lu, nics):
|
|
319 |
def NICListToTuple(lu, nics): |
|
320 | 320 |
"""Build a list of nic information tuples. |
321 | 321 |
|
322 | 322 |
This list is suitable to be passed to _BuildInstanceHookEnv or as a return |
... | ... | |
330 | 330 |
""" |
331 | 331 |
hooks_nics = [] |
332 | 332 |
for nic in nics: |
333 |
hooks_nics.append(_NICToTuple(lu, nic))
|
|
333 |
hooks_nics.append(NICToTuple(lu, nic)) |
|
334 | 334 |
return hooks_nics |
335 | 335 |
|
336 | 336 |
|
337 |
def _CopyLockList(names):
|
|
337 |
def CopyLockList(names): |
|
338 | 338 |
"""Makes a copy of a list of lock names. |
339 | 339 |
|
340 | 340 |
Handles L{locking.ALL_SET} correctly. |
... | ... | |
346 | 346 |
return names[:] |
347 | 347 |
|
348 | 348 |
|
349 |
def _ReleaseLocks(lu, level, names=None, keep=None):
|
|
349 |
def ReleaseLocks(lu, level, names=None, keep=None): |
|
350 | 350 |
"""Releases locks owned by an LU. |
351 | 351 |
|
352 | 352 |
@type lu: L{LogicalUnit} |
... | ... | |
398 | 398 |
|
399 | 399 |
def _ComputeIPolicyNodeViolation(ipolicy, instance, current_group, |
400 | 400 |
target_group, cfg, |
401 |
_compute_fn=_ComputeIPolicyInstanceViolation):
|
|
401 |
_compute_fn=ComputeIPolicyInstanceViolation): |
|
402 | 402 |
"""Compute if instance meets the specs of the new target group. |
403 | 403 |
|
404 | 404 |
@param ipolicy: The ipolicy to verify |
... | ... | |
408 | 408 |
@type cfg: L{config.ConfigWriter} |
409 | 409 |
@param cfg: Cluster configuration |
410 | 410 |
@param _compute_fn: The function to verify ipolicy (unittest only) |
411 |
@see: L{ganeti.cmdlib.common._ComputeIPolicySpecViolation}
|
|
411 |
@see: L{ganeti.cmdlib.common.ComputeIPolicySpecViolation} |
|
412 | 412 |
|
413 | 413 |
""" |
414 | 414 |
if current_group == target_group: |
... | ... | |
417 | 417 |
return _compute_fn(ipolicy, instance, cfg) |
418 | 418 |
|
419 | 419 |
|
420 |
def _CheckTargetNodeIPolicy(lu, ipolicy, instance, node, cfg, ignore=False,
|
|
421 |
_compute_fn=_ComputeIPolicyNodeViolation):
|
|
420 |
def CheckTargetNodeIPolicy(lu, ipolicy, instance, node, cfg, ignore=False, |
|
421 |
_compute_fn=_ComputeIPolicyNodeViolation): |
|
422 | 422 |
"""Checks that the target node is correct in terms of instance policy. |
423 | 423 |
|
424 | 424 |
@param ipolicy: The ipolicy to verify |
... | ... | |
428 | 428 |
@param cfg: Cluster configuration |
429 | 429 |
@param ignore: Ignore violations of the ipolicy |
430 | 430 |
@param _compute_fn: The function to verify ipolicy (unittest only) |
431 |
@see: L{ganeti.cmdlib.common._ComputeIPolicySpecViolation}
|
|
431 |
@see: L{ganeti.cmdlib.common.ComputeIPolicySpecViolation} |
|
432 | 432 |
|
433 | 433 |
""" |
434 | 434 |
primary_node = lu.cfg.GetNodeInfo(instance.primary_node) |
... | ... | |
443 | 443 |
raise errors.OpPrereqError(msg, errors.ECODE_INVAL) |
444 | 444 |
|
445 | 445 |
|
446 |
def _GetInstanceInfoText(instance):
|
|
446 |
def GetInstanceInfoText(instance): |
|
447 | 447 |
"""Compute that text that should be added to the disk's metadata. |
448 | 448 |
|
449 | 449 |
""" |
450 | 450 |
return "originstname+%s" % instance.name |
451 | 451 |
|
452 | 452 |
|
453 |
def _CheckNodeFreeMemory(lu, node, reason, requested, hypervisor_name):
|
|
453 |
def CheckNodeFreeMemory(lu, node, reason, requested, hypervisor_name): |
|
454 | 454 |
"""Checks if a node has enough free memory. |
455 | 455 |
|
456 | 456 |
This function checks if a given node has the needed amount of free |
... | ... | |
492 | 492 |
return free_mem |
493 | 493 |
|
494 | 494 |
|
495 |
def _CheckInstanceBridgesExist(lu, instance, node=None):
|
|
495 |
def CheckInstanceBridgesExist(lu, instance, node=None): |
|
496 | 496 |
"""Check that the brigdes needed by an instance exist. |
497 | 497 |
|
498 | 498 |
""" |
499 | 499 |
if node is None: |
500 | 500 |
node = instance.primary_node |
501 |
_CheckNicsBridgesExist(lu, instance.nics, node)
|
|
501 |
CheckNicsBridgesExist(lu, instance.nics, node) |
|
502 | 502 |
|
503 | 503 |
|
504 |
def _CheckNicsBridgesExist(lu, target_nics, target_node):
|
|
504 |
def CheckNicsBridgesExist(lu, target_nics, target_node): |
|
505 | 505 |
"""Check that the brigdes needed by a list of nics exist. |
506 | 506 |
|
507 | 507 |
""" |
... | ... | |
515 | 515 |
target_node, prereq=True, ecode=errors.ECODE_ENVIRON) |
516 | 516 |
|
517 | 517 |
|
518 |
def _CheckNodeHasOS(lu, node, os_name, force_variant):
|
|
518 |
def CheckNodeHasOS(lu, node, os_name, force_variant): |
|
519 | 519 |
"""Ensure that a node supports a given OS. |
520 | 520 |
|
521 | 521 |
@param lu: the LU on behalf of which we make the check |
Also available in: Unified diff