Example #1
0
    def execute(self, image_obj, context):
        """
        Main entry point for the trigger execution. Will clear any previously saved exec state and call the evaluate() function.
        :param image_obj:
        :param context:
        :return:
        """
        self.reset()

        if self.gate_cls.__lifecycle_state__ != LifecycleStates.eol and self.__lifecycle_state__ != LifecycleStates.eol:
            if image_obj is None:
                raise TriggerEvaluationError(
                    trigger=self,
                    message='No image provided to evaluate against')
            try:
                self.evaluate(image_obj, context)
            except Exception as e:
                logger.exception(
                    'Error evaluating trigger. Aborting trigger execution')
                raise TriggerEvaluationError(
                    trigger=self,
                    message=
                    'Error executing gate {} trigger {} with params: {}. Msg: {}'
                    .format(self.gate_cls.__gate_name__, self.__trigger_name__,
                            self.eval_params, e.message))

        return True
Example #2
0
    def execute(self, image_obj, exec_context):
        """
        Execute the trigger specified in the rule with the image and gate (for prepared context) and exec_context)

        :param image_obj: The image to execute against
        :param exec_context: The prepared execution context from the gate init
        :return: a tuple of a list of erros and a list of PolicyRuleDecisions, one for each fired trigger match produced by the trigger execution
        """

        matches = None

        try:
            if not self.configured_trigger:
                log.error(
                    'No configured trigger to execute for gate {} and trigger: {}. Returning'
                    .format(self.gate_name, self.trigger_name))
                raise TriggerNotFoundError(trigger_name=self.trigger_name,
                                           gate_name=self.gate_name)

            try:
                self.configured_trigger.execute(image_obj, exec_context)
            except TriggerEvaluationError as e:
                log.exception('Error executing trigger {} on image {}'.format(
                    self.trigger_name, image_obj.id))
                raise
            except Exception as e:
                log.exception(
                    'Unmapped exception caught during trigger evaluation')
                raise TriggerEvaluationError(
                    'Could not evaluate trigger due to error in evaluation execution'
                )

            matches = self.configured_trigger.fired
            decisions = []

            # Try all rules and record all decisions and errors so multiple errors can be reported if present, not just the first encountered
            for match in matches:
                try:
                    decisions.append(
                        PolicyRuleDecision(trigger_match=match,
                                           policy_rule=self))
                except TriggerEvaluationError as e:
                    log.exception(
                        'Policy rule decision mapping exception: {}'.format(e))
                    self.errors.append(str(e))

            return self.errors, decisions
        except Exception as e:
            log.exception('Error executing trigger!')
            raise
Example #3
0
    def execute(self, image_obj, context):
        """
        Main entry point for the trigger execution. Will clear any previously saved exec state and call the evaluate() function.
        :param image_obj:
        :param context:
        :return:
        """
        self.reset()

        if (self.gate_cls.__lifecycle_state__ != LifecycleStates.eol
                and self.__lifecycle_state__ != LifecycleStates.eol):
            if image_obj is None:
                raise TriggerEvaluationError(
                    trigger=self,
                    message="No image provided to evaluate against")
            try:
                self.evaluate(image_obj, context)
            except Exception as e:
                logger.exception(
                    "Error evaluating trigger. Aborting trigger execution")
                raise TriggerEvaluationError(trigger=self, message=str(e))

        return True