terragrunt-apply-all-everywhere 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 1
  11. ;;
  12. -t|--test)
  13. TESTING="/bin/echo TESTING: "
  14. shift
  15. ;;
  16. -l|--local)
  17. LOCAL="--local"
  18. shift
  19. ;;
  20. -n|--notlocal)
  21. NOTLOCAL="--notlocal"
  22. shift
  23. ;;
  24. -d|--debug)
  25. >&2 echo debug: Enabling debugging..
  26. DEBUG=1
  27. debugstr="--debug"
  28. shift
  29. ;;
  30. -s|--skipqualys)
  31. SKIPQUALYS="--skipqualys"
  32. shift
  33. ;;
  34. # -p|--only-path)
  35. # if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then
  36. # ONLY_PATH=$2
  37. # shift 2
  38. # else
  39. # echo "Error: Argument for $1 is missing" >&2
  40. # exit 1
  41. # fi
  42. # ;;
  43. -*|--*=) # unsupported flags
  44. echo "Error: Unsupported flag $1" >&2
  45. exit 1
  46. ;;
  47. *) # preserve positional arguments
  48. PARAMS="$PARAMS $1"
  49. shift
  50. ;;
  51. esac
  52. done
  53. # set positional arguments in their proper place
  54. eval set -- "$PARAMS"
  55. }
  56. # Main
  57. argparse $*
  58. SHORT_PWD=$( basename ${PWD} )
  59. PARENT_PWD=$( basename $( cd .. && pwd ) )
  60. [[ $DEBUG == 1 ]] && >&2 echo debug: PWD=$PWD
  61. [[ $DEBUG == 1 ]] && >&2 echo debug: SHORT_PWD=$SHORT_PWD
  62. # Sanity Checking
  63. if [[ $SHORT_PWD != "xdr-terraform-live" ]]; then
  64. read -p "WARNING! Not running from 'xdr-terraform-live'. PWD is $SHORT_PWD. Continue anyway? [Y/n]? " -n 1 -r
  65. echo ""
  66. if [[ $REPLY =~ ^[Nn]$ ]]
  67. then
  68. echo Exiting...
  69. exit 1
  70. fi
  71. fi
  72. for e in test common prod; do
  73. pushd $e > /dev/null
  74. for p in aws aws-us-gov; do
  75. pushd $p > /dev/null
  76. for a in $(find . -type d -mindepth 1 -maxdepth 1); do
  77. pushd $a > /dev/null
  78. echo ""
  79. echo ""
  80. echo "*************************************************************************************"
  81. echo "Beginning environment '$e', partition '$p', account '$a'"
  82. echo "*************************************************************************************"
  83. echo ""
  84. echo ""
  85. if [[ -f UNUSED.ACCOUNT ]]; then
  86. echo -- This account is marked as unused. Skipping...
  87. popd > /dev/null
  88. continue
  89. fi
  90. if [[ -f UNMANAGED.ACCOUNT ]]; then
  91. echo -- This account is marked as unmanaged. Skipping...
  92. popd > /dev/null
  93. continue
  94. fi
  95. EXITCODE=1 # Assume error
  96. if [[ $DEBUG == 1 ]]; then
  97. echo debug: Would run: terragrunt-apply-all $TESTING $LOCAL $NOTLOCAL $debugstr $SKIPQUALYS
  98. EXITCODE=$?
  99. else
  100. terragrunt-apply-all $TESTING $LOCAL $NOTLOCAL $DEBUG $SKIPQUALYS
  101. EXITCODE=$?
  102. fi
  103. if [[ $EXITCODE != 0 ]]; then
  104. # Prompt to continue after each module. Easier than ctrl-c...
  105. read -p "Terragrunt failed for environment '$e', partition '$p', account '$a'.. Continue to next account [Y/n]? " -n 1 -r
  106. echo ""
  107. if [[ $REPLY =~ ^[Nn]$ ]]
  108. then
  109. echo Exiting...
  110. exit 1
  111. fi
  112. fi
  113. popd > /dev/null
  114. done
  115. popd > /dev/null
  116. done
  117. popd > /dev/null
  118. done
  119. echo Finished.
  120. exit 0