terragrunt-apply-all-everywhere 3.8 KB

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