-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall_terraform.sh
More file actions
executable file
Β·162 lines (133 loc) Β· 5.63 KB
/
install_terraform.sh
File metadata and controls
executable file
Β·162 lines (133 loc) Β· 5.63 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/usr/bin/env bash
set -e
echo "π§ Terraform Installation Script"
echo "================================"
OS="$(uname -s)"
# Function to verify installation
verify_installation() {
echo "π Verifying installation..."
if command -v terraform &> /dev/null; then
echo "β
Terraform successfully installed!"
echo "π Version: $(terraform version | head -n1)"
echo ""
echo "π Next steps:"
echo "1. Run 'terraform init' in a project directory to initialize"
echo "2. Run 'terraform --help' to see available commands"
echo ""
echo "π Documentation: https://www.terraform.io/docs"
else
echo "β Installation failed. Terraform not found in PATH."
exit 1
fi
}
case "$OS" in
Linux*)
echo "π§ Detected Linux system"
# Detect distribution
if [[ -f /etc/os-release ]]; then
. /etc/os-release
DISTRO=$ID
else
echo "β Cannot detect Linux distribution"
exit 1
fi
echo "π Distribution: $DISTRO"
case $DISTRO in
ubuntu|debian|pop|linuxmint|elementary)
echo "π¦ Installing Terraform on Ubuntu/Debian..."
# Install dependencies
if ! command -v wget &> /dev/null; then
sudo apt update
sudo apt install -y wget
fi
# Add HashiCorp repository
wget -O - https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
# Determine the codename
if [[ -n "${UBUNTU_CODENAME:-}" ]]; then
CODENAME=$UBUNTU_CODENAME
elif command -v lsb_release &> /dev/null; then
CODENAME=$(lsb_release -cs)
else
CODENAME=$(grep VERSION_CODENAME /etc/os-release | cut -d= -f2)
fi
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $CODENAME main" | \
sudo tee /etc/apt/sources.list.d/hashicorp.list
# Update and install
sudo apt update
sudo apt install -y terraform
;;
rhel|centos|fedora|rocky|almalinux)
echo "π¦ Installing Terraform on RHEL/Fedora..."
# Add HashiCorp repository
if command -v dnf &> /dev/null; then
sudo dnf install -y dnf-plugins-core
sudo dnf config-manager --add-repo https://rpm.releases.hashicorp.com/fedora/hashicorp.repo
sudo dnf install -y terraform
else
sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo
sudo yum install -y terraform
fi
;;
arch|manjaro|endeavouros)
echo "π¦ Installing Terraform on Arch Linux..."
sudo pacman -Sy terraform --noconfirm
;;
alpine)
echo "π¦ Installing Terraform on Alpine Linux..."
sudo apk add terraform
;;
*)
echo "β οΈ Distribution not directly supported, installing via binary..."
# Detect architecture
case $(uname -m) in
x86_64) ARCH="amd64" ;;
aarch64) ARCH="arm64" ;;
armv7l) ARCH="arm" ;;
i386|i686) ARCH="386" ;;
*) echo "β Unsupported architecture"; exit 1 ;;
esac
# Get latest version
LATEST_VERSION=$(curl -s https://api.github.com/repos/hashicorp/terraform/releases/latest | grep -o '"tag_name": "v[^"]*' | grep -o 'v[^"]*' | sed 's/v//')
if [[ -z "$LATEST_VERSION" ]]; then
echo "β Failed to fetch latest version"
exit 1
fi
echo "π₯ Downloading Terraform ${LATEST_VERSION}..."
cd /tmp
wget "https://releases.hashicorp.com/terraform/${LATEST_VERSION}/terraform_${LATEST_VERSION}_linux_${ARCH}.zip"
# Install unzip if not present
if ! command -v unzip &> /dev/null; then
if command -v apt &> /dev/null; then
sudo apt install -y unzip
elif command -v yum &> /dev/null; then
sudo yum install -y unzip
elif command -v apk &> /dev/null; then
sudo apk add unzip
fi
fi
unzip "terraform_${LATEST_VERSION}_linux_${ARCH}.zip"
sudo mv terraform /usr/local/bin/
sudo chmod +x /usr/local/bin/terraform
rm -f "terraform_${LATEST_VERSION}_linux_${ARCH}.zip"
;;
esac
;;
Darwin*)
echo "π Detected macOS system"
# Check if Homebrew is installed
if ! command -v brew &> /dev/null; then
echo "β Homebrew is required but not installed."
echo "Install it from https://brew.sh/"
exit 1
fi
echo "π¦ Installing Terraform via Homebrew..."
brew tap hashicorp/tap
brew install hashicorp/tap/terraform
;;
*)
echo "β Unsupported operating system: $OS"
exit 1
;;
esac
verify_installation