示例#1
0
    def process_exception(self, request, exception):
        if isinstance(exception, PulpException):
            status = exception.http_status_code
            response = serialization.error.http_error_obj(status, str(exception))
            response.update(exception.data_dict())
            response['error'] = exception.to_dict()
            logger.info(str(exception))
            response_obj = HttpResponse(json.dumps(response), status=status,
                                        content_type="application/json")
        else:
            status = httplib.INTERNAL_SERVER_ERROR
            response = serialization.error.http_error_obj(status, str(exception))
            msg = _('Unhandled Exception')
            logger.error(msg)

        if status == httplib.INTERNAL_SERVER_ERROR:
            logger.exception(str(exception))
            e_type, e_value, trace = sys.exc_info()
            response['exception'] = traceback.format_exception_only(e_type, e_value)
            response['traceback'] = traceback.format_tb(trace)
            response_obj = HttpResponseServerError(json.dumps(response),
                                                   content_type="application/json")
        else:
            logger.info(str(exception))

        response_obj['Content-Encoding'] = 'utf-8'
        return response_obj
示例#2
0
 def process_exception(self, request, exception):
     if isinstance(exception, PulpException):
         status = exception.http_status_code
         response = serialization.error.http_error_obj(
             status, str(exception))
         response.update(exception.data_dict())
         response['error'] = exception.to_dict()
         logger.info(str(exception))
         response_obj = HttpResponse(json.dumps(response),
                                     status=status,
                                     content_type="application/json")
     else:
         status = httplib.INTERNAL_SERVER_ERROR
         response = serialization.error.http_error_obj(
             status, str(exception))
         msg = _('Unhandled Exception')
         logger.error(msg)
         logger.exception(str(exception))
         e_type, e_value, trace = sys.exc_info()
         response['exception'] = traceback.format_exception_only(
             e_type, e_value)
         response['traceback'] = traceback.format_tb(trace)
         response_obj = HttpResponseServerError(
             json.dumps(response), content_type="application/json")
     response_obj['Content-Encoding'] = 'utf-8'
     return response_obj
示例#3
0
    def __call__(self, environ, start_response):
        try:
            return self.app(environ, start_response)
        except Exception, e:
            if isinstance(e, PulpException):
                status = e.http_status_code
                response = serialization.error.http_error_obj(status, str(e))
                response.update(e.data_dict())
                response['error'] = e.to_dict()
            else:
                # If it's not a Pulp exception, return a 500
                msg = _('Unhandled Exception')
                logger.error(msg)
                status = httplib.INTERNAL_SERVER_ERROR
                response = serialization.error.http_error_obj(status, str(e))

            if status == httplib.INTERNAL_SERVER_ERROR or self.debug:
                logger.exception(str(e))
                e_type, e_value, trace = sys.exc_info()
                response['exception'] = traceback.format_exception_only(
                    e_type, e_value)
                response['traceback'] = traceback.format_tb(trace)
            else:
                logger.info(str(e))

            serialized_response = json.dumps(response)
            self.headers['Content-Length'] = str(len(serialized_response))
            status_str = '%d %s' % (status, http_responses[status])
            start_response(status_str,
                           [(k, v) for k, v in self.headers.items()])
            return [serialized_response]
示例#4
0
文件: exception.py 项目: ashcrow/pulp
 def __call__(self, environ, start_response):
     try:
         return self.app(environ, start_response)
     except Exception, e:
         _LOG.exception(e)
         t, e, tb = sys.exc_info()
         status = None
         error_obj = None
         record_exception_and_traceback = self.debug
         if isinstance(e, PulpException):
             status = e.http_status_code
             if type(e) == PulpException:
                 # un-derived exceptions earn you a traceback
                 record_exception_and_traceback = True
             error_obj = serialization.error.http_error_obj(status, str(e))
             error_obj.update(e.data_dict())
             if record_exception_and_traceback:
                 error_obj['exception'] = traceback.format_exception_only(t, e)
                 error_obj['traceback'] = traceback.format_tb(tb)
         else:
             msg = _('Unhandled Exception')
             _LOG.exception(msg)
             status = httplib.INTERNAL_SERVER_ERROR
             error_obj = serialization.error.exception_obj(e, tb, msg)
         serialized_error = json.dumps(error_obj)
         self.headers['Content-Length'] = str(len(serialized_error))
         status_str = '%d %s' % (status, http_responses[status])
         start_response(status_str, [(k, v) for k, v in self.headers.items()])
         return [serialized_error]
示例#5
0
 def _write_file_footer(self):
     """
     Write out the end of the json file
     """
     self.metadata_file_handle.write('],"tags":')
     self.metadata_file_handle.write(json.dumps(self.tags))
     self.metadata_file_handle.write('}')
示例#6
0
 def __call__(self, environ, start_response):
     try:
         return self.app(environ, start_response)
     except Exception, e:
         _LOG.exception(e)
         t, e, tb = sys.exc_info()
         status = None
         error_obj = None
         record_exception_and_traceback = self.debug
         if isinstance(e, PulpException):
             status = e.http_status_code
             if type(e) == PulpException:
                 # un-derived exceptions earn you a traceback
                 record_exception_and_traceback = True
             error_obj = serialization.error.http_error_obj(status, str(e))
             error_obj.update(e.data_dict())
             if record_exception_and_traceback:
                 error_obj['exception'] = traceback.format_exception_only(
                     t, e)
                 error_obj['traceback'] = traceback.format_tb(tb)
         else:
             msg = _('Unhandled Exception')
             _LOG.exception(msg)
             status = httplib.INTERNAL_SERVER_ERROR
             error_obj = serialization.error.exception_obj(e, tb, msg)
         serialized_error = json.dumps(error_obj)
         self.headers['Content-Length'] = str(len(serialized_error))
         status_str = '%d %s' % (status, http_responses[status])
         start_response(status_str,
                        [(k, v) for k, v in self.headers.items()])
         return [serialized_error]
示例#7
0
    def __call__(self, environ, start_response):
        try:
            return self.app(environ, start_response)
        except Exception, e:
            if isinstance(e, PulpException):
                status = e.http_status_code
                response = serialization.error.http_error_obj(status, str(e))
                response.update(e.data_dict())
                response['error'] = e.to_dict()
            else:
                # If it's not a Pulp exception, return a 500
                msg = _('Unhandled Exception')
                logger.error(msg)
                status = httplib.INTERNAL_SERVER_ERROR
                response = serialization.error.http_error_obj(status, str(e))

            if status == httplib.INTERNAL_SERVER_ERROR or self.debug:
                logger.exception(str(e))
                e_type, e_value, trace = sys.exc_info()
                response['exception'] = traceback.format_exception_only(e_type, e_value)
                response['traceback'] = traceback.format_tb(trace)
            else:
                logger.info(str(e))

            serialized_response = json.dumps(response)
            self.headers['Content-Length'] = str(len(serialized_response))
            status_str = '%d %s' % (status, http_responses[status])
            start_response(status_str, [(k, v) for k, v in self.headers.items()])
            return [serialized_response]
示例#8
0
def handle_event(notifier_config, event):
    """
    If email is enabled in the server settings, sends an email to each recipient
    listed in the notifier_config.

    :param notifier_config: dictionary with keys 'subject', which defines the
                            subject of each email message, and 'addresses',
                            which is a list of strings that are email addresses
                            that should receive this notification.
    :type  notifier_config: dict
    :param event:   Event instance
    :type  event:   pulp.server.event.data.event
    :return: None
    """
    if not config.getboolean('email', 'enabled'):
        return
    body = json.dumps(event.data(), indent=2, default=json_util.default)
    subject = notifier_config['subject']
    addresses = notifier_config['addresses']

    for address in addresses:
        thread = threading.Thread(target=_send_email,
                                  args=(subject, body, address))
        thread.daemon = True
        thread.start()
示例#9
0
 def _output(self, data):
     """
     JSON encode the response and set the appropriate headers
     """
     body = json.dumps(data, default=json_encoder)
     http.header('Content-Type', 'application/json')
     http.header('Content-Length', len(body))
     return body
示例#10
0
文件: base.py 项目: pombreda/pulp
 def _output(self, data):
     """
     JSON encode the response and set the appropriate headers
     """
     body = json.dumps(data, default=json_encoder)
     http.header('Content-Type', 'application/json')
     http.header('Content-Length', len(body))
     return body
示例#11
0
文件: http.py 项目: BrnoPCmaniak/pulp
def handle_event(notifier_config, event):
    # fire the actual http push function off in a separate thread to keep
    # pulp from blocking or deadlocking due to the tasking subsystem
    json_body = json.dumps(event.data(), default=json_util.default)
    _logger.info(json_body)
    thread = threading.Thread(target=_send_post, args=[notifier_config, json_body])
    thread.setDaemon(True)
    thread.start()
示例#12
0
文件: url.py 项目: maxamillion/pulp
    def encode(thing):
        """
        Encode the specified object.

        :param thing: A python object.
        :return: The json string.
        :rtype: str
        """
        return json.dumps(thing)
示例#13
0
    def encode(thing):
        """
        Encode the specified object.

        :param thing: A python object.
        :return: The json string.
        :rtype: str
        """
        return json.dumps(thing)
示例#14
0
def handle_event(notifier_config, event):
    # fire the actual http push function off in a separate thread to keep
    # pulp from blocking or deadlocking due to the tasking subsystem
    json_body = json.dumps(event.data(), default=json_util.default)
    _logger.info(json_body)
    thread = threading.Thread(target=_send_post,
                              args=[notifier_config, json_body])
    thread.setDaemon(True)
    thread.start()
示例#15
0
文件: postponed.py 项目: beav/pulp
def _get_operation_postponed_body(exception):
    report = exception.call_report
    if isinstance(exception.call_report, AsyncResult):
        report = TaskResult.from_async_result(exception.call_report)
    serialized_call_report = report.serialize()
    for task in serialized_call_report['spawned_tasks']:
        href_obj = serialization.dispatch.task_result_href(task)
        task.update(href_obj)

    return json.dumps(serialized_call_report, default=json_util.default)
示例#16
0
    def _get_operation_postponed_body(exception):
        report = exception.call_report
        if isinstance(exception.call_report, AsyncResult):
            report = TaskResult.from_async_result(exception.call_report)
        serialized_call_report = report.serialize()
        for task in serialized_call_report['spawned_tasks']:
            href_obj = dispatch.task_result_href(task)
            task.update(href_obj)

        return json.dumps(serialized_call_report, default=json_util.default)
示例#17
0
文件: exception.py 项目: alanoe/pulp
    def process_exception(self, request, exception):
        """
        Process the catched exception.

        :param request: WSGI request object
        :type request: django.core.handlers.wsgi.WSGIRequest
        :param exception: unhandled exception
        :type exception: Exception

        :return: Response containing processed exception
        :rtype: django.http.HttpResponseServerError
        """

        try:
            if isinstance(exception, PulpException):
                status = exception.http_status_code
                response = error.http_error_obj(status, str(exception))
                response.update(exception.data_dict())
                response['error'] = exception.to_dict()
                logger.info(str(exception))
                response_obj = HttpResponse(json.dumps(response), status=status,
                                            content_type="application/json; charset=utf-8")
            else:
                status = httplib.INTERNAL_SERVER_ERROR
                response = error.http_error_obj(status, str(exception))
                msg = _('Unhandled Exception')
                logger.error(msg)

            if status == httplib.INTERNAL_SERVER_ERROR:
                logger.exception(str(exception))
                e_type, e_value, trace = sys.exc_info()
                response['exception'] = traceback.format_exception_only(e_type, e_value)
                response['traceback'] = traceback.format_tb(trace)
                response_obj = HttpResponseServerError(
                    json.dumps(response), content_type="application/json; charset=utf-8")

            response_obj['Content-Encoding'] = 'utf-8'
            return response_obj
        # Because this defines our own exception handling, if something goes wrong we need to make
        # sure that it gets logged.
        except Exception, e:
            logger.exception(e)
            raise
示例#18
0
    def process_exception(self, request, exception):
        """
        Process the catched exception.

        :param request: WSGI request object
        :type request: django.core.handlers.wsgi.WSGIRequest
        :param exception: unhandled exception
        :type exception: Exception

        :return: Response containing processed exception
        :rtype: django.http.HttpResponseServerError
        """

        try:
            if isinstance(exception, PulpException):
                status = exception.http_status_code
                response = error.http_error_obj(status, str(exception))
                response.update(exception.data_dict())
                response['error'] = exception.to_dict()
                logger.info(str(exception))
                response_obj = HttpResponse(json.dumps(response), status=status,
                                            content_type="application/json; charset=utf-8")
            else:
                status = httplib.INTERNAL_SERVER_ERROR
                response = error.http_error_obj(status, str(exception))
                msg = _('Unhandled Exception')
                logger.error(msg)

            if status == httplib.INTERNAL_SERVER_ERROR:
                logger.exception(str(exception))
                e_type, e_value, trace = sys.exc_info()
                response['exception'] = traceback.format_exception_only(e_type, e_value)
                response['traceback'] = traceback.format_tb(trace)
                response_obj = HttpResponseServerError(
                    json.dumps(response), content_type="application/json; charset=utf-8")

            return response_obj
        # Because this defines our own exception handling, if something goes wrong we need to make
        # sure that it gets logged.
        except Exception, e:
            logger.exception(e)
            raise
示例#19
0
    def process_exception(self, request, exception):
        """
        Process the catched exception.

        :param request: WSGI request object
        :type request: django.core.handlers.wsgi.WSGIRequest
        :param exception: unhandled exception
        :type exception: Exception

        :return: Response containing processed exception
        :rtype: django.http.HttpResponseServerError
        """

        if isinstance(exception, PulpException):
            status = exception.http_status_code
            response = error.http_error_obj(status, str(exception))
            response.update(exception.data_dict())
            response['error'] = exception.to_dict()
            logger.info(str(exception))
            response_obj = HttpResponse(json.dumps(response),
                                        status=status,
                                        content_type="application/json")
        else:
            status = httplib.INTERNAL_SERVER_ERROR
            response = error.http_error_obj(status, str(exception))
            msg = _('Unhandled Exception')
            logger.error(msg)

        if status == httplib.INTERNAL_SERVER_ERROR:
            logger.exception(str(exception))
            e_type, e_value, trace = sys.exc_info()
            response['exception'] = traceback.format_exception_only(
                e_type, e_value)
            response['traceback'] = traceback.format_tb(trace)
            response_obj = HttpResponseServerError(
                json.dumps(response), content_type="application/json")
        else:
            logger.info(str(exception))

        response_obj['Content-Encoding'] = 'utf-8'
        return response_obj
示例#20
0
 def add(self, unit):
     """
     Add (write) the specified unit to the file as a json encoded string.
     :param unit: A content unit.
     :type unit: dict
     :raise IOError: on I/O errors.
     :raise ValueError: json encoding errors
     """
     self.total_units += 1
     json_unit = json.dumps(unit)
     self.fp.write(json_unit)
     self.fp.write('\n')
示例#21
0
 def __call__(self, environ, start_response):
     try:
         return self.app(environ, start_response)
     except OperationPostponed, e:
         serialized_call_report = e.call_report.serialize()
         href_obj = serialization.dispatch.task_href(e.call_report)
         serialized_call_report.update(href_obj)
         body = json.dumps(serialized_call_report)
         self.headers['Content-Length'] = str(len(body))
         start_str = '%d %s' % (e.http_status_code, http_responses[e.http_status_code])
         start_response(start_str, [(k, v) for k, v in self.headers.items()])
         return [body]
示例#22
0
    def add_unit_metadata(self, unit):
        """
        Add the specific metadata for this unit

        :param unit: The docker unit to add to the images metadata file
        :type unit: pulp.plugins.model.AssociatedUnit
        """
        super(RedirectFileContext, self).add_unit_metadata(unit)
        image_id = unit.unit_key['image_id']
        unit_data = {'id': image_id}
        string_representation = json.dumps(unit_data)
        self.metadata_file_handle.write(string_representation)
示例#23
0
class PostponedOperationMiddleware(object):
    """
    Catch OperationPostponed exceptions and return an HTTP Accepted response
    along with the proper serialization of the asynchronous call information.
    """
    def __init__(self, app):
        self.app = app
        self.headers = {
            'Content-Encoding': 'utf-8',
            'Content-Type': 'application/json',
            'Content-Length': '-1'
        }

    def __call__(self, environ, start_response):

        try:
            return self.app(environ, start_response)

        except OperationPostponed, e:
            serialized_call_report = e.call_report.serialize()
            href_obj = serialization.dispatch.task_href(e.call_report)
            serialized_call_report.update(href_obj)

            body = json.dumps(serialized_call_report,
                              default=json_util.default)

            self.headers['Content-Length'] = str(len(body))
            start_str = '%d %s' % (e.http_status_code,
                                   http_responses[e.http_status_code])

            start_response(start_str,
                           [(k, v) for k, v in self.headers.items()])
            return [body]

        except MultipleOperationsPostponed, e:
            serialized_call_report_list = []

            for call_report in e.call_report_list:
                href_obj = serialization.dispatch.task_group_href(call_report)
                serialized_call_report = call_report.serialize()
                serialized_call_report.update(href_obj)
                serialized_call_report_list.append(serialized_call_report)

            body = json.dumps(serialized_call_report_list,
                              default=json_util.default)

            self.headers['Content-Length'] = str(len(body))
            start_str = '%d %s' % (e.http_status_code,
                                   http_responses[e.http_status_code])

            start_response(start_str,
                           [(k, v) for k, v in self.headers.items()])
            return [body]
示例#24
0
 def add(self, unit):
     """
     Add (write) the specified unit to the file as a json encoded string.
     :param unit: A content unit.
     :type unit: dict
     :raise IOError: on I/O errors.
     :raise ValueError: json encoding errors
     """
     self.total_units += 1
     json_unit = json.dumps(unit)
     self.fp.write(json_unit)
     self.fp.write('\n')
示例#25
0
    def add_unit_metadata(self, unit):
        """
        Add the specific metadata for this unit

        :param unit: The docker unit to add to the images metadata file
        :type unit: pulp_docker.plugins.db.models.DockerImage
        """
        super(RedirectFileContext, self).add_unit_metadata(unit)
        unit_data = {
            'id': unit.image_id
        }
        string_representation = json.dumps(unit_data)
        self.metadata_file_handle.write(string_representation)
示例#26
0
文件: http.py 项目: ashcrow/pulp
def handle_event(notifier_config, event):
    # fire the actual http push function off in a separate thread to keep
    # pulp from blocking or deadlocking due to the tasking subsystem

    data = event.data()

    LOG.info(data)

    body = json.dumps(data)

    thread = threading.Thread(target=_send_post, args=[notifier_config, body])
    thread.setDaemon(True)
    thread.start()
示例#27
0
文件: rest.py 项目: tomlanyon/pulp
 def request(self, method, path, body=None):
     try:
         body = json.dumps(body, default=json_util.default)
     except:
         pass
     self.http.request(method, path, body=body, headers=self.auth.header())
     response = self.http.getresponse()
     body = response.read()
     try:
         body = json.loads(body)
     except:
         pass
     return (response.status, body)
示例#28
0
文件: http.py 项目: tomlanyon/pulp
def handle_event(notifier_config, event):
    # fire the actual http push function off in a separate thread to keep
    # pulp from blocking or deadlocking due to the tasking subsystem

    data = event.data()

    LOG.info(data)

    body = json.dumps(data)

    thread = threading.Thread(target=_send_post, args=[notifier_config, body])
    thread.setDaemon(True)
    thread.start()
示例#29
0
    def add_unit_metadata(self, unit):
        """
        Add the specific metadata for this unit

        :param unit: The docker unit to add to the images metadata file
        :type unit: pulp.plugins.model.AssociatedUnit
        """
        super(RedirectFileContext, self).add_unit_metadata(unit)
        image_id = unit.unit_key['image_id']
        unit_data = {
            'id': image_id
        }
        string_representation = json.dumps(unit_data)
        self.metadata_file_handle.write(string_representation)
示例#30
0
    def _get_operation_postponed_body(exception):
        report = exception.call_report
        if isinstance(exception.call_report, AsyncResult):
            report = TaskResult.from_async_result(exception.call_report)
        serialized_call_report = report.serialize()
        for task in serialized_call_report['spawned_tasks']:
            href_obj = dispatch.task_result_href(task)
            task.update(href_obj)

        # Use the object's serializer if it is a Mongoengine Document.
        result = serialized_call_report.get('result')
        if hasattr(result, 'serializer'):
            serialized_call_report['result'] = result.serializer(result).data

        return json.dumps(serialized_call_report, default=json_util.default)
