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 sys |
17
|
|
|
import json |
18
|
|
|
import argparse |
19
|
|
|
|
20
|
|
|
from st2common import log as logging |
21
|
|
|
from st2actions import config |
22
|
|
|
from st2actions.runners.pythonrunner import Action |
23
|
|
|
from st2actions.runners.utils import get_logger_for_python_runner_action |
24
|
|
|
from st2actions.runners.utils import get_action_class_instance |
25
|
|
|
from st2common.util import loader as action_loader |
26
|
|
|
from st2common.util.config_parser import ContentPackConfigParser |
27
|
|
|
from st2common.constants.action import ACTION_OUTPUT_RESULT_DELIMITER |
28
|
|
|
from st2common.service_setup import db_setup |
29
|
|
|
from st2common.services.datastore import DatastoreService |
30
|
|
|
|
31
|
|
|
__all__ = [ |
32
|
|
|
'PythonActionWrapper', |
33
|
|
|
'ActionService' |
34
|
|
|
] |
35
|
|
|
|
36
|
|
|
LOG = logging.getLogger(__name__) |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
class ActionService(object): |
40
|
|
|
""" |
41
|
|
|
Instance of this class is passed to the action instance and exposes "public" |
42
|
|
|
methods which can be called by the action. |
43
|
|
|
""" |
44
|
|
|
|
45
|
|
|
def __init__(self, action_wrapper): |
46
|
|
|
logger = get_logger_for_python_runner_action(action_name=action_wrapper._class_name) |
|
|
|
|
47
|
|
|
|
48
|
|
|
self._action_wrapper = action_wrapper |
49
|
|
|
self._datastore_service = DatastoreService(logger=logger, |
50
|
|
|
pack_name=self._action_wrapper._pack, |
|
|
|
|
51
|
|
|
class_name=self._action_wrapper._class_name, |
|
|
|
|
52
|
|
|
api_username='action_service') |
53
|
|
|
|
54
|
|
|
################################## |
55
|
|
|
# Methods for datastore management |
56
|
|
|
################################## |
57
|
|
|
|
58
|
|
|
def list_values(self, local=True, prefix=None): |
59
|
|
|
return self._datastore_service.list_values(local, prefix) |
60
|
|
|
|
61
|
|
|
def get_value(self, name, local=True): |
62
|
|
|
return self._datastore_service.get_value(name, local) |
63
|
|
|
|
64
|
|
|
def set_value(self, name, value, ttl=None, local=True): |
65
|
|
|
return self._datastore_service.set_value(name, value, ttl, local) |
66
|
|
|
|
67
|
|
|
def delete_value(self, name, local=True): |
68
|
|
|
return self._datastore_service.delete_value(name, local) |
69
|
|
|
|
70
|
|
|
|
71
|
|
|
class PythonActionWrapper(object): |
72
|
|
|
def __init__(self, pack, file_path, parameters=None, parent_args=None): |
|
|
|
|
73
|
|
|
""" |
74
|
|
|
:param pack: Name of the pack this action belongs to. |
75
|
|
|
:type pack: ``str`` |
76
|
|
|
|
77
|
|
|
:param file_path: Path to the action module. |
78
|
|
|
:type file_path: ``str`` |
79
|
|
|
|
80
|
|
|
:param parameters: action parameters. |
81
|
|
|
:type parameters: ``dict`` or ``None`` |
82
|
|
|
|
83
|
|
|
:param parent_args: Command line arguments passed to the parent process. |
84
|
|
|
:type parse_args: ``list`` |
85
|
|
|
""" |
86
|
|
|
|
87
|
|
|
self._pack = pack |
88
|
|
|
self._file_path = file_path |
89
|
|
|
self._parameters = parameters or {} |
90
|
|
|
self._parent_args = parent_args or [] |
91
|
|
|
self._class_name = None |
92
|
|
|
self._logger = logging.getLogger('PythonActionWrapper') |
93
|
|
|
|
94
|
|
|
try: |
95
|
|
|
config.parse_args(args=self._parent_args) |
96
|
|
|
except Exception: |
97
|
|
|
pass |
98
|
|
|
else: |
99
|
|
|
db_setup() |
100
|
|
|
|
101
|
|
|
def run(self): |
102
|
|
|
action = self._get_action_instance() |
103
|
|
|
output = action.run(**self._parameters) |
104
|
|
|
|
105
|
|
|
# Print output to stdout so the parent can capture it |
106
|
|
|
sys.stdout.write(ACTION_OUTPUT_RESULT_DELIMITER) |
107
|
|
|
print_output = None |
108
|
|
|
try: |
109
|
|
|
print_output = json.dumps(output) |
110
|
|
|
except: |
111
|
|
|
print_output = str(output) |
112
|
|
|
sys.stdout.write(print_output + '\n') |
113
|
|
|
sys.stdout.write(ACTION_OUTPUT_RESULT_DELIMITER) |
114
|
|
|
|
115
|
|
|
def _get_action_instance(self): |
116
|
|
|
actions_cls = action_loader.register_plugin(Action, self._file_path) |
117
|
|
|
action_cls = actions_cls[0] if actions_cls and len(actions_cls) > 0 else None |
118
|
|
|
|
119
|
|
|
if not action_cls: |
120
|
|
|
raise Exception('File "%s" has no action or the file doesn\'t exist.' % |
121
|
|
|
(self._file_path)) |
122
|
|
|
|
123
|
|
|
config_parser = ContentPackConfigParser(pack_name=self._pack) |
124
|
|
|
config = config_parser.get_action_config(action_file_path=self._file_path) |
|
|
|
|
125
|
|
|
|
126
|
|
|
if config: |
127
|
|
|
LOG.info('Using config "%s" for action "%s"' % (config.file_path, |
128
|
|
|
self._file_path)) |
129
|
|
|
config = config.config |
130
|
|
|
else: |
131
|
|
|
LOG.info('No config found for action "%s"' % (self._file_path)) |
132
|
|
|
config = None |
133
|
|
|
|
134
|
|
|
action_service = ActionService(action_wrapper=self) |
135
|
|
|
action_instance = get_action_class_instance(action_cls=action_cls, |
136
|
|
|
config=config, |
137
|
|
|
action_service=action_service) |
138
|
|
|
return action_instance |
139
|
|
|
|
140
|
|
|
|
141
|
|
|
if __name__ == '__main__': |
142
|
|
|
parser = argparse.ArgumentParser(description='Python action runner process wrapper') |
143
|
|
|
parser.add_argument('--pack', required=True, |
144
|
|
|
help='Name of the pack this action belongs to') |
145
|
|
|
parser.add_argument('--file-path', required=True, |
146
|
|
|
help='Path to the action module') |
147
|
|
|
parser.add_argument('--parameters', required=False, |
148
|
|
|
help='Serialized action parameters') |
149
|
|
|
parser.add_argument('--parent-args', required=False, |
150
|
|
|
help='Command line arguments passed to the parent process') |
151
|
|
|
args = parser.parse_args() |
152
|
|
|
|
153
|
|
|
parameters = args.parameters |
154
|
|
|
parameters = json.loads(parameters) if parameters else {} |
155
|
|
|
parent_args = json.loads(args.parent_args) if args.parent_args else [] |
156
|
|
|
|
157
|
|
|
assert isinstance(parent_args, list) |
158
|
|
|
|
159
|
|
|
obj = PythonActionWrapper(pack=args.pack, |
160
|
|
|
file_path=args.file_path, |
161
|
|
|
parameters=parameters, |
162
|
|
|
parent_args=parent_args) |
163
|
|
|
|
164
|
|
|
obj.run() |
165
|
|
|
|
Prefixing a member variable
_
is usually regarded as the equivalent of declaring it with protected visibility that exists in other languages. Consequentially, such a member should only be accessed from the same class or a child class: