Просмотр исходного кода

实现展现Guest的创建进度

James Iter 9 лет назад
Родитель
Сommit
ee3c5d5dea
11 измененных файлов с 30 добавлено и 17 удалено
  1. 1 0
      docs/todo.md
  2. 1 1
      main.py
  3. 1 0
      misc/init.sql
  4. 7 1
      models/event_processor.py
  5. 1 0
      models/guest.py
  6. 2 0
      models/initialize.py
  7. 1 0
      models/status.py
  8. 12 12
      models/utils.py
  9. 2 1
      requirements.txt
  10. 1 1
      templates/guests_show.html
  11. 1 1
      templates/login.html

+ 1 - 0
docs/todo.md

@@ -34,6 +34,7 @@
 - [x] 重新设计创建虚拟机时的机制,虚拟机创建分配由 JimV-C 角色实现
 - [ ] 虚拟机状态变化时时展现在前端页面上
 - [ ] 迁移时计算出剩余时间
+- [ ] 创建虚拟机时显示进度
 - [x] 重新设计 JimV-N 到 JimV-C 的回调机制,现有实现比较混乱。改进为基于对象的回调
 - [x] 重新设计创建虚拟机机制,批量创建虚拟机时,依照宿主机负载大小,倒序依次挨个分配虚拟机的创建任务
 - [x] 规范日志路径。把日志路径调整至 /var/log/ 下

+ 1 - 1
main.py

@@ -17,7 +17,7 @@ from flask.ext.session import Session
 
 from models import Utils
 from models.event_processor import EventProcessor
-from models.initialize import app, logger, q_ws, Init
+from models.initialize import socketio, logger, q_ws, Init
 import api_route_table
 import views_route_table
 from models import Database as db

+ 1 - 0
misc/init.sql

