Exemple #1
0
    def monitorstatusmessage(self, req):

        context = req.environ['nova.context']
        statusdict = {
            'nova': '0',
            'glance': '0',
            'cinder': '0',
            'neutron': '0',
            'keystone': '0',
            'mysql': '0',
            'rabbitmq': '0'
        }

        for (key, value) in statusdict.items():
            try:
                if key == 'cinder':
                    self.cinder_api.get_all(context)
                elif key == 'glance':
                    glance.get_default_image_service()
                elif key == 'nova':
                    self.aggregate_api.get_aggregate_list(context)
                elif key == 'neutron':
                    self.network_api.get_all(context)
                elif key == 'db':
                    dbapi.service_get_all(context)
                elif key == 'rabbitmq' or key == 'keystone':
                    api_version_request.min_api_version()
            except Exception, e:
                statusdict[key] = '1'
Exemple #2
0
    def _refresh_os_mixins(self, extras):
        """
        Register images as OsTemplate mixins from
        information retrieved from glance (shared and user-specific).
        """
        template_schema = 'http://schemas.openstack.org/template/os#'
        image_service = glance.get_default_image_service()

        images = image_service.detail(extras['nova_ctx'])

        for img in images:
            # If the image is a kernel or ram one
            # and we're not to filter them out then register it.
            if (((img['container_format'] or img['disk_format'])
                 in ('ari', 'aki'))):
                msg = 'Not registering kernel/RAM image.'
                LOG.debug(msg)
                continue
            ctg_term = occify_terms(img['name'])
            os_template = os_mixins.OsTemplate(
                term=ctg_term,
                scheme=template_schema,
                os_id=img['id'],
                related=[infrastructure.OS_TEMPLATE],
                attributes=None,
                title='This is an OS ' + img['name'] + ' VM image',
                location='/' + ctg_term + '/')

            try:
                self.registry.get_backend(os_template, extras)
            except AttributeError:
                msg = 'Registering an OS image type as: %s' % str(os_template)
                LOG.debug(msg)
                self.register_backend(os_template, MIXIN_BACKEND)
Exemple #3
0
    def setUp(self):
        super(SchedulerTestCase, self).setUp()
        self.stubs.Set(compute_api, 'API', fakes.FakeComputeAPI)

        def fake_show(meh, context, id):
            if id:
                return {
                    'id': id,
                    'min_disk': None,
                    'min_ram': None,
                    'name': 'fake_name',
                    'status': 'active',
                    'properties': {
                        'kernel_id': 'fake_kernel_id',
                        'ramdisk_id': 'fake_ramdisk_id',
                        'something_else': 'meow'
                    }
                }
            else:
                raise exception.ImageNotFound(image_id=id)

        fake_image.stub_out_image_service(self.stubs)
        self.stubs.Set(fake_image._FakeImageService, 'show', fake_show)
        self.image_service = glance.get_default_image_service()

        self.driver = self.driver_cls()
        self.context = context.RequestContext('fake_user', 'fake_project')
        self.topic = 'fake_topic'
        self.servicegroup_api = servicegroup.API()
Exemple #4
0
    def _refresh_os_mixins(self, extras):
        """
        Register images as OsTemplate mixins from
        information retrieved from glance (shared and user-specific).
        """
        template_schema = 'http://schemas.openstack.org/template/os#'
        image_service = glance.get_default_image_service()

        images = image_service.detail(extras['nova_ctx'])

        for img in images:
            # If the image is a kernel or ram one
            # and we're not to filter them out then register it.
            if (((img['container_format'] or img['disk_format']) in ('ari',
                                                                     'aki'))):
                msg = 'Not registering kernel/RAM image.'
                LOG.debug(msg)
                continue
            ctg_term = occify_terms(img['name'])
            os_template = os_mixins.OsTemplate(term=ctg_term,
                                               scheme=template_schema,
                                               os_id=img['id'],
                                               related=[infrastructure.
                                                        OS_TEMPLATE],
                                               attributes=None,
                                               title='This is an OS ' +
                                                     img['name'] + ' VM image',
                                               location='/' + ctg_term + '/')

            try:
                self.registry.get_backend(os_template, extras)
            except AttributeError:
                msg = 'Registering an OS image type as: %s' % str(os_template)
                LOG.debug(msg)
                self.register_backend(os_template, MIXIN_BACKEND)
Exemple #5
0
    def setUp(self):
        super(SchedulerTestCase, self).setUp()
        self.stubs.Set(compute_api, "API", fakes.FakeComputeAPI)

        def fake_show(meh, context, id):
            if id:
                return {
                    "id": id,
                    "min_disk": None,
                    "min_ram": None,
                    "name": "fake_name",
                    "status": "active",
                    "properties": {
                        "kernel_id": "fake_kernel_id",
                        "ramdisk_id": "fake_ramdisk_id",
                        "something_else": "meow",
                    },
                }
            else:
                raise exception.ImageNotFound(image_id=id)

        fake_image.stub_out_image_service(self.stubs)
        self.stubs.Set(fake_image._FakeImageService, "show", fake_show)
        self.image_service = glance.get_default_image_service()

        self.driver = self.driver_cls()
        self.context = context.RequestContext("fake_user", "fake_project")
        self.topic = "fake_topic"
        self.servicegroup_api = servicegroup.API()
Exemple #6
0
    def _upload_files(self, context, instance_ref, blessed_files):
        image_service = glance.get_default_image_service()
        blessed_image_refs = []
        for blessed_file in blessed_files:

            image_name = blessed_file.split("/")[-1]
            image_ref = self._create_image(context, image_service,
                                           instance_ref, image_name)
            blessed_image_refs.append(image_ref)

            # Send up the file data to the newly created image.
            metadata = {
                'is_public': False,
                'status': 'active',
                'name': image_name,
                'properties': {
                    'image_state': 'available',
                    'owner_id': instance_ref['project_id']
                }
            }
            metadata['disk_format'] = "raw"
            metadata['container_format'] = "bare"

            # Upload that image to the image service
            with open(blessed_file) as image_file:
                image_service.update(context, image_ref, metadata, image_file)

        return blessed_image_refs
Exemple #7
0
    def _upload_files(self, context, instance_ref, blessed_files):
        image_service = glance.get_default_image_service()
        blessed_image_refs = []
        for blessed_file in blessed_files:

            image_name = blessed_file.split("/")[-1]
            image_ref = self._create_image(context, image_service, instance_ref, image_name)
            blessed_image_refs.append(image_ref)

            # Send up the file data to the newly created image.
            metadata = {'is_public': False,
                        'status': 'active',
                        'name': image_name,
                        'properties': {
                                       'image_state': 'available',
                                       'owner_id': instance_ref['project_id']}
                        }
            metadata['disk_format'] = "raw"
            metadata['container_format'] = "bare"

            # Upload that image to the image service
            with open(blessed_file) as image_file:
                image_service.update(context,
                                     image_ref,
                                     metadata,
                                     image_file)

        return blessed_image_refs
Exemple #8
0
    def _test_create_volume_backed_image_with_metadata_from_volume(self, extra_metadata=None):
        def _fake_id(x):
            return "%s-%s-%s-%s" % (x * 8, x * 4, x * 4, x * 12)

        body = dict(create_image=dict(name="snapshot_of_volume_backed"))
        if extra_metadata:
            body["create_image"]["metadata"] = extra_metadata

        image_service = glance.get_default_image_service()

        def fake_block_device_mapping_get_all_by_instance(context, inst_id, use_slave=False):
            return [
                fake_block_device.FakeDbBlockDeviceDict(
                    {
                        "volume_id": _fake_id("a"),
                        "source_type": "snapshot",
                        "destination_type": "volume",
                        "volume_size": 1,
                        "device_name": "vda",
                        "snapshot_id": 1,
                        "boot_index": 0,
                        "delete_on_termination": False,
                        "no_device": None,
                    }
                )
            ]

        self.stubs.Set(db, "block_device_mapping_get_all_by_instance", fake_block_device_mapping_get_all_by_instance)

        instance = fakes.fake_instance_get(image_ref="", vm_state=vm_states.ACTIVE, root_device_name="/dev/vda")
        self.stubs.Set(db, "instance_get_by_uuid", instance)

        fake_metadata = {"test_key1": "test_value1", "test_key2": "test_value2"}
        volume = dict(
            id=_fake_id("a"), size=1, host="fake", display_description="fake", volume_image_metadata=fake_metadata
        )
        snapshot = dict(id=_fake_id("d"))
        self.mox.StubOutWithMock(self.controller.compute_api, "volume_api")
        volume_api = self.controller.compute_api.volume_api
        volume_api.get(mox.IgnoreArg(), volume["id"]).AndReturn(volume)
        volume_api.get(mox.IgnoreArg(), volume["id"]).AndReturn(volume)
        volume_api.create_snapshot_force(mox.IgnoreArg(), volume["id"], mox.IgnoreArg(), mox.IgnoreArg()).AndReturn(
            snapshot
        )

        req = fakes.HTTPRequestV3.blank(self.url)

        self.mox.ReplayAll()
        response = self.controller._action_create_image(req, FAKE_UUID, body)
        location = response.headers["Location"]
        image_id = location.replace("http://localhost:9292/images/", "")
        image = image_service.show(None, image_id)

        properties = image["properties"]
        self.assertEqual(properties["test_key1"], "test_value1")
        self.assertEqual(properties["test_key2"], "test_value2")
        if extra_metadata:
            for key, val in extra_metadata.items():
                self.assertEqual(properties[key], val)
    def snapshot(self, context, instance, image_id, update_task_state):
        """Snapshots the specified instance.
        :param context: security context
        :param instance: nova.objects.instance.Instance
        :param image_id: Reference to a pre-created image that will
                         hold the snapshot.
        """
        return
        config = self.load_config()

        azure_sms = self.get_management_service(ServiceManagementService,
                                                config=config)

        # Power off vm
        result = self.power_off(instance=instance)
        result.wait()

        hosted_service_name = 'compunovacloud'
        deployment_name = 'dep1'
        vm_name = 'vm1'
        image_name = instance.uuid + 'image'

        image = CaptureRoleAsVMImage('Specialized', image_name,
                                     image_name + 'label',
                                     image_name + 'description', 'english',
                                     'openstack-virtual-machines')

        result = azure_sms.capture_vm_image(hosted_service_name,
                                            deployment_name, vm_name, image)

        image_service = glance.get_default_image_service()

        snapshot = image_service.show(context, image_id)
        LOG.debug("**** Snapshot info--> %s" % snapshot)
        snapshot_name = haikunator.haikunate()
        image_url = glance.generate_image_url(image_id)
        LOG.debug("**** image url--> '%s' ****" % image_url)

        image_metadata = {
            'is_public': False,
            'status': 'active',
            'name': '-'.join(('azure', snapshot_name)),
            'properties': {
                'kernel_id': instance['kernel_id'],
                'image_location': 'snapshot',
                'image_state': 'available',
                'ramdisk_id': instance['ramdisk_id'],
                'owner_id': instance['project_id']
            }
        }
        if instance['os_type']:
            image_metadata['properties']['os_type'] = instance['os_type']

        update_task_state(task_state=task_states.IMAGE_UPLOADING,
                          expected_state=task_states.IMAGE_SNAPSHOT)

        azure_sms.snapshot(service_name, vm_name, image_id, snapshot_name)
        image_service.update(context, image_id, image_metadata,
                             "fake image data")
    def test_create_volume_backed_image_with_metadata_from_volume(self):
        def _fake_id(x):
            return '%s-%s-%s-%s' % (x * 8, x * 4, x * 4, x * 12)

        body = dict(createImage=dict(name='snapshot_of_volume_backed'))

        image_service = glance.get_default_image_service()

        def fake_block_device_mapping_get_all_by_instance(context, inst_id):
            return [
                dict(volume_id=_fake_id('a'),
                     source_type='snapshot',
                     destination_type='volume',
                     volume_size=1,
                     device_name='vda',
                     snapshot_id=1,
                     boot_index=0,
                     delete_on_termination=False,
                     no_device=None)
            ]

        self.stubs.Set(db, 'block_device_mapping_get_all_by_instance',
                       fake_block_device_mapping_get_all_by_instance)

        instance = fakes.fake_instance_get(image_ref='',
                                           vm_state=vm_states.ACTIVE,
                                           root_device_name='/dev/vda')
        self.stubs.Set(db, 'instance_get_by_uuid', instance)

        volume = dict(id=_fake_id('a'),
                      size=1,
                      host='fake',
                      display_description='fake')
        snapshot = dict(id=_fake_id('d'))
        self.mox.StubOutWithMock(self.controller.compute_api, 'volume_api')
        volume_api = self.controller.compute_api.volume_api
        volume_api.get(mox.IgnoreArg(), volume['id']).AndReturn(volume)
        volume_api.create_snapshot_force(mox.IgnoreArg(), volume['id'],
                                         mox.IgnoreArg(),
                                         mox.IgnoreArg()).AndReturn(snapshot)

        def fake_bdm_image_metadata(fd, context, bdms, legacy_bdm):
            return {'test_key1': 'test_value1', 'test_key2': 'test_value2'}

        req = fakes.HTTPRequest.blank(self.url)
        self.stubs.Set(compute_api.API, '_get_bdm_image_metadata',
                       fake_bdm_image_metadata)

        self.mox.ReplayAll()
        response = self.controller._action_create_image(req, FAKE_UUID, body)
        location = response.headers['Location']
        image_id = location.replace('http://localhost/v2/fake/images/', '')
        image = image_service.show(None, image_id)

        properties = image['properties']
        self.assertEqual(properties['test_key1'], 'test_value1')
        self.assertEqual(properties['test_key2'], 'test_value2')
Exemple #11
0
 def _delete_images(self, context, image_refs):
     image_service = glance.get_default_image_service()
     for image_ref in image_refs:
         try:
             image_service.delete(context, image_ref)
         except exception.ImageNotFound:
             # Simply ignore this error because the end result
             # is that the image is no longer there.
             LOG.debug("The image %s was not found in the image service when removing it." % (image_ref))
Exemple #12
0
    def test_create_volume_backed_image_with_metadata_from_volume(self):

        def _fake_id(x):
            return '%s-%s-%s-%s' % (x * 8, x * 4, x * 4, x * 12)

        body = dict(create_image=dict(name='snapshot_of_volume_backed'))

        image_service = glance.get_default_image_service()

        def fake_block_device_mapping_get_all_by_instance(context, inst_id,
                                                          use_slave=False):
            return [fake_block_device.FakeDbBlockDeviceDict(
                        {'volume_id': _fake_id('a'),
                         'source_type': 'snapshot',
                         'destination_type': 'volume',
                         'volume_size': 1,
                         'device_name': 'vda',
                         'snapshot_id': 1,
                         'boot_index': 0,
                         'delete_on_termination': False,
                         'no_device': None})]

        self.stubs.Set(db, 'block_device_mapping_get_all_by_instance',
                       fake_block_device_mapping_get_all_by_instance)

        instance = fakes.fake_instance_get(image_ref='',
                                           vm_state=vm_states.ACTIVE,
                                           root_device_name='/dev/vda')
        self.stubs.Set(db, 'instance_get_by_uuid', instance)

        fake_metadata = {'test_key1': 'test_value1',
                         'test_key2': 'test_value2'}
        volume = dict(id=_fake_id('a'),
                      size=1,
                      host='fake',
                      display_description='fake',
                      volume_image_metadata=fake_metadata)
        snapshot = dict(id=_fake_id('d'))
        self.mox.StubOutWithMock(self.controller.compute_api, 'volume_api')
        volume_api = self.controller.compute_api.volume_api
        volume_api.get(mox.IgnoreArg(), volume['id']).AndReturn(volume)
        volume_api.get(mox.IgnoreArg(), volume['id']).AndReturn(volume)
        volume_api.create_snapshot_force(mox.IgnoreArg(), volume['id'],
               mox.IgnoreArg(), mox.IgnoreArg()).AndReturn(snapshot)

        req = fakes.HTTPRequestV3.blank(self.url)

        self.mox.ReplayAll()
        response = self.controller._action_create_image(req, FAKE_UUID, body)
        location = response.headers['Location']
        image_id = location.replace('http://localhost:9292/images/', '')
        image = image_service.show(None, image_id)

        properties = image['properties']
        self.assertEqual(properties['test_key1'], 'test_value1')
        self.assertEqual(properties['test_key2'], 'test_value2')
Exemple #13
0
 def _delete_images(self, context, image_refs):
     image_service = glance.get_default_image_service()
     for image_ref in image_refs:
         try:
             image_service.delete(context, image_ref)
         except exception.ImageNotFound:
             # Simply ignore this error because the end result
             # is that the image is no longer there.
             LOG.debug(
                 "The image %s was not found in the image service when removing it."
                 % (image_ref))
Exemple #14
0
    def _get_session(self, _context):
        """Returns a client session that can be used to query for image
        information.

        :param _context: The `nova.context.Context` object for the request
        """
        # TODO(jaypipes): Refactor glance.get_remote_image_service and
        #                 glance.get_default_image_service into a single
        #                 method that takes a context and actually respects
        #                 it, returning a real session object that keeps
        #                 the context alive...
        return glance.get_default_image_service()
Exemple #15
0
    def _get_session(self, _context):
        """Returns a client session that can be used to query for image
        information.

        :param context: The `nova.context.Context` object for the request
        """
        #TODO(jaypipes): Refactor glance.get_remote_image_service and
        #                glance.get_default_image_service into a single
        #                method that takes a context and actually respects
        #                it, returning a real session object that keeps
        #                the context alive...
        return glance.get_default_image_service()
    def test_create_volume_backed_image_with_metadata_from_volume(self):
        def _fake_id(x):
            return "%s-%s-%s-%s" % (x * 8, x * 4, x * 4, x * 12)

        body = dict(createImage=dict(name="snapshot_of_volume_backed"))

        image_service = glance.get_default_image_service()

        def fake_block_device_mapping_get_all_by_instance(context, inst_id):
            return [
                dict(
                    volume_id=_fake_id("a"),
                    source_type="snapshot",
                    destination_type="volume",
                    volume_size=1,
                    device_name="vda",
                    snapshot_id=1,
                    boot_index=0,
                    delete_on_termination=False,
                    no_device=None,
                )
            ]

        self.stubs.Set(db, "block_device_mapping_get_all_by_instance", fake_block_device_mapping_get_all_by_instance)

        instance = fakes.fake_instance_get(image_ref="", vm_state=vm_states.ACTIVE, root_device_name="/dev/vda")
        self.stubs.Set(db, "instance_get_by_uuid", instance)

        volume = dict(id=_fake_id("a"), size=1, host="fake", display_description="fake")
        snapshot = dict(id=_fake_id("d"))
        self.mox.StubOutWithMock(self.controller.compute_api, "volume_api")
        volume_api = self.controller.compute_api.volume_api
        volume_api.get(mox.IgnoreArg(), volume["id"]).AndReturn(volume)
        volume_api.create_snapshot_force(mox.IgnoreArg(), volume["id"], mox.IgnoreArg(), mox.IgnoreArg()).AndReturn(
            snapshot
        )

        def fake_bdm_image_metadata(fd, context, bdms, legacy_bdm):
            return {"test_key1": "test_value1", "test_key2": "test_value2"}

        req = fakes.HTTPRequest.blank(self.url)
        self.stubs.Set(compute_api.API, "_get_bdm_image_metadata", fake_bdm_image_metadata)

        self.mox.ReplayAll()
        response = self.controller._action_create_image(req, FAKE_UUID, body)
        location = response.headers["Location"]
        image_id = location.replace("http://localhost/v2/fake/images/", "")
        image = image_service.show(None, image_id)

        properties = image["properties"]
        self.assertEqual(properties["test_key1"], "test_value1")
        self.assertEqual(properties["test_key2"], "test_value2")
 def __init__(self, context, instance, destination,
              block_migration, disk_over_commit):
     self.context = context
     self.instance = instance
     self.destination = destination
     self.block_migration = block_migration
     self.disk_over_commit = disk_over_commit
     self.source = instance.host
     self.migrate_data = None
     self.compute_rpcapi = compute_rpcapi.ComputeAPI()
     self.servicegroup_api = servicegroup.API()
     self.scheduler_rpcapi = scheduler_rpcapi.SchedulerAPI()
     self.image_service = glance.get_default_image_service()
Exemple #18
0
 def __init__(self, context, instance, destination, block_migration,
              disk_over_commit):
     self.context = context
     self.instance = instance
     self.destination = destination
     self.block_migration = block_migration
     self.disk_over_commit = disk_over_commit
     self.source = instance.host
     self.migrate_data = None
     self.compute_rpcapi = compute_rpcapi.ComputeAPI()
     self.servicegroup_api = servicegroup.API()
     self.scheduler_rpcapi = scheduler_rpcapi.SchedulerAPI()
     self.image_service = glance.get_default_image_service()
Exemple #19
0
 def __init__(self, context, instance, destination, block_migration,
              disk_over_commit, select_hosts_callback):
     self.context = context
     self.instance = instance
     self.destination = destination
     self.block_migration = block_migration
     self.disk_over_commit = disk_over_commit
     self.select_hosts_callback = select_hosts_callback
     self.source = instance['host']
     self.migrate_data = None
     self.compute_rpcapi = compute_rpcapi.ComputeAPI()
     self.servicegroup_api = servicegroup.API()
     self.image_service = glance.get_default_image_service()
Exemple #20
0
 def __init__(self, context, instance, destination,
              block_migration, disk_over_commit,
              select_hosts_callback):
     self.context = context
     self.instance = instance
     self.destination = destination
     self.block_migration = block_migration
     self.disk_over_commit = disk_over_commit
     self.select_hosts_callback = select_hosts_callback
     self.source = instance['host']
     self.migrate_data = None
     self.compute_rpcapi = compute_rpcapi.ComputeAPI()
     self.servicegroup_api = servicegroup.API()
     self.image_service = glance.get_default_image_service()
Exemple #21
0
    def snapshot(self, context, instance, image_id, update_task_state):
        """Snapshot an image of the specified instance on EC2 and create an
        Image which gets stored in AMI (internally in EBS Snapshot)

        :param context: security context
        :param instance: nova.objects.instance.Instance
        :param image_id: Reference to a pre-created image that will hold the
        snapshot.
        """
        if instance.metadata.get('ec2_id', None) is None:
            raise exception.InstanceNotFound(instance_id=instance['uuid'])
        # Adding the below line only alters the state of the instance and not
        # its image in OpenStack.
        update_task_state(task_state=task_states.IMAGE_UPLOADING,
                          expected_state=task_states.IMAGE_SNAPSHOT)
        ec2_id = self._get_ec2_id_from_instance(instance)
        ec_instance_info = self.ec2_conn.get_only_instances(
            instance_ids=[ec2_id],
            filters=None,
            dry_run=False,
            max_results=None)
        ec2_instance = ec_instance_info[0]
        if ec2_instance.state == 'running':
            ec2_image_id = ec2_instance.create_image(
                name=str(image_id),
                description="Image created by OpenStack",
                no_reboot=False,
                dry_run=False)
            LOG.info("Image created: %s." % ec2_image_id)
        # The instance will be in pending state when it comes up, waiting
        # for it to be in available
        self._wait_for_image_state(ec2_image_id, "available")

        image_api = glance.get_default_image_service()
        image_ref = glance.generate_image_url(image_id)

        metadata = {
            'is_public': False,
            'location': image_ref,
            'properties': {
                'kernel_id': instance['kernel_id'],
                'image_state': 'available',
                'owner_id': instance['project_id'],
                'ramdisk_id': instance['ramdisk_id'],
                'ec2_image_id': ec2_image_id
            }
        }
        # TODO(jhurt): This currently fails, leaving the status of an instance
        #              as 'snapshotting'
        image_api.update(context, image_id, metadata)
Exemple #22
0
    def _get_image_ami_id_from_meta(self, context, image_lacking_meta):
        """Pulls the Image AMI ID from the location attribute of Image Meta

        :param image_meta:
        :return: ami_id
        """
        image_api = glance.get_default_image_service()
        image_meta = image_api._client.call(context, 2, 'get',
                                            image_lacking_meta.id)
        LOG.info("Calling _get_image_ami_id_from_meta Meta: %s" % image_meta)
        try:
            return image_meta['aws_image_id']
        except Exception as e:
            LOG.error("Error in parsing Image Id: %s" % e)
            raise exception.BuildAbortException("Invalid or Non-Existent Image"
                                                " ID Error")
Exemple #23
0
    def snapshot(self, context, instance, image_id, update_task_state):
        """Snapshot an image of the specified instance
        on EC2 and create an Image which gets stored in AMI (internally in EBS Snapshot)
        :param context: security context
        :param instance: nova.objects.instance.Instance
        :param image_id: Reference to a pre-created image that will hold the snapshot.
        """
        LOG.info("***** Calling SNAPSHOT *******************")

        if instance['metadata']['ec2_id'] is None:
            raise exception.InstanceNotRunning(instance_id=instance['uuid'])

        # Adding the below line only alters the state of the instance and not
        # its image in OpenStack.
        update_task_state(
            task_state=task_states.IMAGE_UPLOADING, expected_state=task_states.IMAGE_SNAPSHOT)
        ec2_id = instance['metadata']['ec2_id']
        ec_instance_info = self.ec2_conn.get_only_instances(
            instance_ids=[ec2_id], filters=None, dry_run=False, max_results=None)
        ec2_instance = ec_instance_info[0]
        if ec2_instance.state == 'running':
            ec2_image_id = ec2_instance.create_image(name=str(
                image_id), description="Image from OpenStack", no_reboot=False, dry_run=False)
            LOG.info("Image has been created state to %s." % ec2_image_id)

        # The instance will be in pending state when it comes up, waiting forit to be in available
        self._wait_for_image_state(ec2_image_id, "available")

        image_api = glance.get_default_image_service()
        image_ref = glance.generate_image_url(image_id)

        metadata = {'is_public': False,
                    'location': image_ref,
                    'properties': {
                        'kernel_id': instance['kernel_id'],
                        'image_state': 'available',
                        'owner_id': instance['project_id'],
                        'ramdisk_id': instance['ramdisk_id'],
                        'ec2_image_id': ec2_image_id }
                    }
        image_api.update(context, image_id, metadata)
    def snapshot(self, context, instance, image_id, update_task_state):
        aws_node = self._get_node_by_uuid(instance.uuid)

        update_task_state(task_state=task_states.IMAGE_UPLOADING,
                          expected_state=task_states.IMAGE_SNAPSHOT)

        if aws_node.state['Name'] == 'running':
            ec2_image = aws_node.create_image(
                Name=str(image_id),
                Description="Image from OpenStack",
                NoReboot=False,
                DryRun=False)
        else:
            # TODO: else case
            LOG.error(
                'Node state: "{}". Must be "running" for snapshot.'.format(
                    aws_node.state['Name']))
            return
        # The instance will be in pending state when it comes up, waiting for it to be in available
        self._wait_for_image_state(ec2_image, "available")

        image_api = glance.get_default_image_service()
        image_ref = glance.generate_image_url(image_id)

        metadata = {
            'is_public': False,
            'location': image_ref,
            'properties': {
                'kernel_id': instance['kernel_id'],
                'image_state': 'available',
                'owner_id': instance['project_id'],
                'ramdisk_id': instance['ramdisk_id'],
                'ec2_image_id': ec2_image.id
            }
        }
        # TODO: HTTPInternalServerError: 500 Internal Server Error:
        # TODO: The server has either erred or is incapable of performing the requested operation.
        image_api.update(context, image_id, metadata)
Exemple #25
0
    def setUp(self):
        super(SchedulerTestCase, self).setUp()
        self.stubs.Set(compute_api, 'API', fakes.FakeComputeAPI)

        def fake_show(meh, context, id):
            if id:
                return {'id': id, 'min_disk': None, 'min_ram': None,
                        'name': 'fake_name',
                        'status': 'active',
                        'properties': {'kernel_id': 'fake_kernel_id',
                                       'ramdisk_id': 'fake_ramdisk_id',
                                       'something_else': 'meow'}}
            else:
                raise exception.ImageNotFound(image_id=id)

        fake_image.stub_out_image_service(self.stubs)
        self.stubs.Set(fake_image._FakeImageService, 'show', fake_show)
        self.image_service = glance.get_default_image_service()

        self.driver = self.driver_cls()
        self.context = context.RequestContext('fake_user', 'fake_project')
        self.topic = 'fake_topic'
        self.servicegroup_api = servicegroup.API()
    def _do_test_create_volume_backed_image(self, extra_properties):
        def _fake_id(x):
            return "%s-%s-%s-%s" % (x * 8, x * 4, x * 4, x * 12)

        body = dict(createImage=dict(name="snapshot_of_volume_backed"))

        if extra_properties:
            body["createImage"]["metadata"] = extra_properties

        image_service = glance.get_default_image_service()

        bdm = [dict(volume_id=_fake_id("a"), volume_size=1, device_name="vda", delete_on_termination=False)]
        props = dict(
            kernel_id=_fake_id("b"), ramdisk_id=_fake_id("c"), root_device_name="/dev/vda", block_device_mapping=bdm
        )
        original_image = dict(properties=props, container_format="ami", status="active", is_public=True)

        image_service.create(None, original_image)

        def fake_block_device_mapping_get_all_by_instance(context, inst_id, use_slave=False):
            return [
                fake_block_device.FakeDbBlockDeviceDict(
                    {
                        "volume_id": _fake_id("a"),
                        "source_type": "snapshot",
                        "destination_type": "volume",
                        "volume_size": 1,
                        "device_name": "vda",
                        "snapshot_id": 1,
                        "boot_index": 0,
                        "delete_on_termination": False,
                        "no_device": None,
                    }
                )
            ]

        self.stubs.Set(db, "block_device_mapping_get_all_by_instance", fake_block_device_mapping_get_all_by_instance)

        instance = fakes.fake_instance_get(
            image_ref=original_image["id"], vm_state=vm_states.ACTIVE, root_device_name="/dev/vda"
        )
        self.stubs.Set(db, "instance_get_by_uuid", instance)

        volume = dict(id=_fake_id("a"), size=1, host="fake", display_description="fake")
        snapshot = dict(id=_fake_id("d"))
        self.mox.StubOutWithMock(self.controller.compute_api, "volume_api")
        volume_api = self.controller.compute_api.volume_api
        volume_api.get(mox.IgnoreArg(), volume["id"]).AndReturn(volume)
        volume_api.create_snapshot_force(mox.IgnoreArg(), volume["id"], mox.IgnoreArg(), mox.IgnoreArg()).AndReturn(
            snapshot
        )

        self.mox.ReplayAll()

        req = fakes.HTTPRequest.blank(self.url)
        response = self.controller._action_create_image(req, FAKE_UUID, body)

        location = response.headers["Location"]
        image_id = location.replace("http://localhost/v2/fake/images/", "")
        image = image_service.show(None, image_id)

        self.assertEqual(image["name"], "snapshot_of_volume_backed")
        properties = image["properties"]
        self.assertEqual(properties["kernel_id"], _fake_id("b"))
        self.assertEqual(properties["ramdisk_id"], _fake_id("c"))
        self.assertEqual(properties["root_device_name"], "/dev/vda")
        self.assertEqual(properties["bdm_v2"], True)
        bdms = properties["block_device_mapping"]
        self.assertEqual(len(bdms), 1)
        self.assertEqual(bdms[0]["boot_index"], 0)
        self.assertEqual(bdms[0]["source_type"], "snapshot")
        self.assertEqual(bdms[0]["destination_type"], "volume")
        self.assertEqual(bdms[0]["snapshot_id"], snapshot["id"])
        for fld in ("connection_info", "id", "instance_uuid", "device_name"):
            self.assertTrue(fld not in bdms[0])
        for k in extra_properties.keys():
            self.assertEqual(properties[k], extra_properties[k])
