def test_create_and_delete_record_set(self): """ Creates then deletes a DNS record set :return: """ hosted_zone_id = 'Z1UNV9CC0W9AR7' # Unit testing zone in the vungle2 account zone_domain = 'unit-test-zone.com' hostname = 'test-unittest' comment = 'unit test record' route53 = modules.route53.Route53() route53.set_hosted_zone_id(hosted_zone_id) resource_record_set_dict = { 'Name': hostname+'.'+zone_domain, 'Type': 'A', 'SetIdentifier': '2222', 'Weight': 10, 'TTL': 15, 'ResourceRecords': [ { 'Value': '2.2.2.2' } ] } # Create response = route53.create_resource_record_sets('UPSERT', resource_record_set_dict, comment) #print "XXXXX" #print response['ChangeInfo']['Status'] #print response self.assertEqual(response['ChangeInfo']['Comment'], comment) # Delete - Need to match everything, including the HealthCheckId if it is there resource_record_set_dict = { 'Name': hostname+'.'+zone_domain+'.', 'Type': 'A', 'SetIdentifier': '2222', 'Weight': 10, 'TTL': 15, 'ResourceRecords': [ { 'Value': '2.2.2.2' } ] } response_delete = route53.create_resource_record_sets('DELETE', resource_record_set_dict, comment) print response_delete
def test_create_record_set(self): """ Creates or updates a record if it is already there with the new values :return: """ hosted_zone_id = 'Z1UNV9CC0W9AR7' # Unit testing zone in the vungle2 account zone_domain = 'unit-test-zone.com' hostname = 'test-unittest' comment = 'unit test record' route53 = modules.route53.Route53() route53.set_hosted_zone_id(hosted_zone_id) resource_record_set_dict = { 'Name': hostname+'.'+zone_domain, 'Type': 'A', 'SetIdentifier': '1111', 'Weight': 10, 'TTL': 15, 'ResourceRecords': [ { 'Value': '1.1.1.1' }, ], 'HealthCheckId': 'a29a5665-7bea-47b2-b977-b56b45301a6e' } response = route53.create_resource_record_sets('UPSERT', resource_record_set_dict, comment) #print "XXXXX" #print response['ChangeInfo']['Status'] #print response self.assertEqual(response['ChangeInfo']['Comment'], comment)
def ec2_launch_event(ec2_instance_id): """ When an ec2 instance launches it will add the health checks and dns records for the node """ # config settings = configparser.ConfigParser() settings.read('config.ini') logging.info("Event: ec2_launch_event") logging.info("Working on ec2-instance id: " + ec2_instance_id) logging.info("Using route53 hosted zone id: " + settings.get('route53', 'hosted_zone')) logging.info("Domain name: " + settings.get('route53', 'domain_name')) # Get instance information ec2 = modules.ec2.Ec2() response_ec2_describe = ec2.describe_instances(ec2_instance_id) logging.debug(response_ec2_describe) logging.info("Instance public dns: " + response_ec2_describe['Reservations'][0]['Instances'][0] ['PublicDnsName']) logging.info("Instance public IP: " + response_ec2_describe['Reservations'] [0]['Instances'][0]['PublicIpAddress']) # Filter on the machine_filter config value to determine if we want to add this machine into the DNS machine_tag_value = ec2.get_tag_from_describe_instance_response( response_ec2_describe, settings.get('machine_filter', 'ec2_tag_key')) if settings.get('machine_filter', 'ec2_tag_value') == machine_tag_value: logging.info("This machine passes the machine_filter. Add to DNS. " + machine_tag_value) # init route53 object route53 = modules.route53.Route53() health_check_config_dict = { 'Port': int(settings.get('health_check', 'port')), 'Type': settings.get('health_check', 'protocol_type'), 'ResourcePath': settings.get('health_check', 'ResourcePath'), 'FullyQualifiedDomainName': response_ec2_describe['Reservations'][0]['Instances'][0] ['PublicDnsName'], 'RequestInterval': int(settings.get('health_check', 'RequestInterval')), 'FailureThreshold': int(settings.get('health_check', 'FailureThreshold')), } response_create_health_check = route53.create_health_check( health_check_config_dict) logging.debug(response_create_health_check) logging.info("Health check id: " + response_create_health_check['HealthCheck']['Id']) # Add tag for health check, and also adding additional tags so that we can find the DNS record later. # We can only delete the DNS A Record if we have all of this information. response = route53.change_tags_for_resource_health_check( response_create_health_check['HealthCheck']['Id'], 'Name', settings.get('health_check', 'name')) response = route53.change_tags_for_resource_health_check( response_create_health_check['HealthCheck']['Id'], 'instance-id', ec2_instance_id) response = route53.change_tags_for_resource_health_check( response_create_health_check['HealthCheck']['Id'], 'instance-public-ip', response_ec2_describe['Reservations'][0] ['Instances'][0]['PublicIpAddress']) # Create DNS record object route53.set_hosted_zone_id(settings.get('route53', 'hosted_zone')) # Get the DNS name to a simple or weighted dns_name = '' if settings.get('dns_record_type', 'type') == 'simple': dns_name = ec2_instance_id + '.' + settings.get( 'route53', 'domain_name') elif settings.get('dns_record_type', 'type') == 'weighted': dns_name = settings.get('dns_record_type', 'dns_name') + '.' + settings.get( 'route53', 'domain_name') # Add DNS record resource_record_set_dict = { 'Name': dns_name, 'Type': settings.get('dns_record_set', 'type'), 'SetIdentifier': ec2_instance_id, 'Weight': int(settings.get('dns_record_set', 'Weight')), 'TTL': int(settings.get('dns_record_set', 'TTL')), 'ResourceRecords': [ { 'Value': response_ec2_describe['Reservations'][0]['Instances'][0] ['PublicIpAddress'] }, ], 'HealthCheckId': response_create_health_check['HealthCheck']['Id'] } response_create_resource_record_sets = route53.create_resource_record_sets( 'UPSERT', resource_record_set_dict, settings.get('dns_record_set', 'comment')) logging.debug(response_create_resource_record_sets) else: logging.info( "This machine is not part of the machine_filter. Not adding to DNS. " + machine_tag_value)
def ec2_terminate_event(ec2_instance_id): """ When an ec2 instance is terminated, the DNS record and health check for this server is removed """ # config settings = configparser.ConfigParser() settings.read('config.ini') logging.info("Event: ec2_termination_event") logging.info("Working on ec2-instance id: " + ec2_instance_id) logging.info("Using route53 hosted zone id: " + settings.get('route53', 'hosted_zone')) logging.info("Domain name: " + settings.get('route53', 'domain_name')) # Get the DNS name to a simple or weighted dns_name = '' if settings.get('dns_record_type', 'type') == 'simple': dns_name = ec2_instance_id + '.' + settings.get( 'route53', 'domain_name') elif settings.get('dns_record_type', 'type') == 'weighted': dns_name = settings.get('dns_record_type', 'dns_name') + '.' + settings.get( 'route53', 'domain_name') # init route53 object route53 = modules.route53.Route53() route53.set_hosted_zone_id(settings.get('route53', 'hosted_zone')) health_check_id = route53.get_health_check_by_tag('instance-id', ec2_instance_id) instance_public_ip = route53.get_health_check_tag_value( ec2_instance_id, 'instance-public-ip') # Delete DNS record resource_record_set_dict = { 'Name': dns_name, 'Type': settings.get('dns_record_set', 'type'), 'SetIdentifier': ec2_instance_id, 'Weight': int(settings.get('dns_record_set', 'Weight')), 'TTL': int(settings.get('dns_record_set', 'TTL')), 'ResourceRecords': [ { 'Value': instance_public_ip }, ], 'HealthCheckId': health_check_id } logging.debug(resource_record_set_dict) try: response_delete_resource_record_sets = route53.create_resource_record_sets( 'DELETE', resource_record_set_dict, '') logging.debug(response_delete_resource_record_sets) except: logging.info("Unable to delete the record set") logging.info(resource_record_set_dict) # Search for health check via tag searched_health_check_id = route53.get_health_check_by_tag( 'instance-id', ec2_instance_id) # Delete health check try: delete_response = route53.delete_health_check(searched_health_check_id) except: logging.info("Unable to delete the health check")
def ec2_launch_event(ec2_instance_id): """ When an ec2 instance launches it will add the health checks and dns records for the node """ # config settings = configparser.ConfigParser() settings.read('config.ini') logging.info("Event: ec2_launch_event") logging.info("Working on ec2-instance id: "+ec2_instance_id) logging.info("Using route53 hosted zone id: "+settings.get('route53', 'hosted_zone')) logging.info("Domain name: "+settings.get('route53', 'domain_name')) # Get instance information ec2 = modules.ec2.Ec2() response_ec2_describe = ec2.describe_instances(ec2_instance_id) logging.debug(response_ec2_describe) logging.info("Instance public dns: "+response_ec2_describe['Reservations'][0]['Instances'][0]['PublicDnsName']) logging.info("Instance public IP: "+response_ec2_describe['Reservations'][0]['Instances'][0]['PublicIpAddress']) # Filter on the machine_filter config value to determine if we want to add this machine into the DNS machine_tag_value = ec2.get_tag_from_describe_instance_response(response_ec2_describe, settings.get('machine_filter', 'ec2_tag_key')) if settings.get('machine_filter', 'ec2_tag_value') == machine_tag_value: logging.info("This machine passes the machine_filter. Add to DNS. "+machine_tag_value) # init route53 object route53 = modules.route53.Route53() health_check_config_dict = { 'Port': int(settings.get('health_check', 'port')), 'Type': settings.get('health_check', 'protocol_type'), 'ResourcePath': settings.get('health_check', 'ResourcePath'), 'FullyQualifiedDomainName': response_ec2_describe['Reservations'][0]['Instances'][0]['PublicDnsName'], 'RequestInterval': int(settings.get('health_check', 'RequestInterval')), 'FailureThreshold': int(settings.get('health_check', 'FailureThreshold')), } response_create_health_check = route53.create_health_check(health_check_config_dict) logging.debug(response_create_health_check) logging.info("Health check id: "+response_create_health_check['HealthCheck']['Id']) # Add tag for health check, and also adding additional tags so that we can find the DNS record later. # We can only delete the DNS A Record if we have all of this information. response = route53.change_tags_for_resource_health_check(response_create_health_check['HealthCheck']['Id'], 'Name', settings.get('health_check', 'name')) response = route53.change_tags_for_resource_health_check(response_create_health_check['HealthCheck']['Id'], 'instance-id', ec2_instance_id) response = route53.change_tags_for_resource_health_check(response_create_health_check['HealthCheck']['Id'], 'instance-public-ip', response_ec2_describe['Reservations'][0]['Instances'][0]['PublicIpAddress']) # Create DNS record object route53.set_hosted_zone_id(settings.get('route53', 'hosted_zone')) # Get the DNS name to a simple or weighted dns_name = '' if settings.get('dns_record_type', 'type') == 'simple': dns_name = ec2_instance_id+'.'+settings.get('route53', 'domain_name') elif settings.get('dns_record_type', 'type') == 'weighted': dns_name = settings.get('dns_record_type', 'dns_name')+'.'+settings.get('route53', 'domain_name') # Add DNS record resource_record_set_dict = { 'Name': dns_name, 'Type': settings.get('dns_record_set', 'type'), 'SetIdentifier': ec2_instance_id, 'Weight': int(settings.get('dns_record_set', 'Weight')), 'TTL': int(settings.get('dns_record_set', 'TTL')), 'ResourceRecords': [ { 'Value': response_ec2_describe['Reservations'][0]['Instances'][0]['PublicIpAddress'] }, ], 'HealthCheckId': response_create_health_check['HealthCheck']['Id'] } response_create_resource_record_sets = route53.create_resource_record_sets('UPSERT', resource_record_set_dict, settings.get('dns_record_set', 'comment')) logging.debug(response_create_resource_record_sets) else: logging.info("This machine is not part of the machine_filter. Not adding to DNS. "+machine_tag_value)
def ec2_terminate_event(ec2_instance_id): """ When an ec2 instance is terminated, the DNS record and health check for this server is removed """ # config settings = configparser.ConfigParser() settings.read('config.ini') logging.info("Event: ec2_termination_event") logging.info("Working on ec2-instance id: "+ec2_instance_id) logging.info("Using route53 hosted zone id: "+settings.get('route53', 'hosted_zone')) logging.info("Domain name: "+settings.get('route53', 'domain_name')) # Get the DNS name to a simple or weighted dns_name = '' if settings.get('dns_record_type', 'type') == 'simple': dns_name = ec2_instance_id+'.'+settings.get('route53', 'domain_name') elif settings.get('dns_record_type', 'type') == 'weighted': dns_name = settings.get('dns_record_type', 'dns_name')+'.'+settings.get('route53', 'domain_name') # init route53 object route53 = modules.route53.Route53() route53.set_hosted_zone_id(settings.get('route53', 'hosted_zone')) health_check_id = route53.get_health_check_by_tag('instance-id', ec2_instance_id) instance_public_ip = route53.get_health_check_tag_value(ec2_instance_id, 'instance-public-ip') # Delete DNS record resource_record_set_dict = { 'Name': dns_name, 'Type': settings.get('dns_record_set', 'type'), 'SetIdentifier': ec2_instance_id, 'Weight': int(settings.get('dns_record_set', 'Weight')), 'TTL': int(settings.get('dns_record_set', 'TTL')), 'ResourceRecords': [ { 'Value': instance_public_ip }, ], 'HealthCheckId': health_check_id } logging.debug(resource_record_set_dict) try: response_delete_resource_record_sets = route53.create_resource_record_sets('DELETE', resource_record_set_dict, '') logging.debug(response_delete_resource_record_sets) except: logging.info("Unable to delete the record set") logging.info(resource_record_set_dict) # Search for health check via tag searched_health_check_id = route53.get_health_check_by_tag('instance-id', ec2_instance_id) # Delete health check try: delete_response = route53.delete_health_check(searched_health_check_id) except: logging.info("Unable to delete the health check")