github.com/technosophos/deis@v1.7.1-0.20150915173815-f9005256004b/contrib/aws/route53-wildcard.py (about) 1 #!/usr/bin/env python 2 """ 3 The route53-wildcard utility is used to add and remove wildcard 4 DNS entries from an AWS Route53 zonefile. 5 6 Usage: route53-wildcard.py <action> <name> <value> [options] 7 8 Options: 9 10 --zone=<zone> name of zone to use [defaults to parsing name] 11 --region=<region> AWS region to use [default: us-west-2]. 12 --ttl=<ttl> record TTL to use [default: 60]. 13 14 Examples: 15 16 ./route53-wildcard.py create mycluster.gabrtv.io deis-elb.blah.amazonaws.com 17 ./route53-wildcard.py delete mycluster.gabrtv.io deis-elb.blah.amazonaws.com 18 """ 19 20 from __future__ import print_function 21 22 import boto.route53 23 import docopt 24 import socket 25 import time 26 import uuid 27 28 29 def parse_args(): 30 return docopt.docopt(__doc__) 31 32 33 def create_cname(args): 34 conn = boto.route53.connect_to_region(args['--region']) 35 zone = conn.get_zone(args['<zone>']) 36 name = args['<name>'] 37 status = zone.add_cname(name, args['<value>'], ttl=args['--ttl']) 38 print("waiting for record to sync: {}".format(status)) 39 while status.update() != "INSYNC": 40 time.sleep(2) 41 print(status) 42 print('waiting for wildcard domain to become available...', end='') 43 # AWS docs say it can take up to 30 minutes for route53 changes to happen, although 44 # it seems to be almost immediate. 45 for i in xrange(120): 46 try: 47 random_hostname = str(uuid.uuid4())[:8] 48 if socket.gethostbyname("{}.{}".format(random_hostname, name)): 49 print('ok') 50 break 51 except socket.gaierror: 52 time.sleep(15) 53 54 55 def delete_cname(args, block=False): 56 conn = boto.route53.connect_to_region(args['--region']) 57 zone = conn.get_zone(args['<zone>']) 58 status = zone.delete_cname(args['<name>']) 59 if block: 60 print("waiting for record to sync: {}".format(status)) 61 while status.update() != "INSYNC": 62 time.sleep(2) 63 print(status) 64 65 if __name__ == '__main__': 66 args = parse_args() 67 68 # calculate zone from provided name 69 zone = '.'.join(args['<name>'].split('.')[-2:]) 70 args['<zone>'] = zone 71 72 # add a * to the provided name 73 args['<name>'] = u'\\052.'+unicode(args['<name>']) 74 75 if args['<action>'] == 'create': 76 create_cname(args) 77 elif args['<action>'] == 'delete': 78 delete_cname(args) 79 else: 80 raise NotImplementedError