glance.common.utils module

System-level utilities and helper functions.

class glance.common.utils.CooperativeReader(fd)[source]

Bases: object

An eventlet thread friendly class for reading in image data.

When accessing data either through the iterator or the read method we perform a sleep to allow a co-operative yield. When there is more than one image being uploaded/downloaded this prevents eventlet thread starvation, ie allows all threads to be scheduled periodically rather than having the same thread be continuously active.

read(length=None)[source]

Return the requested amount of bytes, fetching the next chunk of the underlying iterator when needed.

This is replaced with cooperative_read in __init__ if the underlying fd already supports read().

class glance.common.utils.LimitingReader(data, limit, exception_class=<class 'glance.common.exception.ImageSizeLimitExceeded'>)[source]

Bases: object

Reader designed to fail when reading image data past the configured allowable amount.

read(i)[source]
glance.common.utils.chunkiter(fp, chunk_size=65536)[source]

Return an iterator to a file-like obj which yields fixed size chunks

Parameters:
  • fp – a file-like object

  • chunk_size – maximum size of chunk

glance.common.utils.chunkreadable(iter, chunk_size=65536)[source]

Wrap a readable iterator with a reader yielding chunks of a preferred size, otherwise leave iterator unchanged.

Parameters:
  • iter – an iter which may also be readable

  • chunk_size – maximum size of chunk

glance.common.utils.cooperative_iter(iter)[source]

Return an iterator which schedules after each iteration. This can prevent eventlet thread starvation.

Parameters:

iter – an iterator to wrap

glance.common.utils.cooperative_read(fd)[source]

Wrap a file descriptor’s read with a partial function which schedules after each read. This can prevent eventlet thread starvation.

Parameters:

fd – a file descriptor to wrap

glance.common.utils.create_mashup_dict(image_meta)[source]

Returns a dictionary-like mashup of the image core properties and the image custom properties from given image metadata.

Parameters:

image_meta – metadata of image with core and custom properties

glance.common.utils.evaluate_filter_op(value, operator, threshold)[source]

Evaluate a comparison operator. Designed for use on a comparative-filtering query field.

Parameters:
  • value – evaluated against the operator, as left side of expression

  • operator – any supported filter operation

  • threshold – to compare value against, as right side of expression

Raises:

InvalidFilterOperatorValue – if an unknown operator is provided

Returns:

boolean result of applied comparison

glance.common.utils.get_image_meta_from_headers(response)[source]

Processes HTTP headers from a supplied response that match the x-image-meta and x-image-meta-property and returns a mapping of image metadata and properties

Parameters:

response – Response to process

glance.common.utils.get_stores_from_request(req, body)[source]

Processes a supplied request and extract stores from it

Parameters:
  • req – request to process

  • body – request body

Raises:

glance_store.UnknownScheme – if a store is not valid

Returns:

a list of stores

glance.common.utils.get_test_suite_socket()[source]
glance.common.utils.image_meta_to_http_headers(image_meta)[source]

Returns a set of image metadata into a dict of HTTP headers that can be fed to either a Webob Request object or an httplib.HTTP(S)Connection object

Parameters:

image_meta – Mapping of image metadata

glance.common.utils.is_http_store_configured(url)[source]
glance.common.utils.is_valid_fqdn(fqdn)[source]

Verify whether a host is a valid FQDN.

glance.common.utils.is_valid_hostname(hostname)[source]

Verify whether a hostname (not an FQDN) is valid.

glance.common.utils.mutating(func)[source]

Decorator to enforce read-only logic

glance.common.utils.no_4byte_params(f)[source]

Checks that no 4 byte unicode characters are allowed in dicts’ keys/values and string’s parameters

glance.common.utils.normalize_hostname(host)[source]

Normalize IP address to standard format or return hostname.

Uses ipaddress module to validate and normalize IP addresses, rejecting encoded formats. For hostnames, requires DNS resolution to ensure they are valid and not encoded IP attempts.

Parameters:

host – hostname or IP address

Returns:

normalized IP address, hostname unchanged, or None

glance.common.utils.parse_valid_host_port(host_port)[source]

Given a “host:port” string, attempts to parse it as intelligently as possible to determine if it is valid. This includes IPv6 [host]:port form, IPv4 ip:port form, and hostname:port or fqdn:port form.

Invalid inputs will raise a ValueError, while valid inputs will return a (host, port) tuple where the port will always be of type int.

glance.common.utils.safe_mkdirs(path)[source]
glance.common.utils.setup_remote_pydev_debug(host, port)[source]
glance.common.utils.sort_image_locations(locations)[source]
glance.common.utils.split_filter_op(expression)[source]

Split operator from threshold in an expression. Designed for use on a comparative-filtering query field. When no operator is found, default to an equality comparison.

Parameters:

expression – the expression to parse

Returns:

a tuple (operator, threshold) parsed from expression

glance.common.utils.split_filter_value_for_quotes(value)[source]

Split filter values

Split values by commas and quotes for ‘in’ operator, according api-wg.

glance.common.utils.stash_conf_values()[source]

Make a copy of some of the current global CONF’s settings. Allows determining if any of these values have changed when the config is reloaded.

glance.common.utils.validate_import_uri(uri)[source]

Validate requested uri for Image Import web-download.

Parameters:

uri – target uri to be validated

glance.common.utils.validate_quotes(value)[source]

Validate filter values

Validation opening/closing quotes in the expression.