Ejemplo n.º 1
0
def upload_volume(context, image_service, image_meta, volume_path):
    image_id = image_meta['id']
    if (image_meta['disk_format'] == 'raw'):
        LOG.debug("%s was raw, no need to convert to %s" %
                  (image_id, image_meta['disk_format']))
        with utils.temporary_chown(volume_path):
            with utils.file_open(volume_path) as image_file:
                image_service.update(context, image_id, {}, image_file)
        return

    if (FLAGS.image_conversion_dir and not
            os.path.exists(FLAGS.image_conversion_dir)):
        os.makedirs(FLAGS.image_conversion_dir)

    fd, tmp = tempfile.mkstemp(dir=FLAGS.image_conversion_dir)
    os.close(fd)
    with utils.remove_path_on_error(tmp):
        LOG.debug("%s was raw, converting to %s" %
                  (image_id, image_meta['disk_format']))
        convert_image(volume_path, tmp, image_meta['disk_format'])

        data = qemu_img_info(tmp)
        if data.file_format != image_meta['disk_format']:
            raise exception.ImageUnacceptable(
                image_id=image_id,
                reason=_("Converted to %(f1)s, but format is now %(f2)s") %
                {'f1': image_meta['disk_format'], 'f2': data.file_format})

        with utils.file_open(tmp) as image_file:
            image_service.update(context, image_id, {}, image_file)
        os.unlink(tmp)
Ejemplo n.º 2
0
def upload_volume(context, image_service, image_meta, volume_path):
    image_id = image_meta['id']
    if (image_meta['disk_format'] == 'raw'):
        LOG.debug("%s was raw, no need to convert to %s" %
                  (image_id, image_meta['disk_format']))
        with utils.temporary_chown(volume_path):
            with utils.file_open(volume_path) as image_file:
                image_service.update(context, image_id, {}, image_file)
        return

    if (FLAGS.image_conversion_dir and not
            os.path.exists(FLAGS.image_conversion_dir)):
        os.makedirs(FLAGS.image_conversion_dir)

    fd, tmp = tempfile.mkstemp(dir=FLAGS.image_conversion_dir)
    os.close(fd)
    with utils.remove_path_on_error(tmp):
        LOG.debug("%s was raw, converting to %s" %
                  (image_id, image_meta['disk_format']))
        convert_image(volume_path, tmp, image_meta['disk_format'])

        data = qemu_img_info(tmp)
        if data.file_format != image_meta['disk_format']:
            raise exception.ImageUnacceptable(
                image_id=image_id,
                reason=_("Converted to %(f1)s, but format is now %(f2)s") %
                {'f1': image_meta['disk_format'], 'f2': data.file_format})

        with utils.file_open(tmp) as image_file:
            image_service.update(context, image_id, {}, image_file)
        os.unlink(tmp)
Ejemplo n.º 3
0
 def backup_volume(self, context, backup, backup_service):
     """Create a new backup from an existing volume."""
     volume = self.db.volume_get(context, backup['volume_id'])
     volume_path = self.local_path(volume)
     with utils.temporary_chown(volume_path):
         with utils.file_open(volume_path) as volume_file:
             backup_service.backup(backup, volume_file)
Ejemplo n.º 4
0
 def backup_volume(self, context, backup, backup_service):
     """Create a new backup from an existing volume."""
     volume = self.db.volume_get(context, backup["volume_id"])
     volume_path = self.local_path(volume)
     with utils.temporary_chown(volume_path):
         with utils.file_open(volume_path) as volume_file:
             backup_service.backup(backup, volume_file)
Ejemplo n.º 5
0
    def copy_image_to_volume(self, context, volume, image_service, image_id):
        """Fetch the image from image_service and write it to the volume."""
        LOG.debug(_('copy_image_to_volume %s.') % volume['name'])
        initiator = get_iscsi_initiator()
        connector = {}
        connector['initiator'] = initiator

        iscsi_properties, volume_path = self._attach_volume(
            context, volume, connector)

        with utils.temporary_chown(volume_path):
            with utils.file_open(volume_path, "wb") as image_file:
                image_service.download(context, image_id, image_file)

        self.terminate_connection(volume, connector)
Ejemplo n.º 6
0
    def copy_volume_to_image(self, context, volume, image_service, image_meta):
        """Copy the volume to the specified image."""
        LOG.debug(_('copy_volume_to_image %s.') % volume['name'])
        initiator = get_iscsi_initiator()
        connector = {}
        connector['initiator'] = initiator

        iscsi_properties, volume_path = self._attach_volume(
            context, volume, connector)

        with utils.temporary_chown(volume_path):
            with utils.file_open(volume_path) as volume_file:
                image_service.update(context, image_meta['id'], {},
                                     volume_file)

        self.terminate_connection(volume, connector)
Ejemplo n.º 7
0
 def copy_volume_to_image(self, context, volume, image_service, image_id):
     """Copy the volume to the specified image."""
     volume_path = self.local_path(volume)
     with utils.temporary_chown(volume_path):
         with utils.file_open(volume_path) as volume_file:
             image_service.update(context, image_id, {}, volume_file)
Ejemplo n.º 8
0
 def copy_image_to_volume(self, context, volume, image_service, image_id):
     """Fetch the image from image_service and write it to the volume."""
     volume_path = self.local_path(volume)
     with utils.temporary_chown(volume_path):
         with utils.file_open(volume_path, "wb") as image_file:
             image_service.download(context, image_id, image_file)
Ejemplo n.º 9
0
 def copy_volume_to_image(self, context, volume, image_service, image_id):
     """Copy the volume to the specified image."""
     volume_path = self.local_path(volume)
     with utils.temporary_chown(volume_path):
         with utils.file_open(volume_path) as volume_file:
             image_service.update(context, image_id, {}, volume_file)
Ejemplo n.º 10
0
 def copy_image_to_volume(self, context, volume, image_service, image_id):
     """Fetch the image from image_service and write it to the volume."""
     volume_path = self.local_path(volume)
     with utils.temporary_chown(volume_path):
         with utils.file_open(volume_path, "wb") as image_file:
             image_service.download(context, image_id, image_file)
Ejemplo n.º 11
0
 def restore_backup(self, context, backup, volume, backup_service):
     """Restore an existing backup to a new or existing volume."""
     volume_path = self.local_path(volume)
     with utils.temporary_chown(volume_path):
         with utils.file_open(volume_path, 'wb') as volume_file:
             backup_service.restore(backup, volume['id'], volume_file)
Ejemplo n.º 12
0
 def restore_backup(self, context, backup, volume, backup_service):
     """Restore an existing backup to a new or existing volume."""
     volume_path = self.local_path(volume)
     with utils.temporary_chown(volume_path):
         with utils.file_open(volume_path, "wb") as volume_file:
             backup_service.restore(backup, volume["id"], volume_file)