validations_libs.utils module

validations_libs.utils.check_community_validations_dir(basedir=PosixPath('/home/zuul/src/opendev.org/openstack/validations-libs/.tox/docs/community-validations'), subdirs=[PosixPath('/home/zuul/src/opendev.org/openstack/validations-libs/.tox/docs/community-validations/roles'), PosixPath('/home/zuul/src/opendev.org/openstack/validations-libs/.tox/docs/community-validations/playbooks'), PosixPath('/home/zuul/src/opendev.org/openstack/validations-libs/.tox/docs/community-validations/library'), PosixPath('/home/zuul/src/opendev.org/openstack/validations-libs/.tox/docs/community-validations/lookup_plugins')])[source]

Check presence of the community validations directory structure

The community validations are stored and located in:

/home/<username>/community-validations
├── library
├── lookup_plugins
├── playbooks
└── roles

This function checks for the presence of the community-validations directory in the $HOME of the user running the validation CLI. If the primary directory doesn’t exist, this function will create it and will check if the four subdirectories are present and will create them otherwise.

Parameters:
  • basedir (pathlib.PosixPath) – Absolute path of the community validations

  • subdirs (list of pathlib.PosixPath) – List of Absolute path of the community validations subdirs

Return type:

NoneType

validations_libs.utils.community_validations_on(validation_config)[source]

Check for flag for community validations to be enabled The default value is true

Parameters:

validation_config (dict) – A dictionary of configuration for Validation loaded from an validation.cfg file.

Returns:

A boolean with the status of community validations flag

Return type:

bool

validations_libs.utils.create_artifacts_dir(log_path='/home/zuul/src/opendev.org/openstack/validations-libs/.tox/docs/validations', prefix='')[source]

Create Ansible artifacts directory for the validation run :param log_path: Directory asbolute path :type log_path: string :param prefix: Playbook name :type prefix: string :return: UUID of the validation run, absolute path of the validation artifacts directory :rtype: string, string

validations_libs.utils.create_log_dir(log_path='/home/zuul/src/opendev.org/openstack/validations-libs/.tox/docs/validations')[source]

Check for presence of the selected validations log dir. Create the directory if needed, and use fallback if that proves too tall an order.

Log the failure if encountering OSError or PermissionError.

Parameters:

log_path (string) – path of the selected log directory

Returns:

valid path to the log directory

Return type:

string

Raises:

RuntimeError if even the fallback proves unavailable.

validations_libs.utils.current_time()[source]

Return current time

validations_libs.utils.find_config_file(config_file_name='validation.cfg')[source]

Find the config file for Validation in the following order: * environment validation VALIDATION_CONFIG * current user directory * user home directory * Python prefix path which has been used for the installation * /etc/validation.cfg

validations_libs.utils.get_validation_group_name_list(groups_path=None)[source]

Get the validation group name list only

Params groups_path:

The path the groups.yaml file

Returns:

The group name list

Return type:

list

Example:

>>> get_validation_group_name_list()
['group1',
 'group2',
 'group3',
 'group4']
validations_libs.utils.get_validation_parameters(validation)[source]

Return dictionary of parameters

validations_libs.utils.get_validations_data(validation, path='/usr/share/ansible/validation-playbooks', validation_config=None)[source]

Return validation data with format:

ID, Name, Description, Groups, Parameters

Parameters:
  • validation (string) – Name of the validation without the yaml extension. Defaults to constants.ANSIBLE_VALIDATION_DIR

  • path (string) – The path to the validations directory

  • validation_config (dict) – A dictionary of configuration for Validation loaded from an validation.cfg file.

Returns:

The validation data with the format (ID, Name, Description, Groups, Parameters)

Return type:

dict

Example:

>>> validation = 'check-something'
>>> get_validations_data(validation)
{'Description': 'Verify that the server has enough something',
 'Groups': ['group1', 'group2'],
 'Categories': ['category1', 'category2'],
 'products': ['product1', 'product2'],
 'ID': 'check-something',
 'Name': 'Verify the server fits the something requirements',
 'Parameters': {'param1': 24}}
