terragrunt-apply-all 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #! /bin/bash
  2. #
  3. # Do a more sane apply-all via terragrunt
  4. function argparse {
  5. PARAMS=""
  6. while (( "$#" )); do
  7. case "$1" in
  8. -h|--help)
  9. echo Usage: $0 '[-r|--refresh] [-l|--local] [-t|--test] [-s|--skipqualys] [-d|--debug]'
  10. exit 0
  11. ;;
  12. -t|--test)
  13. TESTING="/bin/echo TESTING: "
  14. shift
  15. ;;
  16. -l|--local)
  17. LOCAL="1"
  18. shift
  19. ;;
  20. -n|--notlocal)
  21. NOTLOCAL="1"
  22. shift
  23. ;;
  24. -d|--debug)
  25. >&2 echo debug: Enabling debugging..
  26. DEBUG=1
  27. shift
  28. ;;
  29. -r|--refresh)
  30. # Refresh "refreshes" the state from the aws api, even if the configuration seemingly
  31. # matches what's on disk. For example, after an upgrade to terraform where they've added
  32. # support for a new configuration item.
  33. REFRESH="-refresh-only"
  34. shift
  35. ;;
  36. -s|--skipqualys)
  37. SKIPQUALYS=1
  38. shift
  39. ;;
  40. # -p|--only-path)
  41. # if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then
  42. # ONLY_PATH=$2
  43. # shift 2
  44. # else
  45. # echo "Error: Argument for $1 is missing" >&2
  46. # exit 1
  47. # fi
  48. # ;;
  49. -*|--*=) # unsupported flags
  50. echo "Error: Unsupported flag $1" >&2
  51. exit 1
  52. ;;
  53. *) # preserve positional arguments
  54. PARAMS="$PARAMS $1"
  55. shift
  56. ;;
  57. esac
  58. done
  59. # set positional arguments in their proper place
  60. eval set -- "$PARAMS"
  61. if [[ $LOCAL && $NOTLOCAL ]]; then
  62. echo ""
  63. echo "ERROR: Cannot specify both '--local' and '--nonlocal'. Pick one."
  64. exit 1
  65. fi
  66. if [[ $LOCAL ]]; then
  67. TERRAGRUNT_BIN=`which terragrunt-local`
  68. else
  69. if [[ $NOTLOCAL ]]; then
  70. [[ $DEBUG == 1 ]] && >&2 echo debug: Not local specified, not prompting.
  71. # # This turned out to be annoying, I _usually_ run it with --notlocal
  72. # else
  73. # read -p "Local not specified. Specify '--notlocal' to skip this question. Are you sure? [Y/n]? " -n 1 -r
  74. # echo ""
  75. # if [[ $REPLY =~ ^[Nn]$ ]]
  76. # then
  77. # echo Exiting...
  78. # exit 1
  79. # fi
  80. fi
  81. TERRAGRUNT_BIN=`which terragrunt`
  82. fi
  83. if [[ ! -x $TERRAGRUNT_BIN ]]; then
  84. >&2 echo "Error: terragrunt executable ($TERRAGRUNT_BIN) not found or not executable."
  85. exit 4
  86. fi
  87. }
  88. # Main
  89. argparse $*
  90. SHORT_PWD=$( basename ${PWD} )
  91. PARENT_PWD=$( basename $( cd .. && pwd ) )
  92. [[ $DEBUG == 1 ]] && >&2 echo debug: PWD=$PWD
  93. [[ $DEBUG == 1 ]] && >&2 echo debug: SHORT_PWD=$SHORT_PWD
  94. # Sanity Checking
  95. if [[ $SHORT_PWD == "000-skeleton" ]]; then
  96. >&2 echo Error: Cannot run from skeleton directory. Exiting...
  97. exit 1
  98. fi
  99. if [[ $SHORT_PWD =~ ^[0-9]{3}-.* ]]; then
  100. >&2 echo Error: We appear to be in a module directory. Please run from the account directory you wish to update. Exiting...
  101. exit 2
  102. fi
  103. if [[ ! $PARENT_PWD =~ ^aws ]]; then
  104. >&2 echo Error: We do not appear to be in an account directory. Failing...
  105. exit 3
  106. fi
  107. for i in `seq -f "%g*" 0 9 | sort -n`; do
  108. EXITCODE=1 # Assume error
  109. MODULE=$( basename $i )
  110. if [[ -d $MODULE ]]; then
  111. echo "====================================================================================="
  112. echo "Processing module $MODULE... PWD = `pwd`"
  113. echo "====================================================================================="
  114. if [[ $SKIPQUALYS == 1 && $MODULE =~ qualys ]]; then
  115. echo "Skipping due to skipqualys flag"
  116. echo ""
  117. continue
  118. else
  119. [[ $DEBUG == 1 ]] && echo "Not qualys - SKIPQUALYS = ${SKIPQUALYS}; Module = ${MODULE}"
  120. fi
  121. pushd . > /dev/null
  122. cd $MODULE
  123. if [[ $(basename $(pwd)) =~ regional ]]; then
  124. echo "=========== We are in a regional directory, recursing..."
  125. for i in *; do
  126. if [[ -d $i ]]; then
  127. echo "========== Region: $i"
  128. pushd . > /dev/null
  129. cd $i
  130. [[ $TESTING ]] && ${TERRAGRUNT_BIN} plan # Run a plan if testing
  131. [[ $TESTING ]] || ${TERRAGRUNT_BIN} init # Run an init and apply
  132. [[ $TESTING ]] || ${TERRAGRUNT_BIN} apply ${REFRESH}
  133. EXITCODE=$?
  134. popd > /dev/null
  135. echo "========== Region completed: $i"
  136. fi
  137. done
  138. elif [[ -f DISABLED ]]; then
  139. echo Skipping module due to \"DISABLED\" file.
  140. EXITCODE=0
  141. else
  142. [[ $TESTING ]] && ${TERRAGRUNT_BIN} plan # Run a plan if testing
  143. [[ $TESTING ]] || ${TERRAGRUNT_BIN} init # Run an init and apply otherwise
  144. [[ $TESTING ]] || ${TERRAGRUNT_BIN} apply ${REFRESH}
  145. EXITCODE=$?
  146. fi
  147. popd > /dev/null
  148. echo "=======================================DONE=========================================="
  149. echo ""
  150. echo ""
  151. if [[ $EXITCODE != 0 ]]; then
  152. # Prompt to continue after each module. Easier than ctrl-c...
  153. read -p "Terragrunt completed. Continue to next module [Y/n]? " -n 1 -r
  154. echo ""
  155. if [[ $REPLY =~ ^[Nn]$ ]]
  156. then
  157. echo Exiting...
  158. exit 1
  159. fi
  160. fi
  161. fi
  162. done
  163. echo Finished.
  164. exit 0