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 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
|
# 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.
"""Compute v2 Server Migration action implementations"""
import uuid
from openstack import utils as sdk_utils
from osc_lib.command import command
from osc_lib import exceptions
from osc_lib import utils
from openstackclient.common import pagination
from openstackclient.i18n import _
from openstackclient.identity import common as identity_common
class ListMigration(command.Lister):
_description = _("""List server migrations""")
def get_parser(self, prog_name):
parser = super().get_parser(prog_name)
parser.add_argument(
'--server',
metavar='<server>',
help=_('Filter migrations by server (name or ID)'),
)
parser.add_argument(
'--host',
metavar='<host>',
help=_('Filter migrations by source or destination host'),
)
parser.add_argument(
'--status',
metavar='<status>',
help=_('Filter migrations by status'),
)
parser.add_argument(
'--type',
metavar='<type>',
choices=[
'evacuation',
'live-migration',
'cold-migration',
'resize',
],
help=_('Filter migrations by type'),
)
pagination.add_marker_pagination_option_to_parser(parser)
parser.add_argument(
'--changes-since',
dest='changes_since',
metavar='<changes-since>',
help=_(
"List only migrations changed later or equal to a certain "
"point of time. The provided time should be an ISO 8061 "
"formatted time, e.g. ``2016-03-04T06:27:59Z``. "
"(supported with --os-compute-api-version 2.59 or above)"
),
)
parser.add_argument(
'--changes-before',
dest='changes_before',
metavar='<changes-before>',
help=_(
"List only migrations changed earlier or equal to a "
"certain point of time. The provided time should be an ISO "
"8061 formatted time, e.g. ``2016-03-04T06:27:59Z``. "
"(supported with --os-compute-api-version 2.66 or above)"
),
)
parser.add_argument(
'--project',
metavar='<project>',
help=_(
"Filter migrations by project (name or ID) "
"(supported with --os-compute-api-version 2.80 or above)"
),
)
identity_common.add_project_domain_option_to_parser(parser)
parser.add_argument(
'--user',
metavar='<user>',
help=_(
"Filter migrations by user (name or ID) "
"(supported with --os-compute-api-version 2.80 or above)"
),
)
identity_common.add_user_domain_option_to_parser(parser)
return parser
def print_migrations(self, parsed_args, compute_client, migrations):
column_headers = [
'Source Node',
'Dest Node',
'Source Compute',
'Dest Compute',
'Dest Host',
'Status',
'Server UUID',
'Old Flavor',
'New Flavor',
'Created At',
'Updated At',
]
# Response fields coming back from the REST API are not always exactly
# the same as the column header names.
columns = [
'source_node',
'dest_node',
'source_compute',
'dest_compute',
'dest_host',
'status',
'server_id',
'old_flavor_id',
'new_flavor_id',
'created_at',
'updated_at',
]
# Insert migrations UUID after ID
if sdk_utils.supports_microversion(compute_client, "2.59"):
column_headers.insert(0, "UUID")
columns.insert(0, "uuid")
if sdk_utils.supports_microversion(compute_client, "2.23"):
column_headers.insert(0, "Id")
columns.insert(0, "id")
column_headers.insert(len(column_headers) - 2, "Type")
columns.insert(len(columns) - 2, "migration_type")
if sdk_utils.supports_microversion(compute_client, "2.80"):
if parsed_args.project:
column_headers.insert(len(column_headers) - 2, "Project")
columns.insert(len(columns) - 2, "project_id")
if parsed_args.user:
column_headers.insert(len(column_headers) - 2, "User")
columns.insert(len(columns) - 2, "user_id")
return (
column_headers,
(utils.get_item_properties(mig, columns) for mig in migrations),
)
def take_action(self, parsed_args):
compute_client = self.app.client_manager.sdk_connection.compute
identity_client = self.app.client_manager.identity
search_opts = {}
if parsed_args.host is not None:
search_opts['host'] = parsed_args.host
if parsed_args.status is not None:
search_opts['status'] = parsed_args.status
if parsed_args.server:
server = compute_client.find_server(
parsed_args.server, ignore_missing=False
)
search_opts['instance_uuid'] = server.id
if parsed_args.type:
migration_type = parsed_args.type
# we're using an alias because the default value is confusing
if migration_type == 'cold-migration':
migration_type = 'migration'
search_opts['migration_type'] = migration_type
if parsed_args.marker:
if not sdk_utils.supports_microversion(compute_client, "2.59"):
msg = _(
'--os-compute-api-version 2.59 or greater is required to '
'support the --marker option'
)
raise exceptions.CommandError(msg)
search_opts['marker'] = parsed_args.marker
if parsed_args.limit:
if not sdk_utils.supports_microversion(compute_client, "2.59"):
msg = _(
'--os-compute-api-version 2.59 or greater is required to '
'support the --limit option'
)
raise exceptions.CommandError(msg)
search_opts['limit'] = parsed_args.limit
search_opts['paginated'] = False
if parsed_args.changes_since:
if not sdk_utils.supports_microversion(compute_client, "2.59"):
msg = _(
'--os-compute-api-version 2.59 or greater is required to '
'support the --changes-since option'
)
raise exceptions.CommandError(msg)
search_opts['changes_since'] = parsed_args.changes_since
if parsed_args.changes_before:
if not sdk_utils.supports_microversion(compute_client, "2.66"):
msg = _(
'--os-compute-api-version 2.66 or greater is required to '
'support the --changes-before option'
)
raise exceptions.CommandError(msg)
search_opts['changes_before'] = parsed_args.changes_before
if parsed_args.project:
if not sdk_utils.supports_microversion(compute_client, "2.80"):
msg = _(
'--os-compute-api-version 2.80 or greater is required to '
'support the --project option'
)
raise exceptions.CommandError(msg)
search_opts['project_id'] = identity_common.find_project(
identity_client,
parsed_args.project,
parsed_args.project_domain,
).id
if parsed_args.user:
if not sdk_utils.supports_microversion(compute_client, "2.80"):
msg = _(
'--os-compute-api-version 2.80 or greater is required to '
'support the --user option'
)
raise exceptions.CommandError(msg)
search_opts['user_id'] = identity_common.find_user(
identity_client,
parsed_args.user,
parsed_args.user_domain,
).id
migrations = list(compute_client.migrations(**search_opts))
return self.print_migrations(parsed_args, compute_client, migrations)
def _get_migration_by_uuid(compute_client, server_id, migration_uuid):
for migration in compute_client.server_migrations(server_id):
if migration.uuid == migration_uuid:
return migration
else:
msg = _(
'In-progress live migration %(migration)s is not found for '
'server %(server)s'
)
raise exceptions.CommandError(
msg
% {
'migration': migration_uuid,
'server': server_id,
}
)
class ShowMigration(command.ShowOne):
"""Show an in-progress live migration for a given server.
Note that it is not possible to show cold migrations or completed
live-migrations. Use 'openstack server migration list' to get details for
these.
"""
def get_parser(self, prog_name):
parser = super().get_parser(prog_name)
parser.add_argument(
'server',
metavar='<server>',
help=_('Server (name or ID)'),
)
parser.add_argument(
'migration',
metavar='<migration>',
help=_("Migration (ID)"),
)
return parser
def take_action(self, parsed_args):
compute_client = self.app.client_manager.sdk_connection.compute
if not sdk_utils.supports_microversion(compute_client, '2.24'):
msg = _(
'--os-compute-api-version 2.24 or greater is required to '
'support the server migration show command'
)
raise exceptions.CommandError(msg)
if not parsed_args.migration.isdigit():
try:
uuid.UUID(parsed_args.migration)
except ValueError:
msg = _('The <migration> argument must be an ID or UUID')
raise exceptions.CommandError(msg)
if not sdk_utils.supports_microversion(compute_client, '2.59'):
msg = _(
'--os-compute-api-version 2.59 or greater is required to '
'retrieve server migrations by UUID'
)
raise exceptions.CommandError(msg)
server = compute_client.find_server(
parsed_args.server,
ignore_missing=False,
)
# the nova API doesn't currently allow retrieval by UUID but it's a
# reasonably common operation so emulate this behavior by listing
# migrations - the responses are identical
if not parsed_args.migration.isdigit():
server_migration = _get_migration_by_uuid(
compute_client,
server.id,
parsed_args.migration,
)
else:
server_migration = compute_client.get_server_migration(
server.id,
parsed_args.migration,
ignore_missing=False,
)
column_headers = (
'ID',
'Server UUID',
'Status',
'Source Compute',
'Source Node',
'Dest Compute',
'Dest Host',
'Dest Node',
'Memory Total Bytes',
'Memory Processed Bytes',
'Memory Remaining Bytes',
'Disk Total Bytes',
'Disk Processed Bytes',
'Disk Remaining Bytes',
'Created At',
'Updated At',
)
columns = (
'id',
'server_id',
'status',
'source_compute',
'source_node',
'dest_compute',
'dest_host',
'dest_node',
'memory_total_bytes',
'memory_processed_bytes',
'memory_remaining_bytes',
'disk_total_bytes',
'disk_processed_bytes',
'disk_remaining_bytes',
'created_at',
'updated_at',
)
if sdk_utils.supports_microversion(compute_client, '2.59'):
column_headers += ('UUID',)
columns += ('uuid',)
if sdk_utils.supports_microversion(compute_client, '2.80'):
column_headers += ('User ID', 'Project ID')
columns += ('user_id', 'project_id')
data = utils.get_item_properties(server_migration, columns)
return column_headers, data
class AbortMigration(command.Command):
"""Cancel an ongoing live migration.
This command requires ``--os-compute-api-version`` 2.24 or greater.
"""
def get_parser(self, prog_name):
parser = super().get_parser(prog_name)
parser.add_argument(
'server',
metavar='<server>',
help=_('Server (name or ID)'),
)
parser.add_argument(
'migration',
metavar='<migration>',
help=_("Migration (ID)"),
)
return parser
def take_action(self, parsed_args):
compute_client = self.app.client_manager.sdk_connection.compute
if not sdk_utils.supports_microversion(compute_client, '2.24'):
msg = _(
'--os-compute-api-version 2.24 or greater is required to '
'support the server migration abort command'
)
raise exceptions.CommandError(msg)
if not parsed_args.migration.isdigit():
try:
uuid.UUID(parsed_args.migration)
except ValueError:
msg = _('The <migration> argument must be an ID or UUID')
raise exceptions.CommandError(msg)
if not sdk_utils.supports_microversion(compute_client, '2.59'):
msg = _(
'--os-compute-api-version 2.59 or greater is required to '
'abort server migrations by UUID'
)
raise exceptions.CommandError(msg)
server = compute_client.find_server(
parsed_args.server,
ignore_missing=False,
)
# the nova API doesn't currently allow retrieval by UUID but it's a
# reasonably common operation so emulate this behavior by listing
# migrations - the responses are identical
migration_id = parsed_args.migration
if not parsed_args.migration.isdigit():
migration_id = _get_migration_by_uuid(
compute_client,
server.id,
parsed_args.migration,
).id
compute_client.abort_server_migration(
migration_id,
server.id,
ignore_missing=False,
)
class ForceCompleteMigration(command.Command):
"""Force an ongoing live migration to complete.
This command requires ``--os-compute-api-version`` 2.22 or greater.
"""
def get_parser(self, prog_name):
parser = super().get_parser(prog_name)
parser.add_argument(
'server',
metavar='<server>',
help=_('Server (name or ID)'),
)
parser.add_argument(
'migration', metavar='<migration>', help=_('Migration (ID)')
)
return parser
def take_action(self, parsed_args):
compute_client = self.app.client_manager.sdk_connection.compute
if not sdk_utils.supports_microversion(compute_client, '2.22'):
msg = _(
'--os-compute-api-version 2.22 or greater is required to '
'support the server migration force complete command'
)
raise exceptions.CommandError(msg)
if not parsed_args.migration.isdigit():
try:
uuid.UUID(parsed_args.migration)
except ValueError:
msg = _('The <migration> argument must be an ID or UUID')
raise exceptions.CommandError(msg)
if not sdk_utils.supports_microversion(compute_client, '2.59'):
msg = _(
'--os-compute-api-version 2.59 or greater is required to '
'abort server migrations by UUID'
)
raise exceptions.CommandError(msg)
server = compute_client.find_server(
parsed_args.server,
ignore_missing=False,
)
# the nova API doesn't currently allow retrieval by UUID but it's a
# reasonably common operation so emulate this behavior by listing
# migrations - the responses are identical
migration_id = parsed_args.migration
if not parsed_args.migration.isdigit():
migration_id = _get_migration_by_uuid(
compute_client,
server.id,
parsed_args.migration,
).id
compute_client.force_complete_server_migration(migration_id, server.id)
|