2021-02-10 16:42:30 +00:00
|
|
|
import json
|
2021-09-09 13:20:44 +00:00
|
|
|
import pathlib
|
2021-02-10 16:42:30 +00:00
|
|
|
import urllib.request
|
|
|
|
import urllib.parse
|
|
|
|
|
|
|
|
from typing import Dict, List
|
|
|
|
|
|
|
|
|
|
|
|
def get_registry_uri(namespace: str,
|
|
|
|
gitlab_uri: str = "https://gitlab.com") -> str:
|
|
|
|
"""
|
|
|
|
Construct a v4 API URI pointing the namespaced project's image registry.
|
|
|
|
|
|
|
|
:param namespace: GitLab project namespace, e.g. "libvirt/libvirt"
|
|
|
|
:param gitlab_uri: GitLab base URI, can be a private deployment
|
|
|
|
:param api_version: GitLab REST API version number
|
|
|
|
:return: URI pointing to a namespaced project's image registry
|
|
|
|
"""
|
|
|
|
|
|
|
|
# this converts something like "libvirt/libvirt" to "libvirt%2Flibvirt"
|
|
|
|
namespace_urlenc = urllib.parse.quote_plus(namespace)
|
|
|
|
|
|
|
|
project_uri = f"{gitlab_uri}/api/v4/projects/{namespace_urlenc}"
|
|
|
|
|
|
|
|
uri = project_uri + "/registry/repositories"
|
|
|
|
return uri
|
|
|
|
|
|
|
|
|
|
|
|
def get_registry_images(uri: str) -> List[Dict]:
|
|
|
|
"""
|
|
|
|
List all container images that are currently available in the given GitLab
|
|
|
|
project.
|
|
|
|
|
|
|
|
:param uri: URI pointing to a GitLab instance's image registry
|
|
|
|
:return: list of container image names
|
|
|
|
"""
|
|
|
|
|
|
|
|
r = urllib.request.urlopen(uri + "?per_page=100")
|
|
|
|
|
|
|
|
# read the HTTP response and load the JSON part of it
|
|
|
|
return json.loads(r.read().decode())
|
2021-03-15 14:42:13 +00:00
|
|
|
|
|
|
|
|
2021-09-09 13:20:44 +00:00
|
|
|
def get_dockerfiles(base_dir) -> List:
|
2021-03-15 14:42:13 +00:00
|
|
|
"""
|
2021-09-09 13:20:44 +00:00
|
|
|
List all container dockerfiles in the local directory.
|
2021-03-15 14:42:13 +00:00
|
|
|
|
2021-09-09 13:20:44 +00:00
|
|
|
:return: list of dockerfile names
|
2021-03-15 14:42:13 +00:00
|
|
|
"""
|
|
|
|
|
2021-09-09 13:20:44 +00:00
|
|
|
dkrs = []
|
|
|
|
d = pathlib.Path(base_dir, "containers")
|
|
|
|
for f in d.iterdir():
|
|
|
|
if f.suffix == ".Dockerfile":
|
|
|
|
dkrs.append(f.stem)
|
|
|
|
return dkrs
|
2021-03-15 14:42:13 +00:00
|
|
|
|
|
|
|
|
2021-09-09 13:20:44 +00:00
|
|
|
def get_registry_stale_images(registry_uri: str, base_dir: str) -> Dict[str, int]:
|
2021-03-15 14:42:13 +00:00
|
|
|
"""
|
|
|
|
Check the GitLab image registry for images that we no longer support and
|
|
|
|
which should be deleted.
|
|
|
|
|
|
|
|
:param uri: URI pointing to a GitLab instance's image registry
|
2021-09-09 13:20:44 +00:00
|
|
|
:param base_dir: local repository base directory
|
2021-03-15 14:42:13 +00:00
|
|
|
:return: dictionary formatted as: {<gitlab_image_name>: <gitlab_image_id>}
|
|
|
|
"""
|
|
|
|
|
2021-09-09 13:20:44 +00:00
|
|
|
dockerfiles = get_dockerfiles(base_dir)
|
2021-03-15 14:42:13 +00:00
|
|
|
images = get_registry_images(registry_uri)
|
2021-09-10 13:35:49 +00:00
|
|
|
name_prefix = "ci-"
|
2021-03-15 14:42:13 +00:00
|
|
|
|
|
|
|
stale_images = {}
|
|
|
|
for img in images:
|
2021-09-10 13:35:49 +00:00
|
|
|
if img["name"][len(name_prefix):] not in dockerfiles:
|
2021-03-15 14:42:13 +00:00
|
|
|
stale_images[img["name"]] = img["id"]
|
|
|
|
|
|
|
|
return stale_images
|