terragrunt-apply-all-everywhere 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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] [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. # -p|--only-path)
  59. # if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then
  60. # ONLY_PATH=$2
  61. # shift 2
  62. # else
  63. # echo "Error: Argument for $1 is missing" >&2
  64. # exit 1
  65. # fi
  66. # ;;
  67. -*|--*=) # unsupported flags
  68. echo "Error: Unsupported flag $1" >&2
  69. exit 1
  70. ;;
  71. *) # preserve positional arguments
  72. PARAMS="$PARAMS $1"
  73. shift
  74. ;;
  75. esac
  76. done
  77. if [[ ${#ENVS[@]} == 0 ]]; then
  78. # Default to all environments
  79. ENVS+=("test")
  80. ENVS+=("common")
  81. ENVS+=("prod")
  82. fi
  83. # set positional arguments in their proper place
  84. eval set -- "$PARAMS"
  85. }
  86. # Main
  87. argparse $*
  88. SHORT_PWD=$( basename ${PWD} )
  89. PARENT_PWD=$( basename $( cd .. && pwd ) )
  90. [[ $DEBUG == 1 ]] && >&2 echo debug: PWD=$PWD
  91. [[ $DEBUG == 1 ]] && >&2 echo debug: SHORT_PWD=$SHORT_PWD
  92. # Sanity Checking
  93. if [[ $SHORT_PWD != "xdr-terraform-live" ]]; then
  94. read -p "WARNING! Not running from 'xdr-terraform-live'. PWD is $SHORT_PWD. Continue anyway? [Y/n]? " -n 1 -r
  95. echo ""
  96. if [[ $REPLY =~ ^[Nn]$ ]]
  97. then
  98. echo Exiting...
  99. exit 1
  100. fi
  101. fi
  102. [[ $DEBUG == 1 ]] && >&2 echo debug: ENVS=${ENVS[*]}
  103. for e in ${ENVS[*]}; do
  104. pushd $e > /dev/null
  105. for p in aws aws-us-gov; do
  106. pushd $p > /dev/null
  107. for a in $(find . -type d -mindepth 1 -maxdepth 1); do
  108. pushd $a > /dev/null
  109. echo ""
  110. echo ""
  111. echo "*************************************************************************************"
  112. echo "Beginning environment '$e', partition '$p', account '$a'"
  113. echo "*************************************************************************************"
  114. echo ""
  115. echo ""
  116. if [[ -f UNUSED.ACCOUNT ]]; then
  117. echo -- This account is marked as unused. Skipping...
  118. popd > /dev/null
  119. continue
  120. fi
  121. if [[ -f UNMANAGED.ACCOUNT ]]; then
  122. echo -- This account is marked as unmanaged. Skipping...
  123. popd > /dev/null
  124. continue
  125. fi
  126. EXITCODE=1 # Assume error
  127. if [[ $DEBUG == 1 ]]; then
  128. echo debug: Would run: terragrunt-apply-all $TESTING $REFRESH $LOCAL $NOTLOCAL $debugstr $SKIPQUALYS
  129. EXITCODE=$?
  130. else
  131. terragrunt-apply-all $TESTING $REFRESH $LOCAL $NOTLOCAL $DEBUG $SKIPQUALYS
  132. EXITCODE=$?
  133. fi
  134. if [[ $EXITCODE != 0 ]]; then
  135. # Prompt to continue after each module. Easier than ctrl-c...
  136. read -p "Terragrunt failed for environment '$e', partition '$p', account '$a'.. Continue to next account [Y/n]? " -n 1 -r
  137. echo ""
  138. if [[ $REPLY =~ ^[Nn]$ ]]
  139. then
  140. echo Exiting...
  141. exit 1
  142. fi
  143. fi
  144. popd > /dev/null
  145. done
  146. popd > /dev/null
  147. done
  148. popd > /dev/null
  149. done
  150. echo Finished.
  151. exit 0