Updates
This commit is contained in:
53
tplan
53
tplan
@ -1,19 +1,27 @@
|
|||||||
#!/bin/bash
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
set -e
|
set -e
|
||||||
|
|
||||||
# In case plans directory is not there
|
# In case plans directory is not there
|
||||||
[[ -d plans ]] || mkdir plans
|
PLAN_FOLDER=".plans"
|
||||||
|
[[ -d $PLAN_FOLDER ]] || mkdir -p "${PLAN_FOLDER}"
|
||||||
|
|
||||||
# Prepare plan variables
|
# Prepare plan variables
|
||||||
PLAN_NAME="$( gdate --iso-8601=minutes )-plan"
|
if type -t gdate >/dev/null ; then
|
||||||
PLAN_FILE="plans/${PLAN_NAME}"
|
# GNU date on MacOS is gdate
|
||||||
|
PLAN_NAME="$( gdate --iso-8601=minutes )-plan"
|
||||||
|
else
|
||||||
|
# Fall back to Linux date
|
||||||
|
PLAN_NAME="$( date --iso-8601=minutes )-plan"
|
||||||
|
fi
|
||||||
|
PLAN_FILE="${PLAN_FOLDER}/${PLAN_NAME}"
|
||||||
|
|
||||||
# Compress old plans
|
# Compress old plans
|
||||||
find plans -type f ! -name "*.gz" -execdir gzip -f "{}" \;
|
find "${PLAN_FOLDER}" -type f ! -name "*.gz" -execdir gzip -f "{}" \; || exit
|
||||||
|
|
||||||
# Pick tfvars file
|
# Pick tfvars file
|
||||||
TFVAR_COUNT=$( find . -type f -name "*.tfvars" | wc -l )
|
readarray -t FILELIST <<<$( find . -type f -name "*.tfvars" )
|
||||||
|
TFVAR_COUNT=${#FILELIST[@]}
|
||||||
if (( TFVAR_COUNT <= 0 )); then
|
if (( TFVAR_COUNT <= 0 )); then
|
||||||
printf "No tfvars file found.\n"
|
printf "No tfvars file found.\n"
|
||||||
exit 1
|
exit 1
|
||||||
@ -23,56 +31,45 @@ FILELIST_OUTPUT=$(find . -type f -name "*.tfvars")
|
|||||||
printf "${FILELIST_OUTPUT}\n" | nl
|
printf "${FILELIST_OUTPUT}\n" | nl
|
||||||
read -rp "Pick file number (1): " TFVAR_PICK
|
read -rp "Pick file number (1): " TFVAR_PICK
|
||||||
if (( TFVAR_PICK > 0 )) && (( TFVAR_PICK <= TFVAR_COUNT )); then
|
if (( TFVAR_PICK > 0 )) && (( TFVAR_PICK <= TFVAR_COUNT )); then
|
||||||
:
|
TFVAR_PICK_FILE=${FILELIST[$(( TFVAR_PICK++ ))]}
|
||||||
else
|
else
|
||||||
printf "Pick is invalid\n" && exit 1
|
printf "Pick is invalid.\n"
|
||||||
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Get picked file
|
|
||||||
TFVAR_PICK_FILE=$( printf "${FILELIST_OUTPUT}\n" | head -n "${TFVAR_PICK}" | tail -1 )
|
|
||||||
|
|
||||||
# Autoformat before execution
|
# Autoformat before execution
|
||||||
terraform fmt &&
|
terraform fmt &&
|
||||||
find . -type f \( -name "*.tf" -o -name "*.tfvars" \) -exec terraform fmt "{}" \; || exit
|
find . -type f \( -name "*.tf" -o -name "*.tfvars" \) -exec terraform fmt "{}" \; || exit
|
||||||
|
|
||||||
# Validate configuration
|
# Validate configuration
|
||||||
terraform validate .
|
terraform validate . || exit 1
|
||||||
|
|
||||||
# Upgrade first to handle module changes
|
# Upgrade first to handle module changes
|
||||||
terraform init -upgrade || exit 1
|
terraform init -upgrade || exit 1
|
||||||
|
|
||||||
if (( TFVAR_COUNT > 0 ));then
|
# Start Terraform plan with selected var file
|
||||||
terraform plan -out "${PLAN_FILE}" -var-file="${TFVAR_PICK_FILE}"
|
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
|
if ! (( $? == 0 )); then
|
||||||
printf "Please check your Terraform files.\n"
|
printf "Terraform plan failed. Please check your Terraform files.\n"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
printf "Apply? terraform apply \"${PLAN_FILE}\"\n"
|
|
||||||
|
|
||||||
apply_plan_file_auto_approve () {
|
apply_plan_file_auto_approve () {
|
||||||
mv "${PLAN_FILE}" "${PLAN_FILE}-applied"
|
|
||||||
PLAN_FILE="${PLAN_FILE}-applied"
|
|
||||||
terraform apply -auto-approve "${PLAN_FILE}"
|
terraform apply -auto-approve "${PLAN_FILE}"
|
||||||
|
mv "${PLAN_FILE}" "${PLAN_FILE}-applied"
|
||||||
}
|
}
|
||||||
|
|
||||||
apply_plan_file_approve () {
|
apply_plan_file_approve () {
|
||||||
mv "${PLAN_FILE}" "${PLAN_FILE}-applied"
|
|
||||||
PLAN_FILE="${PLAN_FILE}-applied"
|
|
||||||
terraform apply "${PLAN_FILE}"
|
terraform apply "${PLAN_FILE}"
|
||||||
|
mv "${PLAN_FILE}" "${PLAN_FILE}-applied"
|
||||||
}
|
}
|
||||||
|
|
||||||
if [[ $1 == '-y' ]]; then
|
if [[ $1 == '-y' ]]; then
|
||||||
apply_plan_file_auto_approve
|
apply_plan_file_auto_approve
|
||||||
exit
|
exit
|
||||||
fi
|
fi
|
||||||
|
read -rp "Apply?\n terraform apply \"${PLAN_FILE}\" (y/n): " ANSWER
|
||||||
read -rp "(y/n): " ANSWER
|
|
||||||
if [[ $ANSWER == 'y' ]]; then
|
if [[ $ANSWER == 'y' ]]; then
|
||||||
apply_plan_file_approve
|
apply_plan_file_approve
|
||||||
|
exit
|
||||||
fi
|
fi
|
||||||
|
Reference in New Issue
Block a user