#!/bin/bash
#------------------------------------------------------------------------------
set -u
# Terraform Demo Driver script.
# This is a wrapper to Robert Cowham's project:
# https://swarm.workshop.perforce.com/projects/perforce_software-sdp-cloud-deploy
function msg () { echo -e "$*"; }
function errmsg () { msg "\nError: ${1:-Unknown Error}\n"; }
function bail () { errmsg "${1:-Unknown Error}"; exit ${2:-1}; }
# Function cmd("echo Hello", "Description of command.")
function cmd () {
local cmdAndArgs="${1:-echo}"
local desc="${2:-}"
local log=$TmpFile
local -i ec=0
[[ -n "$desc" ]] && msg "$desc"
msg "Executing: $cmdAndArgs"
$cmdAndArgs > $log 2>&1
ec=$?
echo "EXIT_CODE: $ec" >> $log
cat $log
return $ec
}
function do_preflight_checks()
{
msg "Preflight checks"
local util utilPath
local -i errors=0
local -i checks=0
for util in ansible terraform; do
checks+=1
utilPath="$(which $util 2>/dev/null)"
if [[ -n "$utilPath" ]]; then
msg "Verified: PATH contains $util."
else
errmsg "Could not find [$util] in PATH: $PATH"
errors+=1
fi
done
for file in ~/.aws/config ~/.aws/credentials ~/.ssh/id_rsa.pub; do
checks+=1
if [[ -r "$file" ]]; then
msg "Verified: File exists: $file"
else
errmsg "Missing file: $file"
errors+=1
fi
done
if [[ $errors -eq 0 ]]; then
msg "Preflight detected no issues in $checks checks."
else
errmsg "Preflight detected $errors issues in $checks checks."
fi
return $errors
}
declare Version=1.0.6
declare DemoRoot=/home/perforce/tfdemo
declare P4CloneCmd="p4 -u ftp clone -p public.perforce.com:1666 -f //guest/perforce_software/sdp-cloud-deploy/main/..."
declare TmpFile=$(mktemp)
declare SampleAZ="eu-west-1"
declare MyAZ="us-east-2"
declare SampleProfile=robert_dev
declare MyProfile="sdp-deploy"
do_preflight_checks || bail "Preflight checks failed."
if [[ -d "$DemoRoot" ]]; then
cmd "rm -rf $DemoRoot" || bail "Failed to remove dir: $DemoRoot"
fi
cmd "mkdir -p $DemoRoot" || bail "Failed to mkdir: $DemoRoot"
cd "$DemoRoot" || bail "Couldn't cd to: $DemoRoot"
cmd "$P4CloneCmd" "Clone SDP Cloud Deploy Terraform project files."
cmd "terraform init" "Initializing Terraform."
msg "Replace sample AWS AZ with my preferred AZ, $MyAZ."
for f in $(grep -lr "$SampleAZ" *); do
msg " Updating AZ in $f"
sed -e "s/$SampleAZ/$MyAZ/g" $f > "$f.new" || bail "Substitution failed."
mv "$f.new" "$f" || bail "This failed: mv $f.new $f"
done
msg "Replace sample AWS profile with my preferred profile, $MyProfile."
for f in $(grep -lr "$SampleProfile" *); do
msg " Updating profile in $f"
sed -e "s/$SampleProfile/$MyProfile/g" $f > "$f.new" || bail "Substitution failed."
mv "$f.new" "$f" || bail "This failed: mv $f.new $f"
done
msg "Renamed <AZ>.tfvars file"
for f in $(find . -type f -name "$SampleAZ.*"); do
newF=$(echo $f | sed -e "s/$SampleAZ/$MyAZ/g")
mv "$f" "$newF" || bail "This failed: mv $f $newF"
done
cmd "terraform plan -var-file=env/dev/$MyAZ.tfvars" \
"Terraform Planning ..." ||\
bail "Terraform plan failed."
cmd "terraform apply" \
"Applying Terraform Plan ..." ||\
bail "Terraform apply failed."
exit 0