COT.helper_tools module

Third-party helper tools.

Abstracts away operations that require third-party helper programs, especially those that are not available through PyPI.

Exceptions

HelperNotFoundError A helper program cannot be located.
HelperError A helper program exited with non-zero return code.

Functions

check_call Wrapper for subprocess.check_call().
check_output Wrapper for subprocess.check_output().
convert_disk_image Convert the given disk image to the requested format/subformat.
create_disk_image Create a new disk image at the requested location.
get_checksum Get the checksum of the given file.
get_disk_capacity Get the storage capacity of the given disk image.
get_disk_format Get the disk image format of the given file.
get_ovftool_version Get installed ovftool version as a StrictVersion object.
get_qemu_img_version Get installed qemu-img version as a StrictVersion object.
validate_ovf_for_esxi Use VMware’s ovftool program to validate an OVF or OVA.
exception HelperError[source]

Bases: exceptions.EnvironmentError

A helper program exited with non-zero return code.

exception HelperNotFoundError[source]

Bases: exceptions.OSError

A helper program cannot be located.

check_call(args, require_success=True)[source]

Wrapper for subprocess.check_call().

Unlike check_output() below, this does not redirect stdout/stderr; all output from the subprocess will be sent to stdout/stderr as normal.

Parameters:
  • args (list) – Command to invoke and its associated args
  • require_success (boolean) – If False, do not raise an error when the command exits with a return code other than 0
Raises:
  • HelperNotFoundError – if the command doesn’t exist (instead of a OSError)
  • HelperError – if the command returns a value other than 0 and require_success is not False
check_output(args, require_success=True)[source]

Wrapper for subprocess.check_output().

Automatically redirects stderr to stdout, captures both to a buffer, and generates a debug message with the stdout contents.

Parameters:
  • args (list) – Command to invoke and its associated args
  • require_success (boolean) – If False, do not raise an error when the command exits with a return code other than 0
Returns:

Captured stdout/stderr from the command

Raises:
  • HelperNotFoundError – if the command doesn’t exist (instead of a OSError)
  • HelperError – if the command returns a value other than 0 and require_success is not False
convert_disk_image(file_path, output_dir, new_format, new_subformat=None)[source]

Convert the given disk image to the requested format/subformat.

If the disk is already in this format then it is unchanged; otherwise, will convert to a new disk in the specified output_dir and return its path.

Current supported conversions:

  • .vmdk (any format) to .vmdk (streamOptimized)
  • .img to .vmdk (streamOptimized)
Parameters:
  • file_path (str) – Disk image file to inspect/convert
  • output_dir (str) – Directory to place converted image into, if needed
  • new_format (str) – Desired final format
  • new_subformat (str) – Desired final subformat
Returns:

  • file_path, if no conversion was required
  • or a file path in output_dir containing the converted image

Raises ValueUnsupportedError:
 

if the new_format and/or new_subformat are not supported conversion targets.

create_disk_image(file_path, file_format=None, capacity=None, contents=[])[source]

Create a new disk image at the requested location.

Either capacity or contents or both must be specified.

Parameters:
  • file_path (str) – Desired location of new disk image
  • file_format (str) – Desired image format (if not specified, this will be derived from the file extension of file_path)
  • capacity – TODO what’s the expected format?
  • contents (list) – List of file paths to package into the created image. If not specified, the image will be left blank and unformatted.
get_checksum(file_path, checksum_type)[source]

Get the checksum of the given file.

Parameters:
  • file_path (str) – Path to file to checksum
  • checksum_type (str) – Supported values are ‘md5’ and ‘sha1’.
Returns:

String containing hexadecimal file checksum

get_disk_capacity(file_path)[source]

Get the storage capacity of the given disk image.

Parameters:file_path (str) – Path to disk image file to inspect
Returns:Disk capacity, in bytes
get_disk_format(file_path)[source]

Get the disk image format of the given file.

Warning

If file_path refers to a file which is not a disk image at all, this function will return ('raw', None).

Parameters:file_path (str) – Path to disk image file to inspect.
Returns:(format, subformat)
  • format may be 'vmdk', 'raw', or 'qcow2'
  • subformat may be None, or various strings for 'vmdk' files.
get_ovftool_version()[source]

Get installed ovftool version as a StrictVersion object.

Returns:StrictVersion instance
Raises HelperNotFoundError:
 if ovftool is not found.
get_qemu_img_version()[source]

Get installed qemu-img version as a StrictVersion object.

Returns:StrictVersion instance
Raises HelperNotFoundError:
 if qemu-img is not found.
validate_ovf_for_esxi(ovf_file)[source]

Use VMware’s ovftool program to validate an OVF or OVA.

This checks the file against the OVF standard and any VMware-specific requirements.

Parameters:

ovf_file (str) – File to validate

Returns:

Output from ovftool

Raises:
  • HelperNotFoundError – if ovftool is not found.
  • HelperError – if ovftool regards the file as invalid