validations_libs.utils.get_validations_parameters(validations_data, validation_name=None, groups=None, categories=None, products=None)[source]

Return parameters for a list of validations

Parameters:
  • validations_data (list) – A list of absolute validations playbooks path

  • validation_name (list) – A list of validation name

  • groups (list) – A list of validation groups

  • categories (list) – A list of validation categories

  • products (list) – A list of validation products

Returns:

a dictionary containing the current parameters for each validation_name or groups

Return type:

dict

Example:

>>> validations_data = ['/foo/bar/check-ram.yaml',
                        '/foo/bar/check-cpu.yaml']
>>> validation_name = ['check-ram', 'check-cpu']
>>> get_validations_parameters(validations_data, validation_name)
{'check-cpu': {'parameters': {'minimal_cpu_count': 8}},
 'check-ram': {'parameters': {'minimal_ram_gb': 24}}}
validations_libs.utils.get_validations_playbook(path, validation_id=None, groups=None, categories=None, products=None, validation_config=None)[source]

Get a list of validations playbooks paths either by their names, their groups, by their categories or by their products.

Parameters:
  • path (string) – Path of the validations playbooks

  • validation_id (list) – List of validation name

  • groups (list) – List of validation group

  • categories (list) – List of validation category

  • products (list) – List of validation product

  • validation_config (dict) – A dictionary of configuration for Validation loaded from an validation.cfg file.

Returns:

A list of absolute validations playbooks path

Return type:

list

Example:

>>> path = '/usr/share/validation-playbooks'
>>> validation_id = ['512e','check-cpu']
>>> groups = None
>>> categories = None
>>> products = None
>>> get_validations_playbook(path=path,
                             validation_id=validation_id,
                             groups=groups,
                             categories=categories,
                             products=products)
['/usr/share/ansible/validation-playbooks/512e.yaml',
 '/usr/share/ansible/validation-playbooks/check-cpu.yaml',]
validations_libs.utils.load_config(config)[source]

Load Config File from CLI

validations_libs.utils.parse_all_validations_on_disk(path, groups=None, categories=None, products=None, validation_config=None)[source]

Return a list of validations metadata which can be sorted by Groups, by Categories or by Products.

Parameters:
  • path (string) – The absolute path of the validations directory

  • groups (list) – Groups of validations

  • categories (list) – Categories of validations

  • products (list) – Products of validations

  • validation_config (dict) – A dictionary of configuration for Validation loaded from an validation.cfg file.

Returns:

A list of validations metadata.

Return type:

list

Example:

>>> path = '/foo/bar'
>>> parse_all_validations_on_disk(path)
[{'categories': ['storage'],
  'products': ['product1'],
  'description': 'Detect whether the node disks use Advanced Format.',
  'groups': ['prep', 'pre-deployment'],
  'id': '512e',
  'name': 'Advanced Format 512e Support'},
 {'categories': ['system'],
  'products': ['product1'],
  'description': 'Make sure that the server has enough CPU cores.',
  'groups': ['prep', 'pre-introspection'],
  'id': 'check-cpu',
  'name': 'Verify if the server fits the CPU core requirements'}]
validations_libs.utils.read_validation_groups_file(groups_path=None)[source]

Load groups.yaml file and return a dictionary with its contents

Params groups_path:

The path the groups.yaml file

Returns:

The group list with their descriptions

Return type:

dict

Example:

>>> read_validation_groups_file()
{'group1': [{'description': 'Group1 description.'}],
 'group2': [{'description': 'Group2 description.'}]}
validations_libs.utils.run_command_and_log(log, cmd, cwd=None, env=None)[source]

Run command and log output

Parameters:
  • log (Logger) – Logger instance for logging

  • cmd (String) – Command to run in list form

  • cwd – Current working directory for execution

  • env (List) – Modified environment for command run