-
Notifications
You must be signed in to change notification settings - Fork 3
/
build.py
executable file
·559 lines (484 loc) · 14.8 KB
/
build.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
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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
#!/usr/bin/env python3
import subprocess
import fire
import json
import os
import sys
import tempfile
SUPPORT_PLATFORMS = ["linux/amd64", "linux/arm64"]
def get_current_platform():
import platform
system_os = platform.system().lower()
machine = platform.machine()
if system_os != "linux":
raise ValueError(f"unsupported platform: {system_os}")
cur_platform = None
if machine in ["x86_64", "amd64"]:
cur_platform = f"{system_os}/amd64"
elif platform.machine() in ["aarch64", "arm64"]:
cur_platform = f"{system_os}/arm64"
assert cur_platform in SUPPORT_PLATFORMS
return cur_platform
def log(s):
print(f"\033[96m{s}\033[0m")
def run(cmd, shell=True, check=True, interactive=False, **kw):
log(f"=> run: {cmd}")
if interactive:
choice = input("Contine? Enter for yes, other for Exit. ")
if choice.strip():
sys.exit(0)
return subprocess.run(cmd, shell=shell, check=check, **kw)
def get_git_tag(cwd):
return (
run(
"git describe --tag --abbrev=0",
stdout=subprocess.PIPE,
cwd=cwd,
)
.stdout.decode()
.strip()
)
def get_git_commit(cwd):
return (
run(
"git rev-parse HEAD",
stdout=subprocess.PIPE,
cwd=cwd,
)
.stdout.decode()
.strip()
)
def _buildx(
dockerfile,
image_name,
tags, # list of tags, e.g. ["tag1", "tag2"]
target="",
build_args="",
push=True,
write_to_docker=False,
cwd=".",
):
if len(tags) < 1:
raise ValueError("tags must not be empty")
current_platform = get_current_platform()
if target:
target = f"--target {target}"
# Build
with tempfile.NamedTemporaryFile() as fp:
fp.close()
metadata_file = fp.name
if push:
output = f"type=image,name={image_name},push-by-digest=true,name-canonical=true,push=true"
else:
output = (
f"type=image,name={image_name},name-canonical=true,push=false"
)
# build and push digest
cmd = f"""
docker buildx build \
--file {dockerfile} \
{target} {build_args} \
--output {output} \
--metadata-file {metadata_file} .
""".strip()
run(cmd, shell=True, cwd=cwd, check=True)
digest = json.load(open(metadata_file))["containerimage.digest"]
log(f"-> [build] {image_name} digest: {digest}")
# FIXME: merge into the Build step
# [Optional] writes to local image store, so it will appear in `docker images`
if write_to_docker:
for tag in tags:
cmd = f"""
docker buildx build \
--file {dockerfile} {target} {build_args} \
--output type=docker,name={image_name}:{tag} .
""".strip()
run(cmd, shell=True, cwd=cwd, check=True)
# Push
info_format = '--format "{{json .Manifest}}"'
fetch_info_cmd = (
f"docker buildx imagetools inspect {info_format} {image_name}:{tags[0]}"
)
image_info = run(
fetch_info_cmd,
shell=True,
check=False,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
)
if image_info.returncode != 0:
if "not found" in image_info.stdout.decode():
exist_manifests = []
else:
raise RuntimeError(image_info.stdout.decode())
else:
exist_manifests = json.loads(image_info.stdout).get("manifests")
if not exist_manifests:
# FIXME: get architecture from exist image(no manifests)
#
# for now we assume the exist image is for linux/amd64
exist_manifests = [
{
"platform": {"os": "linux", "architecture": "amd64"},
"digest": exist_manifests["digest"],
}
]
digests_to_push = set([digest])
for m in exist_manifests:
platform = m["platform"]["os"] + "/" + m["platform"]["architecture"]
if platform in SUPPORT_PLATFORMS and platform != current_platform:
digests_to_push.add(m["digest"])
tag_param = " ".join(f"--tag {image_name}:{tag}" for tag in tags)
digest_param = " ".join(f"{image_name}@{d}" for d in digests_to_push)
log(f"-> [push] tags: {tag_param}, digests: {digest_param}")
if push:
cmd = f"docker buildx imagetools create {tag_param} {digest_param}"
run(cmd, shell=True, cwd=cwd)
def setup(work_dir=".", use_container_builder=True):
ld_dir = os.path.join(work_dir, "LogDevice")
hs_dir = os.path.join(work_dir, "docker-haskell")
cmd = f"""
git clone --recurse-submodules https://github.com/hstreamdb/LogDevice.git {ld_dir}
git clone --recurse-submodules https://github.com/hstreamdb/docker-haskell.git {hs_dir}
"""
run(cmd, shell=True, check=True)
run(
"git checkout -b stable origin/stable",
shell=True,
cwd=ld_dir,
check=True,
)
# XXX: Required for push-by-digest feature
#
# NOTE: Cannot use a local image to FROM when using docker-container
# builder, so we need to push the image to a registry.
#
# - https://github.com/docker/buildx/issues/1453
# - https://github.com/moby/buildkit/issues/2343
if use_container_builder:
cmd = "docker buildx create --use --name build --node build --driver-opt network=host"
run(cmd, shell=True, check=True)
def logdevice_builder(
ld_dir="./LogDevice", no_push=False, no_write_to_docker=False
):
run(
"git checkout stable && git submodule update --init --recursive",
shell=True,
cwd=ld_dir,
)
_buildx(
"docker/Dockerfile.builder",
"hstreamdb/logdevice-builder",
["latest"],
push=not no_push,
write_to_docker=not no_write_to_docker,
cwd=ld_dir,
)
def logdevice(ld_dir="./LogDevice", no_push=False, no_write_to_docker=False):
run(
"git checkout stable && git submodule update --init --recursive",
shell=True,
cwd=ld_dir,
)
_buildx(
"docker/Dockerfile",
"hstreamdb/logdevice",
["latest"],
push=not no_push,
write_to_docker=not no_write_to_docker,
cwd=ld_dir,
)
_buildx(
"docker/Dockerfile",
"hstreamdb/logdevice-client",
["latest"],
target="client",
push=not no_push,
write_to_docker=not no_write_to_docker,
cwd=ld_dir,
)
def logdevice_builder_rqlite(
ld_dir="./LogDevice", no_push=False, no_write_to_docker=False
):
run(
"git checkout main && git submodule update --init --recursive",
shell=True,
cwd=ld_dir,
)
_buildx(
"docker/Dockerfile.builder",
"hstreamdb/logdevice-builder",
["rqlite"],
push=not no_push,
write_to_docker=not no_write_to_docker,
cwd=ld_dir,
)
def logdevice_rqlite(
ld_dir="./LogDevice", no_push=False, no_write_to_docker=False
):
run(
"git checkout main && git submodule update --init --recursive",
shell=True,
cwd=ld_dir,
)
_buildx(
"docker/Dockerfile",
"hstreamdb/logdevice",
["rqlite"],
push=not no_push,
write_to_docker=not no_write_to_docker,
cwd=ld_dir,
)
_buildx(
"docker/Dockerfile",
"hstreamdb/logdevice-client",
["rqlite"],
target="client",
push=not no_push,
write_to_docker=not no_write_to_docker,
cwd=ld_dir,
)
def grpc(
hs_dir="./docker-haskell",
no_push=False,
no_write_to_docker=False,
version="1.54.2",
):
_buildx(
"dockerfiles/grpc",
"ghcr.io/hstreamdb/grpc",
[version],
build_args=f"--build-arg GRPC=v{version}",
push=not no_push,
write_to_docker=not no_write_to_docker,
cwd=hs_dir,
)
def _ghc(ghcs, hs_dir, no_push, no_write_to_docker):
_buildx(
"dockerfiles/ghc_from_haskell",
"ghcr.io/hstreamdb/ghc",
ghcs,
build_args=f"--build-arg GHC={ghcs[0]}",
push=not no_push,
write_to_docker=not no_write_to_docker,
cwd=hs_dir,
)
def ghc810(hs_dir="./docker-haskell", no_push=False, no_write_to_docker=False):
_ghc(["8.10.7", "8.10"], hs_dir, no_push, no_write_to_docker)
def ghc904(hs_dir="./docker-haskell", no_push=False, no_write_to_docker=False):
_ghc(["9.4.8", "9.4"], hs_dir, no_push, no_write_to_docker)
def ghc906(hs_dir="./docker-haskell", no_push=False, no_write_to_docker=False):
_ghc(["9.6.5", "9.6"], hs_dir, no_push, no_write_to_docker)
def ghc(hs_dir="./docker-haskell", no_push=False, no_write_to_docker=False):
ghc810(
hs_dir=hs_dir, no_push=no_push, no_write_to_docker=no_write_to_docker
)
ghc904(
hs_dir=hs_dir, no_push=no_push, no_write_to_docker=no_write_to_docker
)
ghc906(
hs_dir=hs_dir, no_push=no_push, no_write_to_docker=no_write_to_docker
)
def hsthrift(
hs_dir="./docker-haskell", no_push=False, no_write_to_docker=False
):
_buildx(
"dockerfiles/hsthrift",
"ghcr.io/hstreamdb/hsthrift",
["latest"],
push=not no_push,
write_to_docker=not no_write_to_docker,
cwd=hs_dir,
)
def hadmin_store(
hs_dir="./docker-haskell", no_push=False, no_write_to_docker=False
):
_buildx(
"dockerfiles/hadmin_store",
"ghcr.io/hstreamdb/hadmin-store",
["latest"],
push=not no_push,
write_to_docker=not no_write_to_docker,
cwd=hs_dir,
)
def _haskell(tags, ghc, ld_image, hs_dir, no_push, no_write_to_docker):
_buildx(
"Dockerfile",
"hstreamdb/haskell",
tags,
build_args=f"--build-arg GHC={ghc} --build-arg LD_CLIENT_IMAGE={ld_image}",
push=not no_push,
write_to_docker=not no_write_to_docker,
cwd=hs_dir,
)
def haskell810(
hs_dir="./docker-haskell", no_push=False, no_write_to_docker=False
):
_haskell(
["8.10.7", "8.10"],
"8.10.7",
"hstreamdb/logdevice-client",
hs_dir,
no_push,
no_write_to_docker,
)
def haskell810_rqlite(
hs_dir="./docker-haskell", no_push=False, no_write_to_docker=False
):
_haskell(
["rqlite_8.10.7", "rqlite_8.10"],
"8.10.7",
"hstreamdb/logdevice-client:rqlite",
hs_dir,
no_push,
no_write_to_docker,
)
def haskell904(
hs_dir="./docker-haskell", no_push=False, no_write_to_docker=False
):
_haskell(
["9.4.8", "9.4", "latest"],
"9.4.8",
"hstreamdb/logdevice-client",
hs_dir,
no_push,
no_write_to_docker,
)
def haskell904_rqlite(
hs_dir="./docker-haskell", no_push=False, no_write_to_docker=False
):
_haskell(
["rqlite_9.4.8", "rqlite_9.4", "rqlite"],
"9.4.8",
"hstreamdb/logdevice-client:rqlite",
hs_dir,
no_push,
no_write_to_docker,
)
def haskell906(
hs_dir="./docker-haskell", no_push=False, no_write_to_docker=False
):
_haskell(
["9.6.5", "9.6"],
"9.6.5",
"hstreamdb/logdevice-client",
hs_dir,
no_push,
no_write_to_docker,
)
def haskell906_rqlite(
hs_dir="./docker-haskell", no_push=False, no_write_to_docker=False
):
_haskell(
["rqlite_9.6.5", "rqlite_9.6"],
"9.6.5",
"hstreamdb/logdevice-client:rqlite",
hs_dir,
no_push,
no_write_to_docker,
)
def haskell(hs_dir="./docker-haskell", no_push=False, no_write_to_docker=False):
haskell810(
hs_dir=hs_dir, no_push=no_push, no_write_to_docker=no_write_to_docker
)
haskell810_rqlite(
hs_dir=hs_dir, no_push=no_push, no_write_to_docker=no_write_to_docker
)
haskell904(
hs_dir=hs_dir, no_push=no_push, no_write_to_docker=no_write_to_docker
)
haskell904_rqlite(
hs_dir=hs_dir, no_push=no_push, no_write_to_docker=no_write_to_docker
)
haskell906(
hs_dir=hs_dir, no_push=no_push, no_write_to_docker=no_write_to_docker
)
haskell906_rqlite(
hs_dir=hs_dir, no_push=no_push, no_write_to_docker=no_write_to_docker
)
# TODO: hstream_builder
def hstream(
hstream_dir="./hstream",
tag="v0.19.5",
build_cache="no_cache", # "no_cache" or "cache", currently arm builds have to use "no_cache"
no_push=False,
no_write_to_docker=False,
):
if not os.path.exists(hstream_dir):
run(
f"git clone --recurse-submodules -b {tag} https://github.com/hstreamdb/hstream.git {hstream_dir}"
)
git_tag = get_git_tag(hstream_dir)
git_commit = get_git_commit(hstream_dir)
build_args = f"""
--build-arg HS_IMAGE=hstreamdb/haskell:9.4 \
--build-arg LD_IMAGE=hstreamdb/logdevice:latest \
--build-arg BUILD_CACHE={build_cache} \
--build-arg HSTREAM_VERSION={git_tag} \
--build-arg HSTREAM_VERSION_COMMIT={git_commit}
""".strip()
_buildx(
"docker/Dockerfile",
"hstreamdb/hstream",
[tag],
build_args=build_args,
push=not no_push,
write_to_docker=not no_write_to_docker,
cwd=hstream_dir,
)
def hstream_rqlite(
hstream_dir="./hstream",
tag="v0.19.5",
build_cache="no_cache", # "no_cache" or "cache", currently arm builds have to use "no_cache"
no_push=False,
no_write_to_docker=False,
):
if not os.path.exists(hstream_dir):
run(
f"git clone --recurse-submodules -b {tag} https://github.com/hstreamdb/hstream.git {hstream_dir}"
)
git_tag = get_git_tag(hstream_dir)
git_commit = get_git_commit(hstream_dir)
build_args = f"""
--build-arg HS_IMAGE=hstreamdb/haskell:rqlite_9.4 \
--build-arg LD_IMAGE=hstreamdb/logdevice:rqlite \
--build-arg BUILD_CACHE={build_cache} \
--build-arg HSTREAM_VERSION={git_tag} \
--build-arg HSTREAM_VERSION_COMMIT={git_commit}
""".strip()
_buildx(
"docker/Dockerfile",
"hstreamdb/hstream",
["rqlite_" + tag],
build_args=build_args,
push=not no_push,
write_to_docker=not no_write_to_docker,
cwd=hstream_dir,
)
if __name__ == "__main__":
if "-h" in sys.argv or "--help" in sys.argv:
sys.stdout = open("/dev/null", "w")
fns = [
setup,
logdevice_builder,
logdevice,
logdevice_builder_rqlite,
logdevice_rqlite,
grpc,
ghc810,
ghc904,
ghc906,
ghc,
hsthrift,
hadmin_store,
haskell810,
haskell810_rqlite,
haskell904,
haskell904_rqlite,
haskell906,
haskell906_rqlite,
haskell,
hstream,
hstream_rqlite,
]
fire.Fire({fn.__name__: fn for fn in fns})