Package ganeti :: Package cmdlib :: Module instance_utils
[hide private]
[frames] | no frames]

Module instance_utils

source code

Utility function mainly, but not only used by instance LU's.

Functions [hide private]
dict
BuildInstanceHookEnv(name, primary_node_name, secondary_node_names, os_type, status, minmem, maxmem, vcpus, nics, disk_template, disks, bep, hvp, hypervisor_name, tags)
Builds instance related env variables for hooks
source code
dict
BuildInstanceHookEnvByObject(lu, instance, secondary_nodes=None, disks=None, override=None)
Builds instance related env variables for hooks from an object.
source code
 
GetClusterDomainSecret()
Reads the cluster domain secret.
source code
 
CheckNodeNotDrained(lu, node_uuid)
Ensure that a given node is not drained.
source code
 
CheckNodeVmCapable(lu, node_uuid)
Ensure that a given node is vm capable.
source code
 
RemoveInstance(lu, feedback_fn, instance, ignore_failures)
Utility function to remove an instance.
source code
list of file paths
_StoragePathsRemoved(removed, disks)
Returns an iterable of all storage paths to be removed.
source code
boolean
RemoveDisks(lu, instance, disks=None, target_node_uuid=None, ignore_failures=False)
Remove all or a subset of disks for an instance.
source code
 
NICToTuple(lu, nic)
Build a tupple of nic information.
source code
 
NICListToTuple(lu, nics)
Build a list of nic information tuples.
source code
 
CopyLockList(names)
Makes a copy of a list of lock names.
source code
 
ReleaseLocks(lu, level, names=None, keep=None)
Releases locks owned by an LU.
source code
 
_ComputeIPolicyNodeViolation(ipolicy, instance, current_group, target_group, cfg, _compute_fn=ComputeIPolicyInstanceViolation)
Compute if instance meets the specs of the new target group.
source code
 
CheckTargetNodeIPolicy(lu, ipolicy, instance, node, cfg, ignore=False, _compute_fn=_ComputeIPolicyNodeViolation)
Checks that the target node is correct in terms of instance policy.
source code
 
GetInstanceInfoText(instance)
Compute that text that should be added to the disk's metadata.
source code
integer
CheckNodeFreeMemory(lu, node_uuid, reason, requested, hvname, hvparams)
Checks if a node has enough free memory.
source code
 
CheckInstanceBridgesExist(lu, instance, node_uuid=None)
Check that the brigdes needed by an instance exist.
source code
 
CheckNicsBridgesExist(lu, nics, node_uuid)
Check that the brigdes needed by a list of nics exist.
source code
NoneType
UpdateMetadata(feedback_fn, rpc, instance, osparams_public=None, osparams_private=None, osparams_secret=None)
Updates instance metadata on the metadata daemon on the instance's primary node.
source code
NoneType
CheckCompressionTool(lu, compression_tool)
Checks if the provided compression tool is allowed to be used.
source code
 
BuildDiskLogicalIDEnv(idx, disk)
Helper method to create hooks env related to disk's logical_id
source code
 
BuildDiskEnv(idx, disk)
Helper method to create disk's hooks env
source code
 
CheckInstanceExistence(lu, instance_name)
Raises an error if an instance with the given name exists already.
source code
 
CheckForConflictingIp(lu, ip, node_uuid)
In case of conflicting IP address raise error.
source code
 
ComputeIPolicyInstanceSpecViolation(ipolicy, instance_spec, disk_types, _compute_fn=ComputeIPolicySpecViolation)
Compute if instance specs meets the specs of ipolicy.
source code
string
ComputeInstanceCommunicationNIC(instance_name)
Compute the name of the instance NIC used by instance communication.
source code
list
PrepareContainerMods(mods, private_fn)
Prepares a list of container modifications by adding a private data field.
source code
 
ApplyContainerMods(kind, container, chgdesc, mods, create_fn, attach_fn, modify_fn, remove_fn, detach_fn, post_add_fn=None)
Applies descriptions in mods to container.
source code
 
GetItemFromContainer(identifier, kind, container)
Return the item refered by the identifier.
source code
 
GetIndexFromIdentifier(identifier, kind, container)
Check if the identifier represents a valid container index and return it.
source code
 
InsertItemToIndex(identifier, item, container)
Insert an item to the provided index of a container.
source code
 
CheckNodesPhysicalCPUs(lu, node_uuids, requested, hypervisor_specs)
Checks if nodes have enough physical CPUs
source code
 
CheckHostnameSane(lu, name)
Ensures that a given hostname resolves to a 'sane' name.
source code
 
CheckOpportunisticLocking(op)
Generate error if opportunistic locking is not possible.
source code
 
