-
Notifications
You must be signed in to change notification settings - Fork 135
fix(bench): Remove bench image after archival (backport #508) #530
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import contextlib | ||
| import json | ||
| import os | ||
| import platform | ||
|
|
@@ -62,6 +63,14 @@ def docker_login(self, registry): | |
| password = registry["password"] | ||
| return self.execute(f"docker login -u {username} -p {password} {url}") | ||
|
|
||
| def docker_inspect_manifest(self, image_tag: str): | ||
| try: | ||
| return self.execute(f"docker manifest inspect {image_tag}") | ||
| except AgentException as e: | ||
| if "no such manifest" in e.data.get("output", ""): | ||
| raise Exception(f"Image {image_tag} not found in registry") from e | ||
| raise | ||
|
|
||
| def establish_connection_with_registry(self, max_retries: int, registry: dict[str, str]): | ||
| """Given the attempt count try and establish connection with the registry else Raise""" | ||
| for attempt in range(max_retries): | ||
|
|
@@ -383,8 +392,11 @@ def archive_bench(self, name): | |
| bench_directory = os.path.join(self.benches_directory, name) | ||
| if not os.path.exists(bench_directory): | ||
| return | ||
|
|
||
| image_tag = None | ||
| try: | ||
| bench = Bench(name, self) | ||
| image_tag = bench.docker_image | ||
| except json.JSONDecodeError: | ||
| self.disable_production_on_bench(name) | ||
| except FileNotFoundError as e: | ||
|
|
@@ -397,6 +409,16 @@ def archive_bench(self, name): | |
|
|
||
| self.container_exists(name) | ||
| self.move_bench_to_archived_directory(name) | ||
| if image_tag: | ||
| self.remove_docker_image(image_tag) | ||
|
|
||
| def remove_docker_image(self, image_tag: str): | ||
| # Check if the image is present in registry before trying to remove locally | ||
| with contextlib.suppress(Exception): | ||
| manifest = self.docker_inspect_manifest(image_tag) | ||
| if not manifest: | ||
| return | ||
| self.execute(f"docker rmi {image_tag} --force") | ||
|
Comment on lines
+417
to
+421
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| @job("Cleanup Unused Files", priority="low") | ||
| def cleanup_unused_files(self, force: bool = False): | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if not manifest: returnis unreachable dead codedocker_inspect_manifestnever returns a falsy value on success — ifdocker manifest inspectexits cleanly it emits a non-empty JSON blob; if the image is absent it raisesException("Image ... not found in registry"); for all other failures it re-raisesAgentException. None of those paths produce a falsy return, so the early-return guard will never fire. The guard can be removed to avoid confusion about why the subsequentdocker rmimight be skipped.Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!