diff options
author | Mike Crute <mike@crute.us> | 2017-12-25 05:25:14 +0000 |
---|---|---|
committer | Mike Crute <mike@crute.us> | 2017-12-25 05:25:14 +0000 |
commit | 6fbb4d8ead5762ed69b716eae70cbd23a4c77b5c (patch) | |
tree | 2cc2e0496bc71b0d9137158294517e513eedaf84 | |
parent | eb26234ebcb842419ca052dcfdb3e3259b7d1102 (diff) | |
download | alpine-ec2-ami-6fbb4d8ead5762ed69b716eae70cbd23a4c77b5c.tar.bz2 alpine-ec2-ami-6fbb4d8ead5762ed69b716eae70cbd23a4c77b5c.tar.xz alpine-ec2-ami-6fbb4d8ead5762ed69b716eae70cbd23a4c77b5c.zip |
Also delete snapshots
-rw-r--r-- | scrub-old-amis.py.in | 21 |
1 files changed, 13 insertions, 8 deletions
diff --git a/scrub-old-amis.py.in b/scrub-old-amis.py.in index 756f537..2cbbcf8 100644 --- a/scrub-old-amis.py.in +++ b/scrub-old-amis.py.in | |||
@@ -28,8 +28,9 @@ for region in boto3.session.Session().get_available_regions("ec2"): | |||
28 | 28 | ||
29 | os_rel, ami_rel = match.groups() | 29 | os_rel, ami_rel = match.groups() |
30 | amis.append(( | 30 | amis.append(( |
31 | region, image["ImageId"], float(os_rel), | 31 | region, image["ImageId"], |
32 | int(ami_rel) if ami_rel else 0)) | 32 | image["BlockDeviceMappings"][0]["Ebs"]["SnapshotId"], |
33 | float(os_rel), int(ami_rel) if ami_rel else 0)) | ||
33 | 34 | ||
34 | 35 | ||
35 | # Determine the set to discard based region and version | 36 | # Determine the set to discard based region and version |
@@ -43,10 +44,10 @@ discards = [] | |||
43 | # that a region only has old images. In that case we want to preserve the old | 44 | # that a region only has old images. In that case we want to preserve the old |
44 | # images till we can publish new ones manually so users can still launch | 45 | # images till we can publish new ones manually so users can still launch |
45 | # Alpine systems without interruption. | 46 | # Alpine systems without interruption. |
46 | candidates = sorted(amis, key=lambda i: (i[0], (i[1], i[2])), reverse=True) | 47 | candidates = sorted(amis, key=lambda i: (i[0], (i[1], i[3])), reverse=True) |
47 | 48 | ||
48 | for ami in candidates: | 49 | for ami in candidates: |
49 | (region, ami), version = ami[:2], ami[2:] | 50 | (region, ami, snapshot), version = ami[:3], ami[3:] |
50 | 51 | ||
51 | if version > current: | 52 | if version > current: |
52 | print("{} has AMI '{}' newer than current".format(region, ami)) | 53 | print("{} has AMI '{}' newer than current".format(region, ami)) |
@@ -55,13 +56,17 @@ for ami in candidates: | |||
55 | ok_regions.add(region) | 56 | ok_regions.add(region) |
56 | continue | 57 | continue |
57 | elif version < current and region in ok_regions: | 58 | elif version < current and region in ok_regions: |
58 | discards.append((region, ami)) | 59 | discards.append((region, ami, snapshot)) |
59 | else: | 60 | else: |
60 | print("Not discarding old image in {}".format(region)) | 61 | print("Not discarding old image in {}".format(region)) |
61 | continue | 62 | continue |
62 | 63 | ||
63 | 64 | ||
64 | # Scrub the old ones | 65 | # Scrub the old ones |
65 | for region, image in discards: | 66 | for region, image, snapshot in discards: |
66 | print("Removing image '{}' in {}".format(image, region)) | 67 | print("Removing image '{}', snapshot '{}' in {}".format( |
67 | boto3.client("ec2", region_name=region).deregister_image(ImageId=image) | 68 | image, snapshot, region)) |
69 | |||
70 | ec2 = boto3.client("ec2", region_name=region) | ||
71 | ec2.deregister_image(ImageId=image) | ||
72 | ec2.delete_snapshot(SnapshotId=snapshot) | ||