Passed
Push — develop ( 516817...4f2796 )
by
unknown
07:22 queued 03:43
created

get_metadata()   A

Complexity

Conditions 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 14
Bugs 0 Features 14
Metric Value
cc 1
c 14
b 0
f 14
dl 0
loc 9
rs 9.6666
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
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())):
0 ignored issues
show
Unused Code Coding Style introduced by
There is an unnecessary parenthesis after not.
Loading history...
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
    def get_pack_name(self):
163
        """
164
        Retrieve pack name for the action which is being currently executed.
165
166
        :rtype: ``str``
167
        """
168
        if self.action:
169
            return self.action.pack
170
171
        return pack_constants.DEFAULT_PACK_NAME
172
173
    def get_user(self):
174
        """
175
        Retrieve a name of the user which triggered this action execution.
176
177
        :rtype: ``str``
178
        """
179
        context = getattr(self, 'context', {}) or {}
180
        user = context.get('user', cfg.CONF.system_user.user)
181
182
        return user
183
184
    def _get_common_action_env_variables(self):
185
        """
186
        Retrieve common ST2_ACTION_ environment variables which will be available to the action.
187
188
        Note: Environment variables are prefixed with ST2_ACTION_* so they don't clash with CLI
189
        environment variables.
190
191
        :rtype: ``dict``
192
        """
193
        result = {}
194
        result['ST2_ACTION_PACK_NAME'] = self.get_pack_name()
195
        result['ST2_ACTION_EXECUTION_ID'] = str(self.execution_id)
196
        result['ST2_ACTION_API_URL'] = get_full_public_api_url()
197
198
        if self.auth_token:
199
            result['ST2_ACTION_AUTH_TOKEN'] = self.auth_token.token
200
201
        return result
202
203
    def __str__(self):
204
        attrs = ', '.join(['%s=%s' % (k, v) for k, v in six.iteritems(self.__dict__)])
205
        return '%s@%s(%s)' % (self.__class__.__name__, str(id(self)), attrs)
206
207
208
@six.add_metaclass(abc.ABCMeta)
209
class AsyncActionRunner(ActionRunner):
210
    pass
211
212
213
class ShellRunnerMixin(object):
214
    """
215
    Class which contains utility functions to be used by shell runners.
216
    """
217
218
    def _transform_named_args(self, named_args):
219
        """
220
        Transform named arguments to the final form.
221
222
        :param named_args: Named arguments.
223
        :type named_args: ``dict``
224
225
        :rtype: ``dict``
226
        """
227
        if named_args:
228
            return {self._kwarg_op + k: v for (k, v) in six.iteritems(named_args)}
229
        return None
230
231
    def _get_script_args(self, action_parameters):
232
        """
233
        :param action_parameters: Action parameters.
234
        :type action_parameters: ``dict``
235
236
        :return: (positional_args, named_args)
237
        :rtype: (``str``, ``dict``)
238
        """
239
        # TODO: return list for positional args, command classes should escape it
240
        # and convert it to string
241
242
        is_script_run_as_cmd = self.runner_parameters.get(RUNNER_COMMAND, None)
243
244
        pos_args = ''
245
        named_args = {}
246
247
        if is_script_run_as_cmd:
248
            pos_args = self.runner_parameters.get(RUNNER_COMMAND, '')
249
            named_args = action_parameters
250
        else:
251
            pos_args, named_args = action_utils.get_args(action_parameters, self.action)
252
253
        return pos_args, named_args
254