#!/bin/bash # Functions/variables quit() { if [[ $1 == 0 || $FLAGS_debug == $FLAGS_FALSE ]]; then podman rm -i -f tmp-$epoch 2>&1 > /dev/null fi exit $1 } epoch=$(date +%s.%3N) today=$(date +%Y-%m-%d-T%H%M) # Handle flags source shflags DEFINE_boolean 'squash' false 'squash newly built layers into a single new layer' 's' DEFINE_boolean 'debug' false "Don't delete temporary container on build fail" 'd' DEFINE_string 'tag' 'latest' 'Tag (other than date) to assign to the image' 't' FLAGS_HELP="Usage: $0 [-s] [-d] [-t tag] [directory] [name] Builds an image from the Containerfile and (optionally) Systemdfile in a directory passed as the first argument, and names the image after the second argument. If no first argument is given, the current working directory is used. If no second argument is given, the image is named after the directory. " FLAGS "$@" || exit $? eval set -- "${FLAGS_ARGV}" # Handle errors/arguments/cases if [[ $# -gt 2 ]]; then echo "Error: too many arguments" echo "" flags_help exit 1 fi if [[ -n $1 ]]; then directory=$1 else directory=$(pwd) fi if [[ ! -d $directory ]]; then echo "Error: directory \"$directory\" not found" echo "" flags_help exit 1 else cd $directory fi if [[ -n $2 ]]; then name=$2 else name=$(basename $(pwd)) fi # build options buildopts="" if [[ $FLAGS_squash == $FLAGS_TRUE ]]; then buildopts="$buildopts --squash" fi # Main # build image echo "Building image ..." podman build -f Containerfile -t tmp $buildopts || quit $? # start container echo "Creating container ..." podman create --name tmp-$epoch tmp || quit $? podman start tmp-$epoch || quit $? # Systemdfile is for commands that need systemd to execute echo "Running build steps that require systemd ..." podman exec tmp-$epoch bash -c "if [ -f /root/Systemdfile ]; then /root/Systemdfile; fi" || quit $? # commit finalized container state to image echo "Committing container to image ..." podman commit tmp-$epoch $name:$today || quit $? # tag with latest tag podman tag $name:$today $name:$FLAGS_tag echo "Done!" quit 0