Пример #1
0
    def execute_action(self, actionexecution):
        try:
            actionexec_db = get_actionexec_by_id(actionexecution.id)
        except StackStormDBObjectNotFoundError:
            LOG.exception('Failed to find ActionExecution %s in the database.',
                          actionexecution.id)
            raise

        # Update ActionExecution status to "running"
        actionexec_db = update_actionexecution_status(
            status=ACTIONEXEC_STATUS_RUNNING, actionexec_id=actionexec_db.id)
        # Launch action
        LOG.audit('Launching action execution.',
                  extra={'actionexec': actionexec_db.to_serializable_dict()})

        try:
            result = self.container.dispatch(actionexec_db)
            LOG.debug('Runner dispatch produced result: %s', result)
        except Exception:
            actionexec_db = update_actionexecution_status(
                status=ACTIONEXEC_STATUS_FAILED,
                actionexec_id=actionexec_db.id)
            raise

        if not result:
            raise ActionRunnerException('Failed to execute action.')

        return result
Пример #2
0
    def execute_action(self, actionexecution):
        try:
            actionexec_db = get_actionexec_by_id(actionexecution.id)
        except StackStormDBObjectNotFoundError:
            LOG.exception('Failed to find ActionExecution %s in the database.',
                          actionexecution.id)
            raise

        # Update ActionExecution status to "running"
        actionexec_db = update_actionexecution_status(ACTIONEXEC_STATUS_RUNNING,
                                                      actionexec_db.id)
        # Launch action
        LOG.audit('Launching action execution.',
                  extra={'actionexec': actionexec_db.to_serializable_dict()})

        try:
            result = self.container.dispatch(actionexec_db)
            LOG.debug('Runner dispatch produced result: %s', result)
        except Exception:
            actionexec_db = update_actionexecution_status(ACTIONEXEC_STATUS_FAILED,
                                                          actionexec_db.id)
            raise

        if not result:
            raise ActionRunnerException('Failed to execute action.')

        return result
Пример #3
0
Файл: base.py Проект: miqui/st2
    def _update_action_execution_db(self, actionexec_id, status, result):
        actionexec_db = get_actionexec_by_id(actionexec_id)

        if status in DONE_STATES:
            end_timestamp = isotime.add_utc_tz(datetime.datetime.utcnow())
        else:
            end_timestamp = None

        # Push result data and updated status to ActionExecution DB
        actionexec_db = update_actionexecution_status(
            status=status,
            result=result,
            end_timestamp=end_timestamp,
            actionexec_db=actionexec_db)
        return actionexec_db
Пример #4
0
    def test_update_actionexecution_status(self):
        actionexec_db = ActionExecutionDB()
        actionexec_db.status = 'initializing'
        actionexec_db.start_timestamp = datetime.datetime.utcnow()
        actionexec_db.action = ResourceReference(
            name=ActionDBUtilsTestCase.action_db.name,
            pack=ActionDBUtilsTestCase.action_db.pack).ref
        params = {
            'actionstr': 'foo',
            'some_key_that_aint_exist_in_action_or_runner': 'bar',
            'runnerint': 555
        }
        actionexec_db.parameters = params
        actionexec_db = ActionExecution.add_or_update(actionexec_db)
        origactionexec_db = copy.copy(actionexec_db)

        # Update by id.
        newactionexec_db = action_db_utils.update_actionexecution_status(
            'running', actionexec_id=actionexec_db.id)
        # Verify id didn't change.
        self.assertEqual(origactionexec_db.id, newactionexec_db.id)
        self.assertEqual(newactionexec_db.status, 'running')
Пример #5
0
    def test_update_actionexecution_status_and_end_timestamp(self):
        actionexec_db = ActionExecutionDB()
        actionexec_db.status = 'initializing'
        actionexec_db.start_timestamp = datetime.datetime.utcnow()
        actionexec_db.action = ResourceReference(
            name=ActionDBUtilsTestCase.action_db.name,
            pack=ActionDBUtilsTestCase.action_db.pack).ref
        actionexec_db.parameters = {}
        actionexec_db = ActionExecution.add_or_update(actionexec_db)
        origactionexec_db = copy.copy(actionexec_db)

        # Update by id
        now = datetime.datetime.now()
        newactionexec_db = action_db_utils.update_actionexecution_status(
            status='running',
            end_timestamp=now,
            actionexec_id=actionexec_db.id)

        # Verify id didn't change and end_timestamp has been set
        self.assertEqual(origactionexec_db.id, newactionexec_db.id)
        self.assertEqual(newactionexec_db.status, 'running')
        self.assertEqual(newactionexec_db.end_timestamp, now)
