该文同时发布在[ceph中国社区],署名为Thomas,另外截图不是很清晰,有需要的读者,可以留言索要,谢谢!
在Openstack中创建云主机,首先得有镜像,而Glance模块提供镜像服务功能,包括:镜像的发现、检索及存储等,主要包括:glance-api和glance-registery两个服务,分别负责镜像的存储和元数据管理。下面基于源码,分析下镜像的上传过程。
上传镜像
先通过glance CLI上传镜像,来直观的了解下镜像上传的过程:
# glance --debug image-create --file centos-7.0-x64-20g.qcow2 --disk-format raw --container-format bare --visibility public
通过在命令行中开启--debug
,我们可以发现,上传镜像过程中glance CLI会发送如下三个不同的请求:
那这三个请求分别干了什么事呢? 下面我们来一一分解。
获取镜像属性定义
通过glance/api/v2/router.py.API
中定义的路由映射,我们知道上述的第一个请求由glance/api/v2/schemas.py.Controller.image
方法处理,如下:
#路由映射,代码节选
#完整的函数实现,请参考glance/api/v2/router.py.API.__init__
def __init__(self, mapper):
#从/etc/glance/schema-image.json文件加载用户定义属性
custom_image_properties = images.load_custom_properties()
#创建glance/api/v2/schemas.py.Controller实例
schemas_resource =
schemas.create_resource(custom_image_properties)
#定义路由映射:
#curl -X GET /schemas/image -> shemas_resource.image
mapper.connect('/schemas/image',
controller=schemas_resource,
action='image',
conditions={
'method': ['GET']})
#glance/api/v2/schemas.py.Controller.image
"""返回一个镜像属性字典,结构如下:
[
'name': image,
'properties':xxx
'additionalProperties':xxx
'definitions':xxx
'required':xxx
'links':xxx
]
"""字典值请看下面的分析过程