Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[terraform/upgrade-modules] Update all module sources to latest version #95

Merged
merged 6 commits into from
Aug 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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