Exemple #27
0
    def _test_create_volume_backed_image_with_metadata_from_volume(
            self, extra_metadata=None):

        def _fake_id(x):
            return '%s-%s-%s-%s' % (x * 8, x * 4, x * 4, x * 12)

        body = dict(createImage=dict(name='snapshot_of_volume_backed'))
        if extra_metadata:
            body['createImage']['metadata'] = extra_metadata

        image_service = glance.get_default_image_service()

        def fake_block_device_mapping_get_all_by_instance(context, inst_id,
                                                          use_slave=False):
            return [fake_block_device.FakeDbBlockDeviceDict(
                        {'volume_id': _fake_id('a'),
                         'source_type': 'snapshot',
                         'destination_type': 'volume',
                         'volume_size': 1,
                         'device_name': 'vda',
                         'snapshot_id': 1,
                         'boot_index': 0,
                         'delete_on_termination': False,
                         'no_device': None})]

        self.stub_out('nova.db.block_device_mapping_get_all_by_instance',
                      fake_block_device_mapping_get_all_by_instance)

        instance = fakes.fake_instance_get(
            image_ref='',
            vm_state=vm_states.ACTIVE,
            root_device_name='/dev/vda',
            system_metadata={'image_test_key1': 'test_value1',
                             'image_test_key2': 'test_value2'})
        self.stub_out('nova.db.instance_get_by_uuid', instance)

        volume = dict(id=_fake_id('a'),
                      size=1,
                      host='fake',
                      display_description='fake')

        snapshot = dict(id=_fake_id('d'))

        with test.nested(
            mock.patch.object(self.controller.compute_api.compute_rpcapi,
                'quiesce_instance',
                side_effect=exception.InstanceQuiesceNotSupported(
                    instance_id='fake', reason='test')),
            mock.patch.object(self.controller.compute_api.volume_api, 'get',
                              return_value=volume),
            mock.patch.object(self.controller.compute_api.volume_api,
                              'create_snapshot_force',
                              return_value=snapshot),
        ) as (mock_quiesce, mock_vol_get, mock_vol_create):

            response = self.controller._action_create_image(self.req,
                FAKE_UUID, body=body)
            location = response.headers['Location']
            image_id = location.replace(self.image_base_url, '')
            image = image_service.show(None, image_id)

            properties = image['properties']
            self.assertEqual(properties['test_key1'], 'test_value1')
            self.assertEqual(properties['test_key2'], 'test_value2')
            if extra_metadata:
                for key, val in extra_metadata.items():
                    self.assertEqual(properties[key], val)

            mock_quiesce.assert_called_once_with(mock.ANY, mock.ANY)
            mock_vol_get.assert_called_once_with(mock.ANY, volume['id'])
            mock_vol_create.assert_called_once_with(mock.ANY, volume['id'],
                                                    mock.ANY, mock.ANY)
Exemple #28
0
    def _do_test_create_volume_backed_image(
            self, extra_properties, mock_vol_create_side_effect=None):

        def _fake_id(x):
            return '%s-%s-%s-%s' % (x * 8, x * 4, x * 4, x * 12)

        body = dict(createImage=dict(name='snapshot_of_volume_backed'))

        if extra_properties:
            body['createImage']['metadata'] = extra_properties

        image_service = glance.get_default_image_service()

        bdm = [dict(volume_id=_fake_id('a'),
                    volume_size=1,
                    device_name='vda',
                    delete_on_termination=False)]

        def fake_block_device_mapping_get_all_by_instance(context, inst_id,
                                                          use_slave=False):
            return [fake_block_device.FakeDbBlockDeviceDict(
                        {'volume_id': _fake_id('a'),
                         'source_type': 'snapshot',
                         'destination_type': 'volume',
                         'volume_size': 1,
                         'device_name': 'vda',
                         'snapshot_id': 1,
                         'boot_index': 0,
                         'delete_on_termination': False,
                         'no_device': None})]

        self.stub_out('nova.db.block_device_mapping_get_all_by_instance',
                      fake_block_device_mapping_get_all_by_instance)

        system_metadata = dict(image_kernel_id=_fake_id('b'),
                               image_ramdisk_id=_fake_id('c'),
                               image_root_device_name='/dev/vda',
                               image_block_device_mapping=str(bdm),
                               image_container_format='ami')
        instance = fakes.fake_instance_get(image_ref=uuids.fake,
                                           vm_state=vm_states.ACTIVE,
                                           root_device_name='/dev/vda',
                                           system_metadata=system_metadata)
        self.stub_out('nova.db.instance_get_by_uuid', instance)

        volume = dict(id=_fake_id('a'),
                      size=1,
                      host='fake',
                      display_description='fake')

        snapshot = dict(id=_fake_id('d'))

        with test.nested(
            mock.patch.object(self.controller.compute_api.compute_rpcapi,
                'quiesce_instance',
                side_effect=exception.InstanceQuiesceNotSupported(
                    instance_id='fake', reason='test')),
            mock.patch.object(self.controller.compute_api.volume_api, 'get',
                              return_value=volume),
            mock.patch.object(self.controller.compute_api.volume_api,
                              'create_snapshot_force',
                              return_value=snapshot),
        ) as (mock_quiesce, mock_vol_get, mock_vol_create):

            if mock_vol_create_side_effect:
                mock_vol_create.side_effect = mock_vol_create_side_effect

            response = self.controller._action_create_image(self.req,
                FAKE_UUID, body=body)

            location = response.headers['Location']
            image_id = location.replace(self.image_url or
                                        glance.generate_image_url(''), '')
            image = image_service.show(None, image_id)

            self.assertEqual(image['name'], 'snapshot_of_volume_backed')
            properties = image['properties']
            self.assertEqual(properties['kernel_id'], _fake_id('b'))
            self.assertEqual(properties['ramdisk_id'], _fake_id('c'))
            self.assertEqual(properties['root_device_name'], '/dev/vda')
            self.assertTrue(properties['bdm_v2'])
            bdms = properties['block_device_mapping']
            self.assertEqual(len(bdms), 1)
            self.assertEqual(bdms[0]['boot_index'], 0)
            self.assertEqual(bdms[0]['source_type'], 'snapshot')
            self.assertEqual(bdms[0]['destination_type'], 'volume')
            self.assertEqual(bdms[0]['snapshot_id'], snapshot['id'])
            self.assertEqual('/dev/vda', bdms[0]['device_name'])
            for fld in ('connection_info', 'id', 'instance_uuid'):
                self.assertNotIn(fld, bdms[0])
            for k in extra_properties.keys():
                self.assertEqual(properties[k], extra_properties[k])

            mock_quiesce.assert_called_once_with(mock.ANY, mock.ANY)
            mock_vol_get.assert_called_once_with(mock.ANY, volume['id'])
            mock_vol_create.assert_called_once_with(mock.ANY, volume['id'],
                                                    mock.ANY, mock.ANY)
Exemple #29
0
 def get_repository_url(self, context, image_ref):
     image_service = glance.get_default_image_service()
     image_top = image_service.show(context, image_ref)
     return image_top.get('name')
Exemple #30
0
 def __init__(self, image_service=None, **kwargs):
     self.image_service = (image_service or
                           glance.get_default_image_service())
     self.scheduler_rpcapi = scheduler_rpcapi.SchedulerAPI()
     super(API, self).__init__(**kwargs)
Exemple #31
0
    def _do_test_create_volume_backed_image(self, extra_properties):
        def _fake_id(x):
            return "%s-%s-%s-%s" % (x * 8, x * 4, x * 4, x * 12)

        body = dict(createImage=dict(name="snapshot_of_volume_backed"))

        if extra_properties:
            body["createImage"]["metadata"] = extra_properties

        image_service = glance.get_default_image_service()

        bdm = [dict(volume_id=_fake_id("a"), volume_size=1, device_name="vda", delete_on_termination=False)]

        def fake_block_device_mapping_get_all_by_instance(context, inst_id, use_slave=False):
            return [
                fake_block_device.FakeDbBlockDeviceDict(
                    {
                        "volume_id": _fake_id("a"),
                        "source_type": "snapshot",
                        "destination_type": "volume",
                        "volume_size": 1,
                        "device_name": "vda",
                        "snapshot_id": 1,
                        "boot_index": 0,
                        "delete_on_termination": False,
                        "no_device": None,
                    }
                )
            ]

        self.stub_out("nova.db.block_device_mapping_get_all_by_instance", fake_block_device_mapping_get_all_by_instance)

        system_metadata = dict(
            image_kernel_id=_fake_id("b"),
            image_ramdisk_id=_fake_id("c"),
            image_root_device_name="/dev/vda",
            image_block_device_mapping=str(bdm),
            image_container_format="ami",
        )
        instance = fakes.fake_instance_get(
            image_ref=str(uuid.uuid4()),
            vm_state=vm_states.ACTIVE,
            root_device_name="/dev/vda",
            system_metadata=system_metadata,
        )
        self.stub_out("nova.db.instance_get_by_uuid", instance)

        self.mox.StubOutWithMock(self.controller.compute_api.compute_rpcapi, "quiesce_instance")
        self.controller.compute_api.compute_rpcapi.quiesce_instance(mox.IgnoreArg(), mox.IgnoreArg()).AndRaise(
            exception.InstanceQuiesceNotSupported(instance_id="fake", reason="test")
        )

        volume = dict(id=_fake_id("a"), size=1, host="fake", display_description="fake")
        snapshot = dict(id=_fake_id("d"))
        self.mox.StubOutWithMock(self.controller.compute_api, "volume_api")
        volume_api = self.controller.compute_api.volume_api
        volume_api.get(mox.IgnoreArg(), volume["id"]).AndReturn(volume)
        volume_api.create_snapshot_force(mox.IgnoreArg(), volume["id"], mox.IgnoreArg(), mox.IgnoreArg()).AndReturn(
            snapshot
        )

        self.mox.ReplayAll()

        response = self.controller._action_create_image(self.req, FAKE_UUID, body=body)

        location = response.headers["Location"]
        image_id = location.replace(self.image_url or glance.generate_image_url(""), "")
        image = image_service.show(None, image_id)

        self.assertEqual(image["name"], "snapshot_of_volume_backed")
        properties = image["properties"]
        self.assertEqual(properties["kernel_id"], _fake_id("b"))
        self.assertEqual(properties["ramdisk_id"], _fake_id("c"))
        self.assertEqual(properties["root_device_name"], "/dev/vda")
        self.assertTrue(properties["bdm_v2"])
        bdms = properties["block_device_mapping"]
        self.assertEqual(len(bdms), 1)
        self.assertEqual(bdms[0]["boot_index"], 0)
        self.assertEqual(bdms[0]["source_type"], "snapshot")
        self.assertEqual(bdms[0]["destination_type"], "volume")
        self.assertEqual(bdms[0]["snapshot_id"], snapshot["id"])
        self.assertEqual("/dev/vda", bdms[0]["device_name"])
        for fld in ("connection_info", "id", "instance_uuid"):
            self.assertNotIn(fld, bdms[0])
        for k in extra_properties.keys():
            self.assertEqual(properties[k], extra_properties[k])
    def _do_test_create_volume_backed_image(self, extra_properties):

        def _fake_id(x):
            return '%s-%s-%s-%s' % (x * 8, x * 4, x * 4, x * 12)

        body = dict(createImage=dict(name='snapshot_of_volume_backed'))

        if extra_properties:
            body['createImage']['metadata'] = extra_properties

        image_service = glance.get_default_image_service()

        bdm = [dict(volume_id=_fake_id('a'),
                    volume_size=1,
                    device_name='vda',
                    delete_on_termination=False)]
        props = dict(kernel_id=_fake_id('b'),
                     ramdisk_id=_fake_id('c'),
                     root_device_name='/dev/vda',
                     block_device_mapping=bdm)
        original_image = dict(properties=props,
                              container_format='ami',
                              status='active',
                              is_public=True)

        image_service.create(None, original_image)

        def fake_block_device_mapping_get_all_by_instance(context, inst_id):
            class BDM(object):
                def __init__(self):
                    self.no_device = None
                    self.values = dict(volume_id=_fake_id('a'),
                                       virtual_name=None,
                                       volume_size=1,
                                       device_name='vda',
                                       snapshot_id=1,
                                       delete_on_termination=False)

                def __getattr__(self, name):
                    """Properly delegate dotted lookups"""
                    if name in self.__dict__['values']:
                        return self.values.get(name)
                    try:
                        return self.__dict__[name]
                    except KeyError:
                        raise AttributeError

                def __getitem__(self, key):
                    return self.values.get(key)

                def iteritems(self):
                    return self.values.iteritems()

            return [BDM()]

        self.stubs.Set(db, 'block_device_mapping_get_all_by_instance',
                       fake_block_device_mapping_get_all_by_instance)

        instance = fakes.fake_instance_get(image_ref=original_image['id'],
                                           vm_state=vm_states.ACTIVE,
                                           root_device_name='/dev/vda')
        self.stubs.Set(db, 'instance_get_by_uuid', instance)

        volume = dict(id=_fake_id('a'),
                      size=1,
                      host='fake',
                      display_description='fake')
        snapshot = dict(id=_fake_id('d'))
        self.mox.StubOutWithMock(self.controller.compute_api, 'volume_api')
        volume_api = self.controller.compute_api.volume_api
        volume_api.get(mox.IgnoreArg(), volume['id']).AndReturn(volume)
        volume_api.create_snapshot_force(mox.IgnoreArg(), volume,
                mox.IgnoreArg(), mox.IgnoreArg()).AndReturn(snapshot)

        self.mox.ReplayAll()

        req = fakes.HTTPRequest.blank(self.url)
        response = self.controller._action_create_image(req, FAKE_UUID, body)

        location = response.headers['Location']
        image_id = location.replace('http://localhost/v2/fake/images/', '')
        image = image_service.show(None, image_id)

        self.assertEquals(image['name'], 'snapshot_of_volume_backed')
        properties = image['properties']
        self.assertEquals(properties['kernel_id'], _fake_id('b'))
        self.assertEquals(properties['ramdisk_id'], _fake_id('c'))
        self.assertEquals(properties['root_device_name'], '/dev/vda')
        bdms = properties['block_device_mapping']
        self.assertEquals(len(bdms), 1)
        self.assertEquals(bdms[0]['device_name'], 'vda')
        self.assertEquals(bdms[0]['snapshot_id'], snapshot['id'])
        for k in extra_properties.keys():
            self.assertEquals(properties[k], extra_properties[k])
