ironic_python_agent.utils module

class ironic_python_agent.utils.AccumulatedFailures(exc_class=<class 'RuntimeError'>)[source]

Bases: object

Object to accumulate failures without raising exception.

add(fail, *fmt)[source]

Add failure with optional formatting.

Parameters:
  • fail – exception or error string

  • fmt – formatting arguments (only if fail is a string)

get_error()[source]

Get error string or None.

raise_if_needed()[source]

Raise exception if error list is not empty.

Raises:

RuntimeError

class ironic_python_agent.utils.StreamingClient(verify_ca=True)[source]

Bases: object

A wrapper around HTTP client with TLS, streaming and error handling.

ironic_python_agent.utils.collect_system_logs(journald_max_lines=None)[source]

Collect system logs.

Collect system logs, for distributions using systemd the logs will come from journald. On other distributions the logs will come from the /var/log directory and dmesg output.

Parameters:

journald_max_lines – Maximum number of lines to retrieve from the journald. if None, return everything.

Returns:

A tar, gzip base64 encoded string with the logs.

ironic_python_agent.utils.copy_config_from_vmedia()[source]

Copies any configuration from a virtual media device.

Copies files under /etc/ironic-python-agent and /etc/ironic-python-agent.d.

ironic_python_agent.utils.create_partition_table(dev_name, partition_table_type)[source]

Create a partition table on a disk using parted.

Parameters:
  • dev_name – the disk where we want to create the partition table.

  • partition_table_type – the type of partition table we want to create, for example gpt or msdos.

Raises:

CommandExecutionError if an error is encountered while attempting to create the partition table.

ironic_python_agent.utils.determine_time_method()[source]

Helper method to determine what time utility is present.

Returns:

“ntpdate” if ntpdate has been found, “chrony” if chrony was located, and None if neither are located. If both tools are present, “chrony” will supersede “ntpdate”.

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

Convenience wrapper around ironic_lib’s execute() method.

Executes and logs results from a system command.

ironic_python_agent.utils.extract_device(part)[source]

Extract the device from a partition name or path.

Parameters:

part – the partition

Returns:

a device if success, None otherwise

ironic_python_agent.utils.find_in_lshw(lshw, by_id=None, by_class=None, recursive=False, **fields)[source]

Yield all suitable records from lshw.

ironic_python_agent.utils.get_agent_params()[source]

Gets parameters passed to the agent via kernel cmdline or vmedia.

Parameters can be passed using either the kernel commandline or through virtual media. If boot_method is vmedia, merge params provided via vmedia with those read from the kernel command line.

Although it should never happen, if a variable is both set by vmedia and kernel command line, the setting in vmedia will take precedence.

Returns:

a dict of potential configuration parameters for the agent

ironic_python_agent.utils.get_command_output(command)[source]

Return the output of a given command.

Parameters:

command – The command to be executed.

Raises:

CommandExecutionError if the execution of the command fails.

Returns:

A BytesIO string with the output.

ironic_python_agent.utils.get_journalctl_output(lines=None, units=None)[source]

Query the contents of the systemd journal.

Parameters:
  • lines – Maximum number of lines to retrieve from the logs. If None, return everything.

  • units – A list with the names of the units we should retrieve the logs from. If None retrieve the logs for everything.

Returns:

A log string.

ironic_python_agent.utils.get_node_boot_mode(node)[source]

Returns the node boot mode.

It returns ‘uefi’ if ‘secure_boot’ is set to ‘true’ in ‘instance_info/capabilities’ of node. Otherwise it directly look for boot mode hints into

Parameters:

node – dictionary.

Returns:

‘bios’ or ‘uefi’

ironic_python_agent.utils.get_partition_table_type_from_specs(node)[source]

Returns the node partition label, gpt or msdos.

If boot mode is uefi, return gpt. Else, choice is open, look for disk_label capabilities (instance_info has priority over properties).

Parameters:

node

Returns:

gpt or msdos

ironic_python_agent.utils.get_ssl_client_options(conf)[source]

Format SSL-related requests options.

Parameters:

conf – oslo_config CONF object

Returns:

tuple of ‘verify’ and ‘cert’ values to pass to requests

ironic_python_agent.utils.guess_root_disk(block_devices, min_size_required=4294967296)[source]

Find suitable disk provided that root device hints are not given.

If no hints are passed, order the devices by size (primary key) and name (secondary key), and return the first device larger than min_size_required as the root disk.

ironic_python_agent.utils.gzip_and_b64encode(io_dict=None, file_list=None)[source]

Gzip and base64 encode files and BytesIO buffers.

Parameters:
  • io_dict – A dictionary containing whose the keys are the file names and the value a BytesIO object.

  • file_list – A list of file path.

Returns:

A gzipped and base64 encoded string.

ironic_python_agent.utils.is_char_device(path)[source]

Check if the specified path is a character device.

ironic_python_agent.utils.is_journalctl_present()[source]

Check if the journalctl command is present.

Returns:

True if journalctl is present, False if not.

ironic_python_agent.utils.log_early_log_to_logger()[source]

Logs early logging events to the configured logger.

ironic_python_agent.utils.mounted(source, dest=None, opts=None, fs_type=None, mount_attempts=1, umount_attempts=3)[source]

A context manager for a temporary mount.

Parameters:
  • source – A device to mount.

  • dest – Mount destination. If not specified, a temporary directory will be created and removed afterwards. An existing destination is not removed.

  • opts – Mount options (-o argument).

  • fs_type – File system type (-t argument).

  • mount_attempts – A number of attempts to mount the device.

  • umount_attempts – A number of attempts to unmount the device.

Returns:

A generator yielding the destination.

ironic_python_agent.utils.parse_capabilities(root)[source]

Extract capabilities from provided root dictionary-behaving object.

root.get(‘capabilities’, {}) value can either be a dict, or a json str, or a key1:value1,key2:value2 formatted string.

Parameters:

root – Anything behaving like a dict and containing capabilities formatted as expected. Can be node.get(‘properties’, {}), node.get(‘instance_info’, {}).

Returns:

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

ironic_python_agent.utils.remove_large_keys(var)[source]

Remove specific keys from the var, recursing into dicts and lists.

ironic_python_agent.utils.rescan_device(device)[source]

Force the device to be rescanned

Parameters:

device – device upon which to rescan and update kernel partition records.

ironic_python_agent.utils.split_device_and_partition_number(part)[source]

Extract the partition number from a partition name or path.

Parameters:

part – the partition

Returns:

device and partition number if success, None otherwise

ironic_python_agent.utils.sync_clock(ignore_errors=False)[source]

Syncs the software clock of the system.

This method syncs the system software clock if a NTP server was defined in the “[DEFAULT]ntp_server” configuration parameter. This method does NOT attempt to sync the hardware clock.

It will try to use either ntpdate or chrony to sync the software clock of the system. If neither is found, an exception is raised.

Parameters:

ignore_errors – Boolean value default False that allows for the method to be called and ultimately not raise an exception. This may be useful for opportunistically attempting to sync the system software clock.

Raises:

CommandExecutionError if an error is encountered while attempting to sync the software clock.

ironic_python_agent.utils.try_collect_command_output(io_dict, file_name, command)[source]