From 709902b4b8a7d351ea3f8f0d559b2f58d2db930a Mon Sep 17 00:00:00 2001 From: Clement Chiew Date: Thu, 2 Nov 2023 23:27:08 +0800 Subject: [PATCH] Upload tools --- tdestroy | 28 ++++++++++++++++++++ tplan | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ trefresh | 24 +++++++++++++++++ 3 files changed, 130 insertions(+) create mode 100755 tdestroy create mode 100755 tplan create mode 100755 trefresh diff --git a/tdestroy b/tdestroy new file mode 100755 index 0000000..0a50fac --- /dev/null +++ b/tdestroy @@ -0,0 +1,28 @@ +#!/bin/bash + +set -e + +# Pick tfvars file +TFVAR_COUNT=$( find . -type f -name "*.tfvars" | wc -l ) +if (( TFVAR_COUNT <= 0 )); then + printf "No tfvars file found.\n" + exit 1 +fi +printf "${TFVAR_COUNT} tfvar files found. Pick the number.\n" +FILELIST_OUTPUT=$(find . -type f -name "*.tfvars") +printf "${FILELIST_OUTPUT}\n" | nl +read -p "Pick file number (1): " TFVAR_PICK +if (( TFVAR_PICK > 0 )) && (( TFVAR_PICK <= TFVAR_COUNT )); then + : +else + printf "Pick is invalid\n" && exit 1 +fi + +# Get picked file +TFVAR_PICK_FILE=$( printf "${FILELIST_OUTPUT}\n" | head -n "${TFVAR_PICK}" | tail -1 ) + +if [[ $1 == '-y' ]]; then + terraform destroy -var-file="${TFVAR_PICK_FILE}" <<< 'yes' +else + terraform destroy -var-file="${TFVAR_PICK_FILE}" +fi diff --git a/tplan b/tplan new file mode 100755 index 0000000..46aa26d --- /dev/null +++ b/tplan @@ -0,0 +1,78 @@ +#!/bin/bash + +set -e + +# In case plans directory is not there +[[ -d plans ]] || mkdir plans + +# Prepare plan variables +PLAN_NAME="$( gdate --iso-8601=minutes )-plan" +PLAN_FILE="plans/${PLAN_NAME}" + +# Compress old plans +find plans -type f ! -name "*.gz" -execdir gzip -f "{}" \; + +# Pick tfvars file +TFVAR_COUNT=$( find . -type f -name "*.tfvars" | wc -l ) +if (( TFVAR_COUNT <= 0 )); then + printf "No tfvars file found.\n" + exit 1 +fi +printf "${TFVAR_COUNT} tfvar files found. Pick the number.\n" +FILELIST_OUTPUT=$(find . -type f -name "*.tfvars") +printf "${FILELIST_OUTPUT}\n" | nl +read -rp "Pick file number (1): " TFVAR_PICK +if (( TFVAR_PICK > 0 )) && (( TFVAR_PICK <= TFVAR_COUNT )); then + : +else + printf "Pick is invalid\n" && exit 1 +fi + +# Get picked file +TFVAR_PICK_FILE=$( printf "${FILELIST_OUTPUT}\n" | head -n "${TFVAR_PICK}" | tail -1 ) + +# Autoformat before execution +terraform fmt && + find . -type f \( -name "*.tf" -o -name "*.tfvars" \) -exec terraform fmt "{}" \; || exit + +# Validate configuration +terraform validate . + +# Upgrade first to handle module changes +terraform init -upgrade || exit 1 + +if (( TFVAR_COUNT > 0 ));then + terraform plan -out "${PLAN_FILE}" -var-file="${TFVAR_PICK_FILE}" +else + terraform plan -out "${PLAN_FILE}" +fi + +# Check if Terraform plan failed +if ! (( $? == 0 )); then + printf "Please check your Terraform files.\n" + exit 1 +fi + +printf "Apply? terraform apply \"${PLAN_FILE}\"\n" + +apply_plan_file_auto_approve () { + mv "${PLAN_FILE}" "${PLAN_FILE}-applied" + PLAN_FILE="${PLAN_FILE}-applied" + terraform apply -auto-approve "${PLAN_FILE}" +} + +apply_plan_file_approve () { + mv "${PLAN_FILE}" "${PLAN_FILE}-applied" + PLAN_FILE="${PLAN_FILE}-applied" + terraform apply "${PLAN_FILE}" +} + +if [[ $1 == '-y' ]]; then + apply_plan_file_auto_approve + exit +fi + +read -rp "(y/n): " ANSWER +if [[ $ANSWER == 'y' ]]; then + apply_plan_file_approve +fi diff --git a/trefresh b/trefresh new file mode 100755 index 0000000..a813e2b --- /dev/null +++ b/trefresh @@ -0,0 +1,24 @@ +#!/bin/bash + +set -e + +# Pick tfvars file +TFVAR_COUNT=$( find . -type f -name "*.tfvars" | wc -l ) +if (( TFVAR_COUNT <= 0 )); then + printf "No tfvars file found.\n" + exit 1 +fi +printf "${TFVAR_COUNT} tfvar files found. Pick the number.\n" +FILELIST_OUTPUT=$(find . -type f -name "*.tfvars") +printf "${FILELIST_OUTPUT}\n" | nl +read -p "Pick file number (1): " TFVAR_PICK +if (( TFVAR_PICK > 0 )) && (( TFVAR_PICK <= TFVAR_COUNT )); then + : +else + printf "Pick is invalid\n" && exit 1 +fi + +# Get picked file +TFVAR_PICK_FILE=$( printf "${FILELIST_OUTPUT}\n" | head -n "${TFVAR_PICK}" | tail -1 ) + +terraform plan -refresh-only -var-file="${TFVAR_PICK_FILE}"