From 0052bfbfc253f20b448be5cb2ef4909f5be1d1b3 Mon Sep 17 00:00:00 2001 From: Mostafa Kamal Date: Mon, 2 Sep 2024 23:13:29 +0600 Subject: [PATCH] added run script --- .gitignore | 7 +++--- config.json | 4 --- run.sh | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+), 7 deletions(-) create mode 100755 run.sh diff --git a/.gitignore b/.gitignore index 974c819..460bf58 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ -terraform/*/modules -terraform/*/common-resources.tf -!terraform/common/common-resources.tf +projects/*/modules +projects/*/common-resources.tf +!projects/common/common-resources.tf +auto_generated_backend.tf # Terragrunt specific .terraform diff --git a/config.json b/config.json index f80c068..92c8629 100644 --- a/config.json +++ b/config.json @@ -13,10 +13,6 @@ "dev": { "TF_WORKSPACE": "dev-project", "TG_WORKDIR": "environment/dev" - }, - "default": { - "TF_WORKSPACE": "default", - "TG_WORKDIR": "environment/default" } } } diff --git a/run.sh b/run.sh new file mode 100755 index 0000000..a28559e --- /dev/null +++ b/run.sh @@ -0,0 +1,72 @@ +#!/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" + +# Prompt user to pick an environment +echo "Available environments:" +environments=($(jq -r '.branches | keys | .[]' "$CONFIG_FILE")) +for i in "${!environments[@]}"; do + echo "$((i+1)). ${environments[$i]}" +done + +read -p "Select an environment by number: " env_num + +# Validate the selected environment +if ! [[ "$env_num" =~ ^[0-9]+$ ]] || (( env_num < 1 || env_num > ${#environments[@]} )); then + echo "Error: Invalid selection." + exit 1 +fi + +ENVIRONMENT="${environments[$((env_num-1))]}" + +# 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 "Selected environment: $ENVIRONMENT" +echo "Terraform Workspace: $TF_WORKSPACE" +echo "Terragrunt Working Directory: $TG_WORKDIR" + +# Change directory to the Terragrunt working directory +cd "$TG_WORKDIR" || { echo "Error: Directory '$TG_WORKDIR' does not exist."; exit 1; } + +# Initialize Terraform with Terragrunt +echo "Initializing Terragrunt..." +terragrunt init --terragrunt-non-interactive + +# Prompt user to select a Terragrunt workspace +echo "Available Terragrunt workspaces:" +workspaces=($(terragrunt workspace list | sed 's/^[* ]*//')) +for i in "${!workspaces[@]}"; do + echo "$((i+1)). ${workspaces[$i]}" +done + +read -p "Select a Terragrunt workspace by number: " ws_num + +# Validate the selected Terragrunt workspace +if ! [[ "$ws_num" =~ ^[0-9]+$ ]] || (( ws_num < 1 || ws_num > ${#workspaces[@]} )); then + echo "Error: Invalid selection." + exit 1 +fi + +TG_WORKSPACE="${workspaces[$((ws_num-1))]}" + +# Select the Terragrunt workspace +echo "Selecting Terragrunt workspace: $TG_WORKSPACE" +terragrunt workspace select "$TG_WORKSPACE" + +# Run Terragrunt plan +echo "Running Terragrunt plan..." +terragrunt plan --terragrunt-non-interactive + +echo "Terragrunt plan completed successfully!"