update_all_that_uses 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #! /bin/bash
  2. #
  3. # Update all modules that use a particular variables.tf to a new tag.
  4. #
  5. # Must be run from the `xdr-terraform-modules` directory, with a copy
  6. # of `xdr-terraform-live` at the same directory level.
  7. # "Strict Mode"
  8. set -euo pipefail
  9. IFS=$'\n\t'
  10. function argparse {
  11. PARAMS=""
  12. DEBUG=0
  13. while (( "$#" )); do
  14. case "$1" in
  15. -h|--help)
  16. echo Usage: $0 '[-t|--test] [-d|--debug] VARIABLEFILE NEWTAG'
  17. exit 0
  18. ;;
  19. # -t|--test)
  20. # TESTING="/bin/echo TESTING: "
  21. # shift
  22. # ;;
  23. -d|--debug)
  24. >&2 echo debug: Enabling debugging..
  25. DEBUG=1
  26. shift
  27. ;;
  28. -*|--*=) # unsupported flags
  29. echo "Error: Unsupported flag $1" >&2
  30. exit 1
  31. ;;
  32. *) # preserve positional arguments
  33. PARAMS="$PARAMS $1"
  34. shift
  35. ;;
  36. esac
  37. done
  38. # set positional arguments in their proper place
  39. eval set -- "$PARAMS"
  40. if [[ $# != 2 ]]; then
  41. echo "Incorrect number of parameters."
  42. echo Usage: $0 '[-t|--test] [-d|--debug] VARIABLEFILE NEWTAG'
  43. exit 4
  44. fi
  45. VARIABLEFILE=$1
  46. NEWTAG=$2
  47. }
  48. argparse $*
  49. # Ensure we're in teh proper directory
  50. PWD=`pwd`
  51. BN=`basename $PWD`
  52. if [[ $BN != "xdr-terraform-modules" ]]; then
  53. echo Must be run from xdr-terraform-modules root.
  54. exit 1
  55. fi
  56. if [[ ! -d ../xdr-terraform-live ]]; then
  57. echo Directory \"../xdr-terraform-live\" not found.
  58. exit 5
  59. fi
  60. if [[ ! -f "variables/${VARIABLEFILE}" ]]; then
  61. echo Variable file \"variables/${VARIABLEFILE}\" does not exist.
  62. exit 2
  63. fi
  64. if [[ ! ${NEWTAG} =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
  65. echo Tag \"${NEWTAG}\" does not appear to be in teh proper format.
  66. exit 3
  67. fi
  68. # Gather a list of all terragrunt.hcl files in xdr-terraform live. Should be faster to do this one.
  69. echo Finding all modules in xdr-terraform-live.. This may take a moment.
  70. LIVEMODULES=$( find ../xdr-terraform-live -name "terragrunt.hcl" -type f -depth -mindepth 2 -not -path "*/.terragrunt-cache/*" -not -path "*/000-skeleton/*" )
  71. echo $( echo -n "${LIVEMODULES}" | wc -l ) modules found in xdr-terraform-live.
  72. # Gather the modules that use it
  73. MODULES=$( find base -type l -name ${VARIABLEFILE} | sed "s#/${VARIABLEFILE}\$##" )
  74. set +e
  75. for m in ${MODULES}; do
  76. echo ==== ${VARIABLEFILE} used in module $m
  77. for l in ${LIVEMODULES}; do
  78. if [[ ${DEBUG} > 0 ]]; then
  79. echo ======== Searching for string \"$m\" in \"$l\"
  80. fi
  81. grep --silent ${m} ${l}
  82. RES=$?
  83. if [[ ${RES} == 0 ]]; then
  84. echo ============ $m FOUND in $l
  85. sed -E -i .bak 's/ref=v[0-9]+.[0-9]+\.[0-9]+"$/ref='${NEWTAG}'"/' $l
  86. fi
  87. done
  88. done
  89. set -e