blob: f7485da6c4184a11395da0fc2dca99f69caf3e6d [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)
67
Yaguang Tangba6760e2024-12-13 12:59:40 +080068 if (
69 image_ref.domain() == "registry.atmosphere.dev"
70 or image_ref.domain() == "harbor.atmosphere.dev"
71 ):
Mohammed Naser1dfea6b2024-02-09 01:04:26 -050072 # Get token for docker.io
73 r = requests.get(
Yaguang Tangba6760e2024-12-13 12:59:40 +080074 "https://harbor.atmosphere.dev/service/token",
Mohammed Naser1dfea6b2024-02-09 01:04:26 -050075 timeout=5,
76 params={
77 "service": "harbor-registry",
78 "scope": f"repository:{image_ref.path()}:pull",
79 },
80 )
81 r.raise_for_status()
82 token = r.json()["token"]
83
84 digest = get_digest(image_ref, token=token)
vexxhost-botc66b9cd2024-06-20 14:19:16 +020085 elif image_ref.domain() == "quay.io":
Mohammed Naser8613c862023-04-24 17:26:51 -040086 r = requests.get(
87 f"https://quay.io/api/v1/repository/{image_ref.path()}/tag/",
Mohammed Naser21066a12024-01-02 11:14:53 -050088 timeout=5,
Mohammed Naser8613c862023-04-24 17:26:51 -040089 params={"specificTag": image_ref["tag"]},
90 )
91 r.raise_for_status()
92 digest = r.json()["tags"][0]["manifest_digest"]
vexxhost-botc66b9cd2024-06-20 14:19:16 +020093 elif image_ref.domain() == "docker.io":
Mohammed Naser49e66372023-07-10 14:57:00 -040094 # Get token for docker.io
95 r = requests.get(
96 "https://auth.docker.io/token",
Mohammed Naser21066a12024-01-02 11:14:53 -050097 timeout=5,
Mohammed Naser16baaab2023-07-10 15:07:11 -040098 params={
99 "service": "registry.docker.io",
100 "scope": f"repository:{image_ref.path()}:pull",
101 },
Mohammed Naser49e66372023-07-10 14:57:00 -0400102 )
103 r.raise_for_status()
104 token = r.json()["token"]
105
106 r = requests.get(
107 f"https://registry-1.docker.io/v2/{image_ref.path()}/manifests/{image_ref['tag']}",
Mohammed Naser21066a12024-01-02 11:14:53 -0500108 timeout=5,
Mohammed Naser16baaab2023-07-10 15:07:11 -0400109 headers={
110 "Accept": "application/vnd.docker.distribution.manifest.v2+json",
111 "Authorization": f"Bearer {token}",
112 },
Mohammed Naser49e66372023-07-10 14:57:00 -0400113 )
114 r.raise_for_status()
115 digest = r.headers["Docker-Content-Digest"]
vexxhost-botc66b9cd2024-06-20 14:19:16 +0200116 elif image_ref.domain() == "ghcr.io":
Mohammed Naser21066a12024-01-02 11:14:53 -0500117 # Get token for docker.io
118 r = requests.get(
119 "https://ghcr.io/token",
120 timeout=5,
121 params={
122 "service": "ghcr.io",
123 "scope": f"repository:{image_ref.path()}:pull",
124 },
125 )
126 r.raise_for_status()
127 token = r.json()["token"]
128
129 digest = get_digest(image_ref, token=token)
vexxhost-botc66b9cd2024-06-20 14:19:16 +0200130 else:
131 digest = get_digest(image_ref)
Mohammed Naser21066a12024-01-02 11:14:53 -0500132
133 return f"{image_ref.domain()}/{image_ref.path()}:{image_ref['tag']}@{digest}"
Mohammed Naser8613c862023-04-24 17:26:51 -0400134
135
136def main():
137 logging.register_options(CONF)
138 logging.setup(CONF, "atmosphere-bump-images")
139
140 parser = argparse.ArgumentParser("bump-images")
141 parser.add_argument(
142 "src", help="Path for default values file", type=argparse.FileType("r")
143 )
Mohammed Nasereb257cb2024-04-10 21:14:24 -0400144 parser.add_argument(
145 "dst", help="Path for output file", type=argparse.FileType("r+")
146 )
Mohammed Naser8613c862023-04-24 17:26:51 -0400147
148 args = parser.parse_args()
149
150 yaml = YAML(typ="rt")
151 data = yaml.load(args.src)
152
Mohammed Naser21066a12024-01-02 11:14:53 -0500153 for image in data["_atmosphere_images"]:
Oleksandr Kd4e5b232024-01-18 17:20:16 +0100154 if image in SKIP_IMAGE_LIST:
155 continue
Mohammed Naserfdd5cee2024-02-07 23:45:52 -0500156
Mohammed Naser12207172024-02-05 18:49:35 -0500157 image_src = data["_atmosphere_images"][image].replace(
Mohammed Naserd30f18d2024-04-17 01:20:43 -0400158 "{{ atmosphere_release }}", data["atmosphere_release"]
Mohammed Naser12207172024-02-05 18:49:35 -0500159 )
Mohammed Naser8613c862023-04-24 17:26:51 -0400160 pinned_image = get_pinned_image(image_src)
161
162 LOG.info("Pinning image %s from %s to %s", image, image_src, pinned_image)
ricolinb8ab0172023-06-01 15:41:02 +0800163 data["_atmosphere_images"][image] = pinned_image
Mohammed Naser8613c862023-04-24 17:26:51 -0400164
165 yaml.dump(data, args.dst)
166
167
168if __name__ == "__main__":
169 main()