terragrunt-apply-all 3.5 KB

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