-
Notifications
You must be signed in to change notification settings - Fork 1
/
get-docker-registry-tags.py
executable file
·69 lines (53 loc) · 2.4 KB
/
get-docker-registry-tags.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env python3
# License: MIT
# Author: github.com/danielhoherd
# Purpose: Return the latest image:tag for the given image, or optionally print a list of recent tags for the image.
from distutils.version import LooseVersion
from sys import stderr
import requests
import typer
from packaging import version
def main(image: str, list_tags: bool = typer.Option(False, help="Print a list of recent tags")):
"""Return the latest image:tag for the given image, or optionally print a
list of recent tags for the image.
You need to provide the bare image name that would be used in the
docker pull command.
"""
if ":" in image:
try:
image, tag = image.split(":")
print(f'Stripped tag "{tag}" from input, continuing with "{image}"', file=stderr)
except ValueError as e:
print(f'Input error: "{image}" does not look like a docker image name', file=stderr)
raise SystemExit(1) from e
tags = get_tags_for_image(image)
if list_tags:
for tag in sorted(tags, key=LooseVersion):
print(tag)
else:
newest = max(tags, key=LooseVersion)
print(f"{image}:{newest}")
def get_tags_for_image(image):
"""Return a list of semver tags for the given image."""
return get_tags_for_image_quay_io(image) if image.startswith("quay.io") else get_tags_for_image_docker_io(image)
def get_tags_for_image_docker_io(image):
"""Return a list of tags for docker hub image."""
data = requests.get(f"https://index.docker.io/v1/repositories/{image.removeprefix('docker.io/')}/tags", timeout=30).json()
if isinstance(data, str):
raise SystemExit(f"ERROR: {data}")
tags = {x["name"] for x in data if version.parse(x["name"]).release}
if not tags:
raise SystemExit(f"ERROR: No semver tags for image '{image}'")
return tags
def get_tags_for_image_quay_io(image):
"""Return a list of tags for quay.io image."""
resp = requests.get(f"https://quay.io/api/v1/repository/{image.removeprefix('quay.io/')}/tag?limit=100", timeout=30)
data = resp.json()
if not (200 <= resp.status_code < 300):
raise SystemExit("ERROR: {status} {detail}".format(**data))
tags = {x["name"] for x in data["tags"] if version.parse(x["name"]).release}
if not tags:
raise SystemExit(f"ERROR: No semver tags for image '{image}'")
return tags
if __name__ == "__main__":
typer.run(main)