`

Glance源码架构探秘(一)

阅读更多

        Glance是OpenStack的镜像管理模块,负责镜像的上传、下载等管理。

        Glance项目提供虚拟机镜像的查找、注册和重现,使用RESTful接口接受虚拟机镜像管理的查询请求。

  423x445

Glance-API和Glance-Registry是两个独立运行的服务,在sbin目录下,看过glance部署文章的同学知道,glance部署的最后一步就是启动glance-api和glance-registry这两个服务。这两个服务作为WSGI,会分别响应来自用户的RESTful请求,

Glance对外服务的入口主要有两个

1、Glance-API:主要负责接收响应镜像管理命令的Restful请求,分析消息请求信息并分发其所带的命令(如新增、删除、更新元数据等)。默认绑定端口是9292。

2、Glance-Registry:主要负责接收响应镜像元数据命令的Restful请求,分析消息请求信息并分发其所带的命令(如获取元数据,更新元数据等)。默认绑定端口是9191。

在/etc/glance目录下有两个文件glance-api-paste.ini和glance-registry-paste.ini,两个入口服务启动后,会用配置文件分析模块读取这两个文件,执行其文件中标注的需要执行的WSGIapp和middleware。简述paste文件中的概念:

  app:实际处理REST API请求的python类。
filter:一种装饰器,为app提供一层封装,在app处理请求之前会先调用filter的对象。
pipeline:所对应的对象是对filter和app的的封装,他将多个filter和某个app绑在一起,在app处理请求之前要先通过pipline指定的在app之前的filter的处理。

  在glance-registry中有

  [app:registryapp] paste.app_factory = glance.registry.api.v1:API.factory

证明这是一个独立的服务程序

  我们以glance-api为例,分析其服务入口程序

 """ Glance API Server """ 
 import gettext 
 import os 
 import sys 
 # If ../glance/__init__.py exists, add ../ to Python search path, so that 
 # it will override what happens to be installed in /usr/(local/)lib/python... 
 possible_topdir = os.path.normpath(os.path.join(os.path.abspath(sys.argv[0]), os.pardir, os.pardir)) 
 if os.path.exists(os.path.join(possible_topdir, 'glance', '__init__.py')): 
     sys.path.insert(0, possible_topdir) 
     gettext.install('glance', unicode=1) 
     from glance.common import config 
     from glance.common import wsgi 
     from glance.common import exception 
     from glancmon import log 
     import glance.store 
     def fail(returncode, e): 
         sys.stderr.write("ERROR: %s\n" % e) 
         sys.exit(returncode) 
         if __name__ == '__main__': 
             try: 
                 config.parse_args() 
                 log.setup('glance') 
                 glance.store.create_stores() 
                 glance.store.verify_default_store() 
                 server = wsgi.Server() 
                 server.start(config.load_paste_app(), default_port=9292) 
                 server.wait() 
             except exception.WorkerCreationFailure, e: 
                 fail(2, e) 
             except RuntimeError, e: 
                 fail(1, e)       

     首先程序会调用glance.store.create_stores,注册与后端存储(swift、s3、filesystem等)相关的控制模块,并校验默认存储方式。

紧接着启动一个Http Server,Openstack的WSGI SERVER用到了eventlet这个绿色线程的组件。通过config.load_paste_app()方法,会从上面提到的glance-api-paste.ini配置文件中读取要启动的WSGI app,并在server中启动,并让server线程等待接收http rest查询响应。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics