def iut_provider(self, suite_id):
        """Get the IUT provider configured to suite ID.

        :param suite_id: Suite ID to get IUT provider for.
        :type suite_id: str
        :return: IUT provider object.
        :rtype: :obj:`environment_provider.iut.iut_provider.IutProvider`
        """
        provider_json = self.database.reader.hget(
            f"EnvironmentProvider:{suite_id}", "IUTProvider")
        self.logger.info(provider_json)
        if provider_json:
            provider = IutProvider(
                self.etos,
                self.jsontas,
                json.loads(provider_json,
                           object_pairs_hook=OrderedDict).get("iut"),
            )
            self.etos.config.get("PROVIDERS").append(provider)
            return provider
        return None
    def release(response, task_id):
        """Release an environment.

        :param response: Response object to edit and return.
        :type response: :obj:`falcon.response`
        :param task_id: Task to release.
        :type task_id: str
        """
        try:
            task_result = APP.AsyncResult(task_id)
            result = {
                "status": task_result.status,
            }
            response.status = falcon.HTTP_200
            if task_result.result:
                etos = ETOS(
                    "ETOS Environment Provider",
                    os.getenv("HOSTNAME"),
                    "Environment Provider",
                )
                jsontas = JsonTas()
                registry = ProviderRegistry(etos, jsontas)
                failure = None
                for suite in task_result.result.get("suites", []):
                    try:
                        iut = suite.get("iut")
                        ruleset = registry.get_iut_provider_by_id(
                            iut.get("provider_id"))
                        provider = IutProvider(etos, jsontas, ruleset["iut"])
                        provider.checkin(Iut(**iut))
                    except Exception as exception:  # pylint:disable=broad-except
                        failure = exception

                    try:
                        executor = suite.get("executor")
                        ruleset = registry.get_execution_space_provider_by_id(
                            executor.get("provider_id"))
                        provider = ExecutionSpaceProvider(
                            etos, jsontas, ruleset["execution_space"])
                        provider.checkin(ExecutionSpace(**executor))
                    except Exception as exception:  # pylint:disable=broad-except
                        failure = exception

                    try:
                        log_area = suite.get("log_area")
                        ruleset = registry.get_log_area_provider_by_id(
                            log_area.get("provider_id"))
                        provider = LogAreaProvider(etos, jsontas,
                                                   ruleset["log"])
                        provider.checkin(LogArea(**log_area))
                    except Exception as exception:  # pylint:disable=broad-except
                        failure = exception
                task_result.forget()
                if failure:
                    raise failure
                response.media = {**result}
            else:
                response.media = {
                    "warning": f"Nothing to release with task_id '{task_id}'",
                    **result,
                }
        except Exception as exception:  # pylint:disable=broad-except
            traceback.print_exc()
            response.media = {
                "error": str(exception),
                "details": traceback.format_exc(),
                **result,
            }
Exemple #3
0
def release_environment(etos, jsontas, provider_registry, task_result,
                        release_id):  # pylint:disable=too-many-locals
    """Release an already requested environment.

    :param etos: ETOS library instance.
    :type etos: :obj:`etos_lib.ETOS`
    :param jsontas: JSONTas instance.
    :type jsontas: :obj:`jsontas.jsontas.JsonTas`
    :param provider_registry: The provider registry to get environments from.
    :type provider_registry: :obj:`environment_provider.lib.registry.ProviderRegistry`
    :param celery_worker: The worker holding the task results.
    :type celery_worker: :obj:`celery.Celery`
    :param release_id: The environment ID to release.
    :type release_id: str
    :return: Whether or not the release was successful together with
             a message should the release not be successful.
    :rtype tuple
    """
    if task_result is None or not task_result.result:
        return False, f"Nothing to release with task_id {release_id}"
    failure = None
    for suite in task_result.result.get("suites", []):
        etos.config.set("SUITE_ID", suite.get("suite_id"))
        iut = suite.get("iut")
        iut_ruleset = provider_registry.get_iut_provider_by_id(
            iut.get("provider_id")).get("iut")
        executor = suite.get("executor")
        executor_ruleset = provider_registry.get_execution_space_provider_by_id(
            executor.get("provider_id")).get("execution_space")
        log_area = suite.get("log_area")
        log_area_ruleset = provider_registry.get_log_area_provider_by_id(
            log_area.get("provider_id")).get("log")

        if iut_ruleset.get("type", "jsontas") == "external":
            success, exception = checkin_provider(
                Iut(**iut), ExternalIutProvider(etos, jsontas, iut_ruleset))
        else:
            success, exception = checkin_provider(
                Iut(**iut), IutProvider(etos, jsontas, iut_ruleset))
        if not success:
            failure = exception

        success, exception = checkin_provider(
            LogArea(**log_area),
            LogAreaProvider(etos, jsontas, log_area_ruleset))
        if not success:
            failure = exception
        success, exception = checkin_provider(
            ExecutionSpace(**executor),
            ExecutionSpaceProvider(etos, jsontas, executor_ruleset),
        )
        if not success:
            failure = exception
    task_result.forget()
    if failure:
        # Return the traceback from exception stored in failure.
        return False, "".join(
            traceback.format_exception(failure,
                                       value=failure,
                                       tb=failure.__traceback__))
    return True, ""