|
| 1 | +import functools |
| 2 | +import logging |
| 3 | +import re |
| 4 | +import requests |
| 5 | + |
| 6 | +log = logging.getLogger(__name__) |
| 7 | + |
| 8 | +# Our container images use a certain base image and flavor by default; those |
| 9 | +# values are reflected below. If different values are used, they are appended |
| 10 | +# to the image name. |
| 11 | +DEFAULT_CONTAINER_BASE = 'centos:9' |
| 12 | +DEFAULT_CONTAINER_FLAVOR = 'default' |
| 13 | +DEFAULT_CONTAINER_IMAGE='quay.ceph.io/ceph-ci/ceph:{sha1}' |
| 14 | +CONTAINER_REGEXP = re.compile( |
| 15 | + r"((?P<domain>[a-zA-Z0-9._-]+)/)?((?P<org>[a-zA-Z0-9_-]+)/)?((?P<image>[a-zA-Z0-9_-]+))?(:(?P<tag>[a-zA-Z0-9._-]+))?" |
| 16 | +) |
| 17 | + |
| 18 | + |
| 19 | +def resolve_container_image(image: str): |
| 20 | + """ |
| 21 | + Given an image locator that is potentially incomplete, construct a qualified version. |
| 22 | +
|
| 23 | + ':tag' -> 'quay.ceph.io/ceph-ci/ceph:tag' |
| 24 | + 'image:tag' -> 'quay.ceph.io/ceph-ci/image:tag' |
| 25 | + 'org/image:tag' -> 'quay.ceph.io/org/image:tag' |
| 26 | + 'example.com/org/image:tag' -> 'example.com/org/image:tag' |
| 27 | + """ |
| 28 | + try: |
| 29 | + (image_long, tag) = image.split(':') |
| 30 | + except ValueError: |
| 31 | + raise ValueError(f"Container image spec missing tag: {image}") from None |
| 32 | + domain = 'quay.ceph.io' |
| 33 | + org = 'ceph-ci' |
| 34 | + image = 'ceph' |
| 35 | + image_split = image_long.split('/') |
| 36 | + assert len(image_split) <= 3 |
| 37 | + match len(image_split): |
| 38 | + case 3: |
| 39 | + (domain, org, image) = image_split |
| 40 | + case 2: |
| 41 | + (org, image) = image_split |
| 42 | + case _: |
| 43 | + if image_split[0]: |
| 44 | + image = image_split[0] |
| 45 | + return f"{domain}/{org}/{image}:{tag}" |
| 46 | + |
| 47 | + |
| 48 | +@functools.lru_cache() |
| 49 | +def container_image_exists(image: str): |
| 50 | + """ |
| 51 | + Use the Quay API to check for the existence of a container image. |
| 52 | + Only tested with Quay registries. |
| 53 | + """ |
| 54 | + match = re.match(CONTAINER_REGEXP, image) |
| 55 | + assert match |
| 56 | + obj = match.groupdict() |
| 57 | + url = f"https://{obj['domain']}/api/v1/repository/{obj['org']}/{obj['image']}/tag?filter_tag_name=eq:{obj['tag']}" |
| 58 | + log.info(f"Checking for container existence at: {url}") |
| 59 | + resp = requests.get(url) |
| 60 | + return resp.ok and len(resp.json().get('tags')) >= 1 |
| 61 | + |
| 62 | + |
| 63 | +def container_image_for_hash(hash: str, flavor='default', base_image='centos:9'): |
| 64 | + """ |
| 65 | + Given a sha1 and optionally a base image and flavor, attempt to return a container image locator. |
| 66 | + """ |
| 67 | + tag = hash |
| 68 | + if base_image != DEFAULT_CONTAINER_BASE: |
| 69 | + tag = f"{tag}-{base_image.replace(':', '-')}" |
| 70 | + if flavor != DEFAULT_CONTAINER_FLAVOR: |
| 71 | + tag = f"{tag}-{flavor}" |
| 72 | + image_spec = resolve_container_image(f":{tag}") |
| 73 | + if container_image_exists(image_spec): |
| 74 | + return image_spec |
| 75 | + else: |
| 76 | + log.error(f"Container image not found for hash '{hash}'") |
0 commit comments