Exemplo n.º 1
0
    def update_all(self, req, image_id, body):
        """
        Replaces the members of the image with those specified in the
        body.  The body is a dict with the following format::

            {"memberships": [
                {"member_id": <MEMBER_ID>,
                 ["can_share": [True|False]]}, ...
            ]}
        """
        self._check_can_access_image_members(req.context)
        self._enforce(req, 'modify_member')
        self._raise_404_if_image_deleted(req, image_id)

        memberships = body.get('memberships')
        if memberships:
            new_number_of_members = len(body['memberships'])
            self._enforce_image_member_quota(req, new_number_of_members)

        try:
            registry.replace_members(req.context, image_id, body)
            self._update_store_acls(req, image_id)
        except exception.Invalid as e:
            LOG.debug(encodeutils.exception_to_unicode(e))
            raise webob.exc.HTTPBadRequest(explanation=e.msg)
        except exception.NotFound as e:
            LOG.debug(encodeutils.exception_to_unicode(e))
            raise webob.exc.HTTPNotFound(explanation=e.msg)
        except exception.Forbidden as e:
            LOG.debug(encodeutils.exception_to_unicode(e))
            raise webob.exc.HTTPNotFound(explanation=e.msg)

        return webob.exc.HTTPNoContent()
Exemplo n.º 2
0
    def create(self, req, metadata_object, namespace):
        object_factory = self.gateway.get_metadef_object_factory(req.context)
        object_repo = self.gateway.get_metadef_object_repo(req.context)
        try:
            new_meta_object = object_factory.new_object(
                namespace=namespace,
                **metadata_object.to_dict())
            object_repo.add(new_meta_object)

        except exception.Forbidden as e:
            LOG.debug("User not permitted to create metadata object within "
                      "'%s' namespace", namespace)
            raise webob.exc.HTTPForbidden(explanation=e.msg)
        except exception.Invalid as e:
            msg = (_("Couldn't create metadata object: %s")
                   % encodeutils.exception_to_unicode(e))
            raise webob.exc.HTTPBadRequest(explanation=msg)
        except exception.NotFound as e:
            raise webob.exc.HTTPNotFound(explanation=e.msg)
        except exception.Duplicate as e:
            raise webob.exc.HTTPConflict(explanation=e.msg)
        except Exception as e:
            LOG.error(encodeutils.exception_to_unicode(e))
            raise webob.exc.HTTPInternalServerError()
        return MetadefObject.to_wsme_model(
            new_meta_object,
            get_object_href(namespace, new_meta_object),
            self.obj_schema_link)
Exemplo n.º 3
0
def main():
    """The main function."""

    try:
        config.parse_args()
    except RuntimeError as e:
        sys.exit("ERROR: %s" % encodeutils.exception_to_unicode(e))
    except SystemExit as e:
        sys.exit("Please specify one command")

    # Setup logging
    logging.setup(CONF, 'xmonitor')

    if CONF.token:
        CONF.slavetoken = CONF.token
        CONF.mastertoken = CONF.token

    command = lookup_command(CONF.command)

    try:
        command(CONF, CONF.args)
    except TypeError as e:
        LOG.error(_LE(command.__doc__) % {'prog': command.__name__})  # noqa
        sys.exit("ERROR: %s" % encodeutils.exception_to_unicode(e))
    except ValueError as e:
        LOG.error(_LE(command.__doc__) % {'prog': command.__name__})  # noqa
        sys.exit("ERROR: %s" % encodeutils.exception_to_unicode(e))
Exemplo n.º 4
0
    def index(self, req, marker=None, limit=None, sort_key='created_at',
              sort_dir='desc', filters=None):
        result = {}
        if filters is None:
            filters = {}
        filters['deleted'] = False

        if limit is None:
            limit = CONF.limit_param_default
        limit = min(CONF.api_limit_max, limit)

        task_repo = self.gateway.get_task_stub_repo(req.context)
        try:
            tasks = task_repo.list(marker, limit, sort_key,
                                   sort_dir, filters)
            if len(tasks) != 0 and len(tasks) == limit:
                result['next_marker'] = tasks[-1].task_id
        except (exception.NotFound, exception.InvalidSortKey,
                exception.InvalidFilterRangeValue) as e:
            LOG.warn(encodeutils.exception_to_unicode(e))
            raise webob.exc.HTTPBadRequest(explanation=e.msg)
        except exception.Forbidden as e:
            LOG.warn(encodeutils.exception_to_unicode(e))
            raise webob.exc.HTTPForbidden(explanation=e.msg)
        result['tasks'] = tasks
        return result
Exemplo n.º 5
0
    def index_shared_images(self, req, id):
        """
        Retrieves list of image memberships for the given member.

        :param req: the Request object coming from the wsgi layer
        :param id: the opaque member identifier
        :returns: The response body is a mapping of the following form

        .. code-block:: json

            {'shared_images': [
                {'image_id': <IMAGE>,
                 'can_share': <SHARE_PERMISSION>, ...}, ...
            ]}

        """
        try:
            members = registry.get_member_images(req.context, id)
        except exception.NotFound as e:
            LOG.debug(encodeutils.exception_to_unicode(e))
            raise webob.exc.HTTPNotFound(explanation=e.msg)
        except exception.Forbidden as e:
            LOG.debug(encodeutils.exception_to_unicode(e))
            raise webob.exc.HTTPForbidden(explanation=e.msg)
        return dict(shared_images=members)
Exemplo n.º 6
0
    def create(self, req, namespace, tag_name):
        tag_factory = self.gateway.get_metadef_tag_factory(req.context)
        tag_repo = self.gateway.get_metadef_tag_repo(req.context)
        tag_name_as_dict = {'name': tag_name}
        try:
            self.schema.validate(tag_name_as_dict)
        except exception.InvalidObject as e:
            raise webob.exc.HTTPBadRequest(explanation=e.msg)
        try:
            new_meta_tag = tag_factory.new_tag(
                namespace=namespace,
                **tag_name_as_dict)
            tag_repo.add(new_meta_tag)
        except exception.Invalid as e:
            msg = (_("Couldn't create metadata tag: %s")
                   % encodeutils.exception_to_unicode(e))
            raise webob.exc.HTTPBadRequest(explanation=msg)
        except exception.Forbidden as e:
            LOG.debug("User not permitted to create metadata tag within "
                      "'%s' namespace", namespace)
            raise webob.exc.HTTPForbidden(explanation=e.msg)
        except exception.NotFound as e:
            raise webob.exc.HTTPNotFound(explanation=e.msg)
        except exception.Duplicate as e:
            raise webob.exc.HTTPConflict(explanation=e.msg)
        except Exception as e:
            LOG.error(encodeutils.exception_to_unicode(e))
            raise webob.exc.HTTPInternalServerError()

        return MetadefTag.to_wsme_model(new_meta_tag)
Exemplo n.º 7
0
def _failure_translator():
    """Translates common pymemcache exceptions into tooz exceptions.

    https://github.com/pinterest/pymemcache/blob/d995/pymemcache/client.py#L202
    """
    try:
        yield
    except pymemcache_client.MemcacheUnexpectedCloseError as e:
        utils.raise_with_cause(coordination.ToozConnectionError,
                               encodeutils.exception_to_unicode(e),
                               cause=e)
    except (socket.timeout, socket.error,
            socket.gaierror, socket.herror) as e:
        # TODO(harlowja): get upstream pymemcache to produce a better
        # exception for these, using socket (vs. a memcache specific
        # error) seems sorta not right and/or the best approach...
        msg = encodeutils.exception_to_unicode(e)
        if e.errno is not None:
            msg += " (with errno %s [%s])" % (errno.errorcode[e.errno],
                                              e.errno)
        utils.raise_with_cause(coordination.ToozConnectionError,
                               msg, cause=e)
    except pymemcache_client.MemcacheError as e:
        utils.raise_with_cause(tooz.ToozError,
                               encodeutils.exception_to_unicode(e),
                               cause=e)
Exemplo n.º 8
0
    def create(self, req, image, extra_properties, tags):
        image_factory = self.gateway.get_image_factory(req.context)
        image_repo = self.gateway.get_repo(req.context)
        try:
            image = image_factory.new_image(extra_properties=extra_properties,
                                            tags=tags, **image)
            image_repo.add(image)
        except (exception.DuplicateLocation,
                exception.Invalid) as e:
            raise webob.exc.HTTPBadRequest(explanation=e.msg)
        except (exception.ReservedProperty,
                exception.ReadonlyProperty) as e:
            raise webob.exc.HTTPForbidden(explanation=e.msg)
        except exception.Forbidden as e:
            LOG.debug("User not permitted to create image")
            raise webob.exc.HTTPForbidden(explanation=e.msg)
        except exception.LimitExceeded as e:
            LOG.warn(encodeutils.exception_to_unicode(e))
            raise webob.exc.HTTPRequestEntityTooLarge(
                explanation=e.msg, request=req, content_type='text/plain')
        except exception.Duplicate as e:
            raise webob.exc.HTTPConflict(explanation=e.msg)
        except exception.NotAuthenticated as e:
            raise webob.exc.HTTPUnauthorized(explanation=e.msg)
        except TypeError as e:
            LOG.debug(encodeutils.exception_to_unicode(e))
            raise webob.exc.HTTPBadRequest(explanation=e)

        return image
Exemplo n.º 9
0
    def update(self, req, image_id, changes):
        image_repo = self.gateway.get_repo(req.context)
        try:
            image = image_repo.get(image_id)

            for change in changes:
                change_method_name = '_do_%s' % change['op']
                change_method = getattr(self, change_method_name)
                change_method(req, image, change)

            if changes:
                image_repo.save(image)
        except exception.NotFound as e:
            raise webob.exc.HTTPNotFound(explanation=e.msg)
        except (exception.Invalid, exception.BadStoreUri) as e:
            raise webob.exc.HTTPBadRequest(explanation=e.msg)
        except exception.Forbidden as e:
            LOG.debug("User not permitted to update image '%s'", image_id)
            raise webob.exc.HTTPForbidden(explanation=e.msg)
        except exception.StorageQuotaFull as e:
            msg = (_("Denying attempt to upload image because it exceeds the"
                     " quota: %s") % encodeutils.exception_to_unicode(e))
            LOG.warn(msg)
            raise webob.exc.HTTPRequestEntityTooLarge(
                explanation=msg, request=req, content_type='text/plain')
        except exception.LimitExceeded as e:
            LOG.exception(encodeutils.exception_to_unicode(e))
            raise webob.exc.HTTPRequestEntityTooLarge(
                explanation=e.msg, request=req, content_type='text/plain')
        except exception.NotAuthenticated as e:
            raise webob.exc.HTTPUnauthorized(explanation=e.msg)

        return image
Exemplo n.º 10
0
    def update(self, req, metadata_tag, namespace, tag_name):
        meta_repo = self.gateway.get_metadef_tag_repo(req.context)
        try:
            metadef_tag = meta_repo.get(namespace, tag_name)
            metadef_tag._old_name = metadef_tag.name
            metadef_tag.name = wsme_utils._get_value(
                metadata_tag.name)
            updated_metadata_tag = meta_repo.save(metadef_tag)
        except exception.Invalid as e:
            msg = (_("Couldn't update metadata tag: %s")
                   % encodeutils.exception_to_unicode(e))
            raise webob.exc.HTTPBadRequest(explanation=msg)
        except exception.Forbidden as e:
            LOG.debug("User not permitted to update metadata tag '%s' "
                      "within '%s' namespace", tag_name, namespace)
            raise webob.exc.HTTPForbidden(explanation=e.msg)
        except exception.NotFound as e:
            raise webob.exc.HTTPNotFound(explanation=e.msg)
        except exception.Duplicate as e:
            raise webob.exc.HTTPConflict(explanation=e.msg)
        except Exception as e:
            LOG.error(encodeutils.exception_to_unicode(e))
            raise webob.exc.HTTPInternalServerError()

        return MetadefTag.to_wsme_model(updated_metadata_tag)
Exemplo n.º 11
0
 def update(self, req, image_id, tag_value):
     image_repo = self.gateway.get_repo(req.context)
     try:
         image = image_repo.get(image_id)
         image.tags.add(tag_value)
         image_repo.save(image)
     except exception.NotFound:
         msg = _("Image %s not found.") % image_id
         LOG.warning(msg)
         raise webob.exc.HTTPNotFound(explanation=msg)
     except exception.Forbidden:
         msg = _("Not allowed to update tags for image %s.") % image_id
         LOG.warning(msg)
         raise webob.exc.HTTPForbidden(explanation=msg)
     except exception.Invalid as e:
         msg = (_("Could not update image: %s")
                % encodeutils.exception_to_unicode(e))
         LOG.warning(msg)
         raise webob.exc.HTTPBadRequest(explanation=msg)
     except exception.ImageTagLimitExceeded as e:
         msg = (_("Image tag limit exceeded for image %(id)s: %(e)s:")
                % {"id": image_id,
                   "e": encodeutils.exception_to_unicode(e)})
         LOG.warning(msg)
         raise webob.exc.HTTPRequestEntityTooLarge(explanation=msg)
