Exemple #1
0
 def _mock_get_image_id_fail(self, image_id, exp):
     g_cli_mock = self.m.CreateMockAnything()
     self.m.StubOutWithMock(glance.GlanceClientPlugin, '_create')
     glance.GlanceClientPlugin._create().MultipleTimes().AndReturn(
         g_cli_mock)
     self.m.StubOutWithMock(glance_utils, 'get_image_id')
     glance_utils.get_image_id(g_cli_mock, image_id).AndRaise(exp)
Exemple #2
0
 def _mock_get_image_id_fail(self, image_id, exp):
     g_cli_mock = self.m.CreateMockAnything()
     self.m.StubOutWithMock(glance.GlanceClientPlugin, '_create')
     glance.GlanceClientPlugin._create().MultipleTimes().AndReturn(
         g_cli_mock)
     self.m.StubOutWithMock(glance_utils, 'get_image_id')
     glance_utils.get_image_id(g_cli_mock, image_id).AndRaise(exp)
 def _mock_get_image_id_success(self, imageId_input, imageId):
     g_cli_mock = self.m.CreateMockAnything()
     self.m.StubOutWithMock(clients.OpenStackClients, 'glance')
     clients.OpenStackClients.glance().MultipleTimes().AndReturn(g_cli_mock)
     self.m.StubOutWithMock(glance_utils, 'get_image_id')
     glance_utils.get_image_id(g_cli_mock, imageId_input).MultipleTimes().\
         AndReturn(imageId)
Exemple #4
0
 def _mock_get_image_id_fail(self, image_id, exp):
     g_cli_mock = self.m.CreateMockAnything()
     self.m.StubOutWithMock(clients.OpenStackClients, '_glance')
     clients.OpenStackClients._glance().MultipleTimes().AndReturn(
         g_cli_mock)
     self.m.StubOutWithMock(glance_utils, 'get_image_id')
     glance_utils.get_image_id(g_cli_mock, image_id).AndRaise(exp)
 def _mock_get_image_id_success(self, imageId_input, imageId, update_image=None):
     g_cli_mock = self.m.CreateMockAnything()
     self.m.StubOutWithMock(clients.OpenStackClients, "glance")
     clients.OpenStackClients.glance().MultipleTimes().AndReturn(g_cli_mock)
     self.m.StubOutWithMock(glance_utils, "get_image_id")
     glance_utils.get_image_id(g_cli_mock, imageId_input).MultipleTimes().AndReturn(imageId)
     if update_image:
         glance_utils.get_image_id(g_cli_mock, update_image).MultipleTimes().AndReturn(imageId)
Exemple #6
0
 def _mock_get_image_id_success(self, imageId_input, imageId):
     g_cli_mock = self.m.CreateMockAnything()
     self.m.StubOutWithMock(clients.OpenStackClients, 'glance')
     clients.OpenStackClients.glance().MultipleTimes().AndReturn(
         g_cli_mock)
     self.m.StubOutWithMock(glance_utils, 'get_image_id')
     glance_utils.get_image_id(g_cli_mock, imageId_input).\
         MultipleTimes().AndReturn(imageId)
 def _mock_get_image_id_success(self, imageId_input, imageId):
     g_cli_mock = self.m.CreateMockAnything()
     self.m.StubOutWithMock(glance.GlanceClientPlugin, '_create')
     glance.GlanceClientPlugin._create().MultipleTimes().AndReturn(
         g_cli_mock)
     self.m.StubOutWithMock(glance_utils, 'get_image_id')
     glance_utils.get_image_id(g_cli_mock, imageId_input).MultipleTimes().\
         AndReturn(imageId)
Exemple #8
0
 def _mock_get_image_id_success(self, imageId_input, imageId):
     g_cli_mock = self.m.CreateMockAnything()
     self.m.StubOutWithMock(glance.GlanceClientPlugin, '_create')
     glance.GlanceClientPlugin._create().MultipleTimes().AndReturn(
         g_cli_mock)
     self.m.StubOutWithMock(glance_utils, 'get_image_id')
     glance_utils.get_image_id(g_cli_mock, imageId_input).MultipleTimes().\
         AndReturn(imageId)
Exemple #9
0
    def test_nokey_create(self):

        stack_name = 's_nokey'
        t = template_format.parse(nokey_template)
        stack = utils.parse_stack(t, stack_name=stack_name)

        t['Resources']['WebServer']['Properties']['ImageId'] = 'CentOS 5.2'
        t['Resources']['WebServer']['Properties']['InstanceType'] = \
            '256 MB Server'
        resource_defns = stack.t.resource_definitions(stack)
        instance = instances.Instance('create_instance_name',
                                      resource_defns['WebServer'], stack)

        self.m.StubOutWithMock(nova.NovaClientPlugin, '_create')
        nova.NovaClientPlugin._create().AndReturn(self.fc)
        g_cli_mock = self.m.CreateMockAnything()
        self.m.StubOutWithMock(glance.GlanceClientPlugin, '_create')
        glance.GlanceClientPlugin._create().MultipleTimes().AndReturn(
            g_cli_mock)
        self.m.StubOutWithMock(glance_utils, 'get_image_id')
        glance_utils.get_image_id(g_cli_mock, 'CentOS 5.2').MultipleTimes().\
            AndReturn(1)

        # need to resolve the template functions
        server_userdata = nova_utils.build_userdata(
            instance, instance.t['Properties']['UserData'], 'ec2-user')
        self.m.StubOutWithMock(nova_utils, 'build_userdata')
        nova_utils.build_userdata(instance,
                                  instance.t['Properties']['UserData'],
                                  'ec2-user').AndReturn(server_userdata)

        self.m.StubOutWithMock(self.fc.servers, 'create')
        self.fc.servers.create(image=1,
                               flavor=1,
                               key_name=None,
                               name=utils.PhysName(stack_name, instance.name),
                               security_groups=None,
                               userdata=server_userdata,
                               scheduler_hints=None,
                               meta=None,
                               nics=None,
                               availability_zone=None).AndReturn(
                                   self.fc.servers.list()[1])
        self.m.ReplayAll()

        scheduler.TaskRunner(instance.create)()

        self.m.VerifyAll()
Exemple #10
0
    def test_nokey_create(self):

        stack_name = 's_nokey'
        t = template_format.parse(nokey_template)
        stack = utils.parse_stack(t, stack_name=stack_name)

        t['Resources']['WebServer']['Properties']['ImageId'] = 'CentOS 5.2'
        t['Resources']['WebServer']['Properties']['InstanceType'] = \
            '256 MB Server'
        resource_defns = stack.t.resource_definitions(stack)
        instance = instances.Instance('create_instance_name',
                                      resource_defns['WebServer'], stack)

        self.m.StubOutWithMock(nova.NovaClientPlugin, '_create')
        nova.NovaClientPlugin._create().AndReturn(self.fc)
        g_cli_mock = self.m.CreateMockAnything()
        self.m.StubOutWithMock(clients.OpenStackClients, '_glance')
        clients.OpenStackClients._glance().MultipleTimes().AndReturn(
            g_cli_mock)
        self.m.StubOutWithMock(glance_utils, 'get_image_id')
        glance_utils.get_image_id(g_cli_mock, 'CentOS 5.2').MultipleTimes().\
            AndReturn(1)

        # need to resolve the template functions
        server_userdata = nova_utils.build_userdata(
            instance,
            instance.t['Properties']['UserData'],
            'ec2-user')
        self.m.StubOutWithMock(nova_utils, 'build_userdata')
        nova_utils.build_userdata(
            instance,
            instance.t['Properties']['UserData'],
            'ec2-user').AndReturn(server_userdata)

        self.m.StubOutWithMock(self.fc.servers, 'create')
        self.fc.servers.create(
            image=1, flavor=1, key_name=None,
            name=utils.PhysName(stack_name, instance.name),
            security_groups=None,
            userdata=server_userdata, scheduler_hints=None,
            meta=None, nics=None, availability_zone=None).AndReturn(
                self.fc.servers.list()[1])
        self.m.ReplayAll()

        scheduler.TaskRunner(instance.create)()

        self.m.VerifyAll()
    def _mock_get_image_id_success(self, imageId_input, imageId,
                                   update_image=None):
        g_cli_mock = self.m.CreateMockAnything()
        self.m.StubOutWithMock(clients.OpenStackClients, '_glance')
        clients.OpenStackClients._glance().MultipleTimes().AndReturn(
            g_cli_mock)
        self.m.StubOutWithMock(glance_utils, 'get_image_id')

        # If update_image is None (create case), validation for initial image
        # imageId_input will be invoked multiple times (for each server).
        # If update_image is set (update case), validation of the old property
        # values and new property values will be done, but the order is not
        # deterministic. Therefore, using mox.IgnoreArg() for the update case.
        if update_image is None:
            glance_utils.get_image_id(g_cli_mock, imageId_input).\
                MultipleTimes().AndReturn(imageId)
        else:
            glance_utils.get_image_id(g_cli_mock, mox.IgnoreArg()).\
                MultipleTimes().AndReturn(imageId)
Exemple #12
0
    def test_cinder_create_from_image(self):
        fv = FakeVolumeWithStateTransition('downloading', 'available')
        stack_name = 'test_volume_stack'
        image_id = '46988116-6703-4623-9dbc-2bc6d284021b'
        clients.OpenStackClients.cinder().MultipleTimes().AndReturn(
            self.cinder_fc)
        g_cli_mock = self.m.CreateMockAnything()
        self.m.StubOutWithMock(clients.OpenStackClients, 'glance')
        clients.OpenStackClients.glance().MultipleTimes().AndReturn(
            g_cli_mock)
        self.m.StubOutWithMock(glance_utils, 'get_image_id')
        glance_utils.get_image_id(g_cli_mock, image_id).MultipleTimes().\
            AndReturn(image_id)

        self.cinder_fc.volumes.create(
            size=1, availability_zone='nova',
            display_description='ImageVolumeDescription',
            display_name='ImageVolume',
            imageRef=image_id).AndReturn(fv)

        self.m.ReplayAll()

        t = template_format.parse(volume_template)
        t['Resources']['DataVolume']['Properties'] = {
            'size': '1',
            'name': 'ImageVolume',
            'description': 'ImageVolumeDescription',
            'availability_zone': 'nova',
            'image': image_id,
        }
        stack = utils.parse_stack(t, stack_name=stack_name)

        rsrc = vol.CinderVolume('DataVolume',
                                t['Resources']['DataVolume'],
                                stack)
        self.assertIsNone(rsrc.validate())
        scheduler.TaskRunner(rsrc.create)()
        self.assertEqual((rsrc.CREATE, rsrc.COMPLETE), rsrc.state)
        self.assertEqual('available', fv.status)

        self.m.VerifyAll()
Exemple #13
0
    def _mock_get_image_id_success(self,
                                   imageId_input,
                                   imageId,
                                   update_image=None):
        g_cli_mock = self.m.CreateMockAnything()
        self.m.StubOutWithMock(glance.GlanceClientPlugin, '_create')
        glance.GlanceClientPlugin._create().MultipleTimes().AndReturn(
            g_cli_mock)
        self.m.StubOutWithMock(glance_utils, 'get_image_id')

        # If update_image is None (create case), validation for initial image
        # imageId_input will be invoked multiple times (for each server).
        # If update_image is set (update case), validation of the old property
        # values and new property values will be done, but the order is not
        # deterministic. Therefore, using mox.IgnoreArg() for the update case.
        if update_image is None:
            glance_utils.get_image_id(g_cli_mock, imageId_input).\
                MultipleTimes().AndReturn(imageId)
        else:
            glance_utils.get_image_id(g_cli_mock, mox.IgnoreArg()).\
                MultipleTimes().AndReturn(imageId)
Exemple #14
0
 def test_get_image_id(self):
     """Tests the get_image_id function."""
     my_image = self.m.CreateMockAnything()
     img_id = str(uuid.uuid4())
     img_name = 'myfakeimage'
     my_image.id = img_id
     my_image.name = img_name
     self.glance_client.images = self.m.CreateMockAnything()
     self.glance_client.images.get(img_id).AndReturn(my_image)
     filters = {'name': img_name}
     self.glance_client.images.list(filters=filters).AndReturn([my_image])
     filters = {'name': 'noimage'}
     self.glance_client.images.list(filters=filters).AndReturn([])
     self.m.ReplayAll()
     self.assertEqual(img_id,
                      glance_utils.get_image_id(self.glance_client, img_id))
     self.assertEqual(
         img_id, glance_utils.get_image_id(self.glance_client, img_name))
     self.assertRaises(exception.ImageNotFound, glance_utils.get_image_id,
                       self.glance_client, 'noimage')
     self.m.VerifyAll()
Exemple #15
0
 def test_get_image_id(self):
     """Tests the get_image_id function."""
     my_image = self.m.CreateMockAnything()
     img_id = str(uuid.uuid4())
     img_name = 'myfakeimage'
     my_image.id = img_id
     my_image.name = img_name
     self.glance_client.images = self.m.CreateMockAnything()
     self.glance_client.images.get(img_id).AndReturn(my_image)
     filters = {'name': img_name}
     self.glance_client.images.list(filters=filters).AndReturn([my_image])
     filters = {'name': 'noimage'}
     self.glance_client.images.list(filters=filters).AndReturn([])
     self.m.ReplayAll()
     self.assertEqual(img_id, glance_utils.get_image_id(
         self.glance_client, img_id))
     self.assertEqual(img_id, glance_utils.get_image_id(
         self.glance_client, img_name))
     self.assertRaises(exception.ImageNotFound, glance_utils.get_image_id,
                       self.glance_client, 'noimage')
     self.m.VerifyAll()
Exemple #16
0
    def _create_arguments(self):
        arguments = {
            'size': self.properties[self.SIZE],
            'availability_zone': self.properties[self.AVAILABILITY_ZONE]
        }
        if self.properties.get(self.IMAGE):
            arguments['imageRef'] = glance_utils.get_image_id(
                self.glance(), self.properties[self.IMAGE])
        elif self.properties.get(self.IMAGE_REF):
            arguments['imageRef'] = self.properties[self.IMAGE_REF]

        optionals = (self.SNAPSHOT_ID, self.VOLUME_TYPE, self.SOURCE_VOLID,
                     self.METADATA)
        arguments.update((prop, self.properties[prop]) for prop in optionals
                         if self.properties[prop])
        return arguments
Exemple #17
0
    def _create_arguments(self):
        arguments = {
            'size': self.properties[self.SIZE],
            'availability_zone': self.properties[self.AVAILABILITY_ZONE]
        }
        if self.properties.get(self.IMAGE):
            arguments['imageRef'] = glance_utils.get_image_id(
                self.glance(), self.properties[self.IMAGE])
        elif self.properties.get(self.IMAGE_REF):
            arguments['imageRef'] = self.properties[self.IMAGE_REF]

        optionals = (self.SNAPSHOT_ID, self.VOLUME_TYPE, self.SOURCE_VOLID,
                     self.METADATA)
        arguments.update((prop, self.properties[prop]) for prop in optionals
                         if self.properties[prop])
        return arguments
Exemple #18
0
    def test_get_image_id_by_name_in_uuid(self):
        """Tests the get_image_id function by name in uuid."""
        my_image = self.m.CreateMockAnything()
        img_id = str(uuid.uuid4())
        img_name = str(uuid.uuid4())
        my_image.id = img_id
        my_image.name = img_name
        self.glance_client.images = self.m.CreateMockAnything()
        self.glance_client.images.get(img_name).AndRaise(
            glance_exceptions.NotFound(404))
        filters = {'name': img_name}
        self.glance_client.images.list(filters=filters).MultipleTimes().\
            AndReturn([my_image])
        self.m.ReplayAll()

        self.assertEqual(img_id, glance_utils.get_image_id(self.glance_client,
                                                           img_name))
        self.m.VerifyAll()
Exemple #19
0
    def test_get_image_id_by_name_in_uuid(self):
        """Tests the get_image_id function by name in uuid."""
        my_image = self.m.CreateMockAnything()
        img_id = str(uuid.uuid4())
        img_name = str(uuid.uuid4())
        my_image.id = img_id
        my_image.name = img_name
        self.glance_client.images = self.m.CreateMockAnything()
        self.glance_client.images.get(img_name).AndRaise(
            glance_exceptions.NotFound(404))
        filters = {'name': img_name}
        self.glance_client.images.list(filters=filters).MultipleTimes().\
            AndReturn([my_image])
        self.m.ReplayAll()

        self.assertEqual(
            img_id, glance_utils.get_image_id(self.glance_client, img_name))
        self.m.VerifyAll()