Exemple #33
0
 def __init__(self):
     super(ComputeTaskManager, self).__init__()
     self.compute_rpcapi = compute_rpcapi.ComputeAPI()
     self.scheduler_rpcapi = scheduler_rpcapi.SchedulerAPI()
     self.image_service = glance.get_default_image_service()
     self.quotas = quota.QUOTAS
Exemple #34
0
    def _do_test_create_volume_backed_image(
            self, extra_properties, mock_vol_create_side_effect=None):

        def _fake_id(x):
            return '%s-%s-%s-%s' % (x * 8, x * 4, x * 4, x * 12)

        body = dict(createImage=dict(name='snapshot_of_volume_backed'))

        if extra_properties:
            body['createImage']['metadata'] = extra_properties

        image_service = glance.get_default_image_service()

        bdm = [dict(volume_id=_fake_id('a'),
                    volume_size=1,
                    device_name='vda',
                    delete_on_termination=False)]

        def fake_block_device_mapping_get_all_by_instance(context, inst_id,
                                                          use_slave=False):
            return [fake_block_device.FakeDbBlockDeviceDict(
                        {'volume_id': _fake_id('a'),
                         'source_type': 'snapshot',
                         'destination_type': 'volume',
                         'volume_size': 1,
                         'device_name': 'vda',
                         'snapshot_id': 1,
                         'boot_index': 0,
                         'delete_on_termination': False,
                         'no_device': None})]

        self.stub_out('nova.db.api.block_device_mapping_get_all_by_instance',
                      fake_block_device_mapping_get_all_by_instance)

        system_metadata = dict(image_kernel_id=_fake_id('b'),
                               image_ramdisk_id=_fake_id('c'),
                               image_root_device_name='/dev/vda',
                               image_block_device_mapping=str(bdm),
                               image_container_format='ami')
        instance = fakes.fake_compute_get(image_ref=uuids.fake,
                                          vm_state=vm_states.ACTIVE,
                                          root_device_name='/dev/vda',
                                          system_metadata=system_metadata)
        self.stub_out('nova.compute.api.API.get', instance)

        volume = dict(id=_fake_id('a'),
                      size=1,
                      host='fake',
                      display_description='fake')

        snapshot = dict(id=_fake_id('d'))

        with test.nested(
            mock.patch.object(
                self.controller.compute_api.volume_api, 'get_absolute_limits',
                return_value={'totalSnapshotsUsed': 0,
                              'maxTotalSnapshots': 10}),
            mock.patch.object(self.controller.compute_api.compute_rpcapi,
                'quiesce_instance',
                side_effect=exception.InstanceQuiesceNotSupported(
                    instance_id='fake', reason='test')),
            mock.patch.object(self.controller.compute_api.volume_api, 'get',
                              return_value=volume),
            mock.patch.object(self.controller.compute_api.volume_api,
                              'create_snapshot_force',
                              return_value=snapshot),
        ) as (mock_get_limits, mock_quiesce, mock_vol_get, mock_vol_create):

            if mock_vol_create_side_effect:
                mock_vol_create.side_effect = mock_vol_create_side_effect

            response = self.controller._action_create_image(self.req,
                FAKE_UUID, body=body)

            location = response.headers['Location']
            image_id = location.replace(self.image_url or
                 self.image_api.generate_image_url('', self.context),
                                        '')
            image = image_service.show(None, image_id)

            self.assertEqual(image['name'], 'snapshot_of_volume_backed')
            properties = image['properties']
            self.assertEqual(properties['kernel_id'], _fake_id('b'))
            self.assertEqual(properties['ramdisk_id'], _fake_id('c'))
            self.assertEqual(properties['root_device_name'], '/dev/vda')
            self.assertTrue(properties['bdm_v2'])
            bdms = properties['block_device_mapping']
            self.assertEqual(len(bdms), 1)
            self.assertEqual(bdms[0]['boot_index'], 0)
            self.assertEqual(bdms[0]['source_type'], 'snapshot')
            self.assertEqual(bdms[0]['destination_type'], 'volume')
            self.assertEqual(bdms[0]['snapshot_id'], snapshot['id'])
            self.assertEqual('/dev/vda', bdms[0]['device_name'])
            for fld in ('connection_info', 'id', 'instance_uuid'):
                self.assertNotIn(fld, bdms[0])
            for k in extra_properties.keys():
                self.assertEqual(properties[k], extra_properties[k])

            mock_quiesce.assert_called_once_with(mock.ANY, mock.ANY)
            mock_vol_get.assert_called_once_with(mock.ANY, volume['id'])
            mock_vol_create.assert_called_once_with(mock.ANY, volume['id'],
                                                    mock.ANY, mock.ANY)
Exemple #35
0
    def _do_test_create_volume_backed_image(self, extra_properties):

        def _fake_id(x):
            return '%s-%s-%s-%s' % (x * 8, x * 4, x * 4, x * 12)

        body = dict(createImage=dict(name='snapshot_of_volume_backed'))

        if extra_properties:
            body['createImage']['metadata'] = extra_properties

        image_service = glance.get_default_image_service()

        bdm = [dict(volume_id=_fake_id('a'),
                    volume_size=1,
                    device_name='vda',
                    delete_on_termination=False)]

        def fake_block_device_mapping_get_all_by_instance(context, inst_id,
                                                          use_slave=False):
            return [fake_block_device.FakeDbBlockDeviceDict(
                        {'volume_id': _fake_id('a'),
                         'source_type': 'snapshot',
                         'destination_type': 'volume',
                         'volume_size': 1,
                         'device_name': 'vda',
                         'snapshot_id': 1,
                         'boot_index': 0,
                         'delete_on_termination': False,
                         'no_device': None})]

        self.stub_out('nova.db.block_device_mapping_get_all_by_instance',
                      fake_block_device_mapping_get_all_by_instance)

        system_metadata = dict(image_kernel_id=_fake_id('b'),
                               image_ramdisk_id=_fake_id('c'),
                               image_root_device_name='/dev/vda',
                               image_block_device_mapping=str(bdm),
                               image_container_format='ami')
        instance = fakes.fake_instance_get(image_ref=str(uuid.uuid4()),
                                           vm_state=vm_states.ACTIVE,
                                           root_device_name='/dev/vda',
                                           system_metadata=system_metadata)
        self.stub_out('nova.db.instance_get_by_uuid', instance)

        self.mox.StubOutWithMock(self.controller.compute_api.compute_rpcapi,
                                 'quiesce_instance')
        self.controller.compute_api.compute_rpcapi.quiesce_instance(
            mox.IgnoreArg(), mox.IgnoreArg()).AndRaise(
                exception.InstanceQuiesceNotSupported(instance_id='fake',
                                                      reason='test'))

        volume = dict(id=_fake_id('a'),
                      size=1,
                      host='fake',
                      display_description='fake')
        snapshot = dict(id=_fake_id('d'))
        self.mox.StubOutWithMock(self.controller.compute_api, 'volume_api')
        volume_api = self.controller.compute_api.volume_api
        volume_api.get(mox.IgnoreArg(), volume['id']).AndReturn(volume)
        volume_api.create_snapshot_force(mox.IgnoreArg(), volume['id'],
                mox.IgnoreArg(), mox.IgnoreArg()).AndReturn(snapshot)

        self.mox.ReplayAll()

        response = self.controller._action_create_image(self.req, FAKE_UUID,
                                                        body=body)

        location = response.headers['Location']
        image_id = location.replace(self.image_url or
                                        glance.generate_image_url(''), '')
        image = image_service.show(None, image_id)

        self.assertEqual(image['name'], 'snapshot_of_volume_backed')
        properties = image['properties']
        self.assertEqual(properties['kernel_id'], _fake_id('b'))
        self.assertEqual(properties['ramdisk_id'], _fake_id('c'))
        self.assertEqual(properties['root_device_name'], '/dev/vda')
        self.assertTrue(properties['bdm_v2'])
        bdms = properties['block_device_mapping']
        self.assertEqual(len(bdms), 1)
        self.assertEqual(bdms[0]['boot_index'], 0)
        self.assertEqual(bdms[0]['source_type'], 'snapshot')
        self.assertEqual(bdms[0]['destination_type'], 'volume')
        self.assertEqual(bdms[0]['snapshot_id'], snapshot['id'])
        self.assertEqual('/dev/vda', bdms[0]['device_name'])
        for fld in ('connection_info', 'id', 'instance_uuid'):
            self.assertNotIn(fld, bdms[0])
        for k in extra_properties.keys():
            self.assertEqual(properties[k], extra_properties[k])
Exemple #36
0
    def _do_test_create_volume_backed_image(self, extra_properties):
        def _fake_id(x):
            return '%s-%s-%s-%s' % (x * 8, x * 4, x * 4, x * 12)

        body = dict(createImage=dict(name='snapshot_of_volume_backed'))

        if extra_properties:
            body['createImage']['metadata'] = extra_properties

        image_service = glance.get_default_image_service()

        bdm = [
            dict(volume_id=_fake_id('a'),
                 volume_size=1,
                 device_name='vda',
                 delete_on_termination=False)
        ]
        props = dict(kernel_id=_fake_id('b'),
                     ramdisk_id=_fake_id('c'),
                     root_device_name='/dev/vda',
                     block_device_mapping=bdm)
        original_image = dict(properties=props,
                              container_format='ami',
                              status='active',
                              is_public=True)

        image_service.create(None, original_image)

        def fake_block_device_mapping_get_all_by_instance(context, inst_id):
            return [
                dict(volume_id=_fake_id('a'),
                     virtual_name=None,
                     volume_size=1,
                     device_name='vda',
                     snapshot_id=1,
                     delete_on_termination=False,
                     no_device=None)
            ]

        self.stubs.Set(db, 'block_device_mapping_get_all_by_instance',
                       fake_block_device_mapping_get_all_by_instance)

        instance = fakes.fake_instance_get(image_ref=original_image['id'],
                                           vm_state=vm_states.ACTIVE,
                                           root_device_name='/dev/vda')
        self.stubs.Set(db, 'instance_get_by_uuid', instance)

        volume = dict(id=_fake_id('a'),
                      size=1,
                      host='fake',
                      display_description='fake')
        snapshot = dict(id=_fake_id('d'))
        self.mox.StubOutWithMock(self.controller.compute_api, 'volume_api')
        volume_api = self.controller.compute_api.volume_api
        volume_api.get(mox.IgnoreArg(), volume['id']).AndReturn(volume)
        volume_api.create_snapshot_force(mox.IgnoreArg(), volume,
                                         mox.IgnoreArg(),
                                         mox.IgnoreArg()).AndReturn(snapshot)

        self.mox.ReplayAll()

        req = fakes.HTTPRequest.blank(self.url)
        response = self.controller._action_create_image(req, FAKE_UUID, body)

        location = response.headers['Location']
        image_id = location.replace('http://localhost/v2/fake/images/', '')
        image = image_service.show(None, image_id)

        self.assertEquals(image['name'], 'snapshot_of_volume_backed')
        properties = image['properties']
        self.assertEquals(properties['kernel_id'], _fake_id('b'))
        self.assertEquals(properties['ramdisk_id'], _fake_id('c'))
        self.assertEquals(properties['root_device_name'], '/dev/vda')
        bdms = properties['block_device_mapping']
        self.assertEquals(len(bdms), 1)
        self.assertEquals(bdms[0]['device_name'], 'vda')
        self.assertEquals(bdms[0]['snapshot_id'], snapshot['id'])
        for k in extra_properties.keys():
            self.assertEquals(properties[k], extra_properties[k])
Exemple #37
0
    def _test_create_volume_backed_image_with_metadata_from_volume(
            self, extra_metadata=None):

        def _fake_id(x):
            return '%s-%s-%s-%s' % (x * 8, x * 4, x * 4, x * 12)

        body = dict(createImage=dict(name='snapshot_of_volume_backed'))
        if extra_metadata:
            body['createImage']['metadata'] = extra_metadata

        image_service = glance.get_default_image_service()

        def fake_block_device_mapping_get_all_by_instance(context, inst_id,
                                                          use_slave=False):
            return [fake_block_device.FakeDbBlockDeviceDict(
                        {'volume_id': _fake_id('a'),
                         'source_type': 'snapshot',
                         'destination_type': 'volume',
                         'volume_size': 1,
                         'device_name': 'vda',
                         'snapshot_id': 1,
                         'boot_index': 0,
                         'delete_on_termination': False,
                         'no_device': None})]

        self.stub_out('nova.db.block_device_mapping_get_all_by_instance',
                      fake_block_device_mapping_get_all_by_instance)

        instance = fakes.fake_instance_get(
            image_ref='',
            vm_state=vm_states.ACTIVE,
            root_device_name='/dev/vda',
            system_metadata={'image_test_key1': 'test_value1',
                             'image_test_key2': 'test_value2'})
        self.stub_out('nova.db.instance_get_by_uuid', instance)

        self.mox.StubOutWithMock(self.controller.compute_api.compute_rpcapi,
                                 'quiesce_instance')
        self.controller.compute_api.compute_rpcapi.quiesce_instance(
            mox.IgnoreArg(), mox.IgnoreArg()).AndRaise(
                exception.InstanceQuiesceNotSupported(instance_id='fake',
                                                      reason='test'))

        volume = dict(id=_fake_id('a'),
                      size=1,
                      host='fake',
                      display_description='fake')
        snapshot = dict(id=_fake_id('d'))
        self.mox.StubOutWithMock(self.controller.compute_api, 'volume_api')
        volume_api = self.controller.compute_api.volume_api
        volume_api.get(mox.IgnoreArg(), volume['id']).AndReturn(volume)
        volume_api.create_snapshot_force(mox.IgnoreArg(), volume['id'],
               mox.IgnoreArg(), mox.IgnoreArg()).AndReturn(snapshot)

        self.mox.ReplayAll()
        response = self.controller._action_create_image(self.req, FAKE_UUID,
                                                        body=body)
        location = response.headers['Location']
        image_id = location.replace(self.image_base_url, '')
        image = image_service.show(None, image_id)

        properties = image['properties']
        self.assertEqual(properties['test_key1'], 'test_value1')
        self.assertEqual(properties['test_key2'], 'test_value2')
        if extra_metadata:
            for key, val in extra_metadata.items():
                self.assertEqual(properties[key], val)
Exemple #38
0
 def __init__(self, image_service=None):
     self.image_service = image_service if image_service is not None \
                                     else glance.get_default_image_service()
    def _do_test_create_volume_backed_image(self, extra_properties):

        def _fake_id(x):
            return '%s-%s-%s-%s' % (x * 8, x * 4, x * 4, x * 12)

        body = dict(createImage=dict(name='snapshot_of_volume_backed'))

        if extra_properties:
            body['createImage']['metadata'] = extra_properties

        image_service = glance.get_default_image_service()

        bdm = [dict(volume_id=_fake_id('a'),
                    volume_size=1,
                    device_name='vda',
                    delete_on_termination=False)]
        props = dict(kernel_id=_fake_id('b'),
                     ramdisk_id=_fake_id('c'),
                     root_device_name='/dev/vda',
                     block_device_mapping=bdm)
        original_image = dict(properties=props,
                              container_format='ami',
                              status='active',
                              is_public=True)

        image_service.create(None, original_image)

        def fake_block_device_mapping_get_all_by_instance(context, inst_id,
                                                          use_slave=False):
            return [fake_block_device.FakeDbBlockDeviceDict(
                        {'volume_id': _fake_id('a'),
                         'source_type': 'snapshot',
                         'destination_type': 'volume',
                         'volume_size': 1,
                         'device_name': 'vda',
                         'snapshot_id': 1,
                         'boot_index': 0,
                         'delete_on_termination': False,
                         'no_device': None})]

        self.stubs.Set(db, 'block_device_mapping_get_all_by_instance',
                       fake_block_device_mapping_get_all_by_instance)

        instance = fakes.fake_instance_get(image_ref=original_image['id'],
                                           vm_state=vm_states.ACTIVE,
                                           root_device_name='/dev/vda')
        self.stubs.Set(db, 'instance_get_by_uuid', instance)

        volume = dict(id=_fake_id('a'),
                      size=1,
                      host='fake',
                      display_description='fake')
        snapshot = dict(id=_fake_id('d'))
        self.mox.StubOutWithMock(self.controller.compute_api, 'volume_api')
        volume_api = self.controller.compute_api.volume_api
        volume_api.get(mox.IgnoreArg(), volume['id']).AndReturn(volume)
        volume_api.create_snapshot_force(mox.IgnoreArg(), volume['id'],
                mox.IgnoreArg(), mox.IgnoreArg()).AndReturn(snapshot)

        self.mox.ReplayAll()

        req = fakes.HTTPRequestV3.blank(self.url)
        response = self.controller._action_create_image(req, FAKE_UUID, body)

        location = response.headers['Location']
        image_id = location.replace(glance.generate_image_url(''), '')
        image = image_service.show(None, image_id)

        self.assertEqual(image['name'], 'snapshot_of_volume_backed')
        properties = image['properties']
        self.assertEqual(properties['kernel_id'], _fake_id('b'))
        self.assertEqual(properties['ramdisk_id'], _fake_id('c'))
        self.assertEqual(properties['root_device_name'], '/dev/vda')
        self.assertEqual(properties['bdm_v2'], True)
        bdms = properties['block_device_mapping']
        self.assertEqual(len(bdms), 1)
        self.assertEqual(bdms[0]['boot_index'], 0)
        self.assertEqual(bdms[0]['source_type'], 'snapshot')
        self.assertEqual(bdms[0]['destination_type'], 'volume')
        self.assertEqual(bdms[0]['snapshot_id'], snapshot['id'])
        for fld in ('connection_info', 'id',
                    'instance_uuid', 'device_name'):
            self.assertNotIn(fld, bdms[0])
        for k in extra_properties.keys():
            self.assertEqual(properties[k], extra_properties[k])
