openstack 版本:Newton
注:博文图片采用了很多大牛博客图片,仅作为总结学习,非商用。
该图全面的说明了nova创建虚机的过程,从逻辑的角度清晰的描述了前端请求创建虚拟机之后发生的一系列反应。
上图是以逻辑的关系说明虚机创建的流程,并没有涉及到真正的代码,下面这张图则是从代码的角度来看虚机的创建过程。两张逻辑图相辅相成,能够快速对nova创建虚机流程有一个认识。
上一篇博文已经将nova创建虚机的流程分析到restful调用具体的函数。如下图所示,本篇将从该函数开始分析,展开nova服务的代码文件。
上图的create函数是nova api的入口函数,所有nova api的调用都是从该函数开始的。
从该函数开始到nova调用底层驱动创建虚拟机,要经过的调用流程如下:
- nova.api.openstack.compute.servers.py::Controller:create
- ->nova.compute.api.py::API:create
- ->nova.compute.api.py::API:_create_instance
- ->nova.comductor.ComputeTaskAPI.py::build_instances
- ->nova.conductor.rpcapi.py::ComputeTaskAPI:build_instances
- ->nova.conductor.manager.py::ComputeTaskAPI.py:build_instances
- ->nova.compute.rpcapi.ComputeManager.py::build_and_run_instance
- ->nova.compute.manager.py::ComputeManager:_do_build_and_run_instance
- ->nova.compute.manager.py::ComputeManager:_build_and_run_instance
- ->driver
nova.api.openstack.compute.servers.py:: Controller:create 指该create函数的所在路径是nova文件夹下的/nova/api/openstack/compute/servers.py中的create方法
同理:nova.compute.api.py::API:create 指该create方法在/nova/compute/api.py中的create方法。
所以这个调用的顺序就是如下图所示:
那么接下来就详细分析create()函数方法。
想要搞明白create函数做了什么,就要弄清楚函数传入的参数是什么。create传入了三个参数,分别是self,req,body。
其中self是类方法的标识,如果java类中函数的 "this" 字段。那么req、body分别是什么呢?使用python中print打印到文件的方法,输出了req与body的内容。
req
- POST /v2.1/servers HTTP/1.0
- Accept: application/json
- Accept-Encoding: gzip, deflate
- Connection: keep-alive
- Content-Length: 323
- Content-Type: application/json
- Host: 192.168.252.134:8774
- User-Agent: python-novaclient
- X-Auth-Project-Id: 836c0b02b549489eac82733267830e60
- X-Auth-Token: 31475f46361e41d49c3aa519531192c2
- X-Domain-Id: None
- X-Domain-Name: None
- X-Identity-Status: Confirmed
- X-Is-Admin-Project: False
- X-Project-Domain-Id: default
- X-Project-Domain-Name: Default
- X-Project-Id: 836c0b02b549489eac82733267830e60
- X-Project-Name: demo
- X-Role: Member,anotherrole
- X-Roles: Member,anotherrole
- X-Service-Catalog: [{"endpoints": [{"adminURL": "http://192.168.252.134:8774/v2.1", "region": "RegionOne", "internalURL": "http://192.168.252.134:8774/v2.1", "publicURL": "http://192.168.252.134:8774/v2.1"}], "type": "compute", "name": "nova"}, {"endpoints": [{"adminURL": "http://192.168.252.134:8774/v2/836c0b02b549489eac82733267830e60", "region": "RegionOne", "internalURL": "http://192.168.252.134:8774/v2/836c0b02b549489eac82733267830e60", "publicURL": "http://192.168.252.134:8774/v2/836c0b02b549489eac82733267830e60"}], "type": "compute_legacy", "name": "nova_legacy"}, {"endpoints": [{"adminURL": "http://192.168.252.134:8776/v2/836c0b02b549489eac82733267830e60", "region": "RegionOne", "internalURL": "http://192.168.252.134:8776/v2/836c0b02b549489eac82733267830e60", "publicURL": "http://192.168.252.134:8776/v2/836c0b02b549489eac82733267830e60"}], "type": "volumev2", "name": "cinderv2"}, {"endpoints": [{"adminURL": "http://192.168.252.134:9696/", "region": "RegionOne", "internalURL": "http://192.168.252.134:9696/", "publicURL": "http://192.168.252.134:9696/"}], "type": "network", "name": "neutron"}, {"endpoints": [{"adminURL": "http://192.168.252.134/identity_v2_admin", "region": "RegionOne", "internalURL": "http://192.168.252.134/identity", "publicURL": "http://192.168.252.134/identity"}], "type": "identity", "name": "keystone"}, {"endpoints": [{"adminURL": "http://192.168.252.134:8776/v1/836c0b02b549489eac82733267830e60", "region": "RegionOne", "internalURL": "http://192.168.252.134:8776/v1/836c0b02b549489eac82733267830e60", "publicURL": "http://192.168.252.134:8776/v1/836c0b02b549489eac82733267830e60"}], "type":