invirt-configurize sipb-xen-dhcp
[invirt/packages/invirt-dhcp.git] / code / dhcpserver.py
index b8c5a9c..634cad6 100755 (executable)
@@ -16,30 +16,28 @@ from event_logger import Log
 
 import psycopg2
 import time
-import sipb_xen_database
-from sqlalchemy import create_engine
+from invirt import database
+from invirt.config import structs as config
 
-dhcp_options = {'subnet_mask': '255.255.0.0',
-                'router': '18.181.0.1',
-                'domain_name_server': '18.70.0.160,18.71.0.151,18.72.0.3',
+dhcp_options = {'subnet_mask': config.dhcp.netmask,
+                'router': config.dhcp.gateway,
+                'domain_name_server': ','.join(config.dhcp.dns),
                 'ip_address_lease_time': 60*60*24}
 
 class DhcpBackend:
-    def __init__(self, database=None):
-        if database is not None:
-            self.database = database
-            sipb_xen_database.connect(create_engine(database))
+    def __init__(self):
+        database.connect()
     def findNIC(self, mac):
-        sipb_xen_database.clear_cache()
+        database.clear_cache()
         for i in range(3):
             try:
-                value = sipb_xen_database.NIC.get_by(mac_addr=mac)
+                value = database.NIC.query().filter_by(mac_addr=mac).one()
             except psycopg2.OperationalError:
                 time.sleep(0.5)
                 if i == 2:  #Try twice to reconnect.
                     raise
                 #Sigh.  SQLAlchemy should do this itself.
-                sipb_xen_database.connect(create_engine(self.database))
+                database.connect()
             else:
                 break
         return value
@@ -47,14 +45,14 @@ class DhcpBackend:
         chaddr = hwmac(packet.GetHardwareAddress())
         nic = self.findNIC(str(chaddr))
         if nic is None or nic.ip is None:
-            return ("18.181.0.60", None)
+            return
         ipstr = ''.join(reversed(['%02X' % i for i in ipv4(nic.ip).list()]))
         for line in open('/proc/net/route'):
             parts = line.split()
             if parts[1] == ipstr:
                 Log.Output(Log.debug, "find_interface found "+str(nic.ip)+" on "+parts[0])
-                return ("18.181.0.60", parts[0])
-        return ("18.181.0.60", None)
+                return parts[0]
+        return
                             
     def getParameters(self, **extra):
         all_options=dict(dhcp_options)
@@ -123,13 +121,13 @@ class DhcpBackend:
             options['host_name'], options['domain_name'] = nic.hostname.split('.', 1)
         elif nic.machine.name:
             options['host_name'] = nic.machine.name
-            options['domain_name'] = 'xvm.mit.edu'
+            options['domain_name'] = config.dns.domains[0]
         else:
             hostname = None
         if DhcpOptions['domain_search'] in packet.GetOption('parameter_request_list'):
             options['host_name'] += '.' + options['domain_name']
             del options['domain_name']
-            options['domain_search'] = ['mit.edu']
+            options['domain_search'] = [config.dhcp.search_domain]
         if ip is not None:
             ip = ipv4(ip)
             Log.Output(Log.debug,"dhcp_backend : Discover result = "+str(ip))
@@ -177,7 +175,7 @@ class DhcpServer(pydhcplib.dhcp_network.DhcpServer):
         Log.Output(Log.debug, "__init__ DhcpServer")
 
     def SendDhcpPacketTo(self, To, packet):
-        (ip, intf) = self.backend.find_interface(packet)
+        intf = self.backend.find_interface(packet)
         if intf:
             out_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
             out_socket.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST,1)
@@ -274,7 +272,7 @@ if '__main__' == __name__:
     options = { "server_listen_port":67,
                 "client_listen_port":68,
                 "listen_address":"0.0.0.0"}
-    backend = DhcpBackend('postgres://sipb-xen@sipb-xen-dev/sipb_xen')
+    backend = DhcpBackend()
     server = DhcpServer(backend, options)
 
     while True : server.GetNextDhcpPacket()