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

IP 池加入活跃状态概念

James Iter 7 лет назад
Родитель
Сommit
89d627723b
6 измененных файлов с 32 добавлено и 133 удалено
  1. 1 63
      jimvc/api/config.py
  2. 18 0
      jimvc/api/ip_pool.py
  3. 0 62
      jimvc/models/config.py
  4. 3 1
      jimvc/models/ip_pool.py
  5. 9 7
      jimvc/models/rules.py
  6. 1 0
      misc/init.sql

+ 1 - 63
jimvc/api/config.py

@@ -35,13 +35,6 @@ def r_create():
         Rules.STORAGE_PATH.value,
         Rules.STORAGE_PATH.value,
         Rules.VM_NETWORK.value,
         Rules.VM_NETWORK.value,
         Rules.VM_MANAGE_NETWORK.value,
         Rules.VM_MANAGE_NETWORK.value,
-        Rules.START_IP.value,
-        Rules.END_IP.value,
-        Rules.START_VNC_PORT.value,
-        Rules.NETMASK.value,
-        Rules.GATEWAY.value,
-        Rules.DNS1.value,
-        Rules.DNS2.value,
         Rules.IOPS_BASE.value,
         Rules.IOPS_BASE.value,
         Rules.IOPS_PRE_UNIT.value,
         Rules.IOPS_PRE_UNIT.value,
         Rules.IOPS_CAP.value,
         Rules.IOPS_CAP.value,
@@ -62,13 +55,6 @@ def r_create():
     config.storage_path = request.json.get('storage_path')
     config.storage_path = request.json.get('storage_path')
     config.vm_network = request.json.get('vm_network')
     config.vm_network = request.json.get('vm_network')
     config.vm_manage_network = request.json.get('vm_manage_network')
     config.vm_manage_network = request.json.get('vm_manage_network')
-    config.start_ip = request.json.get('start_ip')
-    config.end_ip = request.json.get('end_ip')
-    config.start_vnc_port = int(request.json.get('start_vnc_port', 15900))
-    config.netmask = request.json.get('netmask')
-    config.gateway = request.json.get('gateway')
-    config.dns1 = request.json.get('dns1')
-    config.dns2 = request.json.get('dns2')
     config.iops_base = int(request.json.get('iops_base', 1000))
     config.iops_base = int(request.json.get('iops_base', 1000))
     config.iops_pre_unit = int(request.json.get('iops_pre_unit', 1))
     config.iops_pre_unit = int(request.json.get('iops_pre_unit', 1))
     config.iops_cap = int(request.json.get('iops_cap', 2000))
     config.iops_cap = int(request.json.get('iops_cap', 2000))
@@ -94,9 +80,6 @@ def r_create():
             ret['state'] = ji.Common.exchange_state(40901)
             ret['state'] = ji.Common.exchange_state(40901)
             return ret
             return ret
 
 
-        config.check_ip()
-        config.generate_available_ip2set()
-        config.generate_available_vnc_port()
         config.create()
         config.create()
         config.update_global_config()
         config.update_global_config()
 
 
@@ -146,41 +129,6 @@ def r_update():
             Rules.VM_MANAGE_NETWORK.value,
             Rules.VM_MANAGE_NETWORK.value,
         )
         )
 
 
-    if 'start_ip' in request.json:
-        args_rules.append(
-            Rules.START_IP.value,
-        )
-
-    if 'end_ip' in request.json:
-        args_rules.append(
-            Rules.END_IP.value,
-        )
-
-    if 'start_vnc_port' in request.json:
-        args_rules.append(
-            Rules.START_VNC_PORT.value,
-        )
-
-    if 'netmask' in request.json:
-        args_rules.append(
-            Rules.NETMASK.value,
-        )
-
-    if 'gateway' in request.json:
-        args_rules.append(
-            Rules.GATEWAY.value,
-        )
-
-    if 'dns1' in request.json:
-        args_rules.append(
-            Rules.DNS1.value,
-        )
-
-    if 'dns2' in request.json:
-        args_rules.append(
-            Rules.DNS2.value,
-        )
-
     if args_rules.__len__() < 1:
     if args_rules.__len__() < 1:
         ret = dict()
         ret = dict()
         ret['state'] = ji.Common.exchange_state(20000)
         ret['state'] = ji.Common.exchange_state(20000)
@@ -197,17 +145,7 @@ def r_update():
         config.storage_path = request.json.get('storage_path', config.storage_path)
         config.storage_path = request.json.get('storage_path', config.storage_path)
         config.vm_network = request.json.get('vm_network', config.vm_network)
         config.vm_network = request.json.get('vm_network', config.vm_network)
         config.vm_manage_network = request.json.get('vm_manage_network', config.vm_manage_network)
         config.vm_manage_network = request.json.get('vm_manage_network', config.vm_manage_network)
-        config.start_ip = request.json.get('start_ip', config.start_ip)
-        config.end_ip = request.json.get('end_ip', config.end_ip)
-        config.start_vnc_port = int(request.json.get('start_vnc_port', config.start_vnc_port))
-        config.netmask = request.json.get('netmask', config.netmask)
-        config.gateway = request.json.get('gateway', config.gateway)
-        config.dns1 = request.json.get('dns1', config.dns1)
-        config.dns2 = request.json.get('dns2', config.dns2)
-
-        config.check_ip()
-        config.generate_available_ip2set()
-        config.generate_available_vnc_port()
+
         config.update()
         config.update()
         config.update_global_config()
         config.update_global_config()
 
 

+ 18 - 0
jimvc/api/ip_pool.py

@@ -45,6 +45,7 @@ def r_create():
         Rules.GATEWAY.value,
         Rules.GATEWAY.value,
         Rules.DNS1.value,
         Rules.DNS1.value,
         Rules.DNS2.value,
         Rules.DNS2.value,
+        Rules.ACTIVITY.value,
         Rules.NAME.value,
         Rules.NAME.value,
         Rules.DESCRIPTION.value
         Rules.DESCRIPTION.value
     ]
     ]
@@ -56,6 +57,7 @@ def r_create():
     ip_pool.gateway = request.json.get('gateway')
     ip_pool.gateway = request.json.get('gateway')
     ip_pool.dns1 = request.json.get('dns1')
     ip_pool.dns1 = request.json.get('dns1')
     ip_pool.dns2 = request.json.get('dns2')
     ip_pool.dns2 = request.json.get('dns2')
+    ip_pool.activity = request.json.get('activity')
     ip_pool.name = request.json.get('name', '')
     ip_pool.name = request.json.get('name', '')
     ip_pool.description = request.json.get('description', '')
     ip_pool.description = request.json.get('description', '')
 
 
@@ -66,6 +68,11 @@ def r_create():
         ret['state'] = ji.Common.exchange_state(20000)
         ret['state'] = ji.Common.exchange_state(20000)
 
 
         ip_pool.check_ip()
         ip_pool.check_ip()
+
+        if ip_pool.activity:
+            # 如果新创建或被更新的 IP 池为活跃状态,则更新其它 IP 池为非活跃状态
+            IPPool.update_by_filter({'activity': 0}, filter_str='id:gt:0')
+
         ip_pool.create()
         ip_pool.create()
 
 
         ip_pool.get()
         ip_pool.get()
@@ -116,6 +123,11 @@ def r_update(ids):
             Rules.DNS2.value,
             Rules.DNS2.value,
         )
         )
 
 
+    if 'activity' in request.json:
+        args_rules.append(
+            Rules.ACTIVITY.value,
+        )
+
     if 'name' in request.json:
     if 'name' in request.json:
         args_rules.append(
         args_rules.append(
             Rules.NAME.value,
             Rules.NAME.value,
@@ -150,10 +162,16 @@ def r_update(ids):
             ip_pool.gateway = request.json.get('gateway', ip_pool.gateway)
             ip_pool.gateway = request.json.get('gateway', ip_pool.gateway)
             ip_pool.dns1 = request.json.get('dns1', ip_pool.dns1)
             ip_pool.dns1 = request.json.get('dns1', ip_pool.dns1)
             ip_pool.dns2 = request.json.get('dns2', ip_pool.dns2)
             ip_pool.dns2 = request.json.get('dns2', ip_pool.dns2)
+            ip_pool.activity = request.json.get('activity', ip_pool.activity)
             ip_pool.name = request.json.get('name', ip_pool.name)
             ip_pool.name = request.json.get('name', ip_pool.name)
             ip_pool.description = request.json.get('description', ip_pool.description)
             ip_pool.description = request.json.get('description', ip_pool.description)
 
 
             ip_pool.check_ip()
             ip_pool.check_ip()
+
+            if ip_pool.activity:
+                # 如果新创建或被更新的 IP 池为活跃状态,则更新其它 IP 池为非活跃状态
+                IPPool.update_by_filter({'activity': 0}, filter_str='id:gt:0')
+
             ip_pool.update()
             ip_pool.update()
             ip_pool.get()
             ip_pool.get()
 
 

+ 0 - 62
jimvc/models/config.py

@@ -2,10 +2,6 @@
 # -*- coding: utf-8 -*-
 # -*- coding: utf-8 -*-
 
 
 
 
-import json
-import jimit as ji
-from IPy import IP, intToIp
-
 from jimvc.models import app_config
 from jimvc.models import app_config
 from jimvc.models import Database as db
 from jimvc.models import Database as db
 from jimvc.models import ORM
 from jimvc.models import ORM
@@ -33,13 +29,6 @@ class Config(ORM):
         self.storage_path = ''
         self.storage_path = ''
         self.vm_network = ''
         self.vm_network = ''
         self.vm_manage_network = ''
         self.vm_manage_network = ''
-        self.start_ip = ''
-        self.end_ip = ''
-        self.start_vnc_port = None
-        self.netmask = ''
-        self.gateway = ''
-        self.dns1 = ''
-        self.dns2 = ''
         self.iops_base = 0
         self.iops_base = 0
         self.iops_pre_unit = 0
         self.iops_pre_unit = 0
         self.iops_cap = 0
         self.iops_cap = 0
@@ -63,57 +52,6 @@ class Config(ORM):
     def get_allow_content_search_keywords():
     def get_allow_content_search_keywords():
         return []
         return []
 
 
-    def check_ip(self):
-        network_segment = IP(self.start_ip + '/' + self.netmask, make_net=True)
-
-        ret = dict()
-        # 起止IP必须在同一个网段中
-        if IP(self.end_ip) not in network_segment:
-            ret['state'] = ji.Common.exchange_state(41251)
-            raise ji.PreviewingError(json.dumps(ret, ensure_ascii=False))
-
-        # 网关必须与将分配给Guest的IP,处于同一个网段中
-        if IP(self.gateway) not in network_segment:
-            ret['state'] = ji.Common.exchange_state(41252)
-            raise ji.PreviewingError(json.dumps(ret, ensure_ascii=False))
-
-        # 网关不能是网络地址或广播地址
-        if IP(self.gateway) in [network_segment[i] for i in (0, -1)]:
-            ret['state'] = ji.Common.exchange_state(41253)
-            raise ji.PreviewingError(json.dumps(ret, ensure_ascii=False))
-
-        # 当用户输入的起始IP为网络地址时,自动重置其为该网段中第一个可用IP
-        if self.start_ip == network_segment[0].__str__():
-            self.start_ip = intToIp((int(IP(self.start_ip).strDec()) + 1).__str__(), 4)
-
-        # 当用户输入的结束IP为广播地址时,自动重置其为该网段中最后一个可用IP
-        if self.end_ip == network_segment[-1].__str__():
-            self.end_ip = intToIp((int(IP(self.end_ip).strDec()) - 1).__str__(), 4)
-
-        # 起始的可用IP地址必须小于结束的可用IP地址
-        if IP(self.start_ip) >= IP(self.end_ip):
-            ret['state'] = ji.Common.exchange_state(41254)
-            raise ji.PreviewingError(json.dumps(ret, ensure_ascii=False))
-
-        return True
-
-    def generate_available_ip2set(self):
-        # 删除现有的可用IP集合
-        db.r.delete(app_config['ip_available_set'])
-
-        for ip_dec in range(int(IP(self.start_ip).strDec()), int(IP(self.end_ip).strDec()) + 1):
-            db.r.sadd(app_config['ip_available_set'], intToIp(ip_dec, 4))
-
-    def generate_available_vnc_port(self):
-        # 删除现有的可用 vnc 端口集合
-        db.r.delete(app_config['vnc_port_available_set'])
-
-        i = 0
-        # 借用可用 IP 范围生成需要的 vnc 端口集合
-        for ip_dec in range(int(IP(self.start_ip).strDec()), int(IP(self.end_ip).strDec()) + 1):
-            i += 1
-            db.r.sadd(app_config['vnc_port_available_set'], self.start_vnc_port + i)
-
     def update_global_config(self):
     def update_global_config(self):
         db.r.hmset(app_config['global_config'], self.__dict__)
         db.r.hmset(app_config['global_config'], self.__dict__)
 
 

+ 3 - 1
jimvc/models/ip_pool.py

@@ -31,13 +31,15 @@ class IPPool(ORM):
         self.dns1 = ''
         self.dns1 = ''
         self.dns2 = ''
         self.dns2 = ''
         self.name = ''
         self.name = ''
+        self.activity = 0
         self.description = ''
         self.description = ''
         self.create_time = ji.Common.tus()
         self.create_time = ji.Common.tus()
 
 
     @staticmethod
     @staticmethod
     def get_filter_keywords():
     def get_filter_keywords():
         return {
         return {
-            'remark': FilterFieldType.STR.value
+            'name': FilterFieldType.STR.value,
+            'activity': FilterFieldType.INT.value
         }
         }
 
 
     @staticmethod
     @staticmethod

+ 9 - 7
jimvc/models/rules.py

@@ -39,13 +39,6 @@ class Rules(Enum):
     STORAGE_PATH = (basestring, 'storage_path')
     STORAGE_PATH = (basestring, 'storage_path')
     VM_NETWORK = (basestring, 'vm_network')
     VM_NETWORK = (basestring, 'vm_network')
     VM_MANAGE_NETWORK = (basestring, 'vm_manage_network')
     VM_MANAGE_NETWORK = (basestring, 'vm_manage_network')
-    START_IP = (REG_IP, 'start_ip')
-    END_IP = (REG_IP, 'end_ip')
-    START_VNC_PORT = (int, 'start_vnc_port')
-    NETMASK = (REG_IP, 'netmask')
-    GATEWAY = (REG_IP, 'gateway')
-    DNS1 = (REG_IP, 'dns1')
-    DNS2 = (REG_IP, 'dns2')
     IOPS_BASE = (int, 'iops_base')
     IOPS_BASE = (int, 'iops_base')
     IOPS_PRE_UNIT = (int, 'iops_pre_unit')
     IOPS_PRE_UNIT = (int, 'iops_pre_unit')
     IOPS_CAP = (int, 'iops_cap')
     IOPS_CAP = (int, 'iops_cap')
@@ -60,6 +53,15 @@ class Rules(Enum):
     BANDWIDTH_IN_URL = (REG_NUMBER, 'bandwidth')
     BANDWIDTH_IN_URL = (REG_NUMBER, 'bandwidth')
     BANDWIDTH_UNIT = (basestring, 'bandwidth_unit', ['k', 'm', 'g'])
     BANDWIDTH_UNIT = (basestring, 'bandwidth_unit', ['k', 'm', 'g'])
 
 
+    START_IP = (REG_IP, 'start_ip')
+    END_IP = (REG_IP, 'end_ip')
+    START_VNC_PORT = (int, 'start_vnc_port')
+    NETMASK = (REG_IP, 'netmask')
+    GATEWAY = (REG_IP, 'gateway')
+    DNS1 = (REG_IP, 'dns1')
+    DNS2 = (REG_IP, 'dns2')
+    ACTIVITY = (int, 'activity')
+
     SSH_KEY_ID_EXT = (REG_NUMBER, 'ssh_key_id')
     SSH_KEY_ID_EXT = (REG_NUMBER, 'ssh_key_id')
     SSH_KEYS_ID = (list, 'ssh_keys_id')
     SSH_KEYS_ID = (list, 'ssh_keys_id')
     PUBLIC_KEY = (basestring, 'public_key')
     PUBLIC_KEY = (basestring, 'public_key')

+ 1 - 0
misc/init.sql

@@ -456,6 +456,7 @@ CREATE TABLE IF NOT EXISTS ip_pool(
     gateway CHAR(15) NOT NULL,
     gateway CHAR(15) NOT NULL,
     dns1 CHAR(15) NOT NULL DEFAULT '223.5.5.5',
     dns1 CHAR(15) NOT NULL DEFAULT '223.5.5.5',
     dns2 CHAR(15) NOT NULL DEFAULT '8.8.8.8',
     dns2 CHAR(15) NOT NULL DEFAULT '8.8.8.8',
+    activity TINYINT UNSIGNED NOT NULL DEFAULT 0,
     name VARCHAR(127) NOT NULL,
     name VARCHAR(127) NOT NULL,
     description TEXT,
     description TEXT,
     create_time BIGINT UNSIGNED NOT NULL,
     create_time BIGINT UNSIGNED NOT NULL,