Passed
Pull Request — master (#4000)
by W
05:55
created

PollingAsyncActionRunner   A

Complexity

Total Complexity 0

Size/Duplication

Total Lines 3
Duplicated Lines 0 %

Importance

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