Last active
April 23, 2020 19:14
-
-
Save kshcherban/c3b21e9c7caab4aeb3308bf1d267d5e1 to your computer and use it in GitHub Desktop.
Bash script to auto update terraform on linux
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
#!/bin/bash | |
INSTALL_DIR="${1:-/opt/terraform}" | |
URL="https://releases.hashicorp.com/terraform" | |
VER="$(curl -sL $URL | grep -v beta | grep -Po "_(\d*\.?){3}" | sed 's/_//' | sort -V | tail -1)" | |
ZIP="terraform_${VER}_linux_amd64.zip" | |
echo "* Downloading ${URL}/${VER}/terraform_${VER}_linux_amd64.zip" | |
curl -s ${URL}/${VER}/terraform_${VER}_linux_amd64.zip -o ${INSTALL_DIR}/${ZIP} | |
echo "* Extracting $ZIP into $INSTALL_DIR" | |
unzip -o ${INSTALL_DIR}/$ZIP -d $INSTALL_DIR && rm -v ${INSTALL_DIR}/$ZIP |
This should work with Mac and Linux:
latest_version="$(curl -sL "https://releases.hashicorp.com/terraform" | grep -v beta | grep '<a href="'.*terraform | cut -d'/' -f3 | sort -V | tail -1)"
My mac's version of sort did not have -V so I used this.
curl -sL "https://releases.hashicorp.com/terraform" | grep -v beta | grep -v rc1 | grep '<a href="'.*terraform | cut -d'/' -f3 | sort -n | head -n 1
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Modified to work with OSX's grep and sort
update_terraform() {
INSTALL_DIR="$(dirname $(which terraform))"
URL="https://releases.hashicorp.com/terraform"
current_version=$($(which terraform) -v | awk '{print $2}'| sed s/v//)
latest_version="$(curl -sL $URL | grep -v beta | egrep -Eo "(\d*.?){3}" | sed 's///' | sort -n | tail -1)"
if [[ $current_version == $latest_version ]]; then
echo "[+] Already up to date"
return 0
fi
ZIP="terraform_${latest_version}darwin_amd64.zip"
echo "[+] Downloading ${URL}/${latest_version}/terraform${latest_version}darwin_amd64.zip"
curl --progress-bar ${URL}/${latest_version}/terraform${latest_version}_darwin_amd64.zip -o ${INSTALL_DIR}/${ZIP}
echo "[+] Extracting $ZIP into $INSTALL_DIR"
unzip -o ${INSTALL_DIR}/$ZIP -d $INSTALL_DIR
echo "[+] Deleting temporary zip"
rm -v ${INSTALL_DIR}/$ZIP
echo "[+] updated to $latest_version"
}