Sfoglia il codice sorgente

加入事件处理器

James Iter 9 anni fa
parent
commit
ef696b5f4a
5 ha cambiato i file con 40 aggiunte e 16 eliminazioni
  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,
     os_template_id BIGINT UNSIGNED NOT NULL,
     create_time BIGINT UNSIGNED NOT NULL,
     create_time BIGINT UNSIGNED NOT NULL,
     status TINYINT UNSIGNED NOT NULL DEFAULT 0,
     status TINYINT UNSIGNED NOT NULL DEFAULT 0,
+    on_host VARCHAR(128) NOT NULL DEFAULT '',
     cpu TINYINT UNSIGNED NOT NULL,
     cpu TINYINT UNSIGNED NOT NULL,
     memory INT UNSIGNED NOT NULL,
     memory INT UNSIGNED NOT NULL,
     ip CHAR(15) NOT NULL,
     ip CHAR(15) NOT NULL,
@@ -24,7 +25,10 @@ CREATE TABLE IF NOT EXISTS guest(
     ENGINE=InnoDB
     ENGINE=InnoDB
     DEFAULT CHARSET=utf8;
     DEFAULT CHARSET=utf8;
 
 
+ALTER TABLE guest ADD INDEX (uuid);
 ALTER TABLE guest ADD INDEX (name);
 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(
 CREATE TABLE IF NOT EXISTS guest_disk(

+ 2 - 2
models/__init__.py

@@ -49,7 +49,7 @@ from os_template import (
 
 
 from status import (
 from status import (
     EmitKind,
     EmitKind,
-    GuestEvent,
+    GuestState,
     LogLevel
     LogLevel
 )
 )
 
 
@@ -69,7 +69,7 @@ __copyright__ = '(c) 2017 by James Iter.'
 
 
 
 
 __all__ = [
 __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'
     'Config', 'Guest', 'GuestDisk', 'OSInit', 'OSInitWrite', 'OSTemplate', 'GuestXML', 'Log', 'EventProcessor'
 ]
 ]
 
 

+ 14 - 1
models/event_processor.py

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

+ 8 - 6
models/guest.py

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

+ 12 - 7
models/status.py

@@ -16,13 +16,18 @@ class EmitKind(IntEnum):
     event = 1
     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):
 class LogLevel(IntEnum):