Преглед изворни кода

Guest 前后端分离调整完毕

James Iter пре 8 година
родитељ
комит
6a9c9514f8
5 измењених фајлова са 161 додато и 114 уклоњено
  1. 116 11
      api/guest.py
  2. 2 0
      api_route_table.py
  3. 25 25
      templates/guest_detail.html
  4. 2 2
      templates/guests_show.html
  5. 16 76
      views/guest.py

+ 116 - 11
api/guest.py

@@ -8,12 +8,14 @@ from math import ceil
 import requests
 import requests
 import json
 import json
 from uuid import uuid4
 from uuid import uuid4
+import random
+import time
 import jimit as ji
 import jimit as ji
 from flask import Blueprint, url_for, request
 from flask import Blueprint, url_for, request
 
 
 from api.base import Base
 from api.base import Base
-from models import DiskState, Host
 from models.initialize import app, dev_table
 from models.initialize import app, dev_table
+from models import DiskState, Host
 from models import Database as db
 from models import Database as db
 from models import Config
 from models import Config
 from models import Disk
 from models import Disk
@@ -50,6 +52,8 @@ blueprints = Blueprint(
 
 
 
 
 guest_base = Base(the_class=Guest, the_blueprint=blueprint, the_blueprints=blueprints)
 guest_base = Base(the_class=Guest, the_blueprint=blueprint, the_blueprints=blueprints)
+os_template_image_base = Base(the_class=OSTemplateImage, the_blueprint=blueprint, the_blueprints=blueprints)
+os_template_profile_base = Base(the_class=OSTemplateProfile, the_blueprint=blueprint, the_blueprints=blueprints)
 
 
 
 
 @Utils.dumps2response
 @Utils.dumps2response
@@ -1311,9 +1315,6 @@ def r_show():
     if keyword is not None:
     if keyword is not None:
         guests_url = url_for('api_guests.r_content_search', _external=True)
         guests_url = url_for('api_guests.r_content_search', _external=True)
 
 
-    os_templates_image_url = url_for('api_os_templates_image.r_get_by_filter', _external=True)
-    os_templates_profile_url = url_for('api_os_templates_profile.r_get_by_filter', _external=True)
-
     if args.__len__() > 0:
     if args.__len__() > 0:
         guests_url = guests_url + '?' + '&'.join(args)
         guests_url = guests_url + '?' + '&'.join(args)
 
 
@@ -1327,16 +1328,14 @@ def r_show():
     guests_ret = requests.get(url=guests_url, cookies=request.cookies)
     guests_ret = requests.get(url=guests_url, cookies=request.cookies)
     guests_ret = json.loads(guests_ret.content)
     guests_ret = json.loads(guests_ret.content)
 
 
-    os_templates_image_ret = requests.get(url=os_templates_image_url, cookies=request.cookies)
-    os_templates_image_ret = json.loads(os_templates_image_ret.content)
+    os_templates_image, _ = OSTemplateImage.get_by_filter()
     os_templates_image_mapping_by_id = dict()
     os_templates_image_mapping_by_id = dict()
-    for os_template_image in os_templates_image_ret['data']:
+    for os_template_image in os_templates_image:
         os_templates_image_mapping_by_id[os_template_image['id']] = os_template_image
         os_templates_image_mapping_by_id[os_template_image['id']] = os_template_image
 
 
-    os_templates_profile_ret = requests.get(url=os_templates_profile_url, cookies=request.cookies)
-    os_templates_profile_ret = json.loads(os_templates_profile_ret.content)
+    os_templates_profile, _ = OSTemplateProfile.get_by_filter()
     os_templates_profile_mapping_by_id = dict()
     os_templates_profile_mapping_by_id = dict()
-    for os_template_profile in os_templates_profile_ret['data']:
+    for os_template_profile in os_templates_profile:
         os_templates_profile_mapping_by_id[os_template_profile['id']] = os_template_profile
         os_templates_profile_mapping_by_id[os_template_profile['id']] = os_template_profile
 
 
     last_page = int(ceil(guests_ret['paging']['total'] / float(page_size)))
     last_page = int(ceil(guests_ret['paging']['total'] / float(page_size)))
@@ -1364,10 +1363,11 @@ def r_show():
     ret['state'] = ji.Common.exchange_state(20000)
     ret['state'] = ji.Common.exchange_state(20000)
 
 
     ret['data'] = {
     ret['data'] = {
-        'guests_ret': guests_ret,
+        'guests': guests_ret['data'],
         'os_templates_image_mapping_by_id': os_templates_image_mapping_by_id,
         'os_templates_image_mapping_by_id': os_templates_image_mapping_by_id,
         'os_templates_profile_mapping_by_id': os_templates_profile_mapping_by_id,
         'os_templates_profile_mapping_by_id': os_templates_profile_mapping_by_id,
         'hosts_mapping_by_node_id': hosts_mapping_by_node_id,
         'hosts_mapping_by_node_id': hosts_mapping_by_node_id,
+        'paging': guests_ret['paging'],
         'page': page,
         'page': page,
         'page_size': page_size,
         'page_size': page_size,
         'keyword': keyword,
         'keyword': keyword,
@@ -1377,3 +1377,108 @@ def r_show():
 
 
     return ret
     return ret
 
 
+
+@Utils.dumps2response
+def r_vnc(uuid):
+
+    guest_ret = guest_base.get(ids=uuid, ids_rule=Rules.UUID.value, by_field='uuid')
+
+    if '200' != guest_ret['state']['code']:
+        return guest_ret
+
+    hosts_url = url_for('api_hosts.r_get_by_filter', _external=True)
+    hosts_ret = requests.get(url=hosts_url, cookies=request.cookies)
+    hosts_ret = json.loads(hosts_ret.content)
+
+    hosts_mapping_by_node_id = dict()
+    for host in hosts_ret['data']:
+        hosts_mapping_by_node_id[int(host['node_id'])] = host
+
+    port = random.randrange(50000, 60000)
+    while True:
+        if not Utils.port_is_opened(port=port):
+            break
+
+        port = random.randrange(50000, 60000)
+
+    payload = {'listen_port': port, 'target_host': hosts_mapping_by_node_id[guest_ret['data']['node_id']]['hostname'],
+               'target_port': guest_ret['data']['vnc_port']}
+
+    db.r.rpush(app.config['ipc_queue'], json.dumps(payload, ensure_ascii=False))
+    time.sleep(1)
+
+    ret = dict()
+    ret['state'] = ji.Common.exchange_state(20000)
+
+    ret['data'] = {
+        'port': port,
+        'vnc_password': guest_ret['data']['vnc_password']
+    }
+
+    return ret
+
+
+@Utils.dumps2response
+def r_detail(uuid):
+
+    hosts_url = url_for('api_hosts.r_get_by_filter', _external=True)
+    hosts_ret = requests.get(url=hosts_url, cookies=request.cookies)
+    hosts_ret = json.loads(hosts_ret.content)
+
+    hosts_mapping_by_node_id = dict()
+    for host in hosts_ret['data']:
+        hosts_mapping_by_node_id[int(host['node_id'])] = host
+
+    guest = Guest()
+    guest.uuid = uuid
+    guest.get_by(field='uuid')
+    guest.ssh_keys = list()
+
+    rows, _ = SSHKeyGuestMapping.get_by_filter(filter_str=':'.join(['guest_uuid', 'in', guest.uuid]))
+
+    ssh_keys_id = list()
+
+    for row in rows:
+        if row['ssh_key_id'] not in ssh_keys_id:
+            ssh_keys_id.append(row['ssh_key_id'].__str__())
+
+    rows, _ = SSHKey.get_by_filter(filter_str=':'.join(['id', 'in', ','.join(ssh_keys_id)]))
+
+    for row in rows:
+        row['url'] = url_for('v_ssh_keys.show')
+        guest.ssh_keys.append(row)
+
+    os_template_image = OSTemplateImage()
+    os_template_image.id = guest.os_template_image_id.__str__()
+    os_template_image.get()
+
+    os_template_profiles, _ = OSTemplateProfile.get_by_filter()
+
+    os_templates_profile_mapping_by_id = dict()
+    for os_template_profile in os_template_profiles:
+        os_templates_profile_mapping_by_id[os_template_profile['id']] = os_template_profile
+
+    disks_url = url_for('api_disks.r_get_by_filter', filter='guest_uuid:in:' + guest.uuid, _external=True)
+    disks_ret = requests.get(url=disks_url, cookies=request.cookies)
+    disks = json.loads(disks_ret.content)['data']
+
+    config = Config()
+    config.id = 1
+    config.get()
+
+    ret = dict()
+    ret['state'] = ji.Common.exchange_state(20000)
+
+    ret['data'] = {
+        'uuid': uuid,
+        'guest': guest.__dict__,
+        'os_template_image': os_template_image.__dict__,
+        'os_templates_profile_mapping_by_id': os_templates_profile_mapping_by_id,
+        'hosts_mapping_by_node_id': hosts_mapping_by_node_id,
+        'disks': disks,
+        'config': config.__dict__
+    }
+
+    return ret
+
+

+ 2 - 0
api_route_table.py

@@ -120,6 +120,8 @@ add_rule_api(guest.blueprints, '/_distribute_count', api_func='guest.r_distribut
 add_rule_api(guest.blueprints, '/_refresh_guest_state', api_func='guest.r_refresh_guest_state', methods=['PATCH'])
 add_rule_api(guest.blueprints, '/_refresh_guest_state', api_func='guest.r_refresh_guest_state', methods=['PATCH'])
 
 
 add_rule_api(guest.blueprints, '/_show', api_func='guest.r_show', methods=['GET'])
 add_rule_api(guest.blueprints, '/_show', api_func='guest.r_show', methods=['GET'])
+add_rule_api(guest.blueprint, '/_detail/<uuid>', api_func='guest.r_detail', methods=['GET'])
+add_rule_api(guest.blueprint, '/_vnc/<uuid>', api_func='guest.r_vnc', methods=['GET'])
 
 
 # Disk操作
 # Disk操作
 add_rule_api(disk.blueprint, '', api_func='disk.r_create', methods=['POST'])
 add_rule_api(disk.blueprint, '', api_func='disk.r_create', methods=['POST'])

+ 25 - 25
templates/guest_detail.html

@@ -457,7 +457,7 @@
                     },
                     },
                     yAxis: {
                     yAxis: {
                         min: 0,
                         min: 0,
-                        max: {{ guest_ret.data.memory }},
+                        max: {{ guest.memory }},
                         minInterval: 2,
                         minInterval: 2,
                         axisTick: {
                         axisTick: {
                             show: false
                             show: false
@@ -897,7 +897,7 @@
     function refresh_render_with_specify_granularity(granularity) {
     function refresh_render_with_specify_granularity(granularity) {
         render_cpu_chart(uuid, granularity);
         render_cpu_chart(uuid, granularity);
         render_traffic_chart(uuid, granularity);
         render_traffic_chart(uuid, granularity);
-        render_disks_chart(uuid, granularity)
+        render_disks_chart(uuid, granularity);
         render_memory_chart(uuid, granularity);
         render_memory_chart(uuid, granularity);
     }
     }
 
 
@@ -984,7 +984,7 @@
 
 
     $(function() { "use strict";
     $(function() { "use strict";
         $("#iops").TouchSpin({
         $("#iops").TouchSpin({
-            max: {{ config_ret.data.iops_cap }},
+            max: {{ config.iops_cap }},
             min: 1,
             min: 1,
             verticalbuttons: true,
             verticalbuttons: true,
             verticalupclass: 'glyph-icon icon-plus',
             verticalupclass: 'glyph-icon icon-plus',
@@ -994,7 +994,7 @@
 
 
     $(function() { "use strict";
     $(function() { "use strict";
         $("#bps").TouchSpin({
         $("#bps").TouchSpin({
-            max: {{ (config_ret.data.bps_cap / 1024 / 1024) | int }},
+            max: {{ (config.bps_cap / 1024 / 1024) | int }},
             min: 1,
             min: 1,
             verticalbuttons: true,
             verticalbuttons: true,
             verticalupclass: 'glyph-icon icon-plus',
             verticalupclass: 'glyph-icon icon-plus',
@@ -1026,7 +1026,7 @@
                             <td>
                             <td>
                                 <span class="guest-label">UUID:&nbsp;&nbsp;&nbsp;&nbsp;</span>
                                 <span class="guest-label">UUID:&nbsp;&nbsp;&nbsp;&nbsp;</span>
                                 <span class="guest-desc">
                                 <span class="guest-desc">
-                                {{ guest_ret.data.uuid }}
+                                {{ guest.uuid }}
                                 </span>
                                 </span>
                             </td>
                             </td>
                         </tr>
                         </tr>
@@ -1034,7 +1034,7 @@
                             <td>
                             <td>
                                 <span class="guest-label">标识:&nbsp;&nbsp;&nbsp;&nbsp;</span>
                                 <span class="guest-label">标识:&nbsp;&nbsp;&nbsp;&nbsp;</span>
                                 <span class="guest-desc">
                                 <span class="guest-desc">
-                                {{ guest_ret.data.label }}
+                                {{ guest.label }}
                                 </span>
                                 </span>
                             </td>
                             </td>
                         </tr>
                         </tr>
@@ -1042,7 +1042,7 @@
                             <td>
                             <td>
                                 <span class="guest-label">名称:&nbsp;&nbsp;&nbsp;&nbsp;</span>
                                 <span class="guest-label">名称:&nbsp;&nbsp;&nbsp;&nbsp;</span>
                                 <span class="guest-desc">
                                 <span class="guest-desc">
-                                {{ guest_ret.data.remark }}
+                                {{ guest.remark }}
                                 </span>
                                 </span>
                             </td>
                             </td>
                         </tr>
                         </tr>
@@ -1050,7 +1050,7 @@
                             <td>
                             <td>
                                 <span class="guest-label">状态:&nbsp;&nbsp;&nbsp;&nbsp;</span>
                                 <span class="guest-label">状态:&nbsp;&nbsp;&nbsp;&nbsp;</span>
                                 <span class="guest-desc">
                                 <span class="guest-desc">
-                                {{ format_guest_status(guest_ret.data.status, guest_ret.data.progress)|safe }}
+                                {{ format_guest_status(guest.status, guest.progress)|safe }}
                                 </span>
                                 </span>
                             </td>
                             </td>
                         </tr>
                         </tr>
@@ -1058,7 +1058,7 @@
                             <td>
                             <td>
                                 <span class="guest-label">CPU:&nbsp;&nbsp;&nbsp;&nbsp;</span>
                                 <span class="guest-label">CPU:&nbsp;&nbsp;&nbsp;&nbsp;</span>
                                 <span class="guest-desc">
                                 <span class="guest-desc">
-                                {{ guest_ret.data.cpu }} 核
+                                {{ guest.cpu }} 核
                                 </span>
                                 </span>
                             </td>
                             </td>
                         </tr>
                         </tr>
@@ -1066,7 +1066,7 @@
                             <td>
                             <td>
                                 <span class="guest-label">内存:&nbsp;&nbsp;&nbsp;&nbsp;</span>
                                 <span class="guest-label">内存:&nbsp;&nbsp;&nbsp;&nbsp;</span>
                                 <span class="guest-desc">
                                 <span class="guest-desc">
-                                {{ guest_ret.data.memory }} GB
+                                {{ guest.memory }} GB
                                 </span>
                                 </span>
                             </td>
                             </td>
                         </tr>
                         </tr>
@@ -1078,7 +1078,7 @@
                                         <td colspan="6">
                                         <td colspan="6">
                                             <span class="guest-label">磁盘:&nbsp;&nbsp;&nbsp;&nbsp;</span>
                                             <span class="guest-label">磁盘:&nbsp;&nbsp;&nbsp;&nbsp;</span>
                                             <span class="guest-desc">
                                             <span class="guest-desc">
-                                                    <a href="/disks?guest_uuid={{ guest_ret.data.uuid }}" style="color: #0275d8;">{{ disks_ret.data|length }}</a>
+                                                    <a href="/disks?guest_uuid={{ guest.uuid }}" style="color: #0275d8;">{{ disks|length }}</a>
                                             </span>
                                             </span>
                                             <button class="btn btn-xs btn-default btn-shortcut disabled" style="float: right;" data-toggle="modal" data-target="#change_disk_quota_modal">变更配额</button>
                                             <button class="btn btn-xs btn-default btn-shortcut disabled" style="float: right;" data-toggle="modal" data-target="#change_disk_quota_modal">变更配额</button>
                                         </td>
                                         </td>
@@ -1093,7 +1093,7 @@
                                     </tr>
                                     </tr>
                                     </thead>
                                     </thead>
                                     <tbody>
                                     <tbody>
-                                    {% for disk in disks_ret.data %}
+                                    {% for disk in disks %}
                                         <tr class="guest-desc">
                                         <tr class="guest-desc">
                                             <td style="display: none;">{{ disk.uuid }}</td>
                                             <td style="display: none;">{{ disk.uuid }}</td>
                                             <td><input title="选中" type="checkbox"></td>
                                             <td><input title="选中" type="checkbox"></td>
@@ -1108,13 +1108,13 @@
                             <td>
                             <td>
                                 <span class="guest-label">操作系统:&nbsp;&nbsp;&nbsp;&nbsp;</span>
                                 <span class="guest-label">操作系统:&nbsp;&nbsp;&nbsp;&nbsp;</span>
                                 <span class="guest-desc">
                                 <span class="guest-desc">
-                                {{ os_template_image_ret.data.label }}
+                                {{ os_template_image.label }}
                                 </span>&nbsp;&nbsp;&nbsp;&nbsp;
                                 </span>&nbsp;&nbsp;&nbsp;&nbsp;
 
 
-                                {% if os_template_image_ret.data.logo == "" %}
-                                    <span class="{{ os_templates_profile_mapping_by_id[os_template_image_ret.data.os_template_profile_id].icon }}"></span>
+                                {% if os_template_image.logo == "" %}
+                                    <span class="{{ os_templates_profile_mapping_by_id[os_template_image.os_template_profile_id].icon }}"></span>
                                 {% else %}
                                 {% else %}
-                                    <span class="{{ os_template_image_ret.data.logo }}"></span>
+                                    <span class="{{ os_template_image.logo }}"></span>
                                 {% endif %}
                                 {% endif %}
                             </td>
                             </td>
                         </tr>
                         </tr>
@@ -1122,7 +1122,7 @@
                             <td>
                             <td>
                                 <span class="guest-label">IP:&nbsp;&nbsp;&nbsp;&nbsp;</span>
                                 <span class="guest-label">IP:&nbsp;&nbsp;&nbsp;&nbsp;</span>
                                 <span class="guest-desc">
                                 <span class="guest-desc">
-                                {{ guest_ret.data.ip }}
+                                {{ guest.ip }}
                                 </span>
                                 </span>
                             </td>
                             </td>
                         </tr>
                         </tr>
@@ -1130,7 +1130,7 @@
                             <td>
                             <td>
                                 <span class="guest-label">密码:&nbsp;&nbsp;&nbsp;&nbsp;</span>
                                 <span class="guest-label">密码:&nbsp;&nbsp;&nbsp;&nbsp;</span>
                                 <span class="guest-desc">
                                 <span class="guest-desc">
-                                {{ guest_ret.data.password }}
+                                {{ guest.password }}
                                 </span>
                                 </span>
                             </td>
                             </td>
                         </tr>
                         </tr>
@@ -1138,7 +1138,7 @@
                             <td>
                             <td>
                                 <span class="guest-label">计算节点:&nbsp;&nbsp;&nbsp;&nbsp;</span>
                                 <span class="guest-label">计算节点:&nbsp;&nbsp;&nbsp;&nbsp;</span>
                                 <span class="guest-desc">
                                 <span class="guest-desc">
-                                {{ hosts_mapping_by_node_id[guest_ret.data.node_id].hostname }}
+                                {{ hosts_mapping_by_node_id[guest.node_id | string].hostname }}
                                 </span>
                                 </span>
                             </td>
                             </td>
                         </tr>
                         </tr>
@@ -1146,7 +1146,7 @@
                             <td>
                             <td>
                                 <span class="guest-label">VNC 端口:&nbsp;&nbsp;&nbsp;&nbsp;</span>
                                 <span class="guest-label">VNC 端口:&nbsp;&nbsp;&nbsp;&nbsp;</span>
                                 <span class="guest-desc">
                                 <span class="guest-desc">
-                                {{ guest_ret.data.vnc_port }}
+                                {{ guest.vnc_port }}
                                 </span>
                                 </span>
                             </td>
                             </td>
                         </tr>
                         </tr>
@@ -1154,7 +1154,7 @@
                             <td>
                             <td>
                                 <span class="guest-label">VNC 密码:&nbsp;&nbsp;&nbsp;&nbsp;</span>
                                 <span class="guest-label">VNC 密码:&nbsp;&nbsp;&nbsp;&nbsp;</span>
                                 <span class="guest-desc">
                                 <span class="guest-desc">
-                                {{ guest_ret.data.vnc_password }}
+                                {{ guest.vnc_password }}
                                 </span>
                                 </span>
                             </td>
                             </td>
                         </tr>
                         </tr>
@@ -1163,7 +1163,7 @@
                                 <span class="guest-label">SSH-KEY 名称:&nbsp;&nbsp;&nbsp;&nbsp;</span>
                                 <span class="guest-label">SSH-KEY 名称:&nbsp;&nbsp;&nbsp;&nbsp;</span>
                                 <span class="guest-desc">
                                 <span class="guest-desc">
                                     {% set ssh_keys_label = [] %}
                                     {% set ssh_keys_label = [] %}
-                                    {% for ssh_key in guest_ret.data.ssh_keys %}
+                                    {% for ssh_key in guest.ssh_keys %}
                                     {% do ssh_keys_label.append('<a href="' + ssh_key.url + '">' + ssh_key.label + '</a>') %}
                                     {% do ssh_keys_label.append('<a href="' + ssh_key.url + '">' + ssh_key.label + '</a>') %}
                                     {% endfor %}
                                     {% endfor %}
                                     {{ ssh_keys_label | join('、') | safe }}
                                     {{ ssh_keys_label | join('、') | safe }}
@@ -1174,7 +1174,7 @@
                             <td>
                             <td>
                                 <span class="guest-label">创建时间:&nbsp;&nbsp;&nbsp;&nbsp;</span>
                                 <span class="guest-label">创建时间:&nbsp;&nbsp;&nbsp;&nbsp;</span>
                                 <span class="guest-desc">
                                 <span class="guest-desc">
-                                {{ format_datetime_by_tus(guest_ret.data.create_time) }}
+                                {{ format_datetime_by_tus(guest.create_time) }}
                                 </span>
                                 </span>
                             </td>
                             </td>
                         </tr>
                         </tr>
@@ -1217,12 +1217,12 @@
                     <div class="form-group" style="margin-bottom: 0;">
                     <div class="form-group" style="margin-bottom: 0;">
                         <label class="col-sm-2 control-label"><span class="glyph-icon icon-exchange" style="transform: rotate(90deg); display: inline-flex;"></span>&nbsp;&nbsp;IOPS</label>
                         <label class="col-sm-2 control-label"><span class="glyph-icon icon-exchange" style="transform: rotate(90deg); display: inline-flex;"></span>&nbsp;&nbsp;IOPS</label>
                         <div class="col-sm-3">
                         <div class="col-sm-3">
-                            <input id="iops" title="IOPS" class="form-control" type="text" value="{{ config_ret.data.iops_base }}" name="iops">
+                            <input id="iops" title="IOPS" class="form-control" type="text" value="{{ config.iops_base }}" name="iops">
                         </div>
                         </div>
                         <span class="col-sm-1"></span>
                         <span class="col-sm-1"></span>
                         <label class="col-sm-2 control-label"><span class="glyph-icon icon-openid"></span>&nbsp;&nbsp;BPS(MiB)</label>
                         <label class="col-sm-2 control-label"><span class="glyph-icon icon-openid"></span>&nbsp;&nbsp;BPS(MiB)</label>
                         <div class="col-sm-3">
                         <div class="col-sm-3">
-                            <input id="bps" title="BPS" class="form-control" type="text" value="{{ config_ret.data.bps_base / 1024 / 1024 | int }}" name="bps">
+                            <input id="bps" title="BPS" class="form-control" type="text" value="{{ config.bps_base / 1024 / 1024 | int }}" name="bps">
                         </div>
                         </div>
                         <span class="col-sm-1"></span>
                         <span class="col-sm-1"></span>
                     </div>
                     </div>

+ 2 - 2
templates/guests_show.html

@@ -839,7 +839,7 @@
                 </tr>
                 </tr>
                 </thead>
                 </thead>
                 <tbody>
                 <tbody>
-                {% for item in guests_ret.data %}
+                {% for item in guests %}
                 <tr role="row" class="odd" onmouseover="row_onmouseover(this);" onmouseout="row_onmouseout(this);">
                 <tr role="row" class="odd" onmouseover="row_onmouseover(this);" onmouseout="row_onmouseout(this);">
                     <td style="display: none;">{{ item.uuid }}</td>
                     <td style="display: none;">{{ item.uuid }}</td>
                     <td><input title="选中" type="checkbox"></td>
                     <td><input title="选中" type="checkbox"></td>
@@ -1057,7 +1057,7 @@
                                     </div>
                                     </div>
                                 </div>
                                 </div>
                                 <div class="col-sm-3" style="font-size: 12px; padding-top: 5px; text-align: right;">
                                 <div class="col-sm-3" style="font-size: 12px; padding-top: 5px; text-align: right;">
-                                    共有{{ guests_ret.paging.total }}条,每页显示:
+                                    共有{{ paging.total }}条,每页显示:
                                     <select id="page_size" name="datatable-row-highlight_length" title="page_size" class="form-control" style="height: 22px; vertical-align: baseline;">
                                     <select id="page_size" name="datatable-row-highlight_length" title="page_size" class="form-control" style="height: 22px; vertical-align: baseline;">
                                         <option value="10" {% if page_size == 10 %} selected {% endif %}>10</option>
                                         <option value="10" {% if page_size == 10 %} selected {% endif %}>10</option>
                                         <option value="20" {% if page_size == 20 %} selected {% endif %}>20</option>
                                         <option value="20" {% if page_size == 20 %} selected {% endif %}>20</option>

+ 16 - 76
views/guest.py

@@ -3,14 +3,8 @@
 
 
 
 
 import json
 import json
-import random
 from flask import Blueprint, render_template, url_for, request
 from flask import Blueprint, render_template, url_for, request
 import requests
 import requests
-import time
-
-from models import Database as db
-from models import Utils
-from models.initialize import config
 
 
 
 
 __author__ = 'James Iter'
 __author__ = 'James Iter'
@@ -45,86 +39,32 @@ def show():
     ret = requests.get(url=url, cookies=request.cookies)
     ret = requests.get(url=url, cookies=request.cookies)
     ret = json.loads(ret.content)
     ret = json.loads(ret.content)
 
 
-    return render_template('guests_show.html', guests_ret=ret['data']['guests_ret'],
+    return render_template('guests_show.html', guests=ret['data']['guests'],
                            resource_path=request.path,
                            resource_path=request.path,
                            os_templates_image_mapping_by_id=ret['data']['os_templates_image_mapping_by_id'],
                            os_templates_image_mapping_by_id=ret['data']['os_templates_image_mapping_by_id'],
                            os_templates_profile_mapping_by_id=ret['data']['os_templates_profile_mapping_by_id'],
                            os_templates_profile_mapping_by_id=ret['data']['os_templates_profile_mapping_by_id'],
                            hosts_mapping_by_node_id=ret['data']['hosts_mapping_by_node_id'], page=ret['data']['page'],
                            hosts_mapping_by_node_id=ret['data']['hosts_mapping_by_node_id'], page=ret['data']['page'],
                            page_size=ret['data']['page_size'], keyword=ret['data']['keyword'],
                            page_size=ret['data']['page_size'], keyword=ret['data']['keyword'],
-                           pages=ret['data']['pages'], last_page=ret['data']['last_page'])
+                           paging=ret['data']['paging'], pages=ret['data']['pages'], last_page=ret['data']['last_page'])
 
 
 
 
 def vnc(uuid):
 def vnc(uuid):
-    host_url = request.host_url.rstrip('/')
-
-    hosts_url = host_url + url_for('api_hosts.r_get_by_filter')
-    guest_url = host_url + url_for('api_guests.r_get', uuids=uuid)
-
-    guest_ret = requests.get(url=guest_url, cookies=request.cookies)
-    guest_ret = json.loads(guest_ret.content)
-
-    hosts_ret = requests.get(url=hosts_url, cookies=request.cookies)
-    hosts_ret = json.loads(hosts_ret.content)
-
-    hosts_mapping_by_node_id = dict()
-    for host in hosts_ret['data']:
-        hosts_mapping_by_node_id[int(host['node_id'])] = host
+    vnc_url = url_for('api_guest.r_vnc', uuid=uuid, _external=True)
+    vnc_ret = requests.get(url=vnc_url, cookies=request.cookies)
+    vnc_ret = json.loads(vnc_ret.content)
 
 
-    port = random.randrange(50000, 60000)
-    while True:
-        if not Utils.port_is_opened(port=port):
-            break
-
-        port = random.randrange(50000, 60000)
-
-    payload = {'listen_port': port, 'target_host': hosts_mapping_by_node_id[guest_ret['data']['node_id']]['hostname'],
-               'target_port': guest_ret['data']['vnc_port']}
-    db.r.rpush(config['ipc_queue'], json.dumps(payload, ensure_ascii=False))
-    time.sleep(1)
-
-    return render_template('vnc_lite.html', port=port, password=guest_ret['data']['vnc_password'])
+    return render_template('vnc_lite.html', port=vnc_ret['data']['port'], password=vnc_ret['data']['vnc_password'])
 
 
 
 
 def detail(uuid):
 def detail(uuid):
-    host_url = request.host_url.rstrip('/')
-
-    hosts_url = host_url + url_for('api_hosts.r_get_by_filter')
-    guest_url = host_url + url_for('api_guests.r_get', uuids=uuid)
-
-    hosts_ret = requests.get(url=hosts_url, cookies=request.cookies)
-    hosts_ret = json.loads(hosts_ret.content)
-
-    hosts_mapping_by_node_id = dict()
-    for host in hosts_ret['data']:
-        hosts_mapping_by_node_id[int(host['node_id'])] = host
-
-    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_templates_profile_url = host_url + url_for('api_os_templates_profile.r_get_by_filter')
-
-    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)
-
-    os_templates_profile_ret = requests.get(url=os_templates_profile_url, cookies=request.cookies)
-    os_templates_profile_ret = json.loads(os_templates_profile_ret.content)
-    os_templates_profile_mapping_by_id = dict()
-    for os_template_profile in os_templates_profile_ret['data']:
-        os_templates_profile_mapping_by_id[os_template_profile['id']] = os_template_profile
-
-    disks_url = host_url + url_for('api_disks.r_get_by_filter', filter='guest_uuid:in:' + guest_ret['data']['uuid'])
-    disks_ret = requests.get(url=disks_url, cookies=request.cookies)
-    disks_ret = json.loads(disks_ret.content)
-
-    url = host_url + '/api/config'
-    config_ret = requests.get(url=url, cookies=request.cookies)
-    config_ret = json.loads(config_ret.content)
-
-    return render_template('guest_detail.html', uuid=uuid, guest_ret=guest_ret,
-                           os_template_image_ret=os_template_image_ret,
-                           os_templates_profile_mapping_by_id=os_templates_profile_mapping_by_id,
-                           hosts_mapping_by_node_id=hosts_mapping_by_node_id,
-                           disks_ret=disks_ret, config_ret=config_ret)
+    guest_detail_url = url_for('api_guest.r_detail', uuid=uuid, _external=True)
+    guest_detail_ret = requests.get(url=guest_detail_url, cookies=request.cookies)
+    guest_detail_ret = json.loads(guest_detail_ret.content)
+
+    return render_template('guest_detail.html', uuid=uuid, guest=guest_detail_ret['data']['guest'],
+                           os_template_image=guest_detail_ret['data']['os_template_image'],
+                           os_templates_profile_mapping_by_id=
+                           guest_detail_ret['data']['os_templates_profile_mapping_by_id'],
+                           hosts_mapping_by_node_id=guest_detail_ret['data']['hosts_mapping_by_node_id'],
+                           disks=guest_detail_ret['data']['disks'], config=guest_detail_ret['data']['config'])