blob: 1bf4112d0befb9ab1c35c77f86bedd278e263319 [file] [log] [blame]
Mohammed Naser8613c862023-04-24 17:26:51 -04001#!/usr/bin/env python3
2
3import argparse
4import functools
5
Mohammed Naser12207172024-02-05 18:49:35 -05006import requests
Mohammed Naser8613c862023-04-24 17:26:51 -04007from docker_image import reference
8from oslo_config import cfg
9from oslo_log import log as logging
10from ruyaml import YAML
Mohammed Naser8613c862023-04-24 17:26:51 -040011
12LOG = logging.getLogger(__name__)
13CONF = cfg.CONF
14
Oleksandr Kd4e5b232024-01-18 17:20:16 +010015SKIP_IMAGE_LIST = ["secretgen_controller"]
Mohammed Naser8613c862023-04-24 17:26:51 -040016
Mohammed Naser891a2902024-01-23 09:47:47 -050017
Mohammed Naser21066a12024-01-02 11:14:53 -050018def get_digest(image_ref, token=None):
vexxhost-bota026e1a2024-06-19 00:37:21 +020019 url = f"https://{image_ref.domain()}/v2/{image_ref.path()}/manifests/{image_ref['tag']}"
20
Mohammed Naser21066a12024-01-02 11:14:53 -050021 headers = {}
22 if token:
23 headers["Authorization"] = f"Bearer {token}"
vexxhost-bota026e1a2024-06-19 00:37:21 +020024 else:
25 r = requests.get(url, timeout=5, verify=False)
26 auth_header = r.headers.get("Www-Authenticate")
27 if auth_header:
28 realm = auth_header.split(",")[0].split("=")[1].strip('"')
29
30 r = requests.get(
31 realm,
32 timeout=5,
33 params={"scope": f"repository:{image_ref.path()}:pull"},
34 verify=False,
35 )
36 r.raise_for_status()
37
38 headers["Authorization"] = f"Bearer {r.json()['token']}"
Mohammed Naser21066a12024-01-02 11:14:53 -050039
40 try:
41 headers["Accept"] = "application/vnd.docker.distribution.manifest.v2+json"
42
43 r = requests.get(
44 f"https://{image_ref.domain()}/v2/{image_ref.path()}/manifests/{image_ref['tag']}",
45 timeout=5,
46 headers=headers,
vexxhost-bota026e1a2024-06-19 00:37:21 +020047 verify=False,
Mohammed Naser21066a12024-01-02 11:14:53 -050048 )
49 r.raise_for_status()
50 return r.headers["Docker-Content-Digest"]
51 except requests.exceptions.HTTPError:
52 headers["Accept"] = "application/vnd.oci.image.index.v1+json"
53
54 r = requests.get(
55 f"https://{image_ref.domain()}/v2/{image_ref.path()}/manifests/{image_ref['tag']}",
56 timeout=5,
57 headers=headers,
vexxhost-bota026e1a2024-06-19 00:37:21 +020058 verify=False,
Mohammed Naser21066a12024-01-02 11:14:53 -050059 )
60 r.raise_for_status()
61 return r.headers["Docker-Content-Digest"]
62
63
Mohammed Naser8613c862023-04-24 17:26:51 -040064@functools.cache
65def get_pinned_image(image_src):
66 image_ref = reference.Reference.parse(image_src)
67
Mohammed Naser1dfea6b2024-02-09 01:04:26 -050068 if image_ref.domain() == "registry.atmosphere.dev":
69 # Get token for docker.io
70 r = requests.get(
71 "https://registry.atmosphere.dev/service/token",
72 timeout=5,
73 params={
74 "service": "harbor-registry",
75 "scope": f"repository:{image_ref.path()}:pull",
76 },
77 )
78 r.raise_for_status()
79 token = r.json()["token"]
80
81 digest = get_digest(image_ref, token=token)
vexxhost-bota026e1a2024-06-19 00:37:21 +020082 elif image_ref.domain() == "quay.io":
Mohammed Naser8613c862023-04-24 17:26:51 -040083 r = requests.get(
84 f"https://quay.io/api/v1/repository/{image_ref.path()}/tag/",
Mohammed Naser21066a12024-01-02 11:14:53 -050085 timeout=5,
Mohammed Naser8613c862023-04-24 17:26:51 -040086 params={"specificTag": image_ref["tag"]},
87 )
88 r.raise_for_status()
89 digest = r.json()["tags"][0]["manifest_digest"]
vexxhost-bota026e1a2024-06-19 00:37:21 +020090 elif image_ref.domain() == "docker.io":
Mohammed Naser49e66372023-07-10 14:57:00 -040091 # Get token for docker.io
92 r = requests.get(
93 "https://auth.docker.io/token",
Mohammed Naser21066a12024-01-02 11:14:53 -050094 timeout=5,
Mohammed Naser16baaab2023-07-10 15:07:11 -040095 params={
96 "service": "registry.docker.io",
97 "scope": f"repository:{image_ref.path()}:pull",
98 },
Mohammed Naser49e66372023-07-10 14:57:00 -040099 )
100 r.raise_for_status()
101 token = r.json()["token"]
102
103 r = requests.get(
104 f"https://registry-1.docker.io/v2/{image_ref.path()}/manifests/{image_ref['tag']}",
Mohammed Naser21066a12024-01-02 11:14:53 -0500105 timeout=5,
Mohammed Naser16baaab2023-07-10 15:07:11 -0400106 headers={
107 "Accept": "application/vnd.docker.distribution.manifest.v2+json",
108 "Authorization": f"Bearer {token}",
109 },
Mohammed Naser49e66372023-07-10 14:57:00 -0400110 )
111 r.raise_for_status()
112 digest = r.headers["Docker-Content-Digest"]
vexxhost-bota026e1a2024-06-19 00:37:21 +0200113 elif image_ref.domain() == "ghcr.io":
Mohammed Naser21066a12024-01-02 11:14:53 -0500114 # Get token for docker.io
115 r = requests.get(
116 "https://ghcr.io/token",
117 timeout=5,
118 params={
119 "service": "ghcr.io",
120 "scope": f"repository:{image_ref.path()}:pull",
121 },
122 )
123 r.raise_for_status()
124 token = r.json()["token"]
125
126 digest = get_digest(image_ref, token=token)
vexxhost-bota026e1a2024-06-19 00:37:21 +0200127 else:
128 digest = get_digest(image_ref)
Mohammed Naser21066a12024-01-02 11:14:53 -0500129
130 return f"{image_ref.domain()}/{image_ref.path()}:{image_ref['tag']}@{digest}"
Mohammed Naser8613c862023-04-24 17:26:51 -0400131
132
133def main():
134 logging.register_options(CONF)
135 logging.setup(CONF, "atmosphere-bump-images")
136
137 parser = argparse.ArgumentParser("bump-images")
138 parser.add_argument(
139 "src", help="Path for default values file", type=argparse.FileType("r")
140 )
Mohammed Naser841686a2024-04-11 14:31:47 -0400141 parser.add_argument(
142 "dst", help="Path for output file", type=argparse.FileType("r+")
143 )
Mohammed Naser8613c862023-04-24 17:26:51 -0400144
145 args = parser.parse_args()
146
147 yaml = YAML(typ="rt")
148 data = yaml.load(args.src)
149
Mohammed Naser21066a12024-01-02 11:14:53 -0500150 for image in data["_atmosphere_images"]:
Oleksandr Kd4e5b232024-01-18 17:20:16 +0100151 if image in SKIP_IMAGE_LIST:
152 continue
Mohammed Naserfdd5cee2024-02-07 23:45:52 -0500153
Mohammed Naser12207172024-02-05 18:49:35 -0500154 image_src = data["_atmosphere_images"][image].replace(
Mohammed Naser19c6c8a2024-04-18 01:47:10 -0400155 "{{ atmosphere_release }}", data["atmosphere_release"]
Mohammed Naser12207172024-02-05 18:49:35 -0500156 )
Mohammed Naser8613c862023-04-24 17:26:51 -0400157 pinned_image = get_pinned_image(image_src)
158
159 LOG.info("Pinning image %s from %s to %s", image, image_src, pinned_image)
ricolinb8ab0172023-06-01 15:41:02 +0800160 data["_atmosphere_images"][image] = pinned_image
Mohammed Naser8613c862023-04-24 17:26:51 -0400161
162 yaml.dump(data, args.dst)
163
164
165if __name__ == "__main__":
166 main()