Package ganeti :: Package utils
[hide private]
[frames] | no frames]

Package utils

source code

Ganeti utility module.

This module holds functions that can be used in both daemons (all) and the command line scripts.

Submodules [hide private]

Classes [hide private]
  FdConditionWaiterHelper
Retry helper for WaitForFdCondition.
  SignalWakeupFd
  SignalHandler
Generic signal handler class.
  FieldSet
A simple field set.
Functions [hide private]
 
ForceDictType(target, key_types, allowed_values=None)
Force the values of a dict to have certain types.
source code
 
ValidateServiceName(name)
Validate the given service name.
source code
dict
ListVolumeGroups()
List volume groups and their size
source code
boolean
BridgeExists(bridge)
Check whether the given bridge exists in the system
source code
 
TryConvert(fn, val)
Try to convert a value ignoring errors.
source code
list of int
ParseCpuMask(cpu_mask)
Parse a CPU mask definition and return the list of CPU IDs.
source code
 
GetHomeDir(user, default=None)
Try to get the homedir of the given user.
source code
int
FirstFree(seq, base=0)
Returns the first non-existing integer from seq.
source code
int or None
SingleWaitForFdCondition(fdobj, event, timeout)
Waits for a condition to occur on the socket.
source code
int or None
WaitForFdCondition(fdobj, event, timeout)
Waits for a condition to occur on the socket.
source code
 
EnsureDaemon(name)
Check for and start daemon if not alive.
source code
 
StopDaemon(name)
Stop daemon
source code
None or str
CheckVolumeGroupSize(vglist, vgname, minsize)
Checks if the volume group list is valid.
source code
 
SplitTime(value)
Splits time as floating point number into a tuple.
source code
 
MergeTime(timetuple)
Merges a tuple into time as a floating point number.
source code
tuple; (value in dictionary, matched groups as list)
FindMatch(data, name)
Tries to find an item in a dictionary matching a name.
source code
list of tuples
GetMounts(filename=constants.PROC_MOUNTS)
Returns the list of mounted filesystems.
source code
 
SignalHandled(signums)
Signal Handled decoration.
source code
Variables [hide private]
  _VALID_SERVICE_NAME_RE = re.compile("^[-_.a-zA-Z0-9]{1,128}$")
  UUID_RE = re.compile("^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-" "...

Imports: os, re, errno, pwd, itertools, select, logging, signal, errors, constants, compat


Function Details [hide private]

ForceDictType(target, key_types, allowed_values=None)

source code 

Force the values of a dict to have certain types.

Parameters:
  • target (dict) - the dict to update
  • key_types (dict) - dict mapping target dict keys to types in constants.ENFORCEABLE_TYPES
  • allowed_values (list) - list of specially allowed values

ValidateServiceName(name)

source code 

Validate the given service name.

Parameters:
  • name (number or string) - Service name or port specification

ListVolumeGroups()

source code 

List volume groups and their size

Returns: dict
Dictionary with keys volume name and values the size of the volume

BridgeExists(bridge)

source code 

Check whether the given bridge exists in the system

Parameters:
  • bridge (str) - the bridge name to check
Returns: boolean
True if it does

TryConvert(fn, val)

source code 

Try to convert a value ignoring errors.

This function tries to apply function fn to val. If no ValueError or TypeError exceptions are raised, it will return the result, else it will return the original value. Any other exceptions are propagated to the caller.

Parameters:
  • fn (callable) - function to apply to the value
  • val - the value to be converted
Returns:
The converted value if the conversion was successful, otherwise the original value.

ParseCpuMask(cpu_mask)

source code 

Parse a CPU mask definition and return the list of CPU IDs.

CPU mask format: comma-separated list of CPU IDs or dash-separated ID ranges Example: "0-2,5" -> "0,1,2,5"

Parameters:
  • cpu_mask (str) - CPU mask definition
Returns: list of int
list of CPU IDs

GetHomeDir(user, default=None)

source code 

Try to get the homedir of the given user.

The user can be passed either as a string (denoting the name) or as an integer (denoting the user id). If the user is not found, the 'default' argument is returned, which defaults to None.

FirstFree(seq, base=0)

source code 

Returns the first non-existing integer from seq.

The seq argument should be a sorted list of positive integers. The first time the index of an element is smaller than the element value, the index will be returned.

The base argument is used to start at a different offset, i.e. [3, 4, 6] with offset=3 will return 5.

Example: [0, 1, 3] will return 2.

Parameters:
  • seq (sequence) - the sequence to be analyzed.
  • base (int) - use this value as the base index of the sequence
Returns: int
the first non-used index in the sequence

SingleWaitForFdCondition(fdobj, event, timeout)

source code 

Waits for a condition to occur on the socket.

Immediately returns at the first interruption.

Parameters:
  • fdobj (integer or object supporting a fileno() method) - entity to wait for events on
  • event (integer) - ORed condition (see select module)
  • timeout (float or None) - Timeout in seconds
Returns: int or None
None for timeout, otherwise occured conditions

WaitForFdCondition(fdobj, event, timeout)

source code 

Waits for a condition to occur on the socket.

Retries until the timeout is expired, even if interrupted.

Parameters:
  • fdobj (integer or object supporting a fileno() method) - entity to wait for events on
  • event (integer) - ORed condition (see select module)
  • timeout (float or None) - Timeout in seconds
Returns: int or None
None for timeout, otherwise occured conditions

CheckVolumeGroupSize(vglist, vgname, minsize)

source code 

Checks if the volume group list is valid.

The function will check if a given volume group is in the list of volume groups and has a minimum size.

Parameters:
  • vglist (dict) - dictionary of volume group names and their size
  • vgname (str) - the volume group we should check
  • minsize (int) - the minimum size we accept
Returns: None or str
None for success, otherwise the error message

SplitTime(value)

source code 

Splits time as floating point number into a tuple.

Parameters:
  • value (int or float) - Time in seconds
Returns:
Tuple containing (seconds, microseconds)

MergeTime(timetuple)

source code 

Merges a tuple into time as a floating point number.

Parameters:
  • timetuple (tuple) - Time as tuple, (seconds, microseconds)
Returns:
Time as a floating point number expressed in seconds

FindMatch(data, name)

source code 

Tries to find an item in a dictionary matching a name.

Callers have to ensure the data names aren't contradictory (e.g. a regexp that matches a string). If the name isn't a direct key, all regular expression objects in the dictionary are matched against it.

Parameters:
  • data (dict) - Dictionary containing data
  • name (string) - Name to look for
Returns: tuple; (value in dictionary, matched groups as list)

GetMounts(filename=constants.PROC_MOUNTS)

source code 

Returns the list of mounted filesystems.

This function is Linux-specific.

Parameters:
  • filename - path of mounts file (/proc/mounts by default)
Returns: list of tuples
list of mount entries (device, mountpoint, fstype, options)

SignalHandled(signums)

source code 

Signal Handled decoration.

This special decorator installs a signal handler and then calls the target function. The function must accept a 'signal_handlers' keyword argument, which will contain a dict indexed by signal number, with SignalHandler objects as values.

The decorator can be safely stacked with iself, to handle multiple signals with different handlers.

Parameters:
  • signums (list) - signals to intercept

Variables Details [hide private]

UUID_RE

Value:
re.compile("^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-" "[a-f0-9]{4}-[a-f0-\
9]{12}$")