Skip to content

Instantly share code, notes, and snippets.

@rammanokar
Last active April 20, 2020 12:53
Show Gist options
  • Save rammanokar/614e7554d68f21ad311a17eb1339e4ea to your computer and use it in GitHub Desktop.
Save rammanokar/614e7554d68f21ad311a17eb1339e4ea to your computer and use it in GitHub Desktop.
Automatic update Terraform
#!/usr/bin/env bash
#Defining Terrafrom Directory
INSTALL_DIR="/opt/terraform"
#Terrafrom URL
URL="https://releases.hashicorp.com/terraform"
#Getting local version
LOCAL_VER="0"
if [ -x "$(command -v terraform)" ]; then
LOCAL_VER=$(terraform -version | grep -v "+" | awk -Fv '{print $2}')
fi
#Getting Online Version
VER="$(curl -sL $URL | grep -v beta | grep -Po "_(\d*\.?){3}" | sed 's/_//' | sort -V | tail -1)"
#Defining Download file name
ZIP="terraform_${VER}_linux_amd64.zip"
if [ "$LOCAL_VER" = "$VER" ]; then
echo "Terraform v$VER is alreay up to date"
elif [ "$EUID" -ne 0 ]; then
echo "Please run as root"
echo "Terraform v$VER update is available for update"
else
mkdir -p "$INSTALL_DIR"
echo "Currently installed Terrafrom v\"$LOCAL_VER\" is out of date. "
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"
ln -sf "${INSTALL_DIR}"/terraform /usr/bin/ #Creating Symbolc link in /usr/bin
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment