Last active
June 25, 2019 14:59
-
-
Save ciiqr/9b930d2b8195eba056cbda1943c2a63a to your computer and use it in GitHub Desktop.
Script for upgrading modules to 0.12 which are used by terragrunt (this has several hard coded and specialized parts, thus this should only be used as inspiration)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -e # exit on failure | |
# set -x # print commands before executing them | |
arrayContains() | |
{ | |
declare e match="$1" | |
shift | |
for e; do | |
[[ "$e" == "$match" ]] && return 0 | |
done | |
return 1 | |
} | |
log() | |
{ | |
echo "[terragrunt-0.12upgrade] $@" | |
} | |
logrun() | |
{ | |
log "$@" | |
"$@" | |
} | |
# main | |
declare -a updatedModules=() | |
while read -r terragruntHcl; do | |
declare directory=$(dirname "$terragruntHcl") | |
declare terragruntCache="$directory/.terragrunt-cache" | |
declare module=$(sed -n 's@.*scoremedia/thescore-terraform-modules.git//\([^?]\{1,\}\)\?.*@\1@p' "$terragruntHcl") | |
# not an actual module, just a parent terragrunt.hcl | |
if [[ -z "$module" ]]; then | |
log "skipping $terragruntHcl, not a module instance" | |
continue | |
fi | |
# we've already updated this module | |
if arrayContains "$module" "${updatedModules[@]}"; then | |
log "skipping $terragruntHcl, $module module already updated" | |
continue | |
fi | |
# progress | |
log "updating $module via $directory" | |
# init | |
logrun ts-toolbox terragrunt init --terragrunt-working-dir "$directory" --terragrunt-source /thescore/thescore-terraform-modules/modules --terragrunt-source-update --terragrunt-non-interactive | |
# upgrade | |
logrun ts-toolbox terragrunt 0.12upgrade -yes --terragrunt-working-dir "$directory" --terragrunt-non-interactive | |
# TODO: this copies terragrunt.hcl and versions.tf, skip these | |
# copy updated modules | |
logrun cp -r "$terragruntCache"/*/*/"$module"/* ~/thescore/thescore-terraform-modules/"$module"/ | |
# add to the list of updated modules | |
updatedModules+=("$module") | |
# spacing | |
echo "" | |
done <<< "$(find . -iname terragrunt.hcl -not -path '*/.terragrunt-cache/*')" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment