|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +set -e |
| 4 | + |
| 5 | +action=$1 |
| 6 | +env=$2 |
| 7 | +shift 2 |
| 8 | +other=$@ |
| 9 | +# must be subscription in lower case |
| 10 | +subscription="" |
| 11 | +BACKEND_CONFIG_PATH="./env/${ENV}/backend.tfvars" |
| 12 | + |
| 13 | +if [ -z "$action" ]; then |
| 14 | + echo "Missed action: init, apply, plan" |
| 15 | + exit 0 |
| 16 | +fi |
| 17 | + |
| 18 | +if [ -z "$env" ]; then |
| 19 | + echo "env should be: dev, uat or prod." |
| 20 | + exit 0 |
| 21 | +fi |
| 22 | + |
| 23 | +source "./env/$env/backend.ini" |
| 24 | + |
| 25 | +az account set -s "${subscription}" |
| 26 | + |
| 27 | +# if using cygwin, we have to transcode the WORKDIR |
| 28 | +if [[ $WORKDIR == /cygdrive/* ]]; then |
| 29 | + WORKDIR=$(cygpath -w $WORKDIR) |
| 30 | +fi |
| 31 | + |
| 32 | +if [ "$action" = "force-unlock" ]; then |
| 33 | + echo "🧭 terraform INIT in env: ${env}" |
| 34 | + terraform init -reconfigure -backend-config="./env/$env/backend.tfvars" $other |
| 35 | + warn_message="You are about to unlock Terraform's remote state. |
| 36 | + This is a dangerous task you want to be aware of before going on. |
| 37 | + This operation won't affect your infrastructure directly. |
| 38 | + However, please note that you may lose pieces of information about partially-applied configurations. |
| 39 | + Please refer to the official Terraform documentation about the command: |
| 40 | + https://developer.hashicorp.com/terraform/cli/commands/force-unlock" |
| 41 | + printf "\n\e[33m%s\e[0m\n\n" "$warn_message" |
| 42 | + |
| 43 | + read -r -p "Please enter the LOCK ID: " lock_id |
| 44 | + terraform force-unlock "$lock_id" |
| 45 | + |
| 46 | + exit 0 # this line prevents the script to go on |
| 47 | +fi |
| 48 | + |
| 49 | +if echo "init plan apply refresh import output state taint destroy" | grep -w "$action" > /dev/null; then |
| 50 | + if [ "$action" = "init" ]; then |
| 51 | + echo "🧭 terraform INIT in env: ${env}" |
| 52 | + terraform "$action" -reconfigure -backend-config="./env/$env/backend.tfvars" $other |
| 53 | + elif [ "$action" = "output" ] || [ "$action" = "state" ] || [ "$action" = "taint" ]; then |
| 54 | + # init terraform backend |
| 55 | + echo "🧭 terraform (output|state|taint) launched with action: ${action} in env: ${env}" |
| 56 | + terraform init -reconfigure -backend-config="./env/$env/backend.tfvars" |
| 57 | + terraform "$action" $other |
| 58 | + else |
| 59 | + # init terraform backend |
| 60 | + echo "🧭 terraform launched with action: ${action} in env: ${env}" |
| 61 | + |
| 62 | + terraform init -reconfigure -backend-config="./env/$env/backend.tfvars" |
| 63 | + terraform "$action" -var-file="./env/$env/terraform.tfvars" $other |
| 64 | + fi |
| 65 | +else |
| 66 | + echo "Action not allowed." |
| 67 | + exit 1 |
| 68 | +fi |
0 commit comments