Exemple #20
0
    def handle_update(self, json_snippet, tmpl_diff, prop_diff):
        if 'Metadata' in tmpl_diff:
            self.metadata_set(tmpl_diff['Metadata'])

        checkers = []
        server = None

        if self.METADATA in prop_diff:
            server = self.nova().servers.get(self.resource_id)
            nova_utils.meta_update(self.nova(),
                                   server,
                                   prop_diff[self.METADATA])

        if self.FLAVOR in prop_diff:

            flavor_update_policy = (
                prop_diff.get(self.FLAVOR_UPDATE_POLICY) or
                self.properties.get(self.FLAVOR_UPDATE_POLICY))

            if flavor_update_policy == 'REPLACE':
                raise resource.UpdateReplace(self.name)

            flavor = prop_diff[self.FLAVOR]
            flavor_id = nova_utils.get_flavor_id(self.nova(), flavor)
            if not server:
                server = self.nova().servers.get(self.resource_id)
            checker = scheduler.TaskRunner(nova_utils.resize, server, flavor,
                                           flavor_id)
            checkers.append(checker)

        if self.IMAGE in prop_diff:
            image_update_policy = (
                prop_diff.get(self.IMAGE_UPDATE_POLICY) or
                self.properties.get(self.IMAGE_UPDATE_POLICY))
            if image_update_policy == 'REPLACE':
                raise resource.UpdateReplace(self.name)
            image = prop_diff[self.IMAGE]
            image_id = glance_utils.get_image_id(self.glance(), image)
            if not server:
                server = self.nova().servers.get(self.resource_id)
            preserve_ephemeral = (
                image_update_policy == 'REBUILD_PRESERVE_EPHEMERAL')
            checker = scheduler.TaskRunner(
                nova_utils.rebuild, server, image_id,
                preserve_ephemeral=preserve_ephemeral)
            checkers.append(checker)

        if self.NAME in prop_diff:
            if not server:
                server = self.nova().servers.get(self.resource_id)
            nova_utils.rename(server, prop_diff[self.NAME])

        if self.NETWORKS in prop_diff:
            new_networks = prop_diff.get(self.NETWORKS)
            attach_first_free_port = False
            if not new_networks:
                new_networks = []
                attach_first_free_port = True
            old_networks = self.properties.get(self.NETWORKS)

            if not server:
                server = self.nova().servers.get(self.resource_id)
            interfaces = server.interface_list()

            # if old networks is None, it means that the server got first
            # free port. so we should detach this interface.
            if old_networks is None:
                for iface in interfaces:
                    checker = scheduler.TaskRunner(server.interface_detach,
                                                   iface.port_id)
                    checkers.append(checker)
            # if we have any information in networks field, we should:
            # 1. find similar networks, if they exist
            # 2. remove these networks from new_networks and old_networks
            #    lists
            # 3. detach unmatched networks, which were present in old_networks
            # 4. attach unmatched networks, which were present in new_networks
            else:
                # remove not updated networks from old and new networks lists,
                # also get list these networks
                not_updated_networks = \
                    self._get_network_matches(old_networks, new_networks)

                self.update_networks_matching_iface_port(
                    old_networks + not_updated_networks, interfaces)

                # according to nova interface-detach command detached port
                # will be deleted
                for net in old_networks:
                    checker = scheduler.TaskRunner(server.interface_detach,
                                                   net.get('port'))
                    checkers.append(checker)

            # attach section similar for both variants that
            # were mentioned above

            for net in new_networks:
                if net.get('port'):
                    checker = scheduler.TaskRunner(server.interface_attach,
                                                   net['port'], None, None)
                    checkers.append(checker)
                elif net.get('network'):
                    checker = scheduler.TaskRunner(server.interface_attach,
                                                   None, net['network'],
                                                   net.get('fixed_ip'))
                    checkers.append(checker)

            # if new_networks is None, we should attach first free port,
            # according to similar behavior during instance creation
            if attach_first_free_port:
                checker = scheduler.TaskRunner(server.interface_attach,
                                               None, None, None)
                checkers.append(checker)

        # Optimization: make sure the first task is started before
        # check_update_complete.
        if checkers:
            checkers[0].start()

        return checkers
