#!/bin/bash
#
# /etc/rc.d/init.d/
#
# Registers DNS with Route 53
#
# chkconfig 345 20 80
#
# Source function library.
. /etc/init.d/functions
# Best practice here is to create an IAM user/group with access to only
# Route53 and use this keypair.
export AWS\\_ACCESS\\_KEY\\_ID=
export AWS\\_SECRET\\_ACCESS\\_KEY=
# Use the domain you have configured in Route 53
export DOMAIN=ianbeyer.com
# This is the hostname of the Weighted Round Robin Group.
export RR=wrr
# Gets the current public hostname - you don't want to use an A record with
# the IP because the CNAME works in conjunction with Amazon's internal DNS
# to correctly resolve instances to their internal address
export PUB\\_HOSTNAME=`curl -s --fail http://169.254.169.254/latest/meta-data/public-hostname`
# Gets the Route 53 Zone ID
export ZONEID=`route53 ls | awk '($2 == "ID:"){printf "%s ",$3;getline;printf "%s\n",$3}' | grep $DOMAIN | awk '{print $1}'`
start() {
echo -n "Registering host with Route 53 : "
# This is the base name of the host you want to use. It will increase the index
# number until it finds one that's not in use, and then use that.
export HOST=amz
export HOSTINDEX=1
INUSE=`route53 get $ZONEID | grep ${HOST}${HOSTINDEX}\.${DOMAIN} | wc -l`
while [[ $INUSE > 0 ]]
do
HOSTINDEX=$((HOSTINDEX + 1))
INUSE=`route53 get $ZONEID | grep ${HOST}${HOSTINDEX}\.${DOMAIN} | wc -l`
done
if [[ "$HOSTINDEX" == "1" ]]; then
FQDN="${HOST}${HOSTINDEX}.${DOMAIN}"
else
# set the new fqdn hostname and shortname
FQDN="${HOST}${HOSTINDEX}.${DOMAIN}"
SHORTNAME="${HOST}${HOSTINDEX}"
fi
# Set the instance hostname -- If you want to make sure that bash
# updates the prompt, run "exec bash" after the script. If you do so
# in the script, bad stuff happens on startup.
hostname $FQDN
echo -n $FQDN
# Add the DNS record
RESULT=`route53 add\\_record $ZONEID $FQDN CNAME $PUB\\_HOSTNAME | grep "PENDING"`
if [[ "$RESULT" == "" ]]; then
echo "... failed.";
else
echo "... success.";
fi
# Add the CNAME record
echo -n "Adding host to round-robin group...";
# Checking to make sure it's not already there.
CNAME=`route53 get $ZONEID | grep $RR | grep $PUB\\_HOSTNAME | wc -l`
if [[ $CNAME = 0 ]]; then
RESULT=`route53 add\\_record $ZONEID $RR.$DOMAIN CNAME $PUB\\_HOSTNAME 60 ${HOST}${HOSTINDEX} 1 | grep "PENDING"`
if [[ "$RESULT" == "" ]]; then
echo "... failed.";
else
echo "... success.";
fi
else
echo "already exists, ignoring";
fi
}
stop() {
echo -n "Deregistering host with Route 53 : "
HOST=`hostname | cut -f1 -d.`
FQDN=`hostname`
# check to make sure it exists in Route53
CNAME=`route53 get $ZONEID | grep $FQDN | grep $PUB\\_HOSTNAME | wc -l`
if [[ $CNAME > 0 ]]; then
RESULT=`route53 del\\_record $ZONEID $FQDN CNAME $PUB\\_HOSTNAME | grep "PENDING"`
if [[ "$RESULT" == "" ]]; then
echo "... failed.";
else
echo "... success.";
fi
else
echo "... not found, ignoring";
fi
echo -n "Deregistering host from RR CNAME..."
# Checking to make sure it exists
CNAME=`route53 get $ZONEID | grep $RR | grep $PUB\\_HOSTNAME | wc -l`
if [[ $CNAME > 0 ]]; then
RESULT=`route53 del\\_record $ZONEID $RR.$DOMAIN CNAME $PUB\\_HOSTNAME 60 ${HOST} 1 | grep "PENDING"`
if [[ "$RESULT" == "" ]]; then
echo "... failed.";
else
echo "... success.";
fi
else
echo "... not found, ignoring";
fi
# resets the hostname to default
hostname `curl -s --fail http://169.254.169.254/latest/meta-data/hostname`
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
\\*)
echo "Usage: {start|stop|restart}"
exit 1
;;
esac
exit $?