def get_loadbalancer(project): tenant = landlord.Tenant() tenant.load_properties() conn = elb.connect_to_region(tenant.get_property('deploy.region'), aws_access_key_id=tenant.get_property('aws.id'), aws_secret_access_key=tenant.get_property('aws.secret')) try: load_balancers = conn.get_all_load_balancers(load_balancer_names=[tenant.get_property('environment')+'-lb-' + project['name']]) if load_balancers: print "[LB] Found load balancer ..." return load_balancers[0] else: raise BotoServerError('LoadBalancerNotFound', 'Load Balancer does not exists') except BotoServerError: print "[LB] No load balancer, creating one ..." #TODO: https://github.com/boto/boto/issues/509 hc = HealthCheck( interval=20, healthy_threshold=3, unhealthy_threshold=5, target='HTTP:9000/public/version.txt') ports = [(80, 9000, 'http')] lb = conn.create_load_balancer(tenant.get_property('environment')+'-lb-' + project['name'], None, ports, [tenant.get_property('deploy.subnet')]) dns.update_dns(lb.dns_name, project) print "[LB] Configuring health checks ... " lb.configure_health_check(hc) # TODO: Apply tags if lb.instances is None: lb.instances = [] return lb
def create_and_start_instances(project): tenant = landlord.Tenant() tenant.load_properties() instance_ids = [] connection = ec2.connect_to_region(tenant.get_property('deploy.region'), aws_access_key_id=tenant.get_property('aws.id'), aws_secret_access_key=tenant.get_property('aws.secret')) print "Running instances ..." reservations = connection.run_instances(image_id=tenant.get_property('deploy.image.id'), key_name=tenant.get_property('deploy.keyfile'), instance_type=tenant.get_property('deploy.instance.type'), subnet_id=tenant.get_property('deploy.subnet'), security_group_ids=[tenant.get_property('deploy.secgroup')], min_count=int(tenant.get_property('deploy.min_count')), max_count=int(tenant.get_property('deploy.max_count')), user_data=Script.get(project, tenant.get_property('environment')), dry_run=False) time.sleep(30) for index, instance in enumerate(reservations.instances): subdomain_name = project['name'] + "-" + str(index+1) instance_name = tenant.get_property('environment') + "-" + subdomain_name print "Instance %s [%s] checking ..." % (instance.id, instance_name) status = instance.update() while status == 'pending': print "Instance %s is still pending ..." % instance.id time.sleep(2) status = instance.update() if status == 'running': instance_ids.append(instance.id) print 'New instance %s accessible at %s' % (instance.id, instance.public_dns_name) try: stopTime = project['stop-time'] except KeyError: stopTime = "NA" try: startTime = project['start-time'] except KeyError: startTime = "NA" instance.add_tags({'Name': instance_name, 'Project': project['name'], 'Version': project['version'], 'StartTime': startTime, 'StopTime': stopTime, 'Environment': tenant.get_property('environment'), 'Capability': tenant.get_property('tag.capability'), 'Client': tenant.get_property('tag.client'), 'Deployer': 'igor'}) print 'Added new Tags, Description and Name to %s' % instance.id dns.update_dns(instance.public_dns_name, project, subdomain=subdomain_name) #TODO: Delete the instance if catch the exception persistence.save(instance_name, project['version']) else: print 'Error with instance %s. The status is "%s"' % (instance.id, status) instance_ids = None return instance_ids
def test_we_create_the_dns_registry(self, mock_landlord, mock_route53): server = "aws.private_dns.resource.server.com" project = {'name': 'myProject'} mock_landlord.Tenant = StubLandlord mock_connection = Mock() mock_record = Mock() mock_connection.get_hosted_zone_by_name.return_value = StubAWSResponse() mock_route53.connection.Route53Connection.return_value = mock_connection mock_route53.record.Record = mock_record mock_route53.record.ResourceRecordSets = Mock() dns.update_dns(server, project) mock_route53.record.ResourceRecordSets.assert_called_with(connection=mock_connection, hosted_zone_id="XXXXXXXX") mock_record.assert_called_with(name="myproject.stage.blah.com", type="CNAME", resource_records=["aws.private_dns.resource.server.com"], ttl=300)