def set_image_data(image, uri, task_id, backend=None): data_iter = None try: LOG.info( "Task %(task_id)s: Got image data uri %(data_uri)s to be " "imported", { "data_uri": uri, "task_id": task_id }) data_iter = script_utils.get_image_data_iter(uri) image.set_data(data_iter, backend=backend) except Exception as e: with excutils.save_and_reraise_exception(): LOG.warn("Task %(task_id)s failed with exception %(error)s" % { "error": encodeutils.exception_to_unicode(e), "task_id": task_id }) LOG.info( "Task %(task_id)s: Could not import image file" " %(image_data)s", { "image_data": uri, "task_id": task_id }) finally: if hasattr(data_iter, 'close'): data_iter.close()
def execute(self): """Create temp file into store and return path to it :param image_id: Glance Image ID """ # NOTE(jokke): We've decided to use staging area for this task as # a way to expect users to configure a local store for pre-import # works on the image to happen. # # While using any path should be "technically" fine, it's not what # we recommend as the best solution. For more details on this, please # refer to the comment in the `_ImportToStore.execute` method. try: data = script_utils.get_image_data_iter(self.uri) except Exception as e: with excutils.save_and_reraise_exception(): LOG.error("Task %(task_id)s failed with exception %(error)s", {"error": encodeutils.exception_to_unicode(e), "task_id": self.task_id}) self._path, bytes_written = self.store.add(self.image_id, data, 0)[0:2] try: content_length = int(data.headers['content-length']) if bytes_written != content_length: msg = (_("Task %(task_id)s failed because downloaded data " "size %(data_size)i is different from expected %(" "expected)i") % {"task_id": self.task_id, "data_size": bytes_written, "expected": content_length}) raise exception.ImportTaskError(msg) except (KeyError, ValueError): pass return self._path
def set_image_data(image, uri, task_id): data_iter = None try: LOG.info( _LI("Task %(task_id)s: Got image data uri %(data_uri)s to be " "imported") % { "data_uri": uri, "task_id": task_id }) data_iter = script_utils.get_image_data_iter(uri) image.set_data(data_iter) except Exception as e: with excutils.save_and_reraise_exception(): LOG.warn( _LW("Task %(task_id)s failed with exception %(error)s") % { "error": common_utils.exception_to_str(e), "task_id": task_id }) LOG.info( _LI("Task %(task_id)s: Could not import image file" " %(image_data)s") % { "image_data": uri, "task_id": task_id }) finally: if isinstance(data_iter, file): data_iter.close()
def execute(self, image_id): """Create temp file into store and return path to it :param image_id: Glance Image ID """ # NOTE(flaper87): We've decided to use a separate `work_dir` for # this task - and tasks coming after this one - as a way to expect # users to configure a local store for pre-import works on the image # to happen. # # While using any path should be "technically" fine, it's not what # we recommend as the best solution. For more details on this, please # refer to the comment in the `_ImportToStore.execute` method. data = script_utils.get_image_data_iter(self.uri) path = self.store.add(image_id, data, 0, context=None)[0] try: # NOTE(flaper87): Consider moving this code to a common # place that other tasks can consume as well. stdout, stderr = putils.trycmd('qemu-img', 'info', '--output=json', path, log_errors=putils.LOG_ALL_ERRORS) except OSError as exc: with excutils.save_and_reraise_exception(): exc_message = encodeutils.exception_to_unicode(exc) msg = (_LE('Failed to execute security checks on the image ' '%(task_id)s: %(exc)s') % { 'task_id': self.task_id, 'exc': exc_message }) LOG.error(msg) metadata = json.loads(stdout) backing_file = metadata.get('backing-filename') if backing_file is not None: msg = _("File %(path)s has invalid backing file " "%(bfile)s, aborting.") % { 'path': path, 'bfile': backing_file } raise RuntimeError(msg) return path
def execute(self): """Create temp file into store and return path to it :param image_id: Glance Image ID """ # NOTE(jokke): We've decided to use staging area for this task as # a way to expect users to configure a local store for pre-import # works on the image to happen. # # While using any path should be "technically" fine, it's not what # we recommend as the best solution. For more details on this, please # refer to the comment in the `_ImportToStore.execute` method. data = script_utils.get_image_data_iter(self.uri) path = self.store.add(self.image_id, data, 0)[0] return path
def execute(self, image_id): """Create temp file into store and return path to it :param image_id: Glance Image ID """ # NOTE(flaper87): We've decided to use a separate `work_dir` for # this task - and tasks coming after this one - as a way to expect # users to configure a local store for pre-import works on the image # to happen. # # While using any path should be "technically" fine, it's not what # we recommend as the best solution. For more details on this, please # refer to the comment in the `_ImportToStore.execute` method. data = script_utils.get_image_data_iter(self.uri) path = self.store.add(image_id, data, 0, context=None)[0] return path
def set_image_data(image, uri, task_id): data_iter = None try: LOG.info(_LI("Task %(task_id)s: Got image data uri %(data_uri)s to be " "imported") % {"data_uri": uri, "task_id": task_id}) data_iter = script_utils.get_image_data_iter(uri) image.set_data(data_iter) except Exception as e: with excutils.save_and_reraise_exception(): LOG.warn(_LW("Task %(task_id)s failed with exception %(error)s") % {"error": common_utils.exception_to_str(e), "task_id": task_id}) LOG.info(_LI("Task %(task_id)s: Could not import image file" " %(image_data)s") % {"image_data": uri, "task_id": task_id}) finally: if isinstance(data_iter, file): data_iter.close()
def set_image_data(image, uri, task_id, backend=None): data_iter = None try: LOG.info(_LI("Task %(task_id)s: Got image data uri %(data_uri)s to be " "imported"), {"data_uri": uri, "task_id": task_id}) data_iter = script_utils.get_image_data_iter(uri) image.set_data(data_iter, backend=backend) except Exception as e: with excutils.save_and_reraise_exception(): LOG.warn(_LW("Task %(task_id)s failed with exception %(error)s") % {"error": encodeutils.exception_to_unicode(e), "task_id": task_id}) LOG.info(_LI("Task %(task_id)s: Could not import image file" " %(image_data)s"), {"image_data": uri, "task_id": task_id}) finally: if hasattr(data_iter, 'close'): data_iter.close()
def execute(self, image_id): """Create temp file into store and return path to it :param image_id: Glance Image ID """ # NOTE(flaper87): We've decided to use a separate `work_dir` for # this task - and tasks coming after this one - as a way to expect # users to configure a local store for pre-import works on the image # to happen. # # While using any path should be "technically" fine, it's not what # we recommend as the best solution. For more details on this, please # refer to the comment in the `_ImportToStore.execute` method. data = script_utils.get_image_data_iter(self.uri) path = self.store.add(image_id, data, 0, context=None)[0] try: # NOTE(flaper87): Consider moving this code to a common # place that other tasks can consume as well. stdout, stderr = putils.trycmd('qemu-img', 'info', '--output=json', path, prlimit=utils.QEMU_IMG_PROC_LIMITS, log_errors=putils.LOG_ALL_ERRORS) except OSError as exc: with excutils.save_and_reraise_exception(): exc_message = encodeutils.exception_to_unicode(exc) msg = _LE('Failed to execute security checks on the image ' '%(task_id)s: %(exc)s') LOG.error(msg, {'task_id': self.task_id, 'exc': exc_message}) metadata = json.loads(stdout) backing_file = metadata.get('backing-filename') if backing_file is not None: msg = _("File %(path)s has invalid backing file " "%(bfile)s, aborting.") % {'path': path, 'bfile': backing_file} raise RuntimeError(msg) return path
def set_image_data(image, uri, task_id, backend=None, set_active=True, callback=None): data_iter = None try: LOG.info( _LI("Task %(task_id)s: Got image data uri %(data_uri)s to be " "imported"), { "data_uri": uri, "task_id": task_id }) data_iter = script_utils.get_image_data_iter(uri) if callback: # If a callback was provided, wrap our data iterator to call # the function every 60 seconds. data_iter = script_utils.CallbackIterator(data_iter, callback, min_interval=60) image.set_data(data_iter, backend=backend, set_active=set_active) except Exception as e: with excutils.save_and_reraise_exception(): LOG.warn( _LW("Task %(task_id)s failed with exception %(error)s") % { "error": encodeutils.exception_to_unicode(e), "task_id": task_id }) LOG.info( _LI("Task %(task_id)s: Could not import image file" " %(image_data)s"), { "image_data": uri, "task_id": task_id }) finally: if hasattr(data_iter, 'close'): data_iter.close()
def execute(self): """Create temp file into store and return path to it :param image_id: Glance Image ID """ # NOTE(jokke): We've decided to use staging area for this task as # a way to expect users to configure a local store for pre-import # works on the image to happen. # # While using any path should be "technically" fine, it's not what # we recommend as the best solution. For more details on this, please # refer to the comment in the `_ImportToStore.execute` method. try: data = script_utils.get_image_data_iter(self.uri) except Exception as e: with excutils.save_and_reraise_exception(): LOG.error("Task %(task_id)s failed with exception %(error)s", {"error": encodeutils.exception_to_unicode(e), "task_id": self.task_id}) path = self.store.add(self.image_id, data, 0)[0] return path
def test_get_image_data_https(self): uri = "https://example.com" response = urllib2.urlopen(uri) expected = response.read() self.assertEqual(expected, script_utils.get_image_data_iter(uri).read())