Exemple #1
0
def deploy(job):
    job.set_status(JobStatus.running)
    project_attributes = job.get_project()
    new_instances = ec2.create_and_start_instances(project_attributes)
    if ec2.is_running(new_instances, project_attributes):
        if project_attributes.get('environment_tier') == EnvironmentTier.WORKER.value:
            old_instances = ec2.get_instances(filters={'tag:Project': project_attributes['name']})
            old_instance_ids = list(set([instance.id for instance in old_instances]) - set(new_instances))
        else:
            load_balancer = loadbalancer.get_loadbalancer(project_attributes)
            old_instance_ids = [instance.id for instance in load_balancer.instances]
            loadbalancer.attach(load_balancer, new_instances)
            loadbalancer.dettach(load_balancer, old_instance_ids)
        ec2.terminate(old_instance_ids)
        job.set_status(JobStatus.done)
    else:
        ec2.terminate(new_instances)
        job.set_status(JobStatus.failed)
Exemple #2
0
    def test_we_create_only_one_instance(self, mock_script, mock_time, mock_dns, mock_landlord, mock_ec2):
        my_project = 'MyProject'
        project = {'name': ('%s' % my_project), 'version': 'v34', 'type': 'play2', 'stop-time': '18', 'start-time': '8'}
        mock_connection = Mock()
        instance1 = Mock()
        instance1.id = 'i-938372'
        instance1.public_dns_name = 'my.awesome.dns.com'
        instance1.update.side_effect = ['pending', 'running']
        reservation = Mock()
        reservation.instances = [instance1]
        mock_connection.run_instances.return_value = reservation
        mock_ec2.connect_to_region.return_value = mock_connection
        mock_landlord.Tenant = StubLandlord
        mock_script.get.return_value = 'TheScript'

        instances = ec2.create_and_start_instances(project)

        mock_ec2.connect_to_region.assert_called_with('deploy.region', aws_access_key_id='aws.id',
                                                      aws_secret_access_key='aws.secret')
        mock_connection.run_instances.assert_called_with(image_id='deploy.image.id', key_name='deploy.keyfile',
                                                         instance_type='deploy.instance.type',
                                                         subnet_id='deploy.subnet',
                                                         security_group_ids=['deploy.secgroup'], min_count=1,
                                                         max_count=1,
                                                         user_data="TheScript",
                                                         dry_run=False)
        instance1.update.assert_has_calls([mock.call(), mock.call()], any_order=True)
        instance1.add_tags.assert_called_with({'Name': ('environment-%s-1' % my_project),
                                   'Project': my_project,
                                   'Version': 'v34',
                                   'StopTime': '18',
                                   'StartTime': '8',
                                   'Environment': 'environment',
                                   'Capability': 'tag.capability',
                                   'Client': 'tag.client',
                                   'Deployer': 'igor'})
        mock_dns.update_dns.assert_called_with('my.awesome.dns.com', project, subdomain=('%s-1' % my_project))
        mock_time.sleep.assert_has_calls([mock.call(30), mock.call(2)])
        self.assertEquals(len(instances), 1)
        self.assertEquals(instances[0], instance1.id)
Exemple #3
0
    def test_we_return_none_when_an_instance_fails(self, mock_script, mock_time, mock_dns, mock_landlord, mock_ec2):
        project = {'name': 'MyProject', 'version': 'v34', 'type': 'play2'}
        mock_connection = Mock()
        instance1 = Mock()
        instance1.id = 'i-938372'
        instance1.public_dns_name = 'my.awesome.dns.com'
        instance1.update.side_effect = ['pending', 'running']

        instance2 = Mock()
        instance2.id = 'i-542211'
        instance2.public_dns_name = 'my.awesome2.dns.com'
        instance2.update.side_effect = ['pending', 'stopped']

        reservation = Mock()
        reservation.instances = [instance1, instance2]
        mock_connection.run_instances.return_value = reservation
        mock_ec2.connect_to_region.return_value = mock_connection
        mock_landlord.Tenant = StubLandlord
        mock_script.get.return_value = 'TheScript'

        instances = ec2.create_and_start_instances(project)

        mock_ec2.connect_to_region.assert_called_with('deploy.region', aws_access_key_id='aws.id',
                                                      aws_secret_access_key='aws.secret')
        mock_connection.run_instances.assert_called_with(image_id='deploy.image.id', key_name='deploy.keyfile',
                                                         instance_type='deploy.instance.type',
                                                         subnet_id='deploy.subnet',
                                                         security_group_ids=['deploy.secgroup'], min_count=1,
                                                         max_count=1,
                                                         user_data="TheScript",
                                                         dry_run=False)
        instance1.update.assert_has_calls([mock.call(), mock.call()], any_order=True)
        instance2.update.assert_has_calls([mock.call(), mock.call()], any_order=True)
        mock_dns.update_dns.assert_called_with('my.awesome.dns.com', project, subdomain='MyProject-1')
        mock_time.sleep.assert_has_calls([mock.call(30), mock.call(2)])
        self.assertIsNone(instances)