Example #1
0
    def test_create_and_delete_health_check(self):
        """
        Will create, tag health check, search for it via a tag, then delete it.

        :return:
        """

        zone_domain = 'unit-test-zone.com'
        hostname = 'test-unittest'
        port = 80
        protocol_type = 'HTTP'

        route53 = modules.route53.Route53()

        health_check_config_dict = {
                'Port': port,
                'Type': protocol_type,
                'ResourcePath': '/',
                'FullyQualifiedDomainName': hostname+'.'+zone_domain,
                'RequestInterval': 10,
                'FailureThreshold': 3
            }

        # Create health check
        response = route53.create_health_check(health_check_config_dict)

        print "XXXXX Create"
        print response

        self.assertEqual(response['HealthCheck']['HealthCheckConfig']['FullyQualifiedDomainName'], hostname+'.'+zone_domain)
        self.assertEquals(response['HealthCheck']['HealthCheckConfig']['ResourcePath'], '/')

        health_check_id = response['HealthCheck']['Id']
        #health_check_creation_status_code = response['ResponseMetadata']['HTTPStatusCode']

        # Tag health check
        tag_name = 'unit-test-name'
        tag_value = 'test_create_and_delete_health_check-'+str(random.randrange(100000, 999999999))
        route53.change_tags_for_resource_health_check(health_check_id, 'Name', 'unit-test')
        route53.change_tags_for_resource_health_check(health_check_id, tag_name, tag_value)

        # Search for health check via tag
        searched_health_check_id = route53.get_health_check_by_tag(tag_name, tag_value)

        self.assertEqual(health_check_id, searched_health_check_id)


        # Delete the health check
        time.sleep(.5)
        delete_response = self.delete_health_check(health_check_id)

        #print "XXXXX Delete"
        #print response

        self.assertEquals(delete_response, None)
Example #2
0
    def test_create_health_check(self):
        """

        :return:
        """

        zone_domain = 'unit-test-zone.com'
        hostname = 'test-unittest'
        port = 80
        protocol_type = 'HTTP'

        route53 = modules.route53.Route53()

        health_check_config_dict = {
                'Port': port,
                'Type': protocol_type,
                'ResourcePath': '/',
                'FullyQualifiedDomainName': hostname+'.'+zone_domain,
                'RequestInterval': 10,
                'FailureThreshold': 3
            }

        # Create health check
        response = route53.create_health_check(health_check_config_dict)

        print "XXXXX Create"
        print response

        health_check_id = response['HealthCheck']['Id']
        #health_check_creation_status_code = response['ResponseMetadata']['HTTPStatusCode']

        self.assertEqual(response['HealthCheck']['HealthCheckConfig']['FullyQualifiedDomainName'], hostname+'.'+zone_domain)
        self.assertEquals(response['HealthCheck']['HealthCheckConfig']['ResourcePath'], '/')

        # Delete the health check
        time.sleep(.5)
        delete_response = self.delete_health_check(health_check_id)

        print "XXXXX Delete"
        print response

        self.assertEquals(delete_response, None)
Example #3
0
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_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)