~ubuntu-cloud-archive/ubuntu/precise/keystone/folsom

« back to all changes in this revision

Viewing changes to keystone/common/wsgi.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2012-03-26 13:41:45 UTC
  • mto: (35.1.2 keystone)
  • mto: This revision was merged to the branch mainline in revision 23.
  • Revision ID: package-import@ubuntu.com-20120326134145-rq0l7xymweuap1ir
Tags: upstream-2012.1~rc1
ImportĀ upstreamĀ versionĀ 2012.1~rc1

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
"""Utility methods for working with WSGI servers."""
22
22
 
23
23
import json
24
 
import logging
25
24
import sys
26
25
 
27
26
import eventlet
34
33
import webob.exc
35
34
 
36
35
from keystone import exception
 
36
from keystone.common import logging
37
37
from keystone.common import utils
38
38
 
39
39
 
 
40
LOG = logging.getLogger(__name__)
 
41
 
 
42
 
40
43
class WritableLogger(object):
41
44
    """A thin wrapper that responds to `write` and logs."""
42
45
 
61
64
 
62
65
    def start(self, key=None, backlog=128):
63
66
        """Run a WSGI server with the given application."""
64
 
        logging.debug('Starting %(arg0)s on %(host)s:%(port)s' %
 
67
        LOG.debug('Starting %(arg0)s on %(host)s:%(port)s' %
65
68
                      {'arg0': sys.argv[0],
66
69
                       'host': self.host,
67
70
                       'port': self.port})
83
86
 
84
87
    def _run(self, application, socket):
85
88
        """Start a WSGI server in a new green thread."""
86
 
        logger = logging.getLogger('eventlet.wsgi.server')
 
89
        log = logging.getLogger('eventlet.wsgi.server')
87
90
        eventlet.wsgi.server(socket, application, custom_pool=self.pool,
88
 
                             log=WritableLogger(logger))
 
91
                             log=WritableLogger(log))
89
92
 
90
93
 
91
94
class Request(webob.Request):
163
166
        arg_dict = req.environ['wsgiorg.routing_args'][1]
164
167
        action = arg_dict.pop('action')
165
168
        del arg_dict['controller']
166
 
        logging.debug('arg_dict: %s', arg_dict)
 
169
        LOG.debug('arg_dict: %s', arg_dict)
167
170
 
168
171
        # allow middleware up the stack to provide context & params
169
172
        context = req.environ.get('openstack.context', {})
180
183
        try:
181
184
            result = method(context, **params)
182
185
        except exception.Error as e:
183
 
            logging.warning(e)
 
186
            LOG.warning(e)
184
187
            return render_exception(e)
 
188
        except Exception as e:
 
189
            logging.exception(e)
 
190
            return render_exception(exception.UnexpectedError(exception=e))
185
191
 
186
192
        if result is None:
187
193
            return render_response(status=(204, 'No Content'))
304
310
 
305
311
    @webob.dec.wsgify(RequestClass=Request)
306
312
    def __call__(self, req):
307
 
        logging.debug('%s %s %s', ('*' * 20), 'REQUEST ENVIRON', ('*' * 20))
 
313
        LOG.debug('%s %s %s', ('*' * 20), 'REQUEST ENVIRON', ('*' * 20))
308
314
        for key, value in req.environ.items():
309
 
            logging.debug('%s = %s', key, value)
310
 
        logging.debug('')
311
 
        logging.debug('%s %s %s', ('*' * 20), 'REQUEST BODY', ('*' * 20))
 
315
            LOG.debug('%s = %s', key, value)
 
316
        LOG.debug('')
 
317
        LOG.debug('%s %s %s', ('*' * 20), 'REQUEST BODY', ('*' * 20))
312
318
        for line in req.body_file:
313
 
            logging.debug(line)
314
 
        logging.debug('')
 
319
            LOG.debug(line)
 
320
        LOG.debug('')
315
321
 
316
322
        resp = req.get_response(self.application)
317
323
 
318
 
        logging.debug('%s %s %s', ('*' * 20), 'RESPONSE HEADERS', ('*' * 20))
 
324
        LOG.debug('%s %s %s', ('*' * 20), 'RESPONSE HEADERS', ('*' * 20))
319
325
        for (key, value) in resp.headers.iteritems():
320
 
            logging.debug('%s = %s', key, value)
321
 
        logging.debug('')
 
326
            LOG.debug('%s = %s', key, value)
 
327
        LOG.debug('')
322
328
 
323
329
        resp.app_iter = self.print_generator(resp.app_iter)
324
330
 
327
333
    @staticmethod
328
334
    def print_generator(app_iter):
329
335
        """Iterator that prints the contents of a wrapper string."""
330
 
        logging.debug('%s %s %s', ('*' * 20), 'RESPONSE BODY', ('*' * 20))
 
336
        LOG.debug('%s %s %s', ('*' * 20), 'RESPONSE BODY', ('*' * 20))
331
337
        for part in app_iter:
332
338
            #sys.stdout.write(part)
333
 
            logging.debug(part)
 
339
            LOG.debug(part)
334
340
            #sys.stdout.flush()
335
341
            yield part
336
342
        print