-
-
Notifications
You must be signed in to change notification settings - Fork 126
Expand file tree
/
Copy pathgenerate_dockerfiles.py
More file actions
258 lines (211 loc) · 10.2 KB
/
generate_dockerfiles.py
File metadata and controls
258 lines (211 loc) · 10.2 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#!/usr/bin/env python3
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import argparse
import operator
import os
import re
import shutil
import requests
import requests_cache
import yaml
from jinja2 import Environment, FileSystemLoader
from adoptium_api import get_supported_versions
requests_cache.install_cache("adoptium_cache", expire_after=3600)
VERSION_OPERATORS = {
"==": operator.eq,
"!=": operator.ne,
">=": operator.ge,
"<=": operator.le,
">": operator.gt,
"<": operator.lt,
}
VERSION_CONDITION_RE = re.compile(r"^(==|!=|>=|<=|>|<)(\d+)$")
def resolve_architectures(default_architectures, overrides, version):
"""Resolve effective architectures for a given version by applying overrides.
All matching overrides are applied in order. Each override has a 'versions'
string (e.g. '==8', '<=11', '>17') and either:
- 'exclude': list of architectures to remove
- 'include': list of architectures to add
- 'architectures': full replacement list (overrides default entirely)
"""
if not overrides:
return default_architectures
result = list(default_architectures)
for override in overrides:
condition = override["versions"].strip()
match = VERSION_CONDITION_RE.match(condition)
if not match:
raise ValueError(f"Invalid version condition: '{condition}'")
op_str, target = match.groups()
if VERSION_OPERATORS[op_str](version, int(target)):
if "architectures" in override:
result = list(override["architectures"])
if "exclude" in override:
result = [a for a in result if a not in override["exclude"]]
if "include" in override:
result = result + [a for a in override["include"] if a not in result]
return result
def archHelper(arch, os_name):
if arch == "aarch64" and os_name == "ubuntu":
return "arm64"
elif arch == "ppc64le" and os_name == "ubuntu":
return "ppc64el"
elif arch == "arm":
return "armhf"
elif arch == "x64":
if os_name == "ubuntu":
return "amd64"
else:
return "x86_64"
else:
return arch
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Generate Dockerfiles for Eclipse Temurin images"
)
# Setup the Jinja2 environment
env = Environment(
loader=FileSystemLoader("docker_templates"), trim_blocks=False, lstrip_blocks=False
)
headers = {
"User-Agent": "Adoptium Dockerfile Updater",
}
# Flag for force removing old Dockerfiles
parser.add_argument("--force", action="store_true", help="Force remove old Dockerfiles")
args = parser.parse_args()
# Remove old Dockerfiles if --force is set
if args.force:
# Remove all top level dirs that are numbers
for dir in os.listdir():
if dir.isdigit():
print(f"Removing {dir}")
shutil.rmtree(dir)
# Load the YAML configuration
with open("config/temurin.yml", "r") as file:
config = yaml.safe_load(file)
# Global architecture overrides apply to all configurations
global_architecture_overrides = config.get("architecture_overrides", [])
# Fetch supported versions from the Adoptium API
supported_versions = get_supported_versions()
# Iterate through OS families and then configurations
for os_family, configurations in config["configurations"].items():
for configuration in configurations:
directory = configuration["directory"]
default_architectures = configuration["architectures"]
local_overrides = configuration.get("architecture_overrides", [])
architecture_overrides = global_architecture_overrides + local_overrides
os_name = configuration["os"]
base_image = configuration["image"]
deprecated = configuration.get("deprecated", None)
versions = configuration.get("versions", supported_versions)
# Define the path for the template based on OS
template_name = f"{os_name}.Dockerfile.j2"
template = env.get_template(template_name)
# Create output directories if they don't exist
for version in versions:
# if deprecated is set and version is greater than or equal to deprecated, skip
if deprecated and version >= deprecated:
continue
architectures = resolve_architectures(default_architectures, architecture_overrides, version)
print("Generating Dockerfiles for", base_image, "-", version)
for image_type in ["jdk", "jre"]:
output_directory = os.path.join(str(version), image_type, directory)
os.makedirs(output_directory, exist_ok=True)
# Fetch latest release for version from Adoptium API
url = f"https://api.adoptium.net/v3/assets/feature_releases/{version}/ga?page=0&image_type={image_type}&os={os_family}&page_size=1&vendor=eclipse"
response = requests.get(url, headers=headers)
# Handle 404 errors gracefully - skip this version if not available
if response.status_code == 404:
print(f"Version {version} not available for {image_type} on {os_family}, skipping...")
continue
response.raise_for_status()
data = response.json()
release = response.json()[0]
# Extract the version number from the release name
openjdk_version = release["release_name"]
# If version doesn't equal 8, get the more accurate version number
if version != 8:
openjdk_version = (
"jdk-" + release["version_data"]["openjdk_version"]
)
# if openjdk_version contains -LTS remove it
if "-LTS" in openjdk_version:
openjdk_version = openjdk_version.replace("-LTS", "")
# Generate the data for each architecture
arch_data = {}
for binary in release["binaries"]:
if (
binary["architecture"] in architectures
and binary["os"] == os_family
):
if os_family == "windows":
# Windows only has x64 binaries
copy_from = openjdk_version.replace(
"jdk", ""
) # jdk8u292-b10 -> 8u292-b10
if version != 8:
copy_from = copy_from.replace("-", "").replace(
"+", "_"
) # 11.0.11+9 -> 11.0.11_9
copy_from = f"{copy_from}-{image_type}-windowsservercore-{base_image.split(':')[1]}"
arch_data = {
"download_url": binary["installer"]["link"],
"checksum": binary["installer"]["checksum"],
"copy_from": copy_from,
}
else:
arch_data[archHelper(binary["architecture"], os_name)] = {
"download_url": binary["package"]["link"],
"checksum": binary["package"]["checksum"],
}
else:
continue
# If arch_data is empty, skip updating the dockerfile
if arch_data.__len__() == 0:
continue
# Sort arch_data by key
arch_data = dict(sorted(arch_data.items()))
# Generate Dockerfile for each architecture
rendered_dockerfile = template.render(
base_image=base_image,
image_type=image_type,
java_version=openjdk_version,
version=version,
arch_data=arch_data,
os_family=os_family,
os=os_name,
)
print("Writing Dockerfile to", output_directory)
# Save the rendered Dockerfile
with open(
os.path.join(output_directory, "Dockerfile"), "w"
) as out_file:
out_file.write(rendered_dockerfile)
if os_family != "windows":
# Entrypoint is currently only needed for CA certificate handling, which is not (yet)
# available on Windows
# Generate entrypoint.sh
template_entrypoint_file = "entrypoint.sh.j2"
template_entrypoint = env.get_template(template_entrypoint_file)
entrypoint = template_entrypoint.render(
image_type=image_type,
os=os_name,
version=version,
)
with open(
os.path.join(output_directory, "entrypoint.sh"), "w"
) as out_file:
out_file.write(entrypoint)
print("Dockerfiles generated successfully!")