Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not remove EC2 images with 'pcw_ignore' tag set to '1' #388

Merged
merged 1 commit into from
Jun 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions ocw/lib/ec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)