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
 
_ComputeMissingKeys(key_path, options, defaults)
Helper functions to compute which keys a invalid.
source code
 
VerifyDictOptions(options, defaults)
Verify a dict has only keys set which also are in the defaults dict.
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
list of lists of int
ParseMultiCpuMask(cpu_mask)
Parse a multiple 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
 
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
 
EpochNano()
Return the current timestamp expressed as number of nanoseconds since the unix epoch
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
 
TimeoutExpired(epoch, timeout, _time_fn=time.time)
Checks whether a timeout has expired.
source code
 
ValidateDeviceNames(kind, container)
Validate instance device names.
source code
Variables [hide private]
  _VALID_SERVICE_NAME_RE = re.compile("^[-_.a-zA-Z0-9]{1,128}$")
  UUID_RE = re.compile(constants.UUID_REGEX)

Imports: JoinDisjointDicts, UniqueSequence, FindDuplicates, text, SequenceToDict, RunningTimeout, FlatToDict, itertools, NiceSortKey, InvertDict, InsertAtPos, NiceSort, FileLock, retry, LockFile, FingerprintFiles, Sha1Hmac, VerifySha1Hmac, hmac, ErrnoOrStr, KP_NEVER, RemoveDir, FileStatHelper, RemoveAuthorizedKey, shutil, VerifyFileID, BytesToMebibyte, ListVisibleFiles, TailFile, IsNormAbsPath, grp, SafeWriteFile, IsBelowDir, RemoveFile, CreateBackup, GetFileID, RenameFile, GetFilesystemStats, CanRead, FindFile, Makedirs, KP_IF_EXISTS, stat, CalculateDirectorySize, PathJoin, AddAuthorizedKey, NewUUID, ReadFile, pwd, ReadOneLineFile, EnsureDirs, KEEP_PERMS_VALUES, ReadPidFile, ReadWatcherPauseFile, KP_ALWAYS, filelock, MakeDirWithPerm, IsUserInGroup, TemporaryFileManager, WriteFile, TimestampForFilename, DaemonPidFileName, WritePidFile, EnforcePermission, ReadLockedPidFile, SetupToolLogging, SetupLogging, CheckVolumeGroupSize, LvmExclusiveTestBadPvSizes, LvmExclusiveCheckNodePvs, ctypes, Mlockall, RemoveEtcHostsEntry, AddHostToEtcHosts, SetEtcHostsEntry, RemoveHostFromEtcHosts, algo, io, WriteErrorToFD, CloseFDs, RunCmd, utils_algo, SetupDaemonFDs, DisableFork, StartDaemon, SetupDaemonEnv, compat, KillProcess, RunResult, utils_wrapper, RunInSeparateProcess, IsProcessHandlingSignal, IsProcessAlive, utils_retry, Daemonize, RunParts, resource, subprocess, StringIO, signal, SimpleRetry, RetryAgain, RETRY_REMAINING_TIME, Retry, RetryTimeout, IsFileStorageEnabled, GetLvmDiskTemplates, LookupSpaceInfoByStorageType, IsSharedFileStorageEnabled, IsDiskTemplateEnabled, GetDiskTemplatesOfStorageType, IsLvmEnabled, GetStorageUnitsOfCluster, LvmGetsEnabled, GenerateSecret, DnsNameGlobPattern, ShellWriter, FormatOrdinal, IsValidShellParam, UnescapeAndSplit, Truncate, FilterEmptyLinesAndComments, FormatKeyValue, MatchNameComponent, LineSplitter, collections, BuildShellCmd, NormalizeAndValidateThreeOctetMacPrefix, SafeEncode, ShellQuoteArgs, ParseUnit, FormatUnit, ShellQuote, FormatTime, NormalizeAndValidateMac, CommaJoin, FormatSeconds, fcntl, IsExecutable, socket, IgnoreSignals, tempfile, ResetTempfileModule, SetNonblockFlag, sys, SetCloseOnExecFlag, RetryOnSignal, GetClosedTempfile, TestDelay, IgnoreProcessNotFound, os, select, CloseFdNoError, datetime, LoadSignedX509Certificate, calendar, X509_SIGNATURE, errors, ExtractX509Certificate, errno, OpenSSL, re, SignX509Certificate, time, HEX_CHAR_RE, PrepareX509CertKeyCheck, GenerateSelfSignedX509Cert, VALID_X509_SIGNATURE_SALT, utils_hash, GetX509CertValidity, GenerateSelfSignedSslCert, utils_io, constants, logging, utils_text, VerifyX509Certificate, pathutils, CheckNodeCertificate


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

_ComputeMissingKeys(key_path, options, defaults)

source code 

Helper functions to compute which keys a invalid.

Parameters:
  • key_path - The current key path (if any)
  • options - The user provided options
  • defaults - The default dictionary
Returns:
A list of invalid keys

VerifyDictOptions(options, defaults)

source code 

Verify a dict has only keys set which also are in the defaults dict.

Parameters:
  • options - The user provided options
  • defaults - The default dictionary
Raises:

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

ParseMultiCpuMask(cpu_mask)

source code 

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

CPU mask format: colon-separated list of comma-separated list of CPU IDs or dash-separated ID ranges, with optional "all" as CPU value Example: "0-2,5:all:1,5,6:2" -> [ [ 0,1,2,5 ], [ -1 ], [ 1, 5, 6 ], [ 2 ] ]

Parameters:
  • cpu_mask (str) - multiple CPU mask definition
Returns: list of lists of int
list of lists 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

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

EpochNano()

source code 

Return the current timestamp expressed as number of nanoseconds since the unix epoch

Returns:
nanoseconds since the Unix epoch

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

ValidateDeviceNames(kind, container)

source code 

Validate instance device names.

Check that a device container contains only unique and valid names.

Parameters:
  • kind (string) - One-word item description
  • container (list) - Container containing the devices