Skip to content

Instantly share code, notes, and snippets.

@checco
Last active October 21, 2023 08:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save checco/5807e92d6fbd59424c2114e11bfdeadc to your computer and use it in GitHub Desktop.
Save checco/5807e92d6fbd59424c2114e11bfdeadc to your computer and use it in GitHub Desktop.
[deprecated] Bash script to upgrade Terraform to the latest minor or major release on Linux
#!/bin/bash
url="https://releases.hashicorp.com/terraform"
latest_version=`curl -s ${url}/ | grep -A6 "<a href=\"../\">../</a>" | grep terraform | cut -d '_' -f 2 | cut -d '<' -f 1`
bin="/usr/local/bin/terraform"
install () {
cd /tmp && \
/bin/wget -q ${url}/${latest_version}/terraform_${latest_version}_linux_amd64.zip && \
/bin/unzip -qq terraform_${latest_version}_linux_amd64.zip
sudo cp terraform ${bin} && sudo cp terraform ${bin}-${latest_version}
}
install_minor () {
cd /tmp && \
/bin/wget -q ${url}/${minor_version}/terraform_${minor_version}_linux_amd64.zip && \
/bin/unzip -qq terraform_${minor_version}_linux_amd64.zip
sudo cp terraform ${bin} && sudo cp terraform ${bin}-${minor_version}
}
if [ ! -f "${bin}" ]; then
echo "Installing Terraform v${latest_version}"
install && echo "Terraform v${latest_version} installed"
else
cur_version=`${bin} version | head -1 | cut -d 'v' -f 2`
minor_version=`curl -s ${url}/ | grep ${cur_version:0:4} | head -1 | cut -d '_' -f 2 | cut -d '<' -f 1`
echo "You're using Terraform ${cur_version}"
if [ "${minor_version}" != "${cur_version}" ]; then
echo "A new Terraform minor release is available: v${minor_version}"
echo -n "Do you want to install this new minor release (y/n)? "
read answer
if [ "$answer" != "${answer#[Yy]}" ]; then
install_minor && echo "Terraform updated to the latest release"
else
echo "Skipped the Terraform update to v${minor_version} release"
fi
fi
if [ "${latest_version}" != "${cur_version}" ]; then
echo "A new Terraform release is available: v${latest_version}"
echo -n "Do you want to install this new release (y/n)? "
read answer
if [ "$answer" != "${answer#[Yy]}" ]; then
install && echo "Terraform updated to the latest release"
else
echo "Skipped the Terraform update to v${latest_version} release"
fi
else
echo "You're already running the latest release"
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment