Delete AWS image builds

The AWS driver adapter was not actually deleting the image build
from AWS when the builder requested it (it was simply missing
the implementation of the method).

This went unnoticed because as long as a launcher is running, it
will act to clean up leaked images.  But it's still better to
have the builder do so synchronously.

Add a test which verifies the behavior.

Change-Id: I0bea930847ded45574bf53ae098b9926d4924107
This commit is contained in:
James E. Blair 2022-04-27 11:59:54 -07:00
parent dae31ef620
commit eb7f58ce0e
3 changed files with 25 additions and 0 deletions

View File

@ -341,7 +341,17 @@ class AwsAdapter(statemachine.Adapter):
return task['ImageId']
def deleteImage(self, external_id):
snaps = set()
self.log.debug(f"Deleting image {external_id}")
for ami in self._listAmis():
if ami.id == external_id:
for bdm in ami.block_device_mappings:
snapid = bdm.get('Ebs', {}).get('SnapshotId')
if snapid:
snaps.add(snapid)
self._deleteAmi(external_id)
for snapshot_id in snaps:
self._deleteSnapshot(snapshot_id)
# Local implementation below

View File

@ -1,3 +1,8 @@
elements-dir: .
images-dir: '{images_dir}'
build-log-dir: '{build_log_dir}'
build-log-retention: 1
zookeeper-servers:
- host: {zookeeper_host}
port: {zookeeper_port}

View File

@ -382,6 +382,16 @@ class TestDriverAws(tests.DBTestCase):
self.assertEqual(node.attributes,
{'key1': 'value1', 'key2': 'value2'})
def test_aws_diskimage_removal(self):
self.patch(AwsAdapter, '_import_image', self.fake_aws.import_image)
self.patch(AwsAdapter, '_get_paginator', self.fake_aws.get_paginator)
configfile = self.setup_config('aws/diskimage.yaml')
self.useBuilder(configfile)
self.waitForImage('ec2-us-west-2', 'fake-image')
self.replace_config(configfile, 'aws/config.yaml')
self.waitForImageDeletion('ec2-us-west-2', 'fake-image')
self.waitForBuildDeletion('fake-image', '0000000001')
def test_aws_resource_cleanup(self):
self.patch(AwsAdapter, '_get_paginator', self.fake_aws.get_paginator)