Ejemplo n.º 1
0
 def test_chained_executions(self):
     action_ref = ResourceReference(name='chain', pack='core')
     execution = ActionExecutionDB(action=action_ref.ref)
     execution = action_service.schedule(execution)
     execution = ActionExecution.get_by_id(str(execution.id))
     self.assertEqual(execution.status, ACTIONEXEC_STATUS_SUCCEEDED)
     history = ActionExecutionHistory.get(execution__id=str(execution.id),
                                          raise_exception=True)
     action, _ = action_utils.get_action_by_dict({
         'name': action_ref.name,
         'pack': action_ref.pack
     })
     self.assertDictEqual(history.action,
                          vars(ActionAPI.from_model(action)))
     runner = RunnerType.get_by_name(action.runner_type['name'])
     self.assertDictEqual(history.runner,
                          vars(RunnerTypeAPI.from_model(runner)))
     execution = ActionExecution.get_by_id(str(execution.id))
     self.assertDictEqual(history.execution,
                          vars(ActionExecutionAPI.from_model(execution)))
     self.assertGreater(len(history.children), 0)
     for child in history.children:
         record = ActionExecutionHistory.get(id=child, raise_exception=True)
         self.assertEqual(record.parent, str(history.id))
         self.assertEqual(record.action['name'], 'local')
         self.assertEqual(record.runner['name'], 'run-local')
Ejemplo n.º 2
0
 def test_basic_execution(self):
     action_ref = ResourceReference(name='local', pack='core')
     execution = ActionExecutionDB(action=action_ref.ref,
                                   parameters={'cmd': 'uname -a'})
     execution = action_service.schedule(execution)
     execution = ActionExecution.get_by_id(str(execution.id))
     self.assertEqual(execution.status, ACTIONEXEC_STATUS_SUCCEEDED)
     history = ActionExecutionHistory.get(execution__id=str(execution.id),
                                          raise_exception=True)
     self.assertDictEqual(history.trigger, {})
     self.assertDictEqual(history.trigger_type, {})
     self.assertDictEqual(history.trigger_instance, {})
     self.assertDictEqual(history.rule, {})
     action, _ = action_utils.get_action_by_dict({
         'name': action_ref.name,
         'pack': action_ref.pack
     })
     self.assertDictEqual(history.action,
                          vars(ActionAPI.from_model(action)))
     runner = RunnerType.get_by_name(action.runner_type['name'])
     self.assertDictEqual(history.runner,
                          vars(RunnerTypeAPI.from_model(runner)))
     execution = ActionExecution.get_by_id(str(execution.id))
     self.assertDictEqual(history.execution,
                          vars(ActionExecutionAPI.from_model(execution)))
