-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathmatrix.py
More file actions
446 lines (380 loc) · 15.2 KB
/
matrix.py
File metadata and controls
446 lines (380 loc) · 15.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
import json
import os
import yaml
import subprocess
import urllib.request
from collections import OrderedDict
from functools import cache
from packaging.version import Version, parse
from util import matches_constraints, list_rsync_dir, format_image_name
try:
from yaml import CLoader as Loader
except ImportError:
from yaml import Loader
with open("config.yaml", "r") as f:
CONFIG = yaml.load(f, Loader)
image_name_format = CONFIG["imageNameFormat"]
@cache
def build_architectures():
architecture_env = os.getenv("KERNEL_ARCHITECTURES", "")
if len(architecture_env) > 0:
return [arch.strip() for arch in architecture_env.split(",")]
architectures = CONFIG["architectures"] # type: list[str]
return architectures
@cache
def get_current_kernel_releases() -> dict[str, any]:
with urllib.request.urlopen("https://www.kernel.org/releases.json") as response:
releases = json.load(response)
return releases
@cache
def get_all_kernel_releases() -> list[str]:
releases = []
for maybe_release_major in list_rsync_dir(
"rsync://rsync.kernel.org/pub/linux/kernel/"
):
if not (
maybe_release_major.startswith("v") and maybe_release_major.endswith("x")
):
continue
for maybe_release_file in list_rsync_dir(
"rsync://rsync.kernel.org/pub/linux/kernel/%s/" % maybe_release_major
):
if not (
maybe_release_file.startswith("linux-")
and maybe_release_file.endswith(".tar.xz")
):
continue
kernel_version = maybe_release_file.replace("linux-", "").replace(
".tar.xz", ""
)
if "-" in kernel_version:
continue
releases.append(kernel_version)
return releases
@cache
def get_all_firmware_releases() -> list[str]:
snapshots = []
for maybe_release_snapshot in list_rsync_dir(
"rsync://rsync.kernel.org/pub/linux/kernel/firmware/"
):
if not (
maybe_release_snapshot.startswith("linux-firmware-") and maybe_release_snapshot.endswith(".xz")
):
continue
firmware_snapshot_version = maybe_release_snapshot.replace("linux-firmware-", "").replace(
".tar.xz", ""
)
snapshots.append(firmware_snapshot_version)
snapshots.sort()
snapshots.reverse()
return snapshots
def merge_matrix(matrix_list: list[list[dict[str, any]]]) -> list[dict[str, any]]:
all_builds = OrderedDict() # type: dict[str, dict[str, any]]
for builds in matrix_list:
for item in builds:
key = "%s::%s" % (item["version"], item["flavor"])
if key not in all_builds:
all_builds[key] = item
else:
for tag in item["tags"]:
if tag not in all_builds[key]["tags"]:
all_builds[key]["tags"].append(tag)
for arch in item["architectures"]:
if arch not in all_builds[key]["architectures"]:
all_builds[key]["architectures"].append(arch)
builds = list(all_builds.values())
builds.sort(key=lambda build: parse(build["version"]))
return builds
def extract_base_images(builds: list[dict[str, any]]):
images = []
for build in builds:
if "produces" not in build:
raise Exception("build did not contain a produces key")
for produce in build["produces"]:
parts = produce.split(":")
image = parts[0]
if image not in images:
images.append(image)
return images
def find_existing_tags(images: list[str]) -> dict[str, list[str]]:
existing = {}
for image in images:
# ignore return code, we just want stdout
result = subprocess.run(
["crane", "ls", "-O", image],
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL,
)
tags = result.stdout.decode("utf-8").splitlines(keepends=False)
existing[image] = tags
return existing
def validate_produce_conflicts(builds: list[dict[str, any]]):
produce_check = {}
for build in builds:
for produce in build["produces"]:
if produce in produce_check:
raise Exception("ERROR: %s was produced more than once" % produce)
else:
produce_check[produce] = produce
def filter_new_builds(builds: list[dict[str, any]]) -> list[dict[str, any]]:
images = extract_base_images(builds)
existing = find_existing_tags(images)
should_builds = []
for build in builds:
should_build = False
for produce in build["produces"]:
parts = produce.split(":")
image = parts[0]
tag = parts[1]
if image not in existing:
should_build = True
elif tag not in existing[image]:
should_build = True
if should_build:
should_builds.append(build)
return should_builds
def limit_gh_builds(builds: list[dict[str, any]]) -> list[dict[str, any]]:
builds.sort(key=lambda build: parse(build["version"]))
if len(builds) > 250:
builds = builds[-250:]
return builds
def is_release_current(version: str) -> bool:
current_kernel_releases = get_current_kernel_releases()
latest_stable = current_kernel_releases["latest_stable"]["version"]
is_current = False
if latest_stable is not None and version == latest_stable:
is_current = True
return is_current
for release in current_kernel_releases["releases"]:
if not release["moniker"] in ["stable", "longterm"]:
continue
if release["version"] == version:
is_current = True
break
return is_current
def filter_matrix(
builds: list[dict[str, any]], constraint: dict[str, any]
) -> list[dict[str, any]]:
output_builds = []
for build in builds:
version = build["version"]
version_info = parse(version)
flavor = build["flavor"]
is_current_release = is_release_current(version_info.base_version)
should_build = matches_constraints(
version_info, flavor, constraint, is_current_release=is_current_release
)
if should_build:
output_builds.append(build)
return output_builds
def filter_config_versions(builds: list[dict[str, any]]) -> list[dict[str, any]]:
output_builds = []
for build in builds:
version = build["version"]
version_info = parse(version)
flavor = build["flavor"]
is_current_release = is_release_current(version_info.base_version)
should_build = False
for constraint in CONFIG["versions"]:
if matches_constraints(
version_info, flavor, constraint, is_current_release=is_current_release
):
should_build = True
if should_build:
output_builds.append(build)
return output_builds
def generate_matrix(tags: dict[str, str]) -> list[dict[str, any]]:
unique_versions = list(set(tags.values()))
unique_versions.sort(key=Version)
version_builds = []
kernel_cdn = "https://cdn.kernel.org/pub/linux/kernel"
# TODO later on we could get cute and let the config drive
# which firmware snapshot to use - but as far as the official firmware goes
# latest should be fine/preferred.
# https://www.kernel.org/doc/html/latest/driver-api/firmware/firmware-usage-guidelines.html
all_firmware_releases = get_all_firmware_releases()
latest_firmware = all_firmware_releases[0]
firmware_url = "%s/firmware/linux-firmware-%s.tar.xz" % (
kernel_cdn,
latest_firmware,
)
firmware_sig_url = "%s/firmware/linux-firmware-%s.tar.sign" % (
kernel_cdn,
latest_firmware,
)
for version in unique_versions:
version_tags = []
for tag in tags:
tag_version = tags[tag]
if tag_version == version:
version_tags.append(tag)
version_info = parse(version)
version_for_url = version
if version_info.micro == 0:
version_for_url = "%s.%s" % (version_info.major, version_info.minor)
src_url = "%s/v%s.x/linux-%s.tar.xz" % (
kernel_cdn,
version_info.major,
version_for_url,
)
for flavor_info in CONFIG["flavors"]:
flavor = flavor_info["name"]
if "constraints" in flavor_info and not matches_constraints(
version_info, flavor, flavor_info["constraints"]
):
continue
if "local_tags" in flavor_info:
for local_tag in flavor_info["local_tags"]:
produces = []
local_version_tags = []
for tag in version_tags:
local_append = tag+"-"+local_tag
local_version_tags.append(local_append)
kernel_output = format_image_name(
image_name_format, flavor, version_info, "[flavor]-kernel", local_append
)
kernel_sdk_output = format_image_name(
image_name_format, flavor, version_info, "[flavor]-kernel-sdk", local_append
)
produces.append(kernel_output)
produces.append(kernel_sdk_output)
version_builds.append(
{
"version": version+"+"+local_tag,
"firmware_url": firmware_url,
"firmware_sig_url": firmware_sig_url,
"tags": local_version_tags,
"source": src_url,
"flavor": flavor,
"architectures": build_architectures(),
"produces": produces,
}
)
else:
produces = []
for tag in version_tags:
kernel_output = format_image_name(
image_name_format, flavor, version_info, "[flavor]-kernel", tag
)
kernel_sdk_output = format_image_name(
image_name_format, flavor, version_info, "[flavor]-kernel-sdk", tag
)
produces.append(kernel_output)
produces.append(kernel_sdk_output)
version_builds.append(
{
"version": version,
"firmware_url": firmware_url,
"firmware_sig_url": firmware_sig_url,
"tags": version_tags,
"source": src_url,
"flavor": flavor,
"architectures": build_architectures(),
"produces": produces,
}
)
return version_builds
def summarize_matrix(builds: list[dict[str, any]]):
for build in builds:
tags = []
image_names = []
for produce in build["produces"]:
tag = produce.split(":")[-1]
image_name = produce.split(":")[-2]
if tag not in tags:
tags.append(tag)
if image_name not in image_names:
image_names.append(image_name)
tags.sort()
print(
"build %s %s for %s with tags %s to %s on %s"
% (
build["flavor"],
build["version"],
", ".join(build["architectures"]),
", ".join(tags),
", ".join(image_names),
build["runner"],
)
)
def build_release_tags(versions: list[str]) -> dict[str, str]:
tags = {}
for raw_version in versions:
parsed_ver = parse(raw_version)
# Hardcode skip of pre-5.x.x kernels
if parsed_ver.major < 5:
print(f'skipping {raw_version}, too old to support')
continue
major = str(parsed_ver.major)
major_minor = "%s.%s" % (parsed_ver.major, parsed_ver.minor)
if major not in tags or parse(tags[major]) < parsed_ver:
tags[major] = raw_version
if major_minor not in tags or parse(tags[major_minor]) < parsed_ver:
tags[major_minor] = raw_version
for tag in list(tags.keys()):
tags[tags[tag]] = tags[tag]
return tags
def generate_stable_matrix() -> list[dict[str, any]]:
current_kernel_releases = get_current_kernel_releases()
latest_stable = current_kernel_releases["latest_stable"]["version"]
versions = [r["version"] for r in current_kernel_releases["releases"]
if r["moniker"] in ["stable", "longterm"]]
tags = build_release_tags(versions)
tags["stable"] = latest_stable
tags["latest"] = latest_stable
return generate_matrix(tags)
def generate_lts_matrix() -> list[dict[str, any]]:
current_kernel_releases = get_current_kernel_releases()
versions = [r["version"] for r in current_kernel_releases["releases"]
if r["moniker"] == "longterm"]
return generate_matrix(build_release_tags(versions))
def generate_backbuild_matrix() -> list[dict[str, any]]:
tags = {}
major_minors = {}
all_releases = get_all_kernel_releases()
for version in all_releases:
parts = parse(version)
major_minor = "%s.%s" % (parts.major, parts.minor)
if major_minor in tags:
existing = tags[major_minor]
if parse(existing) < parts:
tags[major_minor] = version
major_minors[major_minor] = version
else:
tags[major_minor] = version
major_minors[major_minor] = version
for tag in list(tags.keys()):
version = tags[tag]
tags[version] = version
current_kernel_releases = get_current_kernel_releases()
for release in current_kernel_releases["releases"]:
if not release["moniker"] in ["stable", "longterm"]:
continue
for key in list(tags.keys()):
if tags[key] == release["version"]:
tags.pop(key)
for key in list(major_minors.keys()):
if major_minors[key] == release["version"]:
major_minors.pop(key)
parts = parse(release["version"])
major_minor = "%s.%s" % (parts.major, parts.minor)
if major_minor in major_minors:
major_minors.pop(major_minor)
if major_minor in tags:
tags.pop(major_minor)
return generate_matrix(tags)
def pick_runner(build: dict[str, any]) -> str:
version: str = build["version"]
version_info: Version = parse(version)
flavor: str = build["flavor"]
for runner in CONFIG["runners"]:
if matches_constraints(
version_info, flavor, runner, is_current_release=is_release_current(version_info.base_version)
):
return runner["name"]
raise Exception("No runner found for build %s" % build)
def fill_runners(builds: list[dict[str, any]]):
for build in builds:
build["runner"] = pick_runner(build)
def sort_matrix(builds: list[dict[str, any]]):
builds.sort(key=lambda build: Version(build["version"]))