terragrunt-apply-all-everywhere 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #! /bin/bash
  2. #
  3. # Do a more sane apply-all via terragrunt
  4. function argparse {
  5. PARAMS=""
  6. ENVS=()
  7. while (( "$#" )); do
  8. case "$1" in
  9. -h|--help)
  10. echo Usage: $0 '[-l|--local] [-t|--test] [-s|--skipqualys] [-u|--upgrade] [ENVFLAGS] [-d|--debug]'
  11. echo " where ENVFLAGS is one or more of --envprod, --envcommon, --envtest, or --envall. --envall is the default."
  12. exit 1
  13. ;;
  14. -t|--test)
  15. TESTING="/bin/echo TESTING: "
  16. shift
  17. ;;
  18. -l|--local)
  19. LOCAL="--local"
  20. shift
  21. ;;
  22. -n|--notlocal)
  23. NOTLOCAL="--notlocal"
  24. shift
  25. ;;
  26. -d|--debug)
  27. >&2 echo debug: Enabling debugging..
  28. DEBUG=1
  29. debugstr="--debug"
  30. shift
  31. ;;
  32. --envall)
  33. ENVS+=("test")
  34. ENVS+=("common")
  35. ENVS+=("prod")
  36. shift
  37. ;;
  38. --envtest)
  39. ENVS+=("test")
  40. shift
  41. ;;
  42. --envcommon)
  43. ENVS+=("common")
  44. shift
  45. ;;
  46. --envprod)
  47. ENVS+=("prod")
  48. shift
  49. ;;
  50. -r|--refresh)
  51. REFRESH="--refresh"
  52. shift
  53. ;;
  54. -s|--skipqualys)
  55. SKIPQUALYS="--skipqualys"
  56. shift
  57. ;;
  58. -u|--upgrade)
  59. UPGRADE="--upgrade"
  60. shift
  61. ;;
  62. # -p|--only-path)
  63. # if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then
  64. # ONLY_PATH=$2
  65. # shift 2
  66. # else
  67. # echo "Error: Argument for $1 is missing" >&2
  68. # exit 1
  69. # fi
  70. # ;;
  71. -*|--*=) # unsupported flags
  72. echo "Error: Unsupported flag $1" >&2
  73. exit 1
  74. ;;
  75. *) # preserve positional arguments
  76. PARAMS="$PARAMS $1"
  77. shift
  78. ;;
  79. esac
  80. done
  81. if [[ ${#ENVS[@]} == 0 ]]; then
  82. # Default to all environments
  83. ENVS+=("test")
  84. ENVS+=("common")
  85. ENVS+=("prod")
  86. fi
  87. # set positional arguments in their proper place
  88. eval set -- "$PARAMS"
  89. }
  90. # Main
  91. argparse $*
  92. SHORT_PWD=$( basename ${PWD} )
  93. PARENT_PWD=$( basename $( cd .. && pwd ) )
  94. [[ $DEBUG == 1 ]] && >&2 echo debug: PWD=$PWD
  95. [[ $DEBUG == 1 ]] && >&2 echo debug: SHORT_PWD=$SHORT_PWD
  96. # Sanity Checking
  97. if [[ $SHORT_PWD != "xdr-terraform-live" ]]; then
  98. read -p "WARNING! Not running from 'xdr-terraform-live'. PWD is $SHORT_PWD. Continue anyway? [Y/n]? " -n 1 -r
  99. echo ""
  100. if [[ $REPLY =~ ^[Nn]$ ]]
  101. then
  102. echo Exiting...
  103. exit 1
  104. fi
  105. fi
  106. [[ $DEBUG == 1 ]] && >&2 echo debug: ENVS=${ENVS[*]}
  107. for e in ${ENVS[*]}; do
  108. pushd $e > /dev/null
  109. for p in aws aws-us-gov; do
  110. pushd $p > /dev/null
  111. for a in $(find . -type d -mindepth 1 -maxdepth 1); do
  112. pushd $a > /dev/null
  113. echo ""
  114. echo ""
  115. echo "*************************************************************************************"
  116. echo "Beginning environment '$e', partition '$p', account '$a'"
  117. echo "*************************************************************************************"
  118. echo ""
  119. echo ""
  120. if [[ -f UNUSED.ACCOUNT ]]; then
  121. echo -- This account is marked as unused. Skipping...
  122. popd > /dev/null
  123. continue
  124. fi
  125. if [[ -f UNMANAGED.ACCOUNT ]]; then
  126. echo -- This account is marked as unmanaged. Skipping...
  127. popd > /dev/null
  128. continue
  129. fi
  130. EXITCODE=1 # Assume error
  131. if [[ $DEBUG == 1 ]]; then
  132. echo debug: Would run: terragrunt-apply-all $TESTING $REFRESH $LOCAL $NOTLOCAL $UPGRADE $debugstr $SKIPQUALYS
  133. EXITCODE=$?
  134. else
  135. terragrunt-apply-all $TESTING $REFRESH $LOCAL $NOTLOCAL $UPGRADE $DEBUG $SKIPQUALYS
  136. EXITCODE=$?
  137. fi
  138. if [[ $EXITCODE != 0 ]]; then
  139. # Prompt to continue after each module. Easier than ctrl-c...
  140. read -p "Terragrunt failed for environment '$e', partition '$p', account '$a'.. Continue to next account [Y/n]? " -n 1 -r
  141. echo ""
  142. if [[ $REPLY =~ ^[Nn]$ ]]
  143. then
  144. echo Exiting...
  145. exit 1
  146. fi
  147. fi
  148. popd > /dev/null
  149. done
  150. popd > /dev/null
  151. done
  152. popd > /dev/null
  153. done
  154. echo Finished.
  155. exit 0