Browse Source

Add vol and ext_home

www
Fernando Alegre 7 months ago
parent
commit
1b48978ac6
  1. 56
      .install/bin/pdm-build

56
.install/bin/pdm-build

@ -8,6 +8,14 @@ trap 'cleanup' EXIT
epoch=$(date +%s.%3N) epoch=$(date +%s.%3N)
today=$(date +%Y-%m-%d-T%H%M) today=$(date +%Y-%m-%d-T%H%M)
notopt() {
case $1 in
1) return 0;;
0) return 1;;
*) return $1;;
esac
}
badarg() { badarg() {
echo -n "$(basename $0): " >&2 echo -n "$(basename $0): " >&2
echo "$1" >&2 echo "$1" >&2
@ -21,34 +29,37 @@ cleanup() {
fi 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. 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. 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 If second argument is omitted, the directory where the files were found is used
as the image name. as the image name.
Options: 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 -d Debug mode: don't delete the temporary container created by the script
when encountering an error 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 multiple tags
-v vol Mount ~/vol as /vol while building
-h Display this help and exit" -h Display this help and exit"
# Handle options # Handle options
opt_squash=0 opt_squash=1
opt_redo=0 opt_redo=0
opt_debug=0 opt_debug=0
opt_tags=() opt_tags=()
while getopts ':srdt:h' arg; do opt_vols=()
while getopts ':srdt:v:h' arg; do
case $arg in case $arg in
s) opt_squash=1;; s) opt_squash=$(notopt ${opt_squash});;
r) opt_redo=1;; r) opt_redo=$(notopt ${opt_redo});;
d) opt_debug=1;; d) opt_debug=$(notopt ${opt_debug});;
t) opt_tags+=("${OPTARG}");; t) opt_tags+=("${OPTARG}");;
v) opt_vols+=("${OPTARG}");;
h) echo "$help"; exit 0;; h) echo "$help"; exit 0;;
:) badarg "Argument missing for option '-$OPTARG'";; :) badarg "Argument missing for option '-$OPTARG'";;
?) badarg "Invalid option '-$OPTARG'";; ?) badarg "Invalid option '-$OPTARG'";;
@ -82,7 +93,8 @@ else
cd "$directory" cd "$directory"
fi fi
buildopts="" buildopts="--build-arg EXT_HOME=$HOME"
runopts=""
if [[ $opt_squash -eq 1 ]]; then if [[ $opt_squash -eq 1 ]]; then
buildopts="$buildopts --squash-all" buildopts="$buildopts --squash-all"
fi fi
@ -90,28 +102,33 @@ if [[ $opt_redo -eq 1 ]]; then
buildopts="$buildopts --no-cache" buildopts="$buildopts --no-cache"
fi 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 # 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 # because only docker-format images can use the SHELL directive in Containerfiles
export BUILDAH_FORMAT=docker ### export BUILDAH_FORMAT=docker
# build image # build image
echo "Building image ..." echo "Building image ..."
podman build -f Containerfile -t tmp-$epoch $buildopts podman build -f Containerfile -t tmp-$epoch $buildopts
# Systemdfile is for commands that need systemd to execute # Initfile is for commands that need systemd to execute
if [[ -f Systemdfile ]]; then if [[ -f Initfile ]]; then
echo "Running build steps that require systemd ..." echo "Running initialization ..."
echo "Creating temporary container ..." echo "Creating temporary container ..."
podman create --name tmp-$epoch tmp-$epoch podman create --name tmp-$epoch $runopts tmp-$epoch
podman start tmp-$epoch podman start tmp-$epoch
echo "Copying script to container ..." echo "Copying script to container ..."
podman cp Systemdfile tmp-$epoch:/root/ podman cp Initfile tmp-$epoch:/root/
echo "Running script ..." 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 ..." echo "Committing container to image ..."
podman commit tmp-$epoch "$name:$today" podman commit tmp-$epoch "$name:$today"
else 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 # tag image we already built with appropriate tag, and untag with tmp
podman tag tmp-$epoch "$name:$today" podman tag tmp-$epoch "$name:$today"
podman rmi tmp-$epoch podman rmi tmp-$epoch
@ -126,3 +143,4 @@ for tag in "${opt_tags[@]}"; do
done done
echo "Done!" echo "Done!"

Loading…
Cancel
Save