terom@1: """ terom@1: Higher-level DHCP config structure model terom@1: """ terom@1: terom@1: import dhcp_conf as dhcpc terom@1: terom@1: class Config (dhcpc.ConfFile) : terom@1: """ terom@1: A full configuration file terom@1: """ terom@1: terom@1: def __init__ (self, name=dhcpc.ConfFile.DEFAULT_NAME, path=dhcpc.ConfFile.DEFAULT_PATH, terom@4: settings=None, options=None, shared_network=False, subnets=None, hosts=None, comment=None terom@1: ) : terom@1: """ terom@1: Create a full configuration file for the given settings: terom@1: terom@1: settings: a { name: value } mappping of general settings to set terom@1: options: a { opt_name: opt_value } mapping of options to set terom@1: shared_network: define the subnets as a shared network of the given name terom@1: subnets: an iterable of Subnet's to define terom@1: hosts: an iterable of Host's to define terom@1: terom@1: """ terom@1: terom@4: dhcpc.ConfFile.__init__(self, name, path, comment=comment) terom@1: terom@1: # define global settings terom@1: if settings : terom@1: self.add_params(dhcpc.Parameter(setting, value) for setting, value in settings.iteritems()) terom@1: terom@1: # define global options terom@1: if options : terom@1: self.add_params(dhcpc.Option(option, value) for option, value in options.iteritems()) terom@1: terom@1: # the shared-network section, or a series of subnets terom@1: if shared_network : terom@1: self.add_decl(dhcpc.SharedNetwork(shared_network, decls=subnets)) terom@1: terom@1: elif subnets : terom@1: self.add_decls(subnets) terom@1: terom@1: # hosts section terom@1: if hosts : terom@1: self.add_decls(hosts) terom@1: terom@1: class Subnet (dhcpc.Subnet) : terom@1: """ terom@1: A subnet declaration with a router, and optionally a dynamic address pool, and allow/deny unknown clients terom@1: """ terom@1: terom@4: def __init__ (self, subnet, router_idx=1, range=None, unknown_clients=None, comment=None) : terom@1: """ terom@1: @param subnet the addr.IP representing the subnet terom@1: @param router_idx the subnet[index] of the default gateway terom@1: @param range optional (from_idx, to_idx) to define a dhcp pool terom@1: @param unknown_clients optional 'allow'/'deny' to set a policy for unknown clients terom@1: """ terom@1: terom@1: # validate terom@1: if unknown_clients : terom@1: assert unknown_clients in ('allow', 'deny') terom@1: terom@1: super(Subnet, self).__init__(subnet, params=[ terom@1: dhcpc.Option("routers", subnet[router_idx]), terom@1: dhcpc.Parameter("range", subnet[range[0]], subnet[range[1]]) if range else None, terom@1: dhcpc.Parameter(unknown_clients, "unknown-clients") if unknown_clients else None, terom@4: ], comment=comment) terom@1: terom@1: terom@1: class Host (dhcpc.Host) : terom@1: """ terom@1: A host declaration with a hardware address and a IP address terom@1: """ terom@1: terom@4: def __init__ (self, hostname, mac_addr, ip_addr, comment=None) : terom@1: super(Host, self).__init__(hostname, params=[ terom@1: dhcpc.Parameter("hardware ethernet", mac_addr), terom@1: dhcpc.Parameter("fixed-address", ip_addr) terom@4: ], comment=comment) terom@1: