Skip to content

Commit 1a2a95c

Browse files
Merge pull request #282 from frappe/mergify/bp/master/pr-281
fix(size): Calculate docker image size manually (backport #281)
2 parents 47434db + 9456857 commit 1a2a95c

2 files changed

Lines changed: 21 additions & 33 deletions

File tree

agent/server.py

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,9 @@
1010
from contextlib import suppress
1111
from datetime import datetime
1212

13-
from jinja2 import Environment, PackageLoader
14-
from passlib.hash import pbkdf2_sha256 as pbkdf2
15-
from peewee import MySQLDatabase
16-
1713
from agent.application_storage_analyzer import (
1814
analyze_benches_structure,
15+
format_size,
1916
parse_docker_df_output,
2017
parse_total_disk_usage_output,
2118
to_bytes,
@@ -26,7 +23,10 @@
2623
from agent.job import Job, Step, job, step
2724
from agent.patch_handler import run_patches
2825
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
3030

3131

3232
class Server(Base):
@@ -147,20 +147,30 @@ def get_image_size(self, image_tag: str):
147147
except AgentException:
148148
pass
149149

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+
150162
def get_reclaimable_size(self) -> dict[str, dict[str, float] | float]:
151163
"""Checks archived and unused docker artefacts size"""
152164
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())
154166

155167
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)
159169

160170
return {
161171
"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),
164174
}
165175

166176
def _check_site_on_bench(self, bench_name: str):

agent/utils.py

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -250,25 +250,3 @@ def get_supervisor_processes_status() -> dict[str, str | dict[str, str]]:
250250
return dict(nested_status)
251251
except Exception:
252252
return {}
253-
254-
255-
def format_reclaimable_size(output: str) -> tuple[dict[str, float], float]:
256-
"""
257-
Example Output:
258-
72.81MB (1%)
259-
0B (0%)
260-
0B
261-
0B
262-
"""
263-
reclaimable_size = {}
264-
parts = ["images", "containers"]
265-
output = output.split("\n")
266-
total_size = 0
267-
268-
for idx, part in enumerate(parts, start=0):
269-
size = output[idx]
270-
size = size.split()[0]
271-
reclaimable_size[part] = format_size(to_bytes(size))
272-
total_size += to_bytes(size)
273-
274-
return reclaimable_size, total_size

0 commit comments

Comments
 (0)