4
# route53 is similar to sdbadmin for Route53, it's a simple
5
# console utility to perform the most frequent tasks with Route53
7
def _print_zone_info(zoneinfo):
9
print "| ID: %s" % zoneinfo['Id'].split("/")[-1]
10
print "| Name: %s" % zoneinfo['Name']
11
print "| Ref: %s" % zoneinfo['CallerReference']
13
print zoneinfo['Config']
17
def create(conn, hostname, caller_reference=None, comment=''):
18
"""Create a hosted zone, returning the nameservers"""
19
response = conn.create_hosted_zone(hostname, caller_reference, comment)
20
print "Pending, please add the following Name Servers:"
21
for ns in response.NameServers:
24
def delete_zone(conn, hosted_zone_id):
25
"""Delete a hosted zone by ID"""
26
response = conn.delete_hosted_zone(hosted_zone_id)
30
"""List all hosted zones"""
31
response = conn.get_all_hosted_zones()
32
for zoneinfo in response['ListHostedZonesResponse']['HostedZones']:
33
_print_zone_info(zoneinfo)
35
def get(conn, hosted_zone_id, type=None, name=None, maxitems=None):
36
"""Get all the records for a single zone"""
37
response = conn.get_all_rrsets(hosted_zone_id, type, name, maxitems=maxitems)
38
# If a maximum number of items was set, we limit to that number
39
# by turning the response into an actual list (copying it)
40
# instead of allowing it to page
42
response = response[:]
43
print '%-40s %-5s %-20s %s' % ("Name", "Type", "TTL", "Value(s)")
44
for record in response:
45
print '%-40s %-5s %-20s %s' % (record.name, record.type, record.ttl, record.to_print())
48
def add_record(conn, hosted_zone_id, name, type, values, ttl=600, comment=""):
49
"""Add a new record to a zone"""
50
from boto.route53.record import ResourceRecordSets
51
changes = ResourceRecordSets(conn, hosted_zone_id, comment)
52
change = changes.add_change("CREATE", name, type, ttl)
53
for value in values.split(','):
54
change.add_value(value)
55
print changes.commit()
57
def del_record(conn, hosted_zone_id, name, type, values, ttl=600, comment=""):
58
"""Delete a record from a zone"""
59
from boto.route53.record import ResourceRecordSets
60
changes = ResourceRecordSets(conn, hosted_zone_id, comment)
61
change = changes.add_change("DELETE", name, type, ttl)
62
for value in values.split(','):
63
change.add_value(value)
64
print changes.commit()
66
def add_alias(conn, hosted_zone_id, name, type, alias_hosted_zone_id, alias_dns_name, comment=""):
67
"""Add a new alias to a zone"""
68
from boto.route53.record import ResourceRecordSets
69
changes = ResourceRecordSets(conn, hosted_zone_id, comment)
70
change = changes.add_change("CREATE", name, type)
71
change.set_alias(alias_hosted_zone_id, alias_dns_name)
72
print changes.commit()
74
def del_alias(conn, hosted_zone_id, name, type, alias_hosted_zone_id, alias_dns_name, comment=""):
75
"""Delete an alias from a zone"""
76
from boto.route53.record import ResourceRecordSets
77
changes = ResourceRecordSets(conn, hosted_zone_id, comment)
78
change = changes.add_change("DELETE", name, type)
79
change.set_alias(alias_hosted_zone_id, alias_dns_name)
80
print changes.commit()
82
def change_record(conn, hosted_zone_id, name, type, values, ttl=600, comment=""):
83
"""Delete and then add a record to a zone"""
84
from boto.route53.record import ResourceRecordSets
85
changes = ResourceRecordSets(conn, hosted_zone_id, comment)
86
response = conn.get_all_rrsets(hosted_zone_id, type, name, maxitems=1)[0]
87
change1 = changes.add_change("DELETE", name, type, response.ttl)
88
for old_value in response.resource_records:
89
change1.add_value(old_value)
90
change2 = changes.add_change("CREATE", name, type, ttl)
91
for new_value in values.split(','):
92
change2.add_value(new_value)
93
print changes.commit()
95
def help(conn, fnc=None):
96
"""Prints this help message"""
98
self = sys.modules['__main__']
101
cmd = getattr(self, fnc)
104
if not inspect.isfunction(cmd):
105
print "No function named: %s found" % fnc
107
(args, varargs, varkw, defaults) = inspect.getargspec(cmd)
109
print "Usage: %s %s" % (fnc, " ".join([ "[%s]" % a for a in args[1:]]))
111
print "Usage: route53 [command]"
112
for cname in dir(self):
113
if not cname.startswith("_"):
114
cmd = getattr(self, cname)
115
if inspect.isfunction(cmd):
117
print "\t%-20s %s" % (cname, doc)
121
if __name__ == "__main__":
124
conn = boto.connect_route53()
125
self = sys.modules['__main__']
126
if len(sys.argv) >= 2:
128
cmd = getattr(self, sys.argv[1])
141
help(conn, cmd.__name__)