forked from Ellipsis-Labs/solana-verifiable-build
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_image_whitelist.py
92 lines (77 loc) · 2.89 KB
/
update_image_whitelist.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import re
import re
import requests
import os
github_token = os.environ.get('GITHUB_TOKEN')
use_ghcr = os.environ.get('USE_GHCR', 'false').lower() == 'true'
headers = {'Authorization': f'Bearer {github_token}'}
def fetch_all_tags(repository):
"""Fetches all tags for a given Docker Hub repository across all pages."""
url = f"https://hub.docker.com/v2/repositories/{repository}/tags/"
all_tags = []
page = 1
page_size = 100 # Maximum page size Docker Hub allows
while True:
response = requests.get(url, params={"page": page, "page_size": page_size})
if response.status_code != 200:
raise Exception(f"Failed to get Docker images: {response.status_code} {response.text}")
response_data = response.json()
all_tags.extend(response_data.get("results", []))
print(f"Fetched page {page} with {len(response_data.get('results', []))} tags.")
if not response_data.get("next"):
break
page += 1
return all_tags
if use_ghcr:
response = requests.get(
"https://api.github.com/users/ngundotra/packages/container/solana/versions?per_page=100",
headers=headers
)
if response.status_code != 200:
raise Exception(f"Failed to get Docker images: {response.status_code} {response.text}")
results = response.json()
else:
repository = "solanafoundation/solana-verifiable-build"
results = fetch_all_tags(repository)
digest_map = {}
for result in results:
if use_ghcr:
# For GHCR, extract version from metadata
metadata = result.get("metadata", {})
container = metadata.get("container", {})
tags = container.get("tags", [])
for tag in tags:
match = re.match(r'(\d+)\.(\d+)\.(\d+)', tag)
if match:
major, minor, patch = map(int, match.groups())
digest_map[(major, minor, patch)] = result["name"] # "name" contains the digest for GHCR
break
else:
tag_name = result["name"]
if tag_name != "latest":
match = re.match(r'^(\d+)\.(\d+)\.(\d+)$', tag_name)
if match:
try:
major, minor, patch = map(int, match.groups())
digest_map[(major, minor, patch)] = result["digest"]
except ValueError as e:
print(f"Skipping invalid version tag '{tag_name}': {e}")
entries = []
for k, v in sorted(digest_map.items()):
entries.append(f' m.insert({k}, "{v}");')
mappings = "\n".join(entries)
code = f"""
/// THIS FILE IS AUTOGENERATED. DO NOT MODIFY
use lazy_static::lazy_static;
use std::collections::BTreeMap;
lazy_static! {{
pub static ref IMAGE_MAP: BTreeMap<(u32, u32, u32), &'static str> = {{
let mut m = BTreeMap::new();
{mappings}
m
}};
}}
"""
print(code)
with open("src/image_config.rs", "w") as f:
f.write(code.lstrip("\n"))