Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3360286
  • 博文数量: 258
  • 博客积分: 9440
  • 博客等级: 少将
  • 技术积分: 6998
  • 用 户 组: 普通用户
  • 注册时间: 2009-05-03 10:28
个人简介

-- linux爱好者,业余时间热衷于分析linux内核源码 -- 目前主要研究云计算和虚拟化相关的技术,主要包括libvirt/qemu,openstack,opennebula架构和源码分析。 -- 第五届云计算大会演讲嘉宾 微博:@Marshal-Liu

文章分类

全部博文(258)

文章存档

2016年(1)

2015年(4)

2014年(16)

2013年(22)

2012年(41)

2011年(59)

2010年(40)

2009年(75)

分类: 云计算

2014-02-13 14:59:25

    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

paste deploy 概念
1. settions
  paste deploy主要的任务就是搭建连接server与applications的桥梁,所以section就是为了application进行服务的,主要用来分类和描述不同用途的application.
(1)[app:name]
定义一个符合wsgi协议的应用,有几种使用方法:
#指向另外一个配置文件中的application
[app:myapp]
use = config:another_config_file.ini#app_name
# or any URI:
[app:myotherapp]
use = egg:MyApp
# or a callable from a module:
[app:mythirdapp]
use = call:my.project:myapplication
# or even another section:
[app:myfourthapp]
use = myotherapp
# or point to python code directly, and must use protocol currently
[app:myfifthapp]
paste.app_factory = myapp.modulename:app_factor
(2)[composite:name]
composite section也定义了一个application,与application section的区别是composite section是wsgi application的集合,可以包括其他application section中定义的application,最常用的用法是:
// 定义一个mapper URL的composite app,调用urlmap_factory方法,其参数是不同的URL,当加载了此app后,根据不同的URL,重定向到不同的wsgi application
[composite:osapi_compute]
use = call:nova.api.openstack.urlmap:urlmap_factory
/: oscomputeversions
/v1.1: openstack_compute_api_v2
/v2: openstack_compute_api_v2
/v3: openstack_compute_api_v3

[composite:openstack_compute_api_v2]
use = call:nova.api.auth:pipeline_factory
noauth = faultwrap sizelimit noauth ratelimit osapi_compute_app_v2
keystone = faultwrap sizelimit authtoken keystonecontext ratelimit osapi_compute_app_v2
keystone_nolimit = faultwrap sizelimit authtoken keystonecontext osapi_compute_app_v2

[composite:openstack_compute_api_v3]
use = call:nova.api.auth:pipeline_factory_v3
noauth = faultwrap sizelimit noauth_v3 osapi_compute_app_v3
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.
[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先执行。
[pipeline:oscomputeversions]
pipeline = faultwrap oscomputeversionapp
[filter:faultwrap]
paste.filter_factory = nova.api.openstack:FaultWrapper.factory
//filter的嵌套,执行顺序为:ratelimit --> keystonecontext --> authtoken --> sizelimit --> faultwrap (完全类似与decorator)
[composite:openstack_compute_api_v2]
use = call:nova.api.auth:pipeline_factory
noauth = faultwrap sizelimit noauth ratelimit osapi_compute_app_v2
keystone = faultwrap sizelimit authtoken keystonecontext ratelimit osapi_compute_app_v2
keystone_nolimit = faultwrap sizelimit authtoken keystonecontext osapi_compute_app_v2
(6)[server:name]

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

[filter:ratelimit]
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.
paste.deploy.loadwsgi.loadapp(uri, name=None, **kw) //
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


微博: @Marshal-Liu
Email: ustcdylan@gmail.com
阅读(11726) | 评论(0) | 转发(2) |
给主人留下些什么吧!~~