The nova.utils Module

Utilities and helper functions.

class DynamicLoopingCall(f=None, *args, **kw)

Bases: nova.utils.LoopingCallBase

A looping call which happens sleeps until the next known event.

The function called should return how long to sleep for before being called again.

start(initial_delay=None, periodic_interval_max=None)
class ExceptionHelper(target)

Bases: object

Class to wrap another and translate the ClientExceptions raised by its function calls to the actual ones

class FixedIntervalLoopingCall(f=None, *args, **kw)

Bases: nova.utils.LoopingCallBase

A looping call which happens at a fixed interval.

start(interval, initial_delay=None)
class LazyPluggable(pivot, config_group=None, **backends)

Bases: object

A pluggable backend loaded lazily based on some value.

class LoopingCallBase(f=None, *args, **kw)

Bases: object

stop()
wait()
exception LoopingCallDone(retvalue=True)

Bases: exceptions.Exception

Exception to break out and stop a LoopingCall.

The poll-function passed to LoopingCall can raise this exception to break out of the loop normally. This is somewhat analogous to StopIteration.

An optional return-value can be included as the argument to the exception; this return-value will be returned by LoopingCall.wait()

class UndoManager

Bases: object

Provides a mechanism to facilitate rolling back a series of actions when an exception is raised.

rollback_and_reraise(msg=None, **kwargs)

Rollback a series of actions then re-raise the exception.

Note

(sirp) This should only be called within an exception handler.

undo_with(undo_func)
bool_from_str(val)

Convert a string representation of a bool into a bool value.

check_isinstance(obj, cls)

Checks that obj is of type cls, and lets PyLint infer types.

check_string_length(value, name, min_length=0, max_length=None)

Check the length of specified string :param value: the value of the string :param name: the name of the string :param min_length: the min_length of the string :param max_length: the max_length of the string

convert_to_list_dict(lst, label)

Convert a value or list into a list of dicts.

debug(arg)
delete_if_exists(pathname)

delete a file, but ignore file not found error.

dict_to_metadata(metadata)
diff_dict(orig, new)

Return a dict describing how to change orig to new. The keys correspond to values that have changed; the value will be a list of one or two elements. The first element of the list will be either ‘+’ or ‘-‘, indicating whether the key was updated or deleted; if the key was updated, the list will contain a second element, giving the updated value.

execute(*cmd, **kwargs)

Helper method to execute command with optional retry.

If you add a run_as_root=True command, don’t forget to add the corresponding filter to etc/nova/rootwrap.d !

Parameters:
  • cmd – Passed to subprocess.Popen.
  • process_input – Send to opened process.
  • check_exit_code – Single bool, int, or list of allowed exit codes. Defaults to [0]. Raise exception.ProcessExecutionError unless program exits with one of these code.
  • delay_on_retry – True | False. Defaults to True. If set to True, wait a short amount of time before retrying.
  • attempts – How many times to retry cmd.
  • run_as_root – True | False. Defaults to False. If set to True, the command is run with rootwrap.
Raises:
  • exception.NovaException – on receiving unknown arguments
  • exception.ProcessExecutionError
Returns:

a tuple, (stdout, stderr) from the spawned process, or None if the command fails.

file_open(*args, **kwargs)

Open file

see built-in file() documentation for more details

Note: The reason this is kept in a separate module is to easily
be able to provide a stub module that doesn’t alter system state at all (for unit tests)
flatten_dict(dict_, flattened=None)

Recursively flatten a nested dictionary.

generate_mac_address()

Generate an Ethernet MAC address.

generate_password(length=None, symbolgroups=('23456789', 'ABCDEFGHJKLMNPQRSTUVWXYZ', 'abcdefghijkmnopqrstuvwxyz'))

Generate a random password from the supplied symbol groups.

At least one symbol from each group will be included. Unpredictable results if length is less than the number of symbol groups.

Believed to be reasonably secure (with a reasonable password length!)

generate_uid(topic, size=8)
get_from_path(items, path)

Returns a list of items matching the specified path.

Takes an XPath-like expression e.g. prop1/prop2/prop3, and for each item in items, looks up items[prop1][prop2][prop3]. Like XPath, if any of the intermediate results are lists it will treat each list item individually. A ‘None’ in items or any child expressions will be ignored, this function will not throw because of None (anywhere) in items. The returned list will contain no None values.

get_ip_version(network)

Returns the IP version of a network (IPv4 or IPv6). Raises AddrFormatError if invalid network.

get_my_linklocal(interface)
get_shortened_ipv6(address)
get_shortened_ipv6_cidr(address)
get_wrapped_function(function)

Get the method at the bottom of a stack of decorators.

hash_file(file_like_object)

Generate a hash for the contents of a file.

is_int_like(val)

Check if a value looks like an int.

is_valid_boolstr(val)

Check if the provided string is a valid bool string or not.

is_valid_cidr(address)

Check if the provided ipv4 or ipv6 address is a valid CIDR address or not

is_valid_ipv4(address)

Verify that address represents a valid IPv4 address.

is_valid_ipv6(address)
is_valid_ipv6_cidr(address)
last_bytes(file_like_object, num)

Return num bytes from the end of the file, and remaining byte count.

Parameters:
  • file_like_object – The file to read
  • num – The number of bytes to return

:returns (data, remaining)

