blob: f711f84dc9fdf8191a7199562c92d663eab4b1fd [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):
Mohammed Nasere1ad8c82024-06-18 14:43:26 -040019 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}"
Mohammed Nasere1ad8c82024-06-18 14:43:26 -040024 else:
Jason Hall26476242024-02-19 12:56:23 -060025 r = requests.get(url, timeout=5)
Mohammed Nasere1ad8c82024-06-18 14:43:26 -040026 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"},
Mohammed Nasere1ad8c82024-06-18 14:43:26 -040034 )
35 r.raise_for_status()
36
37 headers["Authorization"] = f"Bearer {r.json()['token']}"
Mohammed Naser21066a12024-01-02 11:14:53 -050038
39 try:
40 headers["Accept"] = "application/vnd.docker.distribution.manifest.v2+json"
41
42 r = requests.get(
43 f"https://{image_ref.domain()}/v2/{image_ref.path()}/manifests/{image_ref['tag']}",
44 timeout=5,
45 headers=headers,
46 )
47 r.raise_for_status()
48 return r.headers["Docker-Content-Digest"]
49 except requests.exceptions.HTTPError:
50 headers["Accept"] = "application/vnd.oci.image.index.v1+json"
51
52 r = requests.get(
53 f"https://{image_ref.domain()}/v2/{image_ref.path()}/manifests/{image_ref['tag']}",
54 timeout=5,
55 headers=headers,
56 )
57 r.raise_for_status()
58 return r.headers["Docker-Content-Digest"]
59
60
Mohammed Naser8613c862023-04-24 17:26:51 -040061@functools.cache
62def get_pinned_image(image_src):
63 image_ref = reference.Reference.parse(image_src)
64
Mohammed Naser4ad7b512024-12-11 23:34:48 -050065 if (
66 image_ref.domain() == "registry.atmosphere.dev"
67 or image_ref.domain() == "harbor.atmosphere.dev"
68 ):
Mohammed Naser1dfea6b2024-02-09 01:04:26 -050069 # Get token for docker.io
70 r = requests.get(
Mohammed Naser4ad7b512024-12-11 23:34:48 -050071 "https://harbor.atmosphere.dev/service/token",
Mohammed Naser1dfea6b2024-02-09 01:04:26 -050072 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)
Mohammed Nasere1ad8c82024-06-18 14:43:26 -040082 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"]
Mohammed Nasere1ad8c82024-06-18 14:43:26 -040090 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"]
Mohammed Nasere1ad8c82024-06-18 14:43:26 -0400113 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)
Mohammed Nasere1ad8c82024-06-18 14:43:26 -0400127 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 Nasereb257cb2024-04-10 21:14:24 -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
Jason Hall26476242024-02-19 12:56:23 -0600154 image_src = (
155 data["_atmosphere_images"][image]
156 .replace("{{ atmosphere_release }}", data["atmosphere_release"])
157 .replace("{{ atmosphere_image_prefix }}", "")
Mohammed Naser12207172024-02-05 18:49:35 -0500158 )
Mohammed Naser8613c862023-04-24 17:26:51 -0400159 pinned_image = get_pinned_image(image_src)
160
161 LOG.info("Pinning image %s from %s to %s", image, image_src, pinned_image)
Jason Hall26476242024-02-19 12:56:23 -0600162 data["_atmosphere_images"][image] = "{{ atmosphere_image_prefix }}%s" % (
163 pinned_image,
164 )
Mohammed Naser8613c862023-04-24 17:26:51 -0400165
166 yaml.dump(data, args.dst)
167
168
169if __name__ == "__main__":
170 main()