Esempio 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 fileutils.file_open(volume_path) as image_file:
                image_service.update(context, image_id, {}, image_file)
        return

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

    fd, tmp = tempfile.mkstemp(dir=CONF.image_conversion_dir)
    os.close(fd)
    with fileutils.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 fileutils.file_open(tmp) as image_file:
            image_service.update(context, image_id, {}, image_file)
        os.unlink(tmp)
Esempio n. 2
0
    def test_backup_volume(self):
        self.mox.StubOutWithMock(self._driver, 'db')
        self.mox.StubOutWithMock(self._driver.db, 'volume_get')

        volume = {'id': '2', 'name': self.TEST_VOLNAME}
        self._driver.db.volume_get(context, volume['id']).AndReturn(volume)

        info = imageutils.QemuImgInfo()
        info.file_format = 'raw'
        self.mox.StubOutWithMock(image_utils, 'qemu_img_info')
        image_utils.qemu_img_info(self.TEST_VOLPATH).AndReturn(info)

        self.mox.StubOutWithMock(utils, 'temporary_chown')
        mock_tempchown = self.mox.CreateMockAnything()
        utils.temporary_chown(self.TEST_VOLPATH).AndReturn(mock_tempchown)
        mock_tempchown.__enter__()
        mock_tempchown.__exit__(None, None, None)

        self.mox.StubOutWithMock(fileutils, 'file_open')
        mock_fileopen = self.mox.CreateMockAnything()
        fileutils.file_open(self.TEST_VOLPATH).AndReturn(mock_fileopen)
        mock_fileopen.__enter__()
        mock_fileopen.__exit__(None, None, None)

        backup = {'volume_id': volume['id']}
        mock_servicebackup = self.mox.CreateMockAnything()
        mock_servicebackup.backup(backup, mox_lib.IgnoreArg())

        self.mox.ReplayAll()
        self._driver.backup_volume(context, backup, mock_servicebackup)
        self.mox.VerifyAll()
Esempio n. 3
0
def upload_volume(context, image_service, image_meta, volume_path,
                  volume_format='raw', run_as_root=True):
    image_id = image_meta['id']
    if (image_meta['disk_format'] == volume_format):
        LOG.debug("%s was %s, no need to convert to %s",
                  image_id, volume_format, image_meta['disk_format'])
        if os.name == 'nt' or os.access(volume_path, os.R_OK):
            with fileutils.file_open(volume_path, 'rb') as image_file:
                image_service.update(context, image_id, {}, image_file)
        else:
            with utils.temporary_chown(volume_path):
                with fileutils.file_open(volume_path) as image_file:
                    image_service.update(context, image_id, {}, image_file)
        return

    with temporary_file() as tmp:
        LOG.debug("%s was %s, converting to %s",
                  image_id, volume_format, image_meta['disk_format'])
        convert_image(volume_path, tmp, image_meta['disk_format'],
                      run_as_root=run_as_root)

        data = qemu_img_info(tmp, run_as_root=run_as_root)
        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 fileutils.file_open(tmp, 'rb') as image_file:
            image_service.update(context, image_id, {}, image_file)
Esempio n. 4
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 fileutils.file_open(volume_path) as image_file:
                image_service.update(context, image_id, {}, image_file)
        return

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

    fd, tmp = tempfile.mkstemp(dir=CONF.image_conversion_dir)
    os.close(fd)
    with fileutils.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 fileutils.file_open(tmp) as image_file:
            image_service.update(context, image_id, {}, image_file)
        os.unlink(tmp)
Esempio n. 5
0
def upload_volume(context, image_service, image_meta, volume_path, volume_format="raw"):
    image_id = image_meta["id"]
    if image_meta["disk_format"] == volume_format:
        LOG.debug("%s was %s, no need to convert to %s" % (image_id, volume_format, image_meta["disk_format"]))
        if os.name == "nt":
            with fileutils.file_open(volume_path) as image_file:
                image_service.update(context, image_id, {}, image_file)
        with utils.temporary_chown(volume_path):
            with fileutils.file_open(volume_path) as image_file:
                image_service.update(context, image_id, {}, image_file)
        return

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

    fd, tmp = tempfile.mkstemp(dir=CONF.image_conversion_dir)
    os.close(fd)
    with fileutils.remove_path_on_error(tmp):
        LOG.debug("%s was %s, converting to %s" % (image_id, volume_format, 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 fileutils.file_open(tmp) as image_file:
            image_service.update(context, image_id, {}, image_file)
        fileutils.delete_if_exists(tmp)
Esempio n. 6
0
    def test_backup_volume(self):
        self.mox.StubOutWithMock(self._driver, "db")
        self.mox.StubOutWithMock(self._driver.db, "volume_get")

        volume = {"id": "2", "name": self.TEST_VOLNAME}
        self._driver.db.volume_get(context, volume["id"]).AndReturn(volume)

        info = imageutils.QemuImgInfo()
        info.file_format = "raw"
        self.mox.StubOutWithMock(image_utils, "qemu_img_info")
        image_utils.qemu_img_info(self.TEST_VOLPATH).AndReturn(info)

        self.mox.StubOutWithMock(utils, "temporary_chown")
        mock_tempchown = self.mox.CreateMockAnything()
        utils.temporary_chown(self.TEST_VOLPATH).AndReturn(mock_tempchown)
        mock_tempchown.__enter__()
        mock_tempchown.__exit__(None, None, None)

        self.mox.StubOutWithMock(fileutils, "file_open")
        mock_fileopen = self.mox.CreateMockAnything()
        fileutils.file_open(self.TEST_VOLPATH).AndReturn(mock_fileopen)
        mock_fileopen.__enter__()
        mock_fileopen.__exit__(None, None, None)

        backup = {"volume_id": volume["id"]}
        mock_servicebackup = self.mox.CreateMockAnything()
        mock_servicebackup.backup(backup, mox_lib.IgnoreArg())

        self.mox.ReplayAll()
        self._driver.backup_volume(context, backup, mock_servicebackup)
        self.mox.VerifyAll()
Esempio n. 7
0
    def _backup_device(self, context, backup, backup_service, device):
        """Create a new backup from a volume."""

        LOG.debug('Creating a new backup for %s.', device['name'])
        use_multipath = self.configuration.use_multipath_for_image_xfer
        enforce_multipath = self.configuration.enforce_multipath_for_image_xfer
        properties = utils.brick_get_connector_properties(
            use_multipath, enforce_multipath)
        attach_info, device = self._attach_volume(context, device, properties)

        try:
            volume_path = attach_info['device']['path']

            # Secure network file systems will not chown files.
            if self.secure_file_operations_enabled():
                with fileutils.file_open(volume_path) as volume_file:
                    backup_info = backup_service.backup(backup, volume_file)
            else:
                with utils.temporary_chown(volume_path):
                    with fileutils.file_open(volume_path) as volume_file:
                        backup_info = backup_service.backup(
                            backup, volume_file)

        finally:
            self._detach_volume(context, attach_info, device, properties)
        return backup_info
Esempio n. 8
0
    def test_backup_volume(self):
        self.mox = mox_lib.Mox()
        self._driver.db = self.mox.CreateMockAnything()
        self.mox.StubOutWithMock(self._driver.db, 'volume_get')

        volume = {'id': '2', 'name': self.TEST_VOLNAME}
        self._driver.db.volume_get(context, volume['id']).AndReturn(volume)

        info = imageutils.QemuImgInfo()
        info.file_format = 'raw'
        self.mox.StubOutWithMock(image_utils, 'qemu_img_info')
        image_utils.qemu_img_info(self.TEST_VOLPATH).AndReturn(info)

        self.mox.StubOutWithMock(utils, 'temporary_chown')
        mock_tempchown = mock.MagicMock()
        utils.temporary_chown(self.TEST_VOLPATH).AndReturn(mock_tempchown)

        self.mox.StubOutWithMock(fileutils, 'file_open')
        mock_fileopen = mock.MagicMock()
        fileutils.file_open(self.TEST_VOLPATH).AndReturn(mock_fileopen)

        backup = {'volume_id': volume['id']}
        mock_servicebackup = self.mox.CreateMockAnything()
        mock_servicebackup.backup(backup, mox_lib.IgnoreArg())

        self.mox.ReplayAll()

        self._driver.backup_volume(context, backup, mock_servicebackup)
Esempio n. 9
0
def upload_volume(context,
                  image_service,
                  image_meta,
                  volume_path,
                  volume_format='raw',
                  run_as_root=True):
    image_id = image_meta['id']
    if (image_meta['disk_format'] == volume_format):
        LOG.debug("%s was %s, no need to convert to %s", image_id,
                  volume_format, image_meta['disk_format'])
        if os.name == 'nt' or os.access(volume_path, os.R_OK):
            with fileutils.file_open(volume_path, 'rb') as image_file:
                image_service.update(context, image_id, {}, image_file)
        else:
            with utils.temporary_chown(volume_path):
                with fileutils.file_open(volume_path) as image_file:
                    image_service.update(context, image_id, {}, image_file)
        return

    with temporary_file() as tmp:
        LOG.debug("%s was %s, converting to %s", image_id, volume_format,
                  image_meta['disk_format'])

        data = qemu_img_info(volume_path, run_as_root=run_as_root)
        backing_file = data.backing_file
        fmt = data.file_format
        if backing_file is not None:
            # Disallow backing files as a security measure.
            # This prevents a user from writing an image header into a raw
            # volume with a backing file pointing to data they wish to
            # access.
            raise exception.ImageUnacceptable(
                image_id=image_id,
                reason=_("fmt=%(fmt)s backed by:%(backing_file)s") % {
                    'fmt': fmt,
                    'backing_file': backing_file
                })

        convert_image(volume_path,
                      tmp,
                      image_meta['disk_format'],
                      data.virtual_size,
                      run_as_root=run_as_root)

        data = qemu_img_info(tmp, run_as_root=run_as_root)
        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 fileutils.file_open(tmp, 'rb') as image_file:
            image_service.update(context, image_id, {}, image_file)
Esempio n. 10
0
def upload_volume(context, image_service, image_meta, volume_path,
                  volume_format='raw'):
    image_id = image_meta['id']
    if (image_meta['disk_format'] == volume_format):
        LOG.debug("%s was %s, no need to convert to %s" %
                  (image_id, volume_format, image_meta['disk_format']))
        if os.name == 'nt' or os.access(volume_path, os.R_OK):
            with fileutils.file_open(volume_path, 'rb') as image_file:
                image_service.update(context, image_id, {}, image_file)
        else:
            with utils.temporary_chown(volume_path):
                with fileutils.file_open(volume_path) as image_file:
                    image_service.update(context, image_id, {}, image_file)
        return

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

    fd, tmp = tempfile.mkstemp(dir=CONF.image_conversion_dir)
    os.close(fd)
    with fileutils.remove_path_on_error(tmp):
        LOG.debug("%s was %s, converting to %s" %
                  (image_id, volume_format, image_meta['disk_format']))

        data = qemu_img_info(volume_path)
        backing_file = data.backing_file
        fmt = data.file_format
        if backing_file is not None:
            # Disallow backing files as a security measure.
            # This prevents a user from writing an image header into a raw
            # volume with a backing file pointing to data they wish to
            # access.
            raise exception.ImageUnacceptable(
                image_id=image_id,
                reason=_("fmt=%(fmt)s backed by:%(backing_file)s")
                % {'fmt': fmt, 'backing_file': backing_file})

        convert_image(volume_path, tmp, image_meta['disk_format'],
                      bps_limit=CONF.volume_copy_bps_limit)

        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 fileutils.file_open(tmp, 'rb') as image_file:
            image_service.update(context, image_id, {}, image_file)
        fileutils.delete_if_exists(tmp)
Esempio n. 11
0
def upload_volume_to_vgw(
    context, image_service, image_meta, volume_path, volume, vgw_url, volume_format="raw", run_as_root=True
):
    LOG.error("begin time of upload_volume_to_vgw is %s" % (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())))
    image_id = image_meta["id"]
    volume_id = volume["id"]
    if image_meta["disk_format"] == volume_format:
        LOG.debug("%s was %s, no need to convert to %s" % (image_id, volume_format, image_meta["disk_format"]))
        if os.name == "nt" or os.access(volume_path, os.R_OK):
            with fileutils.file_open(volume_path, "rb") as files:
                r = requests.post(vgw_url, data=files)
                if r.status_code != 200:
                    # LOG.error('upload file  %s  to %s failed' %(file_name,vgw_url))
                    raise exception.ImageUnacceptable(reason=("upload the volume %s back_file failed" % volume_id))
        else:
            with utils.temporary_chown(volume_path):
                with fileutils.file_open(volume_path) as files:
                    r = requests.post(vgw_url, data=files)
                    # LOG.debug('the request result is %s' %(str(r.status_code)))
                    if r.status_code != 200:
                        # LOG.error('upload file  %s  to %s failed' %(file_name,vgw_url))
                        raise exception.ImageUnacceptable(reason=("upload the volume %s back_file failed" % volume_id))
        return

    with temporary_file() as tmp:
        LOG.debug("%s was %s, converting to %s" % (image_id, volume_format, image_meta["disk_format"]))
        LOG.error("begin time of convert_image is %s" % (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())))
        convert_image(volume_path, tmp, image_meta["disk_format"])
        LOG.error("end time of upload_volume_to_vgw is %s" % (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())))
        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 fileutils.file_open(tmp, "rb") as files:
            LOG.error("begin time of upload file is %s" % (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())))
            r = requests.post(vgw_url, data=files)
            # LOG.debug('the request result is %' %(str(r.status_code)))
            if r.status_code != 200:
                # LOG.error('upload file  %s  to %s failed' %(file_name,vgw_url))
                raise exception.ImageUnacceptable(reason=("upload the volume %s back_file failed" % volume_id))
            LOG.error("end time of upload file is %s" % (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())))
    # todo delete the tmp file
    fileutils.delete_if_exists(tmp)
    LOG.error("end time of upload_volume_to_vgw is %s" % (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())))
