Selaa lähdekoodia

磁盘分离完毕

James Iter 8 vuotta sitten
vanhempi
sitoutus
8558fddf34
5 muutettua tiedostoa jossa 98 lisäystä ja 72 poistoa
  1. 42 0
      api/disk.py
  2. 1 0
      api_route_table.py
  3. 6 0
      models/guest.py
  4. 41 41
      templates/disk_detail.html
  5. 8 31
      views/disk.py

+ 42 - 0
api/disk.py

@@ -1,5 +1,7 @@
 #!/usr/bin/env python
 # -*- coding: utf-8 -*-
+
+
 from math import ceil
 
 from flask import Blueprint, request, url_for
@@ -14,6 +16,7 @@ from models import Config
 from models import Disk
 from models import Rules
 from models import Utils
+from models import OSTemplateImage
 from models.status import StorageMode
 
 from base import Base
@@ -611,3 +614,42 @@ def r_show():
     return ret
 
 
+@Utils.dumps2response
+def r_detail(uuid):
+    disk = Disk()
+    disk.uuid = uuid
+    disk.get_by(field='uuid')
+    disk.wrap_device(dev_table=dev_table)
+
+    guest = None
+    os_template_image = None
+
+    config = Config()
+    config.id = 1
+    config.get()
+
+    if disk.sequence != -1:
+        guest = Guest()
+        guest.uuid = disk.guest_uuid
+        guest.get_by('uuid')
+
+        os_template_image = OSTemplateImage()
+        os_template_image.id = guest.os_template_image_id
+        os_template_image.get()
+
+        guest = guest.__dict__
+        os_template_image = os_template_image.__dict__
+
+    ret = dict()
+    ret['state'] = ji.Common.exchange_state(20000)
+
+    ret['data'] = {
+        'guest': guest,
+        'os_template_image': os_template_image,
+        'disk': disk.__dict__,
+        'config': config.__dict__
+    }
+
+    return ret
+
+

+ 1 - 0
api_route_table.py

@@ -133,6 +133,7 @@ add_rule_api(disk.blueprints, '', api_func='disk.r_get_by_filter', methods=['GET
 add_rule_api(disk.blueprints, '/_search', api_func='disk.r_content_search', methods=['GET'])
 add_rule_api(disk.blueprints, '/_distribute_count', api_func='disk.r_distribute_count', methods=['GET'])
 add_rule_api(disk.blueprints, '/_show', api_func='disk.r_show', methods=['GET'])
+add_rule_api(disk.blueprint, '/_detail/<uuid>', api_func='disk.r_detail', methods=['GET'])
 
 # 日志查询
 # 系统模板操作

+ 6 - 0
models/guest.py

@@ -126,6 +126,12 @@ class Disk(ORM):
         self.bps_rd = 0
         self.bps_wr = 0
 
+    def wrap_device(self, dev_table):
+        self.device = None
+
+        if self.sequence >= 0:
+            self.device = u'/dev/' + dev_table[self.sequence]
+
     @staticmethod
     def get_filter_keywords():
         return {

+ 41 - 41
templates/disk_detail.html

@@ -399,7 +399,7 @@
     }
 
     function render_disks_chart(uuid, granularity) {
-        var dev_name = '{{ disk_ret.data.device }}';
+        var dev_name = '{{ disk.device }}';
         var dev_id_throughput = 'disk_throughput_chart';
         var dev_id_iops = 'disk_iops_chart';
 
@@ -465,7 +465,7 @@
 
     $(function() { "use strict";
         $("#iops").TouchSpin({
-            max: {{ config_ret.data.iops_cap }},
+            max: {{ config.iops_cap }},
             min: 0,
             verticalbuttons: true,
             verticalupclass: 'glyph-icon icon-plus',
@@ -475,7 +475,7 @@
 
     $(function() { "use strict";
         $("#iops_rd").TouchSpin({
-            max: {{ config_ret.data.iops_cap - disk_ret.data.iops_wr }},
+            max: {{ config.iops_cap - disk.iops_wr }},
             min: 0,
             verticalbuttons: true,
             verticalupclass: 'glyph-icon icon-plus',
@@ -485,7 +485,7 @@
 
     $(function() { "use strict";
         $("#iops_wr").TouchSpin({
-            max: {{ config_ret.data.iops_cap - disk_ret.data.iops_rd }},
+            max: {{ config.iops_cap - disk.iops_rd }},
             min: 0,
             verticalbuttons: true,
             verticalupclass: 'glyph-icon icon-plus',
@@ -495,7 +495,7 @@
 
     $(function() { "use strict";
         $("#iops_max").TouchSpin({
-            max: {{ config_ret.data.iops_max }},
+            max: {{ config.iops_max }},
             min: 0,
             verticalbuttons: true,
             verticalupclass: 'glyph-icon icon-plus',
@@ -505,7 +505,7 @@
 
     $(function() { "use strict";
         $("#iops_max_length").TouchSpin({
-            max: {{ config_ret.data.iops_max_length }},
+            max: {{ config.iops_max_length }},
             min: 0,
             postfix: '秒',
             verticalbuttons: true,
@@ -516,7 +516,7 @@
 
     $(function() { "use strict";
         $("#bps").TouchSpin({
-            max: {{ (config_ret.data.bps_cap / 1024 / 1024) | int }},
+            max: {{ (config.bps_cap / 1024 / 1024) | int }},
             min: 0,
             postfix: 'MiB',
             verticalbuttons: true,
@@ -527,7 +527,7 @@
 
     $(function() { "use strict";
         $("#bps_rd").TouchSpin({
-            max: {{ ((config_ret.data.bps_cap - disk_ret.data.bps_wr) / 1024 / 1024) | int }},
+            max: {{ ((config.bps_cap - disk.bps_wr) / 1024 / 1024) | int }},
             min: 0,
             postfix: 'MiB',
             verticalbuttons: true,
@@ -538,7 +538,7 @@
 
     $(function() { "use strict";
         $("#bps_wr").TouchSpin({
-            max: {{ ((config_ret.data.bps_cap - disk_ret.data.bps_rd) / 1024 / 1024) | int }},
+            max: {{ ((config.bps_cap - disk.bps_rd) / 1024 / 1024) | int }},
             min: 0,
             postfix: 'MiB',
             verticalbuttons: true,
@@ -549,7 +549,7 @@
 
     $(function() { "use strict";
         $("#bps_max").TouchSpin({
-            max: {{ (config_ret.data.bps_max / 1024 / 1024) | int }},
+            max: {{ (config.bps_max / 1024 / 1024) | int }},
             min: 0,
             postfix: 'MiB',
             verticalbuttons: true,
@@ -560,7 +560,7 @@
 
     $(function() { "use strict";
         $("#bps_max_length").TouchSpin({
-            max: {{ config_ret.data.bps_max_length }},
+            max: {{ config.bps_max_length }},
             min: 0,
             postfix: '秒',
             verticalbuttons: true,
@@ -621,7 +621,7 @@
                             <td>
                                 <span class="guest-label">UUID:&nbsp;&nbsp;&nbsp;&nbsp;</span>
                                 <span class="guest-desc">
-                                {{ disk_ret.data.uuid }}
+                                {{ disk.uuid }}
                                 </span>
                             </td>
                         </tr>
@@ -629,7 +629,7 @@
                             <td>
                                 <span class="guest-label">名称:&nbsp;&nbsp;&nbsp;&nbsp;</span>
                                 <span class="guest-desc">
-                                {{ disk_ret.data.remark }}
+                                {{ disk.remark }}
                                 </span>
                             </td>
                         </tr>
@@ -637,7 +637,7 @@
                             <td>
                                 <span class="guest-label">状态:&nbsp;&nbsp;&nbsp;&nbsp;</span>
                                 <span class="guest-desc">
-                                {{ format_disk_state(disk_ret.data.state)|safe }}
+                                {{ format_disk_state(disk.state)|safe }}
                                 </span>
                             </td>
                         </tr>
@@ -645,7 +645,7 @@
                             <td>
                                 <span class="guest-label">磁盘性别:&nbsp;&nbsp;&nbsp;&nbsp;</span>
                                 <span class="guest-desc">
-                                    {% if disk_ret.data.sequence == 0 %}
+                                    {% if disk.sequence == 0 %}
                                         系统盘
                                     {% else %}
                                         数据盘
@@ -657,7 +657,7 @@
                             <td>
                                 <span class="guest-label">磁盘容量:&nbsp;&nbsp;&nbsp;&nbsp;</span>
                                 <span class="guest-desc">
-                                {{ disk_ret.data.size }} GB
+                                {{ disk.size }} GB
                                 </span>
                             </td>
                         </tr>
@@ -665,8 +665,8 @@
                             <td>
                                 <span class="guest-label">所属虚拟机:&nbsp;&nbsp;&nbsp;&nbsp;</span>
                                 <span class="guest-desc">
-                                    {% if disk_ret.data.sequence != -1 %}
-                                        <a href="/guest/detail/{{ disk_ret.data.guest_uuid }}">{{ guest_ret.data.label }}/{{ guest_ret.data.remark }}</a>
+                                    {% if disk.sequence != -1 %}
+                                        <a href="/guest/detail/{{ disk.guest_uuid }}">{{ guest.label }}/{{ guest.remark }}</a>
                                     {% else %}
                                     {% endif %}
                                 </span>
@@ -676,8 +676,8 @@
                             <td>
                                 <span class="guest-label">设备路径:&nbsp;&nbsp;&nbsp;&nbsp;</span>
                                 <span class="guest-desc">
-                                    {% if disk_ret.data.sequence >= 0 %}
-                                        {{ disk_ret.data.device }}
+                                    {% if disk.sequence >= 0 %}
+                                        {{ disk.device }}
                                     {% endif %}
                                 </span>
                             </td>
@@ -704,11 +704,11 @@
                                     </thead>
                                     <tbody>
                                     <tr class="guest-desc">
-                                        <th style="text-align: center;">{{ disk_ret.data.iops }}</th>
-                                        <th style="text-align: center;">{{ disk_ret.data.iops_rd }}</th>
-                                        <th style="text-align: center;">{{ disk_ret.data.iops_wr }}</th>
-                                        <th style="text-align: center;">{{ disk_ret.data.iops_max }}</th>
-                                        <th style="text-align: center;">{{ disk_ret.data.iops_max_length }} 秒</th>
+                                        <th style="text-align: center;">{{ disk.iops }}</th>
+                                        <th style="text-align: center;">{{ disk.iops_rd }}</th>
+                                        <th style="text-align: center;">{{ disk.iops_wr }}</th>
+                                        <th style="text-align: center;">{{ disk.iops_max }}</th>
+                                        <th style="text-align: center;">{{ disk.iops_max_length }} 秒</th>
                                     </tr>
                                     </tbody>
                                     <thead>
@@ -722,11 +722,11 @@
                                     </thead>
                                     <tbody>
                                     <tr class="guest-desc">
-                                        <th style="text-align: center;">{{ (disk_ret.data.bps / 1024 / 1024) | int }} MiB</th>
-                                        <th style="text-align: center;">{{ (disk_ret.data.bps_rd / 1024 / 1024) | int }} MiB</th>
-                                        <th style="text-align: center;">{{ (disk_ret.data.bps_wr / 1024 / 1024) | int }} MiB</th>
-                                        <th style="text-align: center;">{{ (disk_ret.data.bps_max / 1024 / 1024) | int }} MiB</th>
-                                        <th style="text-align: center;">{{ disk_ret.data.bps_max_length }} 秒</th>
+                                        <th style="text-align: center;">{{ (disk.bps / 1024 / 1024) | int }} MiB</th>
+                                        <th style="text-align: center;">{{ (disk.bps_rd / 1024 / 1024) | int }} MiB</th>
+                                        <th style="text-align: center;">{{ (disk.bps_wr / 1024 / 1024) | int }} MiB</th>
+                                        <th style="text-align: center;">{{ (disk.bps_max / 1024 / 1024) | int }} MiB</th>
+                                        <th style="text-align: center;">{{ disk.bps_max_length }} 秒</th>
                                     </tr>
                                     </tbody>
                                 </table>
@@ -736,7 +736,7 @@
                             <td>
                                 <span class="guest-label">创建时间:&nbsp;&nbsp;&nbsp;&nbsp;</span>
                                 <span class="guest-desc">
-                                {{ format_datetime_by_tus(disk_ret.data.create_time) }}
+                                {{ format_datetime_by_tus(disk.create_time) }}
                                 </span>
                             </td>
                         </tr>
@@ -781,12 +781,12 @@
                             &nbsp;&nbsp;IOPS
                         </label>
                         <div class="col-sm-3">
-                            <input id="iops" title="IOPS" class="form-control" type="text" value="{{ disk_ret.data.iops }}" name="iops">
+                            <input id="iops" title="IOPS" class="form-control" type="text" value="{{ disk.iops }}" name="iops">
                         </div>
                         <span class="col-sm-1"></span>
                         <label class="col-sm-2 control-label"><span class="glyph-icon icon-openid"></span>&nbsp;&nbsp;BPS</label>
                         <div class="col-sm-3">
-                            <input id="bps" title="BPS" class="form-control" type="text" value="{{ (disk_ret.data.bps / 1024 / 1024) | int }}" name="bps">
+                            <input id="bps" title="BPS" class="form-control" type="text" value="{{ (disk.bps / 1024 / 1024) | int }}" name="bps">
                         </div>
                         <span class="col-sm-1"></span>
                     </div>
@@ -796,12 +796,12 @@
                             &nbsp;&nbsp;IOPS 读
                         </label>
                         <div class="col-sm-3">
-                            <input id="iops_rd" title="IOPS 读" class="form-control" type="text" value="{{ disk_ret.data.iops_rd }}" name="iops_rd">
+                            <input id="iops_rd" title="IOPS 读" class="form-control" type="text" value="{{ disk.iops_rd }}" name="iops_rd">
                         </div>
                         <span class="col-sm-1"></span>
                         <label class="col-sm-2 control-label"><span class="glyph-icon icon-openid"></span>&nbsp;&nbsp;BPS 读</label>
                         <div class="col-sm-3">
-                            <input id="bps_rd" title="BPS 读" class="form-control" type="text" value="{{ (disk_ret.data.bps_rd / 1024 / 1024) | int }}" name="bps_rd">
+                            <input id="bps_rd" title="BPS 读" class="form-control" type="text" value="{{ (disk.bps_rd / 1024 / 1024) | int }}" name="bps_rd">
                         </div>
                         <span class="col-sm-1"></span>
                     </div>
@@ -811,12 +811,12 @@
                             &nbsp;&nbsp;IOPS 写
                         </label>
                         <div class="col-sm-3">
-                            <input id="iops_wr" title="IOPS 写" class="form-control" type="text" value="{{ disk_ret.data.iops_wr }}" name="iops_wr">
+                            <input id="iops_wr" title="IOPS 写" class="form-control" type="text" value="{{ disk.iops_wr }}" name="iops_wr">
                         </div>
                         <span class="col-sm-1"></span>
                         <label class="col-sm-2 control-label"><span class="glyph-icon icon-openid"></span>&nbsp;&nbsp;BPS 写</label>
                         <div class="col-sm-3">
-                            <input id="bps_wr" title="BPS 写" class="form-control" type="text" value="{{ (disk_ret.data.bps_wr / 1024 / 1024) | int }}" name="bps_wr">
+                            <input id="bps_wr" title="BPS 写" class="form-control" type="text" value="{{ (disk.bps_wr / 1024 / 1024) | int }}" name="bps_wr">
                         </div>
                         <span class="col-sm-1"></span>
                     </div>
@@ -827,12 +827,12 @@
                             &nbsp;&nbsp;IOPS 桶宽
                         </label>
                         <div class="col-sm-3">
-                            <input id="iops_max" title="IOPS 桶宽" class="form-control" type="text" value="{{ disk_ret.data.iops_max }}" name="iops_max">
+                            <input id="iops_max" title="IOPS 桶宽" class="form-control" type="text" value="{{ disk.iops_max }}" name="iops_max">
                         </div>
                         <span class="col-sm-1"></span>
                         <label class="col-sm-2 control-label"><span class="glyph-icon icon-openid"></span><span class="glyph-icon icon-bitbucket"></span>&nbsp;&nbsp;BPS 桶宽</label>
                         <div class="col-sm-3">
-                            <input id="bps_max" title="BPS 桶宽" class="form-control" type="text" value="{{ (disk_ret.data.bps_max / 1024 / 1024) | int }}" name="bps_max">
+                            <input id="bps_max" title="BPS 桶宽" class="form-control" type="text" value="{{ (disk.bps_max / 1024 / 1024) | int }}" name="bps_max">
                         </div>
                         <span class="col-sm-1"></span>
                     </div>
@@ -843,12 +843,12 @@
                             &nbsp;&nbsp;IOPS 桶高
                         </label>
                         <div class="col-sm-3">
-                            <input id="iops_max_length" title="IOPS 桶高" class="form-control" type="text" value="{{ disk_ret.data.iops_max_length }}" name="iops_max_length">
+                            <input id="iops_max_length" title="IOPS 桶高" class="form-control" type="text" value="{{ disk.iops_max_length }}" name="iops_max_length">
                         </div>
                         <span class="col-sm-1"></span>
                         <label class="col-sm-2 control-label"><span class="glyph-icon icon-openid"></span><span class="glyph-icon icon-bitbucket"></span>&nbsp;&nbsp;BPS 桶高</label>
                         <div class="col-sm-3">
-                            <input id="bps_max_length" title="BPS 桶高" class="form-control" type="text" value="{{ disk_ret.data.bps_max_length }}" name="bps_max_length">
+                            <input id="bps_max_length" title="BPS 桶高" class="form-control" type="text" value="{{ disk.bps_max_length }}" name="bps_max_length">
                         </div>
                         <span class="col-sm-1"></span>
                     </div>

+ 8 - 31
views/disk.py

@@ -5,8 +5,6 @@
 import json
 from flask import Blueprint, render_template, url_for, request
 import requests
-from math import ceil
-from models.status import StorageMode
 
 
 __author__ = 'James Iter'
@@ -56,33 +54,12 @@ def create():
 
 
 def detail(uuid):
-    host_url = request.host_url.rstrip('/')
-
-    disk_url = host_url + url_for('api_disks.r_get', uuids=uuid)
-
-    disk_ret = requests.get(url=disk_url, cookies=request.cookies)
-    disk_ret = json.loads(disk_ret.content)
-
-    guest_ret = None
-    os_template_image_ret = None
-
-    config_url = host_url + url_for('api_config.r_get')
-    config_ret = requests.get(url=config_url, cookies=request.cookies)
-    config_ret = json.loads(config_ret.content)
-
-    if disk_ret['data']['sequence'] != -1:
-        guest_url = host_url + url_for('api_guests.r_get', uuids=disk_ret['data']['guest_uuid'])
-
-        guest_ret = requests.get(url=guest_url, cookies=request.cookies)
-        guest_ret = json.loads(guest_ret.content)
-
-        os_template_image_url = host_url + url_for('api_os_templates_image.r_get',
-                                                   ids=guest_ret['data']['os_template_image_id'].__str__())
-
-        os_template_image_ret = requests.get(url=os_template_image_url, cookies=request.cookies)
-        os_template_image_ret = json.loads(os_template_image_ret.content)
-
-    return render_template('disk_detail.html', uuid=uuid, guest_ret=guest_ret,
-                           os_template_image_ret=os_template_image_ret, disk_ret=disk_ret, config_ret=config_ret)
-
+    disk_detail_url = url_for('api_disk.r_detail', uuid=uuid, _external=True)
+    disk_detail_ret = requests.get(url=disk_detail_url, cookies=request.cookies)
+    disk_detail_ret = json.loads(disk_detail_ret.content)
+
+    return render_template('disk_detail.html', uuid=uuid, guest=disk_detail_ret['data']['guest'],
+                           os_template_image=disk_detail_ret['data']['os_template_image'],
+                           disk=disk_detail_ret['data']['disk'],
+                           config=disk_detail_ret['data']['config'])