#! /bin/bash # # Update all modules that use a particular variables.tf to a new tag. # # Must be run from the `xdr-terraform-modules` directory, with a copy # of `xdr-terraform-live` at the same directory level. # "Strict Mode" set -euo pipefail IFS=$'\n\t' function argparse { PARAMS="" DEBUG=0 while (( "$#" )); do case "$1" in -h|--help) echo Usage: $0 '[-t|--test] [-d|--debug] VARIABLEFILE NEWTAG' exit 0 ;; # -t|--test) # TESTING="/bin/echo TESTING: " # shift # ;; -d|--debug) >&2 echo debug: Enabling debugging.. DEBUG=1 shift ;; -*|--*=) # 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" if [[ $# != 2 ]]; then echo "Incorrect number of parameters." echo Usage: $0 '[-t|--test] [-d|--debug] VARIABLEFILE NEWTAG' exit 4 fi VARIABLEFILE=$1 NEWTAG=$2 } argparse $* # Ensure we're in teh proper directory PWD=`pwd` BN=`basename $PWD` if [[ $BN != "xdr-terraform-modules" ]]; then echo Must be run from xdr-terraform-modules root. exit 1 fi if [[ ! -d ../xdr-terraform-live ]]; then echo Directory \"../xdr-terraform-live\" not found. exit 5 fi if [[ ! -f "variables/${VARIABLEFILE}" ]]; then echo Variable file \"variables/${VARIABLEFILE}\" does not exist. exit 2 fi if [[ ! ${NEWTAG} =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then echo Tag \"${NEWTAG}\" does not appear to be in teh proper format. exit 3 fi # Gather a list of all terragrunt.hcl files in xdr-terraform live. Should be faster to do this one. echo Finding all modules in xdr-terraform-live.. This may take a moment. LIVEMODULES=$( find ../xdr-terraform-live -name "terragrunt.hcl" -type f -depth -mindepth 2 -not -path "*/.terragrunt-cache/*" -not -path "*/000-skeleton/*" ) echo $( echo -n "${LIVEMODULES}" | wc -l ) modules found in xdr-terraform-live. # Gather the modules that use it MODULES=$( find base -type l -name ${VARIABLEFILE} | sed "s#/${VARIABLEFILE}\$##" ) set +e for m in ${MODULES}; do echo ==== ${VARIABLEFILE} used in module $m for l in ${LIVEMODULES}; do if [[ ${DEBUG} > 0 ]]; then echo ======== Searching for string \"$m\" in \"$l\" fi grep --silent ${m} ${l} RES=$? if [[ ${RES} == 0 ]]; then echo ============ $m FOUND in $l sed -E -i .bak 's/ref=v[0-9]+.[0-9]+\.[0-9]+"$/ref='${NEWTAG}'"/' $l fi done done set -e