Exemple #21
0
    def handle_update(self, json_snippet, tmpl_diff, prop_diff):
        if 'Metadata' in tmpl_diff:
            self.metadata_set(tmpl_diff['Metadata'])

        checkers = []
        server = None

        if self.METADATA in prop_diff:
            server = self.nova().servers.get(self.resource_id)
            nova_utils.meta_update(self.nova(),
                                   server,
                                   prop_diff[self.METADATA])

        if self.FLAVOR in prop_diff:

            flavor_update_policy = (
                prop_diff.get(self.FLAVOR_UPDATE_POLICY) or
                self.properties.get(self.FLAVOR_UPDATE_POLICY))

            if flavor_update_policy == 'REPLACE':
                raise resource.UpdateReplace(self.name)

            flavor = prop_diff[self.FLAVOR]
            flavor_id = nova_utils.get_flavor_id(self.nova(), flavor)
            if not server:
                server = self.nova().servers.get(self.resource_id)
            checker = scheduler.TaskRunner(nova_utils.resize, server, flavor,
                                           flavor_id)
            checkers.append(checker)

        if self.IMAGE in prop_diff:
            image_update_policy = (
                prop_diff.get(self.IMAGE_UPDATE_POLICY) or
                self.properties.get(self.IMAGE_UPDATE_POLICY))
            if image_update_policy == 'REPLACE':
                raise resource.UpdateReplace(self.name)
            image = prop_diff[self.IMAGE]
            image_id = glance_utils.get_image_id(self.glance(), image)
            if not server:
                server = self.nova().servers.get(self.resource_id)
            preserve_ephemeral = (
                image_update_policy == 'REBUILD_PRESERVE_EPHEMERAL')
            checker = scheduler.TaskRunner(
                nova_utils.rebuild, server, image_id,
                preserve_ephemeral=preserve_ephemeral)
            checkers.append(checker)

        if self.NAME in prop_diff:
            if not server:
                server = self.nova().servers.get(self.resource_id)
            nova_utils.rename(server, prop_diff[self.NAME])

        if self.NETWORKS in prop_diff:
            new_networks = prop_diff.get(self.NETWORKS)
            attach_first_free_port = False
            if not new_networks:
                new_networks = []
                attach_first_free_port = True
            old_networks = self.properties.get(self.NETWORKS)

            if not server:
                server = self.nova().servers.get(self.resource_id)
            interfaces = server.interface_list()

            # if old networks is None, it means that the server got first
            # free port. so we should detach this interface.
            if old_networks is None:
                for iface in interfaces:
                    checker = scheduler.TaskRunner(server.interface_detach,
                                                   iface.port_id)
                    checkers.append(checker)
            # if we have any information in networks field, we should:
            # 1. find similar networks, if they exist
            # 2. remove these networks from new_networks and old_networks
            #    lists
            # 3. detach unmatched networks, which were present in old_networks
            # 4. attach unmatched networks, which were present in new_networks
            else:
                # remove not updated networks from old and new networks lists,
                # also get list these networks
                not_updated_networks = \
                    self._get_network_matches(old_networks, new_networks)

                self.update_networks_matching_iface_port(
                    old_networks + not_updated_networks, interfaces)

                # according to nova interface-detach command detached port
                # will be deleted
                for net in old_networks:
                    checker = scheduler.TaskRunner(server.interface_detach,
                                                   net.get('port'))
                    checkers.append(checker)

            # attach section similar for both variants that
            # were mentioned above

            for net in new_networks:
                if net.get('port'):
                    checker = scheduler.TaskRunner(server.interface_attach,
                                                   net['port'], None, None)
                    checkers.append(checker)
                elif net.get('network'):
                    checker = scheduler.TaskRunner(server.interface_attach,
                                                   None, net['network'],
                                                   net.get('fixed_ip'))
                    checkers.append(checker)

            # if new_networks is None, we should attach first free port,
            # according to similar behavior during instance creation
            if attach_first_free_port:
                checker = scheduler.TaskRunner(server.interface_attach,
                                               None, None, None)
                checkers.append(checker)

        # Optimization: make sure the first task is started before
        # check_update_complete.
        if checkers:
            checkers[0].start()

        return checkers
Exemple #22
0
 def validate_with_client(self, client, value):
     glance_client = client.glance()
     glance_utils.get_image_id(glance_client, value)
Exemple #23
0
 def image(self):
     """Return the server's image ID."""
     image = self.properties.get(self.IMAGE)
     if image and self._image is None:
         self._image = glance_utils.get_image_id(self.glance(), image)
     return self._image
Exemple #24
0
 def validate_with_client(self, client, value):
     glance_client = client.client('glance')
     glance_utils.get_image_id(glance_client, value)
Exemple #25
0
    def handle_create(self):
        security_groups = self.properties.get(self.SECURITY_GROUPS)

        user_data_format = self.properties.get(self.USER_DATA_FORMAT)
        ud_content = self.properties.get(self.USER_DATA)
        if self.user_data_software_config() or self.user_data_raw():
            if uuidutils.is_uuid_like(ud_content):
                # attempt to load the userdata from software config
                try:
                    ud_content = sc.SoftwareConfig.get_software_config(
                        self.heat(), ud_content)
                except exception.SoftwareConfigMissing:
                    # no config was found, so do not modify the user_data
                    pass

        if self.user_data_software_config():
            self._create_transport_credentials()
            self._populate_deployments_metadata()

        if self.properties[self.ADMIN_USER]:
            instance_user = self.properties[self.ADMIN_USER]
        elif cfg.CONF.instance_user:
            instance_user = cfg.CONF.instance_user
        else:
            instance_user = None

        userdata = nova_utils.build_userdata(
            self,
            ud_content,
            instance_user=instance_user,
            user_data_format=user_data_format)

        flavor = self.properties[self.FLAVOR]
        availability_zone = self.properties[self.AVAILABILITY_ZONE]

        image = self.properties.get(self.IMAGE)
        if image:
            image = glance_utils.get_image_id(self.glance(), image)

        flavor_id = nova_utils.get_flavor_id(self.nova(), flavor)

        instance_meta = self.properties.get(self.METADATA)
        if instance_meta is not None:
            instance_meta = nova_utils.meta_serialize(instance_meta)

        scheduler_hints = self.properties.get(self.SCHEDULER_HINTS)
        nics = self._build_nics(self.properties.get(self.NETWORKS))
        block_device_mapping = self._build_block_device_mapping(
            self.properties.get(self.BLOCK_DEVICE_MAPPING))
        reservation_id = self.properties.get(self.RESERVATION_ID)
        disk_config = self.properties.get(self.DISK_CONFIG)
        admin_pass = self.properties.get(self.ADMIN_PASS) or None
        personality_files = self.properties.get(self.PERSONALITY)
        key_name = self.properties.get(self.KEY_NAME)

        server = None
        try:
            server = self.nova().servers.create(
                name=self._server_name(),
                image=image,
                flavor=flavor_id,
                key_name=key_name,
                security_groups=security_groups,
                userdata=userdata,
                meta=instance_meta,
                scheduler_hints=scheduler_hints,
                nics=nics,
                availability_zone=availability_zone,
                block_device_mapping=block_device_mapping,
                reservation_id=reservation_id,
                config_drive=self._config_drive(),
                disk_config=disk_config,
                files=personality_files,
                admin_pass=admin_pass)
        finally:
            # Avoid a race condition where the thread could be cancelled
            # before the ID is stored
            if server is not None:
                self.resource_id_set(server.id)

        return server
Exemple #26
0
    def handle_create(self):
        security_groups = self._get_security_groups()

        userdata = self.properties[self.USER_DATA] or ''
        flavor = self.properties[self.INSTANCE_TYPE]
        availability_zone = self.properties[self.AVAILABILITY_ZONE]

        image_name = self.properties[self.IMAGE_ID]

        image_id = glance_utils.get_image_id(self.glance(), image_name)

        flavor_id = nova_utils.get_flavor_id(self.nova(), flavor)

        scheduler_hints = {}
        if self.properties[self.NOVA_SCHEDULER_HINTS]:
            for tm in self.properties[self.NOVA_SCHEDULER_HINTS]:
                # adopted from novaclient shell
                hint = tm[self.TAG_KEY]
                hint_value = tm[self.TAG_VALUE]
                if hint in scheduler_hints:
                    if isinstance(scheduler_hints[hint], six.string_types):
                        scheduler_hints[hint] = [scheduler_hints[hint]]
                    scheduler_hints[hint].append(hint_value)
                else:
                    scheduler_hints[hint] = hint_value
        else:
            scheduler_hints = None

        nics = self._build_nics(self.properties[self.NETWORK_INTERFACES],
                                security_groups=security_groups,
                                subnet_id=self.properties[self.SUBNET_ID])
        server = None

        # FIXME(shadower): the instance_user config option is deprecated. Once
        # it's gone, we should always use ec2-user for compatibility with
        # CloudFormation.
        if cfg.CONF.instance_user:
            instance_user = cfg.CONF.instance_user
        else:
            instance_user = '******'

        try:
            server = self.nova().servers.create(
                name=self.physical_resource_name(),
                image=image_id,
                flavor=flavor_id,
                key_name=self.properties[self.KEY_NAME],
                security_groups=security_groups,
                userdata=nova_utils.build_userdata(self, userdata,
                                                   instance_user),
                meta=self._get_nova_metadata(self.properties),
                scheduler_hints=scheduler_hints,
                nics=nics,
                availability_zone=availability_zone)
        finally:
            # Avoid a race condition where the thread could be cancelled
            # before the ID is stored
            if server is not None:
                self.resource_id_set(server.id)

        return server, scheduler.TaskRunner(self._attach_volumes_task())
Exemple #27
0
 def _mock_get_image_id_fail(self, image_id, exp):
     g_cli_mock = self.m.CreateMockAnything()
     self.m.StubOutWithMock(clients.OpenStackClients, 'glance')
     clients.OpenStackClients.glance().MultipleTimes().AndReturn(g_cli_mock)
     self.m.StubOutWithMock(glance_utils, 'get_image_id')
     glance_utils.get_image_id(g_cli_mock, image_id).AndRaise(exp)
