Przeglądaj źródła

初始化页面分离完毕

James Iter 8 lat temu
rodzic
commit
fe5a4de048
4 zmienionych plików z 103 dodań i 94 usunięć
  1. 64 12
      templates/config_init.html
  2. 26 26
      templates/config_show.html
  3. 10 53
      views/config.py
  4. 3 3
      views_route_table.py

+ 64 - 12
templates/config_init.html

@@ -40,6 +40,58 @@
         });
     });
 
+    function create() {
+        var data = {
+            jimv_edition: 0,
+            storage_mode: parseInt($('#storage_mode').val()),
+            vm_network: $('#vm_network').val(),
+            vm_manage_network: $('#vm_manage_network').val(),
+            start_ip: $('#start_ip').val(),
+            end_ip: $('#end_ip').val(),
+            netmask: $('#netmask').val(),
+            gateway: $('#gateway').val(),
+            dns1: $('#dns1').val(),
+            dns2: $('#dns2').val(),
+            start_vnc_port: parseInt($('#start_vnc_port').val()),
+            dfs_volume: $('#dfs_volume').val(),
+            storage_path: $('#storage_path').val(),
+            hdd_type: $('#hdd_type').val(),
+
+            iops_base: parseInt($('#iops_base').val()),
+            iops_pre_unit: parseInt($('#iops_pre_unit').val()),
+            iops_cap: parseInt($('#iops_cap').val()),
+            iops_max: parseInt($('#iops_max').val()),
+            iops_max_length: parseInt($('#iops_max_length').val()),
+            bps_base: parseInt($('#bps_base').val()),
+            bps_pre_unit: parseInt($('#bps_pre_unit').val()),
+            bps_cap: parseInt($('#bps_cap').val()),
+            bps_max: parseInt($('#bps_max').val()),
+            bps_max_length: parseInt($('#bps_max_length').val())
+        };
+
+        var go_back_url = '/';
+
+        $.ajax({
+            url : '/api/config',
+            type : 'POST',
+            contentType: "application/json; charset=utf-8",
+            async: false,
+            dataType: "json",
+            data : JSON.stringify(data),
+            error : function(data, textStatus, xhr) {
+                alter_warning('初始化失败!');
+                alter_danger(data.responseText);
+            },
+            success : function(data, textStatus, xhr) {
+                alter_success('JimV 已被初始化。页面将在3秒钟后自动跳转到首页!!');
+            }
+        });
+
+        setTimeout(function() {
+            window.location.href=go_back_url;
+        }, 3000);
+    }
+
     function hdd_type_select(me) {
         var hdd_type = $(me).val();
 
@@ -97,51 +149,51 @@
                     初始化 JimV
                 </h3>
                 <div class="example-box-wrapper">
-                    <form class="form-horizontal bordered-row" action="/config/create" method="post">
+                    <form class="form-horizontal bordered-row" action="javascript:;">
                         <div class="form-group">
                             <label class="col-sm-2 control-label">网桥(业务网络)</label>
                             <div class="col-sm-3">
-                                <input title="Guest 业务网络" class="form-control" name="vm_network" type="text" value="net-br0">
+                                <input title="Guest 业务网络" class="form-control" id="vm_network" name="vm_network" type="text" value="net-br0">
                             </div>
                             <label class="col-sm-2 control-label">网桥(管理网络)</label>
                             <div class="col-sm-3">
-                                <input title="Guest 管理网络" class="form-control" name="vm_manage_network" type="text" value="net-br0">
+                                <input title="Guest 管理网络" class="form-control" id="vm_manage_network" name="vm_manage_network" type="text" value="net-br0">
                             </div>
                         </div>
                         <div class="form-group">
                             <label class="col-sm-2 control-label">起始 IP</label>
                             <div class="col-sm-3">
-                                <input title="起始 IP" class="form-control" name="start_ip" type="text" value="10.10.1.1">
+                                <input title="起始 IP" class="form-control" id="start_ip" name="start_ip" type="text" value="10.10.1.1">
                             </div>
                             <label class="col-sm-2 control-label">截止 IP</label>
                             <div class="col-sm-3">
-                                <input title="截止 IP" class="form-control" name="end_ip" type="text" value="10.10.15.253">
+                                <input title="截止 IP" class="form-control" id="end_ip" name="end_ip" type="text" value="10.10.15.253">
                             </div>
                         </div>
                         <div class="form-group">
                             <label class="col-sm-2 control-label">子网掩码</label>
                             <div class="col-sm-3">
-                                <input title="子网掩码" class="form-control" name="netmask" type="text" value="255.255.240.0">
+                                <input title="子网掩码" class="form-control" id="netmask" name="netmask" type="text" value="255.255.240.0">
                             </div>
                             <label class="col-sm-2 control-label">网关</label>
                             <div class="col-sm-3">
-                                <input title="网关" class="form-control" name="gateway" type="text" value="10.10.15.254">
+                                <input title="网关" class="form-control" id="gateway" name="gateway" type="text" value="10.10.15.254">
                             </div>
                         </div>
                         <div class="form-group">
                             <label class="col-sm-2 control-label">DNS1</label>
                             <div class="col-sm-3">
-                                <input title="DNS1" class="form-control" name="dns1" type="text" value="223.5.5.5">
+                                <input title="DNS1" class="form-control" id="dns1" name="dns1" type="text" value="223.5.5.5">
                             </div>
                             <label class="col-sm-2 control-label">DNS2</label>
                             <div class="col-sm-3">
-                                <input title="DNS2" class="form-control" name="dns2" type="text" value="8.8.8.8">
+                                <input title="DNS2" class="form-control" id="dns2" name="dns2" type="text" value="8.8.8.8">
                             </div>
                         </div>
                         <div class="form-group">
                             <label class="col-sm-2 control-label">起始 VNC 端口</label>
                             <div class="col-sm-3">
-                                <input title="起始 VNC 端口" class="form-control" name="start_vnc_port" type="text" value="15900">
+                                <input title="起始 VNC 端口" class="form-control" id="start_vnc_port" name="start_vnc_port" type="text" value="15900">
                             </div>
                         </div>
                         <div class="form-group">
@@ -162,7 +214,7 @@
                         <div class="form-group">
                             <label class="col-sm-2 control-label">虚拟机磁盘存放路径</label>
                             <div class="col-sm-3">
-                                <input title="虚拟机磁盘存放路径" class="form-control" name="storage_path" type="text" value="/opt/Images">
+                                <input title="虚拟机磁盘存放路径" class="form-control" id="storage_path" name="storage_path" type="text" value="/opt/Images">
                             </div>
                             <!-- HDD for Hard Disk Drive -->
                             <label id="hdd_type_label" class="col-sm-2 control-label">磁盘类型</label>
@@ -191,7 +243,7 @@
                         <div class="form-group">
                             <label class="col-sm-2 control-label"></label>
                             <div class="col-sm-3 pull-right">
-                                <button class="btn btn-blue-alt" style="width: 180px; height: 40px; font-size: 16px;">初始化</button>
+                                <button class="btn btn-blue-alt" style="width: 180px; height: 40px; font-size: 16px;" onclick="create()">初始化</button>
                             </div>
                         </div>
                     </form>

+ 26 - 26
templates/config_show.html

@@ -265,68 +265,68 @@
                     <div class="form-group">
                         <label class="col-sm-2 control-label">网桥(业务网络)</label>
                         <div class="col-sm-3">
-                            <input title="Guest 业务网络" class="form-control" name="vm_network" type="text" disabled value="{{ config_ret.data.vm_network }}">
+                            <input title="Guest 业务网络" class="form-control" name="vm_network" type="text" disabled value="{{ config.vm_network }}">
                         </div>
                         <label class="col-sm-2 control-label">网桥(管理网络)</label>
                         <div class="col-sm-3">
-                            <input title="Guest 管理网络" class="form-control" name="vm_manage_network" type="text" disabled value="{{ config_ret.data.vm_manage_network }}">
+                            <input title="Guest 管理网络" class="form-control" name="vm_manage_network" type="text" disabled value="{{ config.vm_manage_network }}">
                         </div>
                     </div>
                     <div class="form-group">
                         <label class="col-sm-2 control-label">起始 IP</label>
                         <div class="col-sm-3">
-                            <input title="起始 IP" class="form-control" name="start_ip" type="text" disabled value="{{ config_ret.data.start_ip }}">
+                            <input title="起始 IP" class="form-control" name="start_ip" type="text" disabled value="{{ config.start_ip }}">
                         </div>
                         <label class="col-sm-2 control-label">截止 IP</label>
                         <div class="col-sm-3">
-                            <input title="截止 IP" class="form-control" name="end_ip" type="text" disabled value="{{ config_ret.data.end_ip }}">
+                            <input title="截止 IP" class="form-control" name="end_ip" type="text" disabled value="{{ config.end_ip }}">
                         </div>
                     </div>
                     <div class="form-group">
                         <label class="col-sm-2 control-label">子网掩码</label>
                         <div class="col-sm-3">
-                            <input title="子网掩码" class="form-control" name="netmask" type="text" disabled value="{{ config_ret.data.netmask }}">
+                            <input title="子网掩码" class="form-control" name="netmask" type="text" disabled value="{{ config.netmask }}">
                         </div>
                         <label class="col-sm-2 control-label">网关</label>
                         <div class="col-sm-3">
-                            <input title="网关" class="form-control" name="gateway" type="text" disabled value="{{ config_ret.data.gateway }}">
+                            <input title="网关" class="form-control" name="gateway" type="text" disabled value="{{ config.gateway }}">
                         </div>
                     </div>
                     <div class="form-group">
                         <label class="col-sm-2 control-label">DNS1</label>
                         <div class="col-sm-3">
-                            <input title="DNS1" class="form-control" name="dns1" type="text" disabled value="{{ config_ret.data.dns1 }}">
+                            <input title="DNS1" class="form-control" name="dns1" type="text" disabled value="{{ config.dns1 }}">
                         </div>
                         <label class="col-sm-2 control-label">DNS2</label>
                         <div class="col-sm-3">
-                            <input title="DNS2" class="form-control" name="dns2" type="text" disabled value="{{ config_ret.data.dns2 }}">
+                            <input title="DNS2" class="form-control" name="dns2" type="text" disabled value="{{ config.dns2 }}">
                         </div>
                     </div>
                     <div class="form-group">
                         <label class="col-sm-2 control-label">起始 VNC 端口</label>
                         <div class="col-sm-3">
-                            <input title="起始 VNC 端口" class="form-control" name="start_vnc_port" type="text" disabled value="{{ config_ret.data.start_vnc_port }}">
+                            <input title="起始 VNC 端口" class="form-control" name="start_vnc_port" type="text" disabled value="{{ config.start_vnc_port }}">
                         </div>
                     </div>
                     <div class="form-group">
                         <label class="col-sm-2 control-label">虚拟机磁盘存储模式</label>
                         <div class="col-sm-3">
                             <select id="storage_mode" title="虚拟机磁盘存储模式" class="selectpicker" name="storage_mode" data-width="100%" disabled>
-                                <option {% if config_ret.data.storage_mode == 0 %}selected{% endif %} value="0">本地存储</option>
-                                <option {% if config_ret.data.storage_mode == 1 %}selected{% endif %} value="1" data-subtext="NFS">挂载共享</option>
-                                <option {% if config_ret.data.storage_mode == 2 %}selected{% endif %} value="2">Ceph</option>
-                                <option {% if config_ret.data.storage_mode == 3 %}selected{% endif %} value="3">GlusterFS</option>
+                                <option {% if config.storage_mode == 0 %}selected{% endif %} value="0">本地存储</option>
+                                <option {% if config.storage_mode == 1 %}selected{% endif %} value="1" data-subtext="NFS">挂载共享</option>
+                                <option {% if config.storage_mode == 2 %}selected{% endif %} value="2">Ceph</option>
+                                <option {% if config.storage_mode == 3 %}selected{% endif %} value="3">GlusterFS</option>
                             </select>
                         </div>
-                        <label id="dfs_volume_label" class="col-sm-2 control-label" style="display: {% if config_ret.data.storage_mode in [3] %}unset{% else %}none{% endif %};">分布式文件系统存储卷</label>
+                        <label id="dfs_volume_label" class="col-sm-2 control-label" style="display: {% if config.storage_mode in [3] %}unset{% else %}none{% endif %};">分布式文件系统存储卷</label>
                         <div class="col-sm-3">
-                            <input id="dfs_volume" title="分布式文件系统存储卷" class="form-control" name="dfs_volume" type="text" style="display: {% if config_ret.data.storage_mode in [3] %}unset{% else %}none{% endif %};" disabled value="{{ config_ret.data.dfs_volume }}">
+                            <input id="dfs_volume" title="分布式文件系统存储卷" class="form-control" name="dfs_volume" type="text" style="display: {% if config.storage_mode in [3] %}unset{% else %}none{% endif %};" disabled value="{{ config.dfs_volume }}">
                         </div>
                     </div>
                     <div class="form-group">
                         <label class="col-sm-2 control-label">虚拟机磁盘存放路径</label>
                         <div class="col-sm-3">
-                            <input title="虚拟机磁盘存放路径" class="form-control" name="storage_path" type="text" disabled value="{{ config_ret.data.storage_path }}">
+                            <input title="虚拟机磁盘存放路径" class="form-control" name="storage_path" type="text" disabled value="{{ config.storage_path }}">
                         </div>
                     </div>
                 </form>
@@ -339,12 +339,12 @@
                             &nbsp;&nbsp;IOPS 基准值
                         </label>
                         <div class="col-sm-3">
-                            <input id="iops_base" title="IOPS 基准值" class="form-control" type="text" value="{{ config_ret.data.iops_base }}" name="iops_base">
+                            <input id="iops_base" title="IOPS 基准值" class="form-control" type="text" value="{{ config.iops_base }}" name="iops_base">
                         </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_base" title="BPS 基准值" class="form-control" type="text" value="{{ (config_ret.data.bps_base / 1024 / 1024) | int }}" name="bps_base">
+                            <input id="bps_base" title="BPS 基准值" class="form-control" type="text" value="{{ (config.bps_base / 1024 / 1024) | int }}" name="bps_base">
                         </div>
                         <span class="col-sm-1"></span>
                     </div>
@@ -354,12 +354,12 @@
                             &nbsp;&nbsp;IOPS/GiB 增益
                         </label>
                         <div class="col-sm-3">
-                            <input id="iops_pre_unit" title="IOPS/GiB 增益" class="form-control" type="text" value="{{ config_ret.data.iops_pre_unit }}" name="iops_pre_unit">
+                            <input id="iops_pre_unit" title="IOPS/GiB 增益" class="form-control" type="text" value="{{ config.iops_pre_unit }}" name="iops_pre_unit">
                         </div>
                         <span class="col-sm-1"></span>
                         <label class="col-sm-2 control-label"><span class="glyph-icon icon-openid"></span>&nbsp;&nbsp;BPS/GiB 增益</label>
                         <div class="col-sm-3">
-                            <input id="bps_pre_unit" title="BPS/GiB 增益 (MiB)" class="form-control" type="text" value="{{ '%0.2f'|format(config_ret.data.bps_pre_unit / 1024 / 1024) }}" name="bps_pre_unit">
+                            <input id="bps_pre_unit" title="BPS/GiB 增益 (MiB)" class="form-control" type="text" value="{{ '%0.2f'|format(config.bps_pre_unit / 1024 / 1024) }}" name="bps_pre_unit">
                         </div>
                         <span class="col-sm-1"></span>
                     </div>
@@ -369,12 +369,12 @@
                             &nbsp;&nbsp;IOPS 增益上限
                         </label>
                         <div class="col-sm-3">
-                            <input id="iops_cap" title="IOPS 增益上限" class="form-control" type="text" value="{{ config_ret.data.iops_cap }}" name="iops_cap">
+                            <input id="iops_cap" title="IOPS 增益上限" class="form-control" type="text" value="{{ config.iops_cap }}" name="iops_cap">
                         </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_cap" title="BPS 增益上限 (MiB)" class="form-control" type="text" value="{{ (config_ret.data.bps_cap / 1024 / 1024) | int }}" name="bps_cap">
+                            <input id="bps_cap" title="BPS 增益上限 (MiB)" class="form-control" type="text" value="{{ (config.bps_cap / 1024 / 1024) | int }}" name="bps_cap">
                         </div>
                         <span class="col-sm-1"></span>
                     </div>
@@ -385,12 +385,12 @@
                             &nbsp;&nbsp;IOPS 桶宽
                         </label>
                         <div class="col-sm-3">
-                            <input id="iops_max" title="IOPS 桶宽" class="form-control" type="text" value="{{ config_ret.data.iops_max }}" name="iops_max">
+                            <input id="iops_max" title="IOPS 桶宽" class="form-control" type="text" value="{{ config.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 桶宽 (MiB)" class="form-control" type="text" value="{{ (config_ret.data.bps_max / 1024 / 1024) | int }}" name="bps_max">
+                            <input id="bps_max" title="BPS 桶宽 (MiB)" class="form-control" type="text" value="{{ (config.bps_max / 1024 / 1024) | int }}" name="bps_max">
                         </div>
                         <span class="col-sm-1"></span>
                     </div>
@@ -401,12 +401,12 @@
                             &nbsp;&nbsp;IOPS 桶高
                         </label>
                         <div class="col-sm-3">
-                            <input id="iops_max_length" title="IOPS 桶高 (秒)" class="form-control" type="text" value="{{ config_ret.data.iops_max_length }}" name="iops_max_length">
+                            <input id="iops_max_length" title="IOPS 桶高 (秒)" class="form-control" type="text" value="{{ config.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="{{ config_ret.data.bps_max_length }}" name="bps_max_length">
+                            <input id="bps_max_length" title="BPS 桶高 (秒)" class="form-control" type="text" value="{{ config.bps_max_length }}" name="bps_max_length">
                         </div>
                         <span class="col-sm-1"></span>
                     </div>

+ 10 - 53
views/config.py

@@ -3,7 +3,7 @@
 
 
 import json
-from flask import Blueprint, render_template, request, abort
+from flask import Blueprint, render_template, request, abort, url_for
 import requests
 
 
@@ -21,8 +21,7 @@ blueprint = Blueprint(
 
 
 def show():
-    host_url = request.host_url.rstrip('/')
-    config_url = host_url + '/api/config'
+    config_url = url_for('api_config.r_get', _external=True)
     config_ret = requests.get(url=config_url, cookies=request.cookies)
     config_ret = json.loads(config_ret.content)
 
@@ -30,59 +29,17 @@ def show():
     if config_ret['state']['code'] == '404':
         abort(404)
 
-    return render_template('config_show.html', config_ret=config_ret)
+    return render_template('config_show.html', config=config_ret['data'])
 
 
 def create():
-    host_url = request.host_url.rstrip('/')
-
-    if request.method == 'POST':
-        payload = {
-            'jimv_edition': int(request.form.get('jimv_edition', 0)),
-            'storage_mode': int(request.form.get('storage_mode')),
-            'dfs_volume': request.form.get('dfs_volume'),
-            'storage_path': request.form.get('storage_path'),
-            'vm_network': request.form.get('vm_network'),
-            'vm_manage_network': request.form.get('vm_manage_network'),
-            'start_ip': request.form.get('start_ip'),
-            'end_ip': request.form.get('end_ip'),
-            'start_vnc_port': int(request.form.get('start_vnc_port')),
-            'netmask': request.form.get('netmask'),
-            'gateway': request.form.get('gateway'),
-            'dns1': request.form.get('dns1'),
-            'dns2': request.form.get('dns2'),
-            'iops_base': int(request.form.get('iops_base', 1000)),
-            'iops_pre_unit': int(request.form.get('iops_pre_unit', 1)),
-            'iops_cap': int(request.form.get('iops_cap', 2000)),
-            'iops_max': int(request.form.get('iops_max', 3000)),
-            'iops_max_length': int(request.form.get('iops_max_length', 20)),
-            # 200 MiB
-            'bps_base': int(request.form.get('bps_base', 1024 * 1024 * 200)),
-            # 0.3 MiB
-            'bps_pre_unit': int(request.form.get('bps_pre_unit', 1024 * 1024 * 0.3)),
-            # 500 MiB
-            'bps_cap': int(request.form.get('bps_cap', 1024 * 1024 * 500)),
-            # 1 GiB
-            'bps_max': int(request.form.get('bps_max', 1024 * 1024 * 1024)),
-            'bps_max_length': int(request.form.get('bps_max_length', 10))
-        }
-
-        url = host_url + '/api/config'
-        headers = {'content-type': 'application/json'}
-        config_ret = requests.post(url, data=json.dumps(payload), headers=headers, cookies=request.cookies)
-        config_ret = json.loads(config_ret.content)
-        return render_template('success.html', go_back_url='/', timeout=5000, title='提交成功',
-                               message_title='初始化 JimV 请求已被接受',
-                               message='JimV 已被初始化。页面将在5秒钟后自动跳转到实例列表页面!')
+    config_url = url_for('api_config.r_get', _external=True)
+    config_ret = requests.get(url=config_url, cookies=request.cookies)
+    config_ret = json.loads(config_ret.content)
 
+    # 检测 JimV 的配置是否已被初始化,只有未被初始化时,才展现初始化配置页面
+    if config_ret['state']['code'] == '404':
+        return render_template('config_init.html')
     else:
-        url = host_url + '/api/config'
-        config_ret = requests.get(url=url, cookies=request.cookies)
-        config_ret = json.loads(config_ret.content)
-
-        # 检测 JimV 的配置是否已被初始化,只有未被初始化时,才展现初始化配置页面
-        if config_ret['state']['code'] == '404':
-            return render_template('config_init.html')
-        else:
-            abort(404)
+        abort(404)
 

+ 3 - 3
views_route_table.py

@@ -22,7 +22,7 @@ __copyright__ = '(c) 2017 by James Iter.'
 
 
 add_rule_views(config.blueprint, '', views_func='config.show', methods=['GET'])
-add_rule_views(config.blueprint, '/create', views_func='config.create', methods=['GET', 'POST'])
+add_rule_views(config.blueprint, '/create', views_func='config.create', methods=['GET'])
 
 add_rule_views(misc.blueprint, 'login', views_func='misc.login', methods=['GET'])
 add_rule_views(misc.blueprint, 'change_password', views_func='misc.change_password', methods=['GET'])
@@ -37,8 +37,8 @@ add_rule_views(guest.blueprint, '/vnc/<uuid>', views_func='guest.vnc', methods=[
 add_rule_views(guest.blueprint, '/detail/<uuid>', views_func='guest.detail', methods=['GET'])
 
 add_rule_views(disk.blueprints, '', views_func='disk.show', methods=['GET'])
-add_rule_views(disk.blueprints, '/create', views_func='disk.create', methods=['GET', 'POST'])
-add_rule_views(disk.blueprint, '/detail/<uuid>', views_func='disk.detail', methods=['GET', 'POST'])
+add_rule_views(disk.blueprints, '/create', views_func='disk.create', methods=['GET'])
+add_rule_views(disk.blueprint, '/detail/<uuid>', views_func='disk.detail', methods=['GET'])
 
 add_rule_views(log.blueprints, '', views_func='log.show', methods=['GET'])