Ejemplo n.º 3
0
    def test_triggered_execution(self):
        docs = {
            'trigger_type':
            copy.deepcopy(fixture.ARTIFACTS['trigger_type']),
            'trigger':
            copy.deepcopy(fixture.ARTIFACTS['trigger']),
            'rule':
            copy.deepcopy(fixture.ARTIFACTS['rule']),
            'trigger_instance':
            copy.deepcopy(fixture.ARTIFACTS['trigger_instance'])
        }

        # Trigger an action execution.
        trigger_type = TriggerType.add_or_update(
            TriggerTypeAPI.to_model(TriggerTypeAPI(**docs['trigger_type'])))
        trigger = Trigger.add_or_update(
            TriggerAPI.to_model(TriggerAPI(**docs['trigger'])))
        rule = RuleAPI.to_model(RuleAPI(**docs['rule']))
        rule.trigger = reference.get_str_resource_ref_from_model(trigger)
        rule = Rule.add_or_update(rule)
        trigger_instance = TriggerInstance.add_or_update(
            TriggerInstanceAPI.to_model(
                TriggerInstanceAPI(**docs['trigger_instance'])))
        enforcer = RuleEnforcer(trigger_instance, rule)
        enforcer.enforce()

        # Wait for the action execution to complete and then confirm outcome.
        execution = ActionExecution.get(
            context__trigger_instance__id=str(trigger_instance.id))
        self.assertIsNotNone(execution)
        execution = ActionExecution.get_by_id(str(execution.id))
        self.assertEqual(execution.status, ACTIONEXEC_STATUS_SUCCEEDED)
        history = ActionExecutionHistory.get(execution__id=str(execution.id),
                                             raise_exception=True)
        self.assertDictEqual(history.trigger,
                             vars(TriggerAPI.from_model(trigger)))
        self.assertDictEqual(history.trigger_type,
                             vars(TriggerTypeAPI.from_model(trigger_type)))
        self.assertDictEqual(
            history.trigger_instance,
            vars(TriggerInstanceAPI.from_model(trigger_instance)))
        self.assertDictEqual(history.rule, vars(RuleAPI.from_model(rule)))
        action_ref = ResourceReference.from_string_reference(
            ref=execution.action)
        action, _ = action_utils.get_action_by_dict({
            'name': action_ref.name,
            'pack': action_ref.pack
        })
        self.assertDictEqual(history.action,
                             vars(ActionAPI.from_model(action)))
        runner = RunnerType.get_by_name(action.runner_type['name'])
        self.assertDictEqual(history.runner,
                             vars(RunnerTypeAPI.from_model(runner)))
        execution = ActionExecution.get_by_id(str(execution.id))
        self.assertDictEqual(history.execution,
                             vars(ActionExecutionAPI.from_model(execution)))
Ejemplo n.º 4
0
 def test_bootstrap(self):
     tracker = results_tracker.ResultsTracker()
     tracker._bootstrap()
     eventlet.sleep(0.2)
     exec_id = str(ResultsTrackerTests.states['state1.json'].execution_id)
     exec_db = ActionExecution.get_by_id(exec_id)
     self.assertTrue(exec_db.result['called_with'][exec_id] is not None,
                     exec_db.result)
     exec_id = str(ResultsTrackerTests.states['state2.json'].execution_id)
     exec_db = ActionExecution.get_by_id(exec_id)
     self.assertTrue(exec_db.result['called_with'][exec_id] is not None,
                     exec_db.result)
     tracker.shutdown()
Ejemplo n.º 5
0
    def record_action_execution(self, body):
        try:
            history_id = bson.ObjectId()
            execution = ActionExecution.get_by_id(str(body.id))
            action_ref = ResourceReference.from_string_reference(
                ref=execution.action)
            action_db, _ = action_utils.get_action_by_dict({
                'name':
                action_ref.name,
                'pack':
                action_ref.pack
            })
            runner = RunnerType.get_by_name(action_db.runner_type['name'])

            attrs = {
                'id': history_id,
                'action': vars(ActionAPI.from_model(action_db)),
                'runner': vars(RunnerTypeAPI.from_model(runner)),
                'execution': vars(ActionExecutionAPI.from_model(execution))
            }

            if 'rule' in execution.context:
                rule = reference.get_model_from_ref(
                    Rule, execution.context.get('rule', {}))
                attrs['rule'] = vars(RuleAPI.from_model(rule))

            if 'trigger_instance' in execution.context:
                trigger_instance_id = execution.context.get(
                    'trigger_instance', {})
                trigger_instance_id = trigger_instance_id.get('id', None)
                trigger_instance = TriggerInstance.get_by_id(
                    trigger_instance_id)
                trigger = reference.get_model_by_resource_ref(
                    db_api=Trigger, ref=trigger_instance.trigger)
                trigger_type = reference.get_model_by_resource_ref(
                    db_api=TriggerType, ref=trigger.type)
                trigger_instance = reference.get_model_from_ref(
                    TriggerInstance,
                    execution.context.get('trigger_instance', {}))
                attrs['trigger_instance'] = vars(
                    TriggerInstanceAPI.from_model(trigger_instance))
                attrs['trigger'] = vars(TriggerAPI.from_model(trigger))
                attrs['trigger_type'] = vars(
                    TriggerTypeAPI.from_model(trigger_type))

            parent = ActionExecutionHistory.get(
                execution__id=execution.context.get('parent', ''))
            if parent:
                attrs['parent'] = str(parent.id)
                if str(history_id) not in parent.children:
                    parent.children.append(str(history_id))
                    ActionExecutionHistory.add_or_update(parent)

            history = ActionExecutionHistoryDB(**attrs)
            history = ActionExecutionHistory.add_or_update(history)
        except:
            LOG.exception('An unexpected error occurred while creating the '
                          'action execution history.')
            raise