示例#31
0
    def _get_operation_postponed_body(exception):
        report = exception.call_report
        if isinstance(exception.call_report, AsyncResult):
            report = TaskResult.from_async_result(exception.call_report)
        serialized_call_report = report.serialize()
        if 'spawned_tasks' in serialized_call_report:
            for task in serialized_call_report['spawned_tasks']:
                href_obj = dispatch.task_result_href(task)
                task.update(href_obj)

        # Use the object's serializer if it is a Mongoengine Document.
        result = serialized_call_report.get('result')
        if hasattr(result, 'serializer'):
            serialized_call_report['result'] = result.serializer(result).data

        return json.dumps(serialized_call_report, default=json_util.default)
示例#32
0
文件: rest.py 项目: bartwo/pulp
 def request(self, method, path, body=None):
     try:
         body = json.dumps(body, default=json_util.default)
     except:
         pass
     self.http.request(
         method,
         path,
         body=body,
         headers=self.auth.header())
     response = self.http.getresponse()
     body = response.read()
     try:
         body = json.loads(body)
     except:
         pass
     return (response.status, body)
示例#33
0
    def __call__(self, environ, start_response):

        try:
            return self.app(environ, start_response)

        except OperationPostponed, e:
            serialized_call_report = e.call_report.serialize()
            href_obj = serialization.dispatch.task_href(e.call_report)
            serialized_call_report.update(href_obj)

            body = json.dumps(serialized_call_report,
                              default=json_util.default)

            self.headers['Content-Length'] = str(len(body))
            start_str = '%d %s' % (e.http_status_code,
                                   http_responses[e.http_status_code])

            start_response(start_str,
                           [(k, v) for k, v in self.headers.items()])
            return [body]
示例#34
0
文件: postponed.py 项目: omps/pulp
    def __call__(self, environ, start_response):

        try:
            return self.app(environ, start_response)

        except OperationPostponed, e:
            report = e.call_report
            if isinstance(e.call_report, AsyncResult):
                report = TaskResult.from_async_result(e.call_report)
            serialized_call_report = report.serialize()
            for task in serialized_call_report['spawned_tasks']:
                href_obj = serialization.dispatch.task_result_href(task)
                task.update(href_obj)

            body = json.dumps(serialized_call_report, default=json_util.default)

            self.headers['Content-Length'] = str(len(body))
            start_str = '%d %s' % (e.http_status_code, http_responses[e.http_status_code])

            start_response(start_str, [(k, v) for k, v in self.headers.items()])
            return [body]
示例#35
0
文件: mail.py 项目: ehelms/pulp
def handle_event(notifier_config, event):
    """
    If email is enabled in the server settings, sends an email to each recipient
    listed in the notifier_config.

    :param notifier_config: dictionary with keys 'subject', which defines the
                            subject of each email message, and 'addresses',
                            which is a list of strings that are email addresses
                            that should receive this notification.
    :type  notifier_config: dict
    :param event:   Event instance
    :type  event:   pulp.server.event.data.event
    :return: None
    """
    if not config.getboolean('email', 'enabled'):
        return
    body = json.dumps(event.payload, indent=2)
    subject = notifier_config['subject']
    addresses = notifier_config['addresses']

    for address in addresses:
        thread = threading.Thread(target=_send_email, args=(subject, body, address))
        thread.daemon = True
        thread.start()
示例#36
0
文件: rest.py 项目: bartwo/pulp
 def __body(self, body):
     if body is None:
         return
     else:
         return json.dumps(body)
示例#37
0
文件: rest.py 项目: tomlanyon/pulp
 def __body(self, body):
     if body is None:
         return
     else:
         return json.dumps(body)
示例#38
0
文件: http.py 项目: taftsanders/pulp
def handle_event(notifier_config, event):
    # fire the actual http push function off in a separate thread to keep
    # pulp from blocking or deadlocking due to the tasking subsystem
    json_body = json.dumps(event.data(), default=json_util.default)
    _logger.info(json_body)
    _send_post(notifier_config, json_body)