def run(self):
        try:
            self._run()
        except:
            typ, value, tb = sys.exc_info()
            out = StringIO.StringIO()
            formatTrace(typ, value, tb, stream = out, withLocals = False)
            out.write("\nFull stack:\n")
            formatTrace(typ, value, tb, stream = out, withLocals = True)

            self.sendStatus(C.ERR_GENERIC,
                "Error in Interface Detection call: %s"
                    % str(value), out.getvalue())
Example #2
0
def logErrorAndEmail(req, cfg, exception, e, bt):
    timeStamp = time.ctime(time.time())

    header = 'Unhandled exception from conary repository: %s\n%s: %s\n' % (
        req.hostname, exception.__name__, e)

    # Nicely format the exception
    tbFd, tbPath = tempfile.mkstemp('.txt', 'repos-error-')
    out = os.fdopen(tbFd, 'w+')
    formatTrace(exception, e, bt, stream = out, withLocals = False)
    out.write("\nFull stack:\n")
    formatTrace(exception, e, bt, stream = out, withLocals = True)
    out.seek(0)
    msg = out.read()

    logAndEmail(req, cfg, header, msg)
    # log error
    lines = traceback.format_exception(*sys.exc_info())
    lines.insert(0, "Unhandled exception from conary repository:\n")
    lines.append('Extended traceback at %s\n' % (tbPath,))
    log.error(''.join(lines))
Example #3
0
def logErrorAndEmail(req, cfg, exception, e, bt):
    timeStamp = time.ctime(time.time())

    header = 'Unhandled exception from conary repository: %s\n%s: %s\n' % (
        req.hostname, exception.__name__, e)

    # Nicely format the exception
    tbFd, tbPath = tempfile.mkstemp('.txt', 'repos-error-')
    out = os.fdopen(tbFd, 'w+')
    formatTrace(exception, e, bt, stream=out, withLocals=False)
    out.write("\nFull stack:\n")
    formatTrace(exception, e, bt, stream=out, withLocals=True)
    out.seek(0)
    msg = out.read()

    logAndEmail(req, cfg, header, msg)
    # log error
    lines = traceback.format_exception(*sys.exc_info())
    lines.insert(0, "Unhandled exception from conary repository:\n")
    lines.append('Extended traceback at %s\n' % (tbPath, ))
    log.error(''.join(lines))
Example #4
0
    def _formatErrorLarge(self, request, exc_info):
        e_class, e_value, e_tb = exc_info
        timestamp = time.ctime(time.time())

        # Format large traceback to file
        fd, tbPath = tempfile.mkstemp('.txt', 'repos-error-')
        tb = os.fdopen(fd, 'w+')
        print >> tb, "Unhandled exception from Conary repository", request.host
        print >> tb, "Time of occurence:", timestamp
        print >> tb, "System hostname:", socket.gethostname()
        print >> tb, "See also:", tbPath
        print >> tb
        formatTrace(e_class, e_value, e_tb, stream=tb, withLocals=False)
        print >> tb
        print >> tb, "WSGI Environment:"
        for key, value in sorted(request.environ.items()):
            print >> tb, " %s = %r" % (key, value)
        print >> tb
        print >> tb, "Full trace:"
        try:
            formatTrace(e_class, e_value, e_tb, stream=tb, withLocals=True)
        except:
            print >> tb, "*** Traceback formatter crashed! ***"
            print >> tb, "Formatter crash follows:"
            new_exc = sys.exc_info()
            formatTrace(new_exc[0], new_exc[1], new_exc[2], stream=tb,
                    withLocals=False)
            print >> tb, "*** End formatter crash log ***"
        print >> tb
        print >> tb, "End of traceback report"
        tb.seek(0)
        contents = tb.read()
        tb.close()
        return contents, tbPath
Example #5
0
    def _formatErrorLarge(self, request, exc_info):
        e_class, e_value, e_tb = exc_info
        timestamp = time.ctime(time.time())

        # Format large traceback to file
        fd, tbPath = tempfile.mkstemp('.txt', 'repos-error-')
        tb = os.fdopen(fd, 'w+')
        print >> tb, "Unhandled exception from Conary repository", request.host
        print >> tb, "Time of occurence:", timestamp
        print >> tb, "System hostname:", socket.gethostname()
        print >> tb, "See also:", tbPath
        print >> tb
        formatTrace(e_class, e_value, e_tb, stream=tb, withLocals=False)
        print >> tb
        print >> tb, "WSGI Environment:"
        for key, value in sorted(request.environ.items()):
            print >> tb, " %s = %r" % (key, value)
        print >> tb
        print >> tb, "Full trace:"
        try:
            formatTrace(e_class, e_value, e_tb, stream=tb, withLocals=True)
        except:
            print >> tb, "*** Traceback formatter crashed! ***"
            print >> tb, "Formatter crash follows:"
            new_exc = sys.exc_info()
            formatTrace(new_exc[0],
                        new_exc[1],
                        new_exc[2],
                        stream=tb,
                        withLocals=False)
            print >> tb, "*** End formatter crash log ***"
        print >> tb
        print >> tb, "End of traceback report"
        tb.seek(0)
        contents = tb.read()
        tb.close()
        return contents, tbPath
Example #6
0
 def _formatErrorSmall(self, request, exc_info):
     e_class, e_value, e_tb = exc_info
     tb = StringIO.StringIO()
     print >> tb, "Unhandled exception from Conary repository", request.host
     formatTrace(e_class, e_value, e_tb, stream=tb, withLocals=False)
     return tb.getvalue()
Example #7
0
    def handleXml(self, authToken):
        contentLength = int(self.headers['Content-Length'])
        sio = util.BoundedStringIO()

        actual = util.copyStream(self.rfile, sio, contentLength)
        if contentLength != actual:
            raise Exception(contentLength, actual)

        sio.seek(0)

        encoding = self.headers.get('Content-Encoding', None)
        if encoding == 'deflate':
            sio = util.decompressStream(sio)
            sio.seek(0)

        (params, method) = util.xmlrpcLoad(sio)
        logMe(
            3, "decoded xml-rpc call %s from %d bytes request" %
            (method, contentLength))

        if self.netProxy:
            repos = self.netProxy
        else:
            repos = self.netRepos

        localHost, localPort = self.request.getsockname()[:2]
        if ':' in localHost:
            localHost = '[%s]' % localHost
        localAddr = '%s:%s' % (localHost, localPort)
        request = xmlshims.RequestArgs.fromWire(params)

        if repos is not None:
            try:
                response, extraInfo = repos.callWrapper(
                    protocol='http',
                    port=None,
                    methodname=method,
                    authToken=authToken,
                    request=request,
                    remoteIp=self.connection.getpeername()[0],
                    rawUrl=self.path,
                    localAddr=localAddr,
                    protocolString=self.request_version,
                    headers=self.headers,
                    isSecure=self.server.isSecure,
                )
            except errors.InsufficientPermission:
                self.send_error(403)
                return None
            except:
                # exceptions are handled (logged) in callWrapper - send
                # 500 code back to the client to indicate an error happened
                self.send_error(500)
                from conary.lib import formattrace
                excType, excValue, excTb = sys.exc_info()
                formattrace.formatTrace(excType,
                                        excValue,
                                        excTb,
                                        withLocals=False)
                return None
            logMe(3, "returned from", method)

        rawResponse, headers = response.toWire(request.version)

        sio = util.BoundedStringIO()
        util.xmlrpcDump((rawResponse, ), stream=sio, methodresponse=1)
        respLen = sio.tell()
        logMe(3, "encoded xml-rpc response to %d bytes" % respLen)

        self.send_response(200)
        encoding = self.headers.get('Accept-encoding', '')
        if respLen > 200 and 'deflate' in encoding:
            sio.seek(0)
            sio = util.compressStream(sio, level=5)
            respLen = sio.tell()
            self.send_header('Content-encoding', 'deflate')
        self.send_header("Content-type", "text/xml")
        self.send_header("Content-length", str(respLen))
        for key, value in sorted(headers.items()):
            self.send_header(key, value)
        if extraInfo:
            # If available, send to the client the via headers all the way up
            # to us
            self.send_header(
                'Via',
                proxy.formatViaHeader(localAddr,
                                      'HTTP/1.0',
                                      prefix=extraInfo.getVia()))

        self.end_headers()
        sio.seek(0)
        util.copyStream(sio, self.wfile)
        logMe(3, "sent response to client", respLen, "bytes")
        return respLen
Example #8
0
    def handleXml(self, authToken):
        contentLength = int(self.headers['Content-Length'])
        sio = util.BoundedStringIO()

        actual = util.copyStream(self.rfile, sio, contentLength)
        if contentLength != actual:
            raise Exception(contentLength, actual)

        sio.seek(0)

        encoding = self.headers.get('Content-Encoding', None)
        if encoding == 'deflate':
            sio = util.decompressStream(sio)
            sio.seek(0)

        (params, method) = util.xmlrpcLoad(sio)
        logMe(3, "decoded xml-rpc call %s from %d bytes request" %(method, contentLength))

        if self.netProxy:
            repos = self.netProxy
        else:
            repos = self.netRepos

        localHost, localPort = self.request.getsockname()[:2]
        if ':' in localHost:
            localHost = '[%s]' % localHost
        localAddr = '%s:%s' % (localHost, localPort)
        request = xmlshims.RequestArgs.fromWire(params)

        if repos is not None:
            try:
                response, extraInfo = repos.callWrapper(
                        protocol='http',
                        port=None,
                        methodname=method,
                        authToken=authToken,
                        request=request,
                        remoteIp=self.connection.getpeername()[0],
                        rawUrl=self.path,
                        localAddr=localAddr,
                        protocolString=self.request_version,
                        headers=self.headers,
                        isSecure=self.server.isSecure,
                        )
            except errors.InsufficientPermission:
                self.send_error(403)
                return None
            except:
                # exceptions are handled (logged) in callWrapper - send
                # 500 code back to the client to indicate an error happened
                self.send_error(500)
                from conary.lib import formattrace
                excType, excValue, excTb = sys.exc_info()
                formattrace.formatTrace(excType, excValue, excTb,
                    withLocals = False)
                return None
            logMe(3, "returned from", method)

        rawResponse, headers = response.toWire(request.version)

        sio = util.BoundedStringIO()
        util.xmlrpcDump((rawResponse,), stream = sio, methodresponse=1)
        respLen = sio.tell()
        logMe(3, "encoded xml-rpc response to %d bytes" % respLen)

        self.send_response(200)
        encoding = self.headers.get('Accept-encoding', '')
        if respLen > 200 and 'deflate' in encoding:
            sio.seek(0)
            sio = util.compressStream(sio, level = 5)
            respLen = sio.tell()
            self.send_header('Content-encoding', 'deflate')
        self.send_header("Content-type", "text/xml")
        self.send_header("Content-length", str(respLen))
        for key, value in sorted(headers.items()):
            self.send_header(key, value)
        if extraInfo:
            # If available, send to the client the via headers all the way up
            # to us
            self.send_header('Via', proxy.formatViaHeader(localAddr,
                'HTTP/1.0', prefix=extraInfo.getVia()))

        self.end_headers()
        sio.seek(0)
        util.copyStream(sio, self.wfile)
        logMe(3, "sent response to client", respLen, "bytes")
        return respLen
Example #9
0
 def _formatErrorSmall(self, request, exc_info):
     e_class, e_value, e_tb = exc_info
     tb = StringIO.StringIO()
     print >> tb, "Unhandled exception from Conary repository", request.host
     formatTrace(e_class, e_value, e_tb, stream=tb, withLocals=False)
     return tb.getvalue()
            self.sendStatus(C.ERR_AUTHENTICATION, errmsg)
        except BaseException, e:
            typ, value, tb = sys.exc_info()
            # no epdb on the rbuilder, this breaks stuff!
            #import epdb; epdb.post_mortem(tb, typ, value)
            if e.error:
                errmsg = e.error
            else:
                errmsg = "Error: %s" % e.__class__.__name__
            self.sendStatus(C.ERR_GENERIC, errmsg)
        except:
            typ, value, tb = sys.exc_info()
            # no epdb on the rbuilder, this breaks stuff!
            #import epdb; epdb.post_mortem(tb, typ, value)
            out = StringIO.StringIO()
            formatTrace(typ, value, tb, stream = out, withLocals = False)
            out.write("\nFull stack:\n")
            formatTrace(typ, value, tb, stream = out, withLocals = True)
            formatTrace(typ, value, tb, stream = sys.stderr, withLocals = True)

            log.error(out.getvalue())
            self.sendStatus(C.ERR_GENERIC, "Error: %s" % str(value),
                out.getvalue())

    @classmethod
    def _tempFile(cls, prefix, contents):
        # NamedTemporaryFile will conveniently go *poof* when it gets closed
        tmpf = tempfile.NamedTemporaryFile(dir=cls.TemporaryDir, prefix=prefix)
        tmpf.write(contents)
        # Flush the contents on the disk, so python's ssl lib can see them
        tmpf.flush()