Skip to content

Commit e6f6816

Browse files
committed
delete images in batches
1 parent db0cbbe commit e6f6816

File tree

1 file changed

+26
-12
lines changed

1 file changed

+26
-12
lines changed

automation/services/gcloud-cleaner/scripts/clean_old_images.py

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
]
2222
project = "gcr.io/o1labs-192920"
2323
github_repo = "minaProtocol/mina"
24+
batch_size = 10
25+
2426

2527
def str2bool(v):
2628
if isinstance(v, bool):
@@ -47,16 +49,19 @@ def str2bool(v):
4749
commits_to_skip = list(map(lambda x: x.commit.sha[0:7], repo.get_tags()))
4850

4951

50-
def delete_image(tag,dryrun,reason):
51-
image_prefix = tag["digest"][:15]
52+
def delete_images(tags,dryrun,gcr_repository,reason):
5253
if dryrun:
53-
print(f'[DRYRUN]: {gcr_repository}: deleting {image_prefix} because {reason}')
54+
for tag in tags:
55+
image_prefix = tag[:15]
56+
print(f'[DRYRUN]: {gcr_repository}: deleting {image_prefix} because {reason}')
5457
else:
55-
id = f'{gcr_repository}@{tag["digest"]}'
56-
print(f'{gcr_repository}: deleting {image_prefix} because ${reason}')
57-
cmd = ["gcloud", "container", "images", "delete", id,"--quiet", "--force-delete-tags"]
58-
subprocess.run(cmd)
59-
58+
command = ["gcloud", "container", "images", "delete"]
59+
for tag in tags:
60+
image_prefix = tag[:15]
61+
print(f'{gcr_repository}: deleting {image_prefix} because {reason}')
62+
command.append(f'{gcr_repository}@{tag}')
63+
command.extend(["--quiet", "--force-delete-tags"])
64+
subprocess.run(command)
6065

6166
for gcr_repository in gcr_repositories:
6267
print(f"Cleaning {gcr_repository} repository... it may take a while")
@@ -66,19 +71,28 @@ def delete_image(tag,dryrun,reason):
6671

6772
threshold = datetime.now().date() - timedelta(days=args.age)
6873
deleted = 0
74+
to_be_deleted = []
6975
for tag in tags:
7076
github_tags = tag["tags"]
7177
if not any(any(commit_to_skip in x for x in github_tags) for commit_to_skip in commits_to_skip):
7278
timestamp = tag["timestamp"]
7379
if timestamp is None:
74-
delete_image(tag,args.dryrun,f"image doesn't have timestamp property (which means it's very old) and not tagged with version")
75-
deleted += 1
80+
to_be_deleted.append(tag["digest"])
7681
else:
7782
date = datetime.strptime(timestamp["datetime"],"%Y-%m-%d %H:%M:%S%z")
7883
if date.date() < threshold:
79-
delete_image(tag,args.dryrun,f"is older than {args.age} days and not tagged with version")
80-
deleted += 1
84+
to_be_deleted.append(tag["digest"])
8185

86+
if len(to_be_deleted) > batch_size:
87+
delete_images(to_be_deleted,args.dryrun,gcr_repository,f"is older than {args.age} days and not tagged with version")
88+
deleted += len(to_be_deleted)
89+
to_be_deleted = []
90+
91+
if len(to_be_deleted) > 0:
92+
delete_images(to_be_deleted,args.dryrun,gcr_repository,f"is older than {args.age} days and not tagged with version")
93+
deleted += len(to_be_deleted)
94+
to_be_deleted = []
95+
8296
total = len(tags)
8397

8498
suffix = "will be deleted in standard run" if args.dryrun else "deleted"

0 commit comments

Comments
 (0)