Example #1
0
    def create(self, context, metadata, data=None):
        """Store the image data and return the new image id.

        :raises: Duplicate if the image already exist.

        """
        image_id = str(metadata.get('id', uuidutils.generate_uuid()))
        metadata['id'] = image_id
        if image_id in self.images:
            raise exception.CouldNotUploadImage(image_id=image_id)
        image_meta = copy.deepcopy(metadata)
        # Glance sets the size value when an image is created, so we
        # need to do that here to fake things out if it's not provided
        # by the caller. This is needed to avoid a KeyError in the
        # image-size API.
        if 'size' not in image_meta:
            image_meta['size'] = None
        # Similarly, Glance provides the status on the image once it's created
        # and this is checked in the compute API when booting a server from
        # this image, so we just fake it out to be 'active' even though this
        # is mostly a lie on a newly created image.
        if 'status' not in metadata:
            image_meta['status'] = 'active'
        # The owner of the image is by default the request context project_id.
        if context and 'owner' not in image_meta.get('properties', {}):
            # Note that normally "owner" is a top-level field in an image
            # resource in glance but we have to fake this out for the images
            # proxy API by throwing it into the generic "properties" dict.
            image_meta.get('properties', {})['owner'] = context.project_id
        self.images[image_id] = image_meta
        if data:
            self._imagedata[image_id] = data.read()
        return self.images[image_id]
Example #2
0
    def create(self, context, metadata, data=None):
        """Store the image data and return the new image id.

        :raises: Duplicate if the image already exist.

        """
        image_id = str(metadata.get('id', uuidutils.generate_uuid()))
        metadata['id'] = image_id
        if image_id in self.images:
            raise exception.CouldNotUploadImage(image_id=image_id)
        image_meta = copy.deepcopy(metadata)
        # Glance sets the size value when an image is created, so we
        # need to do that here to fake things out if it's not provided
        # by the caller. This is needed to avoid a KeyError in the
        # image-size API.
        if 'size' not in image_meta:
            image_meta['size'] = None
        # Similarly, Glance provides the status on the image once it's created
        # and this is checked in the compute API when booting a server from
        # this image, so we just fake it out to be 'active' even though this
        # is mostly a lie on a newly created image.
        if 'status' not in metadata:
            image_meta['status'] = 'active'
        self.images[image_id] = image_meta
        if data:
            self._imagedata[image_id] = data.read()
        return self.images[image_id]
Example #3
0
File: glance.py Project: yuans/nova
    def upload_image(self, context, session, instance, vdi_uuids, image_id):
        """Requests that the Glance plugin bundle the specified VDIs and
        push them into Glance using the specified human-friendly name.
        """
        # NOTE(sirp): Currently we only support uploading images as VHD, there
        # is no RAW equivalent (yet)
        max_attempts = CONF.glance_num_retries + 1
        sleep_time = 0.5
        glance_api_servers = glance.get_api_servers()
        properties = {
            'auto_disk_config': instance['auto_disk_config'],
            'os_type': instance['os_type'] or CONF.default_os_type,
        }

        if agent.USE_AGENT_SM_KEY in instance["system_metadata"]:
            properties[agent.USE_AGENT_KEY] = \
                instance["system_metadata"][agent.USE_AGENT_SM_KEY]

        for attempt_num in xrange(1, max_attempts + 1):

            (glance_host, glance_port,
             glance_use_ssl) = glance_api_servers.next()

            try:

                params = {
                    'vdi_uuids': vdi_uuids,
                    'image_id': image_id,
                    'glance_host': glance_host,
                    'glance_port': glance_port,
                    'glance_use_ssl': glance_use_ssl,
                    'sr_path': vm_utils.get_sr_path(session),
                    'auth_token': getattr(context, 'auth_token', None),
                    'properties': properties
                }

                LOG.debug(_("Asking xapi to upload to glance %(vdi_uuids)s as"
                            " ID %(image_id)s"
                            " glance server: %(glance_host)s:%(glance_port)d"
                            " attempt %(attempt_num)d/%(max_attempts)d"),
                          locals(),
                          instance=instance)

                return session.call_plugin_serialized('glance', 'upload_vhd',
                                                      **params)

            except session.XenAPI.Failure as exc:
                _type, _method, error = exc.details[:3]
                if error == 'RetryableError':
                    LOG.error(_('upload_vhd failed: %r') % (exc.details[3:], ))
                else:
                    raise
            time.sleep(sleep_time)
            sleep_time = min(2 * sleep_time, 15)

        raise exception.CouldNotUploadImage(image_id=image_id)