@@ -36,6 +36,7 @@ CREATE TABLE IF NOT EXISTS guest(
     create_time BIGINT UNSIGNED NOT NULL,
     -- 运行时的状态用 status;
     status TINYINT UNSIGNED NOT NULL DEFAULT 0,
+    progress TINYINT UNSIGNED NOT NULL DEFAULT 0,
     on_host VARCHAR(128) NOT NULL DEFAULT '',
     cpu TINYINT UNSIGNED NOT NULL,
     memory INT UNSIGNED NOT NULL,

+ 7 - 1
models/event_processor.py

@@ -98,8 +98,14 @@ class EventProcessor(object):
 
                     cls.guest_migrate_info.create()
 
+        elif cls.guest.status == GuestState.creating.value:
+            cls.guest.progress = cls.message['message']['progress']
+
         cls.guest.update()
-        cls.disk.update_by_filter({'on_host': cls.guest.on_host}, filter_str='guest_uuid:eq:' + cls.guest.uuid)
+
+        # 限定特殊情况下更新磁盘所属 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)
 
     @classmethod
     def host_event_processor(cls):

+ 1 - 0
models/guest.py

@@ -33,6 +33,7 @@ class Guest(ORM):
         self.os_template_id = None
         self.create_time = ji.Common.tus()
         self.status = GuestState.no_state.value
+        self.progress = 0
         self.on_host = ''
         self.cpu = None
         self.memory = None

+ 2 - 0
models/initialize.py

@@ -4,6 +4,7 @@
 
 from multiprocessing import JoinableQueue
 from flask import Flask
+from flask_socketio import SocketIO
 import logging
 from logging.handlers import TimedRotatingFileHandler
 import json
@@ -150,6 +151,7 @@ config = Init.load_config()
 logger = Init.init_logger()
 
 app.config = dict(app.config, **config)
+socketio = SocketIO(app)
 
 ji.index_state['branch'] = dict(ji.index_state['branch'], **own_state_branch)
 

+ 1 - 0
models/status.py

@@ -46,6 +46,7 @@ class GuestState(IntEnum):
     pm_suspended = 7
     migrating = 8
     update = 9
+    creating = 10
     dirty = 255
 
 

+ 12 - 12
models/utils.py

@@ -147,59 +147,59 @@ def utility_processor():
     def format_datetime_by_tus(tus, fmt='%y-%m-%d %H:%M'):
         return time.strftime(fmt, time.localtime(tus/1000/1000))
 
-    def format_guest_status(status):
+    def format_guest_status(_status, progress):
         from status import GuestState
 
         color = 'FF645B'
         icon = 'glyph-icon icon-bolt'
         desc = '未知状态'
 
-        if status == GuestState.running.value:
+        if _status == GuestState.running.value:
             color = '00BB00'
             icon = 'glyph-icon icon-circle'
             desc = '运行中'
 
-        elif status == GuestState.no_state.value:
+        elif _status in [GuestState.no_state.value, GuestState.creating.value]:
             color = 'FFC543'
             icon = 'glyph-icon icon-spinner'
-            desc = '创建中'
+            desc = ' '.join(['创建中', str(progress)+'%'])
 
-        elif status == GuestState.blocked.value:
+        elif _status == GuestState.blocked.value:
             color = '3D4245'
             icon = 'glyph-icon icon-minus-square'
             desc = '被阻塞'
 
-        elif status == GuestState.paused.value:
+        elif _status == GuestState.paused.value:
             color = 'B7B904'
             icon = 'glyph-icon icon-pause'
             desc = '暂停'
 
-        elif status == GuestState.shutdown.value:
+        elif _status == GuestState.shutdown.value:
             color = '4E5356'
             icon = 'glyph-icon icon-terminal'
             desc = '关闭'
 
-        elif status == GuestState.shutoff.value:
+        elif _status == GuestState.shutoff.value:
             color = 'FFC543'
             icon = 'glyph-icon icon-plug'
             desc = '断电'
 
-        elif status == GuestState.crashed.value:
+        elif _status == GuestState.crashed.value:
             color = '9E2927'
             icon = 'glyph-icon icon-question'
             desc = '已崩溃'
 
-        elif status == GuestState.pm_suspended.value:
+        elif _status == GuestState.pm_suspended.value:
             color = 'FCFF07'
             icon = 'glyph-icon icon-anchor'
             desc = '悬挂'
 
-        elif status == GuestState.migrating.value:
+        elif _status == GuestState.migrating.value:
             color = '1CF5E7'
             icon = 'glyph-icon icon-space-shuttle'
             desc = '迁移中'
 
-        elif status == GuestState.dirty.value:
+        elif _status == GuestState.dirty.value:
             color = 'FF0707'
             icon = 'glyph-icon icon-remove'
             desc = '创建失败,待清理'

+ 2 - 1
requirements.txt

@@ -8,4 +8,5 @@ Flask-Session==0.3.0
 gunicorn
 eventlet
 redis
-websockify
+websockify
+flask-socketio

+ 1 - 1
templates/guests_show.html

@@ -654,7 +654,7 @@
                         </div>
                     </td>
                     <td><span class="{{ os_templates_mapping_by_id[item.os_template_id].icon }}"></span></td>
-                    <td>{{ format_guest_status(item.status)|safe }}</td>
+                    <td>{{ format_guest_status(item.status, item.progress)|safe }}</td>
                     <td>{{ item.on_host }}</td>
                     <td>CPU :&nbsp;&nbsp;{{ item.cpu }}&nbsp;核<br />内存 :&nbsp;&nbsp;{{ item.memory }}&nbsp;GB</td>
                     <td>{{ item.ip }}</td>

+ 1 - 1
templates/login.html

@@ -96,7 +96,7 @@
                         </div>
                     </div>
                     <div class="form-group">
-                        <button id="login_submit" type="button" class="btn btn-block btn-primary">Login</button>
+                        <button id="login_submit" type="button" class="btn btn-block btn-primary">登录</button>
                     </div>
                 </div>
             </div>