| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
"""Orchestration v1 Stack Snapshot implementations.""" |
| 15 |
|
| 16 |
import logging |
| 17 |
import sys |
| 18 |
|
| 19 |
from osc_lib.command import command |
| 20 |
from osc_lib import exceptions as exc |
| 21 |
from osc_lib.i18n import _ |
| 22 |
from osc_lib import utils |
| 23 |
|
| 24 |
from heatclient.common import format_utils |
| 25 |
from heatclient import exc as heat_exc |
| 26 |
|
| 27 |
|
| 28 |
class ListSnapshot(command.Lister): |
| 29 |
"""List stack snapshots.""" |
| 30 |
|
| 31 |
log = logging.getLogger(__name__ + ".ListSnapshot") |
| 32 |
|
| 33 |
def get_parser(self, prog_name): |
| 34 |
parser = super().get_parser(prog_name) |
| 35 |
parser.add_argument( |
| 36 |
'stack', |
| 37 |
metavar='<stack>', |
| 38 |
help=_('Name or ID of stack containing the snapshots') |
| 39 |
) |
| 40 |
return parser |
| 41 |
|
| 42 |
def take_action(self, parsed_args): |
| 43 |
self.log.debug('take_action(%s)', parsed_args) |
| 44 |
heat_client = self.app.client_manager.orchestration |
| 45 |
return self._list_snapshot(heat_client, parsed_args) |
| 46 |
|
| 47 |
def _list_snapshot(self, heat_client, parsed_args): |
| 48 |
fields = {'stack_id': parsed_args.stack} |
| 49 |
try: |
| 50 |
snapshots = heat_client.stacks.snapshot_list(**fields) |
| 51 |
except heat_exc.HTTPNotFound: |
| 52 |
raise exc.CommandError(_('Stack not found: %s') % |
| 53 |
parsed_args.stack) |
| 54 |
|
| 55 |
columns = [ |
| 56 |
'id', |
| 57 |
'name', |
| 58 |
'action', |
| 59 |
'status', |
| 60 |
'status_reason', |
| 61 |
'creation_time' |
| 62 |
] |
| 63 |
return ( |
| 64 |
columns, |
| 65 |
(utils.get_dict_properties(s, columns) |
| 66 |
for s in snapshots['snapshots']) |
| 67 |
) |
| 68 |
|
| 69 |
|
| 70 |
class ShowSnapshot(format_utils.YamlFormat): |
| 71 |
"""Show stack snapshot.""" |
| 72 |
|
| 73 |
log = logging.getLogger(__name__ + ".ShowSnapshot") |
| 74 |
|
| 75 |
def get_parser(self, prog_name): |
| 76 |
parser = super().get_parser(prog_name) |
| 77 |
parser.add_argument( |
| 78 |
'stack', |
| 79 |
metavar='<stack>', |
| 80 |
help=_('Name or ID of stack containing the snapshot') |
| 81 |
) |
| 82 |
parser.add_argument( |
| 83 |
'snapshot', |
| 84 |
metavar='<snapshot>', |
| 85 |
help=_('ID of the snapshot to show') |
| 86 |
) |
| 87 |
return parser |
| 88 |
|
| 89 |
def take_action(self, parsed_args): |
| 90 |
self.log.debug('take_action(%s)', parsed_args) |
| 91 |
heat_client = self.app.client_manager.orchestration |
| 92 |
return self._show_snapshot(heat_client, parsed_args.stack, |
| 93 |
parsed_args.snapshot) |
| 94 |
|
| 95 |
def _show_snapshot(self, heat_client, stack_id, snapshot_id): |
| 96 |
try: |
| 97 |
data = heat_client.stacks.snapshot_show(stack_id, snapshot_id) |
| 98 |
except heat_exc.HTTPNotFound: |
| 99 |
raise exc.CommandError(_('Snapshot ID <%(snapshot_id)s> not found ' |
| 100 |
'for stack <%(stack_id)s>') |
| 101 |
% {'snapshot_id': snapshot_id, |
| 102 |
'stack_id': stack_id}) |
| 103 |
|
| 104 |
rows = list(data.values()) |
| 105 |
columns = list(data.keys()) |
| 106 |
return columns, rows |
| 107 |
|
| 108 |
|
| 109 |
class RestoreSnapshot(command.Command): |
| 110 |
"""Restore stack snapshot""" |
| 111 |
|
| 112 |
log = logging.getLogger(__name__ + ".RestoreSnapshot") |
| 113 |
|
| 114 |
def get_parser(self, prog_name): |
| 115 |
parser = super().get_parser(prog_name) |
| 116 |
parser.add_argument( |
| 117 |
'stack', |
| 118 |
metavar='<stack>', |
| 119 |
help=_('Name or ID of stack containing the snapshot') |
| 120 |
) |
| 121 |
parser.add_argument( |
| 122 |
'snapshot', |
| 123 |
metavar='<snapshot>', |
| 124 |
help=_('ID of the snapshot to restore') |
| 125 |
) |
| 126 |
return parser |
| 127 |
|
| 128 |
def take_action(self, parsed_args): |
| 129 |
self.log.debug('take_action(%s)', parsed_args) |
| 130 |
heat_client = self.app.client_manager.orchestration |
| 131 |
return self._restore_snapshot(heat_client, parsed_args) |
| 132 |
|
| 133 |
def _restore_snapshot(self, heat_client, parsed_args): |
| 134 |
fields = {'stack_id': parsed_args.stack, |
| 135 |
'snapshot_id': parsed_args.snapshot} |
| 136 |
try: |
| 137 |
heat_client.stacks.restore(**fields) |
| 138 |
except heat_exc.HTTPNotFound: |
| 139 |
raise exc.CommandError(_('Stack %(stack)s or ' |
| 140 |
'snapshot %(snapshot)s not found.') % |
| 141 |
{'stack': parsed_args.stack, |
| 142 |
'snapshot': parsed_args.snapshot}) |
| 143 |
|
| 144 |
|
| 145 |
class CreateSnapshot(command.ShowOne): |
| 146 |
"""Create stack snapshot.""" |
| 147 |
|
| 148 |
log = logging.getLogger(__name__ + ".CreateSnapshot") |
| 149 |
|
| 150 |
def get_parser(self, prog_name): |
| 151 |
parser = super().get_parser(prog_name) |
| 152 |
parser.add_argument( |
| 153 |
'stack', |
| 154 |
metavar='<stack>', |
| 155 |
help=_('Name or ID of stack') |
| 156 |
) |
| 157 |
parser.add_argument( |
| 158 |
'--name', |
| 159 |
metavar='<name>', |
| 160 |
help=_('Name of snapshot') |
| 161 |
) |
| 162 |
return parser |
| 163 |
|
| 164 |
def take_action(self, parsed_args): |
| 165 |
self.log.debug('take_action(%s)', parsed_args) |
| 166 |
heat_client = self.app.client_manager.orchestration |
| 167 |
|
| 168 |
try: |
| 169 |
data = heat_client.stacks.snapshot(parsed_args.stack, |
| 170 |
parsed_args.name) |
| 171 |
except heat_exc.HTTPNotFound: |
| 172 |
raise exc.CommandError(_('Stack not found: %s') |
| 173 |
% parsed_args.stack) |
| 174 |
|
| 175 |
columns = [ |
| 176 |
'ID', |
| 177 |
'name', |
| 178 |
'action', |
| 179 |
'status', |
| 180 |
'status_reason', |
| 181 |
'data', |
| 182 |
'creation_time' |
| 183 |
] |
| 184 |
return (columns, utils.get_dict_properties(data, columns)) |
| 185 |
|
| 186 |
|
| 187 |
class DeleteSnapshot(command.Command): |
| 188 |
"""Delete stack snapshot.""" |
| 189 |
log = logging.getLogger(__name__ + ".DeleteSnapshot") |
| 190 |
|
| 191 |
def get_parser(self, prog_name): |
| 192 |
parser = super().get_parser(prog_name) |
| 193 |
parser.add_argument( |
| 194 |
'stack', |
| 195 |
metavar='<stack>', |
| 196 |
help=_('Name or ID of stack') |
| 197 |
) |
| 198 |
parser.add_argument( |
| 199 |
'snapshot', |
| 200 |
metavar='<snapshot>', |
| 201 |
help=_('ID of stack snapshot') |
| 202 |
) |
| 203 |
parser.add_argument( |
| 204 |
'-y', '--yes', |
| 205 |
action='store_true', |
| 206 |
help=_('Skip yes/no prompt (assume yes)') |
| 207 |
) |
| 208 |
return parser |
| 209 |
|
| 210 |
def take_action(self, parsed_args): |
| 211 |
self.log.debug('take_action(%s)', parsed_args) |
| 212 |
heat_client = self.app.client_manager.orchestration |
| 213 |
msg = ('User did not confirm snapshot delete ' |
| 214 |
'%sso taking no action.') |
| 215 |
try: |
| 216 |
if not parsed_args.yes and sys.stdin.isatty(): |
| 217 |
sys.stdout.write( |
| 218 |
_('Are you sure you want to delete the snapshot of this ' |
| 219 |
'stack [Y/N]?')) |
| 220 |
prompt_response = sys.stdin.readline().lower() |
| 221 |
if not prompt_response.startswith('y'): |
| 222 |
self.log.info(msg, '') |
| 223 |
return |
| 224 |
except KeyboardInterrupt: |
| 225 |
self.log.info(msg, '(ctrl-c) ') |
| 226 |
return |
| 227 |
except EOFError: |
| 228 |
self.log.info(msg, '(ctrl-d) ') |
| 229 |
return |
| 230 |
|
| 231 |
try: |
| 232 |
heat_client.stacks.snapshot_delete(parsed_args.stack, |
| 233 |
parsed_args.snapshot) |
| 234 |
except heat_exc.HTTPNotFound: |
| 235 |
raise exc.CommandError(_('Snapshot ID <%(snapshot_id)s> not found ' |
| 236 |
'for stack <%(stack_id)s>') |
| 237 |
% {'snapshot_id': parsed_args.snapshot, |
| 238 |
'stack_id': parsed_args.stack}) |