/
shell.py
359 lines (310 loc) · 13.3 KB
/
shell.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
# Copyright 2012 OpenStack LLC.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import json
import textwrap
from heatclient.common import utils
import heatclient.exc as exc
def _set_template_fields(hc, args, fields):
if args.template_file:
tpl = open(args.template_file).read()
if tpl.startswith('{'):
fields['template'] = json.loads(tpl)
else:
fields['template'] = tpl
elif args.template_url:
fields['template_url'] = args.template_url
elif args.template_object:
template_body = hc.raw_request('GET', args.template_object)
if template_body:
fields['template'] = json.loads(template_body)
else:
raise exc.CommandError('Could not fetch template from %s'
% args.template_object)
else:
raise exc.CommandError('Need to specify exactly one of '
'--template-file, --template-url '
'or --template-object')
@utils.arg('-f', '--template-file', metavar='<FILE>',
help='Path to the template.')
@utils.arg('-u', '--template-url', metavar='<URL>',
help='URL of template.')
@utils.arg('-o', '--template-object', metavar='<URL>',
help='URL to retrieve template object (e.g from swift)')
@utils.arg('-c', '--create-timeout', metavar='<TIMEOUT>',
default=60, type=int,
help='Stack creation timeout in minutes. Default: 60')
@utils.arg('-r', '--enable-rollback', default=False, action="store_true",
help='Enable rollback on create/update failure')
@utils.arg('-P', '--parameters', metavar='<KEY1=VALUE1;KEY2=VALUE2...>',
help='Parameter values used to create the stack.')
@utils.arg('name', metavar='<STACK_NAME>',
help='Name of the stack to create.')
def do_create(hc, args):
'''DEPRECATED! Use stack-create instead'''
do_stack_create(hc, args)
@utils.arg('-f', '--template-file', metavar='<FILE>',
help='Path to the template.')
@utils.arg('-u', '--template-url', metavar='<URL>',
help='URL of template.')
@utils.arg('-o', '--template-object', metavar='<URL>',
help='URL to retrieve template object (e.g from swift)')
@utils.arg('-c', '--create-timeout', metavar='<TIMEOUT>',
default=60, type=int,
help='Stack creation timeout in minutes. Default: 60')
@utils.arg('-r', '--enable-rollback', default=False, action="store_true",
help='Enable rollback on create/update failure')
@utils.arg('-P', '--parameters', metavar='<KEY1=VALUE1;KEY2=VALUE2...>',
help='Parameter values used to create the stack.')
@utils.arg('name', metavar='<STACK_NAME>',
help='Name of the stack to create.')
def do_stack_create(hc, args):
'''Create the stack'''
fields = {'stack_name': args.name,
'timeout_mins': args.create_timeout,
'disable_rollback': not(args.enable_rollback),
'parameters': utils.format_parameters(args.parameters)}
_set_template_fields(hc, args, fields)
hc.stacks.create(**fields)
do_stack_list(hc)
@utils.arg('id', metavar='<NAME or ID>', help='Name or ID of stack to delete.')
def do_delete(hc, args):
'''DEPRECATED! Use stack-delete instead'''
do_stack_delete(hc, args)
@utils.arg('id', metavar='<NAME or ID>', help='Name or ID of stack to delete.')
def do_stack_delete(hc, args):
'''Delete the stack'''
fields = {'stack_id': args.id}
try:
hc.stacks.delete(**fields)
except exc.HTTPNotFound:
raise exc.CommandError('Stack not found: %s' % args.id)
else:
do_stack_list(hc)
@utils.arg('id', metavar='<NAME or ID>',
help='Name or ID of stack to describe.')
def do_describe(hc, args):
'''DEPRECATED! Use stack-show instead'''
do_stack_show(hc, args)
@utils.arg('id', metavar='<NAME or ID>',
help='Name or ID of stack to describe.')
def do_stack_show(hc, args):
'''Describe the stack'''
fields = {'stack_id': args.id}
try:
stack = hc.stacks.get(**fields)
except exc.HTTPNotFound:
raise exc.CommandError('Stack not found: %s' % args.id)
else:
text_wrap = lambda d: '\n'.join(textwrap.wrap(d, 55))
link_format = lambda links: '\n'.join([l['href'] for l in links])
json_format = lambda js: json.dumps(js, indent=2)
formatters = {
'description': text_wrap,
'template_description': text_wrap,
'stack_status_reason': text_wrap,
'parameters': json_format,
'outputs': json_format,
'links': link_format
}
utils.print_dict(stack.to_dict(), formatters=formatters)
@utils.arg('-f', '--template-file', metavar='<FILE>',
help='Path to the template.')
@utils.arg('-u', '--template-url', metavar='<URL>',
help='URL of template.')
@utils.arg('-o', '--template-object', metavar='<URL>',
help='URL to retrieve template object (e.g from swift)')
@utils.arg('-P', '--parameters', metavar='<KEY1=VALUE1;KEY2=VALUE2...>',
help='Parameter values used to create the stack.')
@utils.arg('id', metavar='<NAME or ID>',
help='Name or ID of stack to update.')
def do_update(hc, args):
'''DEPRECATED! Use stack-update instead'''
do_stack_update(hc, args)
@utils.arg('-f', '--template-file', metavar='<FILE>',
help='Path to the template.')
@utils.arg('-u', '--template-url', metavar='<URL>',
help='URL of template.')
@utils.arg('-o', '--template-object', metavar='<URL>',
help='URL to retrieve template object (e.g from swift)')
@utils.arg('-P', '--parameters', metavar='<KEY1=VALUE1;KEY2=VALUE2...>',
help='Parameter values used to create the stack.')
@utils.arg('id', metavar='<NAME or ID>',
help='Name or ID of stack to update.')
def do_stack_update(hc, args):
'''Update the stack'''
fields = {'stack_id': args.id,
'parameters': utils.format_parameters(args.parameters)}
_set_template_fields(hc, args, fields)
hc.stacks.update(**fields)
do_list(hc)
def do_list(hc, args={}):
'''DEPRECATED! Use stack-list instead'''
do_stack_list(hc, args)
def do_stack_list(hc, args={}):
'''List the user's stacks'''
kwargs = {}
stacks = hc.stacks.list(**kwargs)
field_labels = ['ID', 'Name', 'Status', 'Created']
fields = ['id', 'stack_name', 'stack_status', 'creation_time']
utils.print_list(stacks, fields, field_labels, sortby=3)
@utils.arg('id', metavar='<NAME or ID>',
help='Name or ID of stack to get the template for.')
def do_gettemplate(hc, args):
'''DEPRECATED! Use template-show instead'''
do_template_show(hc, args)
@utils.arg('id', metavar='<NAME or ID>',
help='Name or ID of stack to get the template for.')
def do_template_show(hc, args):
'''Get the template for the specified stack'''
fields = {'stack_id': args.id}
try:
template = hc.stacks.template(**fields)
except exc.HTTPNotFound:
raise exc.CommandError('Stack not found: %s' % args.id)
else:
print json.dumps(template, indent=2)
@utils.arg('-u', '--template-url', metavar='<URL>',
help='URL of template.')
@utils.arg('-f', '--template-file', metavar='<FILE>',
help='Path to the template.')
@utils.arg('-o', '--template-object', metavar='<URL>',
help='URL to retrieve template object (e.g from swift)')
@utils.arg('-P', '--parameters', metavar='<KEY1=VALUE1;KEY2=VALUE2...>',
help='Parameter values to validate.')
def do_validate(hc, args):
'''DEPRECATED! Use template-validate instead'''
do_template_validate(hc, args)
@utils.arg('-u', '--template-url', metavar='<URL>',
help='URL of template.')
@utils.arg('-f', '--template-file', metavar='<FILE>',
help='Path to the template.')
@utils.arg('-o', '--template-object', metavar='<URL>',
help='URL to retrieve template object (e.g from swift)')
@utils.arg('-P', '--parameters', metavar='<KEY1=VALUE1;KEY2=VALUE2...>',
help='Parameter values to validate.')
def do_template_validate(hc, args):
'''Validate a template with parameters'''
fields = {'parameters': utils.format_parameters(args.parameters)}
_set_template_fields(hc, args, fields)
validation = hc.stacks.validate(**fields)
print json.dumps(validation, indent=2)
@utils.arg('id', metavar='<NAME or ID>',
help='Name or ID of stack to show the resources for.')
def do_resource_list(hc, args):
'''Show list of resources belonging to a stack'''
fields = {'stack_id': args.id}
try:
resources = hc.resources.list(**fields)
except exc.HTTPNotFound:
raise exc.CommandError('Stack not found: %s' % args.id)
else:
field_labels = ['Name', 'Type',
'Status', 'Updated']
fields = ['logical_resource_id', 'resource_type',
'resource_status', 'updated_time']
utils.print_list(resources, fields, field_labels, sortby=3)
@utils.arg('id', metavar='<NAME or ID>',
help='Name or ID of stack to show the resource for.')
@utils.arg('resource', metavar='<RESOURCE>',
help='Name of the resource to show the details for.')
def do_resource(hc, args):
'''DEPRECATED! Use resource-show instead'''
do_resource_show(hc, args)
@utils.arg('id', metavar='<NAME or ID>',
help='Name or ID of stack to show the resource for.')
@utils.arg('resource', metavar='<RESOURCE>',
help='Name of the resource to show the details for.')
def do_resource_show(hc, args):
'''Describe the resource'''
fields = {'stack_id': args.id,
'resource_name': args.resource}
try:
resource = hc.resources.get(**fields)
except exc.HTTPNotFound:
raise exc.CommandError('Stack or resource not found: %s %s' %
(args.id, args.resource))
else:
link_format = lambda links: '\n'.join([l['href'] for l in links])
json_format = lambda js: json.dumps(js, indent=2)
formatters = {
'links': link_format
}
utils.print_dict(resource.to_dict(), formatters=formatters)
@utils.arg('id', metavar='<NAME or ID>',
help='Name or ID of stack to show the resource metadata for.')
@utils.arg('resource', metavar='<RESOURCE>',
help='Name of the resource to show the metadata for.')
def do_resource_metadata(hc, args):
'''List resource metadata'''
fields = {'stack_id': args.id,
'resource_name': args.resource}
try:
resource = hc.resources.metadata(**fields)
except exc.HTTPNotFound:
raise exc.CommandError('Stack or resource not found: %s %s' %
(args.id, args.resource))
else:
formatters = {}
utils.print_dict(resource.to_dict(), formatters=formatters)
@utils.arg('id', metavar='<NAME or ID>',
help='Name or ID of stack to show the events for.')
@utils.arg('-r', '--resource', metavar='<RESOURCE>',
help='Name of the resource to filter events by')
def do_event_list(hc, args):
'''List events for a stack'''
fields = {'stack_id': args.id,
'resource_name': args.resource}
try:
events = hc.events.list(**fields)
except exc.HTTPNotFound:
raise exc.CommandError('Stack not found: %s' % args.id)
else:
field_labels = ['Resource', 'ID', 'Reason',
'Status', 'Event time']
fields = ['logical_resource_id', 'id', 'resource_status_reason',
'resource_status', 'event_time']
utils.print_list(events, fields, field_labels, sortby=4)
@utils.arg('id', metavar='<NAME or ID>',
help='Name or ID of stack to show the events for.')
@utils.arg('resource', metavar='<RESOURCE>',
help='Name of the resource the event belongs to.')
@utils.arg('event', metavar='<EVENT>',
help='ID of event to display details for')
def do_event(hc, args):
'''DEPRECATED! Use event-show instead'''
do_event_show(hc, args)
@utils.arg('id', metavar='<NAME or ID>',
help='Name or ID of stack to show the events for.')
@utils.arg('resource', metavar='<RESOURCE>',
help='Name of the resource the event belongs to.')
@utils.arg('event', metavar='<EVENT>',
help='ID of event to display details for')
def do_event_show(hc, args):
'''Describe the event'''
fields = {'stack_id': args.id,
'resource_name': args.resource,
'event_id': args.event}
try:
event = hc.events.get(**fields)
except exc.HTTPNotFound:
raise exc.CommandError('Stack not found: %s' % args.id)
else:
link_format = lambda links: '\n'.join([l['href'] for l in links])
json_format = lambda js: json.dumps(js, indent=2)
formatters = {
'links': link_format,
'resource_properties': json_format
}
utils.print_dict(event.to_dict(), formatters=formatters)