Skip to content

Instantly share code, notes, and snippets.

@MartinNowak
Last active November 29, 2021 19:03
Show Gist options
  • Save MartinNowak/522125bd7f7e82397dc4d5882581c593 to your computer and use it in GitHub Desktop.
Save MartinNowak/522125bd7f7e82397dc4d5882581c593 to your computer and use it in GitHub Desktop.
script to update terraform binary to (latest) version
#!/usr/bin/env bash
set -ueo pipefail
function cleanup {
rm -rf "$TMP"
}
TMP=$(mktemp -d)
trap cleanup EXIT
cd "$TMP"
VERSIONS=($(curl -fsSL https://releases.hashicorp.com/terraform/ | sed -n 's|\s*<a href="/terraform/\(.*\)/">.*|\1|p' | sort --version-sort --reverse))
if [ $# -gt 0 ]; then
VER=$1
if ! [[ " ${VERSIONS[@]} " =~ " ${VER} " ]]; then
echo "Selected version '$VER' not found in available versions ${VERSIONS[@]}" >&2
exit 1
fi
else
VER=${VERSIONS[0]}
echo "Installing latest version $VER"
fi
mkdir -m=0700 gpg
gpg --quiet --homedir gpg --recv-key 'C874 011F 0AB4 0511 0D02 1055 3436 5D94 72D7 468F'
wget --quiet https://releases.hashicorp.com/terraform/$VER/terraform_${VER}_SHA256SUMS
wget --quiet https://releases.hashicorp.com/terraform/$VER/terraform_${VER}_SHA256SUMS.sig
if ! out=$(gpg --quiet --homedir gpg --verify terraform_${VER}_SHA256SUMS.sig 2>&1); then
echo "$out" >&2
exit 1
fi
wget --quiet https://releases.hashicorp.com/terraform/$VER/terraform_${VER}_linux_amd64.zip
sha256sum --quiet -c <(grep -F linux_amd64.zip terraform_${VER}_SHA256SUMS)
unzip -q terraform_${VER}_linux_amd64.zip terraform
if ! which terraform 2>/dev/null; then
mkdir -p $HOME/.local/bin/
mv terraform $HOME/.local/bin/
elif [[ "$(which terraform)" = /usr/* ]]; then
sudo mv terraform $(which terraform)
else
mv terraform $(which terraform)
fi
terraform --version
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment