-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.sh
executable file
·110 lines (95 loc) · 3.57 KB
/
run.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
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
#!/bin/bash
# Function to read JSON values
function get_json_value() {
local key="$1"
local json_file="$2"
echo $(jq -r "$key" "$json_file")
}
# File path to config.json
CONFIG_FILE="./config.json"
echo "=== Terragrunt Management Script ==="
echo "This script helps manage Terragrunt operations across different environments."
# Prompt user to pick an environment
echo -e "\n=== Environment Selection ==="
echo "Available environments:"
for env in $(jq -r '.branches | keys | .[]' "$CONFIG_FILE"); do
echo " - $env"
done
read -p "Select an environment: " ENVIRONMENT
# Validate the selected environment
if ! jq -e ".branches.$ENVIRONMENT" "$CONFIG_FILE" > /dev/null; then
echo "Error: Invalid environment '$ENVIRONMENT'. Please run the script again and choose a valid environment."
exit 1
fi
# Extract configurations from config.json
TF_WORKSPACE=$(get_json_value ".branches.$ENVIRONMENT.TF_WORKSPACE" "$CONFIG_FILE")
TG_WORKDIR=$(get_json_value ".branches.$ENVIRONMENT.TG_WORKDIR" "$CONFIG_FILE")
TF_VERSION=$(get_json_value ".terraform_version" "$CONFIG_FILE")
TG_VERSION=$(get_json_value ".terragrunt_version" "$CONFIG_FILE")
echo -e "\n=== Configuration Details ==="
echo "Selected environment: $ENVIRONMENT"
echo "Terraform Workspace: $TF_WORKSPACE"
echo "Terragrunt Working Directory: $TG_WORKDIR"
echo "Terraform Version: $TF_VERSION"
echo "Terragrunt Version: $TG_VERSION"
# Prompt user for action
echo -e "\n=== Action Selection ==="
read -p "Select action (plan/apply/destroy): " ACTION
case $ACTION in
plan|apply)
echo -e "\n=== Symlinking Modules ==="
echo "Creating symbolic links for modules..."
./symlink-modules.sh
echo "Symlinking completed."
;;
destroy)
echo -e "\n=== Skipping Symlinking ==="
echo "Symlinking is not required for destroy action."
;;
*)
echo "Error: Invalid action '$ACTION'. Please run the script again and choose plan, apply, or destroy."
exit 1
;;
esac
# Change directory to the Terragrunt working directory
echo -e "\n=== Changing to Terragrunt Working Directory ==="
echo "Navigating to: $TG_WORKDIR"
cd "$TG_WORKDIR" || { echo "Error: Directory '$TG_WORKDIR' does not exist. Please check your configuration."; exit 1; }
# Initialize Terraform with Terragrunt
echo -e "\n=== Initializing Terragrunt ==="
echo "Running: terragrunt init --terragrunt-non-interactive"
terragrunt init --terragrunt-non-interactive
# Select the Terragrunt workspace
echo -e "\n=== Selecting Terragrunt Workspace ==="
echo "Attempting to select workspace: $TF_WORKSPACE"
if terragrunt workspace select "$TF_WORKSPACE"; then
echo "Workspace '$TF_WORKSPACE' selected successfully."
else
echo "Workspace '$TF_WORKSPACE' not found. Creating new workspace..."
terragrunt workspace new "$TF_WORKSPACE"
echo "New workspace '$TF_WORKSPACE' created and selected."
fi
# Run the selected Terragrunt action
echo -e "\n=== Executing Terragrunt $ACTION ==="
echo "Running: terragrunt $ACTION --terragrunt-non-interactive"
case $ACTION in
plan)
terragrunt plan --terragrunt-non-interactive
;;
apply)
terragrunt apply --terragrunt-non-interactive
;;
destroy)
echo "Warning: You are about to destroy resources. This action is irreversible."
read -p "Are you sure you want to proceed with destroy? (yes/no): " CONFIRM
if [ "$CONFIRM" = "yes" ]; then
terragrunt destroy --terragrunt-non-interactive
else
echo "Destroy action cancelled."
exit 0
fi
;;
esac
echo -e "\n=== Operation Complete ==="
echo "Terragrunt $ACTION completed successfully!"
echo "Thank you for using the Terragrunt Management Script."