Exemple #28
0
    def handle_create(self):
        security_groups = self.properties.get(self.SECURITY_GROUPS)

        user_data_format = self.properties.get(self.USER_DATA_FORMAT)
        ud_content = self.properties.get(self.USER_DATA)
        if self.user_data_software_config() or self.user_data_raw():
            if uuidutils.is_uuid_like(ud_content):
                # attempt to load the userdata from software config
                try:
                    ud_content = sc.SoftwareConfig.get_software_config(
                        self.heat(), ud_content)
                except exception.SoftwareConfigMissing:
                    # no config was found, so do not modify the user_data
                    pass

        if self.user_data_software_config():
            self._create_transport_credentials()
            self._populate_deployments_metadata()

        if self.properties[self.ADMIN_USER]:
            instance_user = self.properties[self.ADMIN_USER]
        elif cfg.CONF.instance_user:
            instance_user = cfg.CONF.instance_user
        else:
            instance_user = None

        userdata = nova_utils.build_userdata(
            self,
            ud_content,
            instance_user=instance_user,
            user_data_format=user_data_format)

        flavor = self.properties[self.FLAVOR]
        availability_zone = self.properties[self.AVAILABILITY_ZONE]

        image = self.properties.get(self.IMAGE)
        if image:
            image = glance_utils.get_image_id(self.glance(), image)

        flavor_id = nova_utils.get_flavor_id(self.nova(), flavor)

        instance_meta = self.properties.get(self.METADATA)
        if instance_meta is not None:
            instance_meta = nova_utils.meta_serialize(instance_meta)

        scheduler_hints = self.properties.get(self.SCHEDULER_HINTS)
        nics = self._build_nics(self.properties.get(self.NETWORKS))
        block_device_mapping = self._build_block_device_mapping(
            self.properties.get(self.BLOCK_DEVICE_MAPPING))
        reservation_id = self.properties.get(self.RESERVATION_ID)
        disk_config = self.properties.get(self.DISK_CONFIG)
        admin_pass = self.properties.get(self.ADMIN_PASS) or None
        personality_files = self.properties.get(self.PERSONALITY)
        key_name = self.properties.get(self.KEY_NAME)

        server = None
        try:
            server = self.nova().servers.create(
                name=self.physical_resource_name(),
                image=image,
                flavor=flavor_id,
                key_name=key_name,
                security_groups=security_groups,
                userdata=userdata,
                meta=instance_meta,
                scheduler_hints=scheduler_hints,
                nics=nics,
                availability_zone=availability_zone,
                block_device_mapping=block_device_mapping,
                reservation_id=reservation_id,
                config_drive=self._config_drive(),
                disk_config=disk_config,
                files=personality_files,
                admin_pass=admin_pass)
        finally:
            # Avoid a race condition where the thread could be cancelled
            # before the ID is stored
            if server is not None:
                self.resource_id_set(server.id)

        return server
Exemple #29
0
    def handle_create(self):
        security_groups = self._get_security_groups()

        userdata = self.properties[self.USER_DATA] or ''
        flavor = self.properties[self.INSTANCE_TYPE]
        availability_zone = self.properties[self.AVAILABILITY_ZONE]

        image_name = self.properties[self.IMAGE_ID]

        image_id = glance_utils.get_image_id(self.glance(), image_name)

        flavor_id = nova_utils.get_flavor_id(self.nova(), flavor)

        scheduler_hints = {}
        if self.properties[self.NOVA_SCHEDULER_HINTS]:
            for tm in self.properties[self.NOVA_SCHEDULER_HINTS]:
                # adopted from novaclient shell
                hint = tm[self.TAG_KEY]
                hint_value = tm[self.TAG_VALUE]
                if hint in scheduler_hints:
                    if isinstance(scheduler_hints[hint], six.string_types):
                        scheduler_hints[hint] = [scheduler_hints[hint]]
                    scheduler_hints[hint].append(hint_value)
                else:
                    scheduler_hints[hint] = hint_value
        else:
            scheduler_hints = None

        nics = self._build_nics(self.properties[self.NETWORK_INTERFACES],
                                security_groups=security_groups,
                                subnet_id=self.properties[self.SUBNET_ID])
        server = None

        # FIXME(shadower): the instance_user config option is deprecated. Once
        # it's gone, we should always use ec2-user for compatibility with
        # CloudFormation.
        if cfg.CONF.instance_user:
            instance_user = cfg.CONF.instance_user
        else:
            instance_user = '******'

        try:
            server = self.nova().servers.create(
                name=self.physical_resource_name(),
                image=image_id,
                flavor=flavor_id,
                key_name=self.properties[self.KEY_NAME],
                security_groups=security_groups,
                userdata=nova_utils.build_userdata(self, userdata,
                                                   instance_user),
                meta=self._get_nova_metadata(self.properties),
                scheduler_hints=scheduler_hints,
                nics=nics,
                availability_zone=availability_zone)
        finally:
            # Avoid a race condition where the thread could be cancelled
            # before the ID is stored
            if server is not None:
                self.resource_id_set(server.id)

        return server, scheduler.TaskRunner(self._attach_volumes_task())
Exemple #30
0
 def image(self):
     """Return the server's image ID."""
     image = self.properties.get(self.IMAGE)
     if image and self._image is None:
         self._image = glance_utils.get_image_id(self.glance(), image)
     return self._image