Ejemplo n.º 1
0
    def _get_action_concurrency_key(self, item):
        """
        Gets the concurrency key for a tasks action
        :param item: The task item
        :return: The concurrency key for the tasks action
        """
        action = item[handlers.TASK_TR_ACTION]
        # get the name of the optional method to return the concurrency key
        action_class = actions.get_action_class(action)
        concurrency_key_method = getattr(
            action_class, handlers.ACTION_CONCURRENCY_KEY_METHOD, None)

        # prepare parameters for calling static function that returns the concurrency key
        if concurrency_key_method is not None:
            get_key_params = {
                actions.ACTION_PARAM_RESOURCES:
                handlers.get_item_resource_data(item, self._context),
                actions.ACTION_PARAM_ACCOUNT:
                item[handlers.TASK_TR_ACCOUNT],
                actions.ACTION_PARAM_STACK:
                os.getenv(handlers.ENV_STACK_NAME),
                actions.ACTION_PARAM_STACK_ID:
                os.getenv(handlers.ENV_STACK_ID),
                actions.ACTION_PARAM_TASK_ID:
                item[handlers.TASK_TR_ID],
                actions.ACTION_PARAM_TASK:
                item[handlers.TASK_TR_NAME]
            }
            get_key_params.update(item.get(handlers.TASK_TR_PARAMETERS))
            return concurrency_key_method(get_key_params)
        else:
            # if this method is not available for action then use the name of the action as the key
            return action
    def action_resources(self):
        if self._action_resources is None:

            if not self._event.get(handlers.TASK_TR_S3_RESOURCES, False):
                self._action_resources = handlers.get_item_resource_data(
                    self._event, self._context)
            else:
                bucket = os.getenv(handlers.ENV_RESOURCE_BUCKET)
                key = self.action_id + ".json"
                try:
                    resp = self.s3_client.get_object_with_retries(
                        Bucket=bucket, Key=key)
                    self._event[handlers.TASK_TR_RESOURCES] = resp[
                        "Body"].read().decode('utf-8')
                    self._action_resources = handlers.get_item_resource_data(
                        self._event, self._context)
                except Exception as ex:
                    raise_exception(ERR_READING_S3_RESOURCES, bucket, key,
                                    self.action_id, ex)

        return self._action_resources
    def _get_tasks_to_execute(self):

        task_items = self._build_tasks_for_selected_resources()

        for item in task_items:
            event = {i: item.get(i) for i in item}
            event[handlers.
                  HANDLER_EVENT_ACTION] = handlers.HANDLER_ACTION_EXECUTE
            event[actions.ACTION_SERVICE] = actions.get_action_properties(
                item[handlers.TASK_TR_ACTION]).get(actions.ACTION_SERVICE)
            action_argument = {
                actions.ACTION_PARAM_CONTEXT:
                self.context,
                actions.ACTION_PARAM_EVENT:
                event,
                actions.ACTION_PARAM_SESSION:
                services.get_session(
                    role_arn=item.get(handlers.TASK_TR_ASSUMED_ROLE, None)),
                actions.ACTION_PARAM_RESOURCES:
                handlers.get_item_resource_data(item, context=self.context),
                actions.ACTION_PARAM_DEBUG:
                item[handlers.TASK_TR_DEBUG],
                actions.ACTION_PARAM_DRYRUN:
                item[handlers.TASK_TR_DRYRUN],
                actions.ACTION_PARAM_TASK_ID:
                item[handlers.TASK_TR_ID],
                actions.ACTION_PARAM_TASK:
                item[handlers.TASK_TR_NAME],
                actions.ACTION_PARAM_TASK_TIMEZONE:
                item[handlers.TASK_TR_TIMEZONE],
                actions.ACTION_PARAM_STACK:
                self.action_stack.stack_name
                if self.action_stack is not None else None,
                actions.ACTION_PARAM_STACK_ID:
                self.action_stack.stack_id
                if self.action_stack is not None else None,
                actions.ACTION_PARAM_STACK_RESOURCES:
                self.action_stack_resources,
                actions.ACTION_PARAM_ASSUMED_ROLE:
                item.get(handlers.TASK_TR_ASSUMED_ROLE),
                actions.ACTION_PARAM_STARTED_AT:
                item[handlers.TASK_TR_STARTED_TS],
                actions.ACTION_PARAM_TAGFILTER:
                item[handlers.TASK_TR_TAGFILTER],
                actions.ACTION_PARAM_TIMEOUT:
                item[handlers.TASK_TR_TIMEOUT],
                actions.ACTION_PARAM_LOGGER:
                self.logger,
                actions.ACTION_PARAM_EVENTS:
                self._events,
                actions.ACTION_PARAM_HAS_COMPLETION:
                item[handlers.TASK_TR_HAS_COMPLETION],
                actions.ACTION_PARAM_INTERVAL:
                item[handlers.TASK_INTERVAL]
            }

            if self._assumed_role is None:
                self._assumed_role = action_argument[
                    actions.ACTION_PARAM_ASSUMED_ROLE]

            action_instance = self.action_class(
                action_argument, item.get(handlers.TASK_TR_PARAMETERS, {}))

            self.verify_log_subject(action_argument)
            self.verify_concurrency(action_argument, item)

            yield action_instance