Ejemplo n.º 6
0
 def test_launch_workflow(self):
     MistralRunner.entry_point = mock.PropertyMock(
         return_value=WORKFLOW_YAML)
     execution = ActionExecutionDB(action='core.workflow-v2',
                                   parameters={'friend': 'Rocky'})
     execution = action_service.schedule(execution)
     execution = ActionExecution.get_by_id(str(execution.id))
     self.assertEqual(execution.status, ACTIONEXEC_STATUS_RUNNING)
Ejemplo n.º 7
0
 def test_basic_execution(self):
     action_ref = ResourceReference(name='local', pack='core')
     execution = ActionExecutionDB(action=action_ref.ref, parameters={'cmd': 'uname -a'})
     execution = action_service.schedule(execution)
     execution = ActionExecution.get_by_id(str(execution.id))
     self.assertEqual(execution.status, ACTIONEXEC_STATUS_SUCCEEDED)
     history = ActionExecutionHistory.get(execution__id=str(execution.id), raise_exception=True)
     self.assertDictEqual(history.trigger, {})
     self.assertDictEqual(history.trigger_type, {})
     self.assertDictEqual(history.trigger_instance, {})
     self.assertDictEqual(history.rule, {})
     action, _ = action_utils.get_action_by_dict(
         {'name': action_ref.name, 'pack': action_ref.pack})
     self.assertDictEqual(history.action, vars(ActionAPI.from_model(action)))
     runner = RunnerType.get_by_name(action.runner_type['name'])
     self.assertDictEqual(history.runner, vars(RunnerTypeAPI.from_model(runner)))
     execution = ActionExecution.get_by_id(str(execution.id))
     self.assertDictEqual(history.execution, vars(ActionExecutionAPI.from_model(execution)))
Ejemplo n.º 8
0
 def test_callback(self):
     execution = ActionExecutionDB(
         action='core.local', parameters={'cmd': 'uname -a'},
         callback={'source': 'mistral', 'url': 'http://localhost:8989/v2/tasks/12345'})
     execution = action_service.schedule(execution)
     execution = ActionExecution.get_by_id(str(execution.id))
     self.assertEqual(execution.status, ACTIONEXEC_STATUS_SUCCEEDED)
     requests.request.assert_called_with('PUT', execution.callback['url'],
                                         data=json.dumps({'state': 'SUCCESS', 'result': '{}'}),
                                         headers={'content-type': 'application/json'})
Ejemplo n.º 9
0
 def test_dispatch_runner_failure(self):
     runner_container = get_runner_container()
     params = {'actionstr': 'bar'}
     actionexec_db = self._get_failingaction_exec_db_model(params)
     actionexec_db = ActionExecution.add_or_update(actionexec_db)
     self.assertTrue(runner_container.dispatch(actionexec_db))
     # pickup updated actionexec_db
     actionexec_db = ActionExecution.get_by_id(actionexec_db.id)
     self.assertTrue('message' in actionexec_db.result)
     self.assertTrue('traceback' in actionexec_db.result)
