Add backup volume by glance protection plugin
Change-Id: I60ef750c5d13fa7c3b033f963e13285ba5fb6684 Implements: blueprint backup-volume-data-to-bank
This commit is contained in:
parent
33756861da
commit
a9e872bfe1
|
@ -273,3 +273,25 @@ class BankSection(object):
|
|||
@property
|
||||
def bank(self):
|
||||
return self._bank
|
||||
|
||||
|
||||
class BankIO(object):
|
||||
def __init__(self, bank_section, sorted_objects):
|
||||
super(BankIO, self).__init__()
|
||||
self.bank_section = bank_section
|
||||
self.sorted_objects = sorted_objects
|
||||
self.obj_size = len(sorted_objects)
|
||||
self.length = 0
|
||||
|
||||
def readable(self):
|
||||
return True
|
||||
|
||||
def __iter__(self):
|
||||
return self
|
||||
|
||||
def read(self, length=None):
|
||||
obj_index = self.length
|
||||
self.length += 1
|
||||
if self.length > self.obj_size:
|
||||
return ''
|
||||
return self.bank_section.get_object(self.sorted_objects[obj_index])
|
||||
|
|
|
@ -11,9 +11,7 @@
|
|||
# under the License.
|
||||
|
||||
from functools import partial
|
||||
import os
|
||||
|
||||
from io import BytesIO
|
||||
from karbor.common import constants
|
||||
from karbor import exception
|
||||
from karbor.services.protection.client_factory import ClientFactory
|
||||
|
@ -24,7 +22,6 @@ from karbor.services.protection.protection_plugins import utils
|
|||
from oslo_config import cfg
|
||||
from oslo_log import log as logging
|
||||
|
||||
|
||||
image_backup_opts = [
|
||||
cfg.IntOpt('backup_image_object_size',
|
||||
default=65536*10,
|
||||
|
@ -106,35 +103,11 @@ class ProtectOperation(protection_plugin.Operation):
|
|||
|
||||
def _create_backup(self, glance_client, bank_section, image_id):
|
||||
try:
|
||||
image_response = glance_client.images.data(image_id,
|
||||
do_checksum=True)
|
||||
bank_chunk_num = int(self._data_block_size_bytes/65536)
|
||||
LOG.debug("Creating image backup, bank_chunk_num: %s.",
|
||||
bank_chunk_num)
|
||||
|
||||
# backup the data of image
|
||||
image_chunks_num = 0
|
||||
chunks_num = 1
|
||||
image_response_data = BytesIO()
|
||||
for chunk in image_response:
|
||||
image_response_data.write(chunk)
|
||||
image_chunks_num += 1
|
||||
if image_chunks_num == bank_chunk_num:
|
||||
image_chunks_num = 0
|
||||
image_response_data.seek(0, os.SEEK_SET)
|
||||
data = image_response_data.read(
|
||||
self._data_block_size_bytes)
|
||||
bank_section.update_object("data_" + str(chunks_num), data)
|
||||
image_response_data.truncate(0)
|
||||
image_response_data.seek(0, os.SEEK_SET)
|
||||
chunks_num += 1
|
||||
|
||||
image_response_data.seek(0, os.SEEK_SET)
|
||||
data = image_response_data.read()
|
||||
if data != '':
|
||||
bank_section.update_object("data_" + str(chunks_num), data)
|
||||
else:
|
||||
chunks_num -= 1
|
||||
chunks_num = utils.backup_image_to_bank(
|
||||
glance_client,
|
||||
image_id, bank_section,
|
||||
self._data_block_size_bytes
|
||||
)
|
||||
|
||||
# Save the chunks_num to metadata
|
||||
resource_definition = bank_section.get_object("metadata")
|
||||
|
@ -199,36 +172,11 @@ class RestoreOperation(protection_plugin.Operation):
|
|||
|
||||
glance_client = ClientFactory.create_client('glance', context)
|
||||
bank_section = checkpoint.get_resource_bank_section(original_image_id)
|
||||
image = None
|
||||
image_info = None
|
||||
try:
|
||||
resource_definition = bank_section.get_object('metadata')
|
||||
image_metadata = resource_definition['image_metadata']
|
||||
objects = [key.split("/")[-1] for key in
|
||||
bank_section.list_objects()
|
||||
if (key.split("/")[-1]).startswith("data_")]
|
||||
image_info = utils.restore_image_from_bank(
|
||||
glance_client, bank_section, name)
|
||||
|
||||
# check the chunks_num
|
||||
chunks_num = resource_definition.get("chunks_num", 0)
|
||||
if len(objects) != int(chunks_num):
|
||||
LOG.debug('object num: {0}, chunk num: {1}'.
|
||||
format(len(objects), chunks_num))
|
||||
raise exception.RestoreResourceFailed(
|
||||
name="Image Backup",
|
||||
reason=" The chunks_num of restored image is invalid.",
|
||||
resource_id=original_image_id,
|
||||
resource_type=constants.IMAGE_RESOURCE_TYPE)
|
||||
|
||||
sorted_objects = sorted(objects, key=lambda s: int(s[5:]))
|
||||
image_data = ImageBankIO(bank_section, sorted_objects)
|
||||
disk_format = image_metadata["disk_format"]
|
||||
container_format = image_metadata["container_format"]
|
||||
image = glance_client.images.create(
|
||||
disk_format=disk_format,
|
||||
container_format=container_format,
|
||||
name=name)
|
||||
glance_client.images.upload(image.id, image_data)
|
||||
|
||||
image_info = glance_client.images.get(image.id)
|
||||
if image_info.status != "active":
|
||||
is_success = utils.status_poll(
|
||||
partial(get_image_status, glance_client, image_info.id),
|
||||
|
@ -245,20 +193,14 @@ class RestoreOperation(protection_plugin.Operation):
|
|||
resource_id=image_info.id,
|
||||
resource_type=constants.IMAGE_RESOURCE_TYPE)
|
||||
|
||||
# check the checksum
|
||||
if image_info.checksum != image_metadata["checksum"]:
|
||||
raise exception.RestoreResourceFailed(
|
||||
name="Image Backup",
|
||||
reason=" The checksum of restored image is invalid.",
|
||||
resource_id=original_image_id,
|
||||
resource_type=constants.IMAGE_RESOURCE_TYPE)
|
||||
kwargs.get("new_resources")[original_image_id] = image.id
|
||||
kwargs.get("new_resources")[original_image_id] = image_info.id
|
||||
except Exception as e:
|
||||
LOG.error("Restore image backup failed, image_id: %s.",
|
||||
original_image_id)
|
||||
if image is not None and hasattr(image, 'id'):
|
||||
LOG.info("Delete the failed image, image_id: %s.", image.id)
|
||||
glance_client.images.delete(image.id)
|
||||
if image_info is not None and hasattr(image_info, 'id'):
|
||||
LOG.info("Delete the failed image, image_id: %s.",
|
||||
image_info.id)
|
||||
glance_client.images.delete(image_info.id)
|
||||
raise exception.RestoreResourceFailed(
|
||||
name="Image Backup",
|
||||
reason=e, resource_id=original_image_id,
|
||||
|
@ -267,29 +209,6 @@ class RestoreOperation(protection_plugin.Operation):
|
|||
original_image_id)
|
||||
|
||||
|
||||
class ImageBankIO(object):
|
||||
def __init__(self, bank_section, sorted_objects):
|
||||
super(ImageBankIO, self).__init__()
|
||||
self.bank_section = bank_section
|
||||
self.sorted_objects = sorted_objects
|
||||
self.obj_size = len(sorted_objects)
|
||||
self.length = 0
|
||||
|
||||
def readable(self):
|
||||
return True
|
||||
|
||||
def __iter__(self):
|
||||
return self
|
||||
|
||||
def read(self, length=None):
|
||||
obj_index = self.length
|
||||
self.length += 1
|
||||
if self.length > self.obj_size:
|
||||
return ''
|
||||
return self.bank_section.get_object(
|
||||
self.sorted_objects[obj_index])
|
||||
|
||||
|
||||
class GlanceProtectionPlugin(protection_plugin.ProtectionPlugin):
|
||||
_SUPPORT_RESOURCE_TYPES = [constants.IMAGE_RESOURCE_TYPE]
|
||||
|
||||
|
|
|
@ -9,14 +9,72 @@
|
|||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
from io import BytesIO
|
||||
import os
|
||||
|
||||
from oslo_log import log as logging
|
||||
from oslo_service import loopingcall
|
||||
|
||||
from karbor.services.protection.bank_plugin import BankIO
|
||||
|
||||
LOG = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def backup_image_to_bank(glance_client, image_id, bank_section, object_size):
|
||||
image_response = glance_client.images.data(image_id, do_checksum=True)
|
||||
bank_chunk_num = int(object_size / 65536)
|
||||
|
||||
image_chunks_num = 0
|
||||
chunks_num = 1
|
||||
image_response_data = BytesIO()
|
||||
for chunk in image_response:
|
||||
image_response_data.write(chunk)
|
||||
image_chunks_num += 1
|
||||
if image_chunks_num == bank_chunk_num:
|
||||
image_chunks_num = 0
|
||||
image_response_data.seek(0, os.SEEK_SET)
|
||||
data = image_response_data.read(object_size)
|
||||
bank_section.update_object("data_" + str(chunks_num), data)
|
||||
image_response_data.truncate(0)
|
||||
image_response_data.seek(0, os.SEEK_SET)
|
||||
chunks_num += 1
|
||||
|
||||
image_response_data.seek(0, os.SEEK_SET)
|
||||
data = image_response_data.read()
|
||||
if data != '':
|
||||
bank_section.update_object("data_" + str(chunks_num), data)
|
||||
else:
|
||||
chunks_num -= 1
|
||||
return chunks_num
|
||||
|
||||
|
||||
def restore_image_from_bank(glance_client, bank_section, restore_name):
|
||||
resource_definition = bank_section.get_object('metadata')
|
||||
image_metadata = resource_definition['image_metadata']
|
||||
objects = [key.split("/")[-1] for key in
|
||||
bank_section.list_objects()
|
||||
if (key.split("/")[-1]).startswith("data_")]
|
||||
|
||||
chunks_num = resource_definition.get("chunks_num", 0)
|
||||
if len(objects) != int(chunks_num):
|
||||
raise Exception("The chunks num of restored image is invalid")
|
||||
|
||||
sorted_objects = sorted(objects, key=lambda s: int(s[5:]))
|
||||
image_data = BankIO(bank_section, sorted_objects)
|
||||
disk_format = image_metadata["disk_format"]
|
||||
container_format = image_metadata["container_format"]
|
||||
image = glance_client.images.create(
|
||||
disk_format=disk_format,
|
||||
container_format=container_format,
|
||||
name=restore_name
|
||||
)
|
||||
glance_client.images.upload(image.id, image_data)
|
||||
image_info = glance_client.images.get(image.id)
|
||||
if image_info.checksum != image_metadata["checksum"]:
|
||||
raise Exception("The checksum of restored image is invalid")
|
||||
return image_info
|
||||
|
||||
|
||||
def update_resource_restore_result(restore_record, resource_type, resource_id,
|
||||
status, reason=''):
|
||||
try:
|
||||
|
|
|
@ -0,0 +1,487 @@
|
|||
# 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.
|
||||
|
||||
from functools import partial
|
||||
|
||||
from cinderclient import exceptions as cinder_exc
|
||||
from oslo_config import cfg
|
||||
from oslo_log import log as logging
|
||||
|
||||
from karbor.common import constants
|
||||
from karbor import exception
|
||||
from karbor.services.protection.client_factory import ClientFactory
|
||||
from karbor.services.protection import protection_plugin
|
||||
from karbor.services.protection.protection_plugins import utils
|
||||
from karbor.services.protection.protection_plugins.volume \
|
||||
import volume_glance_plugin_schemas as volume_schemas
|
||||
|
||||
LOG = logging.getLogger(__name__)
|
||||
|
||||
volume_glance_opts = [
|
||||
cfg.IntOpt(
|
||||
'poll_interval', default=15,
|
||||
help='Poll interval for Cinder volume status.'
|
||||
),
|
||||
cfg.BoolOpt(
|
||||
'backup_from_snapshot', default=True,
|
||||
help='First take a snapshot of the volume, and backup from '
|
||||
'it. Minimizes the time the volume is unavailable.'
|
||||
),
|
||||
cfg.IntOpt('backup_image_object_size',
|
||||
default=65536*512,
|
||||
help='The size in bytes of temporary image objects. '
|
||||
'The value must be a multiple of 65536('
|
||||
'the size of image\'s chunk).'),
|
||||
]
|
||||
|
||||
VOLUME_SUCCESS_STATUSES = {'available', 'in-use',
|
||||
'error_extending', 'error_restoring'}
|
||||
|
||||
VOLUME_FAILURE_STATUSES = {'error', 'error_deleting', 'deleting',
|
||||
'not-found'}
|
||||
|
||||
VOLUME_IGNORE_STATUSES = {'attaching', 'creating', 'backing-up',
|
||||
'restoring-backup', 'uploading', 'downloading'}
|
||||
|
||||
|
||||
def get_snapshot_status(cinder_client, snapshot_id):
|
||||
return get_resource_status(cinder_client.volume_snapshots, snapshot_id,
|
||||
'snapshot')
|
||||
|
||||
|
||||
def get_volume_status(cinder_client, volume_id):
|
||||
return get_resource_status(cinder_client.volumes, volume_id, 'volume')
|
||||
|
||||
|
||||
def get_image_status(glance_client, image_id):
|
||||
LOG.debug('Polling image (image_id: %s)', image_id)
|
||||
try:
|
||||
status = glance_client.images.get(image_id)['status']
|
||||
except exception.NotFound:
|
||||
status = 'not-found'
|
||||
LOG.debug('Polled image (image_id: %s) status: %s',
|
||||
image_id, status)
|
||||
return status
|
||||
|
||||
|
||||
def get_resource_status(resource_manager, resource_id, resource_type):
|
||||
LOG.debug('Polling %(resource_type)s (id: %(resource_id)s)',
|
||||
{'resource_type': resource_type, 'resource_id': resource_id})
|
||||
try:
|
||||
resource = resource_manager.get(resource_id)
|
||||
status = resource.status
|
||||
except cinder_exc.NotFound:
|
||||
status = 'not-found'
|
||||
LOG.debug(
|
||||
'Polled %(resource_type)s (id: %(resource_id)s) status: %(status)s',
|
||||
{'resource_type': resource_type, 'resource_id': resource_id,
|
||||
'status': status}
|
||||
)
|
||||
return status
|
||||
|
||||
|
||||
class ProtectOperation(protection_plugin.Operation):
|
||||
def __init__(self, poll_interval, backup_from_snapshot, image_object_size):
|
||||
super(ProtectOperation, self).__init__()
|
||||
self._interval = poll_interval
|
||||
self._backup_from_snapshot = backup_from_snapshot
|
||||
self._image_object_size = image_object_size
|
||||
|
||||
def _create_snapshot(self, cinder_client, volume_id):
|
||||
LOG.info("Start creating snapshot of volume({0}).".format(volume_id))
|
||||
snapshot = cinder_client.volume_snapshots.create(
|
||||
volume_id,
|
||||
name='temporary_snapshot_of_{0}'.format(volume_id),
|
||||
force=True
|
||||
)
|
||||
|
||||
snapshot_id = snapshot.id
|
||||
is_success = utils.status_poll(
|
||||
partial(get_snapshot_status, cinder_client, snapshot_id),
|
||||
interval=self._interval,
|
||||
success_statuses={'available', },
|
||||
failure_statuses={'error', 'error_deleting', 'deleting',
|
||||
'not-found'},
|
||||
ignore_statuses={'creating', },
|
||||
)
|
||||
if is_success is not True:
|
||||
try:
|
||||
snapshot = cinder_client.volume_snapshots.get(snapshot_id)
|
||||
except Exception:
|
||||
reason = 'Unable to find volume snapshot.'
|
||||
else:
|
||||
reason = 'The status of snapshot is %s' % snapshot.status
|
||||
raise exception.CreateResourceFailed(
|
||||
name="Volume Glance Backup",
|
||||
reason=reason,
|
||||
resource_id=volume_id,
|
||||
resource_type=constants.VOLUME_RESOURCE_TYPE
|
||||
)
|
||||
LOG.info("Create snapshot of volume({0}) success, "
|
||||
"snapshot_id({1})".format(volume_id, snapshot_id))
|
||||
return snapshot_id
|
||||
|
||||
def _create_temporary_volume(self, cinder_client, snapshot_id):
|
||||
LOG.info("Start creating volume from snapshot({0}) success"
|
||||
"".format(snapshot_id))
|
||||
snapshot = cinder_client.volume_snapshots.get(snapshot_id)
|
||||
volume = cinder_client.volumes.create(
|
||||
size=snapshot.size,
|
||||
snapshot_id=snapshot_id,
|
||||
name='temporary_volume_of_{0}'.format(snapshot_id)
|
||||
)
|
||||
is_success = utils.status_poll(
|
||||
partial(get_volume_status, cinder_client, volume.id),
|
||||
interval=self._interval,
|
||||
success_statuses=VOLUME_SUCCESS_STATUSES,
|
||||
failure_statuses=VOLUME_FAILURE_STATUSES,
|
||||
ignore_statuses=VOLUME_IGNORE_STATUSES,
|
||||
)
|
||||
volume = cinder_client.volumes.get(volume.id)
|
||||
if is_success is not True:
|
||||
LOG.error('The status of temporary volume is invalid. status:%s',
|
||||
volume.status)
|
||||
reason = 'Invalid status: %s of temporary volume.' % volume.status
|
||||
raise exception.CreateResourceFailed(
|
||||
name="Volume Glance Backup",
|
||||
reason=reason,
|
||||
resource_id=volume.id,
|
||||
resource_type=constants.VOLUME_RESOURCE_TYPE,
|
||||
)
|
||||
LOG.info("Create volume from snapshot({0}) success, "
|
||||
"volume({1})".format(snapshot_id, volume.id))
|
||||
return volume
|
||||
|
||||
def _create_temporary_image(self, cinder_client, glance_client,
|
||||
temporary_volume):
|
||||
LOG.info("Start creating image from volume({0})."
|
||||
"".format(temporary_volume.id))
|
||||
image = cinder_client.volumes.upload_to_image(
|
||||
volume=temporary_volume,
|
||||
force=True,
|
||||
image_name='temporary_image_of_{0}'.format(temporary_volume.id),
|
||||
container_format="bare",
|
||||
disk_format="raw"
|
||||
)
|
||||
image_id = image[1]['os-volume_upload_image']['image_id']
|
||||
is_success = utils.status_poll(
|
||||
partial(get_image_status, glance_client, image_id),
|
||||
interval=self._interval, success_statuses={'active'},
|
||||
ignore_statuses={'queued', 'saving'},
|
||||
failure_statuses={'killed', 'deleted', 'pending_delete',
|
||||
'deactivated', 'NotFound'}
|
||||
)
|
||||
image_info = glance_client.images.get(image_id)
|
||||
if is_success is not True:
|
||||
LOG.error("The status of image (id: %s) is invalid.",
|
||||
image_id)
|
||||
reason = "Invalid status: %s of temporary image." % \
|
||||
image_info.status
|
||||
raise exception.CreateResourceFailed(
|
||||
name="Volume Glance Backup",
|
||||
reason=reason,
|
||||
resource_id=image_id,
|
||||
resource_type=constants.IMAGE_RESOURCE_TYPE)
|
||||
LOG.info("Create image({0}) from volume({1}) "
|
||||
"success.".format(image_id, temporary_volume.id))
|
||||
return image_id
|
||||
|
||||
def _backup_temporary_image(self, glance_client, image_id, bank_section):
|
||||
try:
|
||||
chunks_num = utils.backup_image_to_bank(
|
||||
glance_client,
|
||||
image_id,
|
||||
bank_section,
|
||||
self._image_object_size
|
||||
)
|
||||
image_info = glance_client.images.get(image_id)
|
||||
image_resource_definition = {
|
||||
'chunks_num': chunks_num,
|
||||
'image_metadata': {
|
||||
'checksum': image_info.checksum,
|
||||
'disk_format': image_info.disk_format,
|
||||
"container_format": image_info.container_format
|
||||
}
|
||||
}
|
||||
return image_resource_definition
|
||||
except Exception as err:
|
||||
LOG.exception('Protecting temporary image (id: %s) to bank '
|
||||
'failed.', image_id)
|
||||
raise exception.CreateResourceFailed(
|
||||
name="Volume Glance Backup",
|
||||
reason=err,
|
||||
resource_id=image_id,
|
||||
resource_type=constants.IMAGE_RESOURCE_TYPE)
|
||||
|
||||
def on_main(self, checkpoint, resource, context, parameters, **kwargs):
|
||||
volume_id = resource.id
|
||||
bank_section = checkpoint.get_resource_bank_section(volume_id)
|
||||
cinder_client = ClientFactory.create_client('cinder', context)
|
||||
glance_client = ClientFactory.create_client('glance', context)
|
||||
LOG.info('creating volume backup by glance, volume_id: %s', volume_id)
|
||||
bank_section.update_object('status',
|
||||
constants.RESOURCE_STATUS_PROTECTING)
|
||||
resource_metadata = {
|
||||
'volume_id': volume_id,
|
||||
}
|
||||
is_success = utils.status_poll(
|
||||
partial(get_volume_status, cinder_client, volume_id),
|
||||
interval=self._interval,
|
||||
success_statuses=VOLUME_SUCCESS_STATUSES,
|
||||
failure_statuses=VOLUME_FAILURE_STATUSES,
|
||||
ignore_statuses=VOLUME_IGNORE_STATUSES,
|
||||
)
|
||||
if not is_success:
|
||||
bank_section.update_object('status',
|
||||
constants.RESOURCE_STATUS_ERROR)
|
||||
raise exception.CreateResourceFailed(
|
||||
name="Volume Glance Backup",
|
||||
reason='Volume is in erroneous state',
|
||||
resource_id=volume_id,
|
||||
resource_type=constants.VOLUME_RESOURCE_TYPE,
|
||||
)
|
||||
|
||||
volume_info = cinder_client.volumes.get(volume_id)
|
||||
resource_metadata['volume_size'] = volume_info.size
|
||||
snapshot_id = None
|
||||
temporary_volume = None
|
||||
temporary_image_id = None
|
||||
|
||||
try:
|
||||
snapshot_id = self._create_snapshot(cinder_client, volume_id)
|
||||
temporary_volume = self._create_temporary_volume(
|
||||
cinder_client, snapshot_id)
|
||||
temporary_image_id = self._create_temporary_image(
|
||||
cinder_client, glance_client, temporary_volume)
|
||||
image_resource_metadata = \
|
||||
self._backup_temporary_image(glance_client, temporary_image_id,
|
||||
bank_section)
|
||||
metadata = dict(resource_metadata, **image_resource_metadata)
|
||||
bank_section.update_object('metadata', metadata)
|
||||
bank_section.update_object('status',
|
||||
constants.RESOURCE_STATUS_AVAILABLE)
|
||||
LOG.info('Backed up volume '
|
||||
'(volume_id: %(volume_id)s '
|
||||
'snapshot_id: %(snapshot_id)s '
|
||||
'temporary_volume_id: %(temporary_volume_id)s) '
|
||||
'temporary_image_id: %(temporary_image_id)s '
|
||||
'successfully', {
|
||||
'volume_id': volume_id,
|
||||
'snapshot_id': snapshot_id,
|
||||
'temporary_volume_id': temporary_volume.id,
|
||||
'temporary_image_id': temporary_image_id
|
||||
})
|
||||
finally:
|
||||
if snapshot_id:
|
||||
try:
|
||||
cinder_client.volume_snapshots.delete(snapshot_id)
|
||||
except Exception as e:
|
||||
LOG.warning('Failed deleting snapshot: %(snapshot_id)s. '
|
||||
'Reason: %(reason)s',
|
||||
{'snapshot_id': self.snapshot_id, 'reason': e})
|
||||
|
||||
if temporary_volume:
|
||||
try:
|
||||
cinder_client.volumes.delete(temporary_volume.id)
|
||||
except Exception as e:
|
||||
LOG.warning('Failed deleting temporary volume: '
|
||||
'%(temporary_volume_id)s. '
|
||||
'Reason: %(reason)s', {
|
||||
'temporary_volume_id': temporary_volume.id,
|
||||
'reason': e
|
||||
})
|
||||
if temporary_image_id:
|
||||
try:
|
||||
glance_client.images.delete(temporary_image_id)
|
||||
except Exception as e:
|
||||
LOG.warning('Failed deleting temporary image: '
|
||||
'%(temporary_image_id)s. '
|
||||
'Reason: %(reason)s', {
|
||||
'temporary_image_id': temporary_image_id,
|
||||
'reason': e})
|
||||
|
||||
|
||||
class RestoreOperation(protection_plugin.Operation):
|
||||
def __init__(self, poll_interval):
|
||||
super(RestoreOperation, self).__init__()
|
||||
self._interval = poll_interval
|
||||
|
||||
def _create_volume_from_image(self, cinder_client, temporary_image,
|
||||
restore_name, original_vol_id, volume_size,
|
||||
description):
|
||||
volume = cinder_client.volumes.create(
|
||||
size=volume_size,
|
||||
imageRef=temporary_image.id,
|
||||
name=restore_name,
|
||||
description=description
|
||||
)
|
||||
is_success = utils.status_poll(
|
||||
partial(get_volume_status, cinder_client, volume.id),
|
||||
interval=self._interval,
|
||||
success_statuses=VOLUME_SUCCESS_STATUSES,
|
||||
failure_statuses=VOLUME_FAILURE_STATUSES,
|
||||
ignore_statuses=VOLUME_IGNORE_STATUSES
|
||||
)
|
||||
if not is_success:
|
||||
LOG.error("Restore volume glance backup failed, volume_id: %s.",
|
||||
original_vol_id)
|
||||
if volume is not None and hasattr(volume, 'id'):
|
||||
LOG.info("Delete the failed volume, volume_id: %s.", volume.id)
|
||||
cinder_client.volumes.delete(volume.id)
|
||||
raise exception.CreateResourceFailed(
|
||||
name="Volume Glance Backup",
|
||||
reason='Restored Volume is in erroneous state',
|
||||
resource_id=volume.id,
|
||||
resource_type=constants.VOLUME_RESOURCE_TYPE,
|
||||
)
|
||||
|
||||
def on_main(self, checkpoint, resource, context, parameters, **kwargs):
|
||||
original_volume_id = resource.id
|
||||
restore_name = parameters.get('restore_name',
|
||||
'%s@%s' % (checkpoint.id,
|
||||
original_volume_id))
|
||||
restore_description = parameters.get('restore_description', None)
|
||||
bank_section = checkpoint.get_resource_bank_section(original_volume_id)
|
||||
cinder_client = ClientFactory.create_client('cinder', context)
|
||||
glance_client = ClientFactory.create_client('glance', context)
|
||||
resource_metadata = bank_section.get_object('metadata')
|
||||
volume_size = int(resource_metadata['volume_size'])
|
||||
temporary_image = None
|
||||
try:
|
||||
temporary_image = self._create_temporary_image(
|
||||
bank_section, glance_client, original_volume_id
|
||||
)
|
||||
self._create_volume_from_image(cinder_client, temporary_image,
|
||||
restore_name, original_volume_id,
|
||||
volume_size, restore_description)
|
||||
finally:
|
||||
if temporary_image:
|
||||
try:
|
||||
glance_client.images.delete(temporary_image.id)
|
||||
except Exception as e:
|
||||
LOG.warning('Failed deleting temporary image: '
|
||||
'%(temporary_image_id)s. '
|
||||
'Reason: %(reason)s', {
|
||||
'temporary_image_id': temporary_image.id,
|
||||
'reason': e
|
||||
})
|
||||
LOG.info("Finish restoring volume backup, volume_id: %s.",
|
||||
original_volume_id)
|
||||
|
||||
def _create_temporary_image(self, bank_section, glance_client,
|
||||
original_volume_id):
|
||||
image_info = None
|
||||
try:
|
||||
image_info = utils.restore_image_from_bank(
|
||||
glance_client, bank_section,
|
||||
'temporary_image_of_{0}'.format(original_volume_id))
|
||||
|
||||
if image_info.status != "active":
|
||||
is_success = utils.status_poll(
|
||||
partial(get_image_status, glance_client, image_info.id),
|
||||
interval=self._interval, success_statuses={'active'},
|
||||
ignore_statuses={'queued', 'saving'},
|
||||
failure_statuses={'killed', 'deleted', 'pending_delete',
|
||||
'deactivated', 'not-found'}
|
||||
)
|
||||
if is_success is not True:
|
||||
LOG.error('The status of image is invalid. status:%s',
|
||||
image_info.status)
|
||||
raise exception.RestoreResourceFailed(
|
||||
name="Volume Glance Backup",
|
||||
reason="Create temporary image failed",
|
||||
resource_id=original_volume_id,
|
||||
resource_type=constants.VOLUME_RESOURCE_TYPE)
|
||||
return image_info
|
||||
except Exception as e:
|
||||
LOG.error("Create temporary image of volume failed, "
|
||||
"volume_id: %s.", original_volume_id)
|
||||
LOG.exception(e)
|
||||
if image_info is not None and hasattr(image_info, 'id'):
|
||||
LOG.info("Delete the failed image, image_id: %s.",
|
||||
image_info.id)
|
||||
glance_client.images.delete(image_info.id)
|
||||
raise exception.RestoreResourceFailed(
|
||||
name="Volume Glance Backup",
|
||||
reason=e, resource_id=original_volume_id,
|
||||
resource_type=constants.VOLUME_RESOURCE_TYPE)
|
||||
|
||||
|
||||
class DeleteOperation(protection_plugin.Operation):
|
||||
def on_main(self, checkpoint, resource, context, parameters, **kwargs):
|
||||
volume_id = resource.id
|
||||
bank_section = checkpoint.get_resource_bank_section(volume_id)
|
||||
|
||||
LOG.info("Deleting volume backup, volume_id: %s.", volume_id)
|
||||
try:
|
||||
bank_section.update_object("status",
|
||||
constants.RESOURCE_STATUS_DELETING)
|
||||
objects = bank_section.list_objects()
|
||||
for obj in objects:
|
||||
if obj == "status":
|
||||
continue
|
||||
bank_section.delete_object(obj)
|
||||
bank_section.update_object("status",
|
||||
constants.RESOURCE_STATUS_DELETED)
|
||||
except Exception as err:
|
||||
LOG.error("delete volume backup failed, volume_id: %s.", volume_id)
|
||||
bank_section.update_object("status",
|
||||
constants.RESOURCE_STATUS_ERROR)
|
||||
raise exception.DeleteResourceFailed(
|
||||
name="Volume Glance Backup",
|
||||
reason=err,
|
||||
resource_id=volume_id,
|
||||
resource_type=constants.VOLUME_RESOURCE_TYPE)
|
||||
|
||||
|
||||
class VolumeGlanceProtectionPlugin(protection_plugin.ProtectionPlugin):
|
||||
_SUPPORT_RESOURCE_TYPES = [constants.VOLUME_RESOURCE_TYPE]
|
||||
|
||||
def __init__(self, config=None):
|
||||
super(VolumeGlanceProtectionPlugin, self).__init__(config)
|
||||
self._config.register_opts(volume_glance_opts,
|
||||
'volume_glance_plugin')
|
||||
self._plugin_config = self._config.volume_glance_plugin
|
||||
self._poll_interval = self._plugin_config.poll_interval
|
||||
self._backup_from_snapshot = self._plugin_config.backup_from_snapshot
|
||||
self._image_object_size = self._plugin_config.backup_image_object_size
|
||||
|
||||
@classmethod
|
||||
def get_supported_resources_types(cls):
|
||||
return cls._SUPPORT_RESOURCE_TYPES
|
||||
|
||||
@classmethod
|
||||
def get_options_schema(cls, resources_type):
|
||||
return volume_schemas.OPTIONS_SCHEMA
|
||||
|
||||
@classmethod
|
||||
def get_restore_schema(cls, resources_type):
|
||||
return volume_schemas.RESTORE_SCHEMA
|
||||
|
||||
@classmethod
|
||||
def get_saved_info_schema(cls, resources_type):
|
||||
return volume_schemas.SAVED_INFO_SCHEMA
|
||||
|
||||
@classmethod
|
||||
def get_saved_info(cls, metadata_store, resource):
|
||||
pass
|
||||
|
||||
def get_protect_operation(self, resource):
|
||||
return ProtectOperation(self._poll_interval,
|
||||
self._backup_from_snapshot,
|
||||
self._image_object_size)
|
||||
|
||||
def get_restore_operation(self, resource):
|
||||
return RestoreOperation(self._poll_interval)
|
||||
|
||||
def get_delete_operation(self, resource):
|
||||
return DeleteOperation()
|
|
@ -0,0 +1,51 @@
|
|||
# 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.
|
||||
|
||||
OPTIONS_SCHEMA = {
|
||||
"title": "Volume Glance Protection Options",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"backup_name": {
|
||||
"type": "string",
|
||||
"title": "Backup Name",
|
||||
"description": "The name of the backup.",
|
||||
"default": None
|
||||
},
|
||||
"description": {
|
||||
"type": "string",
|
||||
"title": "Description",
|
||||
"description": "The description of the backup."
|
||||
}
|
||||
},
|
||||
"required": ["backup_name"]
|
||||
}
|
||||
|
||||
RESTORE_SCHEMA = {
|
||||
"title": "Volume Glance Protection Restore",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"restore_name": {
|
||||
"type": "string",
|
||||
"title": "Restore Resource Name",
|
||||
"description": "The name of the restore resource ",
|
||||
"default": None
|
||||
},
|
||||
},
|
||||
"required": ["restore_name"]
|
||||
}
|
||||
|
||||
SAVED_INFO_SCHEMA = {
|
||||
"title": "Volume Glance Protection Saved Info",
|
||||
"type": "object",
|
||||
"properties": {},
|
||||
"required": []
|
||||
}
|
|
@ -0,0 +1,229 @@
|
|||
# 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 collections
|
||||
from karbor.common import constants
|
||||
from karbor.context import RequestContext
|
||||
from karbor.resource import Resource
|
||||
from karbor.services.protection.bank_plugin import Bank
|
||||
from karbor.services.protection.bank_plugin import BankPlugin
|
||||
from karbor.services.protection.bank_plugin import BankSection
|
||||
from karbor.services.protection import client_factory
|
||||
from karbor.services.protection.protection_plugins.volume.\
|
||||
volume_glance_plugin import VolumeGlanceProtectionPlugin
|
||||
from karbor.services.protection.protection_plugins.volume import \
|
||||
volume_glance_plugin_schemas
|
||||
from karbor.tests import base
|
||||
import mock
|
||||
from oslo_config import cfg
|
||||
from oslo_config import fixture
|
||||
|
||||
|
||||
class FakeBankPlugin(BankPlugin):
|
||||
def update_object(self, key, value):
|
||||
return
|
||||
|
||||
def get_object(self, key):
|
||||
return
|
||||
|
||||
def list_objects(self, prefix=None, limit=None, marker=None,
|
||||
sort_dir=None):
|
||||
return
|
||||
|
||||
def delete_object(self, key):
|
||||
return
|
||||
|
||||
def get_owner_id(self):
|
||||
return
|
||||
|
||||
|
||||
fake_bank = Bank(FakeBankPlugin())
|
||||
fake_bank_section = BankSection(bank=fake_bank, section="fake")
|
||||
|
||||
ResourceNode = collections.namedtuple(
|
||||
"ResourceNode",
|
||||
["value",
|
||||
"child_nodes"]
|
||||
)
|
||||
|
||||
|
||||
Volume = collections.namedtuple(
|
||||
"Volume",
|
||||
["id", "status", "size"]
|
||||
)
|
||||
|
||||
Snapshot = collections.namedtuple(
|
||||
"Snapshot",
|
||||
["id", "status", "size"]
|
||||
)
|
||||
|
||||
Image = collections.namedtuple(
|
||||
"Image",
|
||||
["disk_format",
|
||||
"container_format",
|
||||
"status",
|
||||
"id"]
|
||||
)
|
||||
|
||||
|
||||
def call_hooks(operation, checkpoint, resource, context, parameters, **kwargs):
|
||||
def noop(*args, **kwargs):
|
||||
pass
|
||||
|
||||
hooks = (
|
||||
'on_prepare_begin',
|
||||
'on_prepare_finish',
|
||||
'on_main',
|
||||
'on_complete',
|
||||
)
|
||||
for hook_name in hooks:
|
||||
hook = getattr(operation, hook_name, noop)
|
||||
hook(checkpoint, resource, context, parameters, **kwargs)
|
||||
|
||||
|
||||
class FakeCheckpoint(object):
|
||||
def __init__(self):
|
||||
super(FakeCheckpoint, self).__init__()
|
||||
self.bank_section = fake_bank_section
|
||||
|
||||
def get_resource_bank_section(self, resource_id=None):
|
||||
return self.bank_section
|
||||
|
||||
|
||||
class VolumeGlanceProtectionPluginTest(base.TestCase):
|
||||
def setUp(self):
|
||||
super(VolumeGlanceProtectionPluginTest, self).setUp()
|
||||
|
||||
plugin_config = cfg.ConfigOpts()
|
||||
plugin_config_fixture = self.useFixture(fixture.Config(plugin_config))
|
||||
plugin_config_fixture.load_raw_values(
|
||||
group='volume_glance_plugin',
|
||||
poll_interval=0,
|
||||
backup_image_object_size=65536
|
||||
)
|
||||
self.plugin = VolumeGlanceProtectionPlugin(plugin_config)
|
||||
cfg.CONF.set_default('glance_endpoint',
|
||||
'http://127.0.0.1:9292',
|
||||
'glance_client')
|
||||
|
||||
cfg.CONF.set_default('cinder_endpoint',
|
||||
'http://127.0.0.1:8774/v2.1',
|
||||
'cinder_client')
|
||||
self.cntxt = RequestContext(user_id='demo',
|
||||
project_id='abcd',
|
||||
auth_token='efgh'
|
||||
)
|
||||
self.cinder_client = client_factory.ClientFactory.create_client(
|
||||
"cinder", self.cntxt)
|
||||
self.glance_client = client_factory.ClientFactory.create_client(
|
||||
"glance", self.cntxt)
|
||||
self.checkpoint = FakeCheckpoint()
|
||||
|
||||
def test_get_options_schema(self):
|
||||
options_schema = self.plugin.get_options_schema(
|
||||
constants.VOLUME_RESOURCE_TYPE)
|
||||
self.assertEqual(options_schema,
|
||||
volume_glance_plugin_schemas.OPTIONS_SCHEMA)
|
||||
|
||||
def test_get_restore_schema(self):
|
||||
options_schema = self.plugin.get_restore_schema(
|
||||
constants.VOLUME_RESOURCE_TYPE)
|
||||
self.assertEqual(options_schema,
|
||||
volume_glance_plugin_schemas.RESTORE_SCHEMA)
|
||||
|
||||
def test_get_saved_info_schema(self):
|
||||
options_schema = self.plugin.get_saved_info_schema(
|
||||
constants.VOLUME_RESOURCE_TYPE)
|
||||
self.assertEqual(options_schema,
|
||||
volume_glance_plugin_schemas.SAVED_INFO_SCHEMA)
|
||||
|
||||
@mock.patch('karbor.services.protection.protection_plugins'
|
||||
'.utils.status_poll')
|
||||
@mock.patch('karbor.services.protection.clients.glance.create')
|
||||
@mock.patch('karbor.services.protection.clients.cinder.create')
|
||||
def test_create_backup(self, mock_cinder_create,
|
||||
mock_glance_create, mock_status_poll):
|
||||
resource = Resource(id="123",
|
||||
type=constants.VOLUME_RESOURCE_TYPE,
|
||||
name='fake')
|
||||
|
||||
fake_bank_section.update_object = mock.MagicMock()
|
||||
|
||||
protect_operation = self.plugin.get_protect_operation(resource)
|
||||
mock_cinder_create.return_value = self.cinder_client
|
||||
mock_glance_create.return_value = self.glance_client
|
||||
mock_status_poll.return_value = True
|
||||
self.cinder_client.volume_snapshots.create = mock.MagicMock()
|
||||
self.cinder_client.volume_snapshots.create.return_value = Snapshot(
|
||||
id="1234",
|
||||
status="available",
|
||||
size='100000000'
|
||||
)
|
||||
self.cinder_client.volume_snapshots.get = mock.MagicMock()
|
||||
self.cinder_client.volume_snapshots.get.return_value = Snapshot(
|
||||
id="1234",
|
||||
status="available",
|
||||
size='100000000'
|
||||
)
|
||||
self.cinder_client.volumes.create = mock.MagicMock()
|
||||
self.cinder_client.volumes.create.return_value = Volume(
|
||||
id='2345',
|
||||
status='available',
|
||||
size=1
|
||||
)
|
||||
self.cinder_client.volumes.get = mock.MagicMock()
|
||||
self.cinder_client.volumes.get.return_value = Volume(
|
||||
id='2345',
|
||||
status='available',
|
||||
size=1
|
||||
)
|
||||
self.cinder_client.volumes.upload_to_image = mock.MagicMock()
|
||||
self.cinder_client.volumes.upload_to_image.return_value = [202, {
|
||||
'os-volume_upload_image': {
|
||||
'image_id': "3456"
|
||||
}
|
||||
}]
|
||||
|
||||
self.glance_client.images.get = mock.MagicMock()
|
||||
self.glance_client.images.return_value = Image(
|
||||
disk_format="raw",
|
||||
container_format="bare",
|
||||
status="active",
|
||||
id="3456"
|
||||
)
|
||||
fake_bank_section.update_object = mock.MagicMock()
|
||||
self.glance_client.images.data = mock.MagicMock()
|
||||
self.glance_client.images.data.return_value = []
|
||||
mock_status_poll.return_value = True
|
||||
|
||||
self.cinder_client.volume_snapshots.delete = mock.MagicMock()
|
||||
self.cinder_client.volumes.delete = mock.MagicMock()
|
||||
self.glance_client.images.delete = mock.MagicMock()
|
||||
|
||||
call_hooks(protect_operation, self.checkpoint, resource, self.cntxt,
|
||||
{})
|
||||
|
||||
def test_delete_backup(self):
|
||||
resource = Resource(id="123",
|
||||
type=constants.VOLUME_RESOURCE_TYPE,
|
||||
name='fake')
|
||||
|
||||
fake_bank_section.list_objects = mock.MagicMock()
|
||||
fake_bank_section.list_objects.return_value = ["data_1", "data_2"]
|
||||
fake_bank_section.delete_object = mock.MagicMock()
|
||||
delete_operation = self.plugin.get_delete_operation(resource)
|
||||
call_hooks(delete_operation, self.checkpoint, resource, self.cntxt,
|
||||
{})
|
||||
|
||||
def test_get_supported_resources_types(self):
|
||||
types = self.plugin.get_supported_resources_types()
|
||||
self.assertEqual([constants.VOLUME_RESOURCE_TYPE], types)
|
|
@ -49,6 +49,7 @@ karbor.protections =
|
|||
karbor-volume-freezer-plugin = karbor.services.protection.protection_plugins.volume.volume_freezer_plugin:FreezerProtectionPlugin
|
||||
karbor-volume-protection-plugin = karbor.services.protection.protection_plugins.volume.cinder_protection_plugin:CinderBackupProtectionPlugin
|
||||
karbor-volume-snapshot-plugin = karbor.services.protection.protection_plugins.volume.volume_snapshot_plugin:VolumeSnapshotProtectionPlugin
|
||||
karbor-volume-glance-plugin = karbor.services.protection.protection_plugins.volume.volume_glance_plugin:VolumeGlanceProtectionPlugin
|
||||
karbor-image-protection-plugin = karbor.services.protection.protection_plugins.image.image_protection_plugin:GlanceProtectionPlugin
|
||||
karbor-server-protection-plugin = karbor.services.protection.protection_plugins.server.nova_protection_plugin:NovaProtectionPlugin
|
||||
karbor-share-protection-plugin = karbor.services.protection.protection_plugins.share.share_snapshot_plugin:ManilaSnapshotProtectionPlugin
|
||||
|
|
Loading…
Reference in New Issue