本文整理汇总了Python中st2common.persistence.liveaction.LiveAction.add_or_update方法的典型用法代码示例。如果您正苦于以下问题:Python LiveAction.add_or_update方法的具体用法?Python LiveAction.add_or_update怎么用?Python LiveAction.add_or_update使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类st2common.persistence.liveaction.LiveAction
的用法示例。
在下文中一共展示了LiveAction.add_or_update方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_notify_triggers_end_timestamp_none
# 需要导入模块: from st2common.persistence.liveaction import LiveAction [as 别名]
# 或者: from st2common.persistence.liveaction.LiveAction import add_or_update [as 别名]
def test_notify_triggers_end_timestamp_none(self):
liveaction_db = LiveActionDB(action='core.local')
liveaction_db.id = bson.ObjectId()
liveaction_db.description = ''
liveaction_db.status = 'succeeded'
liveaction_db.parameters = {}
on_success = NotificationSubSchema(message='Action succeeded.')
on_failure = NotificationSubSchema(message='Action failed.')
liveaction_db.notify = NotificationSchema(on_success=on_success,
on_failure=on_failure)
liveaction_db.start_timestamp = date_utils.get_datetime_utc_now()
# This tests for end_timestamp being set to None, which can happen when a policy cancels
# a request.
# The assertions within "MockDispatcher.dispatch" will validate that the underlying code
# handles this properly, so all we need to do is keep the call to "notifier.process" below
liveaction_db.end_timestamp = None
LiveAction.add_or_update(liveaction_db)
execution = MOCK_EXECUTION
execution.liveaction = vars(LiveActionAPI.from_model(liveaction_db))
execution.status = liveaction_db.status
dispatcher = NotifierTestCase.MockDispatcher(self)
notifier = Notifier(connection=None, queues=[], trigger_dispatcher=dispatcher)
notifier.process(execution)
示例2: test_notify_triggers_jinja_patterns
# 需要导入模块: from st2common.persistence.liveaction import LiveAction [as 别名]
# 或者: from st2common.persistence.liveaction.LiveAction import add_or_update [as 别名]
def test_notify_triggers_jinja_patterns(self, dispatch):
liveaction_db = LiveActionDB(action='core.local')
liveaction_db.id = bson.ObjectId()
liveaction_db.description = ''
liveaction_db.status = 'succeeded'
liveaction_db.parameters = {'cmd': 'mamma mia', 'runner_foo': 'foo'}
on_success = NotificationSubSchema(message='Command {{action_parameters.cmd}} succeeded.',
data={'stdout': '{{action_results.stdout}}'})
liveaction_db.notify = NotificationSchema(on_success=on_success)
liveaction_db.start_timestamp = date_utils.get_datetime_utc_now()
liveaction_db.end_timestamp = \
(liveaction_db.start_timestamp + datetime.timedelta(seconds=50))
LiveAction.add_or_update(liveaction_db)
execution = MOCK_EXECUTION
execution.liveaction = vars(LiveActionAPI.from_model(liveaction_db))
execution.status = liveaction_db.status
notifier = Notifier(connection=None, queues=[])
notifier.process(execution)
exp = {'status': 'succeeded',
'start_timestamp': isotime.format(liveaction_db.start_timestamp),
'route': 'notify.default', 'runner_ref': 'local-shell-cmd',
'channel': 'notify.default', 'message': u'Command mamma mia succeeded.',
'data': {'result': '{}', 'stdout': 'stuff happens'},
'action_ref': u'core.local',
'execution_id': str(MOCK_EXECUTION.id),
'end_timestamp': isotime.format(liveaction_db.end_timestamp)}
dispatch.assert_called_once_with('core.st2.generic.notifytrigger', payload=exp,
trace_context={})
notifier.process(execution)
示例3: test_created_temporary_auth_token_is_correctly_scoped_to_user_who_ran_the_action
# 需要导入模块: from st2common.persistence.liveaction import LiveAction [as 别名]
# 或者: from st2common.persistence.liveaction.LiveAction import add_or_update [as 别名]
def test_created_temporary_auth_token_is_correctly_scoped_to_user_who_ran_the_action(self):
params = {
'actionstr': 'bar',
'mock_status': action_constants.LIVEACTION_STATUS_SUCCEEDED
}
global global_runner
global_runner = None
def mock_get_runner(*args, **kwargs):
global global_runner
runner = original_get_runner(*args, **kwargs)
global_runner = runner
return runner
# user joe_1
runner_container = get_runner_container()
original_get_runner = runner_container._get_runner
liveaction_db = self._get_failingaction_exec_db_model(params)
liveaction_db = LiveAction.add_or_update(liveaction_db)
liveaction_db.context = {'user': 'user_joe_1'}
executions.create_execution_object(liveaction_db)
runner_container._get_runner = mock_get_runner
self.assertEqual(getattr(global_runner, 'auth_token', None), None)
runner_container.dispatch(liveaction_db)
self.assertEqual(global_runner.auth_token.user, 'user_joe_1')
self.assertEqual(global_runner.auth_token.metadata['service'], 'actions_container')
runner_container._get_runner = original_get_runner
# user mark_1
global_runner = None
runner_container = get_runner_container()
original_get_runner = runner_container._get_runner
liveaction_db = self._get_failingaction_exec_db_model(params)
liveaction_db = LiveAction.add_or_update(liveaction_db)
liveaction_db.context = {'user': 'user_mark_2'}
executions.create_execution_object(liveaction_db)
original_get_runner = runner_container._get_runner
runner_container._get_runner = mock_get_runner
self.assertEqual(getattr(global_runner, 'auth_token', None), None)
runner_container.dispatch(liveaction_db)
self.assertEqual(global_runner.auth_token.user, 'user_mark_2')
self.assertEqual(global_runner.auth_token.metadata['service'], 'actions_container')
示例4: delete
# 需要导入模块: from st2common.persistence.liveaction import LiveAction [as 别名]
# 或者: from st2common.persistence.liveaction.LiveAction import add_or_update [as 别名]
def delete(self, exec_id):
"""
Stops a single execution.
Handles requests:
DELETE /actionexecutions/<id>
"""
execution_api = self._get_one(id=exec_id)
if not execution_api:
abort(http_client.NOT_FOUND, 'Execution with id %s not found.' % exec_id)
return
liveaction_id = execution_api.liveaction['id']
if not liveaction_id:
abort(http_client.INTERNAL_SERVER_ERROR,
'Execution object missing link to liveaction %s.' % liveaction_id)
try:
liveaction_db = LiveAction.get_by_id(liveaction_id)
except:
abort(http_client.INTERNAL_SERVER_ERROR,
'Execution object missing link to liveaction %s.' % liveaction_id)
return
if liveaction_db.status == LIVEACTION_STATUS_CANCELED:
abort(http_client.OK,
'Action is already in "canceled" state.')
if liveaction_db.status not in CANCELABLE_STATES:
abort(http_client.OK,
'Action cannot be canceled. State = %s.' % liveaction_db.status)
return
liveaction_db.status = 'canceled'
liveaction_db.end_timestamp = date_utils.get_datetime_utc_now()
liveaction_db.result = {'message': 'Action canceled by user.'}
try:
LiveAction.add_or_update(liveaction_db)
except:
LOG.exception('Failed updating status to canceled for liveaction %s.',
liveaction_db.id)
abort(http_client.INTERNAL_SERVER_ERROR, 'Failed canceling execution.')
return
execution_db = execution_service.update_execution(liveaction_db)
from_model_kwargs = self._get_from_model_kwargs_for_request(request=pecan.request)
return ActionExecutionAPI.from_model(execution_db, from_model_kwargs)
示例5: test_processing_when_task_completed
# 需要导入模块: from st2common.persistence.liveaction import LiveAction [as 别名]
# 或者: from st2common.persistence.liveaction.LiveAction import add_or_update [as 别名]
def test_processing_when_task_completed(self, mock_execution_queue_delete, mock_action_service):
self.reset()
liveaction_db = self._create_liveaction_db()
LiveAction.publish_status(liveaction_db)
liveaction_db.status = action_constants.LIVEACTION_STATUS_CANCELED
LiveAction.add_or_update(liveaction_db)
schedule_q_db = self.scheduling_queue._get_next_execution()
scheduling_queue.get_handler()._handle_execution(schedule_q_db)
mock_action_service.update_status.assert_not_called()
mock_execution_queue_delete.assert_called_once()
ActionExecutionSchedulingQueue.delete(schedule_q_db)
示例6: test_state_db_created_for_polling_async_actions
# 需要导入模块: from st2common.persistence.liveaction import LiveAction [as 别名]
# 或者: from st2common.persistence.liveaction.LiveAction import add_or_update [as 别名]
def test_state_db_created_for_polling_async_actions(self):
runner_container = get_runner_container()
params = {
'actionstr': 'foo',
'actionint': 20,
'async_test': True
}
liveaction_db = self._get_liveaction_model(
RunnerContainerTest.polling_async_action_db,
params
)
liveaction_db = LiveAction.add_or_update(liveaction_db)
executions.create_execution_object(liveaction_db)
# Assert that execution ran without exceptions.
runner_container.dispatch(liveaction_db)
states = ActionExecutionState.get_all()
found = [state for state in states if state.execution_id == liveaction_db.id]
self.assertTrue(len(found) > 0, 'There should be a state db object.')
self.assertTrue(len(found) == 1, 'There should only be one state db object.')
self.assertTrue(found[0].query_context is not None)
self.assertTrue(found[0].query_module is not None)
示例7: setup_action_models
# 需要导入模块: from st2common.persistence.liveaction import LiveAction [as 别名]
# 或者: from st2common.persistence.liveaction.LiveAction import add_or_update [as 别名]
def setup_action_models(cls):
pack = 'wolfpack'
name = 'action-1'
parameters = {
'actionint': {'type': 'number', 'default': 10, 'position': 0},
'actionfloat': {'type': 'float', 'required': False, 'position': 1},
'actionstr': {'type': 'string', 'required': True, 'position': 2},
'actionbool': {'type': 'boolean', 'required': False, 'position': 3},
'actionlist': {'type': 'list', 'required': False, 'position': 4},
'actionobject': {'type': 'object', 'required': False, 'position': 5},
'actionnull': {'type': 'null', 'required': False, 'position': 6},
'runnerdummy': {'type': 'string', 'default': 'actiondummy'}
}
action_db = ActionDB(pack=pack, name=name, description='awesomeness',
enabled=True,
ref=ResourceReference(name=name, pack=pack).ref,
entry_point='', runner_type={'name': 'test-runner'},
parameters=parameters)
ActionDBUtilsTestCase.action_db = Action.add_or_update(action_db)
liveaction_db = LiveActionDB()
liveaction_db.status = 'initializing'
liveaction_db.start_timestamp = get_datetime_utc_now()
liveaction_db.action = ActionDBUtilsTestCase.action_db.ref
params = {
'actionstr': 'foo',
'some_key_that_aint_exist_in_action_or_runner': 'bar',
'runnerint': 555
}
liveaction_db.parameters = params
ActionDBUtilsTestCase.liveaction_db = LiveAction.add_or_update(liveaction_db)
示例8: setup_action_models
# 需要导入模块: from st2common.persistence.liveaction import LiveAction [as 别名]
# 或者: from st2common.persistence.liveaction.LiveAction import add_or_update [as 别名]
def setup_action_models(cls):
action_db = ActionDB()
action_db.name = 'action-1'
action_db.description = 'awesomeness'
action_db.enabled = True
action_db.pack = 'wolfpack'
action_db.ref = ResourceReference(name=action_db.name, pack=action_db.pack).ref
action_db.entry_point = ''
action_db.runner_type = {'name': 'test-runner'}
action_db.parameters = {
'actionstr': {'type': 'string', 'position': 1, 'required': True},
'actionint': {'type': 'number', 'default': 10, 'position': 0},
'runnerdummy': {'type': 'string', 'default': 'actiondummy'}
}
ActionDBUtilsTestCase.action_db = Action.add_or_update(action_db)
liveaction_db = LiveActionDB()
liveaction_db.status = 'initializing'
liveaction_db.start_timestamp = get_datetime_utc_now()
liveaction_db.action = ActionDBUtilsTestCase.action_db.ref
params = {
'actionstr': 'foo',
'some_key_that_aint_exist_in_action_or_runner': 'bar',
'runnerint': 555
}
liveaction_db.parameters = params
ActionDBUtilsTestCase.liveaction_db = LiveAction.add_or_update(liveaction_db)
示例9: test_liveaction_gets_deleted
# 需要导入模块: from st2common.persistence.liveaction import LiveAction [as 别名]
# 或者: from st2common.persistence.liveaction.LiveAction import add_or_update [as 别名]
def test_liveaction_gets_deleted(self):
now = date_utils.get_datetime_utc_now()
start_ts = now - timedelta(days=15)
end_ts = now - timedelta(days=14)
liveaction_model = copy.deepcopy(self.models['liveactions']['liveaction4.yaml'])
liveaction_model['start_timestamp'] = start_ts
liveaction_model['end_timestamp'] = end_ts
liveaction_model['status'] = action_constants.LIVEACTION_STATUS_SUCCEEDED
liveaction = LiveAction.add_or_update(liveaction_model)
# Write one execution before cut-off threshold
exec_model = copy.deepcopy(self.models['executions']['execution1.yaml'])
exec_model['start_timestamp'] = start_ts
exec_model['end_timestamp'] = end_ts
exec_model['status'] = action_constants.LIVEACTION_STATUS_SUCCEEDED
exec_model['id'] = bson.ObjectId()
exec_model['liveaction']['id'] = str(liveaction.id)
ActionExecution.add_or_update(exec_model)
liveactions = LiveAction.get_all()
executions = ActionExecution.get_all()
self.assertEqual(len(liveactions), 1)
self.assertEqual(len(executions), 1)
purge_executions(logger=LOG, timestamp=now - timedelta(days=10))
liveactions = LiveAction.get_all()
executions = ActionExecution.get_all()
self.assertEqual(len(executions), 0)
self.assertEqual(len(liveactions), 0)
示例10: _schedule_execution
# 需要导入模块: from st2common.persistence.liveaction import LiveAction [as 别名]
# 或者: from st2common.persistence.liveaction.LiveAction import add_or_update [as 别名]
def _schedule_execution(self, liveaction):
# Initialize execution context if it does not exist.
if not hasattr(liveaction, 'context'):
liveaction.context = dict()
liveaction.context['user'] = get_requester()
LOG.debug('User is: %s' % liveaction.context['user'])
# Retrieve other st2 context from request header.
if 'st2-context' in pecan.request.headers and pecan.request.headers['st2-context']:
context = jsonify.try_loads(pecan.request.headers['st2-context'])
if not isinstance(context, dict):
raise ValueError('Unable to convert st2-context from the headers into JSON.')
liveaction.context.update(context)
# Schedule the action execution.
liveaction_db = LiveActionAPI.to_model(liveaction)
liveaction_db, actionexecution_db = action_service.create_request(liveaction_db)
action_db = action_utils.get_action_by_ref(liveaction_db.action)
runnertype_db = action_utils.get_runnertype_by_name(action_db.runner_type['name'])
try:
liveaction_db.parameters = param_utils.render_live_params(
runnertype_db.runner_parameters, action_db.parameters, liveaction_db.parameters,
liveaction_db.context)
except ParamException as e:
raise ValueValidationException(str(e))
liveaction_db = LiveAction.add_or_update(liveaction_db, publish=False)
_, actionexecution_db = action_service.publish_request(liveaction_db, actionexecution_db)
from_model_kwargs = self._get_from_model_kwargs_for_request(request=pecan.request)
return ActionExecutionAPI.from_model(actionexecution_db, from_model_kwargs)
示例11: test_update_same_liveaction_status
# 需要导入模块: from st2common.persistence.liveaction import LiveAction [as 别名]
# 或者: from st2common.persistence.liveaction.LiveAction import add_or_update [as 别名]
def test_update_same_liveaction_status(self):
liveaction_db = LiveActionDB()
liveaction_db.status = 'requested'
liveaction_db.start_timestamp = get_datetime_utc_now()
liveaction_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
}
liveaction_db.parameters = params
liveaction_db = LiveAction.add_or_update(liveaction_db)
origliveaction_db = copy.copy(liveaction_db)
# Update by id.
newliveaction_db = action_db_utils.update_liveaction_status(
status='requested', liveaction_id=liveaction_db.id)
# Verify id didn't change.
self.assertEqual(origliveaction_db.id, newliveaction_db.id)
self.assertEqual(newliveaction_db.status, 'requested')
# Verify that state is not published.
self.assertFalse(LiveActionPublisher.publish_state.called)
示例12: test_execution_creation_action_triggered_by_rule
# 需要导入模块: from st2common.persistence.liveaction import LiveAction [as 别名]
# 或者: from st2common.persistence.liveaction.LiveAction import add_or_update [as 别名]
def test_execution_creation_action_triggered_by_rule(self):
# Wait for the action execution to complete and then confirm outcome.
trigger_type = self.MODELS['triggertypes']['triggertype2.yaml']
trigger = self.MODELS['triggers']['trigger2.yaml']
trigger_instance = self.MODELS['triggerinstances']['trigger_instance_1.yaml']
test_liveaction = self.FIXTURES['liveactions']['liveaction3.yaml']
rule = self.MODELS['rules']['rule3.yaml']
# Setup LiveAction to point to right rule and trigger_instance.
# XXX: We need support for dynamic fixtures.
test_liveaction['context']['rule']['id'] = str(rule.id)
test_liveaction['context']['trigger_instance']['id'] = str(trigger_instance.id)
test_liveaction_api = LiveActionAPI(**test_liveaction)
test_liveaction = LiveAction.add_or_update(LiveActionAPI.to_model(test_liveaction_api))
liveaction = LiveAction.get(context__trigger_instance__id=str(trigger_instance.id))
self.assertIsNotNone(liveaction)
self.assertEqual(liveaction.status, action_constants.LIVEACTION_STATUS_REQUESTED)
executions_util.create_execution_object(liveaction)
execution = self._get_action_execution(liveaction__id=str(liveaction.id),
raise_exception=True)
self.assertDictEqual(execution.trigger, vars(TriggerAPI.from_model(trigger)))
self.assertDictEqual(execution.trigger_type, vars(TriggerTypeAPI.from_model(trigger_type)))
self.assertDictEqual(execution.trigger_instance,
vars(TriggerInstanceAPI.from_model(trigger_instance)))
self.assertDictEqual(execution.rule, vars(RuleAPI.from_model(rule)))
action = action_utils.get_action_by_ref(liveaction.action)
self.assertDictEqual(execution.action, vars(ActionAPI.from_model(action)))
runner = RunnerType.get_by_name(action.runner_type['name'])
self.assertDictEqual(execution.runner, vars(RunnerTypeAPI.from_model(runner)))
liveaction = LiveAction.get_by_id(str(liveaction.id))
self.assertEquals(execution.liveaction['id'], str(liveaction.id))
示例13: update_liveaction_status
# 需要导入模块: from st2common.persistence.liveaction import LiveAction [as 别名]
# 或者: from st2common.persistence.liveaction.LiveAction import add_or_update [as 别名]
def update_liveaction_status(
status=None,
result=None,
context=None,
end_timestamp=None,
liveaction_id=None,
runner_info=None,
liveaction_db=None,
publish=True,
):
"""
Update the status of the specified LiveAction to the value provided in
new_status.
The LiveAction may be specified using either liveaction_id, or as an
liveaction_db instance.
"""
if (liveaction_id is None) and (liveaction_db is None):
raise ValueError("Must specify an liveaction_id or an liveaction_db when " "calling update_LiveAction_status")
if liveaction_db is None:
liveaction_db = get_liveaction_by_id(liveaction_id)
if status not in LIVEACTION_STATUSES:
raise ValueError(
'Attempting to set status for LiveAction "%s" ' 'to unknown status string. Unknown status is "%s"',
liveaction_db,
status,
)
extra = {"liveaction_db": liveaction_db}
LOG.debug('Updating ActionExection: "%s" with status="%s"', liveaction_db.id, status, extra=extra)
old_status = liveaction_db.status
liveaction_db.status = status
if result:
liveaction_db.result = result
if context:
liveaction_db.context.update(context)
if end_timestamp:
liveaction_db.end_timestamp = end_timestamp
if runner_info:
liveaction_db.runner_info = runner_info
liveaction_db = LiveAction.add_or_update(liveaction_db)
LOG.debug("Updated status for LiveAction object.", extra=extra)
if publish and status != old_status:
LiveAction.publish_status(liveaction_db)
LOG.debug("Published status for LiveAction object.", extra=extra)
return liveaction_db
示例14: _create_liveaction_db
# 需要导入模块: from st2common.persistence.liveaction import LiveAction [as 别名]
# 或者: from st2common.persistence.liveaction.LiveAction import add_or_update [as 别名]
def _create_liveaction_db(self, status=action_constants.LIVEACTION_STATUS_REQUESTED):
action_ref = 'wolfpack.action-1'
parameters = {'actionstr': 'fu'}
liveaction_db = LiveActionDB(action=action_ref, parameters=parameters, status=status)
liveaction_db = LiveAction.add_or_update(liveaction_db)
execution_service.create_execution_object(liveaction_db, publish=False)
return liveaction_db
示例15: _create_inquiry
# 需要导入模块: from st2common.persistence.liveaction import LiveAction [as 别名]
# 或者: from st2common.persistence.liveaction.LiveAction import add_or_update [as 别名]
def _create_inquiry(self, ttl, timestamp):
action_db = self.models['actions']['ask.yaml']
liveaction_db = LiveActionDB()
liveaction_db.status = action_constants.LIVEACTION_STATUS_PENDING
liveaction_db.start_timestamp = timestamp
liveaction_db.action = ResourceReference(name=action_db.name, pack=action_db.pack).ref
liveaction_db.result = {'ttl': ttl}
liveaction_db = LiveAction.add_or_update(liveaction_db)
executions.create_execution_object(liveaction_db)