Browse Source

Staging area not cleared if image is deleted while importing

If multiple stores configured in glance and Image is deleted while import
operation is in progress then image data stays in staging area
(filesystem backend) and there is no other way than clearing it
manually.

Modified delete method to delete the data from staging area if image is
deleted while import operation is in progress.
Conflicts:
        glance/api/v2/images.py
        glance/async_/flows/api_image_import.py
        glance/common/store_utils.py

NOTE: Conflicts occured due to reserved store capability is not
available in Stein.

Change-Id: Ib58accd6514e589dccde57fe063815b1ab1ce496
Closes-Bug: #1855417
(cherry picked from commit 5d15f07371)
tags/18.0.1^0
Abhishek Kekane 2 months ago
parent
commit
149ea050cc
2 changed files with 50 additions and 20 deletions
  1. +18
    -20
      glance/api/v2/images.py
  2. +32
    -0
      glance/tests/unit/v2/test_images_resource.py

+ 18
- 20
glance/api/v2/images.py View File

@@ -335,27 +335,25 @@ class ImagesController(object):
image_repo = self.gateway.get_repo(req.context)
try:
image = image_repo.get(image_id)

if image.status == 'uploading':
file_path = str(
CONF.node_staging_uri + '/' + image.image_id)[7:]
if os.path.exists(file_path):
try:
LOG.debug(
"After upload to the backend, deleting staged "
"image data from %(fn)s", {'fn': file_path})
os.unlink(file_path)
except OSError as e:
LOG.error(
"After upload to backend, deletion of staged "
"image data from %(fn)s has failed because "
"[Errno %(en)d]", {'fn': file_path,
'en': e.errno})
else:
LOG.warning(_(
file_path = str(
CONF.node_staging_uri + '/' + image_id)[7:]
if os.path.exists(file_path):
try:
LOG.debug(
"After upload to the backend, deleting staged "
"image data from %(fn)s", {'fn': file_path})
os.unlink(file_path)
except OSError as e:
LOG.error(
"After upload to backend, deletion of staged "
"image data has failed because "
"it cannot be found at %(fn)s"), {'fn': file_path})
"image data from %(fn)s has failed because "
"[Errno %(en)d]", {'fn': file_path,
'en': e.errno})
else:
LOG.warning(_(
"After upload to backend, deletion of staged "
"image data has failed because "
"it cannot be found at %(fn)s"), {'fn': file_path})

image.delete()
image_repo.remove(image)

+ 32
- 0
glance/tests/unit/v2/test_images_resource.py View File

@@ -2493,6 +2493,38 @@ class TestImagesController(base.IsolatedUnitTest):
self.assertEqual('deleted', deleted_img['status'])
self.assertNotIn('%s/%s' % (BASE_URI, UUID1), self.store.data)

def test_verify_staging_data_deleted_on_image_delete(self):
self.config(enabled_backends={'fake-store': 'file'})
self.config(node_staging_uri='file:///tmp/staging')
image_id = str(uuid.uuid4())
self.images = [
_db_fixture(image_id, owner=TENANT1,
name='1',
disk_format='raw',
container_format='bare',
status='importing',
checksum=None,
os_hash_algo=None,
os_hash_value=None),
]
self.db.image_create(None, self.images[0])
request = unit_test_utils.get_fake_request()
try:
with mock.patch.object(os.path, 'exists') as mock_exists:
mock_exists.return_value = True
with mock.patch.object(os, "unlink") as mock_unlik:
self.controller.delete(request, image_id)

self.assertEqual(1, mock_exists.call_count)
self.assertEqual(1, mock_unlik.call_count)
except Exception as e:
self.fail("Delete raised exception: %s" % e)

deleted_img = self.db.image_get(request.context, image_id,
force_show_deleted=True)
self.assertTrue(deleted_img['deleted'])
self.assertEqual('deleted', deleted_img['status'])

def test_delete_with_tags(self):
request = unit_test_utils.get_fake_request()
changes = [

Loading…
Cancel
Save