Ejemplo n.º 10
0
 def test_chained_executions(self):
     action_ref = ResourceReference(name='chain', pack='core')
     execution = ActionExecutionDB(action=action_ref.ref)
     execution = action_service.schedule(execution)
     execution = ActionExecution.get_by_id(str(execution.id))
     self.assertEqual(execution.status, ACTIONEXEC_STATUS_SUCCEEDED)
     history = ActionExecutionHistory.get(execution__id=str(execution.id), raise_exception=True)
     action, _ = action_utils.get_action_by_dict(
         {'name': action_ref.name, 'pack': action_ref.pack})
     self.assertDictEqual(history.action, vars(ActionAPI.from_model(action)))
     runner = RunnerType.get_by_name(action.runner_type['name'])
     self.assertDictEqual(history.runner, vars(RunnerTypeAPI.from_model(runner)))
     execution = ActionExecution.get_by_id(str(execution.id))
     self.assertDictEqual(history.execution, vars(ActionExecutionAPI.from_model(execution)))
     self.assertGreater(len(history.children), 0)
     for child in history.children:
         record = ActionExecutionHistory.get(id=child, raise_exception=True)
         self.assertEqual(record.parent, str(history.id))
         self.assertEqual(record.action['name'], 'local')
         self.assertEqual(record.runner['name'], 'run-local')
Ejemplo n.º 11
0
 def test_dispatch(self):
     runner_container = get_runner_container()
     params = {'actionstr': 'bar'}
     actionexec_db = self._get_action_exec_db_model(params)
     actionexec_db = ActionExecution.add_or_update(actionexec_db)
     # Assert that execution ran successfully.
     self.assertTrue(runner_container.dispatch(actionexec_db))
     actionexec_db = ActionExecution.get_by_id(actionexec_db.id)
     result = actionexec_db.result
     self.assertTrue(result.get('action_params').get('actionint') == 10)
     self.assertTrue(result.get('action_params').get('actionstr') == 'bar')
Ejemplo n.º 12
0
 def test_basic_execution(self):
     execution = ActionExecutionDB(action='core.local',
                                   parameters={'cmd': 'uname -a'})
     execution = action_service.schedule(execution)
     execution = ActionExecution.get_by_id(str(execution.id))
     self.assertEqual(execution.status, ACTIONEXEC_STATUS_FAILED)
     history = ActionExecutionHistory.get(execution__id=str(execution.id),
                                          raise_exception=True)
     self.assertDictEqual(history.trigger, {})
     self.assertDictEqual(history.trigger_type, {})
     self.assertDictEqual(history.trigger_instance, {})
     self.assertDictEqual(history.rule, {})
     action = action_utils.get_action_by_ref('core.local')
     self.assertDictEqual(history.action,
                          vars(ActionAPI.from_model(action)))
     runner = RunnerType.get_by_name(action.runner_type['name'])
     self.assertDictEqual(history.runner,
                          vars(RunnerTypeAPI.from_model(runner)))
     execution = ActionExecution.get_by_id(str(execution.id))
     self.assertDictEqual(history.execution,
                          vars(ActionExecutionAPI.from_model(execution)))
Ejemplo n.º 13
0
 def test_dispatch_runner_failure(self):
     runner_container = get_runner_container()
     params = {
         'actionstr': 'bar'
     }
     actionexec_db = self._get_failingaction_exec_db_model(params)
     actionexec_db = ActionExecution.add_or_update(actionexec_db)
     self.assertTrue(runner_container.dispatch(actionexec_db))
     # pickup updated actionexec_db
     actionexec_db = ActionExecution.get_by_id(actionexec_db.id)
     self.assertTrue('message' in actionexec_db.result)
     self.assertTrue('traceback' in actionexec_db.result)
Ejemplo n.º 14
0
    def test_dispatch_override_default_action_params(self):
        runner_container = get_runner_container()
        params = {'actionstr': 'foo', 'actionint': 20}
        actionexec_db = self._get_action_exec_db_model(params)
        actionexec_db = ActionExecution.add_or_update(actionexec_db)

        # Assert that execution ran successfully.
        result = runner_container.dispatch(actionexec_db)
        self.assertTrue(result)
        actionexec_db = ActionExecution.get_by_id(actionexec_db.id)
        result = actionexec_db.result
        self.assertTrue(result.get('action_params').get('actionint') == 20)
        self.assertTrue(result.get('action_params').get('actionstr') == 'foo')
Ejemplo n.º 15
0
 def test_dispatch(self):
     runner_container = get_runner_container()
     params = {
         'actionstr': 'bar'
     }
     actionexec_db = self._get_action_exec_db_model(params)
     actionexec_db = ActionExecution.add_or_update(actionexec_db)
     # Assert that execution ran successfully.
     self.assertTrue(runner_container.dispatch(actionexec_db))
     actionexec_db = ActionExecution.get_by_id(actionexec_db.id)
     result = actionexec_db.result
     self.assertTrue(result.get('action_params').get('actionint') == 10)
     self.assertTrue(result.get('action_params').get('actionstr') == 'bar')
Ejemplo n.º 16
0
    def test_triggered_execution(self):
        docs = {
            'trigger_type': copy.deepcopy(fixture.ARTIFACTS['trigger_type']),
            'trigger': copy.deepcopy(fixture.ARTIFACTS['trigger']),
            'rule': copy.deepcopy(fixture.ARTIFACTS['rule']),
            'trigger_instance': copy.deepcopy(fixture.ARTIFACTS['trigger_instance'])}

        # Trigger an action execution.
        trigger_type = TriggerType.add_or_update(
            TriggerTypeAPI.to_model(TriggerTypeAPI(**docs['trigger_type'])))
        trigger = Trigger.add_or_update(TriggerAPI.to_model(TriggerAPI(**docs['trigger'])))
        rule = RuleAPI.to_model(RuleAPI(**docs['rule']))
        rule.trigger = reference.get_str_resource_ref_from_model(trigger)
        rule = Rule.add_or_update(rule)
        trigger_instance = TriggerInstance.add_or_update(
            TriggerInstanceAPI.to_model(TriggerInstanceAPI(**docs['trigger_instance'])))
        enforcer = RuleEnforcer(trigger_instance, rule)
        enforcer.enforce()

        # Wait for the action execution to complete and then confirm outcome.
        execution = ActionExecution.get(context__trigger_instance__id=str(trigger_instance.id))
        self.assertIsNotNone(execution)
        execution = ActionExecution.get_by_id(str(execution.id))
        self.assertEqual(execution.status, ACTIONEXEC_STATUS_SUCCEEDED)
        history = ActionExecutionHistory.get(execution__id=str(execution.id), raise_exception=True)
        self.assertDictEqual(history.trigger, vars(TriggerAPI.from_model(trigger)))
        self.assertDictEqual(history.trigger_type, vars(TriggerTypeAPI.from_model(trigger_type)))
        self.assertDictEqual(history.trigger_instance,
                             vars(TriggerInstanceAPI.from_model(trigger_instance)))
        self.assertDictEqual(history.rule, vars(RuleAPI.from_model(rule)))
        action_ref = ResourceReference.from_string_reference(ref=execution.action)
        action, _ = action_utils.get_action_by_dict(
            {'name': action_ref.name, 'pack': action_ref.pack})
        self.assertDictEqual(history.action, vars(ActionAPI.from_model(action)))
        runner = RunnerType.get_by_name(action.runner_type['name'])
        self.assertDictEqual(history.runner, vars(RunnerTypeAPI.from_model(runner)))
        execution = ActionExecution.get_by_id(str(execution.id))
        self.assertDictEqual(history.execution, vars(ActionExecutionAPI.from_model(execution)))
Ejemplo n.º 17
0
 def put(self, id, actionexecution):
     try:
         actionexec_db = ActionExecution.get_by_id(id)
     except:
         msg = 'ActionExecution by id: %s not found.' % id
         pecan.abort(http_client, msg)
     new_actionexec_db = ActionExecutionAPI.to_model(actionexecution)
     if actionexec_db.status != new_actionexec_db.status:
         actionexec_db.status = new_actionexec_db.status
     if actionexec_db.result != new_actionexec_db.result:
         actionexec_db.result = new_actionexec_db.result
     actionexec_db = ActionExecution.add_or_update(actionexec_db)
     actionexec_api = ActionExecutionAPI.from_model(actionexec_db)
     return actionexec_api
Ejemplo n.º 18
0
 def put(self, id, actionexecution):
     try:
         actionexec_db = ActionExecution.get_by_id(id)
     except:
         msg = 'ActionExecution by id: %s not found.' % id
         pecan.abort(http_client, msg)
     new_actionexec_db = ActionExecutionAPI.to_model(actionexecution)
     if actionexec_db.status != new_actionexec_db.status:
         actionexec_db.status = new_actionexec_db.status
     if actionexec_db.result != new_actionexec_db.result:
         actionexec_db.result = new_actionexec_db.result
     actionexec_db = ActionExecution.add_or_update(actionexec_db)
     actionexec_api = ActionExecutionAPI.from_model(actionexec_db)
     return actionexec_api
Ejemplo n.º 19
0
    def record_action_execution(self, body):
        try:
            history_id = bson.ObjectId()
            execution = ActionExecution.get_by_id(str(body.id))
            action_ref = ResourceReference.from_string_reference(ref=execution.action)
            action_db, _ = action_utils.get_action_by_dict(
                {'name': action_ref.name,
                 'pack': action_ref.pack})
            runner = RunnerType.get_by_name(action_db.runner_type['name'])

            attrs = {
                'id': history_id,
                'action': vars(ActionAPI.from_model(action_db)),
                'runner': vars(RunnerTypeAPI.from_model(runner)),
                'execution': vars(ActionExecutionAPI.from_model(execution))
            }

            if 'rule' in execution.context:
                rule = reference.get_model_from_ref(Rule, execution.context.get('rule', {}))
                attrs['rule'] = vars(RuleAPI.from_model(rule))

            if 'trigger_instance' in execution.context:
                trigger_instance_id = execution.context.get('trigger_instance', {})
                trigger_instance_id = trigger_instance_id.get('id', None)
                trigger_instance = TriggerInstance.get_by_id(trigger_instance_id)
                trigger = reference.get_model_by_resource_ref(db_api=Trigger,
                                                              ref=trigger_instance.trigger)
                trigger_type = reference.get_model_by_resource_ref(db_api=TriggerType,
                                                                   ref=trigger.type)
                trigger_instance = reference.get_model_from_ref(
                    TriggerInstance, execution.context.get('trigger_instance', {}))
                attrs['trigger_instance'] = vars(TriggerInstanceAPI.from_model(trigger_instance))
                attrs['trigger'] = vars(TriggerAPI.from_model(trigger))
                attrs['trigger_type'] = vars(TriggerTypeAPI.from_model(trigger_type))

            parent = ActionExecutionHistory.get(execution__id=execution.context.get('parent', ''))
            if parent:
                attrs['parent'] = str(parent.id)
                if str(history_id) not in parent.children:
                    parent.children.append(str(history_id))
                    ActionExecutionHistory.add_or_update(parent)

            history = ActionExecutionHistoryDB(**attrs)
            history = ActionExecutionHistory.add_or_update(history)
        except:
            LOG.exception('An unexpected error occurred while creating the '
                          'action execution history.')
            raise
Ejemplo n.º 20
0
    def test_dispatch_override_default_action_params(self):
        runner_container = get_runner_container()
        params = {
            'actionstr': 'foo',
            'actionint': 20
        }
        actionexec_db = self._get_action_exec_db_model(params)
        actionexec_db = ActionExecution.add_or_update(actionexec_db)

        # Assert that execution ran successfully.
        result = runner_container.dispatch(actionexec_db)
        self.assertTrue(result)
        actionexec_db = ActionExecution.get_by_id(actionexec_db.id)
        result = actionexec_db.result
        self.assertTrue(result.get('action_params').get('actionint') == 20)
        self.assertTrue(result.get('action_params').get('actionstr') == 'foo')
Ejemplo n.º 21
0
def get_actionexec_by_id(actionexec_id):
    """
        Get ActionExecution by id.

        On error, raise ST2DBObjectNotFoundError.
    """
    actionexec = None

    try:
        actionexec = ActionExecution.get_by_id(actionexec_id)
    except (ValidationError, ValueError) as e:
        LOG.error('Database lookup for actionexecution with id="%s" resulted in '
                  'exception: %s', actionexec_id, e)
        raise StackStormDBObjectNotFoundError('Unable to find actionexecution with '
                                              'id="%s"' % actionexec_id)

    return actionexec
Ejemplo n.º 22
0
 def test_schedule(self):
     context = {'user': USERNAME}
     parameters = {'hosts': 'localhost', 'cmd': 'uname -a'}
     request = ActionExecutionDB(action=ACTION_REF, context=context, parameters=parameters)
     request = action_service.schedule(request)
     execution = ActionExecution.get_by_id(str(request.id))
     self.assertIsNotNone(execution)
     self.assertEqual(execution.id, request.id)
     action = '.'.join([self.actiondb.pack, self.actiondb.name])
     actual_action = execution.action
     self.assertEqual(actual_action, action)
     self.assertEqual(execution.context['user'], request.context['user'])
     self.assertDictEqual(execution.parameters, request.parameters)
     self.assertEqual(execution.status, ACTIONEXEC_STATUS_SCHEDULED)
     # mongoengine DateTimeField stores datetime only up to milliseconds
     self.assertEqual(isotime.format(execution.start_timestamp, usec=False),
                      isotime.format(request.start_timestamp, usec=False))
Ejemplo n.º 23
0
 def test_callback(self):
     execution = ActionExecutionDB(
         action='core.local',
         parameters={'cmd': 'uname -a'},
         callback={
             'source': 'mistral',
             'url': 'http://localhost:8989/v2/tasks/12345'
         })
     execution = action_service.schedule(execution)
     execution = ActionExecution.get_by_id(str(execution.id))
     self.assertEqual(execution.status, ACTIONEXEC_STATUS_SUCCEEDED)
     requests.request.assert_called_with(
         'PUT',
         execution.callback['url'],
         data=json.dumps({
             'state': 'SUCCESS',
             'result': '{}'
         }),
         headers={'content-type': 'application/json'})
Ejemplo n.º 24
0
 def test_schedule(self):
     context = {'user': USERNAME}
     parameters = {'hosts': 'localhost', 'cmd': 'uname -a'}
     request = ActionExecutionDB(action=ACTION_REF,
                                 context=context,
                                 parameters=parameters)
     request = action_service.schedule(request)
     execution = ActionExecution.get_by_id(str(request.id))
     self.assertIsNotNone(execution)
     self.assertEqual(execution.id, request.id)
     action = '.'.join([self.actiondb.pack, self.actiondb.name])
     actual_action = execution.action
     self.assertEqual(actual_action, action)
     self.assertEqual(execution.context['user'], request.context['user'])
     self.assertDictEqual(execution.parameters, request.parameters)
     self.assertEqual(execution.status, ACTIONEXEC_STATUS_SCHEDULED)
     # mongoengine DateTimeField stores datetime only up to milliseconds
     self.assertEqual(isotime.format(execution.start_timestamp, usec=False),
                      isotime.format(request.start_timestamp, usec=False))
Ejemplo n.º 25
0
def get_actionexec_by_id(actionexec_id):
    """
        Get ActionExecution by id.

        On error, raise ST2DBObjectNotFoundError.
    """
    actionexec = None

    try:
        actionexec = ActionExecution.get_by_id(actionexec_id)
    except (ValidationError, ValueError) as e:
        LOG.error(
            'Database lookup for actionexecution with id="%s" resulted in '
            'exception: %s', actionexec_id, e)
        raise StackStormDBObjectNotFoundError(
            'Unable to find actionexecution with '
            'id="%s"' % actionexec_id)

    return actionexec
Ejemplo n.º 26
0
 def update_action_execution_history(self, body):
     try:
         count = self.timeout / self.wait
         # Allow up to 1 minute for the post event to create the history record.
         for i in range(count):
             history = ActionExecutionHistory.get(execution__id=str(body.id))
             if history:
                 execution = ActionExecution.get_by_id(str(body.id))
                 history.execution = vars(ActionExecutionAPI.from_model(execution))
                 history = ActionExecutionHistory.add_or_update(history)
                 return
             if i >= count:
                 # If wait failed, create the history record regardless.
                 self.record_action_execution(body)
                 return
             eventlet.sleep(self.wait)
     except:
         LOG.exception('An unexpected error occurred while updating the '
                       'action execution history.')
         raise
