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 os
import platform
import subprocess
import sys
from pathlib import Path
BUILD_CONTEXT = "dangerzone/"
@ -14,13 +16,27 @@ elif platform.system() == "Linux":
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()
print("Building container image")
subprocess.run(
[
CONTAINER_RUNTIME,
args.runtime,
"build",
"--pull",
BUILD_CONTEXT,
@ -33,32 +49,32 @@ def main():
]
)
print("Saving container image")
cmd = subprocess.Popen(
[
CONTAINER_RUNTIME,
"save",
TAG,
],
stdout=subprocess.PIPE,
)
if not args.no_save:
print("Saving container image")
cmd = subprocess.Popen(
[
CONTAINER_RUNTIME,
"save",
TAG,
],
stdout=subprocess.PIPE,
)
print("Compressing container image")
chunk_size = 4 << 12
with gzip.open("share/container.tar.gz", "wb") as gzip_f:
while True:
chunk = cmd.stdout.read(chunk_size)
if len(chunk) > 0:
gzip_f.write(chunk)
else:
break
cmd.wait(5)
print("Compressing container image")
chunk_size = 4 << 20
with gzip.open("share/container.tar.gz", "wb") as gzip_f:
while True:
chunk = cmd.stdout.read(chunk_size)
if len(chunk) > 0:
gzip_f.write(chunk)
else:
break
cmd.wait(5)
print("Looking up the image id")
image_id = subprocess.check_output(
[
CONTAINER_RUNTIME,
args.runtime,
"image",
"list",
"--format",
@ -86,4 +102,4 @@ def export_container_pip_dependencies():
if __name__ == "__main__":
main()
sys.exit(main())