Skip to content

Commit

Permalink
Merge branch 'master' into fix1094
Browse files Browse the repository at this point in the history
  • Loading branch information
cofyc committed Nov 14, 2019
2 parents 0e44d3c + 3692c17 commit 9ad04a9
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 19 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,6 @@ manifests/crd-verify.yaml
/tkctl
/.go-version
kubeconfig

# local output directory
/output/
30 changes: 11 additions & 19 deletions hack/check-terraform.sh
Original file line number Diff line number Diff line change
@@ -1,38 +1,30 @@
#!/usr/bin/env bash

tf_version=0.12.12
set -o errexit
set -o nounset
set -o pipefail

echo "Version info: $(terraform version)"
if which terraform >/dev/null; then
version=$(terraform version|head -1|awk '{print $2}')
if [ ${version} == v${tf_version} ]; then
TERRAFORM=terraform
fi
fi
ROOT=$(unset CDPATH && cd $(dirname "${BASH_SOURCE[0]}")/.. && pwd)
source $ROOT/hack/lib.sh

if [ -z "${TERRAFORM}" ]; then
echo "Installing Terraform (${tf_version})..."
GO111MODULE=on go get github.com/hashicorp/terraform@v${tf_version}
TERRAFORM=$GOPATH/bin/terraform
fi
hack::ensure_terraform

root_dir=$(unset CDPATH && cd $(dirname "${BASH_SOURCE[0]}")/.. && pwd)
terraform_modules=$(find ${root_dir}/deploy -not -path '*/\.*' -type f -name variables.tf | xargs dirname)
terraform_modules=$(find ${ROOT}/deploy -not -path '*/\.*' -type f -name variables.tf | xargs dirname)

for module in $terraform_modules; do
echo "Checking module ${module}"
cd ${module}
if ${TERRAFORM} fmt -check >/dev/null; then
if ${TERRAFORM_BIN} fmt -check >/dev/null; then
echo "Initialize module ${module}..."
${TERRAFORM} init >/dev/null
if ${TERRAFORM} validate > /dev/null; then
${TERRAFORM_BIN} init >/dev/null
if ${TERRAFORM_BIN} validate > /dev/null; then
continue
else
echo "terraform validate failed for ${module}"
exit 1
fi
else
echo "terraform fmt -check failed for ${module}"
terraform fmt
${TERRAFORM_BIN} fmt
fi
done
42 changes: 42 additions & 0 deletions hack/lib.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/bin/bash

if [ -z "$ROOT" ]; then
echo "error: ROOT should be initialized"
exit 1
fi

OS=$(go env GOOS)
ARCH=$(go env GOARCH)
OUTPUT=${ROOT}/output
OUTPUT_BIN=${OUTPUT}/bin
TERRAFORM_BIN=${OUTPUT_BIN}/terraform
TERRAFORM_VERSION=0.12.12

test -d "$OUTPUT_BIN" || mkdir -p "$OUTPUT_BIN"

function hack::verify_terraform() {
if test -x "$TERRAFORM_BIN"; then
local v=$($TERRAFORM_BIN version | awk '/^Terraform v.*/ { print $2 }' | sed 's#^v##')
[[ "$v" == "$TERRAFORM_VERSION" ]]
return
fi
return 1
}

function hack::install_terraform() {
echo "Installing terraform v$TERRAFORM_VERSION..."
local tmpdir=$(mktemp -d)
trap "test -d $tmpdir && rm -r $tmpdir" RETURN
pushd $tmpdir > /dev/null
wget https://releases.hashicorp.com/terraform/${TERRAFORM_VERSION}/terraform_${TERRAFORM_VERSION}_${OS}_${ARCH}.zip
unzip terraform_${TERRAFORM_VERSION}_${OS}_${ARCH}.zip
mv terraform $TERRAFORM_BIN
popd > /dev/null
chmod +x $TERRAFORM_BIN
}

function hack::ensure_terraform() {
if ! hack::verify_terraform; then
hack::install_terraform
fi
}

0 comments on commit 9ad04a9

Please sign in to comment.