本文源码基于Pike版本.
通过setup.py里面:
[entry_points]
console_scripts =
....
neutron-server = neutron.cmd.eventlet.server:main
.....
可知,neutron-server的入口函数在neutron/cmd/eventlet/server/__init__.py:
def main():
server.boot_server(wsgi_eventlet.eventlet_wsgi_server)
def main_rpc_eventlet():
server.boot_server(rpc_eventlet.eventlet_rpc_server)
neutron/server/__init__.py:
def _init_configuration():
# the configuration will be read into the cfg.CONF global data structure
config.init(sys.argv[1:]) #此处传入的参数一般为 --config-file /etc/neutron/neutron.conf --config-file /etc/neutron/plugins/ml2/ml2_conf.ini
config.setup_logging() #设置neutron的logging,就是以neutron为进程名的打印
config.set_config_defaults()
...省略...
def boot_server(server_func):
_init_configuration()
try:
server_func()
...省略...
server_func为传入的wsgi_eventlet.eventlet_wsgi_server,neutron/server/wsgi_eventlet.py:
def eventlet_wsgi_server():
neutron_api = service.serve_wsgi(service.NeutronApiService) #创建neutron-server服务进程
start_api_and_rpc_workers(neutron_api)
def start_api_and_rpc_workers(neutron_api):
try:
worker_launcher = service.start_all_workers()
pool = eventlet.GreenPool()
api_thread = pool.spawn(neutron_api.wait)
plugin_workers_thread = pool.spawn(worker_launcher.wait)
# api and other workers should die together. When one dies,
# kill the other.
api_thread.link(lambda gt: plugin_workers_thread.kill())
plugin_workers_thread.link(lambda gt: api_thread.kill())
pool.waitall()
except NotImplementedError:
LOG.info("RPC was already started in parent process by "
"plugin.")
neutron_api.wait()
def serve_wsgi(cls):
try:
service = cls.create()
service.start()
class NeutronApiService(WsgiService):
...省略....
class WsgiService(object):
"""Base class for WSGI based services.
For each api you define, you must also define these flags:
:<api>_listen: The address on which to listen
:<api>_listen_port: The port on which to listen
"""
def __init__(self, app_name):
self.app_name = app_name
self.wsgi_app = None
def start(self):
self.wsgi_app = _run_wsgi(self.app_name) #加载/etc/neutron/api-paste.ini,启动WSGI服务器
def wait(self):
self.wsgi_app.wait()
def _run_wsgi(app_name):
app = config.load_paste_app(app_name)
if not app:
LOG.error('No known API applications configured.')
return
return