diff --git a/setup.cfg b/setup.cfg index a84978897..e8e55043a 100644 --- a/setup.cfg +++ b/setup.cfg @@ -107,6 +107,7 @@ openstack.tripleoclient.v1 = undercloud_upgrade = tripleoclient.v1.undercloud:UpgradeUndercloud undercloud_backup = tripleoclient.v1.undercloud_backup:BackupUndercloud tripleo_validator_list = tripleoclient.v1.tripleo_validator:TripleOValidatorList + tripleo_validator_run = tripleoclient.v1.tripleo_validator:TripleOValidatorRun oslo.config.opts = undercloud_config = tripleoclient.config.undercloud:list_opts standalone_config = tripleoclient.config.standalone:list_opts diff --git a/tripleoclient/v1/tripleo_validator.py b/tripleoclient/v1/tripleo_validator.py index f80a6d980..adea84545 100644 --- a/tripleoclient/v1/tripleo_validator.py +++ b/tripleoclient/v1/tripleo_validator.py @@ -21,7 +21,6 @@ from osc_lib.i18n import _ from tripleoclient import constants from tripleoclient import utils as oooutils - from tripleoclient.workflows import validations LOG = logging.getLogger(__name__ + ".TripleoValidator") @@ -39,6 +38,11 @@ class _CommaListGroupAction(argparse.Action): setattr(namespace, self.dest, values.split(',')) +class _CommaListAction(argparse.Action): + def __call__(self, parser, namespace, values, option_string=None): + setattr(namespace, self.dest, values.split(',')) + + class TripleOValidatorList(command.Command): """List the available validations""" @@ -98,3 +102,78 @@ class TripleOValidatorList(command.Command): def take_action(self, parsed_args): self._run_validator_list(parsed_args) + + +class TripleOValidatorRun(command.Command): + """Run the available validations""" + + def get_parser(self, prog_name): + parser = argparse.ArgumentParser( + description=self.get_description(), + prog=prog_name, + add_help=False + ) + + parser.add_argument( + '--plan', + action='store', + default='overcloud', + help=_("Execute the validations using a " + "custom plan name. " + "Defaults to: overcloud") + ) + + ex_group = parser.add_mutually_exclusive_group(required=True) + + ex_group.add_argument( + '--validation-name', + metavar='[,,...]', + action=_CommaListAction, + default=[], + help=_("Run specific validations, " + "if more than one validation is required " + "separate the names with commas" + "Defaults to: [] " + "i.e. --validation-name check-ftype,512e " + " --validation-name 512e") + ) + + ex_group.add_argument( + '--group', + metavar='[,,...]', + action=_CommaListGroupAction, + default=[], + help=_("Run specific group validations, " + "if more than one group is required " + "separate the group names with commas" + "Defaults to: ['pre-deployment'] " + "i.e. --group pre-upgrade,prep " + " --group openshift-on-openstack") + ) + + return parser + + def _run_validator_run(self, parsed_args): + clients = self.app.client_manager + + if not parsed_args.validation_name: + workflow_input = { + "plan": parsed_args.plan, + "group_names": parsed_args.group + } + else: + workflow_input = { + "plan": parsed_args.plan, + "validation_names": parsed_args.validation_name + } + + LOG.debug(_('Runnning the validations')) + try: + output = validations.run_validations(clients, workflow_input) + print(oooutils.get_validations_json(output)) + except Exception as e: + print(_("Running the validations finished with errors")) + print('Output: {}'.format(e)) + + def take_action(self, parsed_args): + self._run_validator_run(parsed_args) diff --git a/tripleoclient/workflows/validations.py b/tripleoclient/workflows/validations.py index 229e917c5..fc5755fb5 100644 --- a/tripleoclient/workflows/validations.py +++ b/tripleoclient/workflows/validations.py @@ -16,6 +16,8 @@ import pprint from tripleoclient.workflows import base +from tripleoclient import exceptions + def list_validations(clients, workflow_input): @@ -33,3 +35,37 @@ def list_validations(clients, workflow_input): if 'message' in payload: assert payload['status'] == "SUCCESS", pprint.pformat(payload) return payload['validations'] + + +def run_validations(clients, workflow_input): + + workflow_client = clients.workflow_engine + tripleoclients = clients.tripleoclient + + with tripleoclients.messaging_websocket() as ws: + + if 'group_names' in workflow_input: + print('Running group validations') + execution = base.start_workflow( + workflow_client, + 'tripleo.validations.v1.run_groups', + workflow_input=workflow_input + ) + else: + print('Running single validations') + execution = base.start_workflow( + workflow_client, + 'tripleo.validations.v1.run_validations', + workflow_input=workflow_input + ) + + for payload in base.wait_for_messages(workflow_client, ws, execution): + if 'message' in payload: + if payload['status'] == 'SUCCESS': + return payload['message'] + + if payload['status'] == 'FAILED': + raise exceptions.RegisterOrUpdateError( + 'Exception running validations: {}'.format( + payload['message']) + )