CreateInstanceAllocRequest(op, disks, nics, beparams, node_name_whitelist)
Wrapper around IAReqInstanceAlloc.
source code
 
ComputeFullBeParams(op, cluster)
Computes the full beparams.
source code
 
ComputeNics(op, cluster, default_ip, cfg, ec_id)
Computes the nics.
source code
Variables [hide private]
  _TApplyContModsCbChanges = ht.TMaybeListOf(ht.TAnd(ht.TIsLengt...
Type description for changes as returned by ApplyContainerMods's callbacks

Imports: logging, os, constants, errors, ht, locking, iallocator, network, netutils, objects, pathutils, utils, AnnotateDiskParams, ComputeIPolicyInstanceViolation, CheckDiskTemplateEnabled, ComputeIPolicySpecViolation


Function Details [hide private]

BuildInstanceHookEnv(name, primary_node_name, secondary_node_names, os_type, status, minmem, maxmem, vcpus, nics, disk_template, disks, bep, hvp, hypervisor_name, tags)

source code 

Builds instance related env variables for hooks

This builds the hook environment from individual variables.

Parameters:
  • name (string) - the name of the instance
  • primary_node_name (string) - the name of the instance's primary node
  • secondary_node_names (list) - list of secondary nodes as strings
  • os_type (string) - the name of the instance's OS
  • status (string) - the desired status of the instance
  • minmem (string) - the minimum memory size of the instance
  • maxmem (string) - the maximum memory size of the instance
  • vcpus (string) - the count of VCPUs the instance has
  • nics (list) - list of tuples (name, uuid, ip, mac, mode, link, vlan, net, netinfo) representing the NICs the instance has
  • disk_template (string) - the disk template of the instance
  • disks (list) - list of disks (either objects.Disk or dict)
  • bep (dict) - the backend parameters for the instance
  • hvp (dict) - the hypervisor parameters for the instance
  • hypervisor_name (string) - the hypervisor for the instance
  • tags (list) - list of instance tags as strings
Returns: dict
the hook environment for this instance

BuildInstanceHookEnvByObject(lu, instance, secondary_nodes=None, disks=None, override=None)

source code 

Builds instance related env variables for hooks from an object.

Parameters:
  • lu (LogicalUnit) - the logical unit on whose behalf we execute
  • instance (objects.Instance) - the instance for which we should build the environment
  • override (dict) - dictionary with key/values that will override our values
Returns: dict
the hook environment dictionary

CheckNodeNotDrained(lu, node_uuid)

source code 

Ensure that a given node is not drained.

Parameters:
  • lu - the LU on behalf of which we make the check
  • node_uuid - the node to check
Raises:

CheckNodeVmCapable(lu, node_uuid)

source code 

Ensure that a given node is vm capable.

Parameters:
  • lu - the LU on behalf of which we make the check
  • node_uuid - the node to check
Raises:

_StoragePathsRemoved(removed, disks)

source code 

Returns an iterable of all storage paths to be removed.

A storage path is removed if no disks are contained in it anymore.

Parameters:
  • removed (list of objects.Disk) - The disks that are being removed
  • disks (list of objects.Disk) - All disks attached to the instance
Returns: list of file paths
the storage directories that need to be removed

RemoveDisks(lu, instance, disks=None, target_node_uuid=None, ignore_failures=False)

source code 

Remove all or a subset of disks for an instance.

This abstracts away some work from `AddInstance()` and `RemoveInstance()`. Note that in case some of the devices couldn't be removed, the removal will continue with the other ones.

This function is also used by the disk template conversion mechanism to remove the old block devices of the instance. Since the instance has changed its template at the time we remove the original disks, we must specify the template of the disks we are about to remove as an argument.

Parameters:
  • lu (LogicalUnit) - the logical unit on whose behalf we execute
  • instance (objects.Instance) - the instance whose disks we should remove
  • disks (list of objects.Disk) - the disks to remove; if not specified, all the disks of the instance are removed
  • target_node_uuid (string) - used to override the node on which to remove the disks
Returns: boolean
the success of the removal

NICToTuple(lu, nic)

source code 

Build a tupple of nic information.

Parameters:
  • lu (LogicalUnit) - the logical unit on whose behalf we execute
  • nic (objects.NIC) - nic to convert to hooks tuple

NICListToTuple(lu, nics)

source code 

Build a list of nic information tuples.

This list is suitable to be passed to _BuildInstanceHookEnv or as a return value in LUInstanceQueryData.

Parameters:
  • lu (LogicalUnit) - the logical unit on whose behalf we execute
  • nics (list of objects.NIC) - list of nics to convert to hooks tuples

CopyLockList(names)

source code 

Makes a copy of a list of lock names.

Handles locking.ALL_SET correctly.

ReleaseLocks(lu, level, names=None, keep=None)

source code 

Releases locks owned by an LU.

Parameters:
  • level - Lock level
  • names (list or None) - Names of locks to release
  • keep (list or None) - Names of locks to retain
  • lu (LogicalUnit)

_ComputeIPolicyNodeViolation(ipolicy, instance, current_group, target_group, cfg, _compute_fn=ComputeIPolicyInstanceViolation)

source code 

Compute if instance meets the specs of the new target group.

Parameters:
  • ipolicy - The ipolicy to verify
  • instance - The instance object to verify
  • current_group - The current group of the instance
  • target_group - The new group of the instance
  • cfg (config.ConfigWriter) - Cluster configuration
  • _compute_fn - The function to verify ipolicy (unittest only)

CheckTargetNodeIPolicy(lu, ipolicy, instance, node, cfg, ignore=False, _compute_fn=_ComputeIPolicyNodeViolation)

source code 

Checks that the target node is correct in terms of instance policy.

Parameters:
  • ipolicy - The ipolicy to verify
  • instance - The instance object to verify
  • node - The new node to relocate
  • cfg (config.ConfigWriter) - Cluster configuration
  • ignore - Ignore violations of the ipolicy
  • _compute_fn - The function to verify ipolicy (unittest only)

CheckNodeFreeMemory(lu, node_uuid, reason, requested, hvname, hvparams)

source code 

Checks if a node has enough free memory.

This function checks if a given node has the needed amount of free memory. In case the node has less memory or we cannot get the information from the node, this function raises an OpPrereqError exception.

Parameters:
  • lu (LogicalUnit) - a logical unit from which we get configuration data
  • node_uuid (str) - the node to check
  • reason (str) - string to use in the error message
  • requested (int) - the amount of memory in MiB to check for
  • hvname (string) - the hypervisor's name
  • hvparams (dict of strings) - the hypervisor's parameters
Returns: integer
node current free memory
Raises:

UpdateMetadata(feedback_fn, rpc, instance, osparams_public=None, osparams_private=None, osparams_secret=None)

source code 

Updates instance metadata on the metadata daemon on the instance's primary node.

If the daemon isn't available (not compiled), do nothing.

In case the RPC fails, this function simply issues a warning and proceeds normally.

Parameters:
  • feedback_fn (callable) - function used send feedback back to the caller
  • rpc (rpc.node.RpcRunner) - RPC runner
  • instance (objects.Instance) - instance for which the metadata should be updated
  • osparams_public (NoneType or dict) - public OS parameters used to override those defined in instance
  • osparams_private (NoneType or dict) - private OS parameters used to override those defined in instance
  • osparams_secret (NoneType or dict) - secret OS parameters used to override those defined in instance
Returns: NoneType
None

CheckCompressionTool(lu, compression_tool)

source code 

Checks if the provided compression tool is allowed to be used.

Parameters:
  • compression_tool (string) - Compression tool to use for importing or exporting the instance
Returns: NoneType
None
Raises:

BuildDiskLogicalIDEnv(idx, disk)

source code 

Helper method to create hooks env related to disk's logical_id

Parameters:
  • idx (integer) - The index of the disk
  • disk (objects.Disk) - The disk object

BuildDiskEnv(idx, disk)

source code 

Helper method to create disk's hooks env

Parameters:
  • idx (integer) - The index of the disk
  • disk (objects.Disk or dict) - The disk object or a simple dict in case of LUInstanceCreate

CheckInstanceExistence(lu, instance_name)

source code 

Raises an error if an instance with the given name exists already.

Parameters:
  • instance_name (string) - The name of the instance.

    To be used in the locking phase.

CheckForConflictingIp(lu, ip, node_uuid)

source code 

In case of conflicting IP address raise error.

Parameters:
  • ip (string) - IP address
  • node_uuid (string) - node UUID

ComputeIPolicyInstanceSpecViolation(ipolicy, instance_spec, disk_types, _compute_fn=ComputeIPolicySpecViolation)

source code 

Compute if instance specs meets the specs of ipolicy.

Parameters:
  • ipolicy (dict) - The ipolicy to verify against
  • instance_spec - dict
  • instance_spec - The instance spec to verify
  • disk_types (list of strings) - the disk templates of the instance
  • _compute_fn - The function to verify ipolicy (unittest only)

ComputeInstanceCommunicationNIC(instance_name)

source code 

Compute the name of the instance NIC used by instance communication.

With instance communication, a new NIC is added to the instance. This NIC has a special name that identities it as being part of instance communication, and not just a normal NIC. This function generates the name of the NIC based on a prefix and the instance name

Parameters:
  • instance_name (string) - name of the instance the NIC belongs to
Returns: string
name of the NIC

PrepareContainerMods(mods, private_fn)

source code 

Prepares a list of container modifications by adding a private data field.

Parameters:
  • mods (list of tuples; (operation, index, parameters)) - List of modifications
  • private_fn (callable or None) - Callable for constructing a private data field for a modification
Returns: list

ApplyContainerMods(kind, container, chgdesc, mods, create_fn, attach_fn, modify_fn, remove_fn, detach_fn, post_add_fn=None)

source code 

Applies descriptions in mods to container.

Parameters:
  • kind (string) - One-word item description
  • container (list) - Container to modify
  • chgdesc (None or list) - List of applied changes
  • mods (list) - Modifications as returned by PrepareContainerMods
  • create_fn (callable) - Callback for creating a new item (constants.DDM_ADD); receives absolute item index, parameters and private data object as added by PrepareContainerMods, returns tuple containing new item and changes as list
  • attach_fn (callable) - Callback for attaching an existing item to a container (constants.DDM_ATTACH); receives absolute item index and item UUID or name, returns tuple containing new item and changes as list
  • modify_fn (callable) - Callback for modifying an existing item (constants.DDM_MODIFY); receives absolute item index, item, parameters and private data object as added by PrepareContainerMods, returns changes as list
  • remove_fn (callable) - Callback on removing item; receives absolute item index, item and private data object as added by PrepareContainerMods
  • detach_fn (callable) - Callback on detaching item; receives absolute item index, item and private data object as added by PrepareContainerMods
  • post_add_fn (callable) - Callable for post-processing a newly created item after it has been put into the container. It receives the index of the new item and the new item as parameters.

GetItemFromContainer(identifier, kind, container)

source code 

Return the item refered by the identifier.

Parameters:
  • identifier (string) - Item index or name or UUID
  • kind (string) - One-word item description
  • container (list) - Container to get the item from

GetIndexFromIdentifier(identifier, kind, container)

source code 

Check if the identifier represents a valid container index and return it.

Used in "add" and "attach" actions.

Parameters:
  • identifier (string) - Item index or name or UUID
  • kind (string) - Type of item, e.g. "disk", "nic"
  • container (list) - Container to calculate the index from

InsertItemToIndex(identifier, item, container)

source code 

Insert an item to the provided index of a container.

Used in "add" and "attach" actions.

Parameters:
  • identifier (string) - Item index
  • item (object) - The item to be inserted
  • container (list) - Container to insert the item to

CheckNodesPhysicalCPUs(lu, node_uuids, requested, hypervisor_specs)

source code 

Checks if nodes have enough physical CPUs

This function checks if all given nodes have the needed number of physical CPUs. In case any node has less CPUs or we cannot get the information from the node, this function raises an OpPrereqError exception.

Parameters:
  • lu (LogicalUnit) - a logical unit from which we get configuration data
  • node_uuids (list) - the list of node UUIDs to check
  • requested (int) - the minimum acceptable number of physical CPUs
  • hypervisor_specs (list of pairs (string, dict of strings)) - list of hypervisor specifications in pairs (hypervisor_name, hvparams)
Raises:

CheckHostnameSane(lu, name)

source code 

Ensures that a given hostname resolves to a 'sane' name.

The given name is required to be a prefix of the resolved hostname, to prevent accidental mismatches.

Parameters:
  • lu - the logical unit on behalf of which we're checking
  • name - the name we should resolve and check
Returns:
the resolved hostname object

CreateInstanceAllocRequest(op, disks, nics, beparams, node_name_whitelist)

source code 

Wrapper around IAReqInstanceAlloc.

Parameters:
  • op - The instance opcode
  • disks - The computed disks
  • nics - The computed nics
  • beparams - The full filled beparams
  • node_name_whitelist - List of nodes which should appear as online to the allocator (unless the node is already marked offline)
Returns:
A filled iallocator.IAReqInstanceAlloc

ComputeFullBeParams(op, cluster)

source code 

Computes the full beparams.

Parameters:
  • op - The instance opcode
  • cluster - The cluster config object
Returns:
The fully filled beparams

ComputeNics(op, cluster, default_ip, cfg, ec_id)

source code 

Computes the nics.

Parameters:
  • op - The instance opcode
  • cluster - Cluster configuration object
  • default_ip - The default ip to assign
  • cfg - An instance of the configuration object
  • ec_id - Execution context ID
Returns:
The build up nics

Variables Details [hide private]

_TApplyContModsCbChanges

Type description for changes as returned by ApplyContainerMods's callbacks

Value:
ht.TMaybeListOf(ht.TAnd(ht.TIsLength(2), ht.TItems([ht.TNonEmptyString\
, ht.TAny,])))