Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

executable file 142 lines (126 sloc) 5.228 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
#!/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__)
Something went wrong with that request. Please try again.