terragrunt-apply-all 4.6 KB

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