-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterraform_update.sh
executable file
·68 lines (51 loc) · 2.17 KB
/
terraform_update.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/bin/bash
# Terraform Update Script
# Description: This script updates Terraform to the latest version for Linux (amd64).
# If necessary add script to cron
TERRAFORM_BIN="/usr/local/bin/terraform"
GITHUB_API_URL="https://api.github.com/repos/hashicorp/terraform/releases/latest"
TMP_DIR="/tmp"
if ! command -v curl &> /dev/null; then
echo "curl is required but not installed. Please install curl!"
exit 1
fi
if ! command -v unzip &> /dev/null; then
echo "unzip is required but not installed. Please install unzip!"
exit 1
fi
LATEST_VERSION=$(curl -s $GITHUB_API_URL | grep '"tag_name":' | sed -E 's/.*"v([^"]+)".*/\1/')
if [ -z "$LATEST_VERSION" ]; then
echo "Failed to fetch the latest version information. Exiting."
exit 1
fi
DOWNLOAD_URL="https://releases.hashicorp.com/terraform/$LATEST_VERSION/terraform_${LATEST_VERSION}_linux_amd64.zip"
if [ -f "$TERRAFORM_BIN" ]; then
CURRENT_VERSION=$($TERRAFORM_BIN -v | head -n 1 | sed 's/Terraform v//')
else
CURRENT_VERSION="0.0.0"
fi
if [ "$CURRENT_VERSION" == "$LATEST_VERSION" ]; then
echo "Terraform is already up-to-date (v$LATEST_VERSION)."
exit 0
fi
echo "Downloading Terraform v$LATEST_VERSION to $TMP_DIR..."
curl -Lo "$TMP_DIR/terraform_${LATEST_VERSION}_linux_amd64.zip" "$DOWNLOAD_URL" || { echo "Failed to download Terraform. Exiting."; exit 1; }
if [ ! -f "$TMP_DIR/terraform_${LATEST_VERSION}_linux_amd64.zip" ]; then
echo "Download failed or file not found. Exiting."
exit 1
fi
echo "Unzipping the Terraform archive..."
unzip -o "$TMP_DIR/terraform_${LATEST_VERSION}_linux_amd64.zip" -d "$TMP_DIR" || { echo "Failed to unzip the file. Exiting."; exit 1; }
echo "Installing Terraform..."
sudo mv "$TMP_DIR/terraform" "$TERRAFORM_BIN" || { echo "Failed to move the new Terraform binary to $TERRAFORM_BIN. Exiting."; exit 1; }
sudo chmod +x "$TERRAFORM_BIN"
echo "Cleaning up temporary files..."
rm "$TMP_DIR/terraform_${LATEST_VERSION}_linux_amd64.zip"
NEW_VERSION=$($TERRAFORM_BIN -v | head -n 1 | sed 's/Terraform v//')
if [ "$NEW_VERSION" == "$LATEST_VERSION" ]; then
echo "Terraform successfully updated to v$NEW_VERSION."
else
echo "Failed to update Terraform."
exit 1
fi
exit 0