pvl/config/dhcpd/model.py
changeset 9 2156906bfbf1
parent 7 0f9cae2d7147
equal deleted inserted replaced
8:46d36bc33086 9:2156906bfbf1
       
     1 """
       
     2     Higher-level DHCP config structure model
       
     3 """
       
     4 
       
     5 import dhcp_conf as dhcpc
       
     6 
       
     7 class Config (dhcpc.ConfFile) :
       
     8     """
       
     9         A full configuration file
       
    10     """
       
    11 
       
    12     def __init__ (self, name=dhcpc.ConfFile.DEFAULT_NAME, path=dhcpc.ConfFile.DEFAULT_PATH, 
       
    13             settings=None, options=None, shared_network=False, subnets=None, hosts=None, comment=None
       
    14     ) :
       
    15         """
       
    16             Create a full configuration file for the given settings:
       
    17             
       
    18             settings:       a { name: value } mappping of general settings to set
       
    19             options:        a { opt_name: opt_value } mapping of options to set
       
    20             shared_network: define the subnets as a shared network of the given name
       
    21             subnets:        an iterable of Subnet's to define
       
    22             hosts:          an iterable of Host's to define
       
    23 
       
    24         """
       
    25 
       
    26         dhcpc.ConfFile.__init__(self, name, path, comment=comment)
       
    27 
       
    28         # define global settings
       
    29         if settings :
       
    30             self.add_params(dhcpc.Parameter(setting, value) for setting, value in settings.iteritems())
       
    31         
       
    32         # define global options
       
    33         if options :
       
    34             self.add_params(dhcpc.Option(option, value) for option, value in options.iteritems())
       
    35         
       
    36         # the shared-network section, or a series of subnets
       
    37         if shared_network :
       
    38             self.add_decl(dhcpc.SharedNetwork(shared_network, decls=subnets))
       
    39         
       
    40         elif subnets :
       
    41             self.add_decls(subnets)
       
    42         
       
    43         # hosts section
       
    44         if hosts :
       
    45             self.add_decls(hosts)
       
    46 
       
    47 class Subnet (dhcpc.Subnet) :
       
    48     """
       
    49         A subnet declaration with a router, and optionally a dynamic address pool, and allow/deny unknown clients
       
    50     """
       
    51 
       
    52     def __init__ (self, subnet, router_idx=1, range=None, unknown_clients=None, comment=None) :
       
    53         """
       
    54             @param subnet the addr.IP representing the subnet
       
    55             @param router_idx the subnet[index] of the default gateway
       
    56             @param range optional (from_idx, to_idx) to define a dhcp pool
       
    57             @param unknown_clients optional 'allow'/'deny' to set a policy for unknown clients
       
    58         """
       
    59         
       
    60         # validate
       
    61         if unknown_clients :
       
    62             assert unknown_clients in ('allow', 'deny')
       
    63 
       
    64         super(Subnet, self).__init__(subnet, params=[
       
    65             dhcpc.Option("routers", subnet[router_idx]),
       
    66             dhcpc.Parameter("range", subnet[range[0]], subnet[range[1]]) if range else None,
       
    67             dhcpc.Parameter(unknown_clients, "unknown-clients") if unknown_clients else None,
       
    68         ], comment=comment)
       
    69 
       
    70 
       
    71 class Host (dhcpc.Host) :
       
    72     """
       
    73         A host declaration with a hardware address and a IP address
       
    74     """
       
    75 
       
    76     def __init__ (self, hostname, mac_addr, ip_addr, comment=None) :
       
    77         super(Host, self).__init__(hostname, params=[
       
    78             dhcpc.Parameter("hardware ethernet", mac_addr),
       
    79             dhcpc.Parameter("fixed-address", ip_addr)
       
    80         ], comment=comment)
       
    81