Utilities and helper functions.
Bases: lockfile.LinkFileLock
Implementation of lockfile that allows for a lock per greenthread.
Simply implements lockfile:LockBase init with an addiontall suffix on the unique name of the greenthread identifier
Bases: object
A pluggable backend loaded lazily based on some value.
Bases: object
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()
Bases: object
Provides a mechanism to facilitate rolling back a series of actions when an exception is raised.
Rollback a series of actions then re-raise the exception.
Note
(sirp) This should only be called within an exception handler.
Advance overriden time using a datetime.timedelta.
Advance overriden time by seconds.
Convert a string representation of a bool into a bool value
Checks that obj is of type cls, and lets PyLint infer types.
clean up stale locks left behind by process failures
The lockfile module, used by @synchronized, can leave stale lockfiles behind after process failure. These locks can cause process hangs at startup, when a process deadlocks on a lock which will never be unlocked.
Intended to be called at service startup.
Remove the overridden time.
Convert a value or list into a list of dicts
delete a file, but ignore file not found error
Marks a function, class, or method as being deprecated. For functions and methods, emits a warning each time the function or method is called. For classes, generates a new subclass which will emit a warning each time the class is instantiated, or each time any class or static method is called.
If a message is passed to the decorator, that message will be appended to the emitted warning. This may be used to suggest an alternate way of achieving the desired effect, or to explain why the function, class, or method is deprecated.
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 nova.rootwrap !
Parameters: |
|
---|---|
Raises: |
|
Returns: | a tuple, (stdout, stderr) from the spawned process, or None if the command fails. |
Recursively flatten a nested dictionary.
Generate the URL to glance.
Generate an image URL from an image_ref.
Generate an Ethernet MAC address.
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!)
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.
Generate a hash for the contents of a file.
Return True if before is older than seconds.
For our purposes, a UUID is a string in canonical form:
aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa
Check if the provided ipv4 or ipv6 address is a valid CIDR address or not
valid the address strictly as per format xxx.xxx.xxx.xxx. where xxx is a value between 0 and 255.
Stringify time in ISO 8601 format
This method gives you the most recently completed audit period.
Catches exception, write message to the log, re-raise. This is a common refinement of save_and_reraise that writes a specific message to the log.
Return a path to a particular device.
>>> make_dev_path('xvdc')
/dev/xvdc
>>> make_dev_path('xvdc', 1)
/dev/xvdc1
Return a dict in which the dictionaries keys are mapped to new keys.
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 FLAGS.monkey_patch_modules. The format is “Module path:Decorator function”. Example: ‘nova.api.ec2.cloud:nova.notifier.api.notify_decorator’
Parameters of the decorator is as follows. (See nova.notifier.api.notify_decorator)
name - name of the function function - object of the function
Normalize time in arbitrary timezone to UTC
Turn an iso formatted time back into a datetime.
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.
Turn a formatted time back into a datetime.
Return two dicts, one with keys the other with everything else.
Read from a file if it has been modified.
Parameters: |
|
---|---|
Returns: | data from file |
Secure helper to read file as root.
Protect code that wants to operate on PATH atomically. Any exception will cause PATH to be removed.
Return a hostname which conforms to RFC-952 and RFC-1123 specs.
Check whether a service is up based on last heartbeat.
Override utils.utcnow to return a constant time.
Constant-time string comparison.
Params s1: | the first string |
---|---|
Params s2: | the second string |
Returns: | True if the strings are equal. |
This function takes two strings and compares them. It is intended to be used when doing a comparison for authentication purposes to help guard against timing attacks.
Returns formatted utcnow.
Return a dict that only contains a subset of keys.
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.
Important limitation: you can only have one external lock running per thread at a time. For example the following will fail:
@utils.synchronized(‘testlock1’, external=True) def outer_lock():
@utils.synchronized(‘testlock2’, external=True) def inner_lock():
passinner_lock()
outer_lock()
Temporarily chown a path.
Params owner_uid: | |
---|---|
UID of temporary owner (defaults to current user) |
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()
Decorator that logs how long a particular function took to execute
Local total_seconds implementation for compatibility with python 2.6
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.
Overridable version of utils.utcnow.
Timestamp version of our utcnow function.
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
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
Issues a warning to indicate that the given class is deprecated. If a message is given, it is appended to the deprecation warning.
Issues a warning to indicate that the given function is deprecated. If a message is given, it is appended to the deprecation warning.
Escapes a string so it is valid within XML or XHTML.