Exemple #40
0
    def pre_launch(self,
                   context,
                   new_instance_ref,
                   network_info=None,
                   block_device_info=None,
                   migration=False,
                   skip_image_service=False,
                   image_refs=[]):

        image_base_path = None
        if not (skip_image_service) and CONF.gridcentric_use_image_service:
            # We need to first download the descriptor and the disk files
            # from the image service.
            LOG.debug("Downloading images %s from the image service." %
                      (image_refs))
            image_base_path = os.path.join(CONF.instances_path, '_base')
            if not os.path.exists(image_base_path):
                LOG.debug(
                    'Base path %s does not exist. It will be created now.',
                    image_base_path)
                mkdir_as(image_base_path, self.openstack_uid)
            image_service = glance.get_default_image_service()
            for image_ref in image_refs:
                image = image_service.show(context, image_ref)
                target = os.path.join(image_base_path, image['name'])
                if migration or not os.path.exists(target):
                    # If the path does not exist fetch the data from the image
                    # service.  NOTE: We always fetch in the case of a
                    # migration, as the descriptor may have changed from its
                    # previous state. Migrating VMs are the only case where a
                    # descriptor for an instance will not be a fixed constant.
                    # We download to a temporary location so we can make the
                    # file appear atomically from the right user.
                    fd, temp_target = tempfile.mkstemp(dir=image_base_path)
                    try:
                        os.close(fd)
                        images.fetch(context, image_ref, temp_target,
                                     new_instance_ref['user_id'],
                                     new_instance_ref['project_id'])
                        os.chown(temp_target, self.openstack_uid,
                                 self.openstack_gid)
                        os.chmod(temp_target, 0644)
                        os.rename(temp_target, target)
                    except:
                        os.unlink(temp_target)
                        raise

        # (dscannell) Check to see if we need to convert the network_info
        # object into the legacy format.
        if network_info and self.libvirt_conn.legacy_nwinfo():
            network_info = network_info.legacy()

        # We need to create the libvirt xml, and associated files. Pass back
        # the path to the libvirt.xml file.
        working_dir = os.path.join(CONF.instances_path,
                                   new_instance_ref['name'])
        disk_file = os.path.join(working_dir, "disk")
        libvirt_file = os.path.join(working_dir, "libvirt.xml")

        # Make sure that our working directory exists.
        mkdir_as(working_dir, self.openstack_uid)

        if not (os.path.exists(disk_file)):
            # (dscannell) We will write out a stub 'disk' file so that we don't
            # end up copying this file when setting up everything for libvirt.
            # Essentially, this file will be removed, and replaced by vms as an
            # overlay on the blessed root image.
            touch_as(disk_file, self.openstack_uid)

        # (dscannell) We want to disable any injection. We do this by making a
        # copy of the instance and clearing out some entries. Since OpenStack
        # uses dictionary-list accessors, we can pass this dictionary through
        # that code.
        instance_dict = AttribDictionary(dict(new_instance_ref.iteritems()))

        # The name attribute is special and does not carry over like the rest
        # of the attributes.
        instance_dict['name'] = new_instance_ref['name']
        instance_dict.os_type = new_instance_ref['os_type']
        instance_dict['key_data'] = None
        instance_dict['metadata'] = []
        for network_ref, mapping in network_info:
            network_ref['injected'] = False

        # (dscannell) This was taken from the core nova project as part of the
        # boot path for normal instances. We basically want to mimic this
        # functionality.
        xml = self.libvirt_conn.to_xml(instance_dict,
                                       network_info,
                                       False,
                                       block_device_info=block_device_info)
        self.libvirt_conn._create_image(context,
                                        instance_dict,
                                        xml,
                                        network_info=network_info,
                                        block_device_info=block_device_info)

        if not (migration):
            # (dscannell) Remove the fake disk file (if created).
            os.remove(disk_file)

        # Fix up the permissions on the files that we created so that they are owned by the
        # openstack user.
        for root, dirs, files in os.walk(working_dir, followlinks=True):
            for path in dirs + files:
                LOG.debug("chowning path=%s to openstack user %s" % \
                         (os.path.join(root, path), self.openstack_uid))
                os.chown(os.path.join(root, path), self.openstack_uid,
                         self.openstack_gid)

        # Return the libvirt file, this will be passed in as the name. This
        # parameter is overloaded in the management interface as a libvirt
        # special case.
        return (libvirt_file, image_base_path)
Exemple #41
0
 def __init__(self, **kwargs):
     super(NovaSnapshotDriver, self).__init__(**kwargs)
     self.glance = glance.get_default_image_service()
     self.compute_api = compute.API()
Exemple #42
0
 def __init__(self):
     self.host_manager = importutils.import_object(
             CONF.scheduler_host_manager)
     self.compute_rpcapi = compute_rpcapi.ComputeAPI()
     self.servicegroup_api = servicegroup.API()
     self.image_service = glance.get_default_image_service()
Exemple #43
0
    def snapshot(self, context, instance, image_id, update_task_state):
        """Snapshot an image of the specified instance

        :param context: security context
        :param instance: nova.objects.instance.Instance
        :param image_id: Reference to a pre-created image holding the snapshot.

        Steps:
        1. Find boot disk
        2. Stop instance
        3. Create temporary boot disk snapshot
        4. Start instance
        5. Create temporary disk from snapshot
        6. Create image from disk
        7. Add Image info to glance
        8. Delete temporary disk
        9. Delete temporary snapshot
        """
        instance_stopped = False
        temp_disk_snapshot = False
        temp_disk_from_snapshot = False
        image_created = False

        compute, project, zone = self.gce_svc, self.gce_project, self.gce_zone

        try:
            gce_id = self._get_gce_name_from_instance(instance)
            LOG.info("Taking snapshot of instance %s" % instance.uuid)
            try:
                boot_disk = gceutils.get_instance_boot_disk(
                    compute, project, zone, gce_id)
            except AssertionError:
                reason = "Unable to find boot disk from instance metadata {0}"
                reason = reason.format(instance.uuid)
                raise exception.InvalidMetadata(reason=reason)
            disk_name = boot_disk['name']
            LOG.debug("1. Found boot disk %s for instance %s" %
                      (disk_name, instance.uuid))

            operation = gceutils.stop_instance(compute, project, zone, gce_id)
            gceutils.wait_for_operation(compute, project, operation)
            instance_stopped = True
            LOG.debug("2. Temporarily stopped instance %s" % instance.uuid)

            snapshot_name = 'nsnap-' + disk_name + time.strftime("%s")
            operation = gceutils.snapshot_disk(compute, project, zone,
                                               boot_disk['name'],
                                               snapshot_name)
            gceutils.wait_for_operation(compute, project, operation)
            temp_disk_snapshot = True
            LOG.debug("3. Created boot disk snapshot %s" % snapshot_name)

            operation = gceutils.start_instance(compute, project, zone, gce_id)
            gceutils.wait_for_operation(compute, project, operation)
            instance_stopped = False
            LOG.debug("4. Restart instance after disk snapshot %s" %
                      instance.uuid)

            snapshot_disk_name = 'vol-' + snapshot_name
            operation = gceutils.create_disk_from_snapshot(
                compute, project, zone, snapshot_disk_name, snapshot_name)
            gceutils.wait_for_operation(compute, project, operation)
            snapshot_disk_info = gceutils.get_disk(compute, project, zone,
                                                   snapshot_disk_name)
            temp_disk_from_snapshot = True
            LOG.debug("5. Created disk %s from snapshot %s" %
                      (snapshot_disk_name, snapshot_name))

            update_task_state(task_state=task_states.IMAGE_PENDING_UPLOAD)
            image_api = glance.get_default_image_service()
            image_data = image_api.show(context, image_id)
            name = image_data['name']
            operation = gceutils.create_image_from_disk(
                compute, project, name, snapshot_disk_info['selfLink'])
            gceutils.wait_for_operation(compute,
                                        project,
                                        operation,
                                        timeout=120)
            image_created = True
            LOG.debug("6. Created image %s from disk %s" %
                      (name, snapshot_disk_name))
            LOG.info("Created GCE image %s from instance %s" %
                     (name, instance.uuid))

            update_task_state(task_state=task_states.IMAGE_UPLOADING,
                              expected_state=task_states.IMAGE_PENDING_UPLOAD)
            gce_img_data = gceutils.get_image(compute, project, name)
            image_metadata = {
                'name': name,
                'container_format': 'bare',
                'disk_format': 'raw',
                'is_public': False,
                'status': 'active',
                'properties': {
                    'image_state': 'available',
                    'owner_id': instance.project_id,
                    'ramdisk_id': instance.ramdisk_id,
                    'location': 'gce://%s/%s/%s' % (project, name, image_id),
                    'gce_image_id': gce_img_data['id'],
                    'gce_link': gce_img_data['selfLink'],
                    'gce_size': gce_img_data['diskSizeGb']
                },
            }
            image_api.update(context, image_id, image_metadata)
            LOG.debug("7. Added image to glance %s" % name)

            disk_operation = gceutils.delete_disk(compute, project, zone,
                                                  snapshot_disk_name)
            snap_operation = gceutils.delete_snapshot(compute, project,
                                                      snapshot_name)
            gceutils.wait_for_operation(compute, project, disk_operation)
            temp_disk_from_snapshot = False
            LOG.debug("8. Delete temporary disk %s" % snapshot_disk_name)

            gceutils.wait_for_operation(compute, project, snap_operation)
            temp_disk_snapshot = False
            LOG.debug("9. Delete temporary disk snapshot %s" % snapshot_name)
            LOG.info("Completed snapshot for instance %s" % instance.uuid)

        except Exception as e:
            LOG.exception("An error occured during image creation: %s" % e)
            if instance_stopped:
                operation = gceutils.start_instance(compute, project, zone,
                                                    gce_id)
                gceutils.wait_for_operation(compute, project, operation)
                LOG.debug("Restart instance after disk snapshot %s" %
                          instance.uuid)
            if image_created:
                LOG.info("Rollback snapshot for instance %s, deleting image "
                         "%s from GCE" % (instance.uuid, name))
                operation = gceutils.delete_image(compute, project, name)
                gceutils.wait_for_operation(compute, project, operation)
            if temp_disk_from_snapshot:
                disk_operation = gceutils.delete_disk(compute, project, zone,
                                                      snapshot_disk_name)
                gceutils.wait_for_operation(compute, project, disk_operation)
                LOG.debug("Rollback snapshot for instace %s, delete temporary"
                          " disk %s" % (instance.uuid, snapshot_disk_name))
            if temp_disk_snapshot:
                snap_operation = gceutils.delete_snapshot(
                    compute, project, snapshot_name)
                gceutils.wait_for_operation(compute, project, snap_operation)
                LOG.debug("Rollback snapshot for instance %s, delete temporary"
                          " disk snapshot %s" % (instance.uuid, snapshot_name))
            raise e
Exemple #44
0
 def __init__(self):
     self.image_service = glance.get_default_image_service()
Exemple #45
0
import random

from nova import exception
from nova import volume
from nova.image import glance

from cinder import exception as cinder_ex

from occi import exceptions

# Connection to the nova APIs
from occi_os_api.nova_glue import vm

VOLUME_API = volume.API()

IMAGE_API = glance.get_default_image_service()


def create_storage(size, context, name=None, description=None):
    """
    Create a storage instance.

    size -- Size of the storage. ('occi.storage.size')
    context -- The os context.
    name -- defaults to a random number if needed.
    description -- defaults to the name
    """
    # L8R: A blueprint?
    # OpenStack deals with size in terms of integer.
    # Need to convert float to integer for now and only if the float
    # can be losslessly converted to integer
 def __init__(self, service=None, *args, **kwargs):
     self.cert_rpcapi = nova.cert.rpcapi.CertAPI()
     self.service = service or glance.get_default_image_service()
     self.service.__init__(*args, **kwargs)
Exemple #47
0
 def __init__(self):
     super(ComputeTaskManager, self).__init__()
     self.compute_rpcapi = compute_rpcapi.ComputeAPI()
     self.scheduler_rpcapi = scheduler_rpcapi.SchedulerAPI()
     self.image_service = glance.get_default_image_service()
     self.quotas = quota.QUOTAS
