bladerunner/packer/build-image.sh

92 lines
2.1 KiB
Bash
Raw Permalink Normal View History

#!/usr/bin/env bash
set -euxo pipefail
2023-04-13 03:18:09 +00:00
if [[ "${BASH_SOURCE[0]}" = "${0}" ]]
then
set -euxo pipefail
RUN_MAIN=yes
else
RUN_MAIN=no
fi
IMAGE_TYPE="${1:-ubuntu}"
PACKER_BUILD_FILE="${2:-}"
2023-04-12 08:01:57 +00:00
SKIP_LOCAL_CACHE="${SKIP_LOCAL_CACHE:-no}"
errmsg () {
echo "$@" > /dev/stderr
}
IMAGE_TYPE="${1:-cnode}"
2023-04-12 08:01:57 +00:00
preflight () {
case "${IMAGE_TYPE}" in
cdev) PACKER_BUILD_FILE="boards/cm4-cdev-ubuntu-22.04.2.json" ;;
cnode) PACKER_BUILD_FILE="boards/cm4-cnode-ubuntu-22.04.2.json" ;;
custom)
if [ -z "${PACKER_BUILD_FILE}" ]
then
errmsg "[!] custom board requires a board file path"
exit 1
fi
;;
*)
errmsg "[!] invalid image type ${IMAGE_TYPE}."
errmsg "[!] valid image types are"
errmsg " - cdev"
errmsg " - cnode"
errmsg " - custom path/to/board/file"
exit 1
;;
esac
}
2023-04-12 08:01:57 +00:00
cache_remote_url () {
if [ "${SKIP_LOCAL_CACHE}" = "yes" ]
2023-04-12 08:01:57 +00:00
then
echo "[+] skipping fetch of remote file: SKIP_LOCAL_CACHE=yes"
return 0
fi
local REMOTE_IMAGE_URL="$(jq '.builders[0].file_urls' ${PACKER_BUILD_FILE} | grep https | tr -d ' \",')"
local CACHED_FILE="$(jq '.builders[0].file_urls' ${PACKER_BUILD_FILE} | grep -v https | grep \" | tr -d ' \",')"
2023-04-12 08:01:57 +00:00
if [ -z "${CACHED_FILE}" ]
then
echo "[+] skipping fetch of remote file: no local file provided"
return 0
fi
if [ -z "${REMOTE_IMAGE_URL}" ]
2023-04-12 08:01:57 +00:00
then
echo "[+] skipping fetch of remote file: no remote file provided"
return 0
fi
if [ -s "${CACHED_FILE}" ]
then
echo "[+] skipping fetch of remote file: file already exists. To force redownloading,"
echo " run the following:"
echo ""
echo " rm ${CACHED_FILE}"
return 0
fi
curl -fsSL -o "${CACHED_FILE}" "${REMOTE_IMAGE_URL}"
2023-04-12 08:01:57 +00:00
}
build_image () {
sudo packer build ${PACKER_BUILD_FILE}
}
main () {
preflight
cache_remote_url
build_image
}
2023-04-13 03:18:09 +00:00
if [[ "${RUN_MAIN}" = "yes" ]]
then
main
fi