Esempio n. 1
0
 def enableDHCPInterface(self, host_id, interface_id):
     """Enables DHCP on the specified interface"""
     session = getSessionE(self._session_id)
     
     n = session.getCountOf("SELECT count(*) FROM dhcp_interface " \
             "WHERE host_id=%s and interface_id=%s", \
             (host_id, interface_id))
     if n != 0:
         raise dhcp_error("DHCP already enabled on interface!")
     
     link = getInterfaceLink(self._session_id, interface_id)
     length = cidrToLength(link["network_address"])
     if length == 24:
         # Try and setup a default pool from 129 -> 253
         pool_max = link._maxIP(True)
         pool_min = link._minIP(True, 128)
     else:
         # Try and setup a default pool using the whole range
         pool_max = link._maxIP(True)
         pool_min = link._minIP(True)
     # Check all IPs in the pool are free
     ip = ipnum(pool_min)
     while ip < ipnum(pool_max):
         # Increment the bottom of the pool if an in Use IP is found
         if link._ipInUse(ip):
             pool_min=formatIP(ip+1)
         # Move through the pool
         ip+=1
     # Check the ppol still has some size
     if ipnum(pool_min) >= ipnum(pool_max):
         raise dhcp_error("Unable to allocate a DHCP pool on the link!")
     
     session.execute("INSERT INTO dhcp_interface (host_id, " \
             "interface_id, pool_min, pool_max) VALUES (%s, %s, %s, %s)", \
             (host_id, interface_id, pool_min, pool_max))
     
     # Raise the event
     triggerHostEvent(self._session_id, "dhcpInterfaceEnabled", \
             service_id=self.service_id, host_id=host_id, \
             interface_id=interface_id)
     
     return True
Esempio n. 2
0
    def updateDHCPInterface(self, host_id, interface_id, propName, propValue):
        """Updates the details of the service.

        Returns SUCCESS.
        """
        session = getSessionE(self._session_id)
        propNull = False
        deferName = ""
        deferValue = ""
        
        ################## Validate incoming data ##################

        res = session.query("SELECT substring(pool_min::text from " \
                "'[.0-9]*') AS pool_min, substring(pool_max::text " \
                "from '[.0-9]*') AS pool_max FROM dhcp_interface WHERE " \
                "host_id=%s and interface_id=%s", (host_id, interface_id))
        if len(res) != 1:
            raise dhcp_error("DHCP not enabled on interface!")
        
        link = getInterfaceLink(self._session_id, interface_id)
        if propName == "pool_min":
            if not link.isValidIP(propValue):
                raise dhcp_error("Invalid minimum pool IP! Not in link.")
            mvalid = link.isValidIP(res[0]["pool_max"])
            if ipnum(res[0]["pool_max"]) <= ipnum(propValue) or not mvalid:
                if mvalid:
                    deferValue = "%s/32" % propValue
                    deferName = "pool_max"
                    propValue = res[0]["pool_max"]
                else:
                    deferValue = "%s/32" % link._maxIP(True)
                    deferName = "pool_max"
            propValue = "%s/32" % propValue
        elif propName == "pool_max":
            if not link.isValidIP(propValue):
                raise dhcp_error("Invalid maximum pool IP! Not in link.")
            mvalid = link.isValidIP(res[0]["pool_min"])
            if ipnum(res[0]["pool_min"]) >= ipnum(propValue) or not mvalid:
                if mvalid:
                    deferValue = "%s/32" % propValue
                    deferName = "pool_min"
                    propValue = res[0]["pool_min"]
                else:
                    deferValue = "%s/32" % link._minIP(True, 128)
                    deferName = "pool_min"
            propValue = "%s/32" % propValue
        elif propName == "router_ip":
            if propValue == "":
                propNull = True
            else:
                if not link.isValidIP(propValue):
                    raise dhcp_error("Invalid router IP! Not in link.")
                propValue = "%s/32" % propValue
        else:
            raise dhcp_error("Invalid property name!")
        
        ################## Update the database ##################
        
        if not propNull:
            sql = "UPDATE dhcp_interface SET %s=%%s WHERE " \
                    "host_id=%%s AND interface_id=%%s" % propName
            session.execute(sql, (propValue, host_id, interface_id))
        else:
            sql = "UPDATE dhcp_interface SET %s=NULL WHERE " \
                    "host_id=%%s AND interface_id=%%s" % propName
            session.execute(sql, (host_id, interface_id))
        if deferName != "":
            sql = "UPDATE dhcp_interface SET %s=%%s WHERE " \
                    "host_id=%%s AND interface_id=%%s" % deferName
            session.execute(sql, (deferValue, host_id, interface_id))

        # Raise the event
        triggerHostEvent(self._session_id, "dhcpInterfaceUpdated", \
                service_id=self.service_id, host_id=host_id, \
                interface_id=interface_id, propName=propName)
            
        ################## Clean Up and Return ##################
        return self.returnSuccess()