1. 程式人生 > >Horizon 原始碼閱讀(四)—— 呼叫Novaclient流程

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建立虛擬機器

  1. def server_create(request, name, image, flavor, key_name, user_data,  
  2.                   security_groups, block_device_mapping=None,  
  3.                   block_device_mapping_v2=None
    , nics=None,  
  4.                   availability_zone=None, instance_count=1, admin_pass=None,  
  5.                   disk_config=None, config_drive=None, meta=None):  
  6.     return Server(novaclient(request).servers.create(  
  7.         name, image, flavor, userdata=user_data,  
  8.         security_groups=security_groups,  
  9.         key_name=key_name, block_device_mapping=block_device_mapping,  
  10.         block_device_mapping_v2=block_device_mapping_v2,  
  11.         nics=nics, availability_zone=availability_zone,  
  12.         min_count=instance_count, admin_pass=admin_pass,  
  13.         disk_config=disk_config, config_drive=config_drive,  
  14.         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

  1. def novaclient(request):  
  2.     # 獲取是否SSL證書檢查,預設是禁用
  3.     insecure = getattr(settings, 'OPENSTACK_SSL_NO_VERIFY'False)  
  4.     #獲取CA證書使用來驗證SSL連線,預設是None
  5.     cacert = getattr(settings, 'OPENSTACK_SSL_CACERT'None)  
  6.     #from novaclient.v2 import client as nova_client 返回Client類
  7.     <strong>[1]</strong>c = nova_client.Client(request.user.username,  
  8.                            request.user.token.id,  
  9.                            project_id=request.user.tenant_id,  
  10.                            auth_url=base.url_for(request, 'compute'),  
  11.                            insecure=insecure,  
  12.                            cacert=cacert,  
  13.                            http_log_debug=settings.DEBUG)  
  14.     #設定Token ID 值
  15.     c.client.auth_token = request.user.token.id  
  16.     #設定訪問地址:例如 http://hty-nova:8774/v2/ea4d1859494c490495b027f174de307c
  17.     c.client.management_url = base.url_for(request, 'compute')  
  18.     return c  

novaclient/v2/__init__.py

from novaclient.v2.client import Client 

[1]處程式碼分析,返回一個Client物件

  1. class Client(object):  
  2.     """ 
  3.     頂級物件訪問OpenStack計算API。 
  4.     Top-level object to access the OpenStack Compute API. 
  5.     """
  6.     def __init__(self, username=None, api_key=None, project_id=None,  
  7.                  auth_url=None, insecure=False, timeout=None,  
  8.                  ...):  
  9.         password = api_key  
  10.         self.projectid = project_id  
  11.         ...  
  12.         # extensions 擴充套件
  13.         self.agents = agents.AgentsManager(self)  
  14.         self.dns_domains = floating_ip_dns.FloatingIPDNSDomainManager(self)  
  15.         ...  
  16.         # Add in any extensions...在新增任何擴充套件
  17.         if extensions:  
  18.             for extension in extensions:  
  19.                 if extension.manager_class:  
  20.                     setattr(self, extension.name,  
  21.                             extension.manager_class(self))  
  22.         #構建HTTP客戶端
  23.         self.client = client._construct_http_client(  
  24.             username=username,  
  25.             password=password,  
  26.             ...  
  27.             **kwargs)  
  28. ...  

這個client裡面有一個Client類,擁有一堆的Manager負責管理各種資源,只需引用這些Manager就可以操作資源,然後建立一系列的Manager類來負責處理資源,在這些Manager類中主要使用HTTPClient來發送請求對相應的元件進行操作,最後,將client版本能夠實現的功能封裝成函式,這些函式進而能夠被相應的command呼叫。

2、novaclient(request).servers.create():

novaclient/v2/client.py

引用ServerManager操作server 

  1. class Client(object):  
  2.     def __init__(self, username=None, api_key=None, project_id=None,…)  
  3.              ….  
  4.                   #負責管理servers,只需引用Manager就可以操作servers
  5.                    self.servers = servers.ServerManager(self)  
  6.                    …  

novaclient/v2/servers.py

建立虛擬機器create() 函式

  1. class ServerManager(base.BootingManagerWithFind):  
  2.     resource_class = Server # 資源類,上文定義
  3.     def create(self, name, image, flavor, meta=None, files=None,  
  4.                reservation_id=None, min_count=None,  
  5.                max_count=None, security_groups=None, userdata=None,  
  6.                key_name=None, availability_zone=None,  
  7.                block_device_mapping=None, block_device_mapping_v2=None,  
  8.                nics=None, scheduler_hints=None,  
  9.                config_drive=None, disk_config=None, **kwargs):  
  10.         """ 
  11.         Create (boot) a new server.建立(啟動)新的伺服器。 
  12.         """
  13.        #判斷虛擬機器建立數量
  14.         ifnot min_count:  
  15.             min_count = 1
  16.         ifnot max_count:  
  17.             max_count = min_count  
  18.         if min_count > max_count:  
  19.             min_count = max_count  
  20.         # 組拼引數
  21.         boot_args = [name, image, flavor]  
  22.         boot_kwargs = dict(  
  23.             meta=meta, files=files, userdata=userdata,