Skip to content

Commit ed1b190

Browse files
committed
Added dockerstats command
Signed-off-by: Aloys Baillet <[email protected]>
1 parent 3229093 commit ed1b190

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

python/aswfdocker/cli/aswfdocker.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,31 @@ def images():
267267
click.echo(f"{group}/{image_name}:{version}")
268268

269269

270+
@click.option(
271+
"--sizes", "-s", is_flag=True, help="Display Sizes",
272+
)
273+
@cli.command()
274+
def dockerstats(sizes):
275+
"""Lists all known ci images in this format: IMAGEGROUP/IMAGE:VERSION
276+
"""
277+
if sizes:
278+
total_size = 0
279+
for org, image_type, image in utils.iter_all_images():
280+
image_name = utils.get_image_name(image_type, image)
281+
for tag, size in utils.get_image_sizes(org, image_name).items():
282+
click.echo(f"{org}/{image_name}:{tag} size={size}")
283+
total_size += size
284+
click.echo(f"Total size={total_size}")
285+
else:
286+
total_pull = 0
287+
for org, image_type, image in utils.iter_all_images():
288+
image_name = utils.get_image_name(image_type, image)
289+
pull_count = utils.get_image_pull_count(org, image_name)
290+
click.echo(f"{org}/{image_name} pull_count={pull_count}")
291+
total_pull += pull_count
292+
click.echo(f"Total pull_count={total_pull}")
293+
294+
270295
@cli.command()
271296
@click.option(
272297
"--settings-path", "-p", default="~/.aswfdocker", help="User settings file path.",

python/aswfdocker/utils.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import subprocess
99
import datetime
1010
import logging
11+
import json
12+
import urllib.request
1113

1214
from aswfdocker import constants
1315

@@ -119,3 +121,43 @@ def get_group_from_image(image_type: constants.ImageType, image: str):
119121
if img == image:
120122
return group
121123
raise RuntimeError(f"Cannot find group for image {image}")
124+
125+
126+
def get_image_pull_count(docker_org, image):
127+
url = f"https://hub.docker.com/v2/repositories/{docker_org}/{image}"
128+
try:
129+
d = json.loads(urllib.request.urlopen(url).read())
130+
return d["pull_count"]
131+
except urllib.error.HTTPError:
132+
logger.debug("Failed to load data from URL %r", url)
133+
return 0
134+
135+
136+
def get_image_sizes(docker_org, image):
137+
sizes = {}
138+
url = f"https://hub.docker.com/v2/repositories/{docker_org}/{image}/tags/"
139+
try:
140+
d = json.loads(urllib.request.urlopen(url).read())
141+
except urllib.error.HTTPError:
142+
logger.debug("Failed to load data from URL %r", url)
143+
return sizes
144+
digests = set()
145+
for tag in d["results"]:
146+
digest = tag["images"][0]["digest"]
147+
if digest in digests:
148+
continue
149+
digests.add(digest)
150+
sizes[tag["name"]] = tag["full_size"]
151+
return sizes
152+
153+
154+
def iter_all_images():
155+
for org in (constants.TESTING_DOCKER_ORG, constants.PUBLISH_DOCKER_ORG):
156+
for image_type in (
157+
constants.ImageType.PACKAGE,
158+
constants.ImageType.CI_IMAGE,
159+
constants.ImageType.RT_IMAGE,
160+
):
161+
for _, images in constants.GROUPS[image_type].items():
162+
for image in images:
163+
yield org, image_type, image

0 commit comments

Comments
 (0)