| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
"""Orchestration v1 Stack action implementations""" |
| 15 |
|
| 16 |
import logging |
| 17 |
import sys |
| 18 |
|
| 19 |
from openstackclient.identity import common as identity_common |
| 20 |
from osc_lib.command import command |
| 21 |
from osc_lib import exceptions as exc |
| 22 |
from osc_lib import utils |
| 23 |
from oslo_serialization import jsonutils |
| 24 |
from urllib import request |
| 25 |
import yaml |
| 26 |
|
| 27 |
from heatclient._i18n import _ |
| 28 |
from heatclient.common import event_utils |
| 29 |
from heatclient.common import format_utils |
| 30 |
from heatclient.common import hook_utils |
| 31 |
from heatclient.common import http |
| 32 |
from heatclient.common import template_utils |
| 33 |
from heatclient.common import utils as heat_utils |
| 34 |
from heatclient import exc as heat_exc |
| 35 |
from heatclient.osc.v1 import common |
| 36 |
|
| 37 |
|
| 38 |
class CreateStack(command.ShowOne): |
| 39 |
"""Create a stack.""" |
| 40 |
|
| 41 |
log = logging.getLogger(__name__ + '.CreateStack') |
| 42 |
|
| 43 |
def get_parser(self, prog_name): |
| 44 |
parser = super().get_parser(prog_name) |
| 45 |
parser.add_argument( |
| 46 |
'-e', '--environment', |
| 47 |
metavar='<environment>', |
| 48 |
action='append', |
| 49 |
help=_('Path to the environment. Can be specified multiple times') |
| 50 |
) |
| 51 |
parser.add_argument( |
| 52 |
'-s', '--files-container', |
| 53 |
metavar='<files-container>', |
| 54 |
help=_('Swift files container name. Local files other than ' |
| 55 |
'root template would be ignored. If other files are not ' |
| 56 |
'found in swift, heat engine would raise an error.') |
| 57 |
) |
| 58 |
parser.add_argument( |
| 59 |
'--timeout', |
| 60 |
metavar='<timeout>', |
| 61 |
type=int, |
| 62 |
help=_('Stack creating timeout in minutes') |
| 63 |
) |
| 64 |
parser.add_argument( |
| 65 |
'--pre-create', |
| 66 |
metavar='<resource>', |
| 67 |
default=None, |
| 68 |
action='append', |
| 69 |
help=_('Name of a resource to set a pre-create hook to. Resources ' |
| 70 |
'in nested stacks can be set using slash as a separator: ' |
| 71 |
'``nested_stack/another/my_resource``. You can use ' |
| 72 |
'wildcards to match multiple stacks or resources: ' |
| 73 |
'``nested_stack/an*/*_resource``. This can be specified ' |
| 74 |
'multiple times') |
| 75 |
) |
| 76 |
parser.add_argument( |
| 77 |
'--enable-rollback', |
| 78 |
action='store_true', |
| 79 |
help=_('Enable rollback on create/update failure') |
| 80 |
) |
| 81 |
parser.add_argument( |
| 82 |
'--parameter', |
| 83 |
metavar='<key=value>', |
| 84 |
action='append', |
| 85 |
help=_('Parameter values used to create the stack. This can be ' |
| 86 |
'specified multiple times') |
| 87 |
) |
| 88 |
parser.add_argument( |
| 89 |
'--parameter-file', |
| 90 |
metavar='<key=file>', |
| 91 |
action='append', |
| 92 |
help=_('Parameter values from file used to create the stack. ' |
| 93 |
'This can be specified multiple times. Parameter values ' |
| 94 |
'would be the content of the file') |
| 95 |
) |
| 96 |
parser.add_argument( |
| 97 |
'--wait', |
| 98 |
action='store_true', |
| 99 |
help=_('Wait until stack goes to CREATE_COMPLETE or CREATE_FAILED') |
| 100 |
) |
| 101 |
parser.add_argument( |
| 102 |
'--poll', |
| 103 |
metavar='SECONDS', |
| 104 |
type=int, |
| 105 |
default=5, |
| 106 |
help=_('Poll interval in seconds for use with --wait, ' |
| 107 |
'defaults to 5.') |
| 108 |
) |
| 109 |
parser.add_argument( |
| 110 |
'--tags', |
| 111 |
metavar='<tag1,tag2...>', |
| 112 |
help=_('A list of tags to associate with the stack') |
| 113 |
) |
| 114 |
parser.add_argument( |
| 115 |
'--dry-run', |
| 116 |
action='store_true', |
| 117 |
help=_('Do not actually perform the stack create, but show what ' |
| 118 |
'would be created') |
| 119 |
) |
| 120 |
parser.add_argument( |
| 121 |
'name', |
| 122 |
metavar='<stack-name>', |
| 123 |
help=_('Name of the stack to create') |
| 124 |
) |
| 125 |
parser.add_argument( |
| 126 |
'-t', '--template', |
| 127 |
metavar='<template>', |
| 128 |
required=True, |
| 129 |
help=_('Path to the template') |
| 130 |
) |
| 131 |
|
| 132 |
return parser |
| 133 |
|
| 134 |
def take_action(self, parsed_args): |
| 135 |
self.log.debug('take_action(%s)', parsed_args) |
| 136 |
|
| 137 |
client = self.app.client_manager.orchestration |
| 138 |
|
| 139 |
tpl_files, template = template_utils.process_template_path( |
| 140 |
parsed_args.template, |
| 141 |
object_request=http.authenticated_fetcher(client), |
| 142 |
fetch_child=parsed_args.files_container is None) |
| 143 |
|
| 144 |
env_files_list = [] |
| 145 |
env_files, env = ( |
| 146 |
template_utils.process_multiple_environments_and_files( |
| 147 |
env_paths=parsed_args.environment, |
| 148 |
env_list_tracker=env_files_list, |
| 149 |
fetch_env_files=parsed_args.files_container is None)) |
| 150 |
|
| 151 |
parameters = heat_utils.format_all_parameters( |
| 152 |
parsed_args.parameter, |
| 153 |
parsed_args.parameter_file, |
| 154 |
parsed_args.template) |
| 155 |
|
| 156 |
if parsed_args.pre_create: |
| 157 |
template_utils.hooks_to_env(env, parsed_args.pre_create, |
| 158 |
'pre-create') |
| 159 |
|
| 160 |
fields = { |
| 161 |
'stack_name': parsed_args.name, |
| 162 |
'disable_rollback': not parsed_args.enable_rollback, |
| 163 |
'parameters': parameters, |
| 164 |
'template': template, |
| 165 |
'files': dict(list(tpl_files.items()) + list(env_files.items())), |
| 166 |
'environment': env |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
if env_files_list: |
| 171 |
fields['environment_files'] = env_files_list |
| 172 |
|
| 173 |
if parsed_args.files_container: |
| 174 |
fields['files_container'] = parsed_args.files_container |
| 175 |
|
| 176 |
if parsed_args.tags: |
| 177 |
fields['tags'] = parsed_args.tags |
| 178 |
if parsed_args.timeout: |
| 179 |
fields['timeout_mins'] = parsed_args.timeout |
| 180 |
|
| 181 |
if parsed_args.dry_run: |
| 182 |
stack = client.stacks.preview(**fields) |
| 183 |
|
| 184 |
formatters = { |
| 185 |
'description': common.TextWrapColumn, |
| 186 |
'template_description': common.TextWrapColumn, |
| 187 |
'stack_status_reason': common.TextWrapColumn, |
| 188 |
'parameters': common.JsonColumn, |
| 189 |
'outputs': common.JsonColumn, |
| 190 |
'resources': common.JsonColumn, |
| 191 |
'links': common.LinkColumn, |
| 192 |
} |
| 193 |
|
| 194 |
columns = [] |
| 195 |
for key in stack.to_dict(): |
| 196 |
columns.append(key) |
| 197 |
columns.sort() |
| 198 |
|
| 199 |
return ( |
| 200 |
columns, |
| 201 |
utils.get_item_properties(stack, columns, |
| 202 |
formatters=formatters) |
| 203 |
) |
| 204 |
|
| 205 |
stack = client.stacks.create(**fields)['stack'] |
| 206 |
if parsed_args.wait: |
| 207 |
stack_status, msg = event_utils.poll_for_events( |
| 208 |
client, parsed_args.name, action='CREATE', |
| 209 |
poll_period=parsed_args.poll) |
| 210 |
if stack_status == 'CREATE_FAILED': |
| 211 |
raise exc.CommandError(msg) |
| 212 |
|
| 213 |
return _show_stack(client, stack['id'], format='table', short=True) |
| 214 |
|
| 215 |
|
| 216 |
class UpdateStack(command.ShowOne): |
| 217 |
"""Update a stack.""" |
| 218 |
|
| 219 |
log = logging.getLogger(__name__ + '.UpdateStack') |
| 220 |
|
| 221 |
def get_parser(self, prog_name): |
| 222 |
parser = super().get_parser(prog_name) |
| 223 |
parser.add_argument( |
| 224 |
'-t', '--template', metavar='<template>', |
| 225 |
help=_('Path to the template') |
| 226 |
) |
| 227 |
parser.add_argument( |
| 228 |
'-s', '--files-container', |
| 229 |
metavar='<files-container>', |
| 230 |
help=_('Swift files container name. Local files other than ' |
| 231 |
'root template would be ignored. If other files are not ' |
| 232 |
'found in swift, heat engine would raise an error.') |
| 233 |
) |
| 234 |
parser.add_argument( |
| 235 |
'-e', '--environment', metavar='<environment>', |
| 236 |
action='append', |
| 237 |
help=_('Path to the environment. Can be specified multiple times') |
| 238 |
) |
| 239 |
parser.add_argument( |
| 240 |
'--pre-update', metavar='<resource>', action='append', |
| 241 |
help=_('Name of a resource to set a pre-update hook to. Resources ' |
| 242 |
'in nested stacks can be set using slash as a separator: ' |
| 243 |
'``nested_stack/another/my_resource``. You can use ' |
| 244 |
'wildcards to match multiple stacks or resources: ' |
| 245 |
'``nested_stack/an*/*_resource``. This can be specified ' |
| 246 |
'multiple times') |
| 247 |
) |
| 248 |
parser.add_argument( |
| 249 |
'--timeout', metavar='<timeout>', type=int, |
| 250 |
help=_('Stack update timeout in minutes') |
| 251 |
) |
| 252 |
parser.add_argument( |
| 253 |
'--rollback', metavar='<value>', |
| 254 |
default='keep', |
| 255 |
choices=['enabled', 'disabled', 'keep'], |
| 256 |
help=_('Set rollback on update failure. ' |
| 257 |
'Value "enabled" sets rollback to enabled. ' |
| 258 |
'Value "disabled" sets rollback to disabled. ' |
| 259 |
'Value "keep" uses the value of existing stack to be ' |
| 260 |
'updated.') |
| 261 |
) |
| 262 |
parser.add_argument( |
| 263 |
'--dry-run', action="store_true", |
| 264 |
help=_('Do not actually perform the stack update, but show what ' |
| 265 |
'would be changed') |
| 266 |
) |
| 267 |
parser.add_argument( |
| 268 |
'--show-nested', default=False, action="store_true", |
| 269 |
help=_('Show nested stacks when performing --dry-run') |
| 270 |
) |
| 271 |
parser.add_argument( |
| 272 |
'--parameter', metavar='<key=value>', |
| 273 |
help=_('Parameter values used to create the stack. ' |
| 274 |
'This can be specified multiple times'), |
| 275 |
action='append' |
| 276 |
) |
| 277 |
parser.add_argument( |
| 278 |
'--parameter-file', metavar='<key=file>', |
| 279 |
help=_('Parameter values from file used to create the stack. ' |
| 280 |
'This can be specified multiple times. Parameter value ' |
| 281 |
'would be the content of the file'), |
| 282 |
action='append' |
| 283 |
) |
| 284 |
parser.add_argument( |
| 285 |
'--existing', action="store_true", |
| 286 |
help=_('Re-use the template, parameters and environment of the ' |
| 287 |
'current stack. If the template argument is omitted then ' |
| 288 |
'the existing template is used. If no %(env_arg)s is ' |
| 289 |
'specified then the existing environment is used. ' |
| 290 |
'Parameters specified in %(arg)s will patch over the ' |
| 291 |
'existing values in the current stack. Parameters omitted ' |
| 292 |
'will keep the existing values') % { |
| 293 |
'arg': '--parameter', 'env_arg': '--environment'} |
| 294 |
) |
| 295 |
parser.add_argument( |
| 296 |
'--clear-parameter', metavar='<parameter>', |
| 297 |
help=_('Remove the parameters from the set of parameters of ' |
| 298 |
'current stack for the %(cmd)s. The default value in the ' |
| 299 |
'template will be used. This can be specified multiple ' |
| 300 |
'times') % {'cmd': 'stack-update'}, |
| 301 |
action='append' |
| 302 |
) |
| 303 |
parser.add_argument( |
| 304 |
'stack', metavar='<stack>', |
| 305 |
help=_('Name or ID of stack to update') |
| 306 |
) |
| 307 |
parser.add_argument( |
| 308 |
'--tags', metavar='<tag1,tag2...>', |
| 309 |
help=_('An updated list of tags to associate with the stack') |
| 310 |
) |
| 311 |
parser.add_argument( |
| 312 |
'--wait', |
| 313 |
action='store_true', |
| 314 |
help=_('Wait until stack goes to UPDATE_COMPLETE or ' |
| 315 |
'UPDATE_FAILED') |
| 316 |
) |
| 317 |
parser.add_argument( |
| 318 |
'--converge', |
| 319 |
action='store_true', |
| 320 |
help=_('Stack update with observe on reality.') |
| 321 |
) |
| 322 |
|
| 323 |
return parser |
| 324 |
|
| 325 |
def take_action(self, parsed_args): |
| 326 |
self.log.debug('take_action(%s)', parsed_args) |
| 327 |
|
| 328 |
client = self.app.client_manager.orchestration |
| 329 |
|
| 330 |
tpl_files, template = template_utils.process_template_path( |
| 331 |
parsed_args.template, |
| 332 |
object_request=http.authenticated_fetcher(client), |
| 333 |
existing=parsed_args.existing, |
| 334 |
fetch_child=parsed_args.files_container is None) |
| 335 |
|
| 336 |
env_files_list = [] |
| 337 |
env_files, env = ( |
| 338 |
template_utils.process_multiple_environments_and_files( |
| 339 |
env_paths=parsed_args.environment, |
| 340 |
env_list_tracker=env_files_list, |
| 341 |
fetch_env_files=parsed_args.files_container is None)) |
| 342 |
|
| 343 |
parameters = heat_utils.format_all_parameters( |
| 344 |
parsed_args.parameter, |
| 345 |
parsed_args.parameter_file, |
| 346 |
parsed_args.template) |
| 347 |
|
| 348 |
if parsed_args.pre_update: |
| 349 |
template_utils.hooks_to_env(env, parsed_args.pre_update, |
| 350 |
'pre-update') |
| 351 |
|
| 352 |
fields = { |
| 353 |
'stack_id': parsed_args.stack, |
| 354 |
'parameters': parameters, |
| 355 |
'existing': parsed_args.existing, |
| 356 |
'template': template, |
| 357 |
'files': dict(list(tpl_files.items()) + list(env_files.items())), |
| 358 |
'environment': env |
| 359 |
} |
| 360 |
|
| 361 |
|
| 362 |
if env_files_list: |
| 363 |
fields['environment_files'] = env_files_list |
| 364 |
|
| 365 |
if parsed_args.files_container: |
| 366 |
fields['files_container'] = parsed_args.files_container |
| 367 |
|
| 368 |
if parsed_args.tags: |
| 369 |
fields['tags'] = parsed_args.tags |
| 370 |
if parsed_args.timeout: |
| 371 |
fields['timeout_mins'] = parsed_args.timeout |
| 372 |
if parsed_args.clear_parameter: |
| 373 |
fields['clear_parameters'] = list(parsed_args.clear_parameter) |
| 374 |
|
| 375 |
if parsed_args.rollback != 'keep': |
| 376 |
fields['disable_rollback'] = (parsed_args.rollback == 'disabled') |
| 377 |
|
| 378 |
if parsed_args.dry_run: |
| 379 |
if parsed_args.show_nested: |
| 380 |
fields['show_nested'] = parsed_args.show_nested |
| 381 |
|
| 382 |
changes = client.stacks.preview_update(**fields) |
| 383 |
|
| 384 |
fields = ['state', 'resource_name', 'resource_type', |
| 385 |
'resource_identity'] |
| 386 |
|
| 387 |
columns = sorted(changes.get("resource_changes", {}).keys()) |
| 388 |
data = [common.JsonColumn(changes["resource_changes"][key]) |
| 389 |
for key in columns] |
| 390 |
|
| 391 |
return columns, data |
| 392 |
|
| 393 |
if parsed_args.wait: |
| 394 |
|
| 395 |
events = event_utils.get_events(client, |
| 396 |
stack_id=parsed_args.stack, |
| 397 |
event_args={'sort_dir': 'desc'}, |
| 398 |
limit=1) |
| 399 |
marker = events[0].id if events else None |
| 400 |
|
| 401 |
if parsed_args.converge: |
| 402 |
fields['converge'] = True |
| 403 |
|
| 404 |
client.stacks.update(**fields) |
| 405 |
|
| 406 |
if parsed_args.wait: |
| 407 |
stack = client.stacks.get(parsed_args.stack) |
| 408 |
stack_status, msg = event_utils.poll_for_events( |
| 409 |
client, stack.stack_name, action='UPDATE', marker=marker) |
| 410 |
if stack_status == 'UPDATE_FAILED': |
| 411 |
raise exc.CommandError(msg) |
| 412 |
|
| 413 |
return _show_stack(client, parsed_args.stack, format='table', |
| 414 |
short=True) |
| 415 |
|
| 416 |
|
| 417 |
class ShowStack(command.ShowOne): |
| 418 |
"""Show stack details.""" |
| 419 |
|
| 420 |
log = logging.getLogger(__name__ + ".ShowStack") |
| 421 |
|
| 422 |
def get_parser(self, prog_name): |
| 423 |
parser = super().get_parser(prog_name) |
| 424 |
parser.add_argument( |
| 425 |
'stack', |
| 426 |
metavar='<stack>', |
| 427 |
help='Stack to display (name or ID)', |
| 428 |
) |
| 429 |
parser.add_argument( |
| 430 |
'--no-resolve-outputs', action="store_true", |
| 431 |
help=_('Do not resolve outputs of the stack.') |
| 432 |
) |
| 433 |
return parser |
| 434 |
|
| 435 |
def take_action(self, parsed_args): |
| 436 |
self.log.debug("take_action(%s)", parsed_args) |
| 437 |
|
| 438 |
heat_client = self.app.client_manager.orchestration |
| 439 |
return _show_stack( |
| 440 |
heat_client, stack_id=parsed_args.stack, |
| 441 |
format=parsed_args.formatter, |
| 442 |
resolve_outputs=(not parsed_args.no_resolve_outputs)) |
| 443 |
|
| 444 |
|
| 445 |
def _show_stack(heat_client, stack_id, format='', short=False, |
| 446 |
resolve_outputs=True): |
| 447 |
try: |
| 448 |
_resolve_outputs = not short and resolve_outputs |
| 449 |
data = heat_client.stacks.get(stack_id=stack_id, |
| 450 |
resolve_outputs=_resolve_outputs) |
| 451 |
except heat_exc.HTTPNotFound: |
| 452 |
raise exc.CommandError('Stack not found: %s' % stack_id) |
| 453 |
else: |
| 454 |
|
| 455 |
columns = [ |
| 456 |
'id', |
| 457 |
'stack_name', |
| 458 |
'description', |
| 459 |
'creation_time', |
| 460 |
'updated_time', |
| 461 |
'stack_status', |
| 462 |
'stack_status_reason', |
| 463 |
] |
| 464 |
|
| 465 |
if not short: |
| 466 |
columns.append('parameters') |
| 467 |
if _resolve_outputs: |
| 468 |
columns.append('outputs') |
| 469 |
columns.append('links') |
| 470 |
|
| 471 |
exclude_columns = ('template_description',) |
| 472 |
for key in data.to_dict(): |
| 473 |
|
| 474 |
if key not in columns and key not in exclude_columns: |
| 475 |
columns.append(key) |
| 476 |
|
| 477 |
formatters = {} |
| 478 |
complex_formatter = None |
| 479 |
if format in 'table': |
| 480 |
complex_formatter = common.YamlColumn |
| 481 |
elif format in ('shell', 'value', 'html'): |
| 482 |
complex_formatter = common.JsonColumn |
| 483 |
if complex_formatter: |
| 484 |
formatters['parameters'] = complex_formatter |
| 485 |
formatters['outputs'] = complex_formatter |
| 486 |
formatters['links'] = complex_formatter |
| 487 |
formatters['tags'] = complex_formatter |
| 488 |
|
| 489 |
return columns, utils.get_item_properties(data, columns, |
| 490 |
formatters=formatters) |
| 491 |
|
| 492 |
|
| 493 |
class ListStack(command.Lister): |
| 494 |
"""List stacks.""" |
| 495 |
|
| 496 |
log = logging.getLogger(__name__ + '.ListStack') |
| 497 |
|
| 498 |
def get_parser(self, prog_name): |
| 499 |
parser = super().get_parser(prog_name) |
| 500 |
parser.add_argument( |
| 501 |
'--deleted', |
| 502 |
action='store_true', |
| 503 |
help=_('Include soft-deleted stacks in the stack listing') |
| 504 |
) |
| 505 |
parser.add_argument( |
| 506 |
'--nested', |
| 507 |
action='store_true', |
| 508 |
help=_('Include nested stacks in the stack listing') |
| 509 |
) |
| 510 |
parser.add_argument( |
| 511 |
'--hidden', |
| 512 |
action='store_true', |
| 513 |
help=_('Include hidden stacks in the stack listing') |
| 514 |
) |
| 515 |
parser.add_argument( |
| 516 |
'--property', |
| 517 |
dest='properties', |
| 518 |
metavar='<key=value>', |
| 519 |
help=_('Filter properties to apply on returned stacks (repeat to ' |
| 520 |
'filter on multiple properties)'), |
| 521 |
action='append' |
| 522 |
) |
| 523 |
parser.add_argument( |
| 524 |
'--project', |
| 525 |
dest='project', |
| 526 |
metavar='<project>', |
| 527 |
help=_('Filter stacks by project (name or ID) (admin only)'), |
| 528 |
) |
| 529 |
identity_common.add_project_domain_option_to_parser(parser) |
| 530 |
parser.add_argument( |
| 531 |
'--tags', |
| 532 |
metavar='<tag1,tag2...>', |
| 533 |
help=_('List of tags to filter by. Can be combined with ' |
| 534 |
'--tag-mode to specify how to filter tags') |
| 535 |
) |
| 536 |
parser.add_argument( |
| 537 |
'--tag-mode', |
| 538 |
metavar='<mode>', |
| 539 |
help=_('Method of filtering tags. Must be one of "any", "not", ' |
| 540 |
'or "not-any". If not specified, multiple tags will be ' |
| 541 |
'combined with the boolean AND expression') |
| 542 |
) |
| 543 |
parser.add_argument( |
| 544 |
'--limit', |
| 545 |
metavar='<limit>', |
| 546 |
type=int, |
| 547 |
help=_('The number of stacks returned') |
| 548 |
) |
| 549 |
parser.add_argument( |
| 550 |
'--marker', |
| 551 |
metavar='<id>', |
| 552 |
help=_('Only return stacks that appear after the given ID') |
| 553 |
) |
| 554 |
parser.add_argument( |
| 555 |
'--sort', |
| 556 |
metavar='<key>[:<direction>]', |
| 557 |
help=_('Sort output by selected keys and directions (asc or desc) ' |
| 558 |
'(default: asc). Specify multiple times to sort on ' |
| 559 |
'multiple properties') |
| 560 |
) |
| 561 |
parser.add_argument( |
| 562 |
'--all-projects', |
| 563 |
action='store_true', |
| 564 |
help=_('Include all projects (admin only)') |
| 565 |
) |
| 566 |
parser.add_argument( |
| 567 |
'--short', |
| 568 |
action='store_true', |
| 569 |
help=_('List fewer fields in output') |
| 570 |
) |
| 571 |
parser.add_argument( |
| 572 |
'--long', |
| 573 |
action='store_true', |
| 574 |
help=_('List additional fields in output, this is implied by ' |
| 575 |
'--all-projects') |
| 576 |
) |
| 577 |
return parser |
| 578 |
|
| 579 |
def take_action(self, parsed_args): |
| 580 |
self.log.debug("take_action(%s)", parsed_args) |
| 581 |
|
| 582 |
client = self.app.client_manager.orchestration |
| 583 |
identity_client = self.app.client_manager.identity |
| 584 |
|
| 585 |
project_id = None |
| 586 |
if parsed_args.project: |
| 587 |
project_id = identity_common.find_project( |
| 588 |
identity_client, |
| 589 |
parsed_args.project, |
| 590 |
parsed_args.project_domain).id |
| 591 |
|
| 592 |
return _list(client, args=parsed_args, project_id=project_id) |
| 593 |
|
| 594 |
|
| 595 |
class EnvironmentShowStack(format_utils.YamlFormat): |
| 596 |
"""Show a stack's environment.""" |
| 597 |
|
| 598 |
log = logging.getLogger(__name__) |
| 599 |
|
| 600 |
def get_parser(self, prog_name): |
| 601 |
parser = super().get_parser(prog_name) |
| 602 |
parser.add_argument( |
| 603 |
'stack', |
| 604 |
metavar='<NAME or ID>', |
| 605 |
help=_('Name or ID of stack to query') |
| 606 |
) |
| 607 |
return parser |
| 608 |
|
| 609 |
def take_action(self, parsed_args): |
| 610 |
self.log.debug('take_action(%s)', parsed_args) |
| 611 |
|
| 612 |
client = self.app.client_manager.orchestration |
| 613 |
|
| 614 |
try: |
| 615 |
env = client.stacks.environment(stack_id=parsed_args.stack) |
| 616 |
except heat_exc.HTTPNotFound: |
| 617 |
msg = _('Stack not found: %s') % parsed_args.stack |
| 618 |
raise exc.CommandError(msg) |
| 619 |
|
| 620 |
fields = ['parameters', 'resource_registry', 'parameter_defaults'] |
| 621 |
|
| 622 |
columns = [f for f in fields if f in env] |
| 623 |
data = [env[c] for c in columns] |
| 624 |
|
| 625 |
return columns, data |
| 626 |
|
| 627 |
|
| 628 |
class ListFileStack(format_utils.YamlFormat): |
| 629 |
"""Show a stack's files map.""" |
| 630 |
|
| 631 |
log = logging.getLogger(__name__) |
| 632 |
|
| 633 |
def get_parser(self, prog_name): |
| 634 |
parser = super().get_parser(prog_name) |
| 635 |
parser.add_argument( |
| 636 |
'stack', |
| 637 |
metavar='<NAME or ID>', |
| 638 |
help=_('Name or ID of stack to query') |
| 639 |
) |
| 640 |
return parser |
| 641 |
|
| 642 |
def take_action(self, parsed_args): |
| 643 |
self.log.debug('take_action(%s)', parsed_args) |
| 644 |
|
| 645 |
client = self.app.client_manager.orchestration |
| 646 |
|
| 647 |
try: |
| 648 |
files = client.stacks.files(stack_id=parsed_args.stack) |
| 649 |
except heat_exc.HTTPNotFound: |
| 650 |
msg = _('Stack not found: %s') % parsed_args.stack |
| 651 |
raise exc.CommandError(msg) |
| 652 |
|
| 653 |
return ['files'], [files] |
| 654 |
|
| 655 |
|
| 656 |
def _list(client, args=None, project_id=None): |
| 657 |
kwargs = {} |
| 658 |
columns = [ |
| 659 |
'ID', |
| 660 |
'Stack Name', |
| 661 |
'Stack Status', |
| 662 |
'Creation Time', |
| 663 |
'Updated Time', |
| 664 |
] |
| 665 |
|
| 666 |
if args: |
| 667 |
kwargs = {'limit': args.limit, |
| 668 |
'marker': args.marker, |
| 669 |
'filters': heat_utils.format_parameters(args.properties), |
| 670 |
'tags': None, |
| 671 |
'tags_any': None, |
| 672 |
'not_tags': None, |
| 673 |
'not_tags_any': None, |
| 674 |
'global_tenant': args.all_projects or args.long, |
| 675 |
'show_deleted': args.deleted, |
| 676 |
'show_hidden': args.hidden} |
| 677 |
|
| 678 |
if args.tags: |
| 679 |
if args.tag_mode: |
| 680 |
if args.tag_mode == 'any': |
| 681 |
kwargs['tags_any'] = args.tags |
| 682 |
elif args.tag_mode == 'not': |
| 683 |
kwargs['not_tags'] = args.tags |
| 684 |
elif args.tag_mode == 'not-any': |
| 685 |
kwargs['not_tags_any'] = args.tags |
| 686 |
else: |
| 687 |
err = _('tag mode must be one of "any", "not", "not-any"') |
| 688 |
raise exc.CommandError(err) |
| 689 |
else: |
| 690 |
kwargs['tags'] = args.tags |
| 691 |
|
| 692 |
if args.short: |
| 693 |
columns.pop() |
| 694 |
columns.pop() |
| 695 |
if args.long: |
| 696 |
columns.insert(2, 'Stack Owner') |
| 697 |
|
| 698 |
if args.nested: |
| 699 |
columns.append('Parent') |
| 700 |
kwargs['show_nested'] = True |
| 701 |
|
| 702 |
if args.deleted: |
| 703 |
columns.append('Deletion Time') |
| 704 |
|
| 705 |
if project_id: |
| 706 |
kwargs['tenant'] = project_id |
| 707 |
|
| 708 |
data = client.stacks.list(**kwargs) |
| 709 |
data = list(data) |
| 710 |
for stk in data: |
| 711 |
if hasattr(stk, 'project'): |
| 712 |
columns.insert(2, 'Project') |
| 713 |
break |
| 714 |
data = utils.sort_items(data, args.sort if args else None) |
| 715 |
|
| 716 |
return ( |
| 717 |
columns, |
| 718 |
(utils.get_item_properties(s, columns) for s in data) |
| 719 |
) |
| 720 |
|
| 721 |
|
| 722 |
class DeleteStack(command.Command): |
| 723 |
"""Delete stack(s).""" |
| 724 |
|
| 725 |
log = logging.getLogger(__name__ + ".DeleteStack") |
| 726 |
|
| 727 |
def get_parser(self, prog_name): |
| 728 |
parser = super().get_parser(prog_name) |
| 729 |
parser.add_argument( |
| 730 |
'stack', |
| 731 |
metavar='<stack>', |
| 732 |
nargs='+', |
| 733 |
help=_('Stack(s) to delete (name or ID)') |
| 734 |
) |
| 735 |
parser.add_argument( |
| 736 |
'-y', '--yes', |
| 737 |
action='store_true', |
| 738 |
help=_('Skip yes/no prompt (assume yes)') |
| 739 |
) |
| 740 |
parser.add_argument( |
| 741 |
'--wait', |
| 742 |
action='store_true', |
| 743 |
help=_('Wait for stack delete to complete') |
| 744 |
) |
| 745 |
return parser |
| 746 |
|
| 747 |
def take_action(self, parsed_args): |
| 748 |
self.log.debug("take_action(%s)", parsed_args) |
| 749 |
|
| 750 |
heat_client = self.app.client_manager.orchestration |
| 751 |
|
| 752 |
try: |
| 753 |
if not parsed_args.yes and sys.stdin.isatty(): |
| 754 |
prompt_response = input( |
| 755 |
_("Are you sure you want to delete this stack(s) [y/N]? ") |
| 756 |
).lower() |
| 757 |
if not prompt_response.startswith('y'): |
| 758 |
self.log.info('User did not confirm stack delete so ' |
| 759 |
'taking no action.') |
| 760 |
return |
| 761 |
except KeyboardInterrupt: |
| 762 |
self.log.info('User did not confirm stack delete ' |
| 763 |
'(ctrl-c) so taking no action.') |
| 764 |
return |
| 765 |
except EOFError: |
| 766 |
self.log.info('User did not confirm stack delete ' |
| 767 |
'(ctrl-d) so taking no action.') |
| 768 |
return |
| 769 |
|
| 770 |
failure_count = 0 |
| 771 |
stacks_waiting = [] |
| 772 |
for sid in parsed_args.stack: |
| 773 |
marker = None |
| 774 |
if parsed_args.wait: |
| 775 |
try: |
| 776 |
|
| 777 |
events = event_utils.get_events(heat_client, |
| 778 |
stack_id=sid, |
| 779 |
event_args={ |
| 780 |
'sort_dir': 'desc'}, |
| 781 |
limit=1) |
| 782 |
if events: |
| 783 |
marker = events[0].id |
| 784 |
except heat_exc.CommandError as ex: |
| 785 |
failure_count += 1 |
| 786 |
print(ex) |
| 787 |
continue |
| 788 |
|
| 789 |
try: |
| 790 |
heat_client.stacks.delete(sid) |
| 791 |
stacks_waiting.append((sid, marker)) |
| 792 |
except heat_exc.HTTPNotFound: |
| 793 |
failure_count += 1 |
| 794 |
print(_('Stack not found: %s') % sid) |
| 795 |
except heat_exc.Forbidden: |
| 796 |
failure_count += 1 |
| 797 |
print(_('Forbidden: %s') % sid) |
| 798 |
|
| 799 |
if parsed_args.wait: |
| 800 |
for sid, marker in stacks_waiting: |
| 801 |
try: |
| 802 |
stack_status, msg = event_utils.poll_for_events( |
| 803 |
heat_client, sid, action='DELETE', marker=marker) |
| 804 |
except heat_exc.CommandError: |
| 805 |
continue |
| 806 |
if stack_status == 'DELETE_FAILED': |
| 807 |
failure_count += 1 |
| 808 |
print(msg) |
| 809 |
|
| 810 |
if failure_count: |
| 811 |
msg = (_('Unable to delete %(count)d of the %(total)d stacks.') % |
| 812 |
{'count': failure_count, 'total': len(parsed_args.stack)}) |
| 813 |
raise exc.CommandError(msg) |
| 814 |
|
| 815 |
|
| 816 |
class AdoptStack(command.ShowOne): |
| 817 |
"""Adopt a stack.""" |
| 818 |
|
| 819 |
log = logging.getLogger(__name__ + '.AdoptStack') |
| 820 |
|
| 821 |
def get_parser(self, prog_name): |
| 822 |
parser = super().get_parser(prog_name) |
| 823 |
parser.add_argument( |
| 824 |
'name', |
| 825 |
metavar='<stack-name>', |
| 826 |
help=_('Name of the stack to adopt') |
| 827 |
) |
| 828 |
parser.add_argument( |
| 829 |
'-e', '--environment', |
| 830 |
metavar='<environment>', |
| 831 |
action='append', |
| 832 |
help=_('Path to the environment. Can be specified multiple times') |
| 833 |
) |
| 834 |
parser.add_argument( |
| 835 |
'--timeout', |
| 836 |
metavar='<timeout>', |
| 837 |
type=int, |
| 838 |
help=_('Stack creation timeout in minutes') |
| 839 |
) |
| 840 |
parser.add_argument( |
| 841 |
'--enable-rollback', |
| 842 |
action='store_true', |
| 843 |
help=_('Enable rollback on create/update failure') |
| 844 |
) |
| 845 |
parser.add_argument( |
| 846 |
'--parameter', |
| 847 |
metavar='<key=value>', |
| 848 |
action='append', |
| 849 |
help=_('Parameter values used to create the stack. Can be ' |
| 850 |
'specified multiple times') |
| 851 |
) |
| 852 |
parser.add_argument( |
| 853 |
'--wait', |
| 854 |
action='store_true', |
| 855 |
help=_('Wait until stack adopt completes') |
| 856 |
) |
| 857 |
parser.add_argument( |
| 858 |
'--adopt-file', |
| 859 |
metavar='<adopt-file>', |
| 860 |
required=True, |
| 861 |
help=_('Path to adopt stack data file') |
| 862 |
) |
| 863 |
return parser |
| 864 |
|
| 865 |
def take_action(self, parsed_args): |
| 866 |
self.log.debug('take_action(%s)', parsed_args) |
| 867 |
|
| 868 |
client = self.app.client_manager.orchestration |
| 869 |
|
| 870 |
env_files, env = ( |
| 871 |
template_utils.process_multiple_environments_and_files( |
| 872 |
env_paths=parsed_args.environment)) |
| 873 |
|
| 874 |
adopt_url = heat_utils.normalise_file_path_to_url( |
| 875 |
parsed_args.adopt_file) |
| 876 |
adopt_data = request.urlopen(adopt_url).read().decode('utf-8') |
| 877 |
yaml_adopt_data = yaml.safe_load(adopt_data) or {} |
| 878 |
files = yaml_adopt_data.get('files', {}) |
| 879 |
files.update(env_files) |
| 880 |
fields = { |
| 881 |
'stack_name': parsed_args.name, |
| 882 |
'disable_rollback': not parsed_args.enable_rollback, |
| 883 |
'adopt_stack_data': adopt_data, |
| 884 |
'parameters': heat_utils.format_parameters(parsed_args.parameter), |
| 885 |
'files': files, |
| 886 |
'environment': env, |
| 887 |
'timeout': parsed_args.timeout |
| 888 |
} |
| 889 |
|
| 890 |
stack = client.stacks.create(**fields)['stack'] |
| 891 |
|
| 892 |
if parsed_args.wait: |
| 893 |
stack_status, msg = event_utils.poll_for_events( |
| 894 |
client, parsed_args.name, action='ADOPT') |
| 895 |
if stack_status == 'ADOPT_FAILED': |
| 896 |
raise exc.CommandError(msg) |
| 897 |
|
| 898 |
return _show_stack(client, stack['id'], format='table', short=True) |
| 899 |
|
| 900 |
|
| 901 |
class AbandonStack(format_utils.JsonFormat): |
| 902 |
"""Abandon stack and output results.""" |
| 903 |
|
| 904 |
log = logging.getLogger(__name__ + '.AbandonStack') |
| 905 |
|
| 906 |
def get_parser(self, prog_name): |
| 907 |
parser = super().get_parser(prog_name) |
| 908 |
parser.add_argument( |
| 909 |
'stack', |
| 910 |
metavar='<stack>', |
| 911 |
help=_('Name or ID of stack to abandon') |
| 912 |
) |
| 913 |
parser.add_argument( |
| 914 |
'--output-file', |
| 915 |
metavar='<output-file>', |
| 916 |
help=_('File to output abandon results') |
| 917 |
) |
| 918 |
|
| 919 |
return parser |
| 920 |
|
| 921 |
def take_action(self, parsed_args): |
| 922 |
self.log.debug('take_action(%s)', parsed_args) |
| 923 |
|
| 924 |
client = self.app.client_manager.orchestration |
| 925 |
|
| 926 |
try: |
| 927 |
stack = client.stacks.abandon(stack_id=parsed_args.stack) |
| 928 |
except heat_exc.HTTPNotFound: |
| 929 |
msg = _('Stack not found: %s') % parsed_args.stack |
| 930 |
raise exc.CommandError(msg) |
| 931 |
|
| 932 |
if parsed_args.output_file is not None: |
| 933 |
try: |
| 934 |
with open(parsed_args.output_file, 'w') as f: |
| 935 |
f.write(jsonutils.dumps(stack, indent=2)) |
| 936 |
return [], None |
| 937 |
except OSError as e: |
| 938 |
raise exc.CommandError(str(e)) |
| 939 |
|
| 940 |
data = list(stack.values()) |
| 941 |
columns = list(stack.keys()) |
| 942 |
return columns, data |
| 943 |
|
| 944 |
|
| 945 |
class ExportStack(format_utils.JsonFormat): |
| 946 |
"""Export stack data json.""" |
| 947 |
|
| 948 |
log = logging.getLogger(__name__ + '.ExportStack') |
| 949 |
|
| 950 |
def get_parser(self, prog_name): |
| 951 |
parser = super().get_parser(prog_name) |
| 952 |
parser.add_argument( |
| 953 |
'stack', |
| 954 |
metavar='<stack>', |
| 955 |
help=_('Name or ID of stack to export') |
| 956 |
) |
| 957 |
parser.add_argument( |
| 958 |
'--output-file', |
| 959 |
metavar='<output-file>', |
| 960 |
help=_('File to output export data') |
| 961 |
) |
| 962 |
|
| 963 |
return parser |
| 964 |
|
| 965 |
def take_action(self, parsed_args): |
| 966 |
self.log.debug('take_action(%s)', parsed_args) |
| 967 |
|
| 968 |
client = self.app.client_manager.orchestration |
| 969 |
|
| 970 |
try: |
| 971 |
data_info = client.stacks.export(stack_id=parsed_args.stack) |
| 972 |
except heat_exc.HTTPNotFound: |
| 973 |
msg = _('Stack not found: %s') % parsed_args.stack |
| 974 |
raise exc.CommandError(msg) |
| 975 |
|
| 976 |
if parsed_args.output_file is not None: |
| 977 |
try: |
| 978 |
with open(parsed_args.output_file, 'w') as f: |
| 979 |
f.write(jsonutils.dumps(data_info, indent=2)) |
| 980 |
return [], None |
| 981 |
except OSError as e: |
| 982 |
raise exc.CommandError(str(e)) |
| 983 |
|
| 984 |
data = list(data_info.values()) |
| 985 |
columns = list(data_info.keys()) |
| 986 |
return columns, data |
| 987 |
|
| 988 |
|
| 989 |
class OutputShowStack(command.ShowOne): |
| 990 |
"""Show stack output.""" |
| 991 |
|
| 992 |
log = logging.getLogger(__name__ + '.OutputShowStack') |
| 993 |
|
| 994 |
def get_parser(self, prog_name): |
| 995 |
parser = super().get_parser(prog_name) |
| 996 |
parser.add_argument( |
| 997 |
'stack', |
| 998 |
metavar='<stack>', |
| 999 |
help=_('Name or ID of stack to query') |
| 1000 |
) |
| 1001 |
parser.add_argument( |
| 1002 |
'output', |
| 1003 |
metavar='<output>', |
| 1004 |
nargs='?', |
| 1005 |
default=None, |
| 1006 |
help=_('Name of an output to display') |
| 1007 |
) |
| 1008 |
parser.add_argument( |
| 1009 |
'--all', |
| 1010 |
action='store_true', |
| 1011 |
help=_('Display all stack outputs') |
| 1012 |
) |
| 1013 |
return parser |
| 1014 |
|
| 1015 |
def take_action(self, parsed_args): |
| 1016 |
self.log.debug('take_action(%s)', parsed_args) |
| 1017 |
|
| 1018 |
client = self.app.client_manager.orchestration |
| 1019 |
|
| 1020 |
if not parsed_args.all and parsed_args.output is None: |
| 1021 |
msg = _('Either <OUTPUT NAME> or --all must be specified.') |
| 1022 |
raise exc.CommandError(msg) |
| 1023 |
|
| 1024 |
if parsed_args.all and parsed_args.output is not None: |
| 1025 |
msg = _('Cannot specify both <OUTPUT NAME> and --all.') |
| 1026 |
raise exc.CommandError(msg) |
| 1027 |
|
| 1028 |
if parsed_args.all: |
| 1029 |
try: |
| 1030 |
stack = client.stacks.get(parsed_args.stack) |
| 1031 |
except heat_exc.HTTPNotFound: |
| 1032 |
msg = _('Stack not found: %s') % parsed_args.stack |
| 1033 |
raise exc.CommandError(msg) |
| 1034 |
|
| 1035 |
outputs = stack.to_dict().get('outputs', []) |
| 1036 |
columns = [] |
| 1037 |
values = [] |
| 1038 |
for output in outputs: |
| 1039 |
columns.append(output['output_key']) |
| 1040 |
values.append(common.JsonColumn(output)) |
| 1041 |
|
| 1042 |
return columns, values |
| 1043 |
|
| 1044 |
try: |
| 1045 |
output = client.stacks.output_show(parsed_args.stack, |
| 1046 |
parsed_args.output)['output'] |
| 1047 |
except heat_exc.HTTPNotFound: |
| 1048 |
msg = _('Stack %(id)s or output %(out)s not found.') % { |
| 1049 |
'id': parsed_args.stack, 'out': parsed_args.output} |
| 1050 |
try: |
| 1051 |
output = None |
| 1052 |
stack = client.stacks.get(parsed_args.stack).to_dict() |
| 1053 |
for o in stack.get('outputs', []): |
| 1054 |
if o['output_key'] == parsed_args.output: |
| 1055 |
output = o |
| 1056 |
break |
| 1057 |
if output is None: |
| 1058 |
raise exc.CommandError(msg) |
| 1059 |
except heat_exc.HTTPNotFound: |
| 1060 |
raise exc.CommandError(msg) |
| 1061 |
|
| 1062 |
if 'output_error' in output: |
| 1063 |
msg = _('Output error: %s') % output['output_error'] |
| 1064 |
raise exc.CommandError(msg) |
| 1065 |
|
| 1066 |
return self.dict2columns(output) |
| 1067 |
|
| 1068 |
|
| 1069 |
class OutputListStack(command.Lister): |
| 1070 |
"""List stack outputs.""" |
| 1071 |
|
| 1072 |
log = logging.getLogger(__name__ + '.OutputListStack') |
| 1073 |
|
| 1074 |
def get_parser(self, prog_name): |
| 1075 |
parser = super().get_parser(prog_name) |
| 1076 |
parser.add_argument( |
| 1077 |
'stack', |
| 1078 |
metavar='<stack>', |
| 1079 |
help=_('Name or ID of stack to query') |
| 1080 |
) |
| 1081 |
return parser |
| 1082 |
|
| 1083 |
def take_action(self, parsed_args): |
| 1084 |
self.log.debug('take_action(%s)', parsed_args) |
| 1085 |
|
| 1086 |
client = self.app.client_manager.orchestration |
| 1087 |
|
| 1088 |
try: |
| 1089 |
outputs = client.stacks.output_list(parsed_args.stack)['outputs'] |
| 1090 |
except heat_exc.HTTPNotFound: |
| 1091 |
try: |
| 1092 |
outputs = client.stacks.get( |
| 1093 |
parsed_args.stack).to_dict()['outputs'] |
| 1094 |
except heat_exc.HTTPNotFound: |
| 1095 |
msg = _('Stack not found: %s') % parsed_args.stack |
| 1096 |
raise exc.CommandError(msg) |
| 1097 |
|
| 1098 |
columns = ['output_key', 'description'] |
| 1099 |
|
| 1100 |
return ( |
| 1101 |
columns, |
| 1102 |
(utils.get_dict_properties(s, columns) for s in outputs) |
| 1103 |
) |
| 1104 |
|
| 1105 |
|
| 1106 |
class TemplateShowStack(format_utils.YamlFormat): |
| 1107 |
"""Display stack template.""" |
| 1108 |
|
| 1109 |
log = logging.getLogger(__name__ + '.TemplateShowStack') |
| 1110 |
|
| 1111 |
def get_parser(self, prog_name): |
| 1112 |
parser = super().get_parser(prog_name) |
| 1113 |
parser.add_argument( |
| 1114 |
'stack', |
| 1115 |
metavar='<stack>', |
| 1116 |
help=_('Name or ID of stack to query') |
| 1117 |
) |
| 1118 |
return parser |
| 1119 |
|
| 1120 |
def take_action(self, parsed_args): |
| 1121 |
self.log.debug('take_action(%s)', parsed_args) |
| 1122 |
|
| 1123 |
client = self.app.client_manager.orchestration |
| 1124 |
|
| 1125 |
try: |
| 1126 |
template = client.stacks.template(stack_id=parsed_args.stack) |
| 1127 |
except heat_exc.HTTPNotFound: |
| 1128 |
msg = _('Stack not found: %s') % parsed_args.stack |
| 1129 |
raise exc.CommandError(msg) |
| 1130 |
|
| 1131 |
return self.dict2columns(template) |
| 1132 |
|
| 1133 |
|
| 1134 |
class StackActionBase(command.Lister): |
| 1135 |
"""Stack actions base.""" |
| 1136 |
|
| 1137 |
log = logging.getLogger(__name__ + '.StackActionBase') |
| 1138 |
|
| 1139 |
def _get_parser(self, prog_name, stack_help, wait_help): |
| 1140 |
parser = super().get_parser(prog_name) |
| 1141 |
parser.add_argument( |
| 1142 |
'stack', |
| 1143 |
metavar='<stack>', |
| 1144 |
nargs="+", |
| 1145 |
help=stack_help |
| 1146 |
) |
| 1147 |
parser.add_argument( |
| 1148 |
'--wait', |
| 1149 |
action='store_true', |
| 1150 |
help=wait_help |
| 1151 |
) |
| 1152 |
return parser |
| 1153 |
|
| 1154 |
def _take_action(self, parsed_args, action, action_name=None): |
| 1155 |
self.log.debug("take_action(%s)", parsed_args) |
| 1156 |
heat_client = self.app.client_manager.orchestration |
| 1157 |
return _stacks_action( |
| 1158 |
parsed_args, |
| 1159 |
heat_client, |
| 1160 |
action, |
| 1161 |
action_name |
| 1162 |
) |
| 1163 |
|
| 1164 |
|
| 1165 |
def _stacks_action(parsed_args, heat_client, action, action_name=None): |
| 1166 |
rows = [] |
| 1167 |
columns = [ |
| 1168 |
'ID', |
| 1169 |
'Stack Name', |
| 1170 |
'Stack Status', |
| 1171 |
'Creation Time', |
| 1172 |
'Updated Time' |
| 1173 |
] |
| 1174 |
for stack in parsed_args.stack: |
| 1175 |
data = _stack_action(stack, parsed_args, heat_client, action, |
| 1176 |
action_name) |
| 1177 |
rows += [utils.get_dict_properties(data.to_dict(), columns)] |
| 1178 |
return (columns, rows) |
| 1179 |
|
| 1180 |
|
| 1181 |
def _stack_action(stack, parsed_args, heat_client, action, action_name=None): |
| 1182 |
if parsed_args.wait: |
| 1183 |
|
| 1184 |
events = event_utils.get_events(heat_client, |
| 1185 |
stack_id=stack, |
| 1186 |
event_args={'sort_dir': 'desc'}, |
| 1187 |
limit=1) |
| 1188 |
marker = events[0].id if events else None |
| 1189 |
|
| 1190 |
try: |
| 1191 |
action(stack) |
| 1192 |
except heat_exc.HTTPNotFound: |
| 1193 |
msg = _('Stack not found: %s') % stack |
| 1194 |
raise exc.CommandError(msg) |
| 1195 |
|
| 1196 |
if parsed_args.wait: |
| 1197 |
s = heat_client.stacks.get(stack) |
| 1198 |
stack_status, msg = event_utils.poll_for_events( |
| 1199 |
heat_client, s.stack_name, action=action_name, marker=marker) |
| 1200 |
if action_name: |
| 1201 |
if stack_status == '%s_FAILED' % action_name: |
| 1202 |
raise exc.CommandError(msg) |
| 1203 |
else: |
| 1204 |
if stack_status.endswith('_FAILED'): |
| 1205 |
raise exc.CommandError(msg) |
| 1206 |
|
| 1207 |
return heat_client.stacks.get(stack) |
| 1208 |
|
| 1209 |
|
| 1210 |
class SuspendStack(StackActionBase): |
| 1211 |
"""Suspend a stack.""" |
| 1212 |
|
| 1213 |
log = logging.getLogger(__name__ + '.SuspendStack') |
| 1214 |
|
| 1215 |
def get_parser(self, prog_name): |
| 1216 |
return self._get_parser( |
| 1217 |
prog_name, |
| 1218 |
_('Stack(s) to suspend (name or ID)'), |
| 1219 |
_('Wait for suspend to complete') |
| 1220 |
) |
| 1221 |
|
| 1222 |
def take_action(self, parsed_args): |
| 1223 |
return self._take_action( |
| 1224 |
parsed_args, |
| 1225 |
self.app.client_manager.orchestration.actions.suspend, |
| 1226 |
'SUSPEND' |
| 1227 |
) |
| 1228 |
|
| 1229 |
|
| 1230 |
class ResumeStack(StackActionBase): |
| 1231 |
"""Resume a stack.""" |
| 1232 |
|
| 1233 |
log = logging.getLogger(__name__ + '.ResumeStack') |
| 1234 |
|
| 1235 |
def get_parser(self, prog_name): |
| 1236 |
return self._get_parser( |
| 1237 |
prog_name, |
| 1238 |
_('Stack(s) to resume (name or ID)'), |
| 1239 |
_('Wait for resume to complete') |
| 1240 |
) |
| 1241 |
|
| 1242 |
def take_action(self, parsed_args): |
| 1243 |
return self._take_action( |
| 1244 |
parsed_args, |
| 1245 |
self.app.client_manager.orchestration.actions.resume, |
| 1246 |
'RESUME' |
| 1247 |
) |
| 1248 |
|
| 1249 |
|
| 1250 |
class CheckStack(StackActionBase): |
| 1251 |
"""Check a stack.""" |
| 1252 |
|
| 1253 |
log = logging.getLogger(__name__ + '.CheckStack') |
| 1254 |
|
| 1255 |
def get_parser(self, prog_name): |
| 1256 |
return self._get_parser( |
| 1257 |
prog_name, |
| 1258 |
_('Stack(s) to check update (name or ID)'), |
| 1259 |
_('Wait for check to complete') |
| 1260 |
) |
| 1261 |
|
| 1262 |
def take_action(self, parsed_args): |
| 1263 |
return self._take_action( |
| 1264 |
parsed_args, |
| 1265 |
self.app.client_manager.orchestration.actions.check, |
| 1266 |
'CHECK' |
| 1267 |
) |
| 1268 |
|
| 1269 |
|
| 1270 |
class CancelStack(StackActionBase): |
| 1271 |
"""Cancel current task for a stack. |
| 1272 |
|
| 1273 |
Supported tasks for cancellation: |
| 1274 |
|
| 1275 |
* update |
| 1276 |
* create |
| 1277 |
""" |
| 1278 |
|
| 1279 |
log = logging.getLogger(__name__ + '.CancelStack') |
| 1280 |
|
| 1281 |
def get_parser(self, prog_name): |
| 1282 |
parser = self._get_parser( |
| 1283 |
prog_name, |
| 1284 |
_('Stack(s) to cancel (name or ID)'), |
| 1285 |
_('Wait for cancel to complete') |
| 1286 |
) |
| 1287 |
parser.add_argument( |
| 1288 |
'--no-rollback', |
| 1289 |
action='store_true', |
| 1290 |
help=_('Cancel without rollback') |
| 1291 |
) |
| 1292 |
return parser |
| 1293 |
|
| 1294 |
def take_action(self, parsed_args): |
| 1295 |
self.log.debug("take_action(%s)", parsed_args) |
| 1296 |
rows = [] |
| 1297 |
columns = [ |
| 1298 |
'ID', |
| 1299 |
'Stack Name', |
| 1300 |
'Stack Status', |
| 1301 |
'Creation Time', |
| 1302 |
'Updated Time' |
| 1303 |
] |
| 1304 |
heat_client = self.app.client_manager.orchestration |
| 1305 |
if parsed_args.no_rollback: |
| 1306 |
action = heat_client.actions.cancel_without_rollback |
| 1307 |
allowed_statuses = ['create_in_progress', |
| 1308 |
'update_in_progress'] |
| 1309 |
else: |
| 1310 |
action = heat_client.actions.cancel_update |
| 1311 |
allowed_statuses = ['update_in_progress'] |
| 1312 |
for stack in parsed_args.stack: |
| 1313 |
try: |
| 1314 |
data = heat_client.stacks.get(stack_id=stack) |
| 1315 |
except heat_exc.HTTPNotFound: |
| 1316 |
raise exc.CommandError('Stack not found: %s' % stack) |
| 1317 |
status = getattr(data, 'stack_status').lower() |
| 1318 |
if status in allowed_statuses: |
| 1319 |
data = _stack_action( |
| 1320 |
stack, |
| 1321 |
parsed_args, |
| 1322 |
heat_client, |
| 1323 |
action |
| 1324 |
) |
| 1325 |
rows += [utils.get_dict_properties(data.to_dict(), columns)] |
| 1326 |
else: |
| 1327 |
err = _("Stack %(id)s with status \'%(status)s\' " |
| 1328 |
"not in cancelable state") % { |
| 1329 |
'id': stack, 'status': status} |
| 1330 |
raise exc.CommandError(err) |
| 1331 |
|
| 1332 |
return (columns, rows) |
| 1333 |
|
| 1334 |
|
| 1335 |
class StackHookPoll(command.Lister): |
| 1336 |
'''List resources with pending hook for a stack.''' |
| 1337 |
|
| 1338 |
log = logging.getLogger(__name__ + '.StackHookPoll') |
| 1339 |
|
| 1340 |
def get_parser(self, prog_name): |
| 1341 |
parser = super().get_parser(prog_name) |
| 1342 |
parser.add_argument( |
| 1343 |
'stack', |
| 1344 |
metavar='<stack>', |
| 1345 |
help=_('Stack to display (name or ID)') |
| 1346 |
) |
| 1347 |
parser.add_argument( |
| 1348 |
'--nested-depth', |
| 1349 |
metavar='<nested-depth>', |
| 1350 |
help=_('Depth of nested stacks from which to display hooks') |
| 1351 |
) |
| 1352 |
return parser |
| 1353 |
|
| 1354 |
def take_action(self, parsed_args): |
| 1355 |
self.log.debug("take_action(%s)", parsed_args) |
| 1356 |
heat_client = self.app.client_manager.orchestration |
| 1357 |
return _hook_poll( |
| 1358 |
parsed_args, |
| 1359 |
heat_client |
| 1360 |
) |
| 1361 |
|
| 1362 |
|
| 1363 |
def _hook_poll(args, heat_client): |
| 1364 |
"""List resources with pending hook for a stack.""" |
| 1365 |
|
| 1366 |
|
| 1367 |
|
| 1368 |
|
| 1369 |
|
| 1370 |
|
| 1371 |
|
| 1372 |
|
| 1373 |
|
| 1374 |
|
| 1375 |
|
| 1376 |
columns = ['ID', 'Resource Status Reason', 'Resource Status', 'Event Time'] |
| 1377 |
|
| 1378 |
if args.nested_depth: |
| 1379 |
try: |
| 1380 |
nested_depth = int(args.nested_depth) |
| 1381 |
except ValueError: |
| 1382 |
msg = _("--nested-depth invalid value %s") % args.nested_depth |
| 1383 |
raise exc.CommandError(msg) |
| 1384 |
columns.append('Stack Name') |
| 1385 |
else: |
| 1386 |
nested_depth = 0 |
| 1387 |
|
| 1388 |
hook_type = hook_utils.get_hook_type_via_status(heat_client, args.stack) |
| 1389 |
event_args = {'sort_dir': 'asc'} |
| 1390 |
hook_events = event_utils.get_hook_events( |
| 1391 |
heat_client, stack_id=args.stack, event_args=event_args, |
| 1392 |
nested_depth=nested_depth, hook_type=hook_type) |
| 1393 |
|
| 1394 |
if len(hook_events) >= 1: |
| 1395 |
if hasattr(hook_events[0], 'resource_name'): |
| 1396 |
columns.insert(0, 'Resource Name') |
| 1397 |
else: |
| 1398 |
columns.insert(0, 'Logical Resource ID') |
| 1399 |
|
| 1400 |
rows = (utils.get_item_properties(h, columns) for h in hook_events) |
| 1401 |
return (columns, rows) |
| 1402 |
|
| 1403 |
|
| 1404 |
class StackHookClear(command.Command): |
| 1405 |
"""Clear resource hooks on a given stack.""" |
| 1406 |
|
| 1407 |
log = logging.getLogger(__name__ + '.StackHookClear') |
| 1408 |
|
| 1409 |
def get_parser(self, prog_name): |
| 1410 |
parser = super().get_parser(prog_name) |
| 1411 |
parser.add_argument( |
| 1412 |
'stack', |
| 1413 |
metavar='<stack>', |
| 1414 |
help=_('Stack to display (name or ID)') |
| 1415 |
) |
| 1416 |
parser.add_argument( |
| 1417 |
'--pre-create', |
| 1418 |
action='store_true', |
| 1419 |
help=_('Clear the pre-create hooks') |
| 1420 |
) |
| 1421 |
parser.add_argument( |
| 1422 |
'--pre-update', |
| 1423 |
action='store_true', |
| 1424 |
help=_('Clear the pre-update hooks') |
| 1425 |
) |
| 1426 |
parser.add_argument( |
| 1427 |
'--pre-delete', |
| 1428 |
action='store_true', |
| 1429 |
help=_('Clear the pre-delete hooks') |
| 1430 |
) |
| 1431 |
parser.add_argument( |
| 1432 |
'hook', |
| 1433 |
metavar='<resource>', |
| 1434 |
nargs='+', |
| 1435 |
help=_('Resource names with hooks to clear. Resources ' |
| 1436 |
'in nested stacks can be set using slash as a separator: ' |
| 1437 |
'``nested_stack/another/my_resource``. You can use ' |
| 1438 |
'wildcards to match multiple stacks or resources: ' |
| 1439 |
'``nested_stack/an*/*_resource``') |
| 1440 |
) |
| 1441 |
return parser |
| 1442 |
|
| 1443 |
def take_action(self, parsed_args): |
| 1444 |
self.log.debug("take_action(%s)", parsed_args) |
| 1445 |
heat_client = self.app.client_manager.orchestration |
| 1446 |
return _hook_clear( |
| 1447 |
parsed_args, |
| 1448 |
heat_client |
| 1449 |
) |
| 1450 |
|
| 1451 |
|
| 1452 |
def _hook_clear(args, heat_client): |
| 1453 |
"""Clear resource hooks on a given stack.""" |
| 1454 |
if args.pre_create: |
| 1455 |
hook_type = 'pre-create' |
| 1456 |
elif args.pre_update: |
| 1457 |
hook_type = 'pre-update' |
| 1458 |
elif args.pre_delete: |
| 1459 |
hook_type = 'pre-delete' |
| 1460 |
else: |
| 1461 |
hook_type = hook_utils.get_hook_type_via_status(heat_client, |
| 1462 |
args.stack) |
| 1463 |
|
| 1464 |
for hook_string in args.hook: |
| 1465 |
hook = [b for b in hook_string.split('/') if b] |
| 1466 |
resource_pattern = hook[-1] |
| 1467 |
stack_id = args.stack |
| 1468 |
|
| 1469 |
hook_utils.clear_wildcard_hooks(heat_client, stack_id, hook[:-1], |
| 1470 |
hook_type, resource_pattern) |