blob: 3f6327d90c56b4a09f4ed53851a86ac8b5a6bd54 [file] [log] [blame]
#!/usr/bin/env python
# Author: Chris Moyer
#
# route53 is similar to sdbadmin for Route53, it's a simple
# console utility to perform the most frequent tasks with Route53
def _print_zone_info(zoneinfo):
print "="*80
print "| ID: %s" % zoneinfo['Id'].split("/")[-1]
print "| Name: %s" % zoneinfo['Name']
print "| Ref: %s" % zoneinfo['CallerReference']
print "="*80
print zoneinfo['Config']
print
def create(conn, hostname, caller_reference=None, comment=''):
"""Create a hosted zone, returning the nameservers"""
response = conn.create_hosted_zone(hostname, caller_reference, comment)
print "Pending, please add the following Name Servers:"
for ns in response.NameServers:
print "\t", ns
def delete_zone(conn, hosted_zone_id):
"""Delete a hosted zone by ID"""
response = conn.delete_hosted_zone(hosted_zone_id)
print response
def ls(conn):
"""List all hosted zones"""
response = conn.get_all_hosted_zones()
for zoneinfo in response['ListHostedZonesResponse']['HostedZones']:
_print_zone_info(zoneinfo)
def get(conn, hosted_zone_id, type=None, name=None, maxitems=None):
"""Get all the records for a single zone"""
response = conn.get_all_rrsets(hosted_zone_id, type, name, maxitems=maxitems)
# If a maximum number of items was set, we limit to that number
# by turning the response into an actual list (copying it)
# instead of allowing it to page
if maxitems:
response = response[:]
print '%-40s %-5s %-20s %s' % ("Name", "Type", "TTL", "Value(s)")
for record in response:
print '%-40s %-5s %-20s %s' % (record.name, record.type, record.ttl, record.to_print())
def add_record(conn, hosted_zone_id, name, type, values, ttl=600, comment=""):
"""Add a new record to a zone"""
from boto.route53.record import ResourceRecordSets
changes = ResourceRecordSets(conn, hosted_zone_id, comment)
change = changes.add_change("CREATE", name, type, ttl)
for value in values.split(','):
change.add_value(value)
print changes.commit()
def del_record(conn, hosted_zone_id, name, type, values, ttl=600, comment=""):
"""Delete a record from a zone"""
from boto.route53.record import ResourceRecordSets
changes = ResourceRecordSets(conn, hosted_zone_id, comment)
change = changes.add_change("DELETE", name, type, ttl)
for value in values.split(','):
change.add_value(value)
print changes.commit()
def add_alias(conn, hosted_zone_id, name, type, alias_hosted_zone_id, alias_dns_name, comment=""):
"""Add a new alias to a zone"""
from boto.route53.record import ResourceRecordSets
changes = ResourceRecordSets(conn, hosted_zone_id, comment)
change = changes.add_change("CREATE", name, type)
change.set_alias(alias_hosted_zone_id, alias_dns_name)
print changes.commit()
def del_alias(conn, hosted_zone_id, name, type, alias_hosted_zone_id, alias_dns_name, comment=""):
"""Delete an alias from a zone"""
from boto.route53.record import ResourceRecordSets
changes = ResourceRecordSets(conn, hosted_zone_id, comment)
change = changes.add_change("DELETE", name, type)
change.set_alias(alias_hosted_zone_id, alias_dns_name)
print changes.commit()
def change_record(conn, hosted_zone_id, name, type, values, ttl=600, comment=""):
"""Delete and then add a record to a zone"""
from boto.route53.record import ResourceRecordSets
changes = ResourceRecordSets(conn, hosted_zone_id, comment)
response = conn.get_all_rrsets(hosted_zone_id, type, name, maxitems=1)[0]
change1 = changes.add_change("DELETE", name, type, response.ttl)
for old_value in response.resource_records:
change1.add_value(old_value)
change2 = changes.add_change("CREATE", name, type, ttl)
for new_value in values.split(','):
change2.add_value(new_value)
print changes.commit()
def help(conn, fnc=None):
"""Prints this help message"""
import inspect
self = sys.modules['__main__']
if fnc:
try:
cmd = getattr(self, fnc)
except:
cmd = None
if not inspect.isfunction(cmd):
print "No function named: %s found" % fnc
sys.exit(2)
(args, varargs, varkw, defaults) = inspect.getargspec(cmd)
print cmd.__doc__
print "Usage: %s %s" % (fnc, " ".join([ "[%s]" % a for a in args[1:]]))
else:
print "Usage: route53 [command]"
for cname in dir(self):
if not cname.startswith("_"):
cmd = getattr(self, cname)
if inspect.isfunction(cmd):
doc = cmd.__doc__
print "\t%-20s %s" % (cname, doc)
sys.exit(1)
if __name__ == "__main__":
import boto
import sys
conn = boto.connect_route53()
self = sys.modules['__main__']
if len(sys.argv) >= 2:
try:
cmd = getattr(self, sys.argv[1])
except:
cmd = None
args = sys.argv[2:]
else:
cmd = help
args = []
if not cmd:
cmd = help
try:
cmd(conn, *args)
except TypeError, e:
print e
help(conn, cmd.__name__)