示例#1
0
    def _copy_image_to_volume(self, context, volume_ref,
                              image_id, image_location, image_service):
        """Downloads Glance image to the specified volume."""
        copy_image_to_volume = self.driver.copy_image_to_volume
        volume_id = volume_ref['id']
        LOG.debug("Attempting download of %(image_id)s (%(image_location)s)"
                  " to volume %(volume_id)s.",
                  {'image_id': image_id, 'volume_id': volume_id,
                   'image_location': image_location})
        try:
            copy_image_to_volume(context, volume_ref, image_service, image_id)
        except processutils.ProcessExecutionError as ex:
            LOG.exception(_LE("Failed to copy image %(image_id)s to volume: "
                              "%(volume_id)s"),
                          {'volume_id': volume_id, 'image_id': image_id})
            raise exception.ImageCopyFailure(reason=ex.stderr)
        except exception.ImageUnacceptable as ex:
            LOG.exception(_LE("Failed to copy image to volume: %(volume_id)s"),
                          {'volume_id': volume_id})
            raise exception.ImageUnacceptable(ex)
        except Exception as ex:
            LOG.exception(_LE("Failed to copy image %(image_id)s to "
                              "volume: %(volume_id)s"),
                          {'volume_id': volume_id, 'image_id': image_id})
            if not isinstance(ex, exception.ImageCopyFailure):
                raise exception.ImageCopyFailure(reason=ex)
            else:
                raise

        LOG.debug("Downloaded image %(image_id)s (%(image_location)s)"
                  " to volume %(volume_id)s successfully.",
                  {'image_id': image_id, 'volume_id': volume_id,
                   'image_location': image_location})
示例#2
0
    def _copy_image_to_volume(self, context, volume, image_service, image_id):
        """Downloads Glance image to the specified volume."""
        volume_id = volume['id']
        try:
            self.driver.copy_image_to_volume(context, volume, image_service,
                                             image_id)
        except exception.ProcessExecutionError as ex:
            LOG.error(
                _("Failed to copy image to volume: %(volume_id)s, "
                  "error: %(error)s") % {
                      'volume_id': volume_id,
                      'error': ex.stderr
                  })
            raise exception.ImageCopyFailure(reason=ex.stderr)
        except Exception as ex:
            LOG.error(
                _("Failed to copy image to volume: %(volume_id)s, "
                  "error: %(error)s") % {
                      'volume_id': volume_id,
                      'error': ex
                  })
            raise exception.ImageCopyFailure(reason=ex)

        LOG.info(
            _("Downloaded image %(image_id)s to %(volume_id)s "
              "successfully.") % {
                  'image_id': image_id,
                  'volume_id': volume_id
              })
示例#3
0
    def _copy_image_to_volume(self, context, volume,
                              image_meta, image_location, image_service):

        image_id = image_meta['id']
        """Downloads Glance image to the specified volume."""
        LOG.debug("Attempting download of %(image_id)s (%(image_location)s)"
                  " to volume %(volume_id)s.",
                  {'image_id': image_id, 'volume_id': volume.id,
                   'image_location': image_location})
        try:
            image_properties = image_meta.get('properties', {})
            image_encryption_key = image_properties.get(
                'cinder_encryption_key_id')

            if volume.encryption_key_id and image_encryption_key:
                # If the image provided an encryption key, we have
                # already cloned it to the volume's key in
                # _get_encryption_key_id, so we can do a direct copy.
                self.driver.copy_image_to_volume(
                    context, volume, image_service, image_id)
            elif volume.encryption_key_id:
                # Creating an encrypted volume from a normal, unencrypted,
                # image.
                self.driver.copy_image_to_encrypted_volume(
                    context, volume, image_service, image_id)
            else:
                self.driver.copy_image_to_volume(
                    context, volume, image_service, image_id)
        except processutils.ProcessExecutionError as ex:
            LOG.exception("Failed to copy image %(image_id)s to volume: "
                          "%(volume_id)s",
                          {'volume_id': volume.id, 'image_id': image_id})
            raise exception.ImageCopyFailure(reason=ex.stderr)
        except exception.ImageUnacceptable as ex:
            LOG.exception("Failed to copy image to volume: %(volume_id)s",
                          {'volume_id': volume.id})
            raise exception.ImageUnacceptable(ex)
        except exception.ImageTooBig as ex:
            LOG.exception("Failed to copy image %(image_id)s to volume: "
                          "%(volume_id)s",
                          {'volume_id': volume.id, 'image_id': image_id})
            excutils.save_and_reraise_exception()
        except Exception as ex:
            LOG.exception("Failed to copy image %(image_id)s to "
                          "volume: %(volume_id)s",
                          {'volume_id': volume.id, 'image_id': image_id})
            if not isinstance(ex, exception.ImageCopyFailure):
                raise exception.ImageCopyFailure(reason=ex)
            else:
                raise

        LOG.debug("Downloaded image %(image_id)s (%(image_location)s)"
                  " to volume %(volume_id)s successfully.",
                  {'image_id': image_id, 'volume_id': volume.id,
                   'image_location': image_location})
示例#4
0
    def _use_glance_plugin_to_copy_image_to_volume(self, context, volume,
                                                   image_service, image_id):
        sr_uuid, vdi_uuid = volume['provider_location'].split('/')

        api_servers = glance.get_api_servers()
        glance_server = api_servers.next()
        auth_token = context.auth_token

        overwrite_result = self.nfs_ops.use_glance_plugin_to_overwrite_volume(
            CONF.xenapi_nfs_server,
            CONF.xenapi_nfs_serverpath,
            sr_uuid,
            vdi_uuid,
            glance_server,
            image_id,
            auth_token,
            CONF.xenapi_sr_base_path)

        if overwrite_result is False:
            raise exception.ImageCopyFailure(reason='Overwriting volume '
                                                    'failed.')

        self.nfs_ops.resize_volume(
            CONF.xenapi_nfs_server,
            CONF.xenapi_nfs_serverpath,
            sr_uuid,
            vdi_uuid,
            volume['size'])
示例#5
0
    def test_volume_reimage_raise_exception(self):
        volume = tests_utils.create_volume(self.context)
        self.volume.create_volume(self.context, volume)

        with mock.patch.object(self.volume.driver, 'copy_image_to_volume'
                               ) as mock_cp_img:
            mock_cp_img.side_effect = processutils.ProcessExecutionError
            self.assertRaises(exception.ImageCopyFailure, self.volume.reimage,
                              self.context, volume, self.image_meta)
            self.assertEqual(volume.previous_status, 'available')
            self.assertEqual(volume.status, 'error')

            mock_cp_img.side_effect = exception.ImageUnacceptable(
                image_id=self.image_meta['id'], reason='')
            self.assertRaises(exception.ImageUnacceptable, self.volume.reimage,
                              self.context, volume, self.image_meta)

            mock_cp_img.side_effect = exception.ImageConversionNotAllowed(
                image_id=self.image_meta['id'], reason='')

            with mock.patch.object(
                self.volume.message_api, 'create'
            ) as mock_msg_create:
                self.assertRaises(
                    exception.ImageConversionNotAllowed, self.volume.reimage,
                    self.context, volume, self.image_meta)
                mock_msg_create.assert_called_with(
                    self.context,
                    message_field.Action.REIMAGE_VOLUME,
                    resource_uuid=volume.id,
                    detail=message_field.Detail.IMAGE_FORMAT_UNACCEPTABLE)

            mock_cp_img.side_effect = exception.ImageTooBig(
                image_id=self.image_meta['id'], reason='')
            self.assertRaises(exception.ImageTooBig, self.volume.reimage,
                              self.context, volume, self.image_meta)

            mock_cp_img.side_effect = Exception
            self.assertRaises(exception.ImageCopyFailure, self.volume.reimage,
                              self.context, volume, self.image_meta)

            mock_cp_img.side_effect = exception.ImageCopyFailure(reason='')
            self.assertRaises(exception.ImageCopyFailure, self.volume.reimage,
                              self.context, volume, self.image_meta)
示例#6
0
 def fake_copy_image_to_volume(context, volume, image_service,
                               image_id):
     raise exception.ImageCopyFailure()