Example #1
0
    def create(self, req, task):
        # NOTE(rosmaita): access to this call is enforced in the deserializer

        ctxt = req.context
        task_factory = self.gateway.get_task_factory(ctxt)
        executor_factory = self.gateway.get_task_executor_factory(ctxt)
        task_repo = self.gateway.get_task_repo(ctxt)
        try:
            new_task = task_factory.new_task(
                task_type=task['type'],
                owner=ctxt.owner,
                task_input=task['input'],
                image_id=task['input'].get('image_id'),
                user_id=ctxt.user_id,
                request_id=ctxt.request_id)
            task_repo.add(new_task)
            task_executor = executor_factory.new_task_executor(ctxt)
            pool = common.get_thread_pool("tasks_pool")
            pool.spawn(new_task.run, task_executor)
        except exception.Forbidden as e:
            msg = (_LW("Forbidden to create task. Reason: %(reason)s") % {
                'reason': encodeutils.exception_to_unicode(e)
            })
            LOG.warn(msg)
            raise webob.exc.HTTPForbidden(explanation=e.msg)
        return new_task
Example #2
0
    def import_image(self, req, image_id, body):
        task_factory = self.gateway.get_task_factory(req.context)
        executor_factory = self.gateway.get_task_executor_factory(req.context)
        task_repo = self.gateway.get_task_repo(req.context)

        task_input = {'image_id': image_id}

        try:
            import_task = task_factory.new_task(task_type='api_image_import',
                                                owner=req.context.owner,
                                                task_input=task_input)
            task_repo.add(import_task)
            task_executor = executor_factory.new_task_executor(req.context)
            pool = common.get_thread_pool("tasks_eventlet_pool")
            pool.spawn_n(import_task.run, task_executor)
        except exception.Forbidden as e:
            LOG.debug("User not permitted to create image import task.")
            raise webob.exc.HTTPForbidden(explanation=e.msg)
        except exception.Conflict as e:
            raise webob.exc.HTTPConflict(explanation=e.msg)
        except exception.InvalidImageStatusTransition as e:
            raise webob.exc.HTTPConflict(explanation=e.msg)
        except ValueError as e:
            LOG.debug("Cannot import data for image %(id)s: %(e)s", {
                'id': image_id,
                'e': encodeutils.exception_to_unicode(e)
            })
            raise webob.exc.HTTPBadRequest(
                explanation=encodeutils.exception_to_unicode(e))

        return image_id
Example #3
0
def drain_threadpools():
    # NOTE(danms): If there are any other named pools that we need to
    # drain before exit, they should be in this list.
    pools_to_drain = ['tasks_pool']
    for pool_name in pools_to_drain:
        pool_model = common.get_thread_pool(pool_name)
        LOG.info('Waiting for remaining threads in pool %r', pool_name)
        pool_model.pool.shutdown()
Example #4
0
    def test_drain_threadpools(self):
        # Initialize the thread pool model and tasks_pool, like API
        # under WSGI would, and so we have a pointer to that exact
        # pool object in the cache
        glance.async_.set_threadpool_model('native')
        model = common.get_thread_pool('tasks_pool')

        with mock.patch.object(model.pool, 'shutdown') as mock_shutdown:
            wsgi_app.drain_threadpools()
            # Make sure that shutdown() was called on the tasks_pool
            # ThreadPoolExecutor
            mock_shutdown.assert_called_once_with()
Example #5
0
 def create(self, req, task):
     task_factory = self.gateway.get_task_factory(req.context)
     executor_factory = self.gateway.get_task_executor_factory(req.context)
     task_repo = self.gateway.get_task_repo(req.context)
     try:
         new_task = task_factory.new_task(task_type=task['type'],
                                          owner=req.context.owner,
                                          task_input=task['input'])
         task_repo.add(new_task)
         task_executor = executor_factory.new_task_executor(req.context)
         pool = common.get_thread_pool("tasks_eventlet_pool")
         pool.spawn_n(new_task.run, task_executor)
     except exception.Forbidden as e:
         msg = (_LW("Forbidden to create task. Reason: %(reason)s")
                % {'reason': encodeutils.exception_to_unicode(e)})
         LOG.warn(msg)
         raise webob.exc.HTTPForbidden(explanation=e.msg)
     return new_task
Example #6
0
 def create(self, req, task):
     task_factory = self.gateway.get_task_factory(req.context)
     executor_factory = self.gateway.get_task_executor_factory(req.context)
     task_repo = self.gateway.get_task_repo(req.context)
     try:
         new_task = task_factory.new_task(task_type=task['type'],
                                          owner=req.context.owner,
                                          task_input=task['input'])
         task_repo.add(new_task)
         task_executor = executor_factory.new_task_executor(req.context)
         pool = common.get_thread_pool("tasks_eventlet_pool")
         pool.spawn_n(new_task.run, task_executor)
     except exception.Forbidden as e:
         msg = (_LW("Forbidden to create task. Reason: %(reason)s")
                % {'reason': encodeutils.exception_to_unicode(e)})
         LOG.warn(msg)
         raise webob.exc.HTTPForbidden(explanation=e.msg)
     return new_task
Example #7
0
    def import_image(self, req, image_id, body):
        task_factory = self.gateway.get_task_factory(req.context)
        executor_factory = self.gateway.get_task_executor_factory(req.context)
        task_repo = self.gateway.get_task_repo(req.context)

        task_input = {'image_id': image_id}

        try:
            import_task = task_factory.new_task(task_type='api_image_import',
                                                owner=req.context.owner,
                                                task_input=task_input)
            task_repo.add(import_task)
            task_executor = executor_factory.new_task_executor(req.context)
            pool = common.get_thread_pool("tasks_eventlet_pool")
            pool.spawn_n(import_task.run, task_executor)
        except exception.Forbidden as e:
            LOG.debug("User not permitted to create image import task.")
            raise webob.exc.HTTPForbidden(explanation=e.msg)

        return image_id
Example #8
0
    def run(self):
        images = self.cache.get_queued_images()
        if not images:
            LOG.debug("Nothing to prefetch.")
            return True

        num_images = len(images)
        LOG.debug("Found %d images to prefetch", num_images)

        pool = api_common.get_thread_pool('prefetcher', size=num_images)
        results = pool.map(self.fetch_image_into_cache, images)
        successes = sum([1 for r in results if r is True])
        if successes != num_images:
            LOG.warn(
                _LW("Failed to successfully cache all "
                    "images in queue."))
            return False

        LOG.info(_LI("Successfully cached all %d images"), num_images)
        return True
Example #9
0
    def import_image(self, req, image_id, body):
        task_factory = self.gateway.get_task_factory(req.context)
        executor_factory = self.gateway.get_task_executor_factory(req.context)
        task_repo = self.gateway.get_task_repo(req.context)

        task_input = {'image_id': image_id,
                      'import_req': body}

        import_method = body.get('method').get('name')
        uri = body.get('method').get('uri')
        if (import_method == 'web-download' and
           not utils.validate_import_uri(uri)):
                LOG.debug("URI for web-download does not pass filtering: %s",
                          uri)
                msg = (_("URI for web-download does not pass filtering: %s") %
                       uri)
                raise webob.exc.HTTPBadRequest(explanation=msg)

        try:
            import_task = task_factory.new_task(task_type='api_image_import',
                                                owner=req.context.owner,
                                                task_input=task_input)
            task_repo.add(import_task)
            task_executor = executor_factory.new_task_executor(req.context)
            pool = common.get_thread_pool("tasks_eventlet_pool")
            pool.spawn_n(import_task.run, task_executor)
        except exception.Forbidden as e:
            LOG.debug("User not permitted to create image import task.")
            raise webob.exc.HTTPForbidden(explanation=e.msg)
        except exception.Conflict as e:
            raise webob.exc.HTTPConflict(explanation=e.msg)
        except exception.InvalidImageStatusTransition as e:
            raise webob.exc.HTTPConflict(explanation=e.msg)
        except ValueError as e:
            LOG.debug("Cannot import data for image %(id)s: %(e)s",
                      {'id': image_id,
                       'e': encodeutils.exception_to_unicode(e)})
            raise webob.exc.HTTPBadRequest(
                explanation=encodeutils.exception_to_unicode(e))

        return image_id
Example #10
0
    def import_image(self, req, image_id, body):
        image_repo = self.gateway.get_repo(req.context)
        task_factory = self.gateway.get_task_factory(req.context)
        executor_factory = self.gateway.get_task_executor_factory(req.context)
        task_repo = self.gateway.get_task_repo(req.context)
        import_method = body.get('method').get('name')
        uri = body.get('method').get('uri')

        try:
            image = image_repo.get(image_id)
            if image.status == 'active':
                msg = _("Image with status active cannot be target for import")
                raise exception.Conflict(msg)
            if image.status != 'queued' and import_method == 'web-download':
                msg = _("Image needs to be in 'queued' state to use "
                        "'web-download' method")
                raise exception.Conflict(msg)
            if (image.status != 'uploading'
                    and import_method == 'glance-direct'):
                msg = _("Image needs to be staged before 'glance-direct' "
                        "method can be used")
                raise exception.Conflict(msg)
            if not getattr(image, 'container_format', None):
                msg = _("'container_format' needs to be set before import")
                raise exception.Conflict(msg)
            if not getattr(image, 'disk_format', None):
                msg = _("'disk_format' needs to be set before import")
                raise exception.Conflict(msg)

            backend = None
            if CONF.enabled_backends:
                backend = req.headers.get('x-image-meta-store',
                                          CONF.glance_store.default_backend)
                try:
                    glance_store.get_store_from_store_identifier(backend)
                except glance_store.UnknownScheme:
                    msg = _("Store for scheme %s not found") % backend
                    LOG.warn(msg)
                    raise exception.Conflict(msg)
        except exception.Conflict as e:
            raise webob.exc.HTTPConflict(explanation=e.msg)
        except exception.NotFound as e:
            raise webob.exc.HTTPNotFound(explanation=e.msg)

        task_input = {
            'image_id': image_id,
            'import_req': body,
            'backend': backend
        }

        if (import_method == 'web-download'
                and not utils.validate_import_uri(uri)):
            LOG.debug("URI for web-download does not pass filtering: %s", uri)
            msg = (_("URI for web-download does not pass filtering: %s") % uri)
            raise webob.exc.HTTPBadRequest(explanation=msg)

        try:
            import_task = task_factory.new_task(task_type='api_image_import',
                                                owner=req.context.owner,
                                                task_input=task_input)
            task_repo.add(import_task)
            task_executor = executor_factory.new_task_executor(req.context)
            pool = common.get_thread_pool("tasks_eventlet_pool")
            pool.spawn_n(import_task.run, task_executor)
        except exception.Forbidden as e:
            LOG.debug("User not permitted to create image import task.")
            raise webob.exc.HTTPForbidden(explanation=e.msg)
        except exception.Conflict as e:
            raise webob.exc.HTTPConflict(explanation=e.msg)
        except exception.InvalidImageStatusTransition as e:
            raise webob.exc.HTTPConflict(explanation=e.msg)
        except ValueError as e:
            LOG.debug("Cannot import data for image %(id)s: %(e)s", {
                'id': image_id,
                'e': encodeutils.exception_to_unicode(e)
            })
            raise webob.exc.HTTPBadRequest(
                explanation=encodeutils.exception_to_unicode(e))

        return image_id