Skip to content

Instantly share code, notes, and snippets.

@chhibber
Created January 14, 2014 06:32
Show Gist options
  • Select an option

  • Save chhibber/8414044 to your computer and use it in GitHub Desktop.

Select an option

Save chhibber/8414044 to your computer and use it in GitHub Desktop.
AWS - Bootstrap Examples
#!/bin/bash -
#===============================================================================
# vim: softtabstop=4 shiftwidth=4 expandtab fenc=utf-8 spell spelllang=en
#===============================================================================
set -x
set -e
SALT_BOOTSTRAP_SCRIPT="salt-bootstrap.sh"
SALT_BOOTSTRAP_DOWNLOAD="http://bootstrap.saltstack.org"
exec 2>&1 > /tmp/awsbootstrap.log
# ===== FUNCTION ================================================================
# NAME: log
# DESCRIPTION:
# ===============================================================================
log() {
echo `date` : "$@"
}
# ===== FUNCTION ================================================================
# NAME: __download_salt_bootstrap_script
# DESCRIPTION: Retrieves a URL and writes it to a given path
# ===============================================================================
__download_salt_bootstrap_script() {
curl -L --insecure -o ${SALT_BOOTSTRAP_SCRIPT} ${SALT_BOOTSTRAP_DOWNLOAD} >/dev/null 2>&1 ||
wget --no-check-certificate -O ${SALT_BOOTSTRAP_SCRIPT} ${SALT_BOOTSTRAP_DOWNLOAD} >/dev/null 2>&1 ||
fetch -q -o ${SALT_BOOTSTRAP_SCRIPT} "$SALT_BOOTSTRAP_DOWNLOAD" >/dev/null 2>&1
}
log "#################################################################################################"
log "# Starting System Bootstrap #"
log "#################################################################################################"
export EC2_HOME=/opt/aws/apitools/ec2
export JAVA_HOME=/usr/lib/jvm/jre
#
# Obtain info on our instance based on metadata info and tags
#
export INSTANCE_ID=`/opt/aws/bin/ec2-metadata -i | awk '{print $2}'`
export PLACEMENT=`/opt/aws/bin/ec2-metadata | grep -w ^placement | awk '{ print $2 }'`
export AVAILABILITY_ZONE=`/opt/aws/bin/ec2-metadata -z | awk '{print $2}'`
export AWS_DEFAULT_REGION=${AVAILABILITY_ZONE%?}
export SERVER_ROLE=`aws ec2 describe-tags --filters "Name=resource-id,Values=${INSTANCE_ID}" --output=text | \
grep -w Role | awk '{print $5}'`
export SERVER_ENV=`aws ec2 describe-tags --filters "Name=resource-id,Values=${INSTANCE_ID}" --output=text | \
grep -w Env | awk '{print $5}'`
export SALTVERSION=`aws ec2 describe-tags --filters "Name=resource-id,Values=${INSTANCE_ID}" --output=text | \
grep -w Saltversion | awk '{print $5}'`
export ETH0_MAC=`/sbin/ifconfig | /bin/grep eth0 | \
awk '{print tolower($5)}' | grep '^[0-9a-f]\{2\}\(:[0-9a-f]\{2\}\)\{5\}$'`
export VPC_CIDR_URI="http://169.254.169.254/latest/meta-data/network/interfaces/macs/${ETH0_MAC}/vpc-ipv4-cidr-block"
export VPC_CIDR_RANGE=`curl --retry 3 --retry-delay 0 --silent --fail ${VPC_CIDR_URI}`
if [ -z "$SALTVERSION" ]; then
echo "The saltversion is not set, exiting"
exit
fi
# Download Salt Bootstrap File
#echo "Downloading Salt Bootstrap file and setting permissions"
#__download_salt_bootstrap_script
#chmod 550 ${SALT_BOOTSTRAP_SCRIPT}
#
# Set the hostname and Name tag. We want to set the hostname before the install and starts
# the salt-minion. This way the key name stays consistent on the salt master.
# ie. Instead of getting a host named ip_10_20_125_12 we get us-east1-production-www-c123433
#
AWS_NAME="${PLACEMENT}-${SERVER_ENV}-${SERVER_ROLE}-${INSTANCE_ID}"
hostname $AWS_NAME
/usr/bin/aws ec2 create-tags --resources ${INSTANCE_ID} --tags Key=Name,Value=${AWS_NAME}
#
# Enable the EPEL repo that is already installed on AMI image
#
yum-config-manager --enable epel
yum -y install git
mkdir -p /etc/salt/pki/master
# Remove any old dependencies of salt
/bin/rm -r /tmp/git || echo "Moving along..."
/bin/rm -r /srv/salt || echo "Moving along..."
# Install all things salt!
# /salt-bootstrap.sh -P git v${SALTVERSION}
#
# Role = Salt = Instal salt-master
#
if [ $SERVER_ROLE == "salt" ]; then
/usr/bin/yum -y install salt-master
# Copy salt keys over. We maintain these so that the minions
# don't have issues connecting
/bin/cp /tmp/us-west-2-development-salt-master.pub /etc/salt/pki/master/master.pub
/bin/cp /tmp/us-west-2-development-salt-master.pem /etc/salt/pki/master/master.pem
# Setup Salt Master
mkdir -p /srv/salt
git clone [email protected]:repo.git /srv/salt
echo "Writing out salt-master configuration file"
echo "auto_accept: True" > /etc/salt/master
echo '' >> /etc/salt/master
echo 'autosign_file: /etc/salt/autosign.conf' >> /etc/salt/master
echo '' >> /etc/salt/master
echo "file_roots:" >> /etc/salt/master
echo " base:" >> /etc/salt/master
echo " - /srv/salt" >> /etc/salt/master
echo '' >> /etc/salt/master
echo "pillar_roots:" >> /etc/salt/master
echo " base:" >> /etc/salt/master
echo " - /srv/salt/pillar" >> /etc/salt/master
echo '' >> /etc/salt/master
echo "log_level: info" >> /etc/salt/master
echo "${VPC_CIDR_RANGE}" > /etc/salt/autosign.conf
echo "Starting salt-master..."
/usr/bin/salt-master -d -c /etc/salt
sleep 35
fi
/usr/bin/yum -y install salt-minion
#
# Determine what salt server to point to. Need to make this smarter...
#
[[ "$SERVER_ENV" != "production" ]] && SALTMASTER="${AWS_DEFAULT_REGION}-development-salt.FOOBAR.com"
[[ "$SERVER_ENV" == "production" ]] && SALTMASTER="${AWS_DEFAULT_REGION}-production-salt.FOOBAR.com"
echo "Writing out salt-minion configuration file..."
echo "master: ${SALTMASTER}" > /etc/salt/minion
echo '' >> /etc/salt/minion
echo 'grains:' >> /etc/salt/minion
echo ' roles:' >> /etc/salt/minion
echo " - ${SERVER_ROLE}" >> /etc/salt/minion
echo " environment: ${SERVER_ENV}" >> /etc/salt/minion
echo '' >> /etc/salt/minion
echo 'log_level: info' >> /etc/salt/minion
#
# Start the salt-minion
# restart it after 10 seconds
#
service salt-minion start
# Wait for salt-minion to start
sleep 5
#
# Ensure the salt-minion is connected to salt-master
#
TIMEOUT=120
COUNT=0
while [ ! -f /etc/salt/pki/minion/minion_master.pub ]; do
echo "Waiting for salt to register with master."
if [ "$COUNT" -ge "$TIMEOUT" ]; then
echo "minion_master.pub not detected by timeout"
exit 1
fi
sleep 5
COUNT=$((COUNT+5))
done
sleep 5
echo "Install base requirements"
salt-call state.sls baseRequirements
#
# Send a notification to hip chat
#
curl -d \
"room_id=Development+Notifications&from=AWS+Bootstrap&\
message=\
AMI+Info:+${INSTANCE_ID}+\
Environment:+${SERVER_ENV}+\
Role:+${SERVER_ROLE}+\
+::+Calling+salt.highsate\
&color=green"\
https://api.hipchat.com/v1/rooms/message?auth_token=YOURTOKEN&format=json
echo "Calling salt.highstate"
salt-call state.highstate
if [ "$SERVER_ROLE" == "www" ] || [ "$SERVER_ROLE" == "worker" ]; then
#
# Send a notification to hip chat
#
curl -d \
"room_id=Development+Notifications&from=AWS+Bootstrap&\
message=\
AMI+Info:+${INSTANCE_ID}+\
Environment:+${SERVER_ENV}+\
Role:+${SERVER_ROLE}+\
+::+Deploying+FOOBAR+Application\
&color=green"\
https://api.hipchat.com/v1/rooms/message?auth_token=YOURTOKEN&format=json
echo "Calling salt-call state.sls FOOBARApp.deploy"
salt-call state.sls FOOBARApp.deploy
#
# After the deploy enable god and start the service
#
echo "Enabling and installing god"
salt-call state.sls god
#
# Send a notification to hip chat
#
curl -d \
"room_id=Development+Notifications&from=AWS+Bootstrap&\
message=\
AMI+Info:+${INSTANCE_ID}+\
Environment:+${SERVER_ENV}+\
Role:+${SERVER_ROLE}+\
+::+Application+Deploy+Complete\
&color=green"\
https://api.hipchat.com/v1/rooms/message?auth_token=YOURTOKEN&format=json
fi
log "#################################################################################################"
log "# Finished System Bootstrap #"
log "################################################################################################
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment