Skip to content

Commit

Permalink
Added dockerstats command
Browse files Browse the repository at this point in the history
Signed-off-by: Aloys Baillet <[email protected]>
  • Loading branch information
aloysbaillet committed Aug 19, 2020
1 parent 3229093 commit ed1b190
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
25 changes: 25 additions & 0 deletions python/aswfdocker/cli/aswfdocker.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,31 @@ def images():
click.echo(f"{group}/{image_name}:{version}")


@click.option(
"--sizes", "-s", is_flag=True, help="Display Sizes",
)
@cli.command()
def dockerstats(sizes):
"""Lists all known ci images in this format: IMAGEGROUP/IMAGE:VERSION
"""
if sizes:
total_size = 0
for org, image_type, image in utils.iter_all_images():
image_name = utils.get_image_name(image_type, image)
for tag, size in utils.get_image_sizes(org, image_name).items():
click.echo(f"{org}/{image_name}:{tag} size={size}")
total_size += size
click.echo(f"Total size={total_size}")
else:
total_pull = 0
for org, image_type, image in utils.iter_all_images():
image_name = utils.get_image_name(image_type, image)
pull_count = utils.get_image_pull_count(org, image_name)
click.echo(f"{org}/{image_name} pull_count={pull_count}")
total_pull += pull_count
click.echo(f"Total pull_count={total_pull}")


@cli.command()
@click.option(
"--settings-path", "-p", default="~/.aswfdocker", help="User settings file path.",
Expand Down
42 changes: 42 additions & 0 deletions python/aswfdocker/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import subprocess
import datetime
import logging
import json
import urllib.request

from aswfdocker import constants

Expand Down Expand Up @@ -119,3 +121,43 @@ def get_group_from_image(image_type: constants.ImageType, image: str):
if img == image:
return group
raise RuntimeError(f"Cannot find group for image {image}")


def get_image_pull_count(docker_org, image):
url = f"https://hub.docker.com/v2/repositories/{docker_org}/{image}"
try:
d = json.loads(urllib.request.urlopen(url).read())
return d["pull_count"]
except urllib.error.HTTPError:
logger.debug("Failed to load data from URL %r", url)
return 0


def get_image_sizes(docker_org, image):
sizes = {}
url = f"https://hub.docker.com/v2/repositories/{docker_org}/{image}/tags/"
try:
d = json.loads(urllib.request.urlopen(url).read())
except urllib.error.HTTPError:
logger.debug("Failed to load data from URL %r", url)
return sizes
digests = set()
for tag in d["results"]:
digest = tag["images"][0]["digest"]
if digest in digests:
continue
digests.add(digest)
sizes[tag["name"]] = tag["full_size"]
return sizes


def iter_all_images():
for org in (constants.TESTING_DOCKER_ORG, constants.PUBLISH_DOCKER_ORG):
for image_type in (
constants.ImageType.PACKAGE,
constants.ImageType.CI_IMAGE,
constants.ImageType.RT_IMAGE,
):
for _, images in constants.GROUPS[image_type].items():
for image in images:
yield org, image_type, image

0 comments on commit ed1b190

Please sign in to comment.