Source code for satellite_populate.commands

# coding: utf-8
"""
This module contains commands to interact with satellite populator
and validator.

Commands included:

satellite-populate
------------------

A command to populate the system based in an YAML file describing the
entities::

    $ satellite-populate file.yaml -h myhost.com -o /tmp/validation.yaml

validate
--------

A command to validate the system based in an validation file generated by
the populate or a YAML file with mode: validation::

    $ satellite-populate /tmp/validation.yaml

Use :code:`$ satellite-populate --help` for more info
"""
import click
import os
import sys
import yaml

from satellite_populate.constants import TEST_DATA
from satellite_populate.main import populate

CONFIG_FILE = '.satellite_populate.yaml'
SATELLITE_POPULATE_FILE = 'SATELLITE_POPULATE_FILE'


def _read_populate_settings(config_file):
    """Parse satellite-populate configuration"""
    if config_file.endswith(('.yml', '.yaml', 'json')):
        with open(config_file) as config:
            settings = yaml.load(config)
            if type(settings) == dict:
                return settings
    return {}


[docs]def configure(): """Read satellite-populate settings file.""" if os.path.isfile(os.path.join(os.environ['HOME'], CONFIG_FILE)): config = os.path.join(os.environ['HOME'], CONFIG_FILE) elif SATELLITE_POPULATE_FILE in os.environ.keys(): config = os.environ[SATELLITE_POPULATE_FILE] else: return {} return _read_populate_settings(config)
[docs]def execute_populate(datafile, verbose, output, mode, scheme, port, hostname, username, password, report=True, enable_output=True): """Populate using the data described in `datafile`:""" settings = configure() result = populate( datafile, verbose=verbose or settings.get('verbose'), output=output or settings.get('output'), mode=mode or settings.get('mode'), scheme=scheme or settings.get('scheme'), port=port or settings.get('port'), hostname=hostname or settings.get('hostname'), username=username or settings.get('username'), password=password or settings.get('password'), enable_output=enable_output or settings.get('enable_output') ) if not report: return result.logger.info( "{0} entities already existing in the system".format( len(result.found) ) ) if result.mode == 'populate': result.logger.info( "{0} entities were created in the system".format( len(result.created) ) ) if result.assertion_errors: for error in result.assertion_errors: data = error['data'] result.logger.error( 'assertion: %s is NOT %s to %s', data['value'], error['operator'], data['other'] ) if result.mode == 'validate': sys.exit("System entities did not validated!") if result.validation_errors: for error in result.validation_errors: result.logger.error(error['message']) result.logger.error(error['search']) if result.mode == 'validate': sys.exit("System entities did not validated!") else: if result.mode == 'validate': result.logger.info("System Validated!!!")
VERBOSE_HELP = """ envvar:POPULATE_VERBOSE -v(1):DEBUG -vv(2):INFO -vvv(3):WARNING -vvvv(4):ERROR -vvvvv(5):CRITICAL """ @click.command() @click.argument( 'datafile', required=False, type=click.Path(), envvar='POPULATE_DATAFILE' ) @click.option( '-v', '--verbose', count=True, envvar='POPULATE_VERBOSE', help=VERBOSE_HELP ) @click.option( '-o', '--output', type=click.Path(), default=None, envvar='POPULATE_OUTPUT', help="""envvar:POPULATE_OUTPUT Filepath to output the validation file e.g: -o=validation_123.yaml """ ) @click.option( '-m', '--mode', type=click.Choice(choices=('populate', 'validate')), default=None, envvar='POPULATE_MODE', help="""envvar:POPULATE_MODE valid modes are populate and validate""" ) @click.option('--scheme', default=None, envvar='POPULATE_SCHEMA', help="""envvar:POPULATE_SCHEMA\noptional url schema""") @click.option('--port', default=None, envvar='POPULATE_PORT', type=int, help="""envvar:POPULATE_PORT\noptional url port""") @click.option('-h', '--hostname', default=None, envvar='POPULATE_HOSTNAME', help="""envvar:POPULATE_HOSTNAME\nSatellite URL:server.com""") @click.option('-u', '--username', default=None, envvar='POPULATE_USERNAME', help="""envvar:POPULATE_USERNAME\nAdmin user""") @click.option('-p', '--password', default=None, envvar='POPULATE_PASSWORD', help="""envvar:POPULATE_PASSWORD\nAdmin Password""") @click.option('-t', '--test', default=False, envvar='POPULATE_TEST', help="""envvar:POPULATE_TEST\nRun a simple test""", is_flag=True) @click.option('-r', '--report/--no-report', default=True, is_flag=True, envvar='POPULATE_REPORT', help="""envvar:POPULATE_REPORT\nShow execution report?""") @click.option('--enable-output/--no-output', default=True, is_flag=True, envvar='POPULATE_OUTPUT', help="""envvar:POPULATE_OUTPUT\nShould write validation file?""") def main(datafile, verbose, output, mode, scheme, port, hostname, username, password, test, report, enable_output): """Populates or validates Satellite entities. Full documentation can be found on: https://satellite-populate.readthedocs.io\n $ satellite-populate test_data.yaml -vv\n $ satellite-populate test_data.yaml -vv --mode=validate\n $ satellite-populate test_data.yaml -vv -h myserver.com """ if test: datafile = TEST_DATA verbose = 1 report = False if not datafile: main(['--help']) execute_populate(datafile, verbose, output, mode, scheme, port, hostname, username, password, report, enable_output) if __name__ == "__main__": main()