update_refs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #! /bin/bash
  2. #
  3. # Run from an account directory to update all the child directory's references.
  4. # This should be run before or after `terragrunt-local`
  5. function argparse {
  6. PARAMS=""
  7. while (( "$#" )); do
  8. case "$1" in
  9. -n|--newtag)
  10. if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then
  11. NEWTAG=$2
  12. shift 2
  13. else
  14. echo "Error: Argument for $1 is missing" >&2
  15. exit 1
  16. fi
  17. ;;
  18. -h|--help)
  19. echo Usage: $0 '[-t|--test] [-d|--debug] -n|--newtag NEWTAG'
  20. exit 0
  21. ;;
  22. -t|--test)
  23. TESTING="/bin/echo TESTING: "
  24. shift
  25. ;;
  26. -d|--debug)
  27. >&2 echo debug: Enabling debugging..
  28. DEBUG=1
  29. shift
  30. ;;
  31. # -p|--only-path)
  32. # if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then
  33. # ONLY_PATH=$2
  34. # shift 2
  35. # else
  36. # echo "Error: Argument for $1 is missing" >&2
  37. # exit 1
  38. # fi
  39. # ;;
  40. -*|--*=) # unsupported flags
  41. echo "Error: Unsupported flag $1" >&2
  42. exit 1
  43. ;;
  44. *) # preserve positional arguments
  45. PARAMS="$PARAMS $1"
  46. shift
  47. ;;
  48. esac
  49. done
  50. # set positional arguments in their proper place
  51. eval set -- "$PARAMS"
  52. }
  53. # Main
  54. argparse $*
  55. SHORT_PWD=$( basename ${PWD} )
  56. PARENT_PWD=$( basename $( cd .. && pwd ) )
  57. [[ $DEBUG == 1 ]] && >&2 echo debug: PWD=$PWD
  58. [[ $DEBUG == 1 ]] && >&2 echo debug: SHORT_PWD=$SHORT_PWD
  59. # Sanity Checking
  60. if [[ "$NEWTAG" == "" ]]; then
  61. >&2 echo Error: Parameter \'--newtag\' is required.
  62. exit 5
  63. fi
  64. if [[ $SHORT_PWD =~ ^[0-9]{3}-.* ]]; then
  65. if [[ $SHORT_PWD == "000-skeleton" ]]; then
  66. read -p "Are you sure you wish to update the SKELETON directory [y/N]? " -n 1 -r
  67. echo ""
  68. if [[ $REPLY =~ ^[Yy]$ ]]
  69. then
  70. echo Continuing...
  71. else
  72. echo Exiting...
  73. exit 10
  74. fi
  75. else
  76. >&2 echo Error: We appear to be in a module directory. Please run from the account directory you wish to update. Exiting...
  77. exit 2
  78. fi
  79. fi
  80. if [[ $NEWTAG =~ v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
  81. [[ $TESTING ]] && >&2 echo debug: Newtag $NEWTAG is valid format.
  82. else
  83. >&2 echo Error: Invalid format for new tag. \"$NEWTAG\" must be of format v1.2.3
  84. exit 6
  85. fi
  86. for i in `find . -maxdepth 2 -name "terragrunt.hcl"`; do
  87. echo ======= Processing $i
  88. ${TESTING} sed -E -i .bak 's/ref=v[0-9]+.[0-9]+\.[0-9]+"$/ref='${NEWTAG}'"/' $i
  89. done