Skip to content

Commit

Permalink
[terraform/upgrade-modules] Update all module sources to latest versi…
Browse files Browse the repository at this point in the history
…on (#95)

* Add script to update all terraform module sources

* Add support for `GITHUB_TOKEN` access key. Install json2hcl.
  • Loading branch information
osterman committed Aug 24, 2018
1 parent a281e21 commit e43a67c
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 1 deletion.
67 changes: 67 additions & 0 deletions bin/upgrade_terraform_modules.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#!/usr/bin/env bash

function github_latest_release() {
local org=$1
local repo=$2
local header=""

if [ -n "$GITHUB_TOKEN" ] ; then
header="Authorization: token $GITHUB_TOKEN"
fi

local ref=$(curl -sSL -H "$header" https://api.github.com/repos/$org/$repo/releases/latest | jq .tag_name -r)
if [ $? -eq 0 ]; then
echo $ref
fi
}

function upgrade_modules() {
local file=$1
echo "Processing $file..."
for source in $(json2hcl -reverse < $file | jq -r '.module | .[][] | first | .source' 2>/dev/null); do
if [[ $source =~ github.com/ ]]; then
echo "[GITHUB]: $source"
if [[ $source =~ github.com/(.*?)/(.*?)\.git ]]; then
org="${BASH_REMATCH[1]}"
repo="${BASH_REMATCH[2]}"
fi
if [[ $source =~ \?ref=([0-9.]+) ]]; then
ref="${BASH_REMATCH[1]}"
fi
if [[ $source =~ \?ref=tags/([0-9.]+) ]]; then
ref="${BASH_REMATCH[1]}"
fi

if [ -z "$org" ] || [ -z "$repo" ] || [ -z "$ref" ]; then
echo " - Failed to parse module source (org: $org, repo: $repo, ref: $ref)"
else
latest_ref=$(github_latest_release "$org" "$repo")
latest_source="git::https://github.com/$org/$repo.git?ref=tags/$latest_ref"
if [ "$latest_source" == "$source" ]; then
echo " - Current: $ref"
else
echo " - Latest: $ref -> ${latest_ref}"
echo " - Source: $latest_source"
sed -i"" "s,$source,$latest_source,g" "$file"
fi
fi
else
echo "[SKIPPED]: $source"
fi
done
}

files=""
if [ $# -eq 0 ]; then
echo "Usage: $0 [all|file1.tf...fileN.tf]"
exit 1
elif [ $1 == "all" ]; then
files=$(find . -type f -name '*.tf')
else
files="$*"
fi

for file in $files; do
upgrade_modules $file
done

2 changes: 1 addition & 1 deletion modules/packages/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export INSTALL_PATH ?= $(BUILD_HARNESS_PATH)/vendor
export PACKAGES_VERSION ?= 0.4.0
export PACKAGES_VERSION ?= 0.8.0
export PACKAGES_PATH ?= $(BUILD_HARNESS_PATH)/vendor/packages

## Delete packages
Expand Down
4 changes: 4 additions & 0 deletions modules/terraform/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,7 @@ terraform/validate:
terraform/lint:
@FAIL=`$(TERRAFORM) fmt -write=false | xargs --no-run-if-empty -n 1 printf '\t- %s\n'`; \
[ -z "$$FAIL" ] || (echo "Terraform configuration needs linting. Run '$(TERRAFORM) fmt'"; echo $$FAIL; exit 1)

## Upgrade all terraform module sources
terraform/upgrade-modules: packages/install/json2hcl
@$(BUILD_HARNESS_PATH)/bin/upgrade_terraform_modules.sh all

0 comments on commit e43a67c

Please sign in to comment.