bladerunner/packer/build-image.sh

87 lines
2.2 KiB
Bash
Raw Normal View History

#!/usr/bin/env bash
set -euxo pipefail
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
}
2023-04-12 08:01:57 +00:00
IMAGE_TYPE="${1:-ubuntu}"
preflight () {
case "${IMAGE_TYPE}" in
2023-04-12 08:01:57 +00:00
ubuntu)
PACKER_BUILD_FILE="boards/cm4-cluster-ubuntu-22.04.2.json"
2023-04-12 08:01:57 +00:00
if [ "${SKIP_LOCAL_CACHE}" != "yes" ]
then
REMOTE_IMAGE_URL="$(jq '.builders[0].file_urls' ${PACKER_BUILD_FILE} | grep https | tr -d ' \",')"
2023-04-12 08:01:57 +00:00
fi
;;
custom)
2023-04-12 08:01:57 +00:00
PACKER_BUILD_FILE="${2:-}"
if [ "${SKIP_LOCAL_CACHE}" != "yes" ]
then
REMOTE_IMAGE_URL="$(jq '.builders[0].file_urls' ${PACKER_BUILD_FILE} | grep https | tr -d ' \",')"
2023-04-12 08:01:57 +00:00
fi
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 " - ubuntu"
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 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}
}
preflight
2023-04-12 08:01:57 +00:00
cache_remote_url
build_image