terragrunt-apply-all 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. else
  65. read -p "Local not specified. Specify '--notlocal' to skip this question. Are you sure? [Y/n]? " -n 1 -r
  66. echo ""
  67. if [[ $REPLY =~ ^[Nn]$ ]]
  68. then
  69. echo Exiting...
  70. exit 1
  71. fi
  72. fi
  73. TERRAGRUNT_BIN=`which terragrunt`
  74. fi
  75. if [[ ! -x $TERRAGRUNT_BIN ]]; then
  76. >&2 echo "Error: terragrunt executable ($TERRAGRUNT_BIN) not found or not executable."
  77. exit 4
  78. fi
  79. }
  80. # Main
  81. argparse $*
  82. SHORT_PWD=$( basename ${PWD} )
  83. PARENT_PWD=$( basename $( cd .. && pwd ) )
  84. [[ $DEBUG == 1 ]] && >&2 echo debug: PWD=$PWD
  85. [[ $DEBUG == 1 ]] && >&2 echo debug: SHORT_PWD=$SHORT_PWD
  86. # Sanity Checking
  87. if [[ $SHORT_PWD == "000-skeleton" ]]; then
  88. >&2 echo Error: Cannot run from skeleton directory. Exiting...
  89. exit 1
  90. fi
  91. if [[ $SHORT_PWD =~ ^[0-9]{3}-.* ]]; then
  92. >&2 echo Error: We appear to be in a module directory. Please run from the account directory you wish to update. Exiting...
  93. exit 2
  94. fi
  95. if [[ ! $PARENT_PWD =~ ^aws ]]; then
  96. >&2 echo Error: We do not appear to be in an account directory. Failing...
  97. exit 3
  98. fi
  99. for i in `seq -f "%g*" 0 9 | sort -n`; do
  100. EXITCODE=1 # Assume error
  101. MODULE=$( basename $i )
  102. if [[ -d $MODULE ]]; then
  103. echo "====================================================================================="
  104. echo "Processing module $MODULE..."
  105. echo "====================================================================================="
  106. if [[ $SKIPQUALYS == 1 && $MODULE =~ qualys ]]; then
  107. echo "Skipping due to skipqualys flag"
  108. echo ""
  109. continue
  110. else
  111. [[ $DEBUG == 1 ]] && echo "Not qualys - SKIPQUALYS = ${SKIPQUALYS}; Module = ${MODULE}"
  112. fi
  113. pushd . > /dev/null
  114. cd $MODULE
  115. if [[ $(basename $(pwd)) =~ regional ]]; then
  116. echo "=========== We are in a regional directory, recursing..."
  117. for i in *; do
  118. if [[ -d $i ]]; then
  119. echo "========== Region: $i"
  120. pushd . > /dev/null
  121. cd $i
  122. [[ $TESTING ]] && ${TERRAGRUNT_BIN} plan # Run a plan if testing
  123. [[ $TESTING ]] || ${TERRAGRUNT_BIN} init # Run an init and apply
  124. [[ $TESTING ]] || ${TERRAGRUNT_BIN} apply
  125. EXITCODE=$?
  126. popd > /dev/null
  127. echo "========== Region completed: $i"
  128. fi
  129. done
  130. elif [[ -f DISABLED ]]; then
  131. echo Skipping module due to \"DISABLED\" file.
  132. EXITCODE=0
  133. else
  134. [[ $TESTING ]] && ${TERRAGRUNT_BIN} plan # Run a plan if testing
  135. [[ $TESTING ]] || ${TERRAGRUNT_BIN} init # Run an init and apply otherwise
  136. [[ $TESTING ]] || ${TERRAGRUNT_BIN} apply
  137. EXITCODE=$?
  138. fi
  139. popd > /dev/null
  140. echo "=======================================DONE=========================================="
  141. echo ""
  142. echo ""
  143. if [[ $EXITCODE != 0 ]]; then
  144. # Prompt to continue after each module. Easier than ctrl-c...
  145. read -p "Terragrunt completed. Continue to next module [Y/n]? " -n 1 -r
  146. echo ""
  147. if [[ $REPLY =~ ^[Nn]$ ]]
  148. then
  149. echo Exiting...
  150. exit 1
  151. fi
  152. fi
  153. fi
  154. done
  155. echo Finished.
  156. exit 0