Created
February 27, 2021 11:06
-
-
Save larsfuehrer/f0f076c9dc73a05dc4ab117225d7b717 to your computer and use it in GitHub Desktop.
Script to get Terragrunt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
: ${BINARY_NAME:="terragrunt"} | |
: ${USE_SUDO:="true"} | |
: ${DEBUG:="false"} | |
: ${TERRAGRUNT_INSTALL_DIR:="/usr/local/bin"} | |
HAS_CURL="$(type "curl" &> /dev/null && echo true || echo false)" | |
HAS_WGET="$(type "wget" &> /dev/null && echo true || echo false)" | |
# initArch discovers the architecture for this system. | |
initArch() { | |
ARCH=$(uname -m) | |
case $ARCH in | |
armv5*) ARCH="armv5";; | |
armv6*) ARCH="armv6";; | |
armv7*) ARCH="arm";; | |
aarch64) ARCH="arm64";; | |
x86) ARCH="386";; | |
x86_64) ARCH="amd64";; | |
i686) ARCH="386";; | |
i386) ARCH="386";; | |
esac | |
} | |
# initOS discovers the operating system for this system. | |
initOS() { | |
OS=$(echo `uname`|tr '[:upper:]' '[:lower:]') | |
case "$OS" in | |
# Minimalist GNU for Windows | |
mingw*) OS='windows';; | |
esac | |
} | |
# runs the given command as root (detects if we are root already) | |
runAsRoot() { | |
local CMD="$*" | |
if [ $EUID -ne 0 -a $USE_SUDO = "true" ]; then | |
CMD="sudo $CMD" | |
fi | |
$CMD | |
} | |
# verifySupported checks that the os/arch combination is supported for | |
# binary builds, as well whether or not necessary tools are present. | |
verifySupported() { | |
local supported="darwin-amd64\nlinux-386\nlinux-amd64\nlinux-arm\nlinux-arm64\nlinux-ppc64le\nlinux-s390x\nwindows-amd64" | |
if ! echo "${supported}" | grep -q "${OS}-${ARCH}"; then | |
echo "No prebuilt binary for ${OS}-${ARCH}." | |
echo "To build from source, go to https://github.com/gruntwork-io/terragrunt" | |
exit 1 | |
fi | |
if [ "${HAS_CURL}" != "true" ] && [ "${HAS_WGET}" != "true" ]; then | |
echo "Either curl or wget is required" | |
exit 1 | |
fi | |
} | |
# checkDesiredVersion checks if the desired version is available. | |
checkDesiredVersion() { | |
if [ "x$DESIRED_VERSION" == "x" ]; then | |
# Get tag from release URL | |
local latest_release_url="https://github.com/gruntwork-io/terragrunt/releases" | |
if [ "${HAS_CURL}" == "true" ]; then | |
TAG=$(curl -Ls $latest_release_url | grep 'href="/gruntwork-io/terragrunt/releases/tag/v0.[0-9]*.[0-9]*\"' | grep -v no-underline | head -n 1 | cut -d '"' -f 2 | awk '{n=split($NF,a,"/");print a[n]}' | awk 'a !~ $0{print}; {a=$0}') | |
elif [ "${HAS_WGET}" == "true" ]; then | |
TAG=$(wget $latest_release_url -O - 2>&1 | grep 'href="/gruntwork-io/terragrunt/releases/tag/v0.[0-9]*.[0-9]*\"' | grep -v no-underline | head -n 1 | cut -d '"' -f 2 | awk '{n=split($NF,a,"/");print a[n]}' | awk 'a !~ $0{print}; {a=$0}') | |
fi | |
else | |
TAG=$DESIRED_VERSION | |
fi | |
} | |
# checkTerragruntInstalledVersion checks which version of terragrunt is installed and | |
# if it needs to be changed. | |
checkTerragruntInstalledVersion() { | |
if [[ -f "${TERRAGRUNT_INSTALL_DIR}/${BINARY_NAME}" ]]; then | |
local version=$("${TERRAGRUNT_INSTALL_DIR}/${BINARY_NAME}" --version | awk '{print $3}') | |
if [[ "$version" == "$TAG" ]]; then | |
echo "Terragrunt ${version} is already ${DESIRED_VERSION:-latest}" | |
return 0 | |
else | |
echo "Terragrunt ${TAG} is available. Changing from version ${version}." | |
return 1 | |
fi | |
else | |
return 1 | |
fi | |
} | |
# downloadFile downloads the latest binary | |
downloadFile() { | |
TERRAGRUNT_DIST="terragrunt_${OS}_${ARCH}" | |
DOWNLOAD_URL="https://github.com/gruntwork-io/terragrunt/releases/download/$TAG/$TERRAGRUNT_DIST" | |
TERRAGRUNT_TMP_ROOT="$(mktemp -dt terragrunt-installer-XXXXXX)" | |
TERRAGRUNT_TMP_FILE="$TERRAGRUNT_TMP_ROOT/$TERRAGRUNT_DIST" | |
echo "Downloading $DOWNLOAD_URL" | |
if [ "${HAS_CURL}" == "true" ]; then | |
curl -SsL "$DOWNLOAD_URL" -o "$TERRAGRUNT_TMP_FILE" | |
elif [ "${HAS_WGET}" == "true" ]; then | |
wget -q -O "$TERRAGRUNT_TMP_FILE" "$DOWNLOAD_URL" | |
fi | |
} | |
# installFile installs the Helmfile binary. | |
installFile() { | |
TERRAGRUNT_TMP="$TERRAGRUNT_TMP_ROOT" | |
TERRAGRUNT_TMP_BIN="$TERRAGRUNT_TMP/${BINARY_NAME}_${OS}_${ARCH}" | |
chmod +x "$TERRAGRUNT_TMP_BIN" | |
echo "Preparing to install $BINARY_NAME into ${TERRAGRUNT_INSTALL_DIR}" | |
runAsRoot cp "$TERRAGRUNT_TMP_BIN" "$TERRAGRUNT_INSTALL_DIR/$BINARY_NAME" | |
echo "$BINARY_NAME installed into $TERRAGRUNT_INSTALL_DIR/$BINARY_NAME" | |
} | |
# fail_trap is executed if an error occurs. | |
fail_trap() { | |
result=$? | |
if [ "$result" != "0" ]; then | |
if [[ -n "$INPUT_ARGUMENTS" ]]; then | |
echo "Failed to install $BINARY_NAME with the arguments provided: $INPUT_ARGUMENTS" | |
help | |
else | |
echo "Failed to install $BINARY_NAME" | |
fi | |
echo -e "\tFor support, go to https://github.com/gruntwork-io/terragrunt." | |
fi | |
cleanup | |
exit $result | |
} | |
# testVersion tests the installed client to make sure it is working. | |
testVersion() { | |
set +e | |
TERRAGRUNT="$(command -v $BINARY_NAME)" | |
if [ "$?" = "1" ]; then | |
echo "$BINARY_NAME not found. Is $TERRAGRUNT_INSTALL_DIR on your "'$PATH?' | |
exit 1 | |
fi | |
set -e | |
} | |
# help provides possible cli installation arguments | |
help () { | |
echo "Accepted cli arguments are:" | |
echo -e "\t[--help|-h ] ->> prints this help" | |
echo -e "\t[--version|-v <desired_version>] . When not defined it fetches the latest release from GitHub" | |
echo -e "\te.g. --version v0.28.7" | |
echo -e "\t[--no-sudo] ->> install without sudo" | |
} | |
# cleanup temporary files | |
cleanup() { | |
if [[ -d "${TERRAGRUNT_TMP_ROOT:-}" ]]; then | |
rm -rf "$TERRGRUNT_TMP_ROOT" | |
fi | |
} | |
# Execution | |
#Stop execution on any error | |
trap "fail_trap" EXIT | |
set -e | |
# Set debug if desired | |
if [ "${DEBUG}" == "true" ]; then | |
set -x | |
fi | |
# Parsing input arguments (if any) | |
export INPUT_ARGUMENTS="${@}" | |
set -u | |
while [[ $# -gt 0 ]]; do | |
case $1 in | |
'--version'|-v) | |
shift | |
if [[ $# -ne 0 ]]; then | |
export DESIRED_VERSION="${1}" | |
else | |
echo -e "Please provide the desired version. e.g. --version v0.28.7" | |
exit 0 | |
fi | |
;; | |
'--no-sudo') | |
USE_SUDO="false" | |
;; | |
'--help'|-h) | |
help | |
exit 0 | |
;; | |
*) exit 1 | |
;; | |
esac | |
shift | |
done | |
set +u | |
initArch | |
initOS | |
verifySupported | |
checkDesiredVersion | |
if ! checkTerragruntInstalledVersion; then | |
downloadFile | |
installFile | |
fi | |
testVersion | |
cleanup |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment