Last active
August 19, 2019 15:19
-
-
Save sbz/7fef8c93d1e1e27286d8cacdf086c068 to your computer and use it in GitHub Desktop.
Initialize terraform environment
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
#!/bin/bash | |
set -o errexit | |
set -o nounset | |
set -o pipefail | |
readonly TF_VERSION="v0.11" | |
output_line() { | |
local message=$1 | |
if [[ $(tty -s) -ne 0 ]]; then | |
echo "$message" | |
else | |
echo -e "\033[1m\033[32m[+] $message\033[0m" | |
fi | |
} | |
handle_version() { | |
declare terraform_path | |
declare -a terraform_version_output | |
terraform_path=$(command -v terraform) | |
[[ ! -f ${terraform_path} ]] && { | |
echo "Terraform not found. Please run: brew install terraform@${TF_VERSION:1}" | |
exit 1 | |
} | |
while IFS='' read -r line; do | |
terraform_version_output+=("$line"); | |
done < <(${terraform_path} version) | |
current_version=${terraform_version_output[1]%.*} | |
if [[ $current_version != "${TF_VERSION}" ]]; then | |
echo "Wrong version detected. You need to install ${TF_VERSION}. Exiting" | |
exit 1 | |
fi | |
} | |
handle_region() { | |
# The terraform-provicer-ct below is not managed automatically by terraform init | |
# https://github.com/poseidon/terraform-provider-ct | |
readonly TF_CT_VERSION="v0.4.0" | |
readonly TF_CT_SHASUM="dec5b2735b45b06d9c30ba87e05ebf119c02d2e4facadf4cd79befde9b1fbcce" | |
output_line "Initializing $folder config" | |
if [[ ! -f .terraform/plugins/darwin_amd64/terraform-provider-ct_${TF_CT_VERSION} ]]; then | |
mkdir -p .terraform/plugins/darwin_amd64 &>/dev/null | |
cd .terraform/plugins/darwin_amd64 &>/dev/null | |
curl -s -L -O https://github.com/poseidon/terraform-provider-ct/releases/download/${TF_CT_VERSION}/terraform-provider-ct-${TF_CT_VERSION}-darwin-amd64.tar.gz | |
tar xzC . -f terraform-provider-ct-${TF_CT_VERSION}-darwin-amd64.tar.gz terraform-provider-ct-${TF_CT_VERSION}-darwin-amd64/terraform-provider-ct | |
mv terraform-provider-ct-${TF_CT_VERSION}-darwin-amd64/terraform-provider-ct ./terraform-provider-ct_${TF_CT_VERSION} | |
rm -rf terraform-provider-ct-${TF_CT_VERSION}-darwin-amd64/ terraform-provider-ct-${TF_CT_VERSION}-darwin-amd64.tar.gz | |
if ! echo "${TF_CT_SHASUM} ${PWD}/terraform-provider-ct_${TF_CT_VERSION}" | | |
sha256sum --check --quiet; then | |
echo "Wrong provider sum. Exiting" | |
exit 1 | |
fi | |
fi | |
terraform init -upgrade | |
cd - &>/dev/null | |
} | |
handle_global() { | |
output_line "Initializing $folder config" | |
terraform init -upgrade | |
} | |
main() { | |
handle_version | |
declare -a folders=("global" "region") | |
for folder in "${folders[@]}"; do | |
pushd "$folder" &>/dev/null | |
if [[ $folder == "region" ]]; then | |
handle_region | |
elif [[ $folder == "global" ]]; then | |
handle_global | |
fi | |
popd &>/dev/null | |
done | |
output_line "Terraform configuration completed" | |
} | |
if [[ "${BASH_SOURCE[0]}" = "$0" ]]; then | |
main | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Validated using shellcheck linter