Exemple #48
0
    def _test_create_volume_backed_image_with_metadata_from_volume(
            self, extra_metadata=None):

        def _fake_id(x):
            return '%s-%s-%s-%s' % (x * 8, x * 4, x * 4, x * 12)

        body = dict(createImage=dict(name='snapshot_of_volume_backed'))
        if extra_metadata:
            body['createImage']['metadata'] = extra_metadata

        image_service = glance.get_default_image_service()

        def fake_block_device_mapping_get_all_by_instance(context, inst_id,
                                                          use_slave=False):
            return [fake_block_device.FakeDbBlockDeviceDict(
                        {'volume_id': _fake_id('a'),
                         'source_type': 'snapshot',
                         'destination_type': 'volume',
                         'volume_size': 1,
                         'device_name': 'vda',
                         'snapshot_id': 1,
                         'boot_index': 0,
                         'delete_on_termination': False,
                         'no_device': None})]

        self.stub_out('nova.db.api.block_device_mapping_get_all_by_instance',
                      fake_block_device_mapping_get_all_by_instance)

        instance = fakes.fake_compute_get(
            image_ref='',
            vm_state=vm_states.ACTIVE,
            root_device_name='/dev/vda',
            system_metadata={'image_test_key1': 'test_value1',
                             'image_test_key2': 'test_value2'})
        self.stub_out('nova.compute.api.API.get', instance)

        volume = dict(id=_fake_id('a'),
                      size=1,
                      host='fake',
                      display_description='fake')

        snapshot = dict(id=_fake_id('d'))

        with test.nested(
            mock.patch.object(
                self.controller.compute_api.volume_api, 'get_absolute_limits',
                return_value={'totalSnapshotsUsed': 0,
                              'maxTotalSnapshots': 10}),
            mock.patch.object(self.controller.compute_api.compute_rpcapi,
                'quiesce_instance',
                side_effect=exception.InstanceQuiesceNotSupported(
                    instance_id='fake', reason='test')),
            mock.patch.object(self.controller.compute_api.volume_api, 'get',
                              return_value=volume),
            mock.patch.object(self.controller.compute_api.volume_api,
                              'create_snapshot_force',
                              return_value=snapshot),
        ) as (mock_get_limits, mock_quiesce, mock_vol_get, mock_vol_create):

            response = self.controller._action_create_image(self.req,
                FAKE_UUID, body=body)
            location = response.headers['Location']
            image_id = location.replace(self.image_base_url, '')
            image = image_service.show(None, image_id)

            properties = image['properties']
            self.assertEqual(properties['test_key1'], 'test_value1')
            self.assertEqual(properties['test_key2'], 'test_value2')
            if extra_metadata:
                for key, val in extra_metadata.items():
                    self.assertEqual(properties[key], val)

            mock_quiesce.assert_called_once_with(mock.ANY, mock.ANY)
            mock_vol_get.assert_called_once_with(mock.ANY, volume['id'])
            mock_vol_create.assert_called_once_with(mock.ANY, volume['id'],
                                                    mock.ANY, mock.ANY)
    def _test_create_volume_backed_image_with_metadata_from_volume(
            self, extra_metadata=None):

        def _fake_id(x):
            return '%s-%s-%s-%s' % (x * 8, x * 4, x * 4, x * 12)

        body = dict(createImage=dict(name='snapshot_of_volume_backed'))
        if extra_metadata:
            body['createImage']['metadata'] = extra_metadata

        image_service = glance.get_default_image_service()

        def fake_block_device_mapping_get_all_by_instance(context, inst_id,
                                                          use_slave=False):
            return [fake_block_device.FakeDbBlockDeviceDict(
                        {'volume_id': _fake_id('a'),
                         'source_type': 'snapshot',
                         'destination_type': 'volume',
                         'volume_size': 1,
                         'device_name': 'vda',
                         'snapshot_id': 1,
                         'boot_index': 0,
                         'delete_on_termination': False,
                         'no_device': None})]

        self.stubs.Set(db, 'block_device_mapping_get_all_by_instance',
                       fake_block_device_mapping_get_all_by_instance)

        instance = fakes.fake_instance_get(image_ref='',
                                           vm_state=vm_states.ACTIVE,
                                           root_device_name='/dev/vda')
        self.stubs.Set(db, 'instance_get_by_uuid', instance)

        self.mox.StubOutWithMock(self.controller.compute_api.compute_rpcapi,
                                 'quiesce_instance')
        self.controller.compute_api.compute_rpcapi.quiesce_instance(
            mox.IgnoreArg(), mox.IgnoreArg()).AndRaise(
                exception.InstanceQuiesceNotSupported(instance_id='fake',
                                                      reason='test'))

        fake_metadata = {'test_key1': 'test_value1',
                         'test_key2': 'test_value2'}
        volume = dict(id=_fake_id('a'),
                      size=1,
                      host='fake',
                      display_description='fake',
                      volume_image_metadata=fake_metadata)
        snapshot = dict(id=_fake_id('d'))
        self.mox.StubOutWithMock(self.controller.compute_api, 'volume_api')
        volume_api = self.controller.compute_api.volume_api
        volume_api.get(mox.IgnoreArg(), volume['id']).AndReturn(volume)
        volume_api.get(mox.IgnoreArg(), volume['id']).AndReturn(volume)
        volume_api.create_snapshot_force(mox.IgnoreArg(), volume['id'],
               mox.IgnoreArg(), mox.IgnoreArg()).AndReturn(snapshot)

        self.mox.ReplayAll()
        response = self.controller._action_create_image(self.req, FAKE_UUID,
                                                        body=body)
        location = response.headers['Location']
        image_id = location.replace(self.image_base_url, '')
        image = image_service.show(None, image_id)

        properties = image['properties']
        self.assertEqual(properties['test_key1'], 'test_value1')
        self.assertEqual(properties['test_key2'], 'test_value2')
        if extra_metadata:
            for key, val in extra_metadata.items():
                self.assertEqual(properties[key], val)
Exemple #50
0
 def __init__(self):
     self.image_service = glance.get_default_image_service()
Exemple #51
0
 def __init__(self):
     self.host_manager = importutils.import_object(
         CONF.scheduler_host_manager)
     self.compute_rpcapi = compute_rpcapi.ComputeAPI()
     self.servicegroup_api = servicegroup.API()
     self.image_service = glance.get_default_image_service()
Exemple #52
0
 def __init__(self, image_service=None):
     self.image_service = image_service if image_service is not None \
                                     else glance.get_default_image_service()
Exemple #53
0
 def __init__(self, *args, **kwargs):
     super(API, self).__init__(*args, **kwargs)
     self._image_service = glance.get_default_image_service()
Exemple #54
0
 def __init__(self, service=None, *args, **kwargs):
     self.cert_rpcapi = nova.cert.rpcapi.CertAPI()
     self.service = service or glance.get_default_image_service()
     self.service.__init__(*args, **kwargs)
Exemple #55
0
    def pre_launch(self, context,
                   new_instance_ref,
                   network_info=None,
                   block_device_info=None,
                   migration=False,
                   skip_image_service=False,
                   image_refs=[]):

        image_base_path = None
        if not(skip_image_service) and CONF.gridcentric_use_image_service:
            # We need to first download the descriptor and the disk files
            # from the image service.
            LOG.debug("Downloading images %s from the image service." % (image_refs))
            image_base_path = os.path.join(CONF.instances_path, '_base')
            if not os.path.exists(image_base_path):
                LOG.debug('Base path %s does not exist. It will be created now.', image_base_path)
                mkdir_as(image_base_path, self.openstack_uid)
            image_service = glance.get_default_image_service()
            for image_ref in image_refs:
                image = image_service.show(context, image_ref)
                target = os.path.join(image_base_path, image['name'])
                if migration or not os.path.exists(target):
                    # If the path does not exist fetch the data from the image
                    # service.  NOTE: We always fetch in the case of a
                    # migration, as the descriptor may have changed from its
                    # previous state. Migrating VMs are the only case where a
                    # descriptor for an instance will not be a fixed constant.
                    # We download to a temporary location so we can make the
                    # file appear atomically from the right user.
                    fd, temp_target = tempfile.mkstemp(dir=image_base_path)
                    try:
                        os.close(fd)
                        images.fetch(context,
                                     image_ref,
                                     temp_target,
                                     new_instance_ref['user_id'],
                                     new_instance_ref['project_id'])
                        os.chown(temp_target, self.openstack_uid, self.openstack_gid)
                        os.chmod(temp_target, 0644)
                        os.rename(temp_target, target)
                    except:
                        os.unlink(temp_target)
                        raise

        # (dscannell) Check to see if we need to convert the network_info
        # object into the legacy format.
        if network_info and self.libvirt_conn.legacy_nwinfo():
            network_info = network_info.legacy()

        # We need to create the libvirt xml, and associated files. Pass back
        # the path to the libvirt.xml file.
        working_dir = os.path.join(CONF.instances_path, new_instance_ref['name'])
        disk_file = os.path.join(working_dir, "disk")
        libvirt_file = os.path.join(working_dir, "libvirt.xml")

        # Make sure that our working directory exists.
        mkdir_as(working_dir, self.openstack_uid)

        if not(os.path.exists(disk_file)):
            # (dscannell) We will write out a stub 'disk' file so that we don't
            # end up copying this file when setting up everything for libvirt.
            # Essentially, this file will be removed, and replaced by vms as an
            # overlay on the blessed root image.
            touch_as(disk_file, self.openstack_uid)

        # (dscannell) We want to disable any injection. We do this by making a
        # copy of the instance and clearing out some entries. Since OpenStack
        # uses dictionary-list accessors, we can pass this dictionary through
        # that code.
        instance_dict = AttribDictionary(dict(new_instance_ref.iteritems()))

        # The name attribute is special and does not carry over like the rest
        # of the attributes.
        instance_dict['name'] = new_instance_ref['name']
        instance_dict.os_type = new_instance_ref['os_type']
        instance_dict['key_data'] = None
        instance_dict['metadata'] = []
        for network_ref, mapping in network_info:
            network_ref['injected'] = False

        # (dscannell) This was taken from the core nova project as part of the
        # boot path for normal instances. We basically want to mimic this
        # functionality.
        xml = self.libvirt_conn.to_xml(instance_dict, network_info, False,
                                   block_device_info=block_device_info)
        self.libvirt_conn._create_image(context, instance_dict, xml, network_info=network_info,
                                    block_device_info=block_device_info)

        if not(migration):
            # (dscannell) Remove the fake disk file (if created).
            os.remove(disk_file)

        # Fix up the permissions on the files that we created so that they are owned by the
        # openstack user.
        for root, dirs, files in os.walk(working_dir, followlinks=True):
            for path in dirs + files:
                LOG.debug("chowning path=%s to openstack user %s" % \
                         (os.path.join(root, path), self.openstack_uid))
                os.chown(os.path.join(root, path), self.openstack_uid, self.openstack_gid)

        # Return the libvirt file, this will be passed in as the name. This
        # parameter is overloaded in the management interface as a libvirt
        # special case.
        return (libvirt_file, image_base_path)