Пример #1
0
Файл: ec2.py Проект: jscruz/igor
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
Пример #2
0
    def test_we_can_get_all_instances(self, mock_landlord, mock_ec2):
        persistence.save('Instance1', 'v43')
        persistence.save('Instance2', 'v43')
        mock_landlord.Tenant = StubLandlord
        mock_connection = Mock()
        mock_ec2.connect_to_region.return_value = mock_connection

        instance1 = Mock()
        instance1.id = 'i-938372'
        instance1.dns_name = '192.1.11.1.dnsname'
        instance1.ip_address = '192.1.11.1'
        instance1.state = 'running'
        instance1.tags = {'Name': 'STAGE-Instance-1', 'Project': 'Instance', 'Version': 'v43', 'AutoStopped': 'True'}
        instance1.launch_time = datetime.date.today().isoformat()
        instance1.image_id = 'ami-192812'

        instance2 = Mock()
        instance2.id = 'i-542211'
        instance2.dns_name = '192.5.5.5.dnsname'
        instance2.ip_address = '192.5.5.5'
        instance2.state = 'stopped'
        instance2.tags = {'Name': 'Instance2', 'Project': 'Instance', 'Version': 'v43'}
        instance2.launch_time = datetime.date.today().isoformat()
        instance2.image_id = 'ami-237829'

        mock_connection.get_only_instances.return_value = [instance1, instance2]

        instances = ec2.get_all_instances()

        mock_ec2.connect_to_region.assert_called_with('eu-west-1', aws_access_key_id='aws.id',
                                                      aws_secret_access_key='aws.secret')
        mock_connection.get_only_instances.assert_called_with(filters={"tag:Deployer": "igor"})
        self.assertEquals(len(instances), 2)
        self.assertEquals(instances[0]['name'], 'Instance-1')
        self.assertEquals(instances[0]['version'], 'v43')
        self.assertEquals(instances[0]['project'], 'Instance')
        self.assertEquals(instances[0]['date'], datetime.date.today().isoformat())
        self.assertEquals(instances[0]['ip'], '192.1.11.1')
        self.assertEquals(instances[0]['status'], 'running')
        self.assertEquals(instances[0]['autoStopped'], 'True')

        self.assertEquals(instances[1]['name'], 'Instance2')
        self.assertEquals(instances[1]['version'], 'v43')
        self.assertEquals(instances[1]['project'], 'Instance')
        self.assertEquals(instances[1]['date'], datetime.date.today().isoformat())
        self.assertEquals(instances[1]['ip'], '192.5.5.5')
        self.assertEquals(instances[1]['status'], 'stopped')
        self.assertEquals(instances[1]['ami'], 'ami-237829')
        self.assertEquals(instances[1]['autoStopped'], 'NA')
Пример #3
0
    def test_we_can_get_all_instances_with_region(self, mock_landlord, mock_ec2):
        persistence.save('Instance1', 'v43')
        persistence.save('Instance2', 'v43')
        mock_landlord.Tenant = StubLandlord
        mock_connection = Mock()
        mock_ec2.connect_to_region.return_value = mock_connection

        instance1 = Mock()
        instance1.id = 'i-938372'
        instance1.ip_address = '192.1.11.1'
        instance1.state = 'running'
        instance1.launch_time = datetime.date.today().isoformat()
        instance1.tags = {'Name': 'STAGE-Instance-1', 'Project': 'Instance', 'Version': 'v43', 'StopTime' : '8', 'StartTime': '6'}

        instance2 = Mock()
        instance2.id = 'i-542211'
        instance2.state = 'stopped'
        instance2.ip_address = None
        instance2.launch_time = datetime.date.today().isoformat()
        instance2.tags = {'Name': 'STAGE-Instance-2', 'Project': 'Instance', 'Version': 'v43', 'StopTime' : '9', 'StartTime': '7'}

        mock_connection.get_only_instances.return_value = [instance1, instance2]

        instances = ec2.get_all_instances('myRegion')

        mock_ec2.connect_to_region.assert_called_with('myRegion', aws_access_key_id='aws.id',
                                                      aws_secret_access_key='aws.secret')
        mock_connection.get_only_instances.assert_called_with(filters={"tag:Deployer": "igor"})
        self.assertEquals(len(instances), 2)
        self.assertEquals(instances[0]['name'], 'Instance-1')
        self.assertEquals(instances[0]['version'], 'v43')
        self.assertEquals(instances[0]['stopTime'], '8')
        self.assertEquals(instances[0]['startTime'], '6')
        self.assertEquals(instances[0]['project'], 'Instance')
        self.assertEquals(instances[0]['date'], datetime.date.today().isoformat())
        self.assertEquals(instances[0]['ip'], '192.1.11.1')
        self.assertEquals(instances[0]['status'], 'running')
        self.assertEquals(instances[1]['name'], 'Instance-2')
        self.assertEquals(instances[1]['version'], 'v43')
        self.assertEquals(instances[1]['startTime'], '7')
        self.assertEquals(instances[1]['stopTime'], '9')
        self.assertEquals(instances[1]['project'], 'Instance')
        self.assertEquals(instances[1]['date'], datetime.date.today().isoformat())
        self.assertEquals(instances[1]['ip'], 'None')
        self.assertEquals(instances[1]['status'], 'stopped')
Пример #4
0
 def test_it_should_keep_the_values_in_several_instances(self):
     persistence.save('project', 'v3')
     project = persistence.get('project')
     self.assertEqual('v3', project['version'])