Esempio n. 12
0
 def restore_backup(self, context, backup, volume, backup_service):
     """Restore an existing backup to a new or existing volume."""
     LOG.debug(_("FuseVolumeDriver::restore_backup() volume=%s") % volume)
     volume_path = self.local_path(volume)
     with utils.temporary_chown(volume_path):
         with fileutils.file_open(volume_path, 'wb') as volume_file:
             backup_service.restore(backup, volume['id'], volume_file)
Esempio n. 13
0
    def restore_backup(self, context, backup, volume, backup_service):
        """Restore an existing backup to a new or existing volume."""
        LOG.debug(_('Begin restore of backup %s.') % backup['id'])

        volume_path = self.local_path(volume)
        with fileutils.file_open(volume_path, 'wb') as volume_file:
            backup_service.restore(backup, volume['id'], volume_file)
Esempio n. 14
0
    def restore_backup(self, context, backup, volume, backup_service):
        """Restore an existing backup to a new or existing volume."""
        LOG.debug(_('Begin restore of backup %s.') % backup['id'])

        volume_path = self.local_path(volume)
        with fileutils.file_open(volume_path, 'wb') as volume_file:
            backup_service.restore(backup, volume['id'], volume_file)
Esempio n. 15
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 fileutils.file_open(volume_path) as volume_file:
             backup_service.backup(backup, volume_file)
Esempio n. 16
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 fileutils.file_open(volume_path) as volume_file:
             backup_service.backup(backup, volume_file)
Esempio n. 17
0
 def put_object(self, container, name, reader, content_length=None,
                etag=None, chunk_size=None, content_type=None,
                headers=None, query_string=None):
     object_path = tempfile.gettempdir() + '/' + container + '/' + name
     with fileutils.file_open(object_path, 'wb') as object_file:
         object_file.write(reader.read())
     return hashlib.md5(reader.read()).hexdigest()
Esempio n. 18
0
 def restore_backup(self, context, backup, volume, backup_service):
     """Restore an existing backup to a new or existing volume."""
     LOG.info(_LI('Restoring backup %(backup)s to volume %(volume)s.') %
              {'backup': backup['id'], 'volume': volume['name']})
     volume_local_path = self.local_path(volume)
     with utils.temporary_chown(volume_local_path):
         with fileutils.file_open(volume_local_path, 'wb') as volume_file:
             backup_service.restore(backup, volume['id'], volume_file)
Esempio n. 19
0
def upload_volume(context,
                  image_service,
                  image_meta,
                  volume_path,
                  volume_format='raw',
                  run_as_root=True):
    image_id = image_meta['id']
    if (image_meta['disk_format'] == volume_format):
        LOG.debug("%s was %s, no need to convert to %s" %
                  (image_id, volume_format, image_meta['disk_format']))
        if os.name == 'nt' or os.access(volume_path, os.R_OK):
            with fileutils.file_open(volume_path, 'rb') as image_file:
                image_service.update(context, image_id, {}, image_file)
        else:
            with utils.temporary_chown(volume_path):
                with fileutils.file_open(volume_path) as image_file:
                    image_service.update(context, image_id, {}, image_file)
        return

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

    fd, tmp = tempfile.mkstemp(dir=CONF.image_conversion_dir)
    os.close(fd)
    with fileutils.remove_path_on_error(tmp):
        LOG.debug("%s was %s, converting to %s" %
                  (image_id, volume_format, image_meta['disk_format']))
        convert_image(volume_path,
                      tmp,
                      image_meta['disk_format'],
                      bps_limit=CONF.volume_copy_bps_limit,
                      run_as_root=run_as_root)

        data = qemu_img_info(tmp, run_as_root=run_as_root)
        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 fileutils.file_open(tmp, 'rb') as image_file:
            image_service.update(context, image_id, {}, image_file)
        fileutils.delete_if_exists(tmp)
Esempio n. 20
0
    def restore_backup(self, context, backup, volume, backup_service):
        """Restore an existing backup to a new or existing volume."""
        LOG.debug("Begin restore of backup %s." % backup["id"])

        volume_path = self.local_path(volume)
        with utils.temporary_chown(volume_path):
            with fileutils.file_open(volume_path, "wb") as volume_file:
                backup_service.restore(backup, volume["id"], volume_file)
Esempio n. 21
0
 def get_object(self, container, name):
     LOG.debug("fake get_object %(container)s, %(name)s.",
               {'container': container,
                'name': name})
     if container == 'socket_error_on_get':
         raise socket.error(111, 'ECONNREFUSED')
     object_path = tempfile.gettempdir() + '/' + container + '/' + name
     with fileutils.file_open(object_path, 'rb') as object_file:
         return (None, object_file.read())
Esempio n. 22
0
 def _download_vmdk_from_vcloud(self, context, src_url, dst_file_name):
     local_file_handle = fileutils.file_open(dst_file_name, "wb")
     remote_file_handle = urllib2.urlopen(src_url)
     file_size = remote_file_handle.headers['content-length']
     util.start_transfer(context,
                         IMAGE_TRANSFER_TIMEOUT_SECS,
                         remote_file_handle,
                         file_size,
                         write_file_handle=local_file_handle)
Esempio n. 23
0
def upload_volume(context, image_service, image_meta, volume_path,
                  volume_format='raw', run_as_root=True):
    image_id = image_meta['id']
    if (image_meta['disk_format'] == volume_format):
        LOG.debug("%s was %s, no need to convert to %s",
                  image_id, volume_format, image_meta['disk_format'])
        if os.name == 'nt' or os.access(volume_path, os.R_OK):
            with fileutils.file_open(volume_path, 'rb') as image_file:
                image_service.update(context, image_id, {}, image_file)
        else:
            with utils.temporary_chown(volume_path):
                with fileutils.file_open(volume_path) as image_file:
                    image_service.update(context, image_id, {}, image_file)
        return

    with temporary_file() as tmp:
        LOG.debug("%s was %s, converting to %s",
                  image_id, volume_format, image_meta['disk_format'])

        data = qemu_img_info(volume_path, run_as_root=run_as_root)
        backing_file = data.backing_file
        fmt = data.file_format
        if backing_file is not None:
            # Disallow backing files as a security measure.
            # This prevents a user from writing an image header into a raw
            # volume with a backing file pointing to data they wish to
            # access.
            raise exception.ImageUnacceptable(
                image_id=image_id,
                reason=_("fmt=%(fmt)s backed by:%(backing_file)s")
                % {'fmt': fmt, 'backing_file': backing_file})

        convert_image(volume_path, tmp, image_meta['disk_format'],
                      run_as_root=run_as_root)

        data = qemu_img_info(tmp, run_as_root=run_as_root)
        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 fileutils.file_open(tmp, 'rb') as image_file:
            image_service.update(context, image_id, {}, image_file)
Esempio n. 24
0
    def test_restore_backup(self):
        volume = {'id': '2', 'name': self.TEST_VOLNAME}

        self.mox.StubOutWithMock(utils, 'temporary_chown')
        mock_tempchown = mock.MagicMock()
        utils.temporary_chown(self.TEST_VOLPATH).AndReturn(mock_tempchown)

        self.mox.StubOutWithMock(fileutils, 'file_open')
        mock_fileopen = mock.MagicMock()
        fileutils.file_open(self.TEST_VOLPATH, 'wb').AndReturn(mock_fileopen)

        backup = {'id': 123, 'volume_id': volume['id']}
        mock_servicebackup = self.mox.CreateMockAnything()
        mock_servicebackup.restore(backup, volume['id'], mox_lib.IgnoreArg())

        self.mox.ReplayAll()

        self._driver.restore_backup(context, backup, volume,
                                    mock_servicebackup)
Esempio n. 25
0
    def test_restore_backup(self):
        volume = {'id': '2', 'name': self.TEST_VOLNAME}

        self.mox.StubOutWithMock(utils, 'temporary_chown')
        mock_tempchown = mock.MagicMock()
        utils.temporary_chown(self.TEST_VOLPATH).AndReturn(mock_tempchown)

        self.mox.StubOutWithMock(fileutils, 'file_open')
        mock_fileopen = mock.MagicMock()
        fileutils.file_open(self.TEST_VOLPATH, 'wb').AndReturn(mock_fileopen)

        backup = {'id': 123, 'volume_id': volume['id']}
        mock_servicebackup = self.mox.CreateMockAnything()
        mock_servicebackup.restore(backup, volume['id'], mox_lib.IgnoreArg())

        self.mox.ReplayAll()

        self._driver.restore_backup(context, backup, volume,
                                    mock_servicebackup)
Esempio n. 26
0
 def restore_backup(self, context, backup, volume, backup_service):
     """Restore an existing backup to a new or existing volume."""
     LOG.info(
         _LI("Restoring backup %(backup)s to volume %(volume)s.")
         % {"backup": backup["id"], "volume": volume["name"]}
     )
     volume_local_path = self.local_path(volume)
     with utils.temporary_chown(volume_local_path):
         with fileutils.file_open(volume_local_path, "wb") as volume_file:
             backup_service.restore(backup, volume["id"], volume_file)
Esempio n. 27
0
 def get_object(self, container, name):
     LOG.debug("fake get_object %(container)s, %(name)s.", {
         'container': container,
         'name': name
     })
     if container == 'socket_error_on_get':
         raise socket.error(111, 'ECONNREFUSED')
     object_path = tempfile.gettempdir() + '/' + container + '/' + name
     with fileutils.file_open(object_path, 'rb') as object_file:
         return (None, object_file.read())
Esempio n. 28
0
 def restore_backup(self, context, backup, volume, backup_service):
     """Restore an existing backup to a new or existing volume."""
     LOG.info(_LI('Restoring backup %(backup)s to volume %(volume)s.'), {
         'backup': backup['id'],
         'volume': volume['name']
     })
     volume_local_path = self.local_path(volume)
     with utils.temporary_chown(volume_local_path):
         with fileutils.file_open(volume_local_path, 'wb') as volume_file:
             backup_service.restore(backup, volume['id'], volume_file)
Esempio n. 29
0
    def copy_volume_to_image(self, context, volume, image_service, image_meta):
        """Copy the volume to the specified image."""
        image_id = image_meta["id"]
        with image_utils.temporary_file() as tmp:
            # image_utils.convert_image doesn't support "sheepdog:" source,
            # so we use the qemu-img directly.
            # Sheepdog volume is always raw-formatted.
            cmd = ("qemu-img", "convert", "-f", "raw", "-t", "none", "-O", "raw", "sheepdog:%s" % volume["name"], tmp)
            self._try_execute(*cmd)

            with fileutils.file_open(tmp, "rb") as image_file:
                image_service.update(context, image_id, {}, image_file)
Esempio n. 30
0
    def test_restore_backup(self):
        volume = {"id": "2", "name": self.TEST_VOLNAME}

        self.mox.StubOutWithMock(utils, "temporary_chown")
        mock_tempchown = self.mox.CreateMockAnything()
        utils.temporary_chown(self.TEST_VOLPATH).AndReturn(mock_tempchown)
        mock_tempchown.__enter__()
        mock_tempchown.__exit__(None, None, None)

        self.mox.StubOutWithMock(fileutils, "file_open")
        mock_fileopen = self.mox.CreateMockAnything()
        fileutils.file_open(self.TEST_VOLPATH, "wb").AndReturn(mock_fileopen)
        mock_fileopen.__enter__()
        mock_fileopen.__exit__(None, None, None)

        backup = {"id": 123, "volume_id": volume["id"]}
        mock_servicebackup = self.mox.CreateMockAnything()
        mock_servicebackup.restore(backup, volume["id"], mox_lib.IgnoreArg())

        self.mox.ReplayAll()
        self._driver.restore_backup(context, backup, volume, mock_servicebackup)
        self.mox.VerifyAll()
Esempio n. 31
0
def upload_volume(context,
                  image_service,
                  image_meta,
                  volume_path,
                  volume_format='raw',
                  run_as_root=True):
    image_id = image_meta['id']
    if (image_meta['disk_format'] == volume_format):
        LOG.debug("%s was %s, no need to convert to %s" %
                  (image_id, volume_format, image_meta['disk_format']))
        if os.name == 'nt' or os.access(volume_path, os.R_OK):
            with fileutils.file_open(volume_path, 'rb') as image_file:
                image_service.update(context, image_id, {}, image_file)
        else:
            with utils.temporary_chown(volume_path):
                with fileutils.file_open(volume_path) as image_file:
                    image_service.update(context, image_id, {}, image_file)
        return

    with temporary_file() as tmp:
        LOG.debug("%s was %s, converting to %s" %
                  (image_id, volume_format, image_meta['disk_format']))
        convert_image(volume_path,
                      tmp,
                      image_meta['disk_format'],
                      bps_limit=CONF.volume_copy_bps_limit,
                      run_as_root=run_as_root)

        data = qemu_img_info(tmp, run_as_root=run_as_root)
        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 fileutils.file_open(tmp, 'rb') as image_file:
            image_service.update(context, image_id, {}, image_file)
Esempio n. 32
0
    def copy_volume_to_image(self, context, volume, image_service, image_meta):
        """Copy the volume to the specified image."""
        image_id = image_meta['id']
        with image_utils.temporary_file() as tmp:
            # image_utils.convert_image doesn't support "sheepdog:" source,
            # so we use the qemu-img directly.
            # Sheepdog volume is always raw-formatted.
            cmd = ('qemu-img', 'convert', '-f', 'raw', '-t', 'none', '-O',
                   'raw', 'sheepdog:%s' % volume['name'], tmp)
            self._try_execute(*cmd)

            with fileutils.file_open(tmp, 'rb') as image_file:
                image_service.update(context, image_id, {}, image_file)
Esempio n. 33
0
def upload_volume(context, image_service, image_meta, volume_path,
                  volume_format='raw', run_as_root=True):
    image_id = image_meta['id']
    if (image_meta['disk_format'] == volume_format):
        LOG.debug("%s was %s, no need to convert to %s" %
                  (image_id, volume_format, image_meta['disk_format']))
        if os.name == 'nt' or os.access(volume_path, os.R_OK):
            with fileutils.file_open(volume_path, 'rb') as image_file:
                image_service.update(context, image_id, {}, image_file)
        else:
            with utils.temporary_chown(volume_path):
                with fileutils.file_open(volume_path) as image_file:
                    image_service.update(context, image_id, {}, image_file)
        return

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

    fd, tmp = tempfile.mkstemp(dir=CONF.image_conversion_dir)
    os.close(fd)
    with fileutils.remove_path_on_error(tmp):
        LOG.debug("%s was %s, converting to %s" %
                  (image_id, volume_format, image_meta['disk_format']))
        convert_image(volume_path, tmp, image_meta['disk_format'],
                      bps_limit=CONF.volume_copy_bps_limit,
                      run_as_root=run_as_root)

        data = qemu_img_info(tmp, run_as_root=run_as_root)
        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 fileutils.file_open(tmp, 'rb') as image_file:
            image_service.update(context, image_id, {}, image_file)
        fileutils.delete_if_exists(tmp)
 def put_object(self,
                container,
                name,
                reader,
                content_length=None,
                etag=None,
                chunk_size=None,
                content_type=None,
                headers=None,
                query_string=None):
     object_path = tempfile.gettempdir() + '/' + container + '/' + name
     with fileutils.file_open(object_path, 'wb') as object_file:
         object_file.write(reader.read())
     return hashlib.md5(reader.read()).hexdigest()
Esempio n. 35
0
    def upload_volume(self, context, image_service, image_meta, volume_path):
        LOG.debug("Uploading volume %s: " ,volume_path)
        image_id = image_meta['id']
        if (image_meta['disk_format'] == 'vhd'):
            LOG.debug("%s was raw, no need to convert to %s" %
                      (image_id, image_meta['disk_format']))
            with fileutils.file_open(volume_path) as image_file:
                image_service.update(context, image_id, {}, image_file)
            return

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

        #Copy the volume to the image conversion dir
        temp_vhd_path = os.path.join(CONF.image_conversion_dir, str(image_meta['id']) + ".vhd")
        self.copy_vhd_disk(volume_path, temp_vhd_path)

        fd, tmp = tempfile.mkstemp(dir=CONF.image_conversion_dir)
        os.close(fd)
        with fileutils.remove_path_on_error(tmp):
            LOG.debug("%s was vhd, converting to %s" %
                      (image_id, image_meta['disk_format']))
            self.convert_image(temp_vhd_path, tmp, image_meta['disk_format'])

            data = self.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})

            LOG.debug("Converted size of %s is: %s", data.backing_file, data.disk_size)

            with fileutils.file_open(tmp) as image_file:
                image_service.update(context, image_id, image_meta, image_file)
            os.unlink(tmp)
Esempio n. 36
0
    def restore_backup(self, context, backup, volume, backup_service):
        """Restore an existing backup to a new or existing volume."""
        LOG.debug(_('Restoring backup %(backup)s to '
                    'volume %(volume)s.') %
                  {'backup': backup['id'],
                   'volume': volume['name']})

        (properties,attach_info) = self._attach_volume_syc(context, volume)

        try:
            volume_path = attach_info['device']['path']
            with utils.temporary_chown(volume_path):
                with fileutils.file_open(volume_path, 'wb') as volume_file:
                    backup_service.restore(backup, volume['id'], volume_file)

        finally:
            self._detach_and_terminate_syc(context,attach_info,volume,properties)
Esempio n. 37
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"])

        LOG.debug(("Creating a new backup for volume %s.") % volume["name"])

        properties = utils.brick_get_connector_properties()
        attach_info = self._attach_volume(context, volume, properties)

        try:
            volume_path = attach_info["device"]["path"]
            with utils.temporary_chown(volume_path):
                with fileutils.file_open(volume_path) as volume_file:
                    backup_service.backup(backup, volume_file)

        finally:
            self._detach_volume(context, attach_info, volume, properties)
Esempio n. 38
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'])

        LOG.debug(_('Creating a new backup for volume %s.') % volume['name'])

        properties = utils.brick_get_connector_properties()
        attach_info = self._attach_volume(context, volume, properties)

        try:
            volume_path = attach_info['device']['path']
            with utils.temporary_chown(volume_path):
                with fileutils.file_open(volume_path) as volume_file:
                    backup_service.backup(backup, volume_file)

        finally:
            self._detach_volume(context, attach_info, volume, properties)
Esempio n. 39
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'])

        LOG.debug(_('Creating a new backup for volume %s.') %
                  volume['name'])

        (properties,attach_info) = self._attach_volume_syc(context, volume)

        try:
            volume_path = attach_info['device']['path']
            with utils.temporary_chown(volume_path):
                with fileutils.file_open(volume_path) as volume_file:
                    backup_service.backup(backup, volume_file)

        finally:
            self._detach_and_terminate_syc(context,attach_info,volume,properties)
Esempio n. 40
0
    def restore_backup(self, context, backup, volume, backup_service):
        """Restore an existing backup to a new or existing volume."""
        LOG.debug(
            ("Restoring backup %(backup)s to " "volume %(volume)s.")
            % {"backup": backup["id"], "volume": volume["name"]}
        )

        properties = utils.brick_get_connector_properties()
        attach_info = self._attach_volume(context, volume, properties)

        try:
            volume_path = attach_info["device"]["path"]
            with utils.temporary_chown(volume_path):
                with fileutils.file_open(volume_path, "wb") as volume_file:
                    backup_service.restore(backup, volume["id"], volume_file)

        finally:
            self._detach_volume(context, attach_info, volume, properties)