Exemplo n.º 12
0
    def execute(self, file_path, **kwargs):

        target_format = CONF.image_conversion.output_format
        # TODO(jokke): Once we support other schemas we need to take them into
        # account and handle the paths here.
        src_path = file_path.split('file://')[-1]
        dest_path = "%(path)s.%(target)s" % {'path': src_path,
                                             'target': target_format}
        self.dest_path = dest_path

        try:
            stdout, stderr = putils.trycmd("qemu-img", "info",
                                           "--output=json",
                                           src_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 = ("Failed to do introspection as part of image "
                       "conversion for %(iid)s: %(err)s")
                LOG.error(msg, {'iid': self.image_id, 'err': exc_message})

        if stderr:
            raise RuntimeError(stderr)

        metadata = json.loads(stdout)
        source_format = metadata.get('format')
        virtual_size = metadata.get('virtual-size', 0)
        image = self.image_repo.get(self.image_id)
        image.virtual_size = virtual_size

        if source_format == target_format:
            LOG.debug("Source is already in target format, "
                      "not doing conversion for %s", self.image_id)
            self.image_repo.save(image)
            return file_path

        try:
            stdout, stderr = putils.trycmd('qemu-img', 'convert',
                                           '-f', source_format,
                                           '-O', target_format,
                                           src_path, dest_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 = "Failed to do image conversion for %(iid)s: %(err)s"
                LOG.error(msg, {'iid': self.image_id, 'err': exc_message})

        if stderr:
            raise RuntimeError(stderr)

        image.disk_format = target_format
        image.container_format = 'bare'
        self.image_repo.save(image)

        os.remove(src_path)

        return "file://%s" % dest_path
Exemplo n.º 13
0
    def cache_tee_iter(self, image_id, image_iter, image_checksum):
        try:
            current_checksum = hashlib.md5()

            with self.driver.open_for_write(image_id) as cache_file:
                for chunk in image_iter:
                    try:
                        cache_file.write(chunk)
                    finally:
                        current_checksum.update(chunk)
                        yield chunk
                cache_file.flush()

                if (image_checksum and
                        image_checksum != current_checksum.hexdigest()):
                    msg = _("Checksum verification failed. Aborted "
                            "caching of image '%s'.") % image_id
                    raise exception.GlanceException(msg)

        except exception.GlanceException as e:
            with excutils.save_and_reraise_exception():
                # image_iter has given us bad, (size_checked_iter has found a
                # bad length), or corrupt data (checksum is wrong).
                LOG.exception(encodeutils.exception_to_unicode(e))
        except Exception as e:
            LOG.exception(_LE("Exception encountered while tee'ing "
                              "image '%(image_id)s' into cache: %(error)s. "
                              "Continuing with response.") %
                          {'image_id': image_id,
                           'error': encodeutils.exception_to_unicode(e)})

            # If no checksum provided continue responding even if
            # caching failed.
            for chunk in image_iter:
                yield chunk
Exemplo n.º 14
0
 def update(self, req, metadata_object, namespace, object_name):
     meta_repo = self.gateway.get_metadef_object_repo(req.context)
     try:
         metadef_object = meta_repo.get(namespace, object_name)
         metadef_object._old_name = metadef_object.name
         metadef_object.name = wsme_utils._get_value(
             metadata_object.name)
         metadef_object.description = wsme_utils._get_value(
             metadata_object.description)
         metadef_object.required = wsme_utils._get_value(
             metadata_object.required)
         metadef_object.properties = wsme_utils._get_value(
             metadata_object.properties)
         updated_metadata_obj = meta_repo.save(metadef_object)
     except exception.Invalid as e:
         msg = (_("Couldn't update metadata object: %s")
                % encodeutils.exception_to_unicode(e))
         raise webob.exc.HTTPBadRequest(explanation=msg)
     except exception.Forbidden as e:
         LOG.debug("User not permitted to update metadata object '%s' "
                   "within '%s' namespace ", object_name, namespace)
         raise webob.exc.HTTPForbidden(explanation=e.msg)
     except exception.NotFound as e:
         raise webob.exc.HTTPNotFound(explanation=e.msg)
     except exception.Duplicate as e:
         raise webob.exc.HTTPConflict(explanation=e.msg)
     except Exception as e:
         LOG.error(encodeutils.exception_to_unicode(e))
         raise webob.exc.HTTPInternalServerError()
     return MetadefObject.to_wsme_model(
         updated_metadata_obj,
         get_object_href(namespace, updated_metadata_obj),
         self.obj_schema_link)
Exemplo n.º 15
0
    def execute(self, image_id):
        """Finishing the task flow

        :param image_id: Glance Image ID
        """
        task = script_utils.get_task(self.task_repo, self.task_id)
        if task is None:
            return
        try:
            task.succeed({'image_id': image_id})
        except Exception as e:
            # Note: The message string contains Error in it to indicate
            # in the task.message that it's a error message for the user.

            # TODO(nikhil): need to bring back save_and_reraise_exception when
            # necessary
            log_msg = _LE("Task ID %(task_id)s failed. Error: %(exc_type)s: "
                          "%(e)s")
            LOG.exception(log_msg, {'exc_type': six.text_type(type(e)),
                                    'e': encodeutils.exception_to_unicode(e),
                                    'task_id': task.task_id})

            err_msg = _("Error: %(exc_type)s: %(e)s")
            task.fail(err_msg % {'exc_type': six.text_type(type(e)),
                                 'e': encodeutils.exception_to_unicode(e)})
        finally:
            self.task_repo.save(task)

        LOG.info(_LI("%(task_id)s of %(task_type)s completed"),
                 {'task_id': self.task_id, 'task_type': self.task_type})
Exemplo n.º 16
0
def parse_isotime(timestr):
    """Parse time from ISO 8601 format."""
    try:
        return iso8601.parse_date(timestr)
    except iso8601.ParseError as e:
        raise ValueError(encodeutils.exception_to_unicode(e))
    except TypeError as e:
        raise ValueError(encodeutils.exception_to_unicode(e))
Exemplo n.º 17
0
    def update(self, req, id, body):
        """Updates an existing image with the registry.

        :param req: wsgi Request object
        :param body: Dictionary of information about the image
        :param id:  The opaque internal identifier for the image

        :retval Returns the updated image information as a mapping,
        """
        image_data = body["image"]
        from_state = body.get("from_state", None)

        # Prohibit modification of 'owner'
        if not req.context.is_admin and "owner" in image_data:
            del image_data["owner"]

        if "location" in image_data:
            image_data["locations"] = [image_data.pop("location")]

        purge_props = req.headers.get("X-Glance-Registry-Purge-Props", "false")
        try:
            LOG.debug(
                "Updating image %(id)s with metadata: %(image_data)r",
                {"id": id, "image_data": {k: v for k, v in image_data.items() if k != "locations"}},
            )
            image_data = _normalize_image_location_for_db(image_data)
            if purge_props == "true":
                purge_props = True
            else:
                purge_props = False

            updated_image = self.db_api.image_update(
                req.context, id, image_data, purge_props=purge_props, from_state=from_state
            )

            LOG.info(_LI("Updating metadata for image %(id)s"), {"id": id})
            return dict(image=make_image_dict(updated_image))
        except exception.Invalid as e:
            msg = _("Failed to update image metadata. " "Got error: %s") % encodeutils.exception_to_unicode(e)
            LOG.error(msg)
            return exc.HTTPBadRequest(msg)
        except exception.ImageNotFound:
            LOG.info(_LI("Image %(id)s not found"), {"id": id})
            raise exc.HTTPNotFound(body="Image not found", request=req, content_type="text/plain")
        except exception.ForbiddenPublicImage:
            LOG.info(_LI("Update denied for public image %(id)s"), {"id": id})
            raise exc.HTTPForbidden()
        except exception.Forbidden:
            # If it's private and doesn't belong to them, don't let on
            # that it exists
            LOG.info(_LI("Access denied to image %(id)s but returning" " 'not found'"), {"id": id})
            raise exc.HTTPNotFound(body="Image not found", request=req, content_type="text/plain")
        except exception.Conflict as e:
            LOG.info(encodeutils.exception_to_unicode(e))
            raise exc.HTTPConflict(body="Image operation conflicts", request=req, content_type="text/plain")
        except Exception:
            LOG.exception(_LE("Unable to update image %s") % id)
            raise
Exemplo n.º 18
0
    def validate(self):
        res = super(SaharaNodeGroupTemplate, self).validate()
        if res:
            return res
        pool = self.properties[self.FLOATING_IP_POOL]
        if pool:
            if self.is_using_neutron():
                neutron_client_plugin = self.client_plugin('neutron')
                try:
                    neutron_client_plugin.find_resourceid_by_name_or_id(
                        neutron_client_plugin.RES_TYPE_NETWORK,
                        pool)
                except Exception as ex:
                    if (neutron_client_plugin.is_not_found(ex)
                            or neutron_client_plugin.is_no_unique(ex)):
                        err_msg = encodeutils.exception_to_unicode(ex)
                        raise exception.StackValidationFailed(message=err_msg)
                    raise

            else:
                try:
                    self.client('nova').floating_ip_pools.find(name=pool)
                except Exception as ex:
                    if self.client_plugin('nova').is_not_found(ex):
                        err_msg = encodeutils.exception_to_unicode(ex)
                        raise exception.StackValidationFailed(message=err_msg)
                    raise

        self.client_plugin().validate_hadoop_version(
            self.properties[self.PLUGIN_NAME],
            self.properties[self.HADOOP_VERSION]
        )

        # validate node processes
        plugin = self.client().plugins.get_version_details(
            self.properties[self.PLUGIN_NAME],
            self.properties[self.HADOOP_VERSION])
        allowed_processes = [item for sublist in
                             list(six.itervalues(plugin.node_processes))
                             for item in sublist]
        unsupported_processes = []
        for process in self.properties[self.NODE_PROCESSES]:
            if process not in allowed_processes:
                unsupported_processes.append(process)
        if unsupported_processes:
            msg = (_("Plugin %(plugin)s doesn't support the following "
                     "node processes: %(unsupported)s. Allowed processes are: "
                     "%(allowed)s") %
                   {'plugin': self.properties[self.PLUGIN_NAME],
                    'unsupported': ', '.join(unsupported_processes),
                    'allowed': ', '.join(allowed_processes)})
            raise exception.StackValidationFailed(
                path=[self.stack.t.RESOURCES,
                      self.name,
                      self.stack.t.get_section_name(rsrc_defn.PROPERTIES)],
                message=msg)
Exemplo n.º 19
0
        def rollback(e):
            set_attr('error', encodeutils.exception_to_unicode(e))

            invalid_path = self.get_image_filepath(image_id, 'invalid')
            LOG.debug("Fetch of cache file failed (%(e)s), rolling back by "
                      "moving '%(incomplete_path)s' to "
                      "'%(invalid_path)s'" %
                      {'e': encodeutils.exception_to_unicode(e),
                       'incomplete_path': incomplete_path,
                       'invalid_path': invalid_path})
            os.rename(incomplete_path, invalid_path)
Exemplo n.º 20
0
    def index(self, req, segment_id):
        """Returns a list a hosts."""
        context = req.environ['masakari.context']
        authorize(context)

        try:
            filters = {}
            limit, marker = common.get_limit_and_marker(req)
            sort_keys, sort_dirs = common.get_sort_params(req.params)

            segment = objects.FailoverSegment.get_by_uuid(context,
                                                          segment_id)

            filters['failover_segment_id'] = segment.uuid
            if 'name' in req.params:
                filters['name'] = req.params['name']

            if 'type' in req.params:
                filters['type'] = req.params['type']

            if 'control_attributes' in req.params:
                filters['control_attributes'] = req.params[
                    'control_attributes']

            if 'on_maintenance' in req.params:
                try:
                    filters['on_maintenance'] = strutils.bool_from_string(
                        req.params['on_maintenance'], strict=True)
                except ValueError as ex:
                    msg = _("Invalid value for on_maintenance: "
                            "%s") % encodeutils.exception_to_unicode(ex)
                    raise exc.HTTPBadRequest(explanation=msg)

            if 'reserved' in req.params:
                try:
                    filters['reserved'] = strutils.bool_from_string(
                        req.params['reserved'], strict=True)
                except ValueError as ex:
                    msg = _("Invalid value for reserved: "
                            "%s") % encodeutils.exception_to_unicode(ex)
                    raise exc.HTTPBadRequest(explanation=msg)

            hosts = self.api.get_all(context, filters=filters,
                                     sort_keys=sort_keys, sort_dirs=sort_dirs,
                                     limit=limit, marker=marker)
        except exception.MarkerNotFound as ex:
            raise exc.HTTPBadRequest(explanation=ex.format_message())
        except exception.Invalid as e:
            raise exc.HTTPBadRequest(explanation=e.format_message())
        except exception.FailoverSegmentNotFound as ex:
            raise exc.HTTPNotFound(explanation=ex.format_message())

        return {'hosts': hosts}
Exemplo n.º 21
0
def _translate_failures():
    """Translates common redis exceptions into tooz exceptions."""
    try:
        yield
    except (exceptions.ConnectionError, exceptions.TimeoutError) as e:
        coordination.raise_with_cause(coordination.ToozConnectionError,
                                      encodeutils.exception_to_unicode(e),
                                      cause=e)
    except exceptions.RedisError as e:
        coordination.raise_with_cause(coordination.ToozError,
                                      encodeutils.exception_to_unicode(e),
                                      cause=e)
Exemplo n.º 22
0
Arquivo: etcd.py Projeto: rocknio/tooz
 def wrapper(*args, **kwargs):
     try:
         return func(*args, **kwargs)
     except ValueError as e:
         # Typically json decoding failed for some reason.
         coordination.raise_with_cause(coordination.ToozError,
                                       encodeutils.exception_to_unicode(e),
                                       cause=e)
     except requests.exceptions.RequestException as e:
         coordination.raise_with_cause(coordination.ToozConnectionError,
                                       encodeutils.exception_to_unicode(e),
                                       cause=e)
Exemplo n.º 23
0
 def _update_capabilities_handler(async_result, timeout,
                                  timeout_exception, group_id, member_id):
     try:
         async_result.get(block=True, timeout=timeout)
     except timeout_exception as e:
         coordination.raise_with_cause(coordination.OperationTimedOut,
                                       encodeutils.exception_to_unicode(e),
                                       cause=e)
     except exceptions.NoNodeError:
         raise coordination.MemberNotJoined(group_id, member_id)
     except exceptions.ZookeeperError as e:
         coordination.raise_with_cause(coordination.ToozError,
                                       encodeutils.exception_to_unicode(e),
                                       cause=e)
Exemplo n.º 24
0
 def wrapper(*args, **kwargs):
     try:
         return func(*args, **kwargs)
     except etcd3_exc.ConnectionFailedError as e:
         utils.raise_with_cause(coordination.ToozConnectionError,
                                encodeutils.exception_to_unicode(e),
                                cause=e)
     except etcd3_exc.ConnectionTimeoutError as e:
         utils.raise_with_cause(coordination.OperationTimedOut,
                                encodeutils.exception_to_unicode(e),
                                cause=e)
     except etcd3_exc.Etcd3Exception as e:
         utils.raise_with_cause(coordination.ToozError,
                                encodeutils.exception_to_unicode(e),
                                cause=e)
Exemplo n.º 25
0
 def test_exception_with_non_ascii_str(self):
     bad_string = chr(200)
     excp = ValueError(bad_string)
     fail = failure.Failure.from_exception(excp)
     self.assertEqual(fail.exception_str,
                      encodeutils.exception_to_unicode(excp))
     # This is slightly different on py2 vs py3... due to how
     # __str__ or __unicode__ is called and what is expected from
     # both...
     if six.PY2:
         msg = encodeutils.exception_to_unicode(excp)
         expected = 'Failure: ValueError: %s' % msg.encode('utf-8')
     else:
         expected = u'Failure: ValueError: \xc8'
     self.assertEqual(str(fail), expected)
    def process(self, ctxt, publisher_id, event_type, payload, metadata):
        self.ctxt = ctxt
        self.user_id = self.ctxt.get('user_id')

        try:
            handler_name = utils.get_notification_handler_name(event_type)
            handler = getattr(self, handler_name)
            if handler:
                handler(payload)
            return oslo_messaging.NotificationResult.HANDLED
        except sql_exc.OperationalError as e:
            LOG.info("Operational Error occurred. Please restart the agent.")
            LOG.error(encodeutils.exception_to_unicode(e))
        except Exception as e:
            LOG.error(encodeutils.exception_to_unicode(e))
Exemplo n.º 27
0
        def _lock():
            # NOTE(sileht): mysql-server (<5.7.5) allows only one lock per
            # connection at a time:
            #  select GET_LOCK("a", 0);
            #  select GET_LOCK("b", 0); <-- this release lock "a" ...
            # Or
            #  select GET_LOCK("a", 0);
            #  select GET_LOCK("a", 0); release and lock again "a"
            #
            # So, we track locally the lock status with self.acquired
            if self.acquired is True:
                if blocking:
                    raise _retry.TryAgain
                return False

            try:
                if not self._conn.open:
                    self._conn.connect()
                with self._conn as cur:
                    cur.execute("SELECT GET_LOCK(%s, 0);", self.name)
                    # Can return NULL on error
                    if cur.fetchone()[0] is 1:
                        self.acquired = True
                        return True
            except pymysql.MySQLError as e:
                utils.raise_with_cause(
                    tooz.ToozError,
                    encodeutils.exception_to_unicode(e),
                    cause=e)

            if blocking:
                raise _retry.TryAgain
            self._conn.close()
            return False
Exemplo n.º 28
0
    def _create_container_if_missing(self, container, connection):
        """
        Creates a missing container in Swift if the
        ``swift_store_create_container_on_put`` option is set.

        :param container: Name of container to create
        :param connection: Connection to swift service
        """
        try:
            connection.head_container(container)
        except swiftclient.ClientException as e:
            if e.http_status == http_client.NOT_FOUND:
                if self.conf.glance_store.swift_store_create_container_on_put:
                    try:
                        msg = (_LI("Creating swift container %(container)s") %
                               {'container': container})
                        LOG.info(msg)
                        connection.put_container(container)
                    except swiftclient.ClientException as e:
                        msg = (_("Failed to add container to Swift.\n"
                                 "Got error from Swift: %s.")
                               % encodeutils.exception_to_unicode(e))
                        raise glance_store.BackendException(msg)
                else:
                    msg = (_("The container %(container)s does not exist in "
                             "Swift. Please set the "
                             "swift_store_create_container_on_put option "
                             "to add container to Swift automatically.") %
                           {'container': container})
                    raise glance_store.BackendException(msg)
            else:
                raise
Exemplo n.º 29
0
    def get_connection(parsed_url, options, defer_connect=False):
        host = parsed_url.hostname
        port = parsed_url.port or MySQLLock.MYSQL_DEFAULT_PORT
        dbname = parsed_url.path[1:]
        username = parsed_url.username
        password = parsed_url.password
        unix_socket = options.get("unix_socket")

        try:
            if unix_socket:
                return pymysql.Connect(unix_socket=unix_socket,
                                       port=port,
                                       user=username,
                                       passwd=password,
                                       database=dbname,
                                       defer_connect=defer_connect)
            else:
                return pymysql.Connect(host=host,
                                       port=port,
                                       user=username,
                                       passwd=password,
                                       database=dbname,
                                       defer_connect=defer_connect)
        except (pymysql.err.OperationalError, pymysql.err.InternalError) as e:
            utils.raise_with_cause(coordination.ToozConnectionError,
                                   encodeutils.exception_to_unicode(e),
                                   cause=e)
Exemplo n.º 30
0
    def show(self, req, namespace, filters=None):
        try:
            # Get namespace
            ns_repo = self.gateway.get_metadef_namespace_repo(req.context)
            namespace_obj = ns_repo.get(namespace)
            namespace_detail = Namespace.to_wsme_model(
                namespace_obj,
                get_namespace_href(namespace_obj),
                self.ns_schema_link)
            ns_filters = dict()
            ns_filters['namespace'] = namespace

            # Get objects
            object_repo = self.gateway.get_metadef_object_repo(req.context)
            db_metaobject_list = object_repo.list(filters=ns_filters)
            object_list = [MetadefObject.to_wsme_model(
                db_metaobject,
                get_object_href(namespace, db_metaobject),
                self.obj_schema_link) for db_metaobject in db_metaobject_list]
            if object_list:
                namespace_detail.objects = object_list

            # Get resource type associations
            rs_repo = self.gateway.get_metadef_resource_type_repo(req.context)
            db_resource_type_list = rs_repo.list(filters=ns_filters)
            resource_type_list = [ResourceTypeAssociation.to_wsme_model(
                resource_type) for resource_type in db_resource_type_list]
            if resource_type_list:
                namespace_detail.resource_type_associations = (
                    resource_type_list)

            # Get properties
            prop_repo = self.gateway.get_metadef_property_repo(req.context)
            db_properties = prop_repo.list(filters=ns_filters)
            property_list = Namespace.to_model_properties(db_properties)
            if property_list:
                namespace_detail.properties = property_list

            if filters and filters['resource_type']:
                namespace_detail = self._prefix_property_name(
                    namespace_detail, filters['resource_type'])

            # Get tags
            tag_repo = self.gateway.get_metadef_tag_repo(req.context)
            db_metatag_list = tag_repo.list(filters=ns_filters)
            tag_list = [MetadefTag(**{'name': db_metatag.name})
                        for db_metatag in db_metatag_list]
            if tag_list:
                namespace_detail.tags = tag_list

        except exception.Forbidden as e:
            LOG.debug("User not permitted to show metadata namespace "
                      "'%s'", namespace)
            raise webob.exc.HTTPForbidden(explanation=e.msg)
        except exception.NotFound as e:
            raise webob.exc.HTTPNotFound(explanation=e.msg)
        except Exception as e:
            LOG.error(encodeutils.exception_to_unicode(e))
            raise webob.exc.HTTPInternalServerError()
        return namespace_detail
Exemplo n.º 31
0
    def _create_bucket(s3_client, s3_host, bucket):
        """Create bucket into the S3.

        :param s3_client: An object with credentials to connect to S3
        :param s3_host: S3 endpoint url
        :param bucket: S3 bucket name
        :raises: BadStoreConfiguration if cannot connect to S3 successfully
        """
        region = get_s3_location(s3_host)
        try:
            s3_client.create_bucket(
                Bucket=bucket, ) if region == '' else s3_client.create_bucket(
                    Bucket=bucket,
                    CreateBucketConfiguration={'LocationConstraint': region})
        except boto_exceptions.ClientError as e:
            msg = ("Failed to add bucket to S3: %s" %
                   encodeutils.exception_to_unicode(e))
            LOG.error(msg)
            raise glance_store.BadStoreConfiguration(store_name='s3',
                                                     reason=msg)
Exemplo n.º 32
0
 def resolve_param(param):
     """Check whether if given item is param and resolve, if it is."""
     # NOTE(prazumovsky): If property uses removed in HOT function,
     # we should not translate it for correct validating and raising
     # validation error.
     if isinstance(param, hot_funcs.Removed):
         raise AttributeError(_('Property uses removed function.'))
     if isinstance(param, (hot_funcs.GetParam, cfn_funcs.ParamRef)):
         try:
             return function.resolve(param)
         except exception.UserParameterMissing as ex:
             # We can't resolve parameter now. Abort translation.
             err_msg = encodeutils.exception_to_unicode(ex)
             raise AttributeError(
                 _('Can not resolve parameter '
                   'due to: %s') % err_msg)
     elif isinstance(param, list):
         return [resolve_param(param_item) for param_item in param]
     else:
         return param
Exemplo n.º 33
0
 def index(self, req, namespace):
     try:
         filters = dict()
         filters['namespace'] = namespace
         prop_repo = self.gateway.get_metadef_property_repo(req.context)
         db_properties = prop_repo.list(filters=filters)
         property_list = Namespace.to_model_properties(db_properties)
         namespace_properties = PropertyTypes()
         namespace_properties.properties = property_list
     except exception.Forbidden as e:
         LOG.debug(
             "User not permitted to retrieve metadata properties "
             "within '%s' namespace", namespace)
         raise webob.exc.HTTPForbidden(explanation=e.msg)
     except exception.NotFound as e:
         raise webob.exc.HTTPNotFound(explanation=e.msg)
     except Exception as e:
         LOG.error(encodeutils.exception_to_unicode(e))
         raise webob.exc.HTTPInternalServerError()
     return namespace_properties
Exemplo n.º 34
0
    def create(self, **kwargs):
        """Create an image."""
        headers = {}
        url = '/v2/images'
        backend = kwargs.pop('backend', None)
        if backend is not None:
            headers['x-image-meta-store'] = backend

        image = self.model()
        for (key, value) in kwargs.items():
            try:
                setattr(image, key, value)
            except warlock.InvalidOperation as e:
                raise TypeError(encodeutils.exception_to_unicode(e))

        resp, body = self.http_client.post(url, headers=headers, data=image)
        # NOTE(esheffield): remove 'self' for now until we have an elegant
        # way to pass it into the model constructor without conflict
        body.pop('self', None)
        return self.model(**body), resp
Exemplo n.º 35
0
 def show(self, req, namespace):
     try:
         filters = {'namespace': namespace}
         rs_type_repo = self.gateway.get_metadef_resource_type_repo(
             req.context)
         db_resource_type_list = rs_type_repo.list(filters=filters)
         resource_type_list = [ResourceTypeAssociation.to_wsme_model(
             resource_type) for resource_type in db_resource_type_list]
         resource_types = ResourceTypeAssociations()
         resource_types.resource_type_associations = resource_type_list
     except exception.Forbidden as e:
         LOG.debug("User not permitted to retrieve metadata resource types "
                   "within '%s' namespace", namespace)
         raise webob.exc.HTTPForbidden(explanation=e.msg)
     except exception.NotFound as e:
         raise webob.exc.HTTPNotFound(explanation=e.msg)
     except Exception as e:
         LOG.error(encodeutils.exception_to_unicode(e))
         raise webob.exc.HTTPInternalServerError(e)
     return resource_types
Exemplo n.º 36
0
    def _do_replace_locations(self, image, value):
        if CONF.show_multiple_locations == False:
            msg = _("It's not allowed to update locations if locations are "
                    "invisible.")
            raise webob.exc.HTTPForbidden(explanation=msg)

        if image.status not in ('active', 'queued'):
            msg = _("It's not allowed to replace locations if image status is "
                    "%s.") % image.status
            raise webob.exc.HTTPConflict(explanation=msg)

        try:
            # NOTE(flwang): _locations_proxy's setattr method will check if
            # the update is acceptable.
            image.locations = value
        except (exception.BadStoreUri, exception.DuplicateLocation) as e:
            raise webob.exc.HTTPBadRequest(explanation=e.msg)
        except ValueError as ve:  # update image status failed.
            raise webob.exc.HTTPBadRequest(
                explanation=encodeutils.exception_to_unicode(ve))
Exemplo n.º 37
0
    def _get_storage_url(self):
        """Get swift endpoint from keystone

        Return endpoint for swift from service catalog. The method works only
        Keystone v3. If you are using different version (1 or 2)
        it returns None.
        :return: swift endpoint
        """
        if self.store.auth_version == '3':
            try:
                return self.client.session.get_endpoint(
                    service_type=self.store.service_type,
                    interface=self.store.endpoint_type,
                    region_name=self.store.region)
            except Exception as e:
                # do the same that swift driver does
                # when catching ClientException
                msg = _("Cannot find swift service endpoint : "
                        "%s") % encodeutils.exception_to_unicode(e)
                raise exceptions.BackendException(msg)
 def update(self, req, namespace, property_name, property_type):
     prop_repo = self.gateway.get_metadef_property_repo(req.context)
     try:
         db_property_type = prop_repo.get(namespace, property_name)
         db_property_type._old_name = db_property_type.name
         db_property_type.name = property_type.name
         db_property_type.schema = (self._to_dict(property_type))['schema']
         updated_property_type = prop_repo.save(db_property_type)
     except exception.Forbidden as e:
         LOG.debug("User not permitted to update metadata property '%s' "
                   "within '%s' namespace", property_name, namespace)
         raise webob.exc.HTTPForbidden(explanation=e.msg)
     except exception.NotFound as e:
         raise webob.exc.HTTPNotFound(explanation=e.msg)
     except exception.Duplicate as e:
         raise webob.exc.HTTPConflict(explanation=e.msg)
     except Exception as e:
         LOG.error(encodeutils.exception_to_unicode(e))
         raise webob.exc.HTTPInternalServerError()
     return self._to_model(updated_property_type)
Exemplo n.º 39
0
def get_verifier(context, image_properties):
    """Retrieve the image properties and use them to create a verifier.

    :param context: the user context for authentication
    :param image_properties: the key-value properties about the image
    :return: instance of cryptography AsymmetricVerificationContext
    :raises glance.common.exception.SignatureVerificationError: if building
            the verifier fails
    """
    if not should_create_verifier(image_properties):
        raise exception.SignatureVerificationError(
            _('Required image properties for signature verification do not'
              ' exist. Cannot verify signature.'))

    signature = get_signature(image_properties[SIGNATURE])
    hash_method = get_hash_method(image_properties[HASH_METHOD])
    signature_key_type = SignatureKeyType.lookup(image_properties[KEY_TYPE])
    public_key = get_public_key(context, image_properties[CERT_UUID],
                                signature_key_type)

    # create the verifier based on the signature key type
    try:
        verifier = signature_key_type.create_verifier(signature, hash_method,
                                                      public_key,
                                                      image_properties)
    except crypto_exception.UnsupportedAlgorithm as e:
        msg = (_LE("Unable to create verifier since algorithm is "
                   "unsupported: %(e)s") % {
                       'e': encodeutils.exception_to_unicode(e)
                   })
        LOG.error(msg)
        raise exception.SignatureVerificationError(
            _('Unable to verify signature since the algorithm is unsupported '
              'on this system'))

    if verifier:
        return verifier
    else:
        # Error creating the verifier
        raise exception.SignatureVerificationError(
            _('Error occurred while creating the verifier'))
Exemplo n.º 40
0
def main():
    CONF.register_cli_opt(command_opt)
    if len(sys.argv) < 2:
        script_name = sys.argv[0]
        print("%s category action [<args>]" % script_name)
        print(_("Available categories:"))
        for category in CATEGORIES:
            print(_("\t%s") % category)
        sys.exit(2)

    try:
        logging.register_options(CONF)
        CONF.set_default(name='use_stderr', default=True)
        cfg_files = cfg.find_config_files(project='glance', prog='glance-api')
        cfg_files.extend(
            cfg.find_config_files(project='glance', prog='glance-manage'))
        config.parse_args(default_config_files=cfg_files)
        config.set_config_defaults()
        logging.setup(CONF, 'glance')
    except RuntimeError as e:
        sys.exit("ERROR: %s" % e)

    try:
        if CONF.command.action.startswith('db'):
            return CONF.command.action_fn()
        else:
            func_kwargs = {}
            for k in CONF.command.action_kwargs:
                v = getattr(CONF.command, 'action_kwarg_' + k)
                if v is None:
                    continue
                if isinstance(v, six.string_types):
                    v = encodeutils.safe_decode(v)
                func_kwargs[k] = v
            func_args = [
                encodeutils.safe_decode(arg)
                for arg in CONF.command.action_args
            ]
            return CONF.command.action_fn(*func_args, **func_kwargs)
    except exception.GlanceException as e:
        sys.exit("ERROR: %s" % encodeutils.exception_to_unicode(e))
Exemplo n.º 41
0
def get_certificate(context, signature_certificate_uuid):
    """Create the certificate object from the retrieved certificate data.

    :param context: the user context for authentication
    :param signature_certificate_uuid: the uuid to use to retrieve the
                                       certificate
    :returns: the certificate cryptography object
    :raises: SignatureVerificationError if the retrieval fails or the format
             is invalid
    """
    keymgr_api = key_manager.API()

    try:
        # The certificate retrieved here is a castellan certificate object
        cert = keymgr_api.get(context, signature_certificate_uuid)
    except KeyManagerError as e:
        # The problem encountered may be backend-specific, since castellan
        # can use different backends.  Rather than importing all possible
        # backends here, the generic "Exception" is used.
        msg = (_LE("Unable to retrieve certificate with ID %(id)s: %(e)s")
               % {'id': signature_certificate_uuid,
                  'e': encodeutils.exception_to_unicode(e)})
        LOG.error(msg)
        raise exception.SignatureVerificationError(
            reason=_('Unable to retrieve certificate with ID: %s')
            % signature_certificate_uuid)

    if cert.format not in CERTIFICATE_FORMATS:
        raise exception.SignatureVerificationError(
            reason=_('Invalid certificate format: %s') % cert.format)

    if cert.format == X_509:
        # castellan always encodes certificates in DER format
        cert_data = cert.get_encoded()
        certificate = x509.load_der_x509_certificate(cert_data,
                                                     default_backend())

    # verify the certificate
    verify_certificate(certificate)

    return certificate
Exemplo n.º 42
0
    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
Exemplo n.º 43
0
def migrate_location_credentials(migrate_engine, to_quoted):
    """
    Migrate location credentials for encrypted swift uri's between the
    quoted and unquoted forms.

    :param migrate_engine: The configured db engine
    :param to_quoted: If True, migrate location credentials from
                      unquoted to quoted form.  If False, do the
                      reverse.
    """
    if not CONF.metadata_encryption_key:
        msg = _LI("'metadata_encryption_key' was not specified in the config"
                  " file or a config file was not specified. This means that"
                  " this migration is a NOOP.")
        LOG.info(msg)
        return

    meta = sqlalchemy.schema.MetaData()
    meta.bind = migrate_engine

    images_table = sqlalchemy.Table('images', meta, autoload=True)

    images = list(images_table.select().execute())

    for image in images:
        try:
            fixed_uri = fix_uri_credentials(image['location'], to_quoted)
            images_table.update().where(
                images_table.c.id == image['id']).values(
                    location=fixed_uri).execute()
        except exception.Invalid:
            msg = _LW("Failed to decrypt location value for image"
                      " %(image_id)s") % {'image_id': image['id']}
            LOG.warn(msg)
        except exception.BadStoreUri as e:
            reason = encodeutils.exception_to_unicode(e)
            msg = _LE("Invalid store uri for image: %(image_id)s. "
                      "Details: %(reason)s") % {'image_id': image.id,
                                                'reason': reason}
            LOG.exception(msg)
            raise
Exemplo n.º 44
0
    def update(self, req, metadata_tag, namespace, tag_name):
        meta_repo = self.gateway.get_metadef_tag_repo(req.context)
        try:
            metadef_tag = meta_repo.get(namespace, tag_name)
            metadef_tag._old_name = metadef_tag.name
            metadef_tag.name = wsme_utils._get_value(metadata_tag.name)
            updated_metadata_tag = meta_repo.save(metadef_tag)
        except exception.Forbidden as e:
            LOG.debug(
                "User not permitted to update metadata tag '%s' "
                "within '%s' namespace", tag_name, namespace)
            raise webob.exc.HTTPForbidden(explanation=e.msg)
        except exception.NotFound as e:
            raise webob.exc.HTTPNotFound(explanation=e.msg)
        except exception.Duplicate as e:
            raise webob.exc.HTTPConflict(explanation=e.msg)
        except Exception as e:
            LOG.error(encodeutils.exception_to_unicode(e))
            raise webob.exc.HTTPInternalServerError()

        return MetadefTag.to_wsme_model(updated_metadata_tag)
Exemplo n.º 45
0
def validate_buffering(buffer_dir):
    if buffer_dir is None:
        msg = _('Configuration option "swift_upload_buffer_dir" is '
                'not set. Please set it to a valid path to buffer '
                'during Swift uploads.')
        raise exceptions.BadStoreConfiguration(store_name='swift',
                                               reason=msg)

    # NOTE(dharinic): Ensure that the provided directory path for
    # buffering is valid
    try:
        _tmpfile = tempfile.TemporaryFile(dir=buffer_dir)
    except OSError as err:
        msg = (_('Unable to use buffer directory set with '
                 '"swift_upload_buffer_dir". Error: %s') %
               encodeutils.exception_to_unicode(err))
        raise exceptions.BadStoreConfiguration(store_name='swift',
                                               reason=msg)
    else:
        _tmpfile.close()
        return True
Exemplo n.º 46
0
    def data_stream(self):
        if len(self.locations) > 0:
            err = None
            try:
                for location in self.locations:
                    data, size = self.store_api.get_from_backend(
                        location['value'],
                        context=self.context)
                    return data
            except Exception as e:
                LOG.warn(_('Get blob %(name)s data failed: '
                           '%(err)s.')
                         % {'name': self.blob.item_key,
                            'err': encodeutils.exception_to_unicode(e)})
                err = e

            # tried all locations
            LOG.error(_LE('Glance tried all active locations to get data '
                          'for blob %s '
                          'but all have failed.') % self.blob.item_key)
            raise err
Exemplo n.º 47
0
 def wrapper(*args, **kwargs):
     try:
         ret = func(*args, **kwargs)
         return SUCCESS if ret is None else ret
     except exception.NotFound:
         options = args[0]
         print("Cache management middleware not enabled on host %s" %
               options.host)
         return FAILURE
     except exception.Forbidden:
         print("Not authorized to make this request.")
         return FAILURE
     except Exception as e:
         options = args[0]
         if options.debug:
             raise
         print("Failed to %s. Got error:" % action)
         pieces = encodeutils.exception_to_unicode(e).split('\n')
         for piece in pieces:
             print(piece)
         return FAILURE
Exemplo n.º 48
0
 def test_replace_too_many_image_locations(self):
     self.config(image_location_quota=1)
     self.image.locations = [{
         "url": "file:///fake.img.tar.gz",
         "metadata": {}
     }]
     locations = [{
         "url": "file:///fake1.img.tar.gz",
         "metadata": {}
     }, {
         "url": "file:///fake2.img.tar.gz",
         "metadata": {}
     }, {
         "url": "file:///fake3.img.tar.gz",
         "metadata": {}
     }]
     exc = self.assertRaises(exception.ImageLocationLimitExceeded, setattr,
                             self.image, 'locations', locations)
     self.assertIn('Attempted: 3, Maximum: 1',
                   encodeutils.exception_to_unicode(exc))
     self.assertEqual(1, len(self.image.locations))
Exemplo n.º 49
0
 def _do_replace_locations(self, image, value):
     if len(image.locations) > 0 and len(value) > 0:
         msg = _("Cannot replace locations from a non-empty "
                 "list to a non-empty list.")
         raise webob.exc.HTTPBadRequest(explanation=msg)
     if len(value) == 0:
         # NOTE(zhiyan): this actually deletes the location
         # from the backend store.
         del image.locations[:]
         if image.status == 'active':
             image.status = 'queued'
     else:   # NOTE(zhiyan): len(image.locations) == 0
         try:
             image.locations = value
             if image.status == 'queued':
                 image.status = 'active'
         except (exception.BadStoreUri, exception.DuplicateLocation) as e:
             raise webob.exc.HTTPBadRequest(explanation=e.msg)
         except ValueError as ve:    # update image status failed.
             raise webob.exc.HTTPBadRequest(
                 explanation=encodeutils.exception_to_unicode(ve))
Exemplo n.º 50
0
def set_index_refresh_interval(index_name, refresh_interval):
    """Set refresh_interval of a given index, basically it is used in the
       reindexing phase. By setting refresh_interval to -1 we disable the
       refresh of offline index to gain a performance boost for the bulk
       updates. After reindexing is done, we will restore refresh_interval
       and put the index online.
    """

    es_engine = searchlight.elasticsearch.get_api()

    body = {
        'index': {
            'refresh_interval': refresh_interval
        }
    }

    try:
        es_engine.indices.put_settings(body, index_name)
    except Exception as e:
        LOG.error(encodeutils.exception_to_unicode(e))
        raise
Exemplo n.º 51
0
def main():
    try:
        config.parse_args()
        config.set_config_defaults()
        wsgi.set_eventlet_hub()
        logging.setup(CONF, 'glance')
        notifier.set_defaults()

        if CONF.profiler.enabled:
            osprofiler.initializer.init_from_conf(conf=CONF,
                                                  context={},
                                                  project="glance",
                                                  service="registry",
                                                  host=CONF.bind_host)

        server = wsgi.Server()
        server.start(config.load_paste_app('glance-registry'),
                     default_port=9191)
        server.wait()
    except RuntimeError as e:
        sys.exit("ERROR: %s" % encodeutils.exception_to_unicode(e))
Exemplo n.º 52
0
    def get_all(self, **kwargs):
        LOG.info('get list resource with args: %s', kwargs)

        resource_type = kwargs.get('resource_type', None)
        all_tenants = kwargs.get('all_tenants', False)
        all_tenants = bool_from_string(all_tenants)
        if all_tenants:
            enforce('list resources:all_tenants', pecan.request.headers,
                    pecan.request.enforcer, {})
        else:
            enforce('list resources', pecan.request.headers,
                    pecan.request.enforcer, {})

        LOG.info('received resources list with filter %s', resource_type)

        try:
            return self._get_resources(resource_type, all_tenants)
        except Exception as e:
            to_unicode = encodeutils.exception_to_unicode(e)
            LOG.exception('failed to list resources %s', to_unicode)
            abort(404, to_unicode)
Exemplo n.º 53
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)
     live_time = CONF.task.task_time_to_live
     try:
         new_task = task_factory.new_task(task_type=task['type'],
                                          owner=req.context.owner,
                                          task_time_to_live=live_time,
                                          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
Exemplo n.º 54
0
    def create(self, req, resource_type, namespace):
        rs_type_factory = self.gateway.get_metadef_resource_type_factory(
            req.context)
        rs_type_repo = self.gateway.get_metadef_resource_type_repo(req.context)
        try:
            new_resource_type = rs_type_factory.new_resource_type(
                namespace=namespace, **resource_type.to_dict())
            rs_type_repo.add(new_resource_type)

        except exception.Forbidden as e:
            LOG.debug("User not permitted to create metadata resource type "
                      "within '%s' namespace", namespace)
            raise webob.exc.HTTPForbidden(explanation=e.msg)
        except exception.NotFound as e:
            raise webob.exc.HTTPNotFound(explanation=e.msg)
        except exception.Duplicate as e:
            raise webob.exc.HTTPConflict(explanation=e.msg)
        except Exception as e:
            LOG.error(encodeutils.exception_to_unicode(e))
            raise webob.exc.HTTPInternalServerError()
        return ResourceTypeAssociation.to_wsme_model(new_resource_type)
Exemplo n.º 55
0
    def create(self, req, task):
        # NOTE(rosmaita): access to this call is enforced in the deserializer

        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
Exemplo n.º 56
0
    def _volume_readonly_update(self, req, id, body):
        """Update volume readonly flag."""
        context = req.environ['cinder.context']
        # Not found exception will be handled at the wsgi level
        volume = self.volume_api.get(context, id)

        try:
            readonly_flag = body['os-update_readonly_flag']['readonly']
        except KeyError:
            msg = _("Must specify readonly in request.")
            raise webob.exc.HTTPBadRequest(explanation=msg)

        try:
            readonly_flag = strutils.bool_from_string(readonly_flag,
                                                      strict=True)
        except ValueError as error:
            err_msg = encodeutils.exception_to_unicode(error)
            msg = _("Invalid value for 'readonly': '%s'") % err_msg
            raise webob.exc.HTTPBadRequest(explanation=msg)

        self.volume_api.update_readonly_flag(context, volume, readonly_flag)
Exemplo n.º 57
0
    def configure(self, re_raise_bsc=False):
        """
        Configure the store to use the stored configuration options
        and initialize capabilities based on current configuration.

        Any store that needs special configuration should implement
        this method.
        """

        try:
            self.configure_add()
        except exceptions.BadStoreConfiguration as e:
            self.unset_capabilities(capabilities.BitMasks.WRITE_ACCESS)
            msg = (_(u"Failed to configure store correctly: %s "
                     "Disabling add method.") %
                   encodeutils.exception_to_unicode(e))
            LOG.warning(msg)
            if re_raise_bsc:
                raise
        finally:
            self.update_capabilities()
Exemplo n.º 58
0
    def delete(self, req, image_id, id):
        """
        Removes a membership from the image.
        """
        self._check_can_access_image_members(req.context)
        self._enforce(req, 'delete_member')
        self._raise_404_if_image_deleted(req, image_id)

        try:
            registry.delete_member(req.context, image_id, id)
            self._update_store_acls(req, image_id)
        except exception.NotFound as e:
            LOG.debug(encodeutils.exception_to_unicode(e))
            raise webob.exc.HTTPNotFound(explanation=e.msg)
        except exception.Forbidden as e:
            LOG.debug(
                "User not permitted to remove membership from image "
                "'%s'", image_id)
            raise webob.exc.HTTPNotFound(explanation=e.msg)

        return webob.exc.HTTPNoContent()
Exemplo n.º 59
0
def safe_delete_from_backend(context, image_id, location):
    """
    Given a location, delete an image from the store and
    update location status to db.

    This function try to handle all known exceptions which might be raised
    by those calls on store and DB modules in its implementation.

    :param context: The request context
    :param image_id: The image identifier
    :param location: The image location entry
    """

    try:
        if CONF.enabled_backends:
            backend = location['metadata'].get('store')
            ret = store_api.delete(location['url'],
                                   backend,
                                   context=context)
        else:
            ret = store_api.delete_from_backend(location['url'],
                                                context=context)

        location['status'] = 'deleted'
        if 'id' in location:
            db_api.get_api().image_location_delete(context, image_id,
                                                   location['id'], 'deleted')
        return ret
    except store_api.NotFound:
        msg = ("The image data for %(iid)s was not found in the store. "
               "The image record has been updated to reflect "
               "this." % {'iid': image_id})
        LOG.warn(msg)
    except store_api.StoreDeleteNotSupported as e:
        LOG.warn(encodeutils.exception_to_unicode(e))
    except store_api.UnsupportedBackend:
        exc_type = sys.exc_info()[0].__name__
        msg = (_LE('Failed to delete image %(image_id)s from store: %(exc)s') %
               dict(image_id=image_id, exc=exc_type))
        LOG.error(msg)
Exemplo n.º 60
0
    def _run(self, task_id, task_type):
        LOG.debug(
            'Taskflow executor picked up the execution of task ID '
            '%(task_id)s of task type '
            '%(task_type)s', {
                'task_id': task_id,
                'task_type': task_type
            })

        task = script_utils.get_task(self.task_repo, task_id)
        if task is None:
            # NOTE: This happens if task is not found in the database. In
            # such cases, there is no way to update the task status so,
            # it's ignored here.
            return

        flow = self._get_flow(task)
        executor = self._fetch_an_executor()
        try:
            engine = engines.load(
                flow,
                engine=CONF.taskflow_executor.engine_mode,
                executor=executor,
                max_workers=CONF.taskflow_executor.max_workers)
            with llistener.DynamicLoggingListener(engine, log=LOG):
                engine.run()
        except Exception as exc:
            with excutils.save_and_reraise_exception():
                LOG.error(
                    _LE('Failed to execute task %(task_id)s: %(exc)s') % {
                        'task_id': task_id,
                        'exc': encodeutils.exception_to_unicode(exc)
                    })
                # TODO(sabari): Check for specific exceptions and update the
                # task failure message.
                task.fail(_('Task failed due to Internal Error'))
                self.task_repo.save(task)
        finally:
            if executor is not None:
                executor.shutdown()