系統整體頁面:

代碼結構:

horizon采用django框架編寫(django是一個強大的mvc 框架。具體參考djangobook中文版 http://djangobook.py3k.cn/2.0/。)
左側面板布局:

代碼:
- vim /usr/share/openstack-dashboard/openstack_dashboard/dashboards/admin/dashboard.py

實例列表布局:

- vim /usr/share/openstack-dashboard/openstack_dashboard/dashboards/project/instances/tables.py

上述三個按鈕在table_actions中定義。

上述按鈕在row_actions中定義。
第一部分:openstack_dashboard調用流程
接下來以resize instance 為例:

重點關注下圖url鏈接:

1、urls.py層代碼分析:
所有頁面的url鏈接都由urls.py處理:
- vim /usr/share/openstack-dashboard/openstack_dashboard/dashboards/project/instances/urls.py

找到risize對應的url行。調用views.ResizeView.as_view().
2、views.py層代碼分析詳解--跟進到views層的ResizeView:
- #說明resize功能使用workflow工作,下一步詳解,請參考:workflow-1.1
- workflow_class = project_workflows.ResizeInstance
- #執行成功跳轉到horizon:project:instances:index鏈接文件:下一步詳解,請參考:模板頁面-1.1
- success_url = reverse_lazy("horizon:project:instances:index")
- #這個函數返回的值給context這個字典,這個字典里的值可以在html中取到
- @memoized.memoized_method
- def get_object(self, *args, **kwargs):
- instance_id = self.kwargs['instance_id']
- try:#根據實例id獲取實例,下一步詳解,下一步api調用流程請參考:“horizon中實例resize的API調用步驟詳解“(與之類似)。
- instance = api.nova.server_get(self.request, instance_id)
- flavor_id = instance.flavor['id']
- flavors = self.get_flavors()
- if flavor_id in flavors:
- instance.flavor_name = flavors[flavor_id].name
- else:
- flavor = api.nova.flavor_get(self.request, flavor_id)
- instance.flavor_name = flavor.name
- except Exception:
- redirect = reverse("horizon:project:instances:index")
- msg = _('Unable to retrieve instance details.')
- exceptions.handle(self.request, msg, redirect=redirect)
- return instance
- #這個函數主要用來填充context字典,該字典可以和對應的html模板傳參
- def get_context_data(self, **kwargs):
- context = super(ResizeView, self).get_context_data(**kwargs)
- context["instance_id"] = self.kwargs['instance_id']
- return context
- #獲取flavors list
- @memoized.memoized_method
- def get_flavors(self, *args, **kwargs):
- try:
- #獲取數據庫中已存在的flavors,下一步api調用流程請參考:“horizon中實例resize的API調用步驟詳解“(與之類似)。
- flavors = api.nova.flavor_list(self.request)
- return SortedDict((str(flavor.id), flavor) for flavor in flavors)
- except Exception:
- redirect = reverse("horizon:project:instances:index")
- exceptions.handle(self.request,
- _('Unable to retrieve flavors.'), redirect=redirect)
- #獲取初始化數據,為對應的表單forms.py提供數據。(譬如下拉框數據如下圖1。)
- def get_initial(self):
- initial = super(ResizeView, self).get_initial()
- _object = self.get_object()
- if _object:
- initial.update({'instance_id': self.kwargs['instance_id'],
- 'name': getattr(_object, 'name', None),
- 'old_flavor_id': _object.flavor['id'],
- 'old_flavor_name': getattr(_object, 'flavor_name', ''),
- 'flavors': self.get_flavors()}) #此處調用上處get_flavors函數
- return initial
圖1:

1、詳解resize功能的workflow機制:
- class ResizeView類中:workflow_class = project_workflows.ResizeInstance
- vim /usr/share/openstack-dashboard/openstack_dashboard/dashboards/project/instances/workflows/resize_instance.py

- #成功與失敗的彈出消息及成功之后的跳轉url地址。
- success_message = _('Scheduled resize of instance "%s".')
- failure_message = _('Unable to resize instance "%s".')
- success_url = "horizon:project:instances:index"
- #完成resize工作流兩個步驟:(如下圖2)
- default_steps = (SetFlavorChoice, create_instance.SetAdvanced)
- #格式化狀態消息輸出
- def format_status_message(self, message):
- return message % self.context.get('name', 'unknown instance')
- #完成resize工作流兩個步驟:(如下圖2)
- @sensitive_variables('context')
- def handle(self, request, context):
- #從views.py層中的context字典獲取數據
- instance_id = context.get('instance_id', None)
- flavor = context.get('flavor', None)
- disk_config = context.get('disk_config', None)
- try: #頁面點擊確認resize按鈕,調用api resize 實例。本步驟將重點分析。下一步詳情,請參考horizon中實例resize的API調用步驟詳解:
- api.nova.server_resize(request, instance_id, flavor, disk_config)
- return True
- except Exception:
- exceptions.handle(request)
- return False
圖2:

模板頁面-1.1:
模板頁面文件位置:
- vim /usr/share/openstack-dashboard/openstack_dashboard/dashboards/project/instances/templates/instances/index.html

其中%%代表要替換掉的變量(若有疑問自行參考djangobook文檔)。
table.render:表示將model層的數據渲染到頁面。
horizon中實例resize的API調用步驟詳解:
接着上述workflow中resize
- api.nova.server_resize(request, instance_id, flavor, disk_config)
根據import原則找到api文件:
- vim /usr/share/openstack-dashboard/openstack_dashboard/api/__init__.py

此處將base、ceilometer、keystone等 都導入進來了。
跟蹤到nova.api代碼中:找到server_resize方法:
- def server_resize(request, instance_id, flavor, disk_config=None, **kwargs):
- novaclient(request).servers.resize(instance_id, flavor,
- disk_config, **kwargs)
- #拼裝client,組裝url參數(包括keystone的token,以及調用v1_1、還是v3版本的novaclient參數等等)
- def novaclient(request):
- insecure = getattr(settings, 'OPENSTACK_SSL_NO_VERIFY', False)
- cacert = getattr(settings, 'OPENSTACK_SSL_CACERT', None)
- LOG.debug('novaclient connection created using token "%s" and url "%s"' %
- (request.user.token.id, base.url_for(request, 'compute')))
- #根據返回的c知道接下來調用的是novaclient代碼:因此上述方法server_resize調用novaclient中server.py中的resize方法
下述流程則參考以下的novaclientAPI調用流程。
- 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)
- c.client.auth_token = request.user.token.id
- c.client.management_url = base.url_for(request, 'compute')
- return c
第二部分:novaclientAPI調用流程
從dashboard代碼入口開始:
- def server_resize(request, instance_id, flavor, disk_config=None, **kwargs):
- novaclient(request).servers.resize(instance_id, flavor,
- disk_config, **kwargs)
下圖為novaclient的代碼結構:
接着調用novaclient模塊中v1_1里面的services.py文件resize方法。

manage里面的resize方法:

調用action方法:

#下面兩句代碼拼接一個url地址
- url = '/servers/%s/action' % base.getid(server)
- return self.api.client.post(url, body=body)
- #發送url,通過wsgi調用nova代碼。