Quantum Server启动流程

本文地址:http://blog.csdn.net/spch2008/article/details/9284217


有关upstart-job的内容已经在linux sysvinit与upstart说过了。

第一:quantum-server.conf

service quantum-server start命令,将执行quantum-server.conf中脚本。

description "Quantum server"
author "Chuck Short <[email protected]>"

start on runlevel [2345]
stop on runlevel [016]

chdir /var/run

pre-start script
        mkdir -p /var/run/quantum
        chown quantum:root /var/run/quantum
end script

script
        [ -r /etc/default/quantum-server ] && . /etc/default/quantum-server
        [ -r "$QUANTUM_PLUGIN_CONFIG" ] && CONF_ARG="--config-file $QUANTUM_PLUGIN_CONFIG"
        exec start-stop-daemon --start --chuid quantum --exec /usr/bin/quantum-server -- \
            --config-file /etc/quantum/quantum.conf \
            --log-file /var/log/quantum/server.log $CONF_ARG
end script

script段中,首先判断/etc/default/quantum-server文件是否存在,如果存在,执行该文件,对QUANTUM_PLUGIN_CONFIG设置值。

QUANTUM_PLUGIN_CONFIG="/etc/quantum/plugins/openvswitch/ovs_quantum_plugin.ini"
其次,判断QUANTUM_PLUGIN_CONFIG是否为空,若不为空,则设置CONF_ARG为 --config-file $QUANTUM_PLUGIN_CONFIG

第三行,执行/usr/bin/quantum-server脚本,并传入后面的参数,相当于

/usr/bin/quantum-server -- \
            --config-file /etc/quantum/quantum.conf \
            --log-file /var/log/quantum/server.log --config-file /etc/quantum/plugins/openvswitch/ovs_quantum_plugin.ini

第二:quantum-server

/usr/bin/quantum-sersver

import eventlet
import os
import sys
sys.path.insert(0, os.getcwd())
from quantum.server import main as server


eventlet.monkey_patch()
server()
导入quantum.server中main,然后调用main。


第三:quantum-server main

/usr/share/pyshared/quantum/server/__init__.py

from quantum import service
def main():
    # the configuration will be read into the cfg.CONF global data structure 
    config.parse(sys.argv)
      
    if not cfg.CONF.config_file:
        sys.exit("ERROR: Unable to find configuration file via the default"
                 " search paths (~/.quantum/, ~/, /etc/quantum/, /etc/) and"
                 " the '--config-file' option!")
        
    try:
        quantum_service = service.serve_wsgi(service.QuantumApiService)
        quantum_service.wait()
    except RuntimeError, e:
        sys.exit("ERROR: %s" % e)
sys.argv为传入的参数,打印出来为:

['/usr/bin/quantum-server', '--', '--config-file', '/etc/quantum/quantum.conf', '--log-file', 
 '/var/log/quantum/server.log', '--config-file', '/etc/quantum/plugins/openvswitch/ovs_quantum_plugin.ini']

首先进行参数的解析,这不是本文的重点,所以先略过。


第四: service.py

def serve_wsgi(cls):
    
    service = cls.create()
    service.start()
    return service
cls为传入的service.QuantumApiService类,创建service对象,然后调用start函数启用服务。

class WsgiService(object):
    
    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()
		
class QuantumApiService(WsgiService):

    @classmethod
    def create(cls):
        app_name = "quantum"
        config.setup_logging(cfg.CONF)
		
        service = cls(app_name)
        return service
QuantumApiService继承WsgiService,创建对象后,调用父类start方法。


#app_name  quantum
def _run_wsgi(app_name):
    
    app = config.load_paste_app(app_name)
  
    server = wsgi.Server("Quantum")
    #绑定套接字
    server.start(app, cfg.CONF.bind_port, cfg.CONF.bind_host)
    return server

load_paste_app("quantum")加载/etc/quantum/api-paste.ini中的quantum段。同时,取得/etc/quantum/quantum.conf中配置的bind_port与bind_host。

[composite:quantum]
use = egg:Paste#urlmap
/: quantumversions
/v2.0: quantumapi_v2_0
返回urlmap应用。


第四:wsgi.py

quantum\wsgi.py

class Server(object):
    
    def __init__(self, name, threads=1000):
        self.pool = eventlet.GreenPool(threads)
        self.name = name

    def start(self, application, port, host='0.0.0.0', backlog=128):
        socket = eventlet.listen((host, port), backlog=backlog)
        self.pool.spawn_n(self._run, application, socket)

    def wait(self):
        try:
            self.pool.waitall()
        except KeyboardInterrupt:
            pass

    def _run(self, application, socket):
        logger = logging.getLogger('eventlet.wsgi.server')
        eventlet.wsgi.server(socket, application, custom_pool=self.pool,
                             log=WritableLogger(logger))

有关eventlet的概念与用法,下次再发。

你可能感兴趣的:(Quantum Server启动流程)