Horizon 原始碼閱讀(四)—— 呼叫Novaclient流程
一、寫在前面
這篇文章主要介紹一下OpenStack(Kilo)關於horizon 呼叫NovaClient的一個分析。
如果轉載,請保留作者資訊。
原文地址:http://blog.csdn.net/u011521019/article/details/48324739
二、novaclient目錄結構
novaclient/
|---client.py --------主要提供HTTPClient類,也提供根據版本建立Client物件的函式
|---base.py --------提供基本的Manager基類
|---shell.py --------命令解析,建立相應版本的Client類物件,呼叫相應版本的shell.py中的函式
...
|---v2
|---client.py ---------版本Client類,擁有一系列Manager類物件,這些Manager可以呼叫相應的元件
|---flavors.py --------具體的Manager類,使用HTTPClient物件與對應的元件進行通訊
...
|---shell.py ————提供每個Command對應的方法
三、以建立虛擬機器為例分析原始碼
/openstack_dashboard/api/nova.py
horizon 呼叫APi建立虛擬機器
- def server_create(request, name, image, flavor, key_name, user_data,
- security_groups, block_device_mapping=None,
-
block_device_mapping_v2=None
- availability_zone=None, instance_count=1, admin_pass=None,
- disk_config=None, config_drive=None, meta=None):
- return Server(novaclient(request).servers.create(
- name, image, flavor, userdata=user_data,
- security_groups=security_groups,
- key_name=key_name, block_device_mapping=block_device_mapping,
- block_device_mapping_v2=block_device_mapping_v2,
- nics=nics, availability_zone=availability_zone,
- min_count=instance_count, admin_pass=admin_pass,
- disk_config=disk_config, config_drive=config_drive,
- meta=meta), request)
返回一個建立後的Server物件,呼叫novaclient(request).servers.create()傳入引數,傳送建立虛擬機器的請求。
novaclient(request).servers.create(
name, image, flavor….), request
呼叫流程:
novaclient(request)-> servers -> create
1、novaclient(request):返回一個novaclient物件。
/openstack_dashboard/api/nova.py
- def novaclient(request):
- # 獲取是否SSL證書檢查,預設是禁用
- insecure = getattr(settings, 'OPENSTACK_SSL_NO_VERIFY', False)
- #獲取CA證書使用來驗證SSL連線,預設是None
- cacert = getattr(settings, 'OPENSTACK_SSL_CACERT', None)
- #from novaclient.v2 import client as nova_client 返回Client類
- <strong>[1]</strong>c = nova_client.Client(request.user.username,
- request.user.token.id,
- project_id=request.user.tenant_id,
- auth_url=base.url_for(request, 'compute'),
- insecure=insecure,
- cacert=cacert,
- http_log_debug=settings.DEBUG)
- #設定Token ID 值
- c.client.auth_token = request.user.token.id
- #設定訪問地址:例如 http://hty-nova:8774/v2/ea4d1859494c490495b027f174de307c
- c.client.management_url = base.url_for(request, 'compute')
- return c
novaclient/v2/__init__.py
from novaclient.v2.client import Client
[1]處程式碼分析,返回一個Client物件
- class Client(object):
- """
- 頂級物件訪問OpenStack計算API。
- Top-level object to access the OpenStack Compute API.
- """
- def __init__(self, username=None, api_key=None, project_id=None,
- auth_url=None, insecure=False, timeout=None,
- ...):
- password = api_key
- self.projectid = project_id
- ...
- # extensions 擴充套件
- self.agents = agents.AgentsManager(self)
- self.dns_domains = floating_ip_dns.FloatingIPDNSDomainManager(self)
- ...
- # Add in any extensions...在新增任何擴充套件
- if extensions:
- for extension in extensions:
- if extension.manager_class:
- setattr(self, extension.name,
- extension.manager_class(self))
- #構建HTTP客戶端
- self.client = client._construct_http_client(
- username=username,
- password=password,
- ...
- **kwargs)
- ...
這個client裡面有一個Client類,擁有一堆的Manager負責管理各種資源,只需引用這些Manager就可以操作資源,然後建立一系列的Manager類來負責處理資源,在這些Manager類中主要使用HTTPClient來發送請求對相應的元件進行操作,最後,將client版本能夠實現的功能封裝成函式,這些函式進而能夠被相應的command呼叫。
2、novaclient(request).servers.create():
novaclient/v2/client.py
引用ServerManager操作server
- class Client(object):
- def __init__(self, username=None, api_key=None, project_id=None,…)
- ….
- #負責管理servers,只需引用Manager就可以操作servers
- self.servers = servers.ServerManager(self)
- …
novaclient/v2/servers.py
建立虛擬機器create() 函式
- class ServerManager(base.BootingManagerWithFind):
- resource_class = Server # 資源類,上文定義
- def create(self, name, image, flavor, meta=None, files=None,
- reservation_id=None, min_count=None,
- max_count=None, security_groups=None, userdata=None,
- key_name=None, availability_zone=None,
- block_device_mapping=None, block_device_mapping_v2=None,
- nics=None, scheduler_hints=None,
- config_drive=None, disk_config=None, **kwargs):
- """
- Create (boot) a new server.建立(啟動)新的伺服器。
- """
- #判斷虛擬機器建立數量
- ifnot min_count:
- min_count = 1
- ifnot max_count:
- max_count = min_count
- if min_count > max_count:
- min_count = max_count
- # 組拼引數
- boot_args = [name, image, flavor]
- boot_kwargs = dict(
- meta=meta, files=files, userdata=userdata,