コード例 #1
0
ファイル: mgmt.py プロジェクト: surajrav/middleware
 def __init__(self, context, ifname, subnet):
     self.context = context
     self.ifname = ifname
     self.subnet = subnet
     self.bridge_if = None
     self.dhcp_server_thread = None
     self.dhcp_server = Server()
     self.allocations = {}
     self.logger = logging.getLogger('ManagementNetwork:{0}'.format(self.ifname))
コード例 #2
0
ファイル: server.py プロジェクト: fnascorral/py-dhcp
def main():
    s = Server()
    s.server_name = 'example server'
    s.on_packet = print_packet
    s.on_request = request
    s.start(sys.argv[1])
    s.serve()
コード例 #3
0
def run():
    """ Run dhcp """
    SETTINGS.parse()

    backend = get_backend(SETTINGS.backend)
    server = Server(backend=backend(),
                    interface=SETTINGS.interface,
                    authoritative=SETTINGS.authoritative,
                    server_ident=SETTINGS.server_ident)

    logger.info("Starting DHCP server")

    while True:
        try:
            server.serve()
        except KeyboardInterrupt:
            break
        except Exception as ex:
            logger.error("Error running DHCP server: %s",
                         str(ex),
                         exc_info=True)

    logger.info("DHCP server stopped")
コード例 #4
0
def setup_dhcp_server():
    global dhcp_server

    def dhcp_request(mac, hostname):
        info('DHCP request from {0} ({1})'.format(hostname, mac))
        lease = Lease()
        lease.client_mac = mac
        lease.client_ip = ipaddress.ip_address(e('${FREENAS_IP}'))
        lease.client_mask = ipaddress.ip_address(e('${NETMASK}'))
        ready.set()
        return lease

    dhcp_server = Server()
    dhcp_server.server_name = 'FreeNAS_test_env'
    dhcp_server.on_request = dhcp_request
    dhcp_server.start(tapdev, ipaddress.ip_address(e('${HOST_IP}')))
    threading.Thread(target=dhcp_server.serve, daemon=True).start()
    info('Started DHCP server on {0}', tapdev)
コード例 #5
0
ファイル: mgmt.py プロジェクト: surajrav/middleware
class ManagementNetwork(object):
    def __init__(self, context, ifname, subnet):
        self.context = context
        self.ifname = ifname
        self.subnet = subnet
        self.bridge_if = None
        self.dhcp_server_thread = None
        self.dhcp_server = Server()
        self.allocations = {}
        self.logger = logging.getLogger('ManagementNetwork:{0}'.format(self.ifname))

    def up(self):
        # Destroy old bridge (if exists)
        try:
            netif.destroy_interface(self.ifname)
        except OSError as err:
            if err.errno != errno.ENXIO:
                raise RuntimeError('Cannot destroy {0}: {1}'.format(self.ifname, str(err)))

        # Setup bridge
        self.bridge_if = netif.get_interface(netif.create_interface('bridge'))
        self.bridge_if.rename(self.ifname)
        self.bridge_if.description = 'containerd management network'
        self.bridge_if.add_address(netif.InterfaceAddress(
            netif.AddressFamily.INET,
            self.subnet
        ))
        self.bridge_if.up()

        self.dhcp_server.server_name = 'FreeNAS'
        self.dhcp_server.on_request = self.dhcp_request
        self.dhcp_server.start(self.ifname, self.subnet.ip)
        self.dhcp_server_thread = gevent.spawn(self.dhcp_worker)

    def down(self):
        self.bridge_if.down()
        netif.destroy_interface(self.ifname)

    def dhcp_worker(self):
        make_nonblocking(self.dhcp_server.bpf.fd)
        self.dhcp_server.serve()

    def allocation_by_ip(self, ip):
        for allocation in self.allocations.values():
            if allocation.lease.client_ip == ip:
                return allocation

        return None

    def pick_ip_address(self):
        for i in self.subnet.network.hosts():
            if i == self.subnet.ip:
                continue

            if not self.allocation_by_ip(i):
                return i

        return None

    def allocate_ip_address(self, mac, hostname):
        allocation = self.allocations.get(mac)
        if not allocation or not allocation.vm():
            self.logger.debug('Trying to match MAC address {0} and hostname {1} to a container'.format(
                mac,
                hostname
            ))

            vm = self.context.vm_by_mgmt_mac(mac)
            if not vm:
                self.logger.warning('Cannot find a match for MAC {0}'.format(mac))
                return None

            allocation = AddressAllocation()
            allocation.vm = weakref.ref(vm)
            allocation.hostname = hostname
            allocation.lease = Lease()
            allocation.lease.client_mac = mac
            allocation.lease.client_ip = self.pick_ip_address()
            allocation.lease.client_mask = self.subnet.netmask
            allocation.lease.router = self.subnet.ip
            allocation.lease.dns_addresses = [ipaddress.ip_address('8.8.8.8'), ipaddress.ip_address('8.8.4.4')]
            self.allocations[mac] = allocation

            vm.changed()

        return allocation

    def dhcp_request(self, mac, hostname):
        self.logger.info('DHCP request from {0} ({1})'.format(mac, hostname))
        allocation = self.allocate_ip_address(mac, hostname)
        if not allocation:
            self.logger.warning('Unknown MAC address {0}'.format(mac))
            return None

        self.logger.info('Allocating IP address {0} to VM {1}'.format(allocation.lease.client_ip, allocation.vm().name))
        return allocation.lease

    def dhcp_release(self, lease):
        pass