Ejemplo n.º 27
0
 def update_action_execution_history(self, body):
     try:
         count = self.timeout / self.wait
         # Allow up to 1 minute for the post event to create the history record.
         for i in range(count):
             history = ActionExecutionHistory.get(execution__id=str(body.id))
             if history:
                 execution = ActionExecution.get_by_id(str(body.id))
                 history.execution = vars(ActionExecutionAPI.from_model(execution))
                 history = ActionExecutionHistory.add_or_update(history)
                 return
             if i >= count:
                 # If wait failed, create the history record regardless.
                 self.record_action_execution(body)
                 return
             eventlet.sleep(self.wait)
     except:
         LOG.exception('An unexpected error occurred while updating the '
                       'action execution history.')
         raise
Ejemplo n.º 28
0
def schedule(execution):

    # Use the user context from the parent action execution. Subtasks in a workflow
    # action can be invoked by a system user and so we want to use the user context
    # from the original workflow action.
    if getattr(execution, 'context', None) and 'parent' in execution.context:
        parent = ActionExecution.get_by_id(execution.context['parent'])
        execution.context['user'] = getattr(parent, 'context', dict()).get('user')

    # Validate action.
    action_db = action_utils.get_action_by_ref(execution.action)
    if not action_db:
        raise ValueError('Action "%s" cannot be found.' % execution.action)
    if not action_db.enabled:
        raise ValueError('Unable to execute. Action "%s" is disabled.' % execution.action)

    runnertype_db = action_utils.get_runnertype_by_name(action_db.runner_type['name'])

    if not hasattr(execution, 'parameters'):
        execution.parameters = dict()

    # Validate action parameters.
    schema = util_schema.get_parameter_schema(action_db)
    validator = util_schema.get_validator()
    jsonschema.validate(execution.parameters, schema, validator)

    # validate that no immutable params are being overriden. Although possible to
    # ignore the override it is safer to inform the user to avoid surprises.
    immutables = _get_immutable_params(action_db.parameters)
    immutables.extend(_get_immutable_params(runnertype_db.runner_parameters))
    overridden_immutables = [p for p in six.iterkeys(execution.parameters) if p in immutables]
    if len(overridden_immutables) > 0:
        raise ValueError('Override of immutable parameter(s) %s is unsupported.'
                         % str(overridden_immutables))

    # Write to database and send to message queue.
    execution.status = ACTIONEXEC_STATUS_SCHEDULED
    execution.start_timestamp = isotime.add_utc_tz(datetime.datetime.utcnow())
    execution = ActionExecution.add_or_update(execution)
    LOG.audit('Action execution scheduled. ActionExecution=%s.', execution)
    return execution
Ejemplo n.º 29
0
 def test_launch_workflow_when_workbook_not_exists(self):
     execution = ActionExecutionDB(action='core.workflow-v2', parameters={'friend': 'Rocky'})
     execution = action_service.schedule(execution)
     execution = ActionExecution.get_by_id(str(execution.id))
     self.assertEqual(execution.status, ACTIONEXEC_STATUS_RUNNING)
Ejemplo n.º 30
0
 def test_launch_workflow_when_definition_changed(self):
     MistralRunner.entry_point = mock.PropertyMock(return_value=WORKFLOW_YAML)
     execution = ActionExecutionDB(action='core.workflow-v2', parameters={'friend': 'Rocky'})
     execution = action_service.schedule(execution)
     execution = ActionExecution.get_by_id(str(execution.id))
     self.assertEqual(execution.status, ACTIONEXEC_STATUS_RUNNING)
Ejemplo n.º 31
0
 def test_launch_workflow_when_workbook_not_exists(self):
     execution = ActionExecutionDB(action='core.workflow-v2',
                                   parameters={'friend': 'Rocky'})
     execution = action_service.schedule(execution)
     execution = ActionExecution.get_by_id(str(execution.id))
     self.assertEqual(execution.status, ACTIONEXEC_STATUS_RUNNING)