123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- #! /bin/bash
- #
- # Run from the xdr-terraform-live root, enviromment root, or partition root
- # to update the reference for all instances of a given module.
- #
- # USAGE:
- # update_module_refs --module 006-account-standards --newtag v4.0.2
- function argparse {
- PARAMS=""
- while (( "$#" )); do
- case "$1" in
- -m|--module)
- if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then
- MODULE=$2
- shift 2
- else
- echo "Error: Argument for $1 is missing" >&2
- exit 1
- fi
- ;;
- -n|--newtag)
- if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then
- NEWTAG=$2
- shift 2
- else
- echo "Error: Argument for $1 is missing" >&2
- exit 1
- fi
- ;;
- -h|--help)
- echo Usage: $0 '[-t|--test] [-d|--debug] -m|--module MODULE -n|--newtag NEWTAG'
- exit 0
- ;;
- -t|--test)
- TESTING="/bin/echo TESTING: "
- shift
- ;;
- -d|--debug)
- >&2 echo debug: Enabling debugging..
- DEBUG=1
- shift
- ;;
- # -p|--only-path)
- # if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then
- # ONLY_PATH=$2
- # shift 2
- # else
- # echo "Error: Argument for $1 is missing" >&2
- # exit 1
- # fi
- # ;;
- -*|--*=) # unsupported flags
- echo "Error: Unsupported flag $1" >&2
- exit 1
- ;;
- *) # preserve positional arguments
- PARAMS="$PARAMS $1"
- shift
- ;;
- esac
- done
- # set positional arguments in their proper place
- eval set -- "$PARAMS"
- }
- # Main
- argparse $*
- SHORT_PWD=$( basename ${PWD} )
- PARENT_PWD=$( basename $( cd .. && pwd ) )
- [[ $DEBUG == 1 ]] && >&2 echo debug: PWD=$PWD
- [[ $DEBUG == 1 ]] && >&2 echo debug: SHORT_PWD=$SHORT_PWD
- # Sanity Checking
- if [[ "$NEWTAG" == "" ]]; then
- >&2 echo Error: Parameter \'--newtag\' is required.
- exit 5
- fi
- # Sanity Checking
- if [[ "$MODULE" == "" ]]; then
- >&2 echo Error: Parameter \'--module\' is required.
- exit 6
- fi
- # Only function in the root, the environment, or the partition directories
- #
- # There's probably a clean way to do this syntax but I couldn't get it to work.
- # This check is "If globals.hcl doesn't exist AND env.hcl doesn't exist AND ..."
- if [ ! -e globals.hcl -a ! -e env.hcl -a ! -e partition.hcl ]; then
- read -p "You do not appear to be in an appropriate directory. Continue anyway [y/N]? " -n 1 -r
- echo ""
- if [[ $REPLY =~ ^[Yy]$ ]]
- then
- echo Continuing...
- else
- echo Exiting...
- exit 10
- fi
- fi
- if [[ $NEWTAG =~ v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
- [[ $TESTING ]] && >&2 echo debug: Newtag $NEWTAG is valid format.
- else
- >&2 echo Error: Invalid format for new tag. \"$NEWTAG\" must be of format v1.2.3
- exit 6
- fi
- if [[ $MODULE =~ ^[0-9]{3}- ]]; then
- [[ $TESTING ]] && >&2 echo debug: module $MODULE is valid format.
- else
- >&2 echo Error: Invalid format for Module. \"$MODULE\" must be of format xxx-modulename, where the x\'s are numeric digits.
- exit 7
- fi
- for i in `find . -type d -name "$MODULE"`; do
- if [[ $i =~ skeleton ]]; then
- echo ======= Skipping $i
- continue
- fi
- echo ======= Processing $i
- ${TESTING} sed -E -i .bak 's/ref=v[0-9]+.[0-9]+\.[0-9]+"$/ref='${NEWTAG}'"/' $i/terragrunt.hcl
- done
|