This Page

Psst... hey. You're reading the latest content, but it might be out of sync with code. You can read Nova 2011.1 docs or all OpenStack docs too.

The nova.utils Module

System-level utilities and helper functions.

class nova.utils.LazyPluggable(pivot, **backends)

Bases: object

A pluggable backend loaded lazily based on some value.

class nova.utils.LoopingCall(f=None, *args, **kw)

Bases: object

start(interval, now=True)
stop()
wait()
exception nova.utils.LoopingCallDone(retvalue=True)

Bases: exceptions.Exception

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()

nova.utils.abspath(s)
nova.utils.advance_time_delta(timedelta)

Advance overriden time using a datetime.timedelta.

nova.utils.advance_time_seconds(seconds)

Advance overriden time by seconds.

nova.utils.check_isinstance(obj, cls)

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

nova.utils.clear_time_override()

Remove the overridden time.

nova.utils.debug(arg)
nova.utils.default_flagfile(filename='nova.conf')
nova.utils.dumps(value)
nova.utils.execute(*cmd, **kwargs)
nova.utils.fetchfile(url, target)
nova.utils.flatten_dict(dict_, flattened=None)

Recursively flatten a nested dictionary

nova.utils.generate_mac()
nova.utils.generate_password(length=20, symbols='23456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz')

Generate a random password from the supplied symbols.

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

nova.utils.generate_uid(topic, size=8)
nova.utils.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.

nova.utils.get_my_linklocal(interface)
nova.utils.import_class(import_str)

Returns a class from a string including module and class

nova.utils.import_object(import_str)

Returns an object including a module or module and class

nova.utils.is_older_than(before, seconds)

Return True if before is older than seconds

nova.utils.isotime(at=None)

Returns iso formatted utcnow.

nova.utils.last_octet(address)
nova.utils.loads(s)
nova.utils.map_dict_keys(dict_, key_map)

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

nova.utils.novadir()
nova.utils.parse_isotime(timestr)

Turn an iso formatted time back into a datetime

nova.utils.parse_mailmap(mailmap='.mailmap')
nova.utils.partition_dict(dict_, keys)

Return two dicts, one containing only keys the other containing everything but keys

nova.utils.runthis(prompt, *cmd, **kwargs)
nova.utils.set_time_override(override_time=datetime.datetime(2011, 12, 9, 13, 49, 42, 773543))

Override utils.utcnow to return a constant time.

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

Return a dict that only contains a subset of keys

nova.utils.synchronized(name, external=False)

Synchronization decorator

Decorating a method like so: @synchronized(‘mylock’) def foo(self, *args):

...

ensures that only one thread will execute the bar method at a time.

Different methods can share the same lock: @synchronized(‘mylock’) def foo(self, *args):

...

@synchronized(‘mylock’) def bar(self, *args):

...

This way only one of either foo or bar can be executing at a time.

The external keyword argument denotes whether this lock should work across multiple processes. This means that if two different workers both run a a method decorated with @synchronized(‘mylock’, external=True), only one of them will execute at a time.

nova.utils.to_global_ipv6(prefix, mac)
nova.utils.to_mac(ipv6_address)
nova.utils.to_primitive(value)
nova.utils.utcnow()

Overridable version of datetime.datetime.utcnow.

nova.utils.utcnow_ts()

Timestamp version of our utcnow function.

nova.utils.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

nova.utils.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

nova.utils.xhtml_escape(value)

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

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