Skip to content

Commit 2360ec2

Browse files
committed
[cloud] Delete underlying snapshots when deleting Alibaba Cloud images
The underlying snapshots are not automatically deleted along with the image, and there is no flag that can be set to cause them to be automatically deleted. Tag the underlying snapshots for deletion before deleting the image, delete the image, and then delete any such tagged snapshots (including any that may remain from a previous failed deletion attempt). Signed-off-by: Michael Brown <mcb30@ipxe.org>
1 parent 82a8fcc commit 2360ec2

1 file changed

Lines changed: 36 additions & 0 deletions

File tree

contrib/cloud/ali-import

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ OSS_FORBIDDEN_REGION_CODE = 'ForbidCreateNewBucket'
8282
OSS_BUCKET_NAME_LEN = 63
8383

8484
IPXE_STORAGE_PREFIX = 'ipxe-upload-temp-'
85+
IPXE_SNAPSHOT_DELETE_TAG = 'ipxe-snapshot-delete'
8586

8687
POLL_INTERVAL_SEC = 5
8788
POLL_MAX_RETRIES = 100
@@ -389,6 +390,21 @@ def delete_image(clients, name):
389390
for image in rsp.body.images.image or ():
390391
logger.info("delete image %s %s (%s)" %
391392
(clients.region, image.image_name, image.image_id))
393+
# Tag associated snapshots for deletion
394+
for disk in image.disk_device_mappings.disk_device_mapping or ():
395+
snapshot_id = disk.snapshot_id
396+
tag = ecs.models.TagResourcesRequestTag(
397+
key=IPXE_SNAPSHOT_DELETE_TAG,
398+
value=IPXE_SNAPSHOT_DELETE_TAG,
399+
)
400+
req = ecs.models.TagResourcesRequest(
401+
region_id=clients.region,
402+
resource_type='snapshot',
403+
resource_id=[snapshot_id],
404+
tag=[tag],
405+
)
406+
rsp = clients.ecs.tag_resources_with_options(req, RUNTIME_OPTS)
407+
# Unpublish image
392408
if image.is_public:
393409
req = ecs.models.ModifyImageSharePermissionRequest(
394410
region_id=clients.region,
@@ -398,11 +414,31 @@ def delete_image(clients, name):
398414
rsp = clients.ecs.modify_image_share_permission_with_options(
399415
req, RUNTIME_OPTS
400416
)
417+
# Delete image
401418
req = ecs.models.DeleteImageRequest(
402419
region_id=clients.region,
403420
image_id=image.image_id
404421
)
405422
rsp = clients.ecs.delete_image_with_options(req, RUNTIME_OPTS)
423+
# Delete any snapshots tagged for deletion
424+
tag = ecs.models.ListTagResourcesRequestTag(
425+
key=IPXE_SNAPSHOT_DELETE_TAG,
426+
value=IPXE_SNAPSHOT_DELETE_TAG,
427+
)
428+
req = ecs.models.ListTagResourcesRequest(
429+
region_id=clients.region,
430+
resource_type='snapshot',
431+
tag=[tag],
432+
)
433+
rsp = clients.ecs.list_tag_resources_with_options(req, RUNTIME_OPTS)
434+
for snapshot in rsp.body.tag_resources.tag_resource or ():
435+
logger.info("delete snapshot %s %s" %
436+
(clients.region, snapshot.resource_id))
437+
req = ecs.models.DeleteSnapshotRequest(
438+
snapshot_id=snapshot.resource_id,
439+
force=True,
440+
)
441+
rsp = clients.ecs.delete_snapshot_with_options(req, RUNTIME_OPTS)
406442

407443
def wait_for_task(clients, task_id):
408444
"""Wait for task to complete"""

0 commit comments

Comments
 (0)