terragrunt-apply-all 5.0 KB

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