Esempio n. 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(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
Esempio n. 2
0
File: worker.py Progetto: miqui/st2
    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
Esempio n. 3
0
 def _run_action(action_ref, parent_execution_id, params, wait_for_completion=True):
     execution = ActionExecutionDB(action=action_ref)
     execution.parameters = ActionChainRunner._cast_params(action_ref, params)
     execution.context = {'parent': str(parent_execution_id)}
     execution = action_service.schedule(execution)
     while (wait_for_completion and
            execution.status != ACTIONEXEC_STATUS_SUCCEEDED and
            execution.status != ACTIONEXEC_STATUS_FAILED):
         eventlet.sleep(1)
         execution = action_db_util.get_actionexec_by_id(execution.id)
     return execution
Esempio n. 4
0
 def _run_action(action_ref,
                 parent_execution_id,
                 params,
                 wait_for_completion=True):
     execution = ActionExecutionDB(action=action_ref)
     execution.parameters = ActionChainRunner._cast_params(
         action_ref, params)
     execution.context = {'parent': str(parent_execution_id)}
     execution = action_service.schedule(execution)
     while (wait_for_completion
            and execution.status != ACTIONEXEC_STATUS_SUCCEEDED
            and execution.status != ACTIONEXEC_STATUS_FAILED):
         eventlet.sleep(1)
         execution = action_db_util.get_actionexec_by_id(execution.id)
     return execution
Esempio n. 5
0
File: base.py Progetto: 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
Esempio n. 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
Esempio n. 7
0
 def test_get_actionexec_existing(self):
     actionexec = action_db_utils.get_actionexec_by_id(ActionDBUtilsTestCase.actionexec_db.id)
     self.assertEqual(actionexec, ActionDBUtilsTestCase.actionexec_db)
Esempio n. 8
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