-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.sh
More file actions
executable file
·94 lines (78 loc) · 2.79 KB
/
Copy pathsetup.sh
File metadata and controls
executable file
·94 lines (78 loc) · 2.79 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
#!/bin/bash
# This script sets up the environment for Cloud Shell and applies the Terraform script.
#usage: bash setup.sh
# Exit immediately if a command exits with a non-zero status.
set -e
echo "Setting up the environment..."
# Check and set PROJECT_ID
if [ -n "$DEVSHELL_PROJECT_ID" ]; then
export PROJECT_ID="$DEVSHELL_PROJECT_ID"
echo "Using Cloud Shell Project ID: '$PROJECT_ID'"
elif [ -z "$PROJECT_ID" ]; then
echo "Error: PROJECT_ID is not set. Please set the PROJECT_ID environment variable or run from Google Cloud Console."
exit 1
fi
echo "Using Project ID: '$PROJECT_ID'"
echo 'project_id = "'"$PROJECT_ID"'"' > terraform.tfvars
api_array=(
"compute.googleapis.com"
"storage-api.googleapis.com"
)
for api in "${api_array[@]}";
do
echo "Enabling API: $api"
gcloud services enable "$api" --project="$DEVSHELL_PROJECT_ID"
done
# Check the Terraform version and updates if needed. Based on jacouh's script https://stackoverflow.com/questions/48491662/comparing-two-version-numbers-in-a-shell-script
version=$(terraform -v | grep -Eo -m 1 '[0-9]+\.[0-9]+\.[0-9]+')
expected_version=1.11.0
IFS="." read -ra xarr <<< "$version"
IFS="." read -ra yarr <<< "$expected_version"
current=true
for i in "${!xarr[@]}"; do
if [ "${xarr[i]}" -ge "${yarr[i]}" ]; then
current=true
elif [ "${xarr[i]}" -lt "${yarr[i]}" ]; then
current=false
break
fi
done
if [[ $current == false ]]; then
read -rp "Terraform version is less than 1.11.0. Do you want to update it? (y/n) " update_response
if [[ "$update_response" != "y" ]]; then
echo "Exiting without updating Terraform."
exit 1
else
echo "Updating Terraform..."
if ! {
wget -O - https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
sudo apt update && sudo apt install terraform
}; then
echo "Error during Terraform update. Exiting..."
exit 1
fi
echo "Terraform updated successfully."
fi
fi
# Many thanks to Praneeth Bilakanti https://praneethreddybilakanti.medium.com/terraform-with-shell-scripts-e6007f975a90 for this bit! :)
echo "Running Terraform init..."
if ! terraform init; then
echo "Error during init. Exiting..."
exit 1
fi
echo "Running Terraform plan..."
if ! terraform plan; then
echo "Error during plan. Exiting..."
exit 1
fi
read -rp "Do you want to apply the changes? (y/n) " response
if [[ "$response" == "y" ]]; then
if ! terraform apply --auto-approve; then
echo "Error during apply. Exiting..."
exit 1
fi
echo "Terraform apply completed successfully."
else
echo "Apply canceled."
fi