Esempio n. 41
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_local_path = self.local_path(volume)
        LOG.info(_LI("Begin backup of volume %s.") % volume["name"])

        qemu_img_info = image_utils.qemu_img_info(volume_local_path)
        if qemu_img_info.file_format != "raw":
            msg = _("Backup is only supported for raw-formatted " "SOFS volumes.")
            raise exception.InvalidVolume(msg)

        if qemu_img_info.backing_file is not None:
            msg = _("Backup is only supported for SOFS volumes " "without backing file.")
            raise exception.InvalidVolume(msg)

        with utils.temporary_chown(volume_local_path):
            with fileutils.file_open(volume_local_path) as volume_file:
                backup_service.backup(backup, volume_file)
Esempio n. 42
0
    def restore_backup(self, context, backup, volume, backup_service):
        """Restore an existing backup to a new or existing volume."""
        LOG.debug(_('Restoring backup %(backup)s to '
                    'volume %(volume)s.') %
                  {'backup': backup['id'],
                   'volume': volume['name']})

        properties = utils.brick_get_connector_properties()
        attach_info = self._attach_volume(context, volume, properties)

        try:
            volume_path = attach_info['device']['path']
            with utils.temporary_chown(volume_path):
                with fileutils.file_open(volume_path, 'wb') as volume_file:
                    backup_service.restore(backup, volume['id'], volume_file)

        finally:
            self._detach_volume(context, attach_info, volume, properties)
Esempio n. 43
0
    def copy_volume_to_image(self, context, volume, image_service, image_meta):
        """Copy the volume to the specified image."""
        image_id = image_meta['id']
        with image_utils.temporary_file() as tmp:
            # image_utils.convert_image doesn't support "sheepdog:" source,
            # so we use the qemu-img directly.
            # Sheepdog volume is always raw-formatted.
            cmd = ('qemu-img',
                   'convert',
                   '-f', 'raw',
                   '-t', 'none',
                   '-O', 'raw',
                   'sheepdog:%s' % volume['name'],
                   tmp)
            self._try_execute(*cmd)

            with fileutils.file_open(tmp, 'rb') as image_file:
                image_service.update(context, image_id, {}, image_file)
Esempio n. 44
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'])

        LOG.debug(_('Creating a new backup for volume %s.') % volume['name'])

        root_helper = 'sudo cinder-rootwrap %s' % CONF.rootwrap_config
        properties = initiator.get_connector_properties(root_helper)
        attach_info = self._attach_volume(context, volume, properties)

        try:
            volume_path = attach_info['device']['path']
            with utils.temporary_chown(volume_path):
                with fileutils.file_open(volume_path) as volume_file:
                    backup_service.backup(backup, volume_file)

        finally:
            self._detach_volume(attach_info)
            self.terminate_connection(volume, properties)
Esempio n. 45
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)
        LOG.debug(_('Begin backup of volume %s.') % volume['name'])

        # create a snapshot that will be used as the backup source
        backup_path = '%s_%s.snap' % (volume_path, backup['id'])
        self._create_gpfs_snap(volume_path, backup_path)
        self._gpfs_redirect(volume_path)
        try:
            with fileutils.file_open(backup_path) as backup_file:
                backup_service.backup(backup, backup_file)
        finally:
            # clean up snapshot file.  If it is a clone parent, delete
            # will fail silently, but be cleaned up when volume is
            # eventually removed.  This ensures we do not accumulate
            # more than gpfs_max_clone_depth snap files.
            self._delete_gpfs_file(backup_path)
Esempio n. 46
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)
        LOG.debug(_('Begin backup of volume %s.') % volume['name'])

        # create a snapshot that will be used as the backup source
        backup_path = '%s_%s.snap' % (volume_path, backup['id'])
        self._create_gpfs_snap(volume_path, backup_path)
        self._gpfs_redirect(volume_path)
        try:
            with fileutils.file_open(backup_path) as backup_file:
                backup_service.backup(backup, backup_file)
        finally:
            # clean up snapshot file.  If it is a clone parent, delete
            # will fail silently, but be cleaned up when volume is
            # eventually removed.  This ensures we do not accumulate
            # more than gpfs_max_clone_depth snap files.
            self._delete_gpfs_file(backup_path)
Esempio n. 47
0
    def restore_backup(self, context, backup, volume, backup_service):
        """Restore an existing backup to a new or existing volume."""
        LOG.debug(_('Restoring backup %(backup)s to '
                    'volume %(volume)s.') %
                  {'backup': backup['id'],
                   'volume': volume['name']})

        root_helper = 'sudo cinder-rootwrap %s' % CONF.rootwrap_config
        properties = initiator.get_connector_properties(root_helper)
        attach_info = self._attach_volume(context, volume, properties)

        try:
            volume_path = attach_info['device']['path']
            with utils.temporary_chown(volume_path):
                with fileutils.file_open(volume_path, 'wb') as volume_file:
                    backup_service.restore(backup, volume['id'], volume_file)

        finally:
            self._detach_volume(attach_info)
            self.terminate_connection(volume, properties)
Esempio n. 48
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'])

        LOG.debug(_('Creating a new backup for volume %s.') %
                  volume['name'])

        root_helper = 'sudo cinder-rootwrap %s' % CONF.rootwrap_config
        properties = initiator.get_connector_properties(root_helper)
        attach_info = self._attach_volume(context, volume, properties)

        try:
            volume_path = attach_info['device']['path']
            with utils.temporary_chown(volume_path):
                with fileutils.file_open(volume_path) as volume_file:
                    backup_service.backup(backup, volume_file)

        finally:
            self._detach_volume(attach_info)
            self.terminate_connection(volume, properties)
Esempio n. 49
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_local_path = self.local_path(volume)
        LOG.info(_LI('Begin backup of volume %s.') % volume['name'])

        qemu_img_info = image_utils.qemu_img_info(volume_local_path)
        if qemu_img_info.file_format != 'raw':
            msg = _('Backup is only supported for raw-formatted '
                    'SOFS volumes.')
            raise exception.InvalidVolume(msg)

        if qemu_img_info.backing_file is not None:
            msg = _('Backup is only supported for SOFS volumes '
                    'without backing file.')
            raise exception.InvalidVolume(msg)

        with utils.temporary_chown(volume_local_path):
            with fileutils.file_open(volume_local_path) as volume_file:
                backup_service.backup(backup, volume_file)
Esempio n. 50
0
    def restore_backup(self, context, backup, volume, backup_service):
        """Restore an existing backup to a new or existing volume."""
        LOG.debug(_('Restoring backup %(backup)s to '
                    'volume %(volume)s.') %
                  {'backup': backup['id'],
                   'volume': volume['name']})

        root_helper = 'sudo cinder-rootwrap %s' % CONF.rootwrap_config
        properties = initiator.get_connector_properties(root_helper)
        attach_info = self._attach_volume(context, volume, properties)

        try:
            volume_path = attach_info['device']['path']
            with utils.temporary_chown(volume_path):
                with fileutils.file_open(volume_path, 'wb') as volume_file:
                    backup_service.restore(backup, volume['id'], volume_file)

        finally:
            self._detach_volume(attach_info)
            self.terminate_connection(volume, properties)
Esempio n. 51
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)
        temp_snapshot = None
        previous_status = volume['previous_status']
        if previous_status == 'in-use':
            temp_snapshot = self._create_temp_snapshot(context, volume)
            backup.temp_snapshot_id = temp_snapshot.id
            backup.save()
            volume_path = self.local_path(temp_snapshot)
        else:
            volume_path = self.local_path(volume)

        try:
            with utils.temporary_chown(volume_path):
                with fileutils.file_open(volume_path) as volume_file:
                    backup_service.backup(backup, volume_file)
        finally:
            if temp_snapshot:
                self._delete_snapshot(context, temp_snapshot)
                backup.temp_snapshot_id = None
                backup.save()
Esempio n. 52
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)
        temp_snapshot = None
        previous_status = volume['previous_status']
        if previous_status == 'in-use':
            temp_snapshot = self._create_temp_snapshot(context, volume)
            backup.temp_snapshot_id = temp_snapshot.id
            backup.save()
            volume_path = self.local_path(temp_snapshot)
        else:
            volume_path = self.local_path(volume)

        try:
            with utils.temporary_chown(volume_path):
                with fileutils.file_open(volume_path) as volume_file:
                    backup_service.backup(backup, volume_file)
        finally:
            if temp_snapshot:
                self._delete_snapshot(context, temp_snapshot)
                backup.temp_snapshot_id = None
                backup.save()
 def get_object(self, container, name):
     if container == 'socket_error_on_get':
         raise socket.error(111, 'ECONNREFUSED')
     object_path = tempfile.gettempdir() + '/' + container + '/' + name
     with fileutils.file_open(object_path, 'rb') as object_file:
         return (None, object_file.read())
Esempio n. 54
0
def upload_volume_to_vgw(context,
                         image_service,
                         image_meta,
                         volume_path,
                         volume,
                         vgw_url,
                         volume_format='raw',
                         run_as_root=True):
    LOG.error('begin time of upload_volume_to_vgw is %s' %
              (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())))
    image_id = image_meta['id']
    volume_id = volume['id']
    if (image_meta['disk_format'] == volume_format):
        LOG.debug("%s was %s, no need to convert to %s" %
                  (image_id, volume_format, image_meta['disk_format']))
        if os.name == 'nt' or os.access(volume_path, os.R_OK):
            with fileutils.file_open(volume_path, 'rb') as files:
                r = requests.post(vgw_url, data=files)
                if r.status_code != 200:
                    #LOG.error('upload file  %s  to %s failed' %(file_name,vgw_url))
                    raise exception.ImageUnacceptable(
                        reason=("upload the volume %s back_file failed" %
                                volume_id))
        else:
            with utils.temporary_chown(volume_path):
                with fileutils.file_open(volume_path) as files:
                    r = requests.post(vgw_url, data=files)
                    #LOG.debug('the request result is %s' %(str(r.status_code)))
                    if r.status_code != 200:
                        #LOG.error('upload file  %s  to %s failed' %(file_name,vgw_url))
                        raise exception.ImageUnacceptable(
                            reason=("upload the volume %s back_file failed" %
                                    volume_id))
        return

    with temporary_file() as tmp:
        LOG.debug("%s was %s, converting to %s" %
                  (image_id, volume_format, image_meta['disk_format']))
        LOG.error('begin time of convert_image is %s' %
                  (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())))
        convert_image(volume_path, tmp, image_meta['disk_format'])
        LOG.error('end time of upload_volume_to_vgw is %s' %
                  (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())))
        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 fileutils.file_open(tmp, 'rb') as files:
            LOG.error('begin time of upload file is %s' %
                      (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())))
            r = requests.post(vgw_url, data=files)
            #LOG.debug('the request result is %' %(str(r.status_code)))
            if r.status_code != 200:
                #LOG.error('upload file  %s  to %s failed' %(file_name,vgw_url))
                raise exception.ImageUnacceptable(
                    reason=("upload the volume %s back_file failed" %
                            volume_id))
            LOG.error('end time of upload file is %s' %
                      (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())))
    #todo delete the tmp file
    fileutils.delete_if_exists(tmp)
    LOG.error('end time of upload_volume_to_vgw is %s' %
              (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())))