server

    init.py文件中包括一个main()函数,是WSGI服务器开始的模块,并且通过调用serve_wsgi来创建一个NeutronApiService的实例。然后通过eventlet的greenpool来运行WSGI的应用程序,响应来自客户端的请求。
    主要过程为:

    绿化各个模块为支持协程(通过打补丁的方式让本地导入的库都支持协程)。

    1. if not cfg.CONF.config_file:
    2. sys.exit(_("ERROR: Unable to find configuration file via the default"
    3. " search paths (~/.neutron/, ~/, /etc/neutron/, /etc/) and"
    4. " the '--config-file' option!"))

    通过解析命令行传入的参数,获取配置文件所在。

    1. pool = eventlet.GreenPool()

    serve_wsgi方法创建NeutronApiService实例(作为一个WsgiService),并调用其的start()来启动socket服务器端。

    1. #neutron.service
    2. def serve_wsgi(cls):
    3. service.start()
    4. except Exception:
    5. with excutils.save_and_reraise_exception():
    6. LOG.exception(_('Unrecoverable error: please check log '
    7. 'for details.'))
    8. return service

    neutron.service.NeutronApiService类继承自neutron.service.WsgiService,其create方法返回一个appname默认为“neutron”的WsgiService对象;start方法则调用_run_wsgi方法。

    1. def start(self):

    _run_wsgi方法主要是从api-paste.ini文件中读取应用(最后是利用neutron.api.v2.router:APIRouter.factory来构造应用),然后为应用创建一个wsgi的服务端,并启动应用,主要代码为。

    1. neutron_rpc = service.serve_rpc()
    2. except NotImplementedError:
    3. LOG.info(_("RPC was already started in parent process by plugin."))
    4. else:
    5. rpc_thread = pool.spawn(neutron_rpc.wait)
    6. rpc_thread.link(lambda gt: api_thread.kill())
    7. api_thread.link(lambda gt: rpc_thread.kill())

    这些代码创建plugin的rpc服务端,并将api和rpc的生存绑定到一起,一个死掉,则另外一个也死掉。

      最后是后台不断等待。

      下面的图表总结了neutron-server的核心启动过程。