Prechádzať zdrojové kódy

创建虚拟机、创建磁盘,完成 on_host 到 node_id 的转变。

James Iter 8 rokov pred
rodič
commit
45a77186c9

+ 1 - 0
api/guest.py

@@ -100,6 +100,7 @@ def r_create():
 
         # 当指定了 host 时,取全部活着的 hosts
         if node_id is not None:
+            node_id = int(node_id)
             available_hosts = Host.get_available_hosts(nonrandom=None)
 
         if available_hosts.__len__() == 0:

+ 2 - 2
models/event_processor.py

@@ -54,7 +54,7 @@ class EventProcessor(object):
     def guest_event_processor(cls):
         cls.guest.uuid = cls.message['message']['uuid']
         cls.guest.get_by('uuid')
-        cls.guest.on_host = cls.message['host']
+        cls.guest.node_id = cls.message['node_id']
         last_status = cls.guest.status
         cls.guest.status = cls.message['type']
 
@@ -108,7 +108,7 @@ class EventProcessor(object):
 
         # 限定特殊情况下更新磁盘所属 Guest,避免迁移、创建时频繁被无意义的更新
         if cls.guest.status in [GuestState.running.value, GuestState.shutoff.value]:
-            cls.disk.update_by_filter({'on_host': cls.guest.on_host}, filter_str='guest_uuid:eq:' + cls.guest.uuid)
+            cls.disk.update_by_filter({'node_id': cls.guest.node_id}, filter_str='guest_uuid:eq:' + cls.guest.uuid)
 
     @classmethod
     def host_event_processor(cls):

+ 10 - 10
models/host.py

@@ -38,39 +38,39 @@ class Host(object):
         if 'threads_status' not in v:
             v['threads_status'] = {
                 'instruction_process_engine': {
-                    'timestamp': 0,
-                    'alive': False
+                    'timestamp': 0
                 },
                 'host_state_report_engine': {
-                    'timestamp': 0,
-                    'alive': False
+                    'timestamp': 0
                 },
                 'guest_creating_progress_report_engine': {
-                    'timestamp': 0,
-                    'alive': False
+                    'timestamp': 0
                 },
                 'guest_performance_collection_engine': {
-                    'timestamp': 0,
-                    'alive': False
+                    'timestamp': 0
                 },
                 'host_performance_collection_engine': {
-                    'timestamp': 0,
-                    'alive': False
+                    'timestamp': 0
                 }
             }
 
+        v['threads_status']['instruction_process_engine']['alive'] = False
         if v['threads_status']['instruction_process_engine']['timestamp'] + coupler_length >= g.ts:
             v['threads_status']['instruction_process_engine']['alive'] = True
 
+        v['threads_status']['host_state_report_engine']['alive'] = False
         if v['threads_status']['host_state_report_engine']['timestamp'] + coupler_length >= g.ts:
             v['threads_status']['host_state_report_engine']['alive'] = True
 
+        v['threads_status']['guest_creating_progress_report_engine']['alive'] = False
         if v['threads_status']['guest_creating_progress_report_engine']['timestamp'] + coupler_length >= g.ts:
             v['threads_status']['guest_creating_progress_report_engine']['alive'] = True
 
+        v['threads_status']['guest_performance_collection_engine']['alive'] = False
         if v['threads_status']['guest_performance_collection_engine']['timestamp'] + coupler_length >= g.ts:
             v['threads_status']['guest_performance_collection_engine']['alive'] = True
 
+        v['threads_status']['host_performance_collection_engine']['alive'] = False
         if v['threads_status']['host_performance_collection_engine']['timestamp'] + coupler_length >= g.ts:
             v['threads_status']['host_performance_collection_engine']['alive'] = True
 

+ 3 - 3
templates/disk_create.html

@@ -38,7 +38,7 @@
             $('.add-transition').addClass(transAttr);
         });
         {% if show_on_host %}
-            refresh_hosts_selectpicker($('#on_host'));
+            refresh_hosts_selectpicker($('#node_id'));
         {% else %}
             $('#on_host_form').remove();
         {% endif %}
@@ -119,7 +119,7 @@
                 selectpicker.empty();
                 $.each(data.data, function(k, v) {
                     selectpicker.append(
-                        $('<option>', {value: v['hostname'], text: v['hostname']})
+                        $('<option>', {value: v['node_id'], text: v['hostname']})
                     );
                 });
                 selectpicker.selectpicker('refresh');
@@ -159,7 +159,7 @@
                     <div id="on_host_form" class="form-group">
                         <label class="col-sm-2 control-label"><span class="glyph-icon icon-desktop"></span>&nbsp;&nbsp;计算节点</label>
                         <div class="col-sm-6">
-                            <select id="on_host" name="on_host" title="计算节点" class="selectpicker">
+                            <select id="node_id" name="node_id" title="计算节点" class="selectpicker">
                             </select>
                         </div>
                     </div>

+ 4 - 2
templates/disks_show.html

@@ -158,8 +158,8 @@
 
             // 单机版加入与磁盘同一计算节点的过滤
             {% if show_on_host %}
-                var on_host = $(me.relatedTarget).parent().parent().parent().parent().prev().prev().prev().text();
-                url += '&filter=on_host:eq:' + on_host;
+                var node_id = $(me.relatedTarget).parent().parent().parent().parent().prev().prev().prev().text();
+                url += '&filter=node_id:eq:' + node_id;
             {% endif %}
 
             $.ajax({
@@ -626,6 +626,7 @@
                     <th>BPS</th>
                     <th>所属虚拟机</th>
                     <th style="{% if not show_on_host %}display: none;{% endif %}">所属计算节点</th>
+                    <th style="display: none;">NodeID</th>
                     <th>磁盘性别</th>
                     <th>创建时间</th>
                     <th>操作</th>
@@ -661,6 +662,7 @@
                     {% endif %}</a>
                     </td>
                     <td style="{% if not show_on_host %}display: none;{% endif %}">{{ hosts_mapping_by_node_id[item.node_id].hostname }}</td>
+                    <td style="display: none;">{{ item.node_id }}</td>
                     <td>
                         {% if item.sequence == 0 %}
                             系统盘

+ 6 - 6
templates/guest_create.html

@@ -119,11 +119,11 @@
 
     function allocation_by_random_click_handler(me) {
         if ($(me).prop('checked')) {
-            $('#create_guest_form').formValidation('removeField', 'on_host');
-            $('#on_host').parent().parent().css('display', 'none');
+            $('#create_guest_form').formValidation('removeField', 'node_id');
+            $('#node_id').parent().parent().css('display', 'none');
         } else {
-            $('#create_guest_form').formValidation('addField', 'on_host', {validators: {notEmpty: {}}});
-            $('#on_host').parent().parent().css('display', 'unset');
+            $('#create_guest_form').formValidation('addField', 'node_id', {validators: {notEmpty: {}}});
+            $('#node_id').parent().parent().css('display', 'unset');
         }
     }
 </script>
@@ -249,9 +249,9 @@
                                 </div>
                             </div>
                             <div class="row form-group" style="display: none;">
-                                <select id="on_host" name="on_host" title="请选择计算节点..." class="selectpicker" data-width="100%">
+                                <select id="node_id" name="node_id" title="请选择计算节点..." class="selectpicker" data-width="100%">
                                     {% for item in hosts_ret.data %}
-                                        <option value="{{ item.hostname }}" data-icon="{{ item.icon }}">&nbsp;&nbsp;{{ item.hostname }}</option>
+                                        <option value="{{ item.node_id }}" data-icon="{{ item.icon }}">&nbsp;&nbsp;{{ item.hostname }}</option>
                                     {% endfor %}
                                 </select>
                             </div>

+ 2 - 2
views/disk.py

@@ -170,13 +170,13 @@ def create():
         size = request.form.get('size')
         quantity = request.form.get('quantity')
         remark = request.form.get('remark')
-        on_host = request.form.get('on_host')
+        node_id = request.form.get('node_id')
 
         payload = {
             "size": int(size),
             "quantity": int(quantity),
             "remark": remark,
-            "on_host": on_host
+            "node_id": node_id
         }
 
         url = host_url + '/api/disk'

+ 11 - 3
views/guest.py

@@ -133,7 +133,7 @@ def create():
         password = request.form.get('password')
         remark = request.form.get('remark')
         allocation_by_random = 'allocation_by_random' in request.form
-        on_host = request.form.get('on_host')
+        node_id = request.form.get('node_id')
 
         if not isinstance(ability, basestring):
             pass
@@ -156,7 +156,7 @@ def create():
         }
 
         if not allocation_by_random:
-            payload['on_host'] = on_host
+            payload['node_id'] = node_id
 
         url = host_url + '/api/guest'
         headers = {'content-type': 'application/json'}
@@ -207,11 +207,19 @@ def port_is_opened(port):
 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
+
     port = random.randrange(50000, 60000)
     while True:
         if not port_is_opened(port=port):
@@ -219,7 +227,7 @@ def vnc(uuid):
 
         port = random.randrange(50000, 60000)
 
-    payload = {'listen_port': port, 'target_host': guest_ret['data']['on_host'],
+    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)