Browse Source

Adds to helpful scripts to update tags when you change variables

Simple to use!

```
cd ~/xdr-terraform-modules
update_all_that_uses employee_ips.tf v5.0.12
```

That'll update everything in xdr-terraform-live.

Then, after you merge:

```
cd ~/xdr-terraform-live
apply_last_updated_modules
```

And it'll apply everything that had just been merged!
Fred Damstra [afs macbook] 3 years ago
parent
commit
253efd7dfb
2 changed files with 153 additions and 0 deletions
  1. 51 0
      bin/apply_last_updated_modules
  2. 102 0
      bin/update_all_that_uses

+ 51 - 0
bin/apply_last_updated_modules

@@ -0,0 +1,51 @@
+#! /bin/bash
+# 
+# Runs an init and apply in all copies of a module
+#
+# TODO: Would be really nice if this did some input validation, and if you
+# could specify 'local' or not.
+
+
+# Grab a list of the terragrunt.hcls that were updated in the last git commit,
+# but ignore the root terragrunt.hcl, and strip terragrunt.hcl from the end.
+UPDATED_MODULES=$( git diff --name-only HEAD HEAD~1 | grep terragrunt.hcl | egrep -v "^terragrunt.hcl" | sed "s#/terragrunt.hcl##" | sort -r )
+
+PWD=`pwd`
+BN=`basename $PWD`
+if [[ $BN != "xdr-terraform-live" ]]; then
+  echo Must be run from xdr-terraform-live root.
+  exit 1
+fi
+
+echo The following modules will be applied:
+for m in ${UPDATED_MODULES}; do
+  printf '\t%s\n' $m
+done
+echo ""
+
+read -p "Continue [Y/n]? " -n 1 -r
+echo ""
+if [[ $REPLY =~ ^[Nn]$ ]]; then
+  echo Exiting....
+  exit 1
+fi
+
+for m in ${UPDATED_MODULES}; do
+  echo ""
+  echo =============================================
+  echo \ Applying $m
+  echo =============================================
+  pushd $m
+  terragrunt apply
+  EXITCODE=$?
+  if [[ $EXITCODE != 0 ]]; then
+    # Prompt to continue after each failed module.
+    read -p "Terragrunt completed unsuccessfully. Continue to next module [Y/n]? " -n 1 -r
+    echo ""
+    if [[ $REPLY =~ ^[Nn]$ ]]
+    then
+        echo Exiting...
+        exit 2
+    fi
+  fi
+done

+ 102 - 0
bin/update_all_that_uses

@@ -0,0 +1,102 @@
+#! /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/*" )
+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
+