dhcp.py
author Tero Marttila <terom@fixme.fi>
Thu, 02 Apr 2009 21:11:01 +0300
changeset 3 ff98fa9b84ce
parent 1 2223ade4f259
child 4 8b633782f02d
permissions -rw-r--r--
implement comments properly
"""
    Higher-level DHCP config structure model
"""

import dhcp_conf as dhcpc

class Config (dhcpc.ConfFile) :
    """
        A full configuration file
    """

    def __init__ (self, name=dhcpc.ConfFile.DEFAULT_NAME, path=dhcpc.ConfFile.DEFAULT_PATH, 
            settings=None, options=None, shared_network=False, subnets=None, hosts=None
    ) :
        """
            Create a full configuration file for the given settings:
            
            settings:       a { name: value } mappping of general settings to set
            options:        a { opt_name: opt_value } mapping of options to set
            shared_network: define the subnets as a shared network of the given name
            subnets:        an iterable of Subnet's to define
            hosts:          an iterable of Host's to define

        """

        dhcpc.ConfFile.__init__(self, name, path)

        # define global settings
        if settings :
            self.add_params(dhcpc.Parameter(setting, value) for setting, value in settings.iteritems())
        
        # define global options
        if options :
            self.add_params(dhcpc.Option(option, value) for option, value in options.iteritems())
        
        # the shared-network section, or a series of subnets
        if shared_network :
            self.add_decl(dhcpc.SharedNetwork(shared_network, decls=subnets))
        
        elif subnets :
            self.add_decls(subnets)
        
        # hosts section
        if hosts :
            self.add_decls(hosts)

class Subnet (dhcpc.Subnet) :
    """
        A subnet declaration with a router, and optionally a dynamic address pool, and allow/deny unknown clients
    """

    def __init__ (self, subnet, router_idx=1, range=None, unknown_clients=None) :
        """
            @param subnet the addr.IP representing the subnet
            @param router_idx the subnet[index] of the default gateway
            @param range optional (from_idx, to_idx) to define a dhcp pool
            @param unknown_clients optional 'allow'/'deny' to set a policy for unknown clients
        """
        
        # validate
        if unknown_clients :
            assert unknown_clients in ('allow', 'deny')

        super(Subnet, self).__init__(subnet, params=[
            dhcpc.Option("routers", subnet[router_idx]),
            dhcpc.Parameter("range", subnet[range[0]], subnet[range[1]]) if range else None,
            dhcpc.Parameter(unknown_clients, "unknown-clients") if unknown_clients else None,
        ])


class Host (dhcpc.Host) :
    """
        A host declaration with a hardware address and a IP address
    """

    def __init__ (self, hostname, mac_addr, ip_addr) :
        super(Host, self).__init__(hostname, params=[
            dhcpc.Parameter("hardware ethernet", mac_addr),
            dhcpc.Parameter("fixed-address", ip_addr)
        ])