install: Make build image script more flexible

Add the following functionality to the build image script:

1. Let the user choose the container runtime of their choice. In some
   systems, both Docker and Podman may be available, so we need to let
   the user choose which runtime they want.
2. Let users choose if they want to save the image. For non-production
   builds, we may want to simply build the container image, without
   the time penalty of compression.
This commit is contained in:
Alex Pyrgiotis 2024-01-03 16:50:30 +02:00 committed by deeplow
parent f1d90c6fa9
commit f254575cb4
No known key found for this signature in database
GPG key ID: 577982871529A52A

View file

@ -1,7 +1,9 @@
import argparse
import gzip import gzip
import os import os
import platform import platform
import subprocess import subprocess
import sys
from pathlib import Path from pathlib import Path
BUILD_CONTEXT = "dangerzone/" BUILD_CONTEXT = "dangerzone/"
@ -14,13 +16,27 @@ elif platform.system() == "Linux":
def main(): def main():
print("exporting container pip dependencies") parser = argparse.ArgumentParser()
parser.add_argument(
"--runtime",
choices=["docker", "podman"],
default=CONTAINER_RUNTIME,
help=f"The container runtime for building the image (default: {CONTAINER_RUNTIME})",
)
parser.add_argument(
"--no-save",
action="store_true",
help="Do not save the container image as a tarball in share/container.tar.gz",
)
args = parser.parse_args()
print("Exporting container pip dependencies")
export_container_pip_dependencies() export_container_pip_dependencies()
print("Building container image") print("Building container image")
subprocess.run( subprocess.run(
[ [
CONTAINER_RUNTIME, args.runtime,
"build", "build",
"--pull", "--pull",
BUILD_CONTEXT, BUILD_CONTEXT,
@ -33,32 +49,32 @@ def main():
] ]
) )
print("Saving container image") if not args.no_save:
cmd = subprocess.Popen( print("Saving container image")
[ cmd = subprocess.Popen(
CONTAINER_RUNTIME, [
"save", CONTAINER_RUNTIME,
TAG, "save",
], TAG,
stdout=subprocess.PIPE, ],
) stdout=subprocess.PIPE,
)
print("Compressing container image") print("Compressing container image")
chunk_size = 4 << 12 chunk_size = 4 << 20
with gzip.open("share/container.tar.gz", "wb") as gzip_f: with gzip.open("share/container.tar.gz", "wb") as gzip_f:
while True: while True:
chunk = cmd.stdout.read(chunk_size) chunk = cmd.stdout.read(chunk_size)
if len(chunk) > 0: if len(chunk) > 0:
gzip_f.write(chunk) gzip_f.write(chunk)
else: else:
break break
cmd.wait(5)
cmd.wait(5)
print("Looking up the image id") print("Looking up the image id")
image_id = subprocess.check_output( image_id = subprocess.check_output(
[ [
CONTAINER_RUNTIME, args.runtime,
"image", "image",
"list", "list",
"--format", "--format",
@ -86,4 +102,4 @@ def export_container_pip_dependencies():
if __name__ == "__main__": if __name__ == "__main__":
main() sys.exit(main())