分享

openstack源码分析 - Paste Deployment介绍

问题导读
1、什么是 Paste Deployment?
2、如何写一个application?
3、 nova-api.ini主要定义了哪些?





Paste Deployment用于发现和配置WSGI Application和Server,有了Paste Deployment,WSGI applications只需向其用户提供一个单独的入口loadapp函数,然后用户调用这个函数就可以使用已经开发好的WSGI application,同时,由于只提供了一个入口,WSGI的开发者也不再需要将App的具体实现暴露给用户,大大简化了整个开发过程。
    这里涉及到了两个概念: server和application,server可能大家比较熟悉,即常用的apache,nignx等等,applications广义理解就是一 个符合wsgi规范的一个可调用的object,规范指的是:一个接受两个参数的函数(environ,start_response)
    目的:使得web application像library,well encapsulated and reusable, but still with rich reusable functionality
1.png


paste deploy 概念
1. settions
  paste deploy主要的任务就是搭建连接server与applications的桥梁,所以section就是为了application进行服务的,主要用来分类和描述不同用途的application.
(1)[app:name]
定义一个符合wsgi协议的应用,有几种使用方法:
  1. #指向另外一个配置文件中的application
  2. [app:myapp]
  3. use = config:another_config_file.ini#app_name
  4. # or any URI:
  5. [app:myotherapp]
  6. use = egg:MyApp
  7. # or a callable from a module:
  8. [app:mythirdapp]
  9. use = call:my.project:myapplication
  10. # or even another section:
  11. [app:myfourthapp]
  12. use = myotherapp
  13. # or point to python code directly, and must use protocol currently
  14. [app:myfifthapp]
  15. paste.app_factory = myapp.modulename:app_factor
复制代码



(2)[composite:name]
composite section也定义了一个application,与application section的区别是composite section是wsgi application的集合,可以包括其他application section中定义的application,最常用的用法是:
  1. // 定义一个mapper URL的composite app,调用urlmap_factory方法,其参数是不同的URL,当加载了此app后,根据不同的URL,重定向到不同的wsgi application
  2. [composite:osapi_compute]
  3. use = call:nova.api.openstack.urlmap:urlmap_factory
  4. /: oscomputeversions
  5. /v1.1: openstack_compute_api_v2
  6. /v2: openstack_compute_api_v2
  7. /v3: openstack_compute_api_v3
  8. [composite:openstack_compute_api_v2]
  9. use = call:nova.api.auth:pipeline_factory
  10. noauth = faultwrap sizelimit noauth ratelimit osapi_compute_app_v2
  11. keystone = faultwrap sizelimit authtoken keystonecontext ratelimit osapi_compute_app_v2
  12. keystone_nolimit = faultwrap sizelimit authtoken keystonecontext osapi_compute_app_v2
  13. [composite:openstack_compute_api_v3]
  14. use = call:nova.api.auth:pipeline_factory_v3
  15. noauth = faultwrap sizelimit noauth_v3 osapi_compute_app_v3
  16. keystone = faultwrap sizelimit authtoken keystonecontext osapi_compute_app_v3
复制代码



(3)[filter:name]
  定义过滤器,类似python中的decorator(装饰器),接受一个application参数作为对象,返回一个封装后的 application,这里只是定义了filter,并没有将filter作用到application上。 将filter作用到app的方法有两种: filter-app section和 pipe section

(4)[filter-app:name]
  与filter类似,唯一的区别是filter-app可以使用next参数来指定filter要作用的application.
  1. [filter-app:blog] use = egg:Authentication#auth next = blogapp roles = admin htpasswd = /home/me/users.htpasswd [app:blogapp] use = egg:BlogApp database = sqlite:/home/me/blog.db
复制代码



(5)[pipeline:name]
将一系列的filter作用到app上,下面例子即将faultwrap filter作用到oscomputeversionapp application上,当然可以将多个filter作用到一个app上,作用顺序为靠近app的filter先执行。
  1. [pipeline:oscomputeversions]
  2. pipeline = faultwrap oscomputeversionapp
  3. [filter:faultwrap]
  4. paste.filter_factory = nova.api.openstack:FaultWrapper.factory
  5. //filter的嵌套,执行顺序为:ratelimit --> keystonecontext --> authtoken --> sizelimit --> faultwrap (完全类似与decorator)
  6. [composite:openstack_compute_api_v2]
  7. use = call:nova.api.auth:pipeline_factory
  8. noauth = faultwrap sizelimit noauth ratelimit osapi_compute_app_v2
  9. keystone = faultwrap sizelimit authtoken keystonecontext ratelimit osapi_compute_app_v2
  10. keystone_nolimit = faultwrap sizelimit authtoken keystonecontext osapi_compute_app_v2
复制代码

(6)[server:name]


2. protocol
到现在为止,大体了解了6种section,其实每种section都是一个application,只是每种application的功能不同而已,另外,在section中指定了每种section的入口,前面给出的例子中大多通过use来给出的,比如:
  1. [composite:osapi_compute]
  2. use = call:nova.api.openstack.urlmap:urlmap_factory
  3. /: oscomputeversions
  4. /v1.1: openstack_compute_api_v2
  5. /v2: openstack_compute_api_v2
  6. /v3: openstack_compute_api_v3
  7. 通 过use = call:nova.api.openstack.urlmap:urlmap_factory,指定了composite application osapi_compute的入口为nova.api.openstack.urlmap:urlmap_factory,即urlmap.py中的函数 urlmap_factory.
  8. 除了采用use的方式指定application,可以使用paste.xxx_factory的方式来指定application的入口,其中paste.xxx_factory称为protocol:
  9. [app:oscomputeversionapp]
  10. paste.app_factory = nova.api.openstack.compute.versions:Versions.factory
  11. [filter:ratelimit]
  12. paste.filter_factory = nova.api.openstack.compute.limits:RateLimitingMiddleware.factory
复制代码

当 前比较常用的potocol 有:paste_app_factory,paste.filter_factory,paste.compsite_factory,paste.filter_app_factory, paste.server_factory,用来分别上述的6种section指定入口。


3. 加载 wsgi application
上面详细介绍了wsgi application的种类以及对应的entry的定义,下面来分析下如何server端如何来加载这些applications.
  1. paste.deploy.loadwsgi.loadapp(uri, name=None, **kw) //
  2. paste.deploy.loadwsgi.loadserver(uri, name=None, **kw) paste.deploy.loadwsgi.loadfilter(uri, name=None, **kw) paste.deploy.loadwsgi.appconfig(uri, name=None, relative_to=None, global_conf=None)
复制代码



4. nova的paste deployment配置文件分析
   前面提到,paste deployment就是完成从配置文件中加载WSGI App的功能,以nova-api为例,分析下都定义了哪些WSGI App以及每个App的含义。
   nova-api.ini主要定义了三个WSGI App: metadata, ec2和osapi_compute。
  1. ############
  2. # Metadata #
  3. ############
  4. ##metata复合application,入口为urlmap,根据url不同实现跳转
  5. [composite:metadata]
  6. use = egg:Paste#urlmap
  7. /: meta
  8. ## 一些列filter application组成的pipeline,即在执行application metaapp之前,执行每一个filter,执行顺序为靠近app的filter先执行
  9. [pipeline:meta]
  10. pipeline = ec2faultwrap logrequest metaapp
  11. ##wsgi application metaapp的入口函数为: nova.api.metadata.handler:MetadataRequestHandler.factory
  12. [app:metaapp]
  13. paste.app_factory = nova.api.metadata.handler:MetadataRequestHandler.factory
  14. #######
  15. # EC2 #
  16. #######
  17. [composite:ec2]
  18. use = egg:Paste#urlmap
  19. /services/Cloud: ec2cloud
  20. [composite:ec2cloud]
  21. use = call:nova.api.auth:pipeline_factory
  22. noauth = ec2faultwrap logrequest ec2noauth cloudrequest validator ec2executor
  23. keystone = ec2faultwrap logrequest ec2keystoneauth cloudrequest validator ec2executor
  24. [filter:ec2faultwrap]
  25. paste.filter_factory = nova.api.ec2:FaultWrapper.factory
  26. [filter:logrequest]
  27. paste.filter_factory = nova.api.ec2:RequestLogging.factory
  28. [filter:ec2lockout]
  29. paste.filter_factory = nova.api.ec2:Lockout.factory
  30. [filter:ec2keystoneauth]
  31. paste.filter_factory = nova.api.ec2:EC2KeystoneAuth.factory
  32. [filter:ec2noauth]
  33. paste.filter_factory = nova.api.ec2:NoAuth.factory
  34. [filter:cloudrequest]
  35. controller = nova.api.ec2.cloud.CloudController
  36. paste.filter_factory = nova.api.ec2:Requestify.factory
  37. [filter:authorizer]
  38. paste.filter_factory = nova.api.ec2:Authorizer.factory
  39. [filter:validator]
  40. paste.filter_factory = nova.api.ec2:Validator.factory
  41. [app:ec2executor]
  42. paste.app_factory = nova.api.ec2:Executor.factory
  43. #############
  44. # Openstack #
  45. #############
  46. ###nova-api service启动后,调用deploy.loadpp使用config方式来load section名为osapi_compute的应用,找到配置文件中的名为osapi_compute的section,调用nova.api.openstack.urlmap中的urlmap_factory函数,其他的三行作为local_conf参数传入。
  47. [composite:osapi_compute]
  48. use = call:nova.api.openstack.urlmap:urlmap_factory
  49. /: oscomputeversions
  50. /v1.1: openstack_compute_api_v2
  51. /v2: openstack_compute_api_v2
  52. /v3: openstack_compute_api_v3
  53. [composite:openstack_compute_api_v2]
  54. use = call:nova.api.auth:pipeline_factory
  55. noauth = faultwrap sizelimit noauth ratelimit osapi_compute_app_v2
  56. keystone = faultwrap sizelimit authtoken keystonecontext ratelimit osapi_compute_app_v2
  57. keystone_nolimit = faultwrap sizelimit authtoken keystonecontext osapi_compute_app_v2
  58. [composite:openstack_compute_api_v3]
  59. use = call:nova.api.auth:pipeline_factory_v3
  60. noauth = faultwrap sizelimit noauth_v3 osapi_compute_app_v3
  61. keystone = faultwrap sizelimit authtoken keystonecontext osapi_compute_app_v3
  62. [filter:faultwrap]
  63. paste.filter_factory = nova.api.openstack:FaultWrapper.factory
  64. [filter:noauth]
  65. paste.filter_factory = nova.api.openstack.auth:NoAuthMiddleware.factory
  66. [filter:noauth_v3]
  67. paste.filter_factory = nova.api.openstack.auth:NoAuthMiddlewareV3.factory
  68. [filter:ratelimit]
  69. paste.filter_factory = nova.api.openstack.compute.limits:RateLimitingMiddleware.factory
  70. [filter:sizelimit]
  71. paste.filter_factory = nova.api.sizelimit:RequestBodySizeLimiter.factory
  72. [app:osapi_compute_app_v2]
  73. paste.app_factory = nova.api.openstack.compute:APIRouter.factory
  74. [app:osapi_compute_app_v3]
  75. paste.app_factory = nova.api.openstack.compute:APIRouterV3.factory
  76. [pipeline:oscomputeversions]
  77. pipeline = faultwrap oscomputeversionapp
  78. [app:oscomputeversionapp]
  79. paste.app_factory = nova.api.openstack.compute.versions:Versions.factory
  80. ##########
  81. # Shared #
  82. ##########
  83. [filter:keystonecontext]
  84. paste.filter_factory = nova.api.auth:NovaKeystoneContext.factory
  85. [filter:authtoken]
  86. paste.filter_factory = keystoneclient.middleware.auth_token:filter_factory
复制代码




没找到任何评论,期待你打破沉寂

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

推荐上一条 /2 下一条