2023-04-11 20:41:16 +00:00
|
|
|
#!/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
|
|
|
|
|
2023-04-11 20:41:16 +00:00
|
|
|
IMAGE_TYPE="${1:-ubuntu}"
|
2023-04-12 04:55:00 +00:00
|
|
|
PACKER_BUILD_FILE="${2:-}"
|
2023-04-12 08:01:57 +00:00
|
|
|
SKIP_LOCAL_CACHE="${SKIP_LOCAL_CACHE:-no}"
|
2023-04-11 20:41:16 +00:00
|
|
|
|
|
|
|
errmsg () {
|
|
|
|
echo "$@" > /dev/stderr
|
|
|
|
}
|
|
|
|
|
2023-04-14 03:42:03 +00:00
|
|
|
IMAGE_TYPE="${1:-cnode}"
|
2023-04-12 08:01:57 +00:00
|
|
|
|
2023-04-11 20:41:16 +00:00
|
|
|
preflight () {
|
2023-04-12 00:01:56 +00:00
|
|
|
case "${IMAGE_TYPE}" in
|
2023-04-14 03:42:03 +00:00
|
|
|
cdev) PACKER_BUILD_FILE="boards/cm4-cdev-ubuntu-22.04.2.json" ;;
|
|
|
|
cnode) PACKER_BUILD_FILE="cm4-cluster-ubuntu-22.04.2.img" ;;
|
2023-04-12 00:01:56 +00:00
|
|
|
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"
|
2023-04-14 03:42:03 +00:00
|
|
|
errmsg " - cdev"
|
|
|
|
errmsg " - cnode"
|
2023-04-12 00:01:56 +00:00
|
|
|
errmsg " - custom path/to/board/file"
|
2023-04-11 20:41:16 +00:00
|
|
|
exit 1
|
2023-04-12 00:01:56 +00:00
|
|
|
;;
|
2023-04-11 20:41:16 +00:00
|
|
|
esac
|
|
|
|
}
|
|
|
|
|
2023-04-12 08:01:57 +00:00
|
|
|
cache_remote_url () {
|
2023-04-12 19:50:11 +00:00
|
|
|
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
|
|
|
|
|
2023-04-14 03:42:03 +00:00
|
|
|
local REMOTE_IMAGE_URL="$(jq '.builders[0].file_urls' ${PACKER_BUILD_FILE} | grep https | tr -d ' \",')"
|
2023-04-12 19:50:11 +00:00
|
|
|
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
|
|
|
|
|
2023-04-12 19:50:11 +00:00
|
|
|
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
|
|
|
|
|
2023-04-12 19:50:11 +00:00
|
|
|
curl -fsSL -o "${CACHED_FILE}" "${REMOTE_IMAGE_URL}"
|
2023-04-12 08:01:57 +00:00
|
|
|
}
|
|
|
|
|
2023-04-11 20:41:16 +00:00
|
|
|
build_image () {
|
2023-04-12 00:01:56 +00:00
|
|
|
sudo packer build ${PACKER_BUILD_FILE}
|
|
|
|
}
|
|
|
|
|
2023-04-13 03:16:58 +00:00
|
|
|
main () {
|
|
|
|
preflight
|
|
|
|
cache_remote_url
|
|
|
|
build_image
|
|
|
|
}
|
2023-04-12 00:01:56 +00:00
|
|
|
|
2023-04-13 03:18:09 +00:00
|
|
|
if [[ "${RUN_MAIN}" = "yes" ]]
|
2023-04-13 03:16:58 +00:00
|
|
|
then
|
|
|
|
main
|
|
|
|
fi
|