From 7deb49825d48df9371cdd7a470e12a9b0ea8a041 Mon Sep 17 00:00:00 2001 From: David Boreham Date: Fri, 14 Apr 2023 12:10:27 -0600 Subject: [PATCH] Initial commit --- app/build_containers.py | 8 +++++++- app/data/container-build/build-base.sh | 13 +++++++++++++ app/data/container-build/default-build.sh | 11 ++++++----- 3 files changed, 26 insertions(+), 6 deletions(-) create mode 100755 app/data/container-build/build-base.sh diff --git a/app/build_containers.py b/app/build_containers.py index 5b85f5bf..ddc6a59f 100644 --- a/app/build_containers.py +++ b/app/build_containers.py @@ -36,13 +36,16 @@ from .util import include_exclude_check, get_parsed_stack_config @click.command() @click.option('--include', help="only build these containers") @click.option('--exclude', help="don\'t build these containers") +@click.option("--force-rebuild", is_flag=True, default=False, help="Override dependency checking -- always rebuild") +@click.option("--extra-build-args", help="Supply extra arguments to build") @click.pass_context -def command(ctx, include, exclude): +def command(ctx, include, exclude, force_rebuild, extra_build_args): '''build the set of containers required for a complete stack''' quiet = ctx.obj.quiet verbose = ctx.obj.verbose dry_run = ctx.obj.dry_run + debug = ctx.obj.debug local_stack = ctx.obj.local_stack stack = ctx.obj.stack continue_on_error = ctx.obj.continue_on_error @@ -88,6 +91,9 @@ def command(ctx, include, exclude): "CERC_HOST_GID": f"{os.getgid()}", "DOCKER_BUILDKIT": "0" } + container_build_env.update({"CERC_SCRIPT_DEBUG": "true"} if debug else {}) + container_build_env.update({"CERC_FORCE_REBUILD": "true"} if force_rebuild else {}) + container_build_env.update({"CERC_CONTAINER_EXTRA_BUILD_ARGS": extra_build_args} if extra_build_args else {}) def process_container(container): if not quiet: diff --git a/app/data/container-build/build-base.sh b/app/data/container-build/build-base.sh new file mode 100755 index 00000000..dca17fc4 --- /dev/null +++ b/app/data/container-build/build-base.sh @@ -0,0 +1,13 @@ +# source'ed into container build scripts to do generic command setup +if [[ -n "$CERC_SCRIPT_DEBUG" ]]; then + set -x + echo "Build environment variables:" + env +fi +build_command_args="" +if [[ ${CERC_FORCE_REBUILD} == "true" ]]; then + build_command_args="${build_command_args} --pull --no-cache" +fi +if [[ -n "$CERC_CONTAINER_EXTRA_BUILD_ARGS" ]]; then + build_command_args="${build_command_args} ${CERC_CONTAINER_EXTRA_BUILD_ARGS}" +fi diff --git a/app/data/container-build/default-build.sh b/app/data/container-build/default-build.sh index c52dd043..6986f652 100755 --- a/app/data/container-build/default-build.sh +++ b/app/data/container-build/default-build.sh @@ -1,14 +1,15 @@ #!/usr/bin/env bash # Usage: default-build.sh [] # if is not supplied, the context is the directory where the Dockerfile lives -if [[ -n "$CERC_SCRIPT_DEBUG" ]]; then - set -x -fi + +# See: https://stackoverflow.com/a/246128/1701505 +SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) +source ${SCRIPT_DIR}/build-base.sh + if [[ $# -ne 2 ]]; then echo "Illegal number of parameters" >&2 exit 1 fi image_tag=$1 build_dir=$2 -echo "Building ${image_tag} in ${build_dir}" -docker build -t ${image_tag} --build-arg CERC_HOST_UID=${CERC_HOST_UID} --build-arg CERC_HOST_GID=${CERC_HOST_GID} ${build_dir} +docker build -t ${image_tag} ${build_command_args} --build-arg CERC_HOST_UID=${CERC_HOST_UID} --build-arg CERC_HOST_GID=${CERC_HOST_GID} ${build_dir}