Skip to content

Instantly share code, notes, and snippets.

@apolloclark
Last active May 16, 2020 18:27
Show Gist options
  • Save apolloclark/d7ebecf30ee0694ffd931d0296f8528d to your computer and use it in GitHub Desktop.
Save apolloclark/d7ebecf30ee0694ffd931d0296f8528d to your computer and use it in GitHub Desktop.
Bash scripts to upgrade locally installed HashiCorp programs
#!/bin/bash -eu
PACKAGE_LIST="terraform packer sentinel vagrant";
for PACKAGE in $PACKAGE_LIST; do
# retrieve a link to the latest version of Terraform
VERSION_LATEST=$(curl -sSLk https://releases.hashicorp.com/index.json \
| jq ".${PACKAGE}.versions | keys | .[]" | tr -d '"' \
| grep -v 'alpha\|beta\|rc\|oci' | sort --version-sort | tail -n1);
# get the currently installed version
VERSION_CURRENT=$(${PACKAGE} -v | grep -Po "([0-9]|\.)*");
# echo "${VERSION_CURRENT}";
# check if the latest version matches the currently installed version
if [ "$VERSION_LATEST" = "$VERSION_CURRENT" ]; then
echo "Already running the latest version of ${PACKAGE} == ${VERSION_CURRENT}"
continue;
fi
# generate the tfe download URL
URL=$(echo 'https://releases.hashicorp.com/'${PACKAGE}'/'"${VERSION_LATEST}"'/'${PACKAGE}'_'"${VERSION_LATEST}"'_linux_amd64.zip')
echo $URL;
# get the file, install it
cd /tmp
wget -q "${URL}"
unzip ./${PACKAGE}_*.zip
mv ./${PACKAGE} ~/bin
${PACKAGE} --version | grep -F "${VERSION_LATEST}"
rm -rf /tmp/${PACKAGE}*
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment