Initiate deletion of image files if the import was interrupted

If the image is deleted by appropriate API call while its content
is still being uploaded in import task in v2, an exception is raised
and it is not handled in the API code. This leads to the fact that
the uploaded image file stays in a storage and clogs it.

There existed code that safely removes image files if the exception
occurs.

Change-Id: I4f7d1aa103f4ce7abf4026e7097b9e76c24135fa
Closes-Bug: 1371118
This commit is contained in:
Mike Fedosin 2014-09-18 18:07:42 +04:00
parent d62c8f07e7
commit 7858d4d951
1 changed files with 24 additions and 14 deletions

View File

@ -23,6 +23,7 @@ import six
from glance.api.v2 import images as v2_api
from glance.common import exception
from glance.common.scripts import utils as script_utils
from glance.common import store_utils
from glance.common import utils as common_utils
from glance import i18n
from glance.openstack.common import lockutils
@ -92,21 +93,30 @@ def import_image(image_repo, image_factory, task_input, task_id, uri):
# Image object returned from create_image method does not have appropriate
# factories wrapped around it.
image_id = original_image.image_id
new_image = image_repo.get(image_id)
if new_image.status in ['saving']:
new_image.status = 'active'
new_image.size = original_image.size
new_image.virtual_size = original_image.virtual_size
new_image.checksum = original_image.checksum
else:
msg = _("The Image %(image_id)s object being created by this task "
"%(task_id)s, is no longer in valid status for further "
"processing.") % {"image_id": new_image.image_id,
"task_id": task_id}
raise exception.Conflict(msg)
image_repo.save(new_image)
try:
new_image = image_repo.get(image_id)
if new_image.status == 'saving':
new_image.status = 'active'
new_image.size = original_image.size
new_image.virtual_size = original_image.virtual_size
new_image.checksum = original_image.checksum
else:
msg = _("The Image %(image_id)s object being created by this task "
"%(task_id)s, is no longer in valid status for further "
"processing.") % {"image_id": new_image.image_id,
"task_id": task_id}
raise exception.Conflict(msg)
image_repo.save(new_image)
return image_id
return image_id
except (exception.Conflict, exception.NotFound):
with excutils.save_and_reraise_exception():
if original_image.locations:
for location in original_image.locations:
store_utils.delete_image_location_from_backend(
original_image.context,
original_image.image_id,
location)
def create_image(image_repo, image_factory, image_properties, task_id):