Skip to content

Instantly share code, notes, and snippets.

@atomantic
Created May 9, 2016 21:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atomantic/7d78ee36331c35d808476fec55c03386 to your computer and use it in GitHub Desktop.
Save atomantic/7d78ee36331c35d808476fec55c03386 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
###########################################
# Install Terraforming and export AWS #
# @author Adam Eivy #
###########################################
###
# echo helper functions
###
# Colors
ESC_SEQ="\x1b["
COL_RESET=$ESC_SEQ"39;49;00m"
COL_RED=$ESC_SEQ"31;01m"
COL_GREEN=$ESC_SEQ"32;01m"
COL_YELLOW=$ESC_SEQ"33;01m"
function ok() {
echo -e "$COL_GREEN[ok]$COL_RESET "$1
}
function bot() {
echo; echo -e "$COL_GREEN\[._.]/$COL_RESET - "$1
}
function running() {
echo; echo -en " ⇒ "$1"..."
}
function action() {
echo -e "$COL_YELLOW[action]$COL_RESET"; echo -en " ⇒ "$1"..."
}
function warn() {
echo -e "$COL_YELLOW[warning]$COL_RESET "$1
}
function error() {
echo -e "$COL_RED[error]$COL_RESET "$1
}
function install_homebrew() {
running "checking homebrew"
brew_bin=$(which brew) 2>&1 > /dev/null
if [[ $? != 0 ]]; then
action "installing homebrew"
ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)"
if [[ $? != 0 ]]; then
error "unable to install homebrew, script $0 abort!"
exit -1
fi
fi
ok
}
function require_brew() {
running "brew $1 $2"
brew list $1 > /dev/null 2>&1 | true
if [[ ${PIPESTATUS[0]} != 0 ]]; then
action "brew install $1 $2"
brew install $1 $2
if [[ $? != 0 ]]; then
error "failed to install $1! aborting..."
# exit -1
fi
fi
ok
}
case "$1" in
init | setup)
bot "Hi, I'm going to setup/update your environment"
echo "initializing dev environment..."
running "checking terraform"
terraform_bin=$(which terraform) 2>&1 > /dev/null
if [[ $? != 0 ]]; then
install_homebrew
action "installing terraform"
require_brew terraform
if [[ $? != 0 ]]; then
error "unable to install homebrew, script $0 abort!"
exit -1
fi
fi
terraforming_bin=$(which terraforming) 2>&1 > /dev/null
if [[ $? != 0 ]]; then
action "installing terraforming"
gem install terraforming
ok
fi
;;
export)
export AWS_REGION=$2
if [[ -z "$AWS_REGION" ]]; then
bot "no AWS_REGION supplied, using us-west-2 as default"
export AWS_REGION="us-west-2"
fi
bot "exporting $AWS_REGION from amazon"
mkdir -p $AWS_REGION
cd $AWS_REGION
bot "writing main.tf file"
rm main.tf 2>&1 > /dev/null
touch main.tf
regional=(asg dbpg dbsg dbsn ec2 ecc ecsn eip elb igw nacl nif r53r r53z rds rs rt rta sg sn vpc)
for i in "${regional[@]}"; do
echo "processing ${i}"
echo "# "${i} >> main.tf
terraforming ${i} --profile default >> main.tf
ok
done
ok
bot "writing global.tf file"
rm global.tf 2>&1 > /dev/null
touch global.tf
global=(iamg iamgm iamgp iamip iamp iamr iamrp iamu iamup s3)
for i in ${global[@]}; do
echo "processing ${i}"
echo "# "${i} >> global.tf
terraforming ${i} --profile default >> global.tf
ok
done
ok
bot "exporting tfstate into terraform.tfstate"
if [[ ! -f "terraform.tfstate" ]]; then
terraforming vpc --profile default --tfstate > terraform.tfstate
fi
terraforming help | grep terraforming | grep -v help | grep -v s3 | awk '{print "terraforming", $2, "--profile", "default", "--tfstate", "--merge=terraform.tfstate", "--overwrite";}' | bash
ok
bot "ok, now you can run 'terraform plan' to make sure there is no change"
;;
*)
bot "Hi, I can do a few helpful development tricks.\n"
echo -e "Run each as an argument to this script (e.g. 'terraformtool.sh init'):\n"
echo "init - setup the needed tools"
echo "export {AWS_REGION} - use terraforming to export current aws account into .tf files (e.g. dev export us-west-2)"
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment