Skip to content

Commit

Permalink
Merge pull request #388 from pdostal/ec2_pcw_ignore_image
Browse files Browse the repository at this point in the history
Do not remove EC2 images with 'pcw_ignore' tag set to '1'
  • Loading branch information
ricardobranco777 committed Jun 25, 2024
2 parents 40e3e98 + 51e86da commit 6c2c0c3
Showing 1 changed file with 9 additions and 4 deletions.
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)

0 comments on commit 6c2c0c3

Please sign in to comment.