compute: Add support for microversion 2.93

Add '--reimage-boot-volume' and '--no-reimage-boot-volume parameters'
to the rebuild command to allow rebuilding of volume backed instances.

Change-Id: I4a6e30b2cf12f32202a2d9ef1ced347e1dd139f3
This commit is contained in:
whoami-rajat 2022-02-25 22:43:24 +05:30 committed by Stephen Finucane
parent 1f63034441
commit 4024bdb393
3 changed files with 162 additions and 0 deletions

View File

@ -3091,6 +3091,28 @@ class RebuildServer(command.ShowOne):
'(supported by --os-compute-api-version 2.90 or above)' '(supported by --os-compute-api-version 2.90 or above)'
), ),
) )
parser.add_argument(
'--reimage-boot-volume',
action='store_true',
dest='reimage_boot_volume',
default=None,
help=_(
'Rebuild a volume-backed server. This will wipe the root '
'volume data and overwrite it with the provided image. '
'Defaults to False. '
'(supported by --os-compute-api-version 2.93 or above)'
),
)
parser.add_argument(
'--no-reimage-boot-volume',
action='store_false',
dest='reimage_boot_volume',
default=None,
help=_(
'Do not rebuild a volume-backed server. '
'(supported by --os-compute-api-version 2.93 or above)'
),
)
parser.add_argument( parser.add_argument(
'--wait', '--wait',
action='store_true', action='store_true',
@ -3226,6 +3248,41 @@ class RebuildServer(command.ShowOne):
kwargs['hostname'] = parsed_args.hostname kwargs['hostname'] = parsed_args.hostname
v2_93 = api_versions.APIVersion('2.93')
if parsed_args.reimage_boot_volume:
if compute_client.api_version < v2_93:
msg = _(
'--os-compute-api-version 2.93 or greater is required to '
'support the --reimage-boot-volume option'
)
raise exceptions.CommandError(msg)
else:
# force user to explicitly request reimaging of volume-backed
# server
if not server.image:
if compute_client.api_version >= v2_93:
msg = (
'--reimage-boot-volume is required to rebuild a '
'volume-backed server'
)
raise exceptions.CommandError(msg)
else: # microversion < 2.93
# attempts to rebuild a volume-backed server before API
# microversion 2.93 will fail in all cases except one: if
# the user attempts the rebuild with the exact same image
# that the server was initially built with. We can't check
# for this since we don't have the original image ID to
# hand, so we simply warn the user.
# TODO(stephenfin): Make this a failure in a future
# version
self.log.warning(
'Attempting to rebuild a volume-backed server using '
'--os-compute-api-version 2.92 or earlier, which '
'will only succeed if the image is identical to the '
'one initially used. This will be an error in a '
'future release.'
)
try: try:
server = server.rebuild(image, parsed_args.password, **kwargs) server = server.rebuild(image, parsed_args.password, **kwargs)
finally: finally:

View File

@ -6294,6 +6294,103 @@ class TestServerRebuild(TestServer):
parsed_args) parsed_args)
class TestServerRebuildVolumeBacked(TestServer):
def setUp(self):
super().setUp()
self.new_image = image_fakes.create_one_image()
self.find_image_mock.return_value = self.new_image
attrs = {
'image': '',
'networks': {},
'adminPass': 'passw0rd',
}
new_server = compute_fakes.FakeServer.create_one_server(attrs=attrs)
# Fake the server to be rebuilt. The IDs of them should be the same.
attrs['id'] = new_server.id
methods = {
'rebuild': new_server,
}
self.server = compute_fakes.FakeServer.create_one_server(
attrs=attrs,
methods=methods
)
# Return value for utils.find_resource for server.
self.servers_mock.get.return_value = self.server
self.cmd = server.RebuildServer(self.app, None)
def test_rebuild_with_reimage_boot_volume(self):
self.app.client_manager.compute.api_version = \
api_versions.APIVersion('2.93')
arglist = [
self.server.id,
'--reimage-boot-volume',
'--image', self.new_image.id
]
verifylist = [
('server', self.server.id),
('reimage_boot_volume', True),
('image', self.new_image.id)
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
self.cmd.take_action(parsed_args)
self.servers_mock.get.assert_called_with(self.server.id)
self.server.rebuild.assert_called_with(
self.new_image, None)
def test_rebuild_with_no_reimage_boot_volume(self):
self.app.client_manager.compute.api_version = \
api_versions.APIVersion('2.93')
arglist = [
self.server.id,
'--no-reimage-boot-volume',
'--image', self.new_image.id
]
verifylist = [
('server', self.server.id),
('reimage_boot_volume', False),
('image', self.new_image.id)
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
exc = self.assertRaises(
exceptions.CommandError,
self.cmd.take_action,
parsed_args)
self.assertIn('--reimage-boot-volume is required', str(exc))
def test_rebuild_with_reimage_boot_volume_pre_v293(self):
self.app.client_manager.compute.api_version = \
api_versions.APIVersion('2.92')
arglist = [
self.server.id,
'--reimage-boot-volume',
'--image', self.new_image.id
]
verifylist = [
('server', self.server.id),
('reimage_boot_volume', True)
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
exc = self.assertRaises(
exceptions.CommandError,
self.cmd.take_action,
parsed_args)
self.assertIn(
'--os-compute-api-version 2.93 or greater is required', str(exc))
class TestEvacuateServer(TestServer): class TestEvacuateServer(TestServer):
def setUp(self): def setUp(self):

View File

@ -0,0 +1,8 @@
---
features:
- |
The ``server rebuild`` commands now accept two optional
``--reimage-boot-volume`` and ``--no-reimage-boot-volume``option.
Passing these parameter will allow/disallow a user to rebuild a volume
backed server.
This is available from Compute microversion ``2.93`` and onwards.