Created
September 10, 2012 23:13
-
-
Save jibs/3694668 to your computer and use it in GitHub Desktop.
Migrate domain from godaddy to aws route53 via boto (mostly)
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
import boto | |
from boto.route53.record import ResourceRecordSets | |
from collections import defaultdict | |
zone_name = 'yourdomain.com.' | |
zone_id = "ZONEID-HERE" | |
conn = boto.connect_route53() | |
zone = conn.get_hosted_zone(zone_id) | |
supported = ['A', 'CNAME', 'MX', 'TXT', 'AAAA'] | |
# rrsets = conn.get_all_rrsets(zone_id) | |
def parse_bfile(fname): | |
f = open(fname, 'r') | |
result = defaultdict(list) | |
res_key = '' | |
for line in f: | |
if line == '\n' or 'SOA' in line: | |
continue | |
if line.startswith(';'): | |
res_key = line.replace('; ', '').replace(' Records', '').strip() | |
# print res_key | |
continue | |
if res_key in supported: | |
result[res_key].append(line.strip()) | |
return result | |
def add_change(rec_type, name, vals, ttl=60): | |
print("Adding record %s, %s" % (name, vals)) | |
if name == '@': | |
name = '' | |
try: | |
changes = ResourceRecordSets(conn, zone_id) | |
if name: | |
qual_name = '%s.%s' % (name, zone_name) | |
else: | |
qual_name = zone_name | |
change = changes.add_change("CREATE", qual_name, | |
rec_type, ttl) | |
for val in vals: | |
if val == '@': | |
val = zone_name[:-1] | |
print val | |
change.add_value(val) | |
changes.commit() | |
except Exception, e: | |
print("---- Record Add Failure ----") | |
print(e) | |
print("---- Record Add Failure ----") | |
def main(): | |
"""docstring for main""" | |
results = parse_bfile("./YOURDOMAIN.com.zone") | |
# tmp | |
ignore = set(['MX', 'TXT']) | |
for rec_type in (r for r in results if r not in ignore): | |
print("Processing %s records" % rec_type) | |
for record in results[rec_type]: | |
r = record.split('\t') | |
add_change(rec_type, r[0], [r[-1]], ttl=int(r[1])) | |
if 'MX' in results: | |
vals = [r.split('\t')[-2] + ' ' + r.split('\t')[-1] for r in results['MX']] | |
add_change(rec_type, r[0], [vals], ttl=3600) | |
if 'TXT' in results: | |
vals = [r.split('\t')[-1] for r in results['TXT']] | |
print "Adding: ", record | |
add_change(rec_type, r[0], vals, ttl=3600) | |
# changes = ResourceRecordSets(conn, zone_id) | |
# change = changes.add_change("CREATE", 'foo1.%s' % zone_name, "A", 60) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this is a insanely hacky quick script i needed to knock together once - route53 documentation was very sparse when i wrote this, and incase that hasnt changed - i hope this may be of some use..