ironic.common.utils module

Utilities and helper functions.

ironic.common.utils.check_dir(directory_to_check=None, required_space=1)[source]

Check a directory is usable.

This function can be used by drivers to check that directories they need to write to are usable. This should be called from the drivers init function. This function checks that the directory exists and then calls check_dir_writable and check_dir_free_space. If directory_to_check is not provided the default is to use the temp directory.

Parameters
  • directory_to_check – the directory to check.

  • required_space – amount of space to check for in MiB.

Raises

PathNotFound if directory can not be found

Raises

DirectoryNotWritable if user is unable to write to the directory

Raises

InsufficientDiskSpace – if free space is < required space

ironic.common.utils.execute(*cmd, **kwargs)[source]

Convenience wrapper around oslo’s execute() method.

Parameters
  • cmd – Passed to processutils.execute.

  • use_standard_locale – True | False. Defaults to False. If set to True, execute command with standard locale added to environment variables.

Returns

(stdout, stderr) from process execution

Raises

UnknownArgumentError

Raises

ProcessExecutionError

ironic.common.utils.file_has_content(path, content, hash_algo='sha256')[source]

Checks that content of the file is the same as provided reference.

Parameters
  • path – path to file

  • content – reference content to check against

  • hash_algo – hashing algo from hashlib to use, default is ‘sha256’

Returns

True if the hash of reference content is the same as the hash of file’s content, False otherwise

ironic.common.utils.get_updated_capabilities(current_capabilities, new_capabilities)[source]

Returns an updated capability string.

This method updates the original (or current) capabilities with the new capabilities. The original capabilities would typically be from a node’s properties[‘capabilities’]. From new_capabilities, any new capabilities are added, and existing capabilities may have their values updated. This updated capabilities string is returned.

Parameters
  • current_capabilities – Current capability string

  • new_capabilities – the dictionary of capabilities to be updated.

Returns

An updated capability string. with new_capabilities.

Raises

ValueError, if current_capabilities is malformed or if new_capabilities is not a dictionary

ironic.common.utils.is_hostname_safe(hostname)[source]

Old check for valid logical node names.

Retained for compatibility with REST API < 1.10.

Nominally, checks that the supplied hostname conforms to:

In practice, this check has several shortcomings and errors that are more thoroughly documented in bug #1468508.

Parameters

hostname – The hostname to be validated.

Returns

True if valid. False if not.

ironic.common.utils.is_regex_string_in_file(path, string)[source]
ironic.common.utils.is_valid_datapath_id(datapath_id)[source]

Verify the format of an OpenFlow datapath_id.

Check if a datapath_id is valid and contains 16 hexadecimal digits. Datapath ID format: the lower 48-bits are for a MAC address, while the upper 16-bits are implementer-defined.

Parameters

datapath_id – OpenFlow datapath_id to be validated.

Returns

True if valid. False if not.

ironic.common.utils.is_valid_logical_name(hostname)[source]

Determine if a logical name is valid.

The logical name may only consist of RFC3986 unreserved characters, to wit:

ALPHA / DIGIT / “-” / “.” / “_” / “~”

ironic.common.utils.is_valid_no_proxy(no_proxy)[source]

Check no_proxy validity

Check if no_proxy value that will be written to environment variable by ironic-python-agent is valid.

Parameters

no_proxy – the value that requires validity check. Expected to be a comma-separated list of host names, IP addresses and domain names (with optional :port).

Returns

True if no_proxy is valid, False otherwise.

ironic.common.utils.mount(src, dest, *args)[source]

Mounts a device/image file on specified location.

Parameters
  • src – the path to the source file for mounting

  • dest – the path where it needs to be mounted.

  • args – a tuple containing the arguments to be passed to mount command.

Raises

processutils.ProcessExecutionError if it failed to run the process.

ironic.common.utils.parse_instance_info_capabilities(node)[source]

Parse the instance_info capabilities.

One way of having these capabilities set is via Nova, where the capabilities are defined in the Flavor extra_spec and passed to Ironic by the Nova Ironic driver.

NOTE: Although our API fully supports JSON fields, to maintain the backward compatibility with Juno the Nova Ironic driver is sending it as a string.

Parameters

node – a single Node.

Raises

InvalidParameterValue if the capabilities string is not a dictionary or is malformed.

Returns

A dictionary with the capabilities if found, otherwise an empty dictionary.

ironic.common.utils.pop_node_nested_field(node, collection, field, default=None)[source]

Pop a value from a dictionary field of a node.

Parameters
  • node – Node object.

  • collection – Name of the field with the dictionary.

  • field – Nested field name.

  • default – The default value to return.

Returns

The removed value or the default.

ironic.common.utils.render_template(template, params, is_file=True)[source]

Renders Jinja2 template file with given parameters.

Parameters
  • template – full path to the Jinja2 template file

  • params – dictionary with parameters to use when rendering

  • is_file – whether template is file or string with template itself

Returns

the rendered template as a string

ironic.common.utils.rmtree_without_raise(path)[source]
ironic.common.utils.safe_rstrip(value, chars=None)[source]

Removes trailing characters from a string if that does not make it empty

Parameters
  • value – A string value that will be stripped.

  • chars – Characters to remove.

Returns

Stripped value.

ironic.common.utils.set_node_nested_field(node, collection, field, value)[source]

Set a value in a dictionary field of a node.

Parameters
  • node – Node object.

  • collection – Name of the field with the dictionary.

  • field – Nested field name.

  • value – New value.

ironic.common.utils.tempdir(**kwargs)[source]
ironic.common.utils.umount(loc, *args)[source]

Umounts a mounted location.

Parameters
  • loc – the path to be unmounted.

  • args – a tuple containing the arguments to be passed to the umount command.

Raises

processutils.ProcessExecutionError if it failed to run the process.

ironic.common.utils.unix_file_modification_datetime(file_name)[source]
ironic.common.utils.validate_and_normalize_datapath_id(datapath_id)[source]

Validate an OpenFlow datapath_id and return normalized form.

Checks whether the supplied OpenFlow datapath_id is formally correct and normalize it to all lower case.

Parameters

datapath_id – OpenFlow datapath_id to be validated and normalized.

Returns

Normalized and validated OpenFlow datapath_id.

Raises

InvalidDatapathID If an OpenFlow datapath_id is not valid.

ironic.common.utils.validate_and_normalize_mac(address)[source]

Validate a MAC address and return normalized form.

Checks whether the supplied MAC address is formally correct and normalize it to all lower case.

Parameters

address – MAC address to be validated and normalized.

Returns

Normalized and validated MAC address.

Raises

InvalidMAC If the MAC address is not valid.

ironic.common.utils.validate_conductor_group(conductor_group)[source]
ironic.common.utils.validate_network_port(port, port_name='Port')[source]

Validates the given port.

Parameters
  • port – TCP/UDP port.

  • port_name – Name of the port.

Returns

An integer port number.

Raises

InvalidParameterValue, if the port is invalid.

ironic.common.utils.warn_about_deprecated_extra_vif_port_id()[source]
ironic.common.utils.wrap_ipv6(ip)[source]

Wrap the address in square brackets if it’s an IPv6 address.

ironic.common.utils.write_to_file(path, contents)[source]