Skip to content

Commit 289d9f0

Browse files
authored
Merge pull request #4531 from StackStorm/fix_orquesta_config_context_bug
Fix a bug with "{{ config_text.foo }}" not working for executions scheduled via Orquesta workflow
2 parents c4ea71b + 66cce62 commit 289d9f0

File tree

10 files changed

+93
-10
lines changed

10 files changed

+93
-10
lines changed

contrib/runners/orquesta_runner/orquesta_runner/orquesta_runner.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ def _construct_st2_context(self):
6868
'st2': {
6969
'action_execution_id': str(self.execution.id),
7070
'api_url': api_util.get_full_public_api_url(),
71-
'user': self.execution.context.get('user', cfg.CONF.system_user.user)
71+
'user': self.execution.context.get('user', cfg.CONF.system_user.user),
72+
'pack': self.execution.context.get('pack', None)
7273
}
7374
}
7475

contrib/runners/orquesta_runner/tests/unit/test_basic.py

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,11 @@ def test_run_workflow(self):
138138
'workflow_execution_id': str(wf_ex_db.id),
139139
'action_execution_id': str(ac_ex_db.id),
140140
'api_url': 'http://127.0.0.1/v1',
141-
'user': username
141+
'user': username,
142+
'pack': 'orquesta_tests'
143+
},
144+
'parent': {
145+
'pack': 'orquesta_tests'
142146
}
143147
}
144148

@@ -294,6 +298,40 @@ def test_run_workflow_with_unicode_input(self):
294298
self.assertDictEqual(lv_ac_db.result, expected_result)
295299
self.assertDictEqual(ac_ex_db.result, expected_result)
296300

301+
def test_run_workflow_action_config_context(self):
302+
wf_meta = base.get_wf_fixture_meta_data(TEST_PACK_PATH, 'config-context.yaml')
303+
wf_input = {}
304+
lv_ac_db = lv_db_models.LiveActionDB(action=wf_meta['name'], parameters=wf_input)
305+
lv_ac_db, ac_ex_db = ac_svc.request(lv_ac_db)
306+
307+
# Assert action execution is running.
308+
lv_ac_db = lv_db_access.LiveAction.get_by_id(str(lv_ac_db.id))
309+
self.assertEqual(lv_ac_db.status, ac_const.LIVEACTION_STATUS_RUNNING, lv_ac_db.result)
310+
wf_ex_db = wf_db_access.WorkflowExecution.query(action_execution=str(ac_ex_db.id))[0]
311+
self.assertEqual(wf_ex_db.status, ac_const.LIVEACTION_STATUS_RUNNING)
312+
313+
# Assert task1 is already completed.
314+
query_filters = {'workflow_execution': str(wf_ex_db.id), 'task_id': 'task1'}
315+
tk1_ex_db = wf_db_access.TaskExecution.query(**query_filters)[0]
316+
tk1_ac_ex_db = ex_db_access.ActionExecution.query(task_execution=str(tk1_ex_db.id))[0]
317+
tk1_lv_ac_db = lv_db_access.LiveAction.get_by_id(tk1_ac_ex_db.liveaction['id'])
318+
self.assertEqual(tk1_lv_ac_db.status, ac_const.LIVEACTION_STATUS_SUCCEEDED)
319+
self.assertTrue(wf_svc.is_action_execution_under_workflow_context(tk1_ac_ex_db))
320+
321+
# Manually handle action execution completion.
322+
wf_svc.handle_action_execution_completion(tk1_ac_ex_db)
323+
324+
# Assert workflow is completed.
325+
wf_ex_db = wf_db_access.WorkflowExecution.get_by_id(wf_ex_db.id)
326+
self.assertEqual(wf_ex_db.status, wf_states.SUCCEEDED)
327+
lv_ac_db = lv_db_access.LiveAction.get_by_id(str(lv_ac_db.id))
328+
self.assertEqual(lv_ac_db.status, ac_const.LIVEACTION_STATUS_SUCCEEDED)
329+
ac_ex_db = ex_db_access.ActionExecution.get_by_id(str(ac_ex_db.id))
330+
self.assertEqual(ac_ex_db.status, ac_const.LIVEACTION_STATUS_SUCCEEDED)
331+
332+
# Verify config_context works
333+
self.assertEqual(wf_ex_db.output, {'msg': 'value of config key a'})
334+
297335
def test_run_workflow_with_action_less_tasks(self):
298336
wf_meta = base.get_wf_fixture_meta_data(TEST_PACK_PATH, 'action-less-tasks.yaml')
299337
wf_input = {'name': 'Thanos'}

contrib/runners/orquesta_runner/tests/unit/test_context.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,8 @@ def test_runtime_context(self):
115115
expected_st2_ctx = {
116116
'action_execution_id': str(ac_ex_db.id),
117117
'api_url': 'http://127.0.0.1/v1',
118-
'user': 'stanley'
118+
'user': 'stanley',
119+
'pack': 'orquesta_tests'
119120
}
120121

121122
expected_st2_ctx_with_wf_ex_id = copy.deepcopy(expected_st2_ctx)

st2common/st2common/services/action.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,11 @@ def create_request(liveaction, action_db=None, runnertype_db=None):
7676
# Use the user context from the parent action execution. Subtasks in a workflow
7777
# action can be invoked by a system user and so we want to use the user context
7878
# from the original workflow action.
79-
parent_context = executions.get_parent_context(liveaction)
80-
if parent_context:
81-
parent_user = parent_context.get('user', None)
82-
if parent_user:
83-
liveaction.context['user'] = parent_user
79+
parent_context = executions.get_parent_context(liveaction) or {}
80+
parent_user = parent_context.get('user', None)
81+
82+
if parent_user:
83+
liveaction.context['user'] = parent_user
8484

8585
# Validate action
8686
if not action_db:
@@ -97,6 +97,9 @@ def create_request(liveaction, action_db=None, runnertype_db=None):
9797
if not hasattr(liveaction, 'parameters'):
9898
liveaction.parameters = dict()
9999

100+
# For consistency add pack to the context here in addition to RunnerContainer.dispatch() method
101+
liveaction.context['pack'] = action_db.pack
102+
100103
# Validate action parameters.
101104
schema = util_schema.get_schema_for_action_parameters(action_db, runnertype_db)
102105
validator = util_schema.get_validator()

st2common/st2common/services/workflows.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,7 @@ def request_action_execution(wf_ex_db, task_ex_db, st2_ctx, ac_ex_req, delay=Non
537537

538538
# Set context for the action execution.
539539
ac_ex_ctx = {
540+
'pack': st2_ctx.get('pack'),
540541
'user': st2_ctx.get('user'),
541542
'parent': st2_ctx,
542543
'orquesta': {
@@ -887,7 +888,11 @@ def request_next_tasks(wf_ex_db, task_ex_id=None):
887888

888889
# Pass down appropriate st2 context to the task and action execution(s).
889890
root_st2_ctx = wf_ex_db.context.get('st2', {})
890-
st2_ctx = {'execution_id': wf_ac_ex_id, 'user': root_st2_ctx.get('user')}
891+
st2_ctx = {
892+
'execution_id': wf_ac_ex_id,
893+
'user': root_st2_ctx.get('user'),
894+
'pack': root_st2_ctx.get('pack')
895+
}
891896
if root_st2_ctx.get('api_user'):
892897
st2_ctx['api_user'] = root_st2_ctx.get('api_user')
893898

st2common/st2common/util/config_loader.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ def _get_datastore_value_for_expression(self, key, value, config_schema_item=Non
224224
def get_config(pack, user):
225225
"""Returns config for given pack and user.
226226
"""
227-
LOG.debug('Attempting to get config')
227+
LOG.debug('Attempting to get config for pack "%s" and user "%s"' % (pack, user))
228228
if pack and user:
229229
LOG.debug('Pack and user found. Loading config.')
230230
config_loader = ContentPackConfigLoader(
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
name: "config-context-action"
3+
runner_type: "local-shell-cmd"
4+
enabled: true
5+
entry_point: ""
6+
parameters:
7+
cmd:
8+
immutable: true
9+
default: "echo \"{{ config_context.config_key_a }}\""
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
name: config-context
3+
description: Workflow which tests {{ config_context.foo }} notation works default parameter values for workflow actions.
4+
pack: orquesta_tests
5+
runner_type: orquesta
6+
entry_point: workflows/config-context.yaml
7+
enabled: true
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
version: 1.0
2+
3+
description: Workflow which tests {{ config_context }} functionality.
4+
5+
output:
6+
- msg: <% ctx().message %>
7+
8+
tasks:
9+
task1:
10+
action: orquesta_tests.config-context-action
11+
next:
12+
- when: <% succeeded() %>
13+
publish: message=<% result().stdout %>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
config_key_a:
3+
description: "Sample config key."
4+
type: "string"
5+
default: "value of config key a"
6+
required: true

0 commit comments

Comments
 (0)