123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- #! /bin/bash
- #
- # Do a more sane apply-all via terragrunt
- function argparse {
- PARAMS=""
- while (( "$#" )); do
- case "$1" in
- -h|--help)
- echo Usage: $0 '[-l|--local] [-t|--test] [-d|--debug]'
- exit 0
- ;;
- -t|--test)
- TESTING="/bin/echo TESTING: "
- shift
- ;;
- -l|--local)
- LOCAL="1"
- shift
- ;;
- -d|--debug)
- >&2 echo debug: Enabling debugging..
- DEBUG=1
- shift
- ;;
- # -p|--only-path)
- # if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then
- # ONLY_PATH=$2
- # shift 2
- # else
- # echo "Error: Argument for $1 is missing" >&2
- # exit 1
- # fi
- # ;;
- -*|--*=) # unsupported flags
- echo "Error: Unsupported flag $1" >&2
- exit 1
- ;;
- *) # preserve positional arguments
- PARAMS="$PARAMS $1"
- shift
- ;;
- esac
- done
- # set positional arguments in their proper place
- eval set -- "$PARAMS"
- if [[ $LOCAL ]]; then
- TERRAGRUNT_BIN=`which terragrunt-local`
- else
- read -p "Local not specified. Are you sure? [Y/n]? " -n 1 -r
- echo ""
- if [[ $REPLY =~ ^[Nn]$ ]]
- then
- echo Exiting...
- exit 0
- fi
- TERRAGRUNT_BIN=`which terragrunt`
- fi
- if [[ ! -x $TERRAGRUNT_BIN ]]; then
- >&2 echo "Error: terragrunt executable ($TERRAGRUNT_BIN) not found or not executable."
- exit 4
- fi
- }
- # Main
- argparse $*
- SHORT_PWD=$( basename ${PWD} )
- PARENT_PWD=$( basename $( cd .. && pwd ) )
- [[ $DEBUG == 1 ]] && >&2 echo debug: PWD=$PWD
- [[ $DEBUG == 1 ]] && >&2 echo debug: SHORT_PWD=$SHORT_PWD
- # Sanity Checking
- if [[ $SHORT_PWD == "000-skeleton" ]]; then
- >&2 echo Error: Cannot run from skeleton directory. Exiting...
- exit 1
- fi
- if [[ $SHORT_PWD =~ ^[0-9]{3}-.* ]]; then
- >&2 echo Error: We appear to be in a module directory. Please run from the account directory you wish to update. Exiting...
- exit 2
- fi
- if [[ ! $PARENT_PWD =~ ^aws ]]; then
- >&2 echo Error: We do not appear to be in an account directory. Failing...
- exit 3
- fi
- for i in `seq -f "%g*" 0 9 | sort -n`; do
- MODULE=$( basename $i )
- if [[ -d $MODULE ]]; then
- echo "====================================================================================="
- echo "Processing module $MODULE..."
- echo "====================================================================================="
- pushd . > /dev/null
- cd $MODULE
- if [[ $(basename $(pwd)) =~ regional ]]; then
- echo "=========== We are in a regional directory, recursing..."
- for i in *; do
- if [[ -d $i ]]; then
- echo "========== Region: $i"
- pushd . > /dev/null
- cd $i
- [[ $TESTING ]] && ${TERRAGRUNT_BIN} plan # Run a plan if testing
- [[ $TESTING ]] || ${TERRAGRUNT_BIN} init # Run an init and apply
- [[ $TESTING ]] || ${TERRAGRUNT_BIN} apply
- popd > /dev/null
- echo "========== Region completed: $i"
- fi
- done
- else
- [[ $TESTING ]] && ${TERRAGRUNT_BIN} plan # Run a plan if testing
- [[ $TESTING ]] || ${TERRAGRUNT_BIN} init # Run an init and apply otherwise
- [[ $TESTING ]] || ${TERRAGRUNT_BIN} apply
- fi
- popd > /dev/null
- echo "=======================================DONE=========================================="
- echo ""
- echo ""
- # Prompt to continue after each module. Easier than ctrl-c...
- read -p "Terragrunt completed. Continue to next module [Y/n]? " -n 1 -r
- echo ""
- if [[ $REPLY =~ ^[Nn]$ ]]
- then
- echo Exiting...
- exit 0
- fi
- fi
- done
- echo Finished.
|