OpenStack Neutron源码学习:粗识Network创建

作者: LiHui 分类: OpenStack 发布时间: 2018-01-14 22:46

一开始neutron-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.setup_logging()
config.set_config_defaults()
if not cfg.CONF.config_file:
sys.exit(_("ERROR: Unable to find configuration file via the default"
" search paths (~/.neutron/, ~/, /etc/neutron/, /etc/) and"
" the '--config-file' option!"))


def boot_server(server_func):
_init_configuration()
try:
server_func()
except KeyboardInterrupt:
pass
except RuntimeError as e:
sys.exit(_("ERROR: %s") % e)

boot_server里,首先_init_configuration里,获取neutron-server启动参数,然后启动boot_server传入的参数

看真正boot_server的地方

neutron/cmd/eventlet/server/__init__.py

from neutron import server
from neutron.server import rpc_eventlet
from neutron.server import wsgi_eventlet


def main():
server.boot_server(wsgi_eventlet.eventlet_wsgi_server)

传入的参数即为boot_server执行的方法

neutron/server/wsgi_eventlet.py

def eventlet_wsgi_server():
neutron_api = service.serve_wsgi(service.NeutronApiService)
start_api_and_rpc_workers(neutron_api)

先看serve_wsgi方法

neutron/service.py

def serve_wsgi(cls):

try:
service = cls.create()
service.start()
except Exception:
with excutils.save_and_reraise_exception():
LOG.exception('Unrecoverable error: please check log '
'for details.')

registry.notify(resources.PROCESS, events.BEFORE_SPAWN, service)
return service

调用了传入类的create方法,创建一个服务,然后start这个服务

查看NeutronApiService

class NeutronApiService(WsgiService):
"""Class for neutron-api service."""
def __init__(self, app_name):
profiler.setup('neutron-server', cfg.CONF.host)
super(NeutronApiService, self).__init__(app_name)

@classmethod
def create(cls, app_name='neutron'):
# Setup logging early
config.setup_logging()
service = cls(app_name)
return service

从注释可以看出,这里封装了一个API功能的类,调用create方法,传入一个app_name

看基类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)

def wait(self):
self.wsgi_app.wait()

这里NeutronApiService继承WsgiService,并且参数app_name传了一个neutron,只表明NeutronApiService这个neutron服务是一个WSGI服务,create方法构造了这个服务,然后start方法运行了这个服务

def _run_wsgi(app_name):
app = config.load_paste_app(app_name)
if not app:
LOG.error('No known API applications configured.')
return
return run_wsgi_app(app)

调用load_paste_app加载这个服务

neutron/common/config.py

def load_paste_app(app_name):
"""Builds and returns a WSGI app from a paste config file.

:param app_name: Name of the application to load
"""
loader = wsgi.Loader(cfg.CONF)
app = loader.load_app(app_name)
return app

 

待续……

纯属恶搞

浙ICP备16024533号

浙公网安备 33010802007459号