diff --git a/.install/bin/pdm-build b/.install/bin/pdm-build index 1ed5e77..6fc1363 100755 --- a/.install/bin/pdm-build +++ b/.install/bin/pdm-build @@ -8,6 +8,14 @@ trap 'cleanup' EXIT epoch=$(date +%s.%3N) today=$(date +%Y-%m-%d-T%H%M) +notopt() { + case $1 in + 1) return 0;; + 0) return 1;; + *) return $1;; + esac +} + badarg() { echo -n "$(basename $0): " >&2 echo "$1" >&2 @@ -21,34 +29,37 @@ cleanup() { fi } -help="Usage: $(basename $0) [-sdh] [-t tag] [directory] [name] +help="Usage: $(basename $0) [-sdh] [-t tag] [-v vol] [directory] [name] Builds an image from files in a directory, and assigns it a name. -Files used are 'Containerfile' and optionally 'Systemdfile'. If first argument +Files used are 'Containerfile' and optionally 'Initfile'. If first argument is omitted, script assumes files can be found in the current working directory. If second argument is omitted, the directory where the files were found is used as the image name. Options: - -s Squash all layers in the image into a single layer - -r Redo build from scratch instead of using cached layers -d Debug mode: don't delete the temporary container created by the script when encountering an error - -t [tag] Tag the image with the given string. Can be used multiple times to assign + -r Redo build from scratch instead of using cached layers + -s Squash all layers in the image into a single layer + -t tag Tag the image with the given string. Can be used multiple times to assign multiple tags + -v vol Mount ~/vol as /vol while building -h Display this help and exit" # Handle options -opt_squash=0 +opt_squash=1 opt_redo=0 opt_debug=0 opt_tags=() -while getopts ':srdt:h' arg; do +opt_vols=() +while getopts ':srdt:v:h' arg; do case $arg in - s) opt_squash=1;; - r) opt_redo=1;; - d) opt_debug=1;; + s) opt_squash=$(notopt ${opt_squash});; + r) opt_redo=$(notopt ${opt_redo});; + d) opt_debug=$(notopt ${opt_debug});; t) opt_tags+=("${OPTARG}");; + v) opt_vols+=("${OPTARG}");; h) echo "$help"; exit 0;; :) badarg "Argument missing for option '-$OPTARG'";; ?) badarg "Invalid option '-$OPTARG'";; @@ -82,7 +93,8 @@ else cd "$directory" fi -buildopts="" +buildopts="--build-arg EXT_HOME=$HOME" +runopts="" if [[ $opt_squash -eq 1 ]]; then buildopts="$buildopts --squash-all" fi @@ -90,28 +102,33 @@ if [[ $opt_redo -eq 1 ]]; then buildopts="$buildopts --no-cache" fi +for vol in "${opt_vols[@]}"; do + buildopts="$buildopts -v $HOME/vol/${name}/${vol}:/vol/${vol}" + runopts="$runopts -v $HOME/vol/${name}/${vol}:/vol/${vol}" +done + # tell buildah to build images in docker format instead of the default OCI format # because only docker-format images can use the SHELL directive in Containerfiles -export BUILDAH_FORMAT=docker +### export BUILDAH_FORMAT=docker # build image echo "Building image ..." podman build -f Containerfile -t tmp-$epoch $buildopts -# Systemdfile is for commands that need systemd to execute -if [[ -f Systemdfile ]]; then - echo "Running build steps that require systemd ..." +# Initfile is for commands that need systemd to execute +if [[ -f Initfile ]]; then + echo "Running initialization ..." echo "Creating temporary container ..." - podman create --name tmp-$epoch tmp-$epoch + podman create --name tmp-$epoch $runopts tmp-$epoch podman start tmp-$epoch echo "Copying script to container ..." - podman cp Systemdfile tmp-$epoch:/root/ + podman cp Initfile tmp-$epoch:/root/ echo "Running script ..." - podman exec tmp-$epoch bash -c "chmod +x /root/Systemdfile && /root/Systemdfile" + podman exec tmp-$epoch bash -c "chmod +x /root/Initfile && /root/Initfile" echo "Committing container to image ..." podman commit tmp-$epoch "$name:$today" else - echo "Systemdfile not found, skipping temporary container step ..." + echo "Initfile not found, skipping temporary container step ..." # tag image we already built with appropriate tag, and untag with tmp podman tag tmp-$epoch "$name:$today" podman rmi tmp-$epoch @@ -126,3 +143,4 @@ for tag in "${opt_tags[@]}"; do done echo "Done!" +