Пример #6
0
    def _do_run(self, runner, runnertype_db, action_db, actionexec_db):
        # Finalized parameters are resolved and then rendered.
        runner_params, action_params = param_utils.get_finalized_params(
            runnertype_db.runner_parameters, action_db.parameters,
            actionexec_db.parameters)

        resolved_entry_point = self._get_entry_point_abs_path(
            action_db.pack, action_db.entry_point)
        runner.container_service = RunnerContainerService()
        runner.action = action_db
        runner.action_name = action_db.name
        runner.action_execution_id = str(actionexec_db.id)
        runner.entry_point = resolved_entry_point
        runner.runner_parameters = runner_params
        runner.context = getattr(actionexec_db, 'context', dict())
        runner.callback = getattr(actionexec_db, 'callback', dict())
        runner.libs_dir_path = self._get_action_libs_abs_path(
            action_db.pack, action_db.entry_point)
        runner.auth_token = self._create_auth_token(runner.context)

        try:
            LOG.debug('Performing pre-run for runner: %s', runner)
            runner.pre_run()

            LOG.debug('Performing run for runner: %s', runner)
            run_result = runner.run(action_params)
            LOG.debug('Result of run: %s', run_result)
        except:
            LOG.exception('Failed to run action.')
            _, ex, tb = sys.exc_info()
            # mark execution as failed.
            runner.container_service.report_status(ACTIONEXEC_STATUS_FAILED)
            # include the error message and traceback to try and provide some hints.
            runner.container_service.report_result({
                'message':
                str(ex),
                'traceback':
                ''.join(traceback.format_tb(tb, 20))
            })
        finally:
            # Always clean-up the auth_token
            try:
                self._delete_auth_token(runner.auth_token)
            except:
                LOG.warn('Unable to clean-up auth_token.')

        # Re-load Action Execution from DB:
        actionexec_db = get_actionexec_by_id(actionexec_db.id)

        # TODO: Store payload when DB model can hold payload data
        action_result = runner.container_service.get_result()
        actionexec_status = runner.container_service.get_status()
        LOG.debug('Result as reporter to container service: %s', action_result)

        if action_result is None:
            # If the runner didn't set an exit code then the action didn't complete.
            # Therefore, the action produced an error.
            result = False
            if not actionexec_status:
                actionexec_status = ACTIONEXEC_STATUS_FAILED
                runner.container_service.report_status(actionexec_status)
        else:
            # So long as the runner produced an exit code, we can assume that the
            # Live Action ran to completion.
            result = True
            actionexec_db.result = action_result
            if not actionexec_status:
                actionexec_status = ACTIONEXEC_STATUS_SUCCEEDED
                runner.container_service.report_status(actionexec_status)

        # Push result data and updated status to ActionExecution DB
        actionexec_db = update_actionexecution_status(
            actionexec_status, actionexec_db=actionexec_db)

        LOG.debug('Performing post_run for runner: %s', runner)
        runner.post_run()
        runner.container_service = None

        return result, actionexec_db
Пример #7
0
    def _do_run(self, runner, runnertype_db, action_db, actionexec_db):
        # Finalized parameters are resolved and then rendered.
        runner_params, action_params = param_utils.get_finalized_params(
            runnertype_db.runner_parameters, action_db.parameters, actionexec_db.parameters)

        resolved_entry_point = self._get_entry_point_abs_path(action_db.pack,
                                                              action_db.entry_point)
        runner.container_service = RunnerContainerService()
        runner.action = action_db
        runner.action_name = action_db.name
        runner.action_execution_id = str(actionexec_db.id)
        runner.entry_point = resolved_entry_point
        runner.runner_parameters = runner_params
        runner.context = getattr(actionexec_db, 'context', dict())
        runner.callback = getattr(actionexec_db, 'callback', dict())
        runner.libs_dir_path = self._get_action_libs_abs_path(action_db.pack,
                                                              action_db.entry_point)
        runner.auth_token = self._create_auth_token(runner.context)

        try:
            LOG.debug('Performing pre-run for runner: %s', runner)
            runner.pre_run()

            LOG.debug('Performing run for runner: %s', runner)
            run_result = runner.run(action_params)
            LOG.debug('Result of run: %s', run_result)
        except:
            LOG.exception('Failed to run action.')
            _, ex, tb = sys.exc_info()
            # mark execution as failed.
            runner.container_service.report_status(ACTIONEXEC_STATUS_FAILED)
            # include the error message and traceback to try and provide some hints.
            runner.container_service.report_result(
                {'message': str(ex), 'traceback': ''.join(traceback.format_tb(tb, 20))})
        finally:
            # Always clean-up the auth_token
            try:
                self._delete_auth_token(runner.auth_token)
            except:
                LOG.warn('Unable to clean-up auth_token.')

        # Re-load Action Execution from DB:
        actionexec_db = get_actionexec_by_id(actionexec_db.id)

        # TODO: Store payload when DB model can hold payload data
        action_result = runner.container_service.get_result()
        actionexec_status = runner.container_service.get_status()
        LOG.debug('Result as reporter to container service: %s', action_result)

        if action_result is None:
            # If the runner didn't set an exit code then the action didn't complete.
            # Therefore, the action produced an error.
            result = False
            if not actionexec_status:
                actionexec_status = ACTIONEXEC_STATUS_FAILED
                runner.container_service.report_status(actionexec_status)
        else:
            # So long as the runner produced an exit code, we can assume that the
            # Live Action ran to completion.
            result = True
            actionexec_db.result = action_result
            if not actionexec_status:
                actionexec_status = ACTIONEXEC_STATUS_SUCCEEDED
                runner.container_service.report_status(actionexec_status)

        # Push result data and updated status to ActionExecution DB
        actionexec_db = update_actionexecution_status(actionexec_status,
                                                      actionexec_db=actionexec_db)

        LOG.debug('Performing post_run for runner: %s', runner)
        runner.post_run()
        runner.container_service = None

        return result, actionexec_db