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 abc |
17
|
|
|
import pkgutil |
18
|
|
|
|
19
|
|
|
import six |
20
|
|
|
import yaml |
21
|
|
|
from oslo_config import cfg |
22
|
|
|
|
23
|
|
|
from st2common import log as logging |
24
|
|
|
from st2common.constants import action as action_constants |
25
|
|
|
from st2common.constants import pack as pack_constants |
26
|
|
|
from st2common.exceptions.actionrunner import ActionRunnerCreateError |
27
|
|
|
from st2common.util import action_db as action_utils |
28
|
|
|
from st2common.util.loader import register_runner, register_callback_module |
29
|
|
|
from st2common.util.api import get_full_public_api_url |
30
|
|
|
from st2common.util.deprecation import deprecated |
31
|
|
|
|
32
|
|
|
__all__ = [ |
33
|
|
|
'ActionRunner', |
34
|
|
|
'AsyncActionRunner', |
35
|
|
|
'ShellRunnerMixin', |
36
|
|
|
'get_runner', |
37
|
|
|
'get_metadata' |
38
|
|
|
] |
39
|
|
|
|
40
|
|
|
|
41
|
|
|
LOG = logging.getLogger(__name__) |
42
|
|
|
|
43
|
|
|
# constants to lookup in runner_parameters |
44
|
|
|
RUNNER_COMMAND = 'cmd' |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
def get_runner(module_name, config=None): |
48
|
|
|
""" |
49
|
|
|
Load the module and return an instance of the runner. |
50
|
|
|
""" |
51
|
|
|
|
52
|
|
|
LOG.debug('Runner loading python module: %s', module_name) |
53
|
|
|
try: |
54
|
|
|
# TODO: Explore modifying this to support register_plugin |
55
|
|
|
module = register_runner(module_name) |
56
|
|
|
except Exception as e: |
57
|
|
|
LOG.exception('Failed to import module %s.', module_name) |
58
|
|
|
raise ActionRunnerCreateError(e) |
59
|
|
|
|
60
|
|
|
LOG.debug('Instance of runner module: %s', module) |
61
|
|
|
|
62
|
|
|
if config: |
63
|
|
|
runner_kwargs = {'config': config} |
64
|
|
|
else: |
65
|
|
|
runner_kwargs = {} |
66
|
|
|
|
67
|
|
|
runner = module.get_runner(**runner_kwargs) |
68
|
|
|
LOG.debug('Instance of runner: %s', runner) |
69
|
|
|
return runner |
70
|
|
|
|
71
|
|
|
|
72
|
|
|
def get_metadata(package_name): |
73
|
|
|
""" |
74
|
|
|
Return runner related metadata for the provided runner package name. |
75
|
|
|
|
76
|
|
|
:rtype: ``list`` of ``dict`` |
77
|
|
|
""" |
78
|
|
|
file_path = pkgutil.get_data(package_name, 'metadata/runner.yaml') |
79
|
|
|
metadata = yaml.safe_load(file_path) |
80
|
|
|
return metadata |
81
|
|
|
|
82
|
|
|
|
83
|
|
|
@six.add_metaclass(abc.ABCMeta) |
84
|
|
|
class ActionRunner(object): |
85
|
|
|
""" |
86
|
|
|
The interface that must be implemented by each StackStorm |
87
|
|
|
Action Runner implementation. |
88
|
|
|
""" |
89
|
|
|
|
90
|
|
|
def __init__(self, runner_id): |
91
|
|
|
""" |
92
|
|
|
:param id: Runner id. |
93
|
|
|
:type id: ``str`` |
94
|
|
|
""" |
95
|
|
|
self.runner_id = runner_id |
96
|
|
|
|
97
|
|
|
self.runner_type_db = None |
98
|
|
|
self.runner_parameters = None |
99
|
|
|
self.action = None |
100
|
|
|
self.action_name = None |
101
|
|
|
self.liveaction = None |
102
|
|
|
self.liveaction_id = None |
103
|
|
|
self.execution = None |
104
|
|
|
self.execution_id = None |
105
|
|
|
self.entry_point = None |
106
|
|
|
self.libs_dir_path = None |
107
|
|
|
self.context = None |
108
|
|
|
self.callback = None |
109
|
|
|
self.auth_token = None |
110
|
|
|
self.rerun_ex_ref = None |
111
|
|
|
|
112
|
|
|
def pre_run(self): |
113
|
|
|
runner_enabled = getattr(self.runner_type_db, 'enabled', True) |
114
|
|
|
runner_name = getattr(self.runner_type_db, 'name', 'unknown') |
115
|
|
|
if not runner_enabled: |
116
|
|
|
msg = ('Runner "%s" has been disabled by the administrator' % |
117
|
|
|
(runner_name)) |
118
|
|
|
raise ValueError(msg) |
119
|
|
|
|
120
|
|
|
# Run will need to take an action argument |
121
|
|
|
# Run may need result data argument |
122
|
|
|
@abc.abstractmethod |
123
|
|
|
def run(self, action_parameters): |
124
|
|
|
raise NotImplementedError() |
125
|
|
|
|
126
|
|
|
def pause(self): |
127
|
|
|
runner_name = getattr(self.runner_type_db, 'name', 'unknown') |
128
|
|
|
raise NotImplementedError('Pause is not supported for runner %s.' % runner_name) |
129
|
|
|
|
130
|
|
|
def resume(self): |
131
|
|
|
runner_name = getattr(self.runner_type_db, 'name', 'unknown') |
132
|
|
|
raise NotImplementedError('Resume is not supported for runner %s.' % runner_name) |
133
|
|
|
|
134
|
|
|
def cancel(self): |
135
|
|
|
return ( |
136
|
|
|
action_constants.LIVEACTION_STATUS_CANCELED, |
137
|
|
|
self.liveaction.result, |
138
|
|
|
self.liveaction.context |
139
|
|
|
) |
140
|
|
|
|
141
|
|
|
def post_run(self, status, result): |
142
|
|
|
callback = self.callback or {} |
143
|
|
|
|
144
|
|
|
if callback and not (set(['url', 'source']) - set(callback.keys())): |
|
|
|
|
145
|
|
|
callback_url = callback['url'] |
146
|
|
|
callback_module_name = callback['source'] |
147
|
|
|
|
148
|
|
|
try: |
149
|
|
|
callback_module = register_callback_module(callback_module_name) |
150
|
|
|
except: |
151
|
|
|
LOG.exception('Failed importing callback module: %s', callback_module_name) |
152
|
|
|
|
153
|
|
|
callback_handler = callback_module.get_instance() |
154
|
|
|
|
155
|
|
|
callback_handler.callback( |
156
|
|
|
callback_url, |
157
|
|
|
self.context, |
158
|
|
|
status, |
159
|
|
|
result |
160
|
|
|
) |
161
|
|
|
|
162
|
|
|
@deprecated |
163
|
|
|
def get_pack_name(self): |
164
|
|
|
return self.get_pack_ref() |
165
|
|
|
|
166
|
|
|
def get_pack_ref(self): |
167
|
|
|
""" |
168
|
|
|
Retrieve pack name for the action which is being currently executed. |
169
|
|
|
|
170
|
|
|
:rtype: ``str`` |
171
|
|
|
""" |
172
|
|
|
if self.action: |
173
|
|
|
return self.action.pack |
174
|
|
|
|
175
|
|
|
return pack_constants.DEFAULT_PACK_NAME |
176
|
|
|
|
177
|
|
|
def get_user(self): |
178
|
|
|
""" |
179
|
|
|
Retrieve a name of the user which triggered this action execution. |
180
|
|
|
|
181
|
|
|
:rtype: ``str`` |
182
|
|
|
""" |
183
|
|
|
context = getattr(self, 'context', {}) or {} |
184
|
|
|
user = context.get('user', cfg.CONF.system_user.user) |
185
|
|
|
|
186
|
|
|
return user |
187
|
|
|
|
188
|
|
|
def _get_common_action_env_variables(self): |
189
|
|
|
""" |
190
|
|
|
Retrieve common ST2_ACTION_ environment variables which will be available to the action. |
191
|
|
|
|
192
|
|
|
Note: Environment variables are prefixed with ST2_ACTION_* so they don't clash with CLI |
193
|
|
|
environment variables. |
194
|
|
|
|
195
|
|
|
:rtype: ``dict`` |
196
|
|
|
""" |
197
|
|
|
result = {} |
198
|
|
|
result['ST2_ACTION_PACK_NAME'] = self.get_pack_ref() |
199
|
|
|
result['ST2_ACTION_EXECUTION_ID'] = str(self.execution_id) |
200
|
|
|
result['ST2_ACTION_API_URL'] = get_full_public_api_url() |
201
|
|
|
|
202
|
|
|
if self.auth_token: |
203
|
|
|
result['ST2_ACTION_AUTH_TOKEN'] = self.auth_token.token |
204
|
|
|
|
205
|
|
|
return result |
206
|
|
|
|
207
|
|
|
def __str__(self): |
208
|
|
|
attrs = ', '.join(['%s=%s' % (k, v) for k, v in six.iteritems(self.__dict__)]) |
209
|
|
|
return '%s@%s(%s)' % (self.__class__.__name__, str(id(self)), attrs) |
210
|
|
|
|
211
|
|
|
|
212
|
|
|
@six.add_metaclass(abc.ABCMeta) |
213
|
|
|
class AsyncActionRunner(ActionRunner): |
214
|
|
|
pass |
215
|
|
|
|
216
|
|
|
|
217
|
|
|
class ShellRunnerMixin(object): |
218
|
|
|
""" |
219
|
|
|
Class which contains utility functions to be used by shell runners. |
220
|
|
|
""" |
221
|
|
|
|
222
|
|
|
def _transform_named_args(self, named_args): |
223
|
|
|
""" |
224
|
|
|
Transform named arguments to the final form. |
225
|
|
|
|
226
|
|
|
:param named_args: Named arguments. |
227
|
|
|
:type named_args: ``dict`` |
228
|
|
|
|
229
|
|
|
:rtype: ``dict`` |
230
|
|
|
""" |
231
|
|
|
if named_args: |
232
|
|
|
return {self._kwarg_op + k: v for (k, v) in six.iteritems(named_args)} |
233
|
|
|
return None |
234
|
|
|
|
235
|
|
|
def _get_script_args(self, action_parameters): |
236
|
|
|
""" |
237
|
|
|
:param action_parameters: Action parameters. |
238
|
|
|
:type action_parameters: ``dict`` |
239
|
|
|
|
240
|
|
|
:return: (positional_args, named_args) |
241
|
|
|
:rtype: (``str``, ``dict``) |
242
|
|
|
""" |
243
|
|
|
# TODO: return list for positional args, command classes should escape it |
244
|
|
|
# and convert it to string |
245
|
|
|
|
246
|
|
|
is_script_run_as_cmd = self.runner_parameters.get(RUNNER_COMMAND, None) |
247
|
|
|
|
248
|
|
|
pos_args = '' |
249
|
|
|
named_args = {} |
250
|
|
|
|
251
|
|
|
if is_script_run_as_cmd: |
252
|
|
|
pos_args = self.runner_parameters.get(RUNNER_COMMAND, '') |
253
|
|
|
named_args = action_parameters |
254
|
|
|
else: |
255
|
|
|
pos_args, named_args = action_utils.get_args(action_parameters, self.action) |
256
|
|
|
|
257
|
|
|
return pos_args, named_args |
258
|
|
|
|