Monkey patch threading module as early as possible

In oslo.messaging, local thread store is used to pass RPC request
context [1]. If we try to import oslo.messaging before monkey patching
threading library, it attempts to access unpatched storage and fails
with AttributeError.

[1]: oslo/messaging/localcontext.py#L26

blueprint oslo-messaging

Change-Id: Ied7302fcb1d3e14428540e39e3db704550027890
This commit is contained in:
Ihar Hrachyshka 2014-05-30 15:53:25 +02:00
parent fc8546d8c0
commit 161ec8341b
27 changed files with 96 additions and 96 deletions

View File

@ -18,6 +18,8 @@
import os import os
import eventlet import eventlet
eventlet.monkey_patch()
import netaddr import netaddr
from oslo.config import cfg from oslo.config import cfg
@ -607,7 +609,6 @@ def register_options():
def main(): def main():
eventlet.monkey_patch()
register_options() register_options()
cfg.CONF(project='neutron') cfg.CONF(project='neutron')
config.setup_logging(cfg.CONF) config.setup_logging(cfg.CONF)

View File

@ -14,6 +14,8 @@
# #
import eventlet import eventlet
eventlet.monkey_patch()
import netaddr import netaddr
from oslo.config import cfg from oslo.config import cfg
@ -967,7 +969,6 @@ class L3NATAgentWithStateReport(L3NATAgent):
def main(manager='neutron.agent.l3_agent.L3NATAgentWithStateReport'): def main(manager='neutron.agent.l3_agent.L3NATAgentWithStateReport'):
eventlet.monkey_patch()
conf = cfg.CONF conf = cfg.CONF
conf.register_opts(L3NATAgent.OPTS) conf.register_opts(L3NATAgent.OPTS)
config.register_interface_driver_opts_helper(conf) config.register_interface_driver_opts_helper(conf)

View File

@ -22,6 +22,8 @@ import os
import socket import socket
import eventlet import eventlet
eventlet.monkey_patch()
import httplib2 import httplib2
from neutronclient.v2_0 import client from neutronclient.v2_0 import client
from oslo.config import cfg from oslo.config import cfg
@ -377,7 +379,6 @@ class UnixDomainMetadataProxy(object):
def main(): def main():
eventlet.monkey_patch()
cfg.CONF.register_opts(UnixDomainMetadataProxy.OPTS) cfg.CONF.register_opts(UnixDomainMetadataProxy.OPTS)
cfg.CONF.register_opts(MetadataProxyHandler.OPTS) cfg.CONF.register_opts(MetadataProxyHandler.OPTS)
cache.register_oslo_configs(cfg.CONF) cache.register_oslo_configs(cfg.CONF)

View File

@ -20,6 +20,8 @@ import httplib
import socket import socket
import eventlet import eventlet
eventlet.monkey_patch()
import httplib2 import httplib2
from oslo.config import cfg from oslo.config import cfg
import six.moves.urllib.parse as urlparse import six.moves.urllib.parse as urlparse
@ -144,7 +146,6 @@ class ProxyDaemon(daemon.Daemon):
def main(): def main():
eventlet.monkey_patch()
opts = [ opts = [
cfg.StrOpt('network_id', cfg.StrOpt('network_id',
help=_('Network that will have instance metadata ' help=_('Network that will have instance metadata '

View File

@ -18,6 +18,8 @@
import re import re
import eventlet import eventlet
eventlet.monkey_patch()
from oslo.config import cfg from oslo.config import cfg
from neutron.agent.common import config as agent_config from neutron.agent.common import config as agent_config
@ -157,8 +159,6 @@ def main():
installation as it will blindly purge namespaces and their devices. This installation as it will blindly purge namespaces and their devices. This
option also kills any lingering DHCP instances. option also kills any lingering DHCP instances.
""" """
eventlet.monkey_patch()
conf = setup_conf() conf = setup_conf()
conf() conf()
config.setup_logging(conf) config.setup_logging(conf)

View File

@ -17,10 +17,12 @@
# under the License. # under the License.
# @author: Kevin Benton, kevin.benton@bigswitch.com # @author: Kevin Benton, kevin.benton@bigswitch.com
import eventlet
import sys import sys
import time import time
import eventlet
eventlet.monkey_patch()
from oslo.config import cfg from oslo.config import cfg
from neutron.agent.linux import ovs_lib from neutron.agent.linux import ovs_lib
@ -162,7 +164,6 @@ class RestProxyAgent(sg_rpc.SecurityGroupAgentRpcCallbackMixin):
def main(): def main():
eventlet.monkey_patch()
cfg.CONF(project='neutron') cfg.CONF(project='neutron')
config.setup_logging(cfg.CONF) config.setup_logging(cfg.CONF)
pl_config.register_config() pl_config.register_config()

View File

@ -18,11 +18,13 @@
# @author: Pedro Navarro Perez # @author: Pedro Navarro Perez
# @author: Alessandro Pilotti, Cloudbase Solutions Srl # @author: Alessandro Pilotti, Cloudbase Solutions Srl
import eventlet
import platform import platform
import re import re
import time import time
import eventlet
eventlet.monkey_patch()
from oslo.config import cfg from oslo.config import cfg
from neutron.agent.common import config from neutron.agent.common import config
@ -464,7 +466,6 @@ class HyperVNeutronAgent(object):
def main(): def main():
eventlet.monkey_patch()
cfg.CONF(project='neutron') cfg.CONF(project='neutron')
logging_config.setup_logging(cfg.CONF) logging_config.setup_logging(cfg.CONF)

View File

@ -21,6 +21,8 @@ import socket
import time import time
import eventlet import eventlet
eventlet.monkey_patch()
from oslo.config import cfg from oslo.config import cfg
from neutron.agent.linux import ip_lib from neutron.agent.linux import ip_lib
@ -252,7 +254,6 @@ def create_agent_config_map(config):
def main(): def main():
eventlet.monkey_patch()
cfg.CONF.register_opts(ip_lib.OPTS) cfg.CONF.register_opts(ip_lib.OPTS)
cfg.CONF(project='neutron') cfg.CONF(project='neutron')
logging_config.setup_logging(cfg.CONF) logging_config.setup_logging(cfg.CONF)

View File

@ -27,6 +27,8 @@ import sys
import time import time
import eventlet import eventlet
eventlet.monkey_patch()
from oslo.config import cfg from oslo.config import cfg
from neutron.agent import l2population_rpc as l2pop_rpc from neutron.agent import l2population_rpc as l2pop_rpc
@ -1025,7 +1027,6 @@ class LinuxBridgeNeutronAgentRPC(sg_rpc.SecurityGroupAgentRpcMixin):
def main(): def main():
eventlet.monkey_patch()
cfg.CONF(project='neutron') cfg.CONF(project='neutron')
logging_config.setup_logging(cfg.CONF) logging_config.setup_logging(cfg.CONF)

View File

@ -21,6 +21,8 @@ import sys
import time import time
import eventlet import eventlet
eventlet.monkey_patch()
from oslo.config import cfg from oslo.config import cfg
from neutron.agent import rpc as agent_rpc from neutron.agent import rpc as agent_rpc
@ -417,7 +419,6 @@ class MlnxEswitchNeutronAgent(sg_rpc.SecurityGroupAgentRpcMixin):
def main(): def main():
eventlet.monkey_patch()
cfg.CONF(project='neutron') cfg.CONF(project='neutron')
logging_config.setup_logging(cfg.CONF) logging_config.setup_logging(cfg.CONF)

View File

@ -24,6 +24,7 @@ import socket
import time import time
import eventlet import eventlet
eventlet.monkey_patch()
from neutron.agent.linux import ovs_lib from neutron.agent.linux import ovs_lib
from neutron.agent import rpc as agent_rpc from neutron.agent import rpc as agent_rpc
@ -230,8 +231,6 @@ class NECNeutronAgent(object):
def main(): def main():
eventlet.monkey_patch()
config.CONF(project='neutron') config.CONF(project='neutron')
logging_config.setup_logging(config.CONF) logging_config.setup_logging(config.CONF)

View File

@ -20,6 +20,7 @@ import socket
import time import time
import eventlet import eventlet
eventlet.monkey_patch()
from neutron.agent.linux import ovs_lib from neutron.agent.linux import ovs_lib
from neutron.agent import rpc as agent_rpc from neutron.agent import rpc as agent_rpc
@ -160,7 +161,6 @@ class NVSDNeutronAgent(object):
def main(): def main():
eventlet.monkey_patch()
config.CONF(project='neutron') config.CONF(project='neutron')
logging_config.setup_logging(config.CONF) logging_config.setup_logging(config.CONF)

View File

@ -19,6 +19,8 @@ import sys
import time import time
import eventlet import eventlet
eventlet.monkey_patch()
import netaddr import netaddr
from oslo.config import cfg from oslo.config import cfg
from six import moves from six import moves
@ -1461,7 +1463,6 @@ def create_agent_config_map(config):
def main(): def main():
eventlet.monkey_patch()
cfg.CONF.register_opts(ip_lib.OPTS) cfg.CONF.register_opts(ip_lib.OPTS)
cfg.CONF(project='neutron') cfg.CONF(project='neutron')
logging_config.setup_logging(cfg.CONF) logging_config.setup_logging(cfg.CONF)

View File

@ -24,6 +24,8 @@ import sys
import time import time
import eventlet import eventlet
eventlet.monkey_patch()
from oslo.config import cfg from oslo.config import cfg
from ryu.app import client from ryu.app import client
from ryu.app import conf_switch_key from ryu.app import conf_switch_key
@ -282,7 +284,6 @@ class OVSNeutronOFPRyuAgent(sg_rpc.SecurityGroupAgentRpcCallbackMixin):
def main(): def main():
eventlet.monkey_patch()
cfg.CONF(project='neutron') cfg.CONF(project='neutron')
logging_config.setup_logging(cfg.CONF) logging_config.setup_logging(cfg.CONF)

View File

@ -15,14 +15,15 @@
# under the License. # under the License.
# #
import eventlet
import time import time
import eventlet
eventlet.monkey_patch()
from neutron.openstack.common import log as logging from neutron.openstack.common import log as logging
from neutron.plugins.vmware.api_client import base from neutron.plugins.vmware.api_client import base
from neutron.plugins.vmware.api_client import eventlet_request from neutron.plugins.vmware.api_client import eventlet_request
eventlet.monkey_patch()
LOG = logging.getLogger(__name__) LOG = logging.getLogger(__name__)

View File

@ -18,9 +18,11 @@
# If ../neutron/__init__.py exists, add ../ to Python search path, so that # If ../neutron/__init__.py exists, add ../ to Python search path, so that
# it will override what happens to be installed in /usr/(local/)lib/python... # it will override what happens to be installed in /usr/(local/)lib/python...
import eventlet
import sys import sys
import eventlet
eventlet.monkey_patch()
from oslo.config import cfg from oslo.config import cfg
from neutron.common import config from neutron.common import config
@ -34,8 +36,6 @@ LOG = logging.getLogger(__name__)
def main(): def main():
eventlet.monkey_patch()
# the configuration will be read into the cfg.CONF global data structure # the configuration will be read into the cfg.CONF global data structure
config.parse(sys.argv[1:]) config.parse(sys.argv[1:])
if not cfg.CONF.config_file: if not cfg.CONF.config_file:

View File

@ -19,6 +19,8 @@
# #
import eventlet import eventlet
eventlet.monkey_patch()
import netaddr import netaddr
from oslo.config import cfg from oslo.config import cfg
@ -327,7 +329,6 @@ class vArmourL3NATAgentWithStateReport(vArmourL3NATAgent,
def main(): def main():
eventlet.monkey_patch()
conf = cfg.CONF conf = cfg.CONF
conf.register_opts(vArmourL3NATAgent.OPTS) conf.register_opts(vArmourL3NATAgent.OPTS)
config.register_interface_driver_opts_helper(conf) config.register_interface_driver_opts_helper(conf)

View File

@ -17,6 +17,8 @@
# @author: Mark McClain, DreamHost # @author: Mark McClain, DreamHost
import eventlet import eventlet
eventlet.monkey_patch()
from oslo.config import cfg from oslo.config import cfg
from neutron.agent.common import config from neutron.agent.common import config
@ -47,7 +49,6 @@ class LbaasAgentService(rpc_service.Service):
def main(): def main():
eventlet.monkey_patch()
cfg.CONF.register_opts(OPTS) cfg.CONF.register_opts(OPTS)
cfg.CONF.register_opts(manager.OPTS) cfg.CONF.register_opts(manager.OPTS)
# import interface options just in case the driver uses namespaces # import interface options just in case the driver uses namespaces

View File

@ -25,6 +25,8 @@ import time
import eventlet import eventlet
eventlet.monkey_patch(thread=True)
from oslo.config import cfg from oslo.config import cfg
from neutron.api.v2 import attributes from neutron.api.v2 import attributes
@ -39,8 +41,6 @@ from neutron.plugins.common import constants
from neutron.services.loadbalancer.drivers import abstract_driver from neutron.services.loadbalancer.drivers import abstract_driver
from neutron.services.loadbalancer.drivers.radware import exceptions as r_exc from neutron.services.loadbalancer.drivers.radware import exceptions as r_exc
eventlet.monkey_patch(thread=True)
LOG = logging.getLogger(__name__) LOG = logging.getLogger(__name__)
RESP_STATUS = 0 RESP_STATUS = 0

View File

@ -17,6 +17,8 @@
import time import time
import eventlet import eventlet
eventlet.monkey_patch()
from oslo.config import cfg from oslo.config import cfg
from neutron.agent.common import config from neutron.agent.common import config
@ -281,7 +283,6 @@ class MeteringAgentWithStateReport(MeteringAgent):
def main(): def main():
eventlet.monkey_patch()
conf = cfg.CONF conf = cfg.CONF
conf.register_opts(MeteringAgent.Opts) conf.register_opts(MeteringAgent.Opts)
config.register_agent_state_opts_helper(conf) config.register_agent_state_opts_helper(conf)

View File

@ -213,14 +213,12 @@ class TestHyperVNeutronAgent(base.BaseTestCase):
with mock.patch.object(hyperv_neutron_agent, with mock.patch.object(hyperv_neutron_agent,
'HyperVNeutronAgent') as plugin: 'HyperVNeutronAgent') as plugin:
with mock.patch.object(hyperv_neutron_agent.cfg, 'CONF') as cfg: with mock.patch.object(hyperv_neutron_agent.cfg, 'CONF') as cfg:
with mock.patch('eventlet.monkey_patch') as eventlet: with mock.patch.object(
with mock.patch.object( hyperv_neutron_agent,
hyperv_neutron_agent, 'logging_config') as logging_config:
'logging_config') as logging_config:
hyperv_neutron_agent.main() hyperv_neutron_agent.main()
self.assertTrue(cfg.called) self.assertTrue(cfg.called)
self.assertTrue(eventlet.called) self.assertTrue(logging_config.setup_logging.called)
self.assertTrue(logging_config.setup_logging.called) plugin.assert_has_calls([mock.call().daemon_loop()])
plugin.assert_has_calls([mock.call().daemon_loop()])

View File

@ -350,17 +350,15 @@ class TestNecAgentMain(base.BaseTestCase):
def test_main(self): def test_main(self):
with contextlib.nested( with contextlib.nested(
mock.patch.object(nec_neutron_agent, 'NECNeutronAgent'), mock.patch.object(nec_neutron_agent, 'NECNeutronAgent'),
mock.patch('eventlet.monkey_patch'),
mock.patch.object(nec_neutron_agent, 'logging_config'), mock.patch.object(nec_neutron_agent, 'logging_config'),
mock.patch.object(nec_neutron_agent, 'config') mock.patch.object(nec_neutron_agent, 'config')
) as (agent, eventlet, logging_config, cfg): ) as (agent, logging_config, cfg):
cfg.OVS.integration_bridge = 'br-int-x' cfg.OVS.integration_bridge = 'br-int-x'
cfg.AGENT.root_helper = 'dummy-helper' cfg.AGENT.root_helper = 'dummy-helper'
cfg.AGENT.polling_interval = 10 cfg.AGENT.polling_interval = 10
nec_neutron_agent.main() nec_neutron_agent.main()
self.assertTrue(eventlet.called)
self.assertTrue(logging_config.setup_logging.called) self.assertTrue(logging_config.setup_logging.called)
agent.assert_has_calls([ agent.assert_has_calls([
mock.call('br-int-x', 'dummy-helper', 10), mock.call('br-int-x', 'dummy-helper', 10),

View File

@ -161,17 +161,15 @@ class TestOneConvergenceAgentMain(base.BaseTestCase):
def test_main(self): def test_main(self):
with contextlib.nested( with contextlib.nested(
mock.patch.object(nvsd_neutron_agent, 'NVSDNeutronAgent'), mock.patch.object(nvsd_neutron_agent, 'NVSDNeutronAgent'),
mock.patch('eventlet.monkey_patch'),
mock.patch.object(nvsd_neutron_agent, 'logging_config'), mock.patch.object(nvsd_neutron_agent, 'logging_config'),
mock.patch.object(nvsd_neutron_agent, 'config') mock.patch.object(nvsd_neutron_agent, 'config')
) as (agent, eventlet, logging_config, config): ) as (agent, logging_config, config):
config.AGENT.integration_bridge = 'br-int-dummy' config.AGENT.integration_bridge = 'br-int-dummy'
config.AGENT.root_helper = 'root-helper' config.AGENT.root_helper = 'root-helper'
config.AGENT.polling_interval = 5 config.AGENT.polling_interval = 5
nvsd_neutron_agent.main() nvsd_neutron_agent.main()
self.assertTrue(eventlet.called)
self.assertTrue(logging_config.setup_logging.called) self.assertTrue(logging_config.setup_logging.called)
agent.assert_has_calls([ agent.assert_has_calls([
mock.call('br-int-dummy', 'root-helper', 5), mock.call('br-int-dummy', 'root-helper', 5),

View File

@ -42,12 +42,10 @@ class TestLbaasService(base.BaseTestCase):
with contextlib.nested( with contextlib.nested(
mock.patch(logging_str), mock.patch(logging_str),
mock.patch.object(agent.service, 'launch'), mock.patch.object(agent.service, 'launch'),
mock.patch.object(agent, 'eventlet'),
mock.patch('sys.argv'), mock.patch('sys.argv'),
mock.patch.object(agent.manager, 'LbaasAgentManager'), mock.patch.object(agent.manager, 'LbaasAgentManager'),
mock.patch.object(cfg.CONF, 'register_opts') mock.patch.object(cfg.CONF, 'register_opts')
) as (mock_logging, mock_launch, mock_eventlet, sys_argv, mgr_cls, ro): ) as (mock_logging, mock_launch, sys_argv, mgr_cls, ro):
agent.main() agent.main()
self.assertTrue(mock_eventlet.monkey_patch.called)
mock_launch.assert_called_once_with(mock.ANY) mock_launch.assert_called_once_with(mock.ANY)

View File

@ -551,18 +551,16 @@ class TestUnixDomainMetadataProxy(base.BaseTestCase):
def test_main(self): def test_main(self):
with mock.patch.object(agent, 'UnixDomainMetadataProxy') as proxy: with mock.patch.object(agent, 'UnixDomainMetadataProxy') as proxy:
with mock.patch('eventlet.monkey_patch') as eventlet: with mock.patch.object(agent, 'config') as config:
with mock.patch.object(agent, 'config') as config: with mock.patch.object(agent, 'cfg') as cfg:
with mock.patch.object(agent, 'cfg') as cfg: with mock.patch.object(utils, 'cfg'):
with mock.patch.object(utils, 'cfg'): agent.main()
agent.main()
self.assertTrue(eventlet.called) self.assertTrue(config.setup_logging.called)
self.assertTrue(config.setup_logging.called) proxy.assert_has_calls([
proxy.assert_has_calls([ mock.call(cfg.CONF),
mock.call(cfg.CONF), mock.call().run()]
mock.call().run()] )
)
def test_init_state_reporting(self): def test_init_state_reporting(self):
with mock.patch('os.makedirs'): with mock.patch('os.makedirs'):

View File

@ -312,46 +312,42 @@ class TestProxyDaemon(base.BaseTestCase):
def test_main(self): def test_main(self):
with mock.patch.object(ns_proxy, 'ProxyDaemon') as daemon: with mock.patch.object(ns_proxy, 'ProxyDaemon') as daemon:
with mock.patch('eventlet.monkey_patch') as eventlet: with mock.patch.object(ns_proxy, 'config') as config:
with mock.patch.object(ns_proxy, 'config') as config: with mock.patch.object(ns_proxy, 'cfg') as cfg:
with mock.patch.object(ns_proxy, 'cfg') as cfg: with mock.patch.object(utils, 'cfg') as utils_cfg:
with mock.patch.object(utils, 'cfg') as utils_cfg: cfg.CONF.router_id = 'router_id'
cfg.CONF.router_id = 'router_id' cfg.CONF.network_id = None
cfg.CONF.network_id = None cfg.CONF.metadata_port = 9697
cfg.CONF.metadata_port = 9697 cfg.CONF.pid_file = 'pidfile'
cfg.CONF.pid_file = 'pidfile' cfg.CONF.daemonize = True
cfg.CONF.daemonize = True utils_cfg.CONF.log_opt_values.return_value = None
utils_cfg.CONF.log_opt_values.return_value = None ns_proxy.main()
ns_proxy.main()
self.assertTrue(eventlet.called) self.assertTrue(config.setup_logging.called)
self.assertTrue(config.setup_logging.called) daemon.assert_has_calls([
daemon.assert_has_calls([ mock.call('pidfile', 9697,
mock.call('pidfile', 9697, router_id='router_id',
router_id='router_id', network_id=None),
network_id=None), mock.call().start()]
mock.call().start()] )
)
def test_main_dont_fork(self): def test_main_dont_fork(self):
with mock.patch.object(ns_proxy, 'ProxyDaemon') as daemon: with mock.patch.object(ns_proxy, 'ProxyDaemon') as daemon:
with mock.patch('eventlet.monkey_patch') as eventlet: with mock.patch.object(ns_proxy, 'config') as config:
with mock.patch.object(ns_proxy, 'config') as config: with mock.patch.object(ns_proxy, 'cfg') as cfg:
with mock.patch.object(ns_proxy, 'cfg') as cfg: with mock.patch.object(utils, 'cfg') as utils_cfg:
with mock.patch.object(utils, 'cfg') as utils_cfg: cfg.CONF.router_id = 'router_id'
cfg.CONF.router_id = 'router_id' cfg.CONF.network_id = None
cfg.CONF.network_id = None cfg.CONF.metadata_port = 9697
cfg.CONF.metadata_port = 9697 cfg.CONF.pid_file = 'pidfile'
cfg.CONF.pid_file = 'pidfile' cfg.CONF.daemonize = False
cfg.CONF.daemonize = False utils_cfg.CONF.log_opt_values.return_value = None
utils_cfg.CONF.log_opt_values.return_value = None ns_proxy.main()
ns_proxy.main()
self.assertTrue(eventlet.called) self.assertTrue(config.setup_logging.called)
self.assertTrue(config.setup_logging.called) daemon.assert_has_calls([
daemon.assert_has_calls([ mock.call('pidfile', 9697,
mock.call('pidfile', 9697, router_id='router_id',
router_id='router_id', network_id=None),
network_id=None), mock.call().run()]
mock.call().run()] )
)

View File

@ -30,7 +30,7 @@ from xml.etree import ElementTree as etree
from xml.parsers import expat from xml.parsers import expat
import eventlet.wsgi import eventlet.wsgi
eventlet.patcher.monkey_patch(all=False, socket=True) eventlet.patcher.monkey_patch(all=False, socket=True, thread=True)
from oslo.config import cfg from oslo.config import cfg
import routes.middleware import routes.middleware
import webob.dec import webob.dec