update_module_refs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #! /bin/bash
  2. #
  3. # Run from the xdr-terraform-live root, enviromment root, or partition root
  4. # to update the reference for all instances of a given module.
  5. #
  6. # USAGE:
  7. # update_module_refs --module 006-account-standards --newtag v4.0.2
  8. function argparse {
  9. PARAMS=""
  10. while (( "$#" )); do
  11. case "$1" in
  12. -m|--module)
  13. if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then
  14. MODULE=$2
  15. shift 2
  16. else
  17. echo "Error: Argument for $1 is missing" >&2
  18. exit 1
  19. fi
  20. ;;
  21. -n|--newtag)
  22. if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then
  23. NEWTAG=$2
  24. shift 2
  25. else
  26. echo "Error: Argument for $1 is missing" >&2
  27. exit 1
  28. fi
  29. ;;
  30. -h|--help)
  31. echo Usage: $0 '[-t|--test] [-d|--debug] -m|--module MODULE -n|--newtag NEWTAG'
  32. exit 0
  33. ;;
  34. -t|--test)
  35. TESTING="/bin/echo TESTING: "
  36. shift
  37. ;;
  38. -d|--debug)
  39. >&2 echo debug: Enabling debugging..
  40. DEBUG=1
  41. shift
  42. ;;
  43. # -p|--only-path)
  44. # if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then
  45. # ONLY_PATH=$2
  46. # shift 2
  47. # else
  48. # echo "Error: Argument for $1 is missing" >&2
  49. # exit 1
  50. # fi
  51. # ;;
  52. -*|--*=) # unsupported flags
  53. echo "Error: Unsupported flag $1" >&2
  54. exit 1
  55. ;;
  56. *) # preserve positional arguments
  57. PARAMS="$PARAMS $1"
  58. shift
  59. ;;
  60. esac
  61. done
  62. # set positional arguments in their proper place
  63. eval set -- "$PARAMS"
  64. }
  65. # Main
  66. argparse $*
  67. SHORT_PWD=$( basename ${PWD} )
  68. PARENT_PWD=$( basename $( cd .. && pwd ) )
  69. [[ $DEBUG == 1 ]] && >&2 echo debug: PWD=$PWD
  70. [[ $DEBUG == 1 ]] && >&2 echo debug: SHORT_PWD=$SHORT_PWD
  71. # Sanity Checking
  72. if [[ "$NEWTAG" == "" ]]; then
  73. >&2 echo Error: Parameter \'--newtag\' is required.
  74. exit 5
  75. fi
  76. # Sanity Checking
  77. if [[ "$MODULE" == "" ]]; then
  78. >&2 echo Error: Parameter \'--module\' is required.
  79. exit 6
  80. fi
  81. # Only function in the root, the environment, or the partition directories
  82. #
  83. # There's probably a clean way to do this syntax but I couldn't get it to work.
  84. # This check is "If globals.hcl doesn't exist AND env.hcl doesn't exist AND ..."
  85. if [ ! -e globals.hcl -a ! -e env.hcl -a ! -e partition.hcl ]; then
  86. read -p "You do not appear to be in an appropriate directory. Continue anyway [y/N]? " -n 1 -r
  87. echo ""
  88. if [[ $REPLY =~ ^[Yy]$ ]]
  89. then
  90. echo Continuing...
  91. else
  92. echo Exiting...
  93. exit 10
  94. fi
  95. fi
  96. if [[ $NEWTAG =~ v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
  97. [[ $TESTING ]] && >&2 echo debug: Newtag $NEWTAG is valid format.
  98. else
  99. >&2 echo Error: Invalid format for new tag. \"$NEWTAG\" must be of format v1.2.3
  100. exit 6
  101. fi
  102. if [[ $MODULE =~ ^[0-9]{3}- ]]; then
  103. [[ $TESTING ]] && >&2 echo debug: module $MODULE is valid format.
  104. else
  105. >&2 echo Error: Invalid format for Module. \"$MODULE\" must be of format xxx-modulename, where the x\'s are numeric digits.
  106. exit 7
  107. fi
  108. for i in `find . -type d -name "$MODULE"`; do
  109. if [[ $i =~ skeleton ]]; then
  110. echo ======= Skipping $i
  111. continue
  112. fi
  113. echo ======= Processing $i
  114. ${TESTING} sed -E -i .bak 's/ref=v[0-9]+.[0-9]+\.[0-9]+"$/ref='${NEWTAG}'"/' $i/terragrunt.hcl
  115. done