Created
January 19, 2015 18:45
-
-
Save lashex/02e6ad47bdbdb86acda8 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/usr/bin/env python | |
import logging | |
import argparse | |
import ConfigParser | |
import boto.ec2 | |
import boto.route53 | |
from boto.route53.record import ResourceRecordSets | |
config = ConfigParser.SafeConfigParser({ | |
'region_name': 'us-west-2', | |
'environment': None | |
}) | |
def list_servers(): | |
region_name = config.get('general', 'region_name') | |
environment = config.get('general', 'environment') | |
server_name = config.get('general', 'server_name') | |
filter = { | |
"tag:name": server_name, | |
} | |
if environment is not None: | |
filter['tag:environment'] = environment | |
logging.info('List instances filtered by name: {0}'.format( | |
server_name | |
)) | |
logging.info('List instances filtered by environment: {0}'.format( | |
environment | |
)) | |
ec2 = boto.ec2.connect_to_region(region_name=region_name) | |
logging.info('EC2 Connected') | |
logging.info('Getting only instances tagged with:{0}'.format(filter)) | |
instances = ec2.get_only_instances(filters=filter) | |
logging.info('Found {0} instances'.format(len(instances))) | |
logging.info('List of instances:') | |
for instance in instances: | |
logging.info(' instance:{0} '.format(instance.id)) | |
return instances | |
def register_server(): | |
instances = list_servers() | |
if len(instances) is 1 and instances[0].state == 'running': | |
instance = instances[0] | |
region_name = config.get('general', 'region_name') | |
zone_id = config.get('route53', 'zone_id') | |
host_name = config.get('route53', 'host_name') | |
logging.info('instance:{0} public dns name:{1}'.format( | |
instance, instance.public_dns_name)) | |
conn = boto.route53.connect_to_region(region_name=region_name) | |
existing_entries = conn.get_all_rrsets(hosted_zone_id=zone_id) | |
for item in existing_entries: | |
logging.debug(item) | |
change = ResourceRecordSets(conn, zone_id) | |
change.add_change_record( | |
'UPSERT', | |
boto.route53.record.Record( | |
name=host_name, | |
type='CNAME', | |
resource_records=[instance.public_dns_name], | |
ttl=300) | |
) | |
change.commit() | |
logging.info( | |
'instance:{0} w/ public dns name:{1} registered with Zone ID:{2} for host name:{3}'.format( | |
instance.id, instance.public_dns_name, zone_id, host_name | |
)) | |
else: | |
logging.info("Couldn't work with list of instances: {0}".format( | |
instances | |
)) | |
commands = { | |
"list": list_servers, | |
"register": register_server | |
} | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser( | |
description='List instances with a name or register as a Route53 CNAME target', | |
prog='mc-manager' | |
) | |
parser.add_argument('cmd', nargs='?', default='list', choices=commands, | |
help='command to execute [default: %(default)s]') | |
parser.add_argument('--log', dest='log_level', default='INFO', | |
help='set Log Level [INFO|DEBUG|WARNING|ERROR]') | |
parser.add_argument('--config', dest='config', default='r53.cfg', | |
help='the optional config file [default: %(default)s]') | |
args = parser.parse_args() | |
logging.basicConfig(level=args.log_level.upper()) | |
config.read(args.config) | |
cmd = args.cmd.lower() | |
command = commands.get(cmd) | |
if command is None: | |
logging.error('Command {0} not understood.'.format(cmd)) | |
else: | |
command() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment