|
10 | 10 | from contextlib import suppress |
11 | 11 | from datetime import datetime |
12 | 12 |
|
13 | | -from jinja2 import Environment, PackageLoader |
14 | | -from passlib.hash import pbkdf2_sha256 as pbkdf2 |
15 | | -from peewee import MySQLDatabase |
16 | | - |
17 | 13 | from agent.application_storage_analyzer import ( |
18 | 14 | analyze_benches_structure, |
| 15 | + format_size, |
19 | 16 | parse_docker_df_output, |
20 | 17 | parse_total_disk_usage_output, |
21 | 18 | to_bytes, |
|
26 | 23 | from agent.job import Job, Step, job, step |
27 | 24 | from agent.patch_handler import run_patches |
28 | 25 | from agent.site import Site |
29 | | -from agent.utils import get_supervisor_processes_status, is_registry_healthy, format_reclaimable_size |
| 26 | +from agent.utils import get_supervisor_processes_status, is_registry_healthy |
| 27 | +from jinja2 import Environment, PackageLoader |
| 28 | +from passlib.hash import pbkdf2_sha256 as pbkdf2 |
| 29 | +from peewee import MySQLDatabase |
30 | 30 |
|
31 | 31 |
|
32 | 32 | class Server(Base): |
@@ -147,20 +147,30 @@ def get_image_size(self, image_tag: str): |
147 | 147 | except AgentException: |
148 | 148 | pass |
149 | 149 |
|
| 150 | + def unused_image_size(self) -> list[float]: |
| 151 | + """Get the sizes of all the images that are not in use in bytes""" |
| 152 | + images_present = self.execute("docker image ls --format '{{.Repository}}:{{.Tag}} {{.Size}}'")[ |
| 153 | + "output" |
| 154 | + ].split("\n") |
| 155 | + images_present = [image.split() for image in images_present] |
| 156 | + images_in_use = self.execute("docker container ls --format {{.Image}}")["output"].split("\n") |
| 157 | + |
| 158 | + return [ |
| 159 | + to_bytes(size) for image_name, size in images_present if image_name not in images_in_use |
| 160 | + ] |
| 161 | + |
150 | 162 | def get_reclaimable_size(self) -> dict[str, dict[str, float] | float]: |
151 | 163 | """Checks archived and unused docker artefacts size""" |
152 | 164 | archived_folder_size = self.execute("du -sB1 /home/frappe/archived/ | awk '{print $1}'").get("output") |
153 | | - docker_reclaimable_size = self.execute("docker system df --format {{.Reclaimable}}").get("output") |
| 165 | + unused_images_size = sum(self.unused_image_size()) |
154 | 166 |
|
155 | 167 | formatted_archived_folder_size = f"{round(float(archived_folder_size) / 1024**3, 2)}GB" |
156 | | - formatted_docker_reclaimable_size, total_docker_size = format_reclaimable_size( |
157 | | - docker_reclaimable_size |
158 | | - ) |
| 168 | + formatted_unused_image_size = format_size(unused_images_size) |
159 | 169 |
|
160 | 170 | return { |
161 | 171 | "archived": formatted_archived_folder_size, |
162 | | - "docker": formatted_docker_reclaimable_size, |
163 | | - "total": round((total_docker_size + float(archived_folder_size)) / 1024**3, 2), |
| 172 | + "images": formatted_unused_image_size, |
| 173 | + "total": round((unused_images_size + float(archived_folder_size)) / 1024**3, 2), |
164 | 174 | } |
165 | 175 |
|
166 | 176 | def _check_site_on_bench(self, bench_name: str): |
|
0 commit comments