devstack下有个openstack重启服务的脚本 devstack/rejoin-stack.sh, 其调用 stack-screenrc 开启screen,启动nova-api服务:
screen -t n-api bash
stuff "/usr/local/bin/nova-api^M"
logfile /opt/stack/logs/n-api.log.2015-10-27-071959
log on
可知,使用nova-api.py:
import sys
from nova.cmd.api import main
if __name__ == "__main__":
sys.exit(main())
main函数中,通过读取配置文件中的enabled_apis,启动WSGI server:
launcher = service.process_launcher()
for api in CONF.enabled_apis:
should_use_ssl = api in CONF.enabled_ssl_apis
if api == 'ec2':
server = service.WSGIService(api,use_ssl=should_use_ssl,max_url_len=16384)
else:
server = service.WSGIService(api, use_ssl=should_use_ssl)
launcher.launch_service(server, workers=server.workers or 1)
到此,监听对应api服务的application已经建立完毕。
main 函数中的 enabled_apis 是通过 /etc/nova/nova.conf 读取得到的。
enabled_apis = ec2,osapi_compute,metadata
api_paste_config = /etc/nova/api-paste.ini
使用Paste Deploy 进行 分发(/usr/local/lib/python2./dist-packages/paste)
Paste 配置文件的格式:
[section_type:app_name]
use = xxxx
key = value
主要分为两类:
多个app在一个section下:
[composite:osapi_compute]
use = call:nova.api.openstack.urlmap:urlmap_factory
/: oscomputeversions
/v1.1: openstack_compute_api_v21_legacy_v2_compatible
/v2: openstack_compute_api_v21_legacy_v2_compatible
/v2.1: openstack_compute_api_v21
single app
[app:metaapp]
paste.app_factory = nova.api.metadata.handler:MetadataRequestHandler.factory
type : filter-app, filter, pipeline
类似与Django 框架的middleware,对request进行初步的处理,如果有需要,可以传递给下一个函数,进行进一步处理。
大致知道了,paste deploy 的使用方法,接下来一个小Demo, 演示一下paset deploy, 这里使用python自带的simple_server 加载 app。
paset deploy 的 配置文件, config.ini
[composite:test_composite]
use=egg:Paste#urlmap
/:test_app_tag
/test_pipeline:test_pipeline_tag
[app:test_app_tag]
paste.app_factory=test_paste_deploy:TestApp.factory
[pipeline:test_pipeline_tag]
pipeline= test_filter_tag test_app_tag
[filter:test_filter_tag]
paste.filter_factory=test_paste_deploy:TestFilter.factory
python file : test_paste_deploy.py
from os.path import abspath
from paste.deploy import loadapp
from wsgiref.simple_server import make_server
class TestApp:
def __init__(self):
pass
def __call__(self, environ, start_response):
start_response("200 OK", [("Content-type", "text/plain")])
return_string = environ.get('pipeline', None) and environ['pipeline'] or\
"test paste deploy app tag success :)"
return [return_string]
@classmethod
def factory(cls, global_conf, **kwargs):
return TestApp()
class TestFilter:
def __init__(self, app):
self.app = app
def __call__(self, environ, start_response):
environ['pipeline'] = "call TestFilter middleware"
return self.app(environ, start_response)
@classmethod
def factory(cls, global_conf, **kwargs):
return TestFilter
if __name__ == '__main__':
config_file = "config.ini"
appname = "test_composite"
wsgi_app = loadapp("config:%s" % abspath(config_file), appname)
server = make_server('localhost', 54321, wsgi_app)
server.serve_forever()
demo 非常简单,测试了 app, pipeline, filter, composite 的使用,
只要 浏览器中输入
http://127.0.0.1:54321/
http://127.0.0.1:54321/test_pipeline
就可以显示不同的文本内容。