From 01c6bcd9e436831d130964c1d7e5d5922bf5ecea Mon Sep 17 00:00:00 2001 From: John Bresnahan Date: Sat, 29 Dec 2012 05:54:56 -1000 Subject: [PATCH] Adding a means for a glance worker to connect back to a pydevd debugger. That patch allows a developer to remotely run a pydev debugger and a glance worker process connect back to it. Two configuration options have been added: pydev_worker_debug_host . Default is None pydev_worker_debug_port . Default is 5678 The behavior is enabled if pydev_worker_debug_host is not None. If a pydev connection fails to be esstablished an exception is raised. This patch will allow remote debugging as well as more seemless debugging environments with IDEs like pycharm. Change-Id: I7d3f60e373632b256f0e914f18204ce223a3486e --- glance/common/config.py | 6 ++++++ glance/common/utils.py | 21 +++++++++++++++++++++ glance/common/wsgi.py | 5 +++++ 3 files changed, 32 insertions(+) diff --git a/glance/common/config.py b/glance/common/config.py index 53155cf0b2..921dcf660a 100644 --- a/glance/common/config.py +++ b/glance/common/config.py @@ -58,6 +58,12 @@ common_opts = [ help=_("Deploy the v1 OpenStack Images API. ")), cfg.BoolOpt('enable_v2_api', default=True, help=_("Deploy the v2 OpenStack Images API. ")), + cfg.StrOpt('pydev_worker_debug_host', default=None, + help=_('The hostname/IP of the pydev process listening for ' + 'debug connections')), + cfg.IntOpt('pydev_worker_debug_port', default=5678, + help=_('The port on which a pydev process is listening for ' + 'connections.')), ] CONF = cfg.CONF diff --git a/glance/common/utils.py b/glance/common/utils.py index 9070883dd0..1500522d72 100644 --- a/glance/common/utils.py +++ b/glance/common/utils.py @@ -36,6 +36,7 @@ import sys from webob import exc from glance.common import exception +from glance.openstack.common import cfg import glance.openstack.common.log as logging @@ -410,3 +411,23 @@ def mutating(func): content_type="text/plain") return func(self, req, *args, **kwargs) return wrapped + + +def setup_remote_pydev_debug(host, port): + + error_msg = ('Error setting up the debug environment. Verify that the' + ' option pydev_worker_debug_port is pointing to a valid ' + 'hostname or IP on which a pydev server is listening on' + ' the port indicated by pydev_worker_debug_port.') + + try: + from pydev import pydevd + + pydevd.settrace(host, + port=port, + stdoutToServer=True, + stderrToServer=True) + return True + except: + LOG.exception(error_msg) + raise diff --git a/glance/common/wsgi.py b/glance/common/wsgi.py index 7d5e0449a9..a62af7a0a7 100644 --- a/glance/common/wsgi.py +++ b/glance/common/wsgi.py @@ -40,6 +40,7 @@ import webob.dec import webob.exc from glance.common import exception +from glance.common import utils from glance.openstack.common import cfg import glance.openstack.common.log as os_logging @@ -266,6 +267,10 @@ class Server(object): def run_server(self): """Run a WSGI server.""" + if cfg.CONF.pydev_worker_debug_host: + utils.setup_remote_pydev_debug(cfg.CONF.pydev_worker_debug_host, + cfg.CONF.pydev_worker_debug_port) + eventlet.wsgi.HttpProtocol.default_request_version = "HTTP/1.0" try: eventlet.hubs.use_hub('poll')