blob: b5e1ada8e5072b10a1ed756f5ad416bc0148cf01 [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-botc66b9cd2024-06-20 14:19:16 +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-botc66b9cd2024-06-20 14:19:16 +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-botc66b9cd2024-06-20 14:19:16 +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-botc66b9cd2024-06-20 14:19:16 +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)
Mohammed Naser062d07b2025-01-19 00:04:34 -050067 if image_ref.domain() != "harbor.atmosphere.dev":
68 image_ref = reference.Reference.parse("harbor.atmosphere.dev/" + image_src)
Mohammed Naser8613c862023-04-24 17:26:51 -040069
Yaguang Tangba6760e2024-12-13 12:59:40 +080070 if (
71 image_ref.domain() == "registry.atmosphere.dev"
72 or image_ref.domain() == "harbor.atmosphere.dev"
73 ):
Mohammed Naser1dfea6b2024-02-09 01:04:26 -050074 # Get token for docker.io
75 r = requests.get(
Yaguang Tangba6760e2024-12-13 12:59:40 +080076 "https://harbor.atmosphere.dev/service/token",
Mohammed Naser1dfea6b2024-02-09 01:04:26 -050077 timeout=5,
78 params={
79 "service": "harbor-registry",
80 "scope": f"repository:{image_ref.path()}:pull",
81 },
82 )
83 r.raise_for_status()
84 token = r.json()["token"]
85
86 digest = get_digest(image_ref, token=token)
vexxhost-botc66b9cd2024-06-20 14:19:16 +020087 elif image_ref.domain() == "quay.io":
Mohammed Naser8613c862023-04-24 17:26:51 -040088 r = requests.get(
89 f"https://quay.io/api/v1/repository/{image_ref.path()}/tag/",
Mohammed Naser21066a12024-01-02 11:14:53 -050090 timeout=5,
Mohammed Naser8613c862023-04-24 17:26:51 -040091 params={"specificTag": image_ref["tag"]},
92 )
93 r.raise_for_status()
94 digest = r.json()["tags"][0]["manifest_digest"]
vexxhost-botc66b9cd2024-06-20 14:19:16 +020095 elif image_ref.domain() == "docker.io":
Mohammed Naser49e66372023-07-10 14:57:00 -040096 # Get token for docker.io
97 r = requests.get(
98 "https://auth.docker.io/token",
Mohammed Naser21066a12024-01-02 11:14:53 -050099 timeout=5,
Mohammed Naser16baaab2023-07-10 15:07:11 -0400100 params={
101 "service": "registry.docker.io",
102 "scope": f"repository:{image_ref.path()}:pull",
103 },
Mohammed Naser49e66372023-07-10 14:57:00 -0400104 )
105 r.raise_for_status()
106 token = r.json()["token"]
107
108 r = requests.get(
109 f"https://registry-1.docker.io/v2/{image_ref.path()}/manifests/{image_ref['tag']}",
Mohammed Naser21066a12024-01-02 11:14:53 -0500110 timeout=5,
Mohammed Naser16baaab2023-07-10 15:07:11 -0400111 headers={
112 "Accept": "application/vnd.docker.distribution.manifest.v2+json",
113 "Authorization": f"Bearer {token}",
114 },
Mohammed Naser49e66372023-07-10 14:57:00 -0400115 )
116 r.raise_for_status()
117 digest = r.headers["Docker-Content-Digest"]
vexxhost-botc66b9cd2024-06-20 14:19:16 +0200118 elif image_ref.domain() == "ghcr.io":
Mohammed Naser21066a12024-01-02 11:14:53 -0500119 # Get token for docker.io
120 r = requests.get(
121 "https://ghcr.io/token",
122 timeout=5,
123 params={
124 "service": "ghcr.io",
125 "scope": f"repository:{image_ref.path()}:pull",
126 },
127 )
128 r.raise_for_status()
129 token = r.json()["token"]
130
131 digest = get_digest(image_ref, token=token)
vexxhost-botc66b9cd2024-06-20 14:19:16 +0200132 else:
133 digest = get_digest(image_ref)
Mohammed Naser21066a12024-01-02 11:14:53 -0500134
Mohammed Naser062d07b2025-01-19 00:04:34 -0500135 original_ref = reference.Reference.parse(image_src)
136 return (
137 f"{original_ref.domain()}/{original_ref.path()}:{original_ref['tag']}@{digest}"
138 )
Mohammed Naser8613c862023-04-24 17:26:51 -0400139
140
141def main():
142 logging.register_options(CONF)
143 logging.setup(CONF, "atmosphere-bump-images")
144
145 parser = argparse.ArgumentParser("bump-images")
146 parser.add_argument(
147 "src", help="Path for default values file", type=argparse.FileType("r")
148 )
Mohammed Nasereb257cb2024-04-10 21:14:24 -0400149 parser.add_argument(
150 "dst", help="Path for output file", type=argparse.FileType("r+")
151 )
Mohammed Naser8613c862023-04-24 17:26:51 -0400152
153 args = parser.parse_args()
154
155 yaml = YAML(typ="rt")
156 data = yaml.load(args.src)
157
Mohammed Naser21066a12024-01-02 11:14:53 -0500158 for image in data["_atmosphere_images"]:
Oleksandr Kd4e5b232024-01-18 17:20:16 +0100159 if image in SKIP_IMAGE_LIST:
160 continue
Mohammed Naserfdd5cee2024-02-07 23:45:52 -0500161
Mohammed Naser12207172024-02-05 18:49:35 -0500162 image_src = data["_atmosphere_images"][image].replace(
Mohammed Naserd30f18d2024-04-17 01:20:43 -0400163 "{{ atmosphere_release }}", data["atmosphere_release"]
Mohammed Naser12207172024-02-05 18:49:35 -0500164 )
Mohammed Naser062d07b2025-01-19 00:04:34 -0500165 pinned_image = get_pinned_image(image_src).replace(
166 "harbor.atmosphere.dev", "registry.atmosphere.dev"
167 )
Mohammed Naser8613c862023-04-24 17:26:51 -0400168
169 LOG.info("Pinning image %s from %s to %s", image, image_src, pinned_image)
ricolinb8ab0172023-06-01 15:41:02 +0800170 data["_atmosphere_images"][image] = pinned_image
Mohammed Naser8613c862023-04-24 17:26:51 -0400171
172 yaml.dump(data, args.dst)
173
174
175if __name__ == "__main__":
176 main()