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
|
|
|
|
18
|
|
|
import editor |
19
|
|
|
import yaml |
20
|
|
|
|
21
|
|
|
from st2client.models import Config |
22
|
|
|
from st2client.models import Pack |
23
|
|
|
from st2client.models import LiveAction |
24
|
|
|
from st2client.commands import resource |
25
|
|
|
from st2client.commands.action import ActionRunCommandMixin |
26
|
|
|
from st2client.commands.noop import NoopCommand |
27
|
|
|
from st2client.formatters import table |
28
|
|
|
from st2client.exceptions.operations import OperationFailureException |
29
|
|
|
import st2client.utils.terminal as term |
30
|
|
|
from st2client.utils import interactive |
31
|
|
|
|
32
|
|
|
|
33
|
|
|
LIVEACTION_STATUS_REQUESTED = 'requested' |
34
|
|
|
LIVEACTION_STATUS_SCHEDULED = 'scheduled' |
35
|
|
|
LIVEACTION_STATUS_DELAYED = 'delayed' |
36
|
|
|
LIVEACTION_STATUS_RUNNING = 'running' |
37
|
|
|
LIVEACTION_STATUS_SUCCEEDED = 'succeeded' |
38
|
|
|
LIVEACTION_STATUS_FAILED = 'failed' |
39
|
|
|
LIVEACTION_STATUS_TIMED_OUT = 'timeout' |
40
|
|
|
LIVEACTION_STATUS_ABANDONED = 'abandoned' |
41
|
|
|
LIVEACTION_STATUS_CANCELING = 'canceling' |
42
|
|
|
LIVEACTION_STATUS_CANCELED = 'canceled' |
43
|
|
|
|
44
|
|
|
LIVEACTION_COMPLETED_STATES = [ |
45
|
|
|
LIVEACTION_STATUS_SUCCEEDED, |
46
|
|
|
LIVEACTION_STATUS_FAILED, |
47
|
|
|
LIVEACTION_STATUS_TIMED_OUT, |
48
|
|
|
LIVEACTION_STATUS_CANCELED, |
49
|
|
|
LIVEACTION_STATUS_ABANDONED |
50
|
|
|
] |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
class PackBranch(resource.ResourceBranch): |
54
|
|
|
def __init__(self, description, app, subparsers, parent_parser=None): |
55
|
|
|
super(PackBranch, self).__init__( |
56
|
|
|
Pack, description, app, subparsers, |
57
|
|
|
parent_parser=parent_parser, |
58
|
|
|
read_only=True, |
59
|
|
|
commands={ |
60
|
|
|
'list': PackListCommand, |
61
|
|
|
'get': NoopCommand |
62
|
|
|
}) |
63
|
|
|
|
64
|
|
|
self.commands['register'] = PackRegisterCommand(self.resource, self.app, self.subparsers) |
65
|
|
|
self.commands['install'] = PackInstallCommand(self.resource, self.app, self.subparsers) |
66
|
|
|
self.commands['remove'] = PackRemoveCommand(self.resource, self.app, self.subparsers) |
67
|
|
|
self.commands['search'] = PackSearchCommand(self.resource, self.app, self.subparsers) |
68
|
|
|
self.commands['show'] = PackShowCommand(self.resource, self.app, self.subparsers) |
69
|
|
|
self.commands['config'] = PackConfigCommand(self.resource, self.app, self.subparsers) |
70
|
|
|
|
71
|
|
|
|
72
|
|
|
class PackResourceCommand(resource.ResourceCommand): |
73
|
|
|
def run_and_print(self, args, **kwargs): |
74
|
|
|
try: |
75
|
|
|
instance = self.run(args, **kwargs) |
76
|
|
|
if not instance: |
77
|
|
|
raise resource.ResourceNotFoundError("No matching items found") |
78
|
|
|
self.print_output(instance, table.PropertyValueTable, |
79
|
|
|
attributes=['all'], json=args.json, yaml=args.yaml) |
80
|
|
|
except resource.ResourceNotFoundError: |
81
|
|
|
print("No matching items found") |
82
|
|
|
except Exception as e: |
83
|
|
|
message = e.message or str(e) |
84
|
|
|
print('ERROR: %s' % (message)) |
85
|
|
|
raise OperationFailureException(message) |
86
|
|
|
|
87
|
|
|
|
88
|
|
|
class PackAsyncCommand(ActionRunCommandMixin, resource.ResourceCommand): |
89
|
|
|
def __init__(self, *args, **kwargs): |
90
|
|
|
super(PackAsyncCommand, self).__init__(*args, **kwargs) |
91
|
|
|
|
92
|
|
|
self._add_common_options() |
93
|
|
|
|
94
|
|
|
@resource.add_auth_token_to_kwargs_from_cli |
95
|
|
|
def run_and_print(self, args, **kwargs): |
96
|
|
|
instance = self.run(args, **kwargs) |
97
|
|
|
if not instance: |
98
|
|
|
raise Exception('Server did not create instance.') |
99
|
|
|
|
100
|
|
|
parent_id = instance.execution_id |
101
|
|
|
|
102
|
|
|
stream_mgr = self.app.client.managers['Stream'] |
103
|
|
|
|
104
|
|
|
execution = None |
105
|
|
|
|
106
|
|
|
with term.TaskIndicator() as indicator: |
107
|
|
|
events = ['st2.execution__create', 'st2.execution__update'] |
108
|
|
|
for event in stream_mgr.listen(events, **kwargs): |
109
|
|
|
execution = LiveAction(**event) |
110
|
|
|
|
111
|
|
|
if execution.id == parent_id \ |
112
|
|
|
and execution.status in LIVEACTION_COMPLETED_STATES: |
113
|
|
|
break |
114
|
|
|
|
115
|
|
|
if getattr(execution, 'parent', None) == parent_id: |
116
|
|
|
status = execution.status |
117
|
|
|
name = execution.context['chain']['name'] |
118
|
|
|
|
119
|
|
|
if status == LIVEACTION_STATUS_SCHEDULED: |
120
|
|
|
indicator.add_stage(status, name) |
121
|
|
|
if status == LIVEACTION_STATUS_RUNNING: |
122
|
|
|
indicator.update_stage(status, name) |
123
|
|
|
if status in LIVEACTION_COMPLETED_STATES: |
124
|
|
|
indicator.finish_stage(status, name) |
125
|
|
|
|
126
|
|
|
if execution and execution.status == LIVEACTION_STATUS_FAILED: |
127
|
|
|
self._print_execution_details(execution=execution, args=args, **kwargs) |
128
|
|
|
sys.exit(1) |
129
|
|
|
|
130
|
|
|
|
131
|
|
|
class PackListCommand(resource.ResourceListCommand): |
132
|
|
|
display_attributes = ['name', 'description', 'version', 'author'] |
133
|
|
|
attribute_display_order = ['name', 'description', 'version', 'author'] |
134
|
|
|
|
135
|
|
|
|
136
|
|
|
class PackShowCommand(PackResourceCommand): |
137
|
|
|
def __init__(self, resource, *args, **kwargs): |
|
|
|
|
138
|
|
|
super(PackShowCommand, self).__init__(resource, 'show', |
139
|
|
|
'Get information about a %s from the index.' % resource.get_display_name().lower(), |
140
|
|
|
*args, **kwargs) |
141
|
|
|
|
142
|
|
|
self.parser.add_argument('pack', |
143
|
|
|
help='Name of the %s to show.' % |
144
|
|
|
resource.get_display_name().lower()) |
145
|
|
|
|
146
|
|
|
@resource.add_auth_token_to_kwargs_from_cli |
147
|
|
|
def run(self, args, **kwargs): |
148
|
|
|
return self.manager.search(args, **kwargs) |
149
|
|
|
|
150
|
|
|
|
151
|
|
|
class PackInstallCommand(PackAsyncCommand): |
152
|
|
|
def __init__(self, resource, *args, **kwargs): |
|
|
|
|
153
|
|
|
super(PackInstallCommand, self).__init__(resource, 'install', |
154
|
|
|
'Install new %s.' % resource.get_plural_display_name().lower(), |
155
|
|
|
*args, **kwargs) |
156
|
|
|
|
157
|
|
|
self.parser.add_argument('packs', |
158
|
|
|
nargs='+', |
159
|
|
|
metavar='pack', |
160
|
|
|
help='Name of the %s to install.' % |
161
|
|
|
resource.get_plural_display_name().lower()) |
162
|
|
|
|
163
|
|
|
@resource.add_auth_token_to_kwargs_from_cli |
164
|
|
|
def run(self, args, **kwargs): |
165
|
|
|
return self.manager.install(args.packs, **kwargs) |
166
|
|
|
|
167
|
|
|
|
168
|
|
|
class PackRemoveCommand(PackAsyncCommand): |
169
|
|
|
def __init__(self, resource, *args, **kwargs): |
|
|
|
|
170
|
|
|
super(PackRemoveCommand, self).__init__(resource, 'remove', |
171
|
|
|
'Remove %s.' % resource.get_plural_display_name().lower(), |
172
|
|
|
*args, **kwargs) |
173
|
|
|
|
174
|
|
|
self.parser.add_argument('packs', |
175
|
|
|
nargs='+', |
176
|
|
|
metavar='pack', |
177
|
|
|
help='Name of the %s to remove.' % |
178
|
|
|
resource.get_plural_display_name().lower()) |
179
|
|
|
|
180
|
|
|
@resource.add_auth_token_to_kwargs_from_cli |
181
|
|
|
def run(self, args, **kwargs): |
182
|
|
|
return self.manager.remove(args.packs, **kwargs) |
183
|
|
|
|
184
|
|
|
|
185
|
|
|
class PackRegisterCommand(PackResourceCommand): |
186
|
|
|
def __init__(self, resource, *args, **kwargs): |
|
|
|
|
187
|
|
|
super(PackRegisterCommand, self).__init__(resource, 'register', |
188
|
|
|
'Register a %s: sync all file changes with DB.' % resource.get_display_name().lower(), |
189
|
|
|
*args, **kwargs) |
190
|
|
|
|
191
|
|
|
self.parser.add_argument('--packs', |
192
|
|
|
nargs='+', |
193
|
|
|
help='Name of the %s(s) to register.' % |
194
|
|
|
resource.get_display_name().lower()) |
195
|
|
|
|
196
|
|
|
self.parser.add_argument('--types', |
197
|
|
|
nargs='+', |
198
|
|
|
help='Types of content to register.') |
199
|
|
|
|
200
|
|
|
@resource.add_auth_token_to_kwargs_from_cli |
201
|
|
|
def run(self, args, **kwargs): |
202
|
|
|
return self.manager.register(args.packs, args.types, **kwargs) |
203
|
|
|
|
204
|
|
|
|
205
|
|
|
class PackSearchCommand(resource.ResourceTableCommand): |
206
|
|
|
display_attributes = ['name', 'description', 'version', 'author'] |
207
|
|
|
attribute_display_order = ['name', 'description', 'version', 'author'] |
208
|
|
|
|
209
|
|
|
def __init__(self, resource, *args, **kwargs): |
|
|
|
|
210
|
|
|
super(PackSearchCommand, self).__init__(resource, 'search', |
211
|
|
|
'Search for a %s in the directory.' % resource.get_display_name().lower(), |
212
|
|
|
*args, **kwargs) |
213
|
|
|
|
214
|
|
|
self.parser.add_argument('query', |
215
|
|
|
help='Search query.') |
216
|
|
|
|
217
|
|
|
@resource.add_auth_token_to_kwargs_from_cli |
218
|
|
|
def run(self, args, **kwargs): |
219
|
|
|
return self.manager.search(args, **kwargs) |
220
|
|
|
|
221
|
|
|
|
222
|
|
|
class PackConfigCommand(resource.ResourceCommand): |
223
|
|
|
def __init__(self, resource, *args, **kwargs): |
|
|
|
|
224
|
|
|
super(PackConfigCommand, self).__init__(resource, 'config', |
225
|
|
|
'Configure a %s based on config schema.' % resource.get_display_name().lower(), |
226
|
|
|
*args, **kwargs) |
227
|
|
|
|
228
|
|
|
self.parser.add_argument('name', |
229
|
|
|
help='Name of the %s(s) to configure.' % |
230
|
|
|
resource.get_display_name().lower()) |
231
|
|
|
|
232
|
|
|
@resource.add_auth_token_to_kwargs_from_cli |
233
|
|
|
def run(self, args, **kwargs): |
234
|
|
|
schema = self.app.client.managers['ConfigSchema'].get_by_ref_or_id(args.name, **kwargs) |
235
|
|
|
|
236
|
|
|
if not schema: |
237
|
|
|
raise resource.ResourceNotFoundError("%s doesn't have config schema defined" % |
238
|
|
|
self.resource.get_display_name()) |
239
|
|
|
|
240
|
|
|
config = interactive.InteractiveForm(schema.attributes).initiate_dialog() |
241
|
|
|
|
242
|
|
|
message = '---\nDo you want to preview the config in an editor before saving?' |
243
|
|
|
description = 'Secrets would be shown in plain text.' |
244
|
|
|
preview_dialog = interactive.Question(message, {'default': 'y', 'description': description}) |
245
|
|
|
if preview_dialog.read() == 'y': |
246
|
|
|
try: |
247
|
|
|
contents = yaml.safe_dump(config, indent=4, default_flow_style=False) |
248
|
|
|
modified = editor.edit(contents=contents) |
249
|
|
|
config = yaml.safe_load(modified) |
250
|
|
|
except editor.EditorError as e: |
251
|
|
|
print(str(e)) |
252
|
|
|
|
253
|
|
|
message = '---\nDo you want me to save it?' |
254
|
|
|
save_dialog = interactive.Question(message, {'default': 'y'}) |
255
|
|
|
if save_dialog.read() == 'n': |
256
|
|
|
raise OperationFailureException('Interrupted') |
257
|
|
|
|
258
|
|
|
result = self.app.client.managers['Config'].update(Config(pack=args.name, values=config)) |
259
|
|
|
|
260
|
|
|
return result |
261
|
|
|
|
262
|
|
|
def run_and_print(self, args, **kwargs): |
263
|
|
|
try: |
264
|
|
|
instance = self.run(args, **kwargs) |
265
|
|
|
if not instance: |
266
|
|
|
raise Exception("Configuration failed") |
267
|
|
|
self.print_output(instance, table.PropertyValueTable, |
268
|
|
|
attributes=['all'], json=args.json, yaml=args.yaml) |
269
|
|
|
except (KeyboardInterrupt, SystemExit): |
270
|
|
|
raise OperationFailureException('Interrupted') |
271
|
|
|
except Exception as e: |
272
|
|
|
if self.app.client.debug: |
273
|
|
|
raise |
274
|
|
|
|
275
|
|
|
message = e.message or str(e) |
276
|
|
|
print('ERROR: %s' % (message)) |
277
|
|
|
raise OperationFailureException(message) |
278
|
|
|
|
It is generally a bad practice to shadow variables from the outer-scope. In most cases, this is done unintentionally and might lead to unexpected behavior: