-
Notifications
You must be signed in to change notification settings - Fork 0
/
archive-docker.py
executable file
·373 lines (327 loc) · 8.32 KB
/
archive-docker.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
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
#!/usr/bin/python3
import argparse
import datetime
import os
import subprocess
from pathlib import Path
APT_CONF = Path("apt.conf").absolute()
SOURCE_LIST = Path("source.list").absolute()
CWD = Path(os.getcwd()).absolute()
INTERNAL_CONTAINER = "archive-docker"
ARCH_MAP = {
"amd64": "x86_64",
"386": "i586",
"arm64": "aarch64",
"arm": "armh",
"ppc64le": "ppc64le",
}
def apt_date(date):
return datetime.date.fromisoformat(date).strftime("%Y/%m/%d")
def generate_source_list(arch, branch, date):
date = apt_date(date)
repo = "http://ftp.altlinux.org/pub/distributions/archive"
return f"""
rpm {repo}/{branch}/date/{date} {ARCH_MAP[arch]} classic
rpm {repo}/{branch}/date/{date} noarch classic
""".lstrip()
def create_apt_files(arch, branch, date):
APT_CONF.write_text(
f"""
Dir::Etc::main "/dev/null";
Dir::Etc::parts "/var/empty";
Dir::Etc::SourceList "{SOURCE_LIST}";
Dir::Etc::SourceParts "/var/empty";
Dir::Etc::preferences "/dev/null";
Dir::Etc::preferencesparts "/var/empty";
""".lstrip()
)
SOURCE_LIST.write_text(generate_source_list(arch, branch, date))
def make(arch, branch, out_dir, mkimage_profiles_dir):
subprocess.run(
[
"make",
f"APTCONF={APT_CONF}",
f"ARCH={ARCH_MAP[arch]}",
f"BRANCH={branch}",
f"IMAGE_OUTDIR={out_dir}",
"IMAGE_OUTFILE=alt.tar.xz",
"ve/docker.tar.xz",
],
cwd=mkimage_profiles_dir,
check=True,
)
def build_tarball(arch, branch, date, mkimage_profiles_dir):
create_apt_files(arch, branch, date)
out_dir = CWD / arch
out_dir.mkdir(exist_ok=True)
make(arch, branch, out_dir, mkimage_profiles_dir)
def remove_manifest(name):
subprocess.run(
[
"buildah",
"manifest",
"rm",
name,
],
check=False,
stderr=subprocess.DEVNULL,
stdout=subprocess.DEVNULL,
)
def remove_image(name):
subprocess.run(
[
"buildah",
"image",
"rm",
name,
],
check=False,
stderr=subprocess.DEVNULL,
stdout=subprocess.DEVNULL,
)
def remove_container(name):
subprocess.run(
[
"buildah",
"rm",
name,
],
check=False,
stderr=subprocess.DEVNULL,
stdout=subprocess.DEVNULL,
)
def buildah_build(arch, branch, date, manifest, image, packages):
container = f"{INTERNAL_CONTAINER}-{arch}"
def run_command_in_container(*cmd, **kwargs):
subprocess.run(["buildah", "run", container] + list(cmd), **kwargs)
remove_image(image)
remove_container(container)
subprocess.run(
[
"buildah",
"from",
"--arch",
arch,
"--name",
container,
"scratch",
],
)
subprocess.run(
[
"buildah",
"add",
container,
"alt.tar.xz",
"/",
],
cwd=CWD / arch,
)
run_command_in_container(
"sh",
"-c",
"true > /etc/security/limits.d/50-defaults.conf",
)
run_command_in_container(
"sh",
"-c",
"cat > /etc/apt/sources.list.d/alt.list",
input=generate_source_list(arch, branch, date).encode(),
)
if packages:
run_command_in_container(
"apt-get",
"update",
)
run_command_in_container("apt-get", "install", "-y", *packages)
run_command_in_container(
"sh",
"-c",
"rm -f"
" /var/cache/apt/archives/*.rpm"
" /var/cache/apt/*.bin"
" /var/lib/apt/lists/*.*",
)
subprocess.run(
[
"buildah",
"config",
"--cmd",
'["/bin/bash"]',
container,
],
)
subprocess.run(
[
"buildah",
"commit",
"--rm",
"--manifest",
manifest,
container,
image,
],
)
def test(arch, branch, date, image):
podman_run = ["podman", "run", "--rm", image]
commands = []
for value in [branch, ARCH_MAP[arch], apt_date(date)]:
commands.append(["grep", "-q", value, "/etc/apt/sources.list.d/alt.list"])
commands.append(["sh", "-c", "apt-get update && apt-get install -y ncdu"])
for command in commands:
subprocess.run(
podman_run + command,
check=True,
)
def podman_push(image):
subprocess.run(
[
"podman",
"manifest",
"push",
image,
f"docker://{image}",
],
)
def build_all(
arches,
branches,
dates,
mkimage_profiles_dir,
registry,
organization,
name,
stages,
packages,
):
repo = f"{registry}/{organization}"
for branch in branches:
for date in dates:
manifest = f"{repo}/{name}:{branch}-{date}"
if "build_image" in stages:
remove_manifest(manifest)
for arch in arches:
image = f"{repo}/{name}:{branch}-{date}-{arch}"
if "build_tarball" in stages:
build_tarball(arch, branch, date, mkimage_profiles_dir)
if "build_image" in stages:
buildah_build(arch, branch, date, manifest, image, packages)
if "test" in stages:
test(arch, branch, date, image)
if "push" in stages:
podman_push(manifest)
def clean():
subprocess.run(
[
"podman",
"system",
"prune",
"-af",
],
)
def parse_args():
stages = ["build_tarball", "build_image", "push", "clean", "test"]
all_arches = ["amd64", "386", "arm64", "arm", "ppc64le"]
default_arches = ["amd64", "arm64"]
all_branches = ["p9", "p10", "sisyphus"]
default_branches = ["p10", "sisyphus"]
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument(
"-r",
"--registry",
default="registry.altlinux.org",
)
parser.add_argument(
"--mkimage-profiles-dir",
default=Path("~/build/mkimage-profiles").expanduser(),
)
parser.add_argument(
"-o",
"--organization",
required=True,
)
parser.add_argument(
"-n",
"--name",
default="archive",
)
parser.add_argument(
"-d",
"--dates",
nargs="+",
help="list of dates",
)
parser.add_argument(
"-a",
"--arches",
nargs="+",
default=default_arches,
choices=all_arches,
help="list of arches",
)
parser.add_argument(
"--skip-arches",
nargs="+",
default=[],
choices=all_arches,
help="list of skipping arches",
)
parser.add_argument(
"-b",
"--branches",
nargs="+",
default=default_branches,
choices=all_branches,
help="list of branches",
)
parser.add_argument(
"--skip-branches",
nargs="+",
default=[],
choices=all_arches,
help="list of skipping branches",
)
parser.add_argument(
"--stages",
nargs="+",
default=stages,
choices=stages,
help="list of stages",
)
parser.add_argument(
"--skip-stages",
nargs="+",
default=[],
choices=stages,
help="list of skipping stages",
)
parser.add_argument(
"--packages",
nargs="+",
help="list of packages to install",
)
args = parser.parse_args()
args.arches = set(args.arches) - set(args.skip_arches)
args.branches = set(args.branches) - set(args.skip_branches)
args.stages = set(args.stages) - set(args.skip_stages)
return args
def main():
args = parse_args()
stages = args.stages
build_all(
args.arches,
args.branches,
args.dates,
args.mkimage_profiles_dir,
args.registry,
args.organization,
args.name,
stages,
args.packages,
)
if "clean" in stages:
clean()
if __name__ == "__main__":
main()