Skip to content

Commit

Permalink
Feature: implement plan/apply/destroy method
Browse files Browse the repository at this point in the history
  • Loading branch information
mostafa6765 committed Sep 2, 2024
1 parent 861ba61 commit e3d8654
Showing 1 changed file with 67 additions and 16 deletions.
83 changes: 67 additions & 16 deletions run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ function get_json_value() {
# 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"
Expand All @@ -20,7 +24,7 @@ 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'."
echo "Error: Invalid environment '$ENVIRONMENT'. Please run the script again and choose a valid environment."
exit 1
fi

Expand All @@ -30,30 +34,77 @@ 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
cd "$TG_WORKDIR" || { echo "Error: Directory '$TG_WORKDIR' does not exist."; exit 1; }
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 "Initializing Terragrunt..."
echo -e "\n=== Initializing Terragrunt ==="
echo "Running: terragrunt init --terragrunt-non-interactive"
terragrunt init --terragrunt-non-interactive


# Validate the selected Terragrunt workspace
if ! terragrunt workspace list | grep -q "$TF_WORKSPACE"; then
echo "Error: Invalid Terragrunt workspace '$TF_WORKSPACE'."
exit 1
fi

# Select the Terragrunt workspace
echo "Selecting Terragrunt workspace: $TF_WORKSPACE"
terragrunt workspace select "$TF_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 Terragrunt plan
echo "Running Terragrunt plan..."
terragrunt plan --terragrunt-non-interactive
# 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 "Terragrunt plan completed successfully!"
echo -e "\n=== Operation Complete ==="
echo "Terragrunt $ACTION completed successfully!"
echo "Thank you for using the Terragrunt Management Script."

0 comments on commit e3d8654

Please sign in to comment.