You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
87 lines
2.3 KiB
87 lines
2.3 KiB
#!/bin/bash
|
|
|
|
# Functions/variables
|
|
quit() {
|
|
if [[ $1 == 0 || $FLAGS_debug == $FLAGS_FALSE ]]; then
|
|
podman rm -i -f tmp-$epoch 2>&1 > /dev/null
|
|
podman rmi tmp:$epoch 2>&1 > /dev/null
|
|
fi
|
|
exit $1
|
|
}
|
|
|
|
libdir=/tank/local/podman/lib
|
|
today=$(date "+%Y-%m-%d-T%H%M")
|
|
epoch=$(date "+%s.%3N")
|
|
|
|
# Handle flags
|
|
source $libdir/shflags
|
|
DEFINE_boolean 'squash' false 'squash newly built layers into a single new layer' 's'
|
|
DEFINE_boolean 'debug' false "Don't delete temporary image/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
|
|
|
|
if [[ $FLAGS_squash == $FLAGS_TRUE ]]; then
|
|
buildopts="--squash"
|
|
else
|
|
buildopts=""
|
|
fi
|
|
|
|
# Main
|
|
|
|
# build image
|
|
echo "Building container ..."
|
|
podman build -f Containerfile -t tmp:$epoch $buildopts || quit $?
|
|
|
|
# start container
|
|
echo "Creating container ..."
|
|
podman create --name tmp-$epoch tmp:$epoch || 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
|
|
|