Skip to content

Instantly share code, notes, and snippets.

@ebuildy

ebuildy/index.py

Last active Oct 9, 2020
Embed
What would you like to do?
List gitlab container registry size, per project, via gitlab API
import gitlab
import urllib3
import humanfriendly
import timeago, datetime
# 2020-04-24T12:04:26.475+00:00
date_now = datetime.datetime.now()
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
gl = gitlab.Gitlab('https://git.XXXXXXX', private_token='XXXX', ssl_verify=False)
projects = gl.projects.list(per_page=20, simple=True, pagination="keyset", order_by="id", as_list=False)
for project in projects:
repositories = project.repositories.list()
if len(repositories) > 0:
print(f"# {project.name_with_namespace}")
total_images = 0
total_per_project = 0
for repository in repositories:
print(f"## {repository.path}")
tags = repository.tags.list()
for tag in tags:
tag_detail = repository.tags.get(tag.name)
date_time_obj = datetime.datetime.strptime(tag_detail.created_at, '%Y-%m-%dT%H:%M:%S.%f%z').replace(tzinfo=None)
print(f"### {tag.name} | {humanfriendly.format_size(tag_detail.total_size)} | {timeago.format(date_time_obj, date_now)} ({tag_detail.created_at})")
total_per_project += tag_detail.total_size
total_images += 1
if total_per_project > 0:
print(f" * Total = {total_images} image(s) for {humanfriendly.format_size(total_per_project)} *")
print()
pip install humanfriendly
pip install python-gitlab
pip install timeago
python index.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment