Explorar el Código

实现 Guest 删除时,回收 IP、VNC 端口

James Iter hace 9 años
padre
commit
9e4778fb92
Se han modificado 4 ficheros con 40 adiciones y 22 borrados
  1. 2 1
      docs/todo.md
  2. 15 1
      models/event_processor.py
  3. 17 17
      tests/test_guest.py
  4. 6 3
      tests/test_os_init_write.py

+ 2 - 1
docs/todo.md

@@ -6,6 +6,7 @@
 > 则回收。如果不是,则直接丢弃;
 > 2. 给宿主机替换手动指定的任意 IP。此时需要判断手动分配的 IP 是否与已用的 IP 冲突;
 
+- [x] Guest 删除后,回收其 IP、VNC 端口
 - [ ] os_init_write 的更新方法里,是否需要支持 os_init_id 参数
 - [ ] 增加模板镜像上传功能
 - [x] 抽象出视图层的父类,尤其是 get、get_list、delete 这些方法基本相同
@@ -16,4 +17,4 @@
 - [ ] 统计活着的宿主机,并提供获取列表的接口,供迁移时选择
 - [ ] 加入被动更新 Guest xml 的功能,由宿主机主动推送。具体时机待定
 - [x] 统一 guest_disk 称谓为 disk
-- [ ] 分离出 API 与 views 出入口,涉及 add_rule 与 route_table
+- [ ] 分离出 API 与 views 出入口,涉及 add_rule 与 route_table

+ 15 - 1
models/event_processor.py

@@ -4,8 +4,9 @@
 
 import json
 import time
+from IPy import IP
 
-from models import Database as db
+from models import Database as db, Config
 from models import Guest
 from models import Disk
 from models import Log
@@ -26,6 +27,7 @@ class EventProcessor(object):
     log = Log()
     guest = Guest()
     disk = Disk()
+    config = Config()
 
     @classmethod
     def log_processor(cls):
@@ -70,8 +72,20 @@ class EventProcessor(object):
 
         elif action == 'delete_guest':
             if state == ResponseState.success.value:
+                cls.config.id = 1
+                cls.config.get()
                 cls.guest.uuid = uuid
                 cls.guest.get_by('uuid')
+
+                if IP(cls.config.start_ip).int() <= IP(cls.guest.ip).int() <= IP(cls.config.end_ip).int():
+                    if db.r.srem(app.config['ip_used_set'], cls.guest.ip):
+                        db.r.sadd(app.config['ip_available_set'], cls.guest.ip)
+
+                if (cls.guest.vnc_port - cls.config.start_vnc_port) <= \
+                        (IP(cls.config.end_ip).int() - IP(cls.config.start_ip).int()):
+                    if db.r.srem(app.config['vnc_port_used_set'], cls.guest.vnc_port):
+                        db.r.sadd(app.config['vnc_port_available_set'], cls.guest.vnc_port)
+
                 cls.guest.delete()
 
                 cls.disk.uuid = uuid

+ 17 - 17
tests/test_guest.py

@@ -27,23 +27,23 @@ class TestGuest(unittest.TestCase):
 
     # 创建Guest
     # @unittest.skip('skip create guest')
-    # def test_11_create(self):
-    #     payload = {
-    #         "cpu": 4,
-    #         "memory": 4,
-    #         "os_template_id": 6,
-    #         "quantity": 2,
-    #         "name": "",
-    #         "password": "pswd.com",
-    #         "lease_term": 100
-    #     }
-    #
-    #     url = TestGuest.base_url + '/guest'
-    #     headers = {'content-type': 'application/json'}
-    #     r = requests.post(url, data=json.dumps(payload), headers=headers)
-    #     j_r = json.loads(r.content)
-    #     print json.dumps(j_r, ensure_ascii=False)
-    #     self.assertEqual('200', j_r['state']['code'])
+    def test_11_create(self):
+        payload = {
+            "cpu": 4,
+            "memory": 4,
+            "os_template_id": 1,
+            "quantity": 2,
+            "name": "",
+            "password": "pswd.com",
+            "lease_term": 100
+        }
+
+        url = TestGuest.base_url + '/guest'
+        headers = {'content-type': 'application/json'}
+        r = requests.post(url, data=json.dumps(payload), headers=headers)
+        j_r = json.loads(r.content)
+        print json.dumps(j_r, ensure_ascii=False)
+        self.assertEqual('200', j_r['state']['code'])
 
     # 获取 Guest 列表
     # def test_12_get_list(self):

+ 6 - 3
tests/test_os_init_write.py

@@ -185,8 +185,11 @@ class TestOSInitWrite(unittest.TestCase):
     # def test_53_create(self):
     #     payload = {
     #         "os_init_id": 8,
-    #         "path": "/etc/hostname",
-    #         "content": "hostname=\"{HOSTNAME}\""
+    #         "path": "/etc/sysconfig/network",
+    #         "content": "\n".join([
+    #             "NETWORKING=yes",
+    #             "HOSTNAME=\"{HOSTNAME}\""
+    #         ])
     #     }
     #
     #     url = TestOSInitWrite.base_url + '/os_init_write'
@@ -235,7 +238,7 @@ class TestOSInitWrite(unittest.TestCase):
     #     payload = {
     #         "os_init_id": 7,
     #         "path": "/etc/conf.d/hostname",
-    #         "content": "{HOSTNAME}"
+    #         "content": "hostname=\"{HOSTNAME}\""
     #     }
     #
     #     url = TestOSInitWrite.base_url + '/os_init_write'