Example #4
0
    def upload_image(self, context, session, instance, vdi_uuids, image_id):
        params = self._make_params(context, session, image_id)
        params['vdi_uuids'] = vdi_uuids

        props = params['properties'] = {}
        props['auto_disk_config'] = instance['auto_disk_config']
        props['os_type'] = instance['os_type'] or CONF.default_os_type

        try:
            self._call_glance_plugin(session, 'upload_vhd', params)
        except exception.PluginRetriesExceeded:
            raise exception.CouldNotUploadImage(image_id=image_id)
Example #5
0
    def create(self, context, metadata, data=None):
        """Store the image data and return the new image id.

        :raises: Duplicate if the image already exist.

        """
        image_id = str(metadata.get('id', uuid.uuid4()))
        metadata['id'] = image_id
        if image_id in self.images:
            raise exception.CouldNotUploadImage(image_id=image_id)
        self.images[image_id] = copy.deepcopy(metadata)
        if data:
            self._imagedata[image_id] = data.read()
        return self.images[image_id]
 def upload_image(self, context, session, instance, image_id, vdi_uuids):
     try:
         host_url = CONF.xenserver.connection_url
         level = vm_utils.get_compression_level()
         metadata = self._get_metadata(context, instance, image_id)
         image_chunks = xenapi_image.stream_from_vdis(context,
                                                      session,
                                                      instance,
                                                      host_url,
                                                      vdi_uuids,
                                                      compresslevel=level)
         image_stream = utils.IterableToFileAdapter(image_chunks)
         IMAGE_API.update(context, image_id, metadata, data=image_stream)
     except xenapi_exception.OsXenApiException as e:
         LOG.error("Image upload failed with exception: %s", e)
         raise exception.CouldNotUploadImage(image_id=image_id)
Example #7
0
    def upload_image(self, context, session, instance, vdi_uuids, image_id):
        params = self._make_params(context, session, image_id)
        params['vdi_uuids'] = vdi_uuids

        props = params['properties'] = {}
        props['auto_disk_config'] = instance['auto_disk_config']
        props['os_type'] = instance['os_type'] or CONF.default_os_type

        sys_meta = instance["system_metadata"]
        if agent.USE_AGENT_SM_KEY in sys_meta:
            props[agent.USE_AGENT_KEY] = sys_meta[agent.USE_AGENT_SM_KEY]

        try:
            self._call_glance_plugin(session, 'upload_vhd', params)
        except exception.PluginRetriesExceeded:
            raise exception.CouldNotUploadImage(image_id=image_id)
Example #8
0
    def upload_image(self, context, session, instance, vdi_uuids, image_id):
        params = self._make_params(context, session, image_id)
        params['vdi_uuids'] = vdi_uuids

        props = params['properties'] = {}
        props['auto_disk_config'] = instance['auto_disk_config']
        props['os_type'] = instance['os_type'] or CONF.default_os_type

        compression_level = vm_utils.get_compression_level()
        if compression_level:
            props['xenapi_image_compression_level'] = compression_level

        auto_disk_config = utils.get_auto_disk_config_from_instance(instance)
        if utils.is_auto_disk_config_disabled(auto_disk_config):
            props["auto_disk_config"] = "disabled"

        try:
            self._call_glance_plugin(session, 'upload_vhd', params)
        except exception.PluginRetriesExceeded:
            raise exception.CouldNotUploadImage(image_id=image_id)
Example #9
0
    def upload_image(self, context, session, instance, image_id, vdi_uuids):
        params = {'vdi_uuids': vdi_uuids}
        props = params['properties'] = {}
        props['auto_disk_config'] = instance['auto_disk_config']
        props['os_type'] = instance.get(
            'os_type', None) or (CONF.xenserver.default_os_type)

        compression_level = vm_utils.get_compression_level()
        if compression_level:
            props['xenapi_image_compression_level'] = compression_level

        auto_disk_config = utils.get_auto_disk_config_from_instance(instance)
        if utils.is_auto_disk_config_disabled(auto_disk_config):
            props["auto_disk_config"] = "disabled"

        try:
            self._call_glance_plugin(context, instance, session,
                                     host_glance.upload_vhd, image_id, params)
        except xenapi_exception.PluginRetriesExceeded:
            raise exception.CouldNotUploadImage(image_id=image_id)