def upload_storage(context, image_service, image_meta, storage_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(storage_path): with utils.file_open(storage_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(storage_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)
def upload_storage(context, image_service, image_meta, storage_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(storage_path): with utils.file_open(storage_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(storage_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)
def fetch(context, image_service, image_id, path, _user_id, _project_id): # TODO(vish): Improve context handling and add owner and auth data # when it is added to glance. Right now there is no # auth checking in glance, so we assume that access was # checked before we got here. with utils.remove_path_on_error(path): with open(path, "wb") as image_file: image_service.download(context, image_id, image_file)
def fetch_to_raw(context, image_service, image_id, dest, user_id=None, project_id=None): if (FLAGS.image_conversion_dir and not os.path.exists(FLAGS.image_conversion_dir)): os.makedirs(FLAGS.image_conversion_dir) # NOTE(avishay): I'm not crazy about creating temp files which may be # large and cause disk full errors which would confuse users. # Unfortunately it seems that you can't pipe to 'qemu-img convert' because # it seeks. Maybe we can think of something for a future version. fd, tmp = tempfile.mkstemp(dir=FLAGS.image_conversion_dir) os.close(fd) with utils.remove_path_on_error(tmp): fetch(context, image_service, image_id, tmp, user_id, project_id) data = qemu_img_info(tmp) fmt = data.file_format if fmt is None: raise exception.ImageUnacceptable( reason=_("'qemu-img info' parsing failed."), image_id=image_id) backing_file = data.backing_file if backing_file is not None: raise exception.ImageUnacceptable(image_id=image_id, reason=_("fmt=%(fmt)s backed by:" "%(backing_file)s") % locals()) # NOTE(jdg): I'm using qemu-img convert to write # to the storage regardless if it *needs* conversion or not # TODO(avishay): We can speed this up by checking if the image is raw # and if so, writing directly to the device. However, we need to keep # check via 'qemu-img info' that what we copied was in fact a raw # image and not a different format with a backing file, which may be # malicious. LOG.debug("%s was %s, converting to raw" % (image_id, fmt)) convert_image(tmp, dest, 'raw') data = qemu_img_info(dest) if data.file_format != "raw": raise exception.ImageUnacceptable( image_id=image_id, reason=_("Converted to raw, but format is now %s") % data.file_format) os.unlink(tmp)
def fetch_to_raw(context, image_service, image_id, dest, user_id=None, project_id=None): if (FLAGS.image_conversion_dir and not os.path.exists(FLAGS.image_conversion_dir)): os.makedirs(FLAGS.image_conversion_dir) # NOTE(avishay): I'm not crazy about creating temp files which may be # large and cause disk full errors which would confuse users. # Unfortunately it seems that you can't pipe to 'qemu-img convert' because # it seeks. Maybe we can think of something for a future version. fd, tmp = tempfile.mkstemp(dir=FLAGS.image_conversion_dir) os.close(fd) with utils.remove_path_on_error(tmp): fetch(context, image_service, image_id, tmp, user_id, project_id) data = qemu_img_info(tmp) fmt = data.file_format if fmt is None: raise exception.ImageUnacceptable( reason=_("'qemu-img info' parsing failed."), image_id=image_id) backing_file = data.backing_file if backing_file is not None: raise exception.ImageUnacceptable( image_id=image_id, reason=_("fmt=%(fmt)s backed by:" "%(backing_file)s") % locals()) # NOTE(jdg): I'm using qemu-img convert to write # to the storage regardless if it *needs* conversion or not # TODO(avishay): We can speed this up by checking if the image is raw # and if so, writing directly to the device. However, we need to keep # check via 'qemu-img info' that what we copied was in fact a raw # image and not a different format with a backing file, which may be # malicious. LOG.debug("%s was %s, converting to raw" % (image_id, fmt)) convert_image(tmp, dest, 'raw') data = qemu_img_info(dest) if data.file_format != "raw": raise exception.ImageUnacceptable( image_id=image_id, reason=_("Converted to raw, but format is now %s") % data.file_format) os.unlink(tmp)