|
1
|
|
|
# Licensed to the StackStorm, Inc ('StackStorm') under one or more |
|
2
|
|
|
# contributor license agreements. See the NOTICE file distributed with |
|
3
|
|
|
# this work for additional information regarding copyright ownership. |
|
4
|
|
|
# The ASF licenses this file to You under the Apache License, Version 2.0 |
|
5
|
|
|
# (the "License"); you may not use this file except in compliance with |
|
6
|
|
|
# the License. You may obtain a copy of the License at |
|
7
|
|
|
# |
|
8
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0 |
|
9
|
|
|
# |
|
10
|
|
|
# Unless required by applicable law or agreed to in writing, software |
|
11
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, |
|
12
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13
|
|
|
# See the License for the specific language governing permissions and |
|
14
|
|
|
# limitations under the License. |
|
15
|
|
|
|
|
16
|
|
|
import six |
|
17
|
|
|
|
|
18
|
|
|
from st2common import log as logging |
|
19
|
|
|
from st2common.constants import action as action_constants |
|
20
|
|
|
from st2common.exceptions.db import StackStormDBObjectNotFoundError |
|
21
|
|
|
from st2common.exceptions.trace import TraceNotFoundException |
|
22
|
|
|
from st2common.persistence.liveaction import LiveAction |
|
23
|
|
|
from st2common.persistence.execution import ActionExecution |
|
24
|
|
|
from st2common.services import executions |
|
25
|
|
|
from st2common.services import trace as trace_service |
|
|
|
|
|
|
26
|
|
|
from st2common.util import date as date_utils |
|
27
|
|
|
from st2common.util import action_db as action_utils |
|
28
|
|
|
from st2common.util import schema as util_schema |
|
29
|
|
|
|
|
30
|
|
|
|
|
31
|
|
|
__all__ = [ |
|
32
|
|
|
'request', |
|
33
|
|
|
'create_request', |
|
34
|
|
|
'publish_request', |
|
35
|
|
|
'is_action_canceled_or_canceling' |
|
36
|
|
|
] |
|
37
|
|
|
|
|
38
|
|
|
LOG = logging.getLogger(__name__) |
|
39
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
def _get_immutable_params(parameters): |
|
42
|
|
|
if not parameters: |
|
43
|
|
|
return [] |
|
44
|
|
|
return [k for k, v in six.iteritems(parameters) if v.get('immutable', False)] |
|
45
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
def create_request(liveaction): |
|
48
|
|
|
""" |
|
49
|
|
|
Create an action execution. |
|
50
|
|
|
|
|
51
|
|
|
:return: (liveaction, execution) |
|
52
|
|
|
:rtype: tuple |
|
53
|
|
|
""" |
|
54
|
|
|
# Use the user context from the parent action execution. Subtasks in a workflow |
|
55
|
|
|
# action can be invoked by a system user and so we want to use the user context |
|
56
|
|
|
# from the original workflow action. |
|
57
|
|
|
parent_context = executions.get_parent_context(liveaction) |
|
58
|
|
|
if parent_context: |
|
59
|
|
|
parent_user = parent_context.get('user', None) |
|
60
|
|
|
if parent_user: |
|
61
|
|
|
liveaction.context['user'] = parent_user |
|
62
|
|
|
|
|
63
|
|
|
# Validate action. |
|
64
|
|
|
action_db = action_utils.get_action_by_ref(liveaction.action) |
|
65
|
|
|
if not action_db: |
|
66
|
|
|
raise ValueError('Action "%s" cannot be found.' % liveaction.action) |
|
67
|
|
|
if not action_db.enabled: |
|
68
|
|
|
raise ValueError('Unable to execute. Action "%s" is disabled.' % liveaction.action) |
|
69
|
|
|
|
|
70
|
|
|
runnertype_db = action_utils.get_runnertype_by_name(action_db.runner_type['name']) |
|
71
|
|
|
|
|
72
|
|
|
if not hasattr(liveaction, 'parameters'): |
|
73
|
|
|
liveaction.parameters = dict() |
|
74
|
|
|
|
|
75
|
|
|
# Validate action parameters. |
|
76
|
|
|
schema = util_schema.get_schema_for_action_parameters(action_db) |
|
77
|
|
|
validator = util_schema.get_validator() |
|
78
|
|
|
util_schema.validate(liveaction.parameters, schema, validator, use_default=True, |
|
79
|
|
|
allow_default_none=True) |
|
80
|
|
|
|
|
81
|
|
|
# validate that no immutable params are being overriden. Although possible to |
|
82
|
|
|
# ignore the override it is safer to inform the user to avoid surprises. |
|
83
|
|
|
immutables = _get_immutable_params(action_db.parameters) |
|
84
|
|
|
immutables.extend(_get_immutable_params(runnertype_db.runner_parameters)) |
|
85
|
|
|
overridden_immutables = [p for p in six.iterkeys(liveaction.parameters) if p in immutables] |
|
86
|
|
|
if len(overridden_immutables) > 0: |
|
87
|
|
|
raise ValueError('Override of immutable parameter(s) %s is unsupported.' |
|
88
|
|
|
% str(overridden_immutables)) |
|
89
|
|
|
|
|
90
|
|
|
# Set notification settings for action. |
|
91
|
|
|
# XXX: There are cases when we don't want notifications to be sent for a particular |
|
92
|
|
|
# execution. So we should look at liveaction.parameters['notify'] |
|
93
|
|
|
# and not set liveaction.notify. |
|
94
|
|
|
if not _is_notify_empty(action_db.notify): |
|
95
|
|
|
liveaction.notify = action_db.notify |
|
96
|
|
|
|
|
97
|
|
|
# Write to database and send to message queue. |
|
98
|
|
|
liveaction.status = action_constants.LIVEACTION_STATUS_REQUESTED |
|
99
|
|
|
liveaction.start_timestamp = date_utils.get_datetime_utc_now() |
|
100
|
|
|
|
|
101
|
|
|
# Publish creation after both liveaction and actionexecution are created. |
|
102
|
|
|
liveaction = LiveAction.add_or_update(liveaction, publish=False) |
|
103
|
|
|
|
|
104
|
|
|
# Get trace_db if it exists. This could throw. If it throws, we have to cleanup |
|
105
|
|
|
# liveaction object so we don't see things in requested mode. |
|
106
|
|
|
trace_db = None |
|
107
|
|
|
try: |
|
108
|
|
|
_, trace_db = trace_service.get_trace_db_by_live_action(liveaction) |
|
109
|
|
|
except StackStormDBObjectNotFoundError as e: |
|
110
|
|
|
_cleanup_liveaction(liveaction) |
|
111
|
|
|
raise TraceNotFoundException(str(e)) |
|
112
|
|
|
|
|
113
|
|
|
execution = executions.create_execution_object(liveaction, publish=False) |
|
114
|
|
|
|
|
115
|
|
|
if trace_db: |
|
116
|
|
|
trace_service.add_or_update_given_trace_db( |
|
117
|
|
|
trace_db=trace_db, |
|
118
|
|
|
action_executions=[ |
|
119
|
|
|
trace_service.get_trace_component_for_action_execution(execution, liveaction) |
|
120
|
|
|
]) |
|
121
|
|
|
|
|
122
|
|
|
return liveaction, execution |
|
123
|
|
|
|
|
124
|
|
|
|
|
125
|
|
|
def publish_request(liveaction, execution): |
|
126
|
|
|
""" |
|
127
|
|
|
Publish an action execution. |
|
128
|
|
|
|
|
129
|
|
|
:return: (liveaction, execution) |
|
130
|
|
|
:rtype: tuple |
|
131
|
|
|
""" |
|
132
|
|
|
# Assume that this is a creation. |
|
133
|
|
|
LiveAction.publish_create(liveaction) |
|
134
|
|
|
LiveAction.publish_status(liveaction) |
|
135
|
|
|
ActionExecution.publish_create(execution) |
|
136
|
|
|
|
|
137
|
|
|
extra = {'liveaction_db': liveaction, 'execution_db': execution} |
|
138
|
|
|
LOG.audit('Action execution requested. LiveAction.id=%s, ActionExecution.id=%s' % |
|
139
|
|
|
(liveaction.id, execution.id), extra=extra) |
|
140
|
|
|
|
|
141
|
|
|
return liveaction, execution |
|
142
|
|
|
|
|
143
|
|
|
|
|
144
|
|
|
def request(liveaction): |
|
145
|
|
|
liveaction, execution = create_request(liveaction) |
|
146
|
|
|
liveaction, execution = publish_request(liveaction, execution) |
|
147
|
|
|
|
|
148
|
|
|
return liveaction, execution |
|
149
|
|
|
|
|
150
|
|
|
|
|
151
|
|
|
def update_status(liveaction, new_status, result=None, publish=True): |
|
152
|
|
|
if liveaction.status == new_status: |
|
153
|
|
|
return liveaction |
|
154
|
|
|
|
|
155
|
|
|
old_status = liveaction.status |
|
156
|
|
|
|
|
157
|
|
|
liveaction = action_utils.update_liveaction_status( |
|
158
|
|
|
status=new_status, result=result, liveaction_id=liveaction.id, publish=False) |
|
159
|
|
|
|
|
160
|
|
|
action_execution = executions.update_execution(liveaction) |
|
161
|
|
|
|
|
162
|
|
|
msg = ('The status of action execution is changed from %s to %s. ' |
|
163
|
|
|
'<LiveAction.id=%s, ActionExecution.id=%s>' % (old_status, |
|
164
|
|
|
new_status, liveaction.id, action_execution.id)) |
|
165
|
|
|
|
|
166
|
|
|
extra = { |
|
167
|
|
|
'action_execution_db': action_execution, |
|
168
|
|
|
'liveaction_db': liveaction |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
LOG.audit(msg, extra=extra) |
|
172
|
|
|
LOG.info(msg) |
|
173
|
|
|
|
|
174
|
|
|
if publish: |
|
175
|
|
|
LiveAction.publish_status(liveaction) |
|
176
|
|
|
|
|
177
|
|
|
return liveaction |
|
178
|
|
|
|
|
179
|
|
|
|
|
180
|
|
|
def is_action_canceled_or_canceling(liveaction_id): |
|
181
|
|
|
liveaction_db = action_utils.get_liveaction_by_id(liveaction_id) |
|
182
|
|
|
return liveaction_db.status in [action_constants.LIVEACTION_STATUS_CANCELED, |
|
183
|
|
|
action_constants.LIVEACTION_STATUS_CANCELING] |
|
184
|
|
|
|
|
185
|
|
|
|
|
186
|
|
|
def request_cancellation(liveaction, requester): |
|
187
|
|
|
""" |
|
188
|
|
|
Request cancellation of an action execution. |
|
189
|
|
|
|
|
190
|
|
|
:return: (liveaction, execution) |
|
191
|
|
|
:rtype: tuple |
|
192
|
|
|
""" |
|
193
|
|
|
if liveaction.status == action_constants.LIVEACTION_STATUS_CANCELING: |
|
194
|
|
|
return liveaction |
|
195
|
|
|
|
|
196
|
|
|
if liveaction.status not in action_constants.LIVEACTION_CANCELABLE_STATES: |
|
197
|
|
|
raise Exception('Unable to cancel execution because it is already in a completed state.') |
|
198
|
|
|
|
|
199
|
|
|
result = { |
|
200
|
|
|
'message': 'Action canceled by user.', |
|
201
|
|
|
'user': requester |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
# There is real work only when liveaction is still running. |
|
205
|
|
|
status = (action_constants.LIVEACTION_STATUS_CANCELING |
|
206
|
|
|
if liveaction.status == action_constants.LIVEACTION_STATUS_RUNNING |
|
207
|
|
|
else action_constants.LIVEACTION_STATUS_CANCELED) |
|
208
|
|
|
|
|
209
|
|
|
update_status(liveaction, status, result=result) |
|
210
|
|
|
|
|
211
|
|
|
execution = ActionExecution.get(liveaction__id=str(liveaction.id)) |
|
212
|
|
|
|
|
213
|
|
|
return (liveaction, execution) |
|
214
|
|
|
|
|
215
|
|
|
|
|
216
|
|
|
def _cleanup_liveaction(liveaction): |
|
217
|
|
|
try: |
|
218
|
|
|
LiveAction.delete(liveaction) |
|
219
|
|
|
except: |
|
220
|
|
|
LOG.exception('Failed cleaning up LiveAction: %s.', liveaction) |
|
221
|
|
|
pass |
|
|
|
|
|
|
222
|
|
|
|
|
223
|
|
|
|
|
224
|
|
|
def _is_notify_empty(notify_db): |
|
225
|
|
|
""" |
|
226
|
|
|
notify_db is considered to be empty if notify_db is None and neither |
|
227
|
|
|
of on_complete, on_success and on_failure have values. |
|
228
|
|
|
""" |
|
229
|
|
|
if not notify_db: |
|
230
|
|
|
return True |
|
231
|
|
|
return not (notify_db.on_complete or notify_db.on_success or notify_db.on_failure) |
|
232
|
|
|
|