-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelete.py
78 lines (49 loc) · 1.72 KB
/
delete.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import boto3
from datetime import datetime
from datetime import timezone
from dateutil import parser
ec2 = boto3.resource("ec2")
def _filter(name):
return ec2.images.filter(
Filters=[{"Name": "name", "Values": [name]}], Owners=["self"]
)
def _sorted(name):
return sorted(_filter(name), key=lambda i: i.creation_date, reverse=True)
def _delete(images, dry_run):
for c, i in enumerate(images, start=1):
get_snapshot_id = lambda: [
d["Ebs"]["SnapshotId"] for d in i.block_device_mappings if "Ebs" in d
][0]
name = i.name
snapshot_id = get_snapshot_id()
print("Deleting %d/%d" % (c, len(images)))
print("%s deregistering" % name)
if not dry_run:
i.deregister()
print("%s deregistered" % name)
print("%s deleting" % snapshot_id)
if not dry_run:
ec2.Snapshot(snapshot_id).delete()
print("%s deleted" % snapshot_id)
def _list(name):
for c, i in enumerate(images := _sorted(name), start=1):
print("Remaining %d/%d" % (c, len(images)))
print(i.name)
def delete_tail(name, dry_run):
_, *tail = _sorted(name)
_delete(tail, dry_run)
_list(name)
def delete_days(name, days, dry_run):
b, *images = _filter(name)
def s(i):
nonlocal b
j, b = b, i
return j
today = datetime.now(timezone.utc).replace(
hour=0, minute=0, second=0, microsecond=0
)
dt = lambda i: parser.isoparse(i.creation_date)
test = lambda i: (today - dt(i)).days >= days
f = lambda i: s(i) if dt(i) > dt(b) else i # if i is larger swap with bucket (b)
_delete([j for i in images if test(j := f(i))], dry_run)
_list(name)