last_completed_audit_period(unit=None, before=None)

This method gives you the most recently completed audit period.

arguments:
units: string, one of ‘hour’, ‘day’, ‘month’, ‘year’
Periods normally begin at the beginning (UTC) of the period unit (So a ‘day’ period begins at midnight UTC, a ‘month’ unit on the 1st, a ‘year’ on Jan, 1) unit string may be appended with an optional offset like so: 'day@18‘ This will begin the period at 18:00 UTC. 'month@15‘ starts a monthly period on the 15th, and year@3 begins a yearly one on March 1st.
before: Give the audit period most recently completed before
<timestamp>. Defaults to now.
returns: 2 tuple of datetimes (begin, end)
The begin timestamp of this audit period is the same as the end of the previous.
last_octet(address)
make_dev_path(dev, partition=None, base='/dev')

Return a path to a particular device.

>>> make_dev_path('xvdc')
/dev/xvdc
>>> make_dev_path('xvdc', 1)
/dev/xvdc1
map_dict_keys(dict_, key_map)

Return a dict in which the dictionaries keys are mapped to new keys.

metadata_to_dict(metadata)
mkfs(fs, path, label=None)

Format a file or block device

Parameters:
  • fs – Filesystem type (examples include ‘swap’, ‘ext3’, ‘ext4’ ‘btrfs’, etc.)
  • path – Path to file or block device to format
  • label – Volume label to use
monkey_patch()

If the Flags.monkey_patch set as True, this function patches a decorator for all functions in specified modules. You can set decorators for each modules using CONF.monkey_patch_modules. The format is “Module path:Decorator function”. Example:

‘nova.api.ec2.cloud:nova.openstack.common.notifier.api.notify_decorator’

Parameters of the decorator is as follows. (See nova.openstack.common.notifier.api.notify_decorator)

name - name of the function function - object of the function

novadir()
parse_mailmap(mailmap='.mailmap')
parse_server_string(server_str)

Parses the given server_string and returns a list of host and port. If it’s not a combination of host part and port, the port element is a null string. If the input is invalid expression, return a null list.

partition_dict(dict_, keys)

Return two dicts, one with keys the other with everything else.

read_cached_file(filename, cache_info, reload_func=None)

Read from a file if it has been modified.

Parameters:
  • cache_info – dictionary to hold opaque cache.
  • reload_func – optional function to be called with data when file is reloaded due to a modification.
Returns:

data from file

read_file_as_root(file_path)

Secure helper to read file as root.

remove_path_on_error(*args, **kwds)

Protect code that wants to operate on PATH atomically. Any exception will cause PATH to be removed.

sanitize_hostname(hostname)

Return a hostname which conforms to RFC-952 and RFC-1123 specs.

ssh_execute(ssh, cmd, process_input=None, addl_env=None, check_exit_code=True)
str_dict_replace(s, mapping)
subset_dict(dict_, keys)

Return a dict that only contains a subset of keys.

tempdir(*args, **kwds)
temporary_chown(*args, **kwds)

Temporarily chown a path.

Params owner_uid:
 UID of temporary owner (defaults to current user)
temporary_mutation(*args, **kwds)

Temporarily set the attr on a particular object to a given value then revert when finished.

One use of this is to temporarily set the read_deleted flag on a context object:

with temporary_mutation(context, read_deleted=”yes”):
do_something_that_needed_deleted_objects()
timefunc(func)

Decorator that logs how long a particular function took to execute.

to_bytes(text, default=0)

Try to turn a string into a number of bytes. Looks at the last characters of the text to determine what conversion is needed to turn the input text into a byte number.

Supports: B/b, K/k, M/m, G/g, T/t (or the same with b/B on the end)

total_seconds(td)

Local total_seconds implementation for compatibility with python 2.6.

trycmd(*args, **kwargs)

A wrapper around execute() to more easily handle warnings and errors.

Returns an (out, err) tuple of strings containing the output of the command’s stdout and stderr. If ‘err’ is not empty then the command can be considered to have failed.

:discard_warnings True | False. Defaults to False. If set to True,
then for succeeding commands, stderr is cleared
utf8(value)

Try to turn a string into utf-8 if possible.

Code is directly from the utf8 function in http://github.com/facebook/tornado/blob/master/tornado/escape.py

vpn_ping(address, port, timeout=0.05, session_id=None)

Sends a vpn negotiation packet and returns the server session.

Returns False on a failure. Basic packet structure is below.

Client packet (14 bytes):

 0 1      8 9  13
+-+--------+-----+
|x| cli_id |?????|
+-+--------+-----+
x = packet identifier 0x38
cli_id = 64 bit identifier
? = unknown, probably flags/padding

Server packet (26 bytes):

 0 1      8 9  13 14    21 2225
+-+--------+-----+--------+----+
|x| srv_id |?????| cli_id |????|
+-+--------+-----+--------+----+
x = packet identifier 0x40
cli_id = 64 bit identifier
? = unknown, probably flags/padding
bit 9 was 1 and the rest were 0 in testing
walk_class_hierarchy(clazz, encountered=None)

Walk class hierarchy, yielding most derived classes first.

xhtml_escape(value)

Escapes a string so it is valid within XML or XHTML.

Previous topic

The nova.tests.xenapi.test_vm_utils Module

Next topic

The nova.version Module

This Page