terragrunt-apply-all 3.9 KB

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