Last active
July 16, 2023 00:23
-
-
Save ebuildy/ee9926ac0b1b290ee7ed7b75eafe856f to your computer and use it in GitHub Desktop.
List gitlab container registry size, per project, via gitlab API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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