From 51e86da74295279e46fe1ec382d956d3db2de8ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pavel=20Dost=C3=A1l?= Date: Mon, 24 Jun 2024 10:28:30 +0200 Subject: [PATCH] Do not remove EC2 images with 'pcw_ignore' tag set to '1' --- ocw/lib/ec2.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/ocw/lib/ec2.py b/ocw/lib/ec2.py index 7a43a0c7..92b80340 100644 --- a/ocw/lib/ec2.py +++ b/ocw/lib/ec2.py @@ -352,8 +352,13 @@ def cleanup_images(self, valid_period_days: float) -> None: self.log_dbg(f"Found {len(response['Images'])} images in {region}") for img in response['Images']: if EC2.is_outdated(parse(img['CreationDate']), valid_period_days): - if self.dry_run: - self.log_info(f"Image deletion {img['ImageId']} skipped due to dry run mode") + tags = img.get('Tags', []) + pcw_ignore_tag = next((tag for tag in tags if tag['Key'] == Instance.TAG_IGNORE), None) + if pcw_ignore_tag: + self.log_dbg(f"Ignoring {img['Name']} due to 'pcw_ignore' tag set to '1'") else: - self.log_info(f"Delete image '{img['Name']}' (ami:{img['ImageId']})") - self.ec2_client(region).deregister_image(ImageId=img['ImageId'], DryRun=False) + if self.dry_run: + self.log_info(f"Image deletion {img['ImageId']} skipped due to dry run mode") + else: + self.log_info(f"Delete image '{img['Name']}' (ami:{img['ImageId']})") + self.ec2_client(region).deregister_image(ImageId=img['ImageId'], DryRun=False)