Created
May 31, 2012 22:47
-
-
Save mattbailey/2846956 to your computer and use it in GitHub Desktop.
This file contains 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 | |
SLD="<%= @domain %>" | |
AWSID="<%= @awsid %>" | |
AWSKEY="<%= @awskey %>" | |
SHORTHOST=`echo <%= node[:this_server] %> | sed 's/_/-/g' | sed "s/.${SLD}//"` | |
HOSTNAME="${SHORTHOST}.${SLD}" | |
cat > ~/.aws-secrets << EOF | |
%awsSecretAccessKeys = ( | |
"aws" => { | |
id => "$AWSID", | |
key => "$AWSKEY", | |
}, | |
); | |
EOF | |
chmod 600 ~/.aws-secrets | |
sudo rm -f /tmp/lastdns.log | |
echo "*** FINDING PASTHOST AND CURHOST" > /tmp/lastdns.log | |
PASTHOST=`dig ${HOSTNAME} | grep CNAME | awk '{print $NF}' | rev | cut -b 2-| rev 2>> /tmp/lastdns.log` | |
CURHOST=`curl -s http://169.254.169.254/latest/meta-data/public-hostname 2>> /tmp/lastdns.log` | |
echo "HOSTNAME: $HOSTNAME" >> /tmp/lastdns.log | |
echo "PASTHOST: $PASTHOST" >> /tmp/lastdns.log | |
echo "CURHOST: $CURHOST" >> /tmp/lastdns.log | |
cat > /tmp/r53create.xml << EOF | |
<?xml version="1.0" encoding="UTF-8"?> | |
<ChangeResourceRecordSetsRequest xmlns="https://route53.amazonaws.com/doc/2011-05-05/"> | |
<ChangeBatch> | |
<Comment> | |
Update record | |
</Comment> | |
<Changes> | |
<Change> | |
<Action>CREATE</Action> | |
<ResourceRecordSet> | |
<Name>$HOSTNAME</Name> | |
<Type>CNAME</Type> | |
<TTL>300</TTL> | |
<ResourceRecords> | |
<ResourceRecord> | |
<Value>$CURHOST</Value> | |
</ResourceRecord> | |
</ResourceRecords> | |
</ResourceRecordSet> | |
</Change> | |
</Changes> | |
</ChangeBatch> | |
</ChangeResourceRecordSetsRequest> | |
EOF | |
cat > /tmp/r53update.xml << EOF | |
<?xml version="1.0" encoding="UTF-8"?> | |
<ChangeResourceRecordSetsRequest xmlns="https://route53.amazonaws.com/doc/2011-05-05/"> | |
<ChangeBatch> | |
<Comment> | |
Update record | |
</Comment> | |
<Changes> | |
<Change> | |
<Action>DELETE</Action> | |
<ResourceRecordSet> | |
<Name>$HOSTNAME</Name> | |
<Type>CNAME</Type> | |
<TTL>300</TTL> | |
<ResourceRecords> | |
<ResourceRecord> | |
<Value>$PASTHOST</Value> | |
</ResourceRecord> | |
</ResourceRecords> | |
</ResourceRecordSet> | |
</Change> | |
<Change> | |
<Action>CREATE</Action> | |
<ResourceRecordSet> | |
<Name>$HOSTNAME</Name> | |
<Type>CNAME</Type> | |
<TTL>300</TTL> | |
<ResourceRecords> | |
<ResourceRecord> | |
<Value>$CURHOST</Value> | |
</ResourceRecord> | |
</ResourceRecords> | |
</ResourceRecordSet> | |
</Change> | |
</Changes> | |
</ChangeBatch> | |
</ChangeResourceRecordSetsRequest> | |
EOF | |
if [ ! -f /tmp/dnscurl.pl ]; then | |
echo "*** dnscurl.pl NOT FOUND, DOWNLOADING" >> /tmp/lastdns.log | |
wget -q http://awsmedia.s3.amazonaws.com/catalog/attachments/dnscurl.pl -O /tmp/dnscurl.pl 2>> /tmp/lastdns.log | |
fi | |
chmod +x /tmp/dnscurl.pl | |
echo "*** ATTEMPTING CREATE" >> /tmp/lastdns.log | |
/tmp/dnscurl.pl --keyname aws -- -s -H "Content-Type: text/xml; charset=UTF-8" -X POST --upload-file /tmp/r53create.xml https://route53.amazonaws.com/2011-05-05/hostedzone/<%= @zoneid %>/rrset &>> /tmp/lastdns.log | |
echo "\n*** ATTEMPTING DELETE/CREATE" >> /tmp/lastdns.log | |
/tmp/dnscurl.pl --keyname aws -- -s -H "Content-Type: text/xml; charset=UTF-8" -X POST --upload-file /tmp/r53update.xml https://route53.amazonaws.com/2011-05-05/hostedzone/<%= @zoneid %>/rrset &>> /tmp/lastdns.log | |
unlink /tmp/r53create.xml | |
unlink /tmp/r53update.xml | |
unlink ~/.aws-secrets |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment