Skip to content

Instantly share code, notes, and snippets.

@kshcherban
Last active April 23, 2020 19:14
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kshcherban/c3b21e9c7caab4aeb3308bf1d267d5e1 to your computer and use it in GitHub Desktop.
Save kshcherban/c3b21e9c7caab4aeb3308bf1d267d5e1 to your computer and use it in GitHub Desktop.
Bash script to auto update terraform on linux
#!/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
Copy link

ghost commented Mar 2, 2017

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"
}

Copy link

ghost commented Dec 29, 2017

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)"

@cbitter78
Copy link

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