Przeglądaj źródła

加入事件处理器

James Iter 9 lat temu
rodzic
commit
ef696b5f4a
5 zmienionych plików z 40 dodań i 16 usunięć
  1. 4 0
      misc/init.sql
  2. 2 2
      models/__init__.py
  3. 14 1
      models/event_processor.py
  4. 8 6
      models/guest.py
  5. 12 7
      models/status.py

+ 4 - 0
misc/init.sql

@@ -12,6 +12,7 @@ CREATE TABLE IF NOT EXISTS guest(
     os_template_id BIGINT UNSIGNED NOT NULL,
     create_time BIGINT UNSIGNED NOT NULL,
     status TINYINT UNSIGNED NOT NULL DEFAULT 0,
+    on_host VARCHAR(128) NOT NULL DEFAULT '',
     cpu TINYINT UNSIGNED NOT NULL,
     memory INT UNSIGNED NOT NULL,
     ip CHAR(15) NOT NULL,
@@ -24,7 +25,10 @@ CREATE TABLE IF NOT EXISTS guest(
     ENGINE=InnoDB
     DEFAULT CHARSET=utf8;
 
+ALTER TABLE guest ADD INDEX (uuid);
 ALTER TABLE guest ADD INDEX (name);
+ALTER TABLE guest ADD INDEX (on_host);
+ALTER TABLE guest ADD INDEX (ip);
 
 
 CREATE TABLE IF NOT EXISTS guest_disk(

+ 2 - 2
models/__init__.py

@@ -49,7 +49,7 @@ from os_template import (
 
 from status import (
     EmitKind,
-    GuestEvent,
+    GuestState,
     LogLevel
 )
 
@@ -69,7 +69,7 @@ __copyright__ = '(c) 2017 by James Iter.'
 
 
 __all__ = [
-    'Rules', 'Utils', 'Init', 'Database', 'FilterFieldType', 'Filter', 'EmitKind', 'GuestEvent', 'LogLevel', 'ORM',
+    'Rules', 'Utils', 'Init', 'Database', 'FilterFieldType', 'Filter', 'EmitKind', 'GuestState', 'LogLevel', 'ORM',
     'Config', 'Guest', 'GuestDisk', 'OSInit', 'OSInitWrite', 'OSTemplate', 'GuestXML', 'Log', 'EventProcessor'
 ]
 

+ 14 - 1
models/event_processor.py

@@ -6,6 +6,7 @@ import json
 import time
 
 from models import Database as db
+from models import Guest
 from models import Log
 from models import Utils
 from models import EmitKind
@@ -21,6 +22,7 @@ __copyright__ = '(c) 2017 by James Iter.'
 class EventProcessor(object):
     message = None
     log = Log()
+    guest = Guest()
 
     @classmethod
     def log_processor(cls):
@@ -29,6 +31,14 @@ class EventProcessor(object):
 
         cls.log.create()
 
+    @classmethod
+    def event_processor(cls):
+        cls.guest.uuid = cls.message['message']['uuid']
+        cls.guest.get_by('uuid')
+        cls.guest.status = cls.message['type']
+        cls.guest.on_host = cls.message['host']
+        cls.guest.update()
+
     @classmethod
     def launch(cls):
         while True:
@@ -49,7 +59,10 @@ class EventProcessor(object):
                 if cls.message['kind'] == EmitKind.log.value:
                     cls.log_processor()
 
-                if cls.message['kind'] == EmitKind.event.value:
+                elif cls.message['kind'] == EmitKind.event.value:
+                    cls.event_processor()
+
+                else:
                     pass
 
             except Exception as e:

+ 8 - 6
models/guest.py

@@ -6,7 +6,7 @@ import jimit as ji
 
 from filter import FilterFieldType
 from orm import ORM
-from status import GuestEvent
+from status import GuestState
 
 
 __author__ = 'James Iter'
@@ -29,7 +29,8 @@ class Guest(ORM):
         self.remark = ''
         self.os_template_id = None
         self.create_time = ji.Common.tus()
-        self.status = GuestEvent.shutdown.value
+        self.status = GuestState.shutdown.value
+        self.on_host = ''
         self.cpu = None
         self.memory = None
         self.ip = None
@@ -43,8 +44,9 @@ class Guest(ORM):
     def get_filter_keywords():
         return {
             'name': FilterFieldType.STR.value,
-            'remark': FilterFieldType.INT.value,
-            'ip': FilterFieldType.INT.value
+            'remark': FilterFieldType.STR.value,
+            'on_host': FilterFieldType.STR.value,
+            'ip': FilterFieldType.STR.value
         }
 
     @staticmethod
@@ -53,7 +55,7 @@ class Guest(ORM):
 
     @staticmethod
     def get_allow_content_search_keywords():
-        return ['name', 'remark', 'ip']
+        return ['name', 'remark', 'on_host', 'ip']
 
 
 class GuestDisk(ORM):
@@ -75,7 +77,7 @@ class GuestDisk(ORM):
         return {
             'label': FilterFieldType.STR.value,
             'size': FilterFieldType.INT.value,
-            'guest_id': FilterFieldType.INT.value
+            'guest_uuid': FilterFieldType.STR.value
         }
 
     @staticmethod

+ 12 - 7
models/status.py

@@ -16,13 +16,18 @@ class EmitKind(IntEnum):
     event = 1
 
 
-class GuestEvent(IntEnum):
-    shutdown = 0
-    booting = 1
-    running = 2
-    rebooting = 3
-    suspend = 4
-    resuming = 5
+class GuestState(IntEnum):
+    # 参考地址:
+    # http://libvirt.org/docs/libvirt-appdev-guide-python/en-US/html/libvirt_application_development_guide_using_python-Guest_Domains-Information-State.html
+
+    no_state = 0
+    running = 1
+    blocked = 2
+    paused = 3
+    shutdown = 4
+    shutoff = 5
+    crashed = 6
+    pm_suspended = 7
 
 
 class LogLevel(IntEnum):