1 # vim: set encoding=utf-8 et sw=4 sts=4 :
4 import logging ; log = logging.getLogger('pymin.services.dhcp')
6 from pymin.seqtools import Sequence
7 from pymin.dispatcher import Handler, handler, HandlerError
8 from pymin.services.util import Restorable, ConfigWriter, InitdHandler, \
9 TransactionalHandler, ParametersHandler, \
10 DictSubHandler, ReloadHandler
12 __all__ = ('DhcpHandler',)
15 r"""Host(name, ip, mac) -> Host instance :: Class representing a host.
17 name - Host name, should be a fully qualified name, but no checks are done.
18 ip - IP assigned to the hostname.
19 mac - MAC address to associate to the hostname.
22 def __init__(self, name, ip, mac):
23 r"Initialize Host object, see class documentation for details."
29 r"Return a tuple representing the host."
30 return (self.name, self.ip, self.mac)
32 def update(self, ip=None, mac=None):
38 class HostHandler(DictSubHandler):
39 r"""HostHandler(parent) -> HostHandler instance :: Handle a list of hosts.
41 This class is a helper for DhcpHandler to do all the work related to hosts
45 handler_help = u"Manage DHCP hosts"
47 _cont_subhandler_attr = 'hosts'
48 _cont_subhandler_class = Host
50 class DhcpHandler(Restorable, ConfigWriter, ReloadHandler, TransactionalHandler,
51 ParametersHandler, InitdHandler):
52 r"""DhcpHandler([pickle_dir[, config_dir]]) -> DhcpHandler instance.
54 Handles DHCP service commands for the dhcpd program.
56 pickle_dir - Directory where to write the persistent configuration data.
58 config_dir - Directory where to store de generated configuration files.
60 Both defaults to the current working directory.
63 handler_help = u"Manage DHCP service"
67 _persistent_attrs = ('params', 'hosts')
69 _restorable_defaults = dict(
72 domain_name = 'example.com',
73 dns_1 = 'ns1.example.com',
74 dns_2 = 'ns2.example.com',
75 net_address = '192.168.0.0',
76 net_mask = '255.255.255.0',
77 net_start = '192.168.0.100',
78 net_end = '192.168.0.200',
79 net_gateway = '192.168.0.1',
83 _config_writer_files = 'dhcpd.conf'
84 _config_writer_tpl_dir = path.join(path.dirname(__file__), 'templates')
86 def __init__(self, pickle_dir='.', config_dir='.'):
87 r"Initialize DhcpHandler object, see class documentation for details."
88 log.debug(u'DhcpHandler(%r, %r)', pickle_dir, config_dir)
89 self._persistent_dir = pickle_dir
90 self._config_writer_cfg_dir = config_dir
91 self._config_build_templates()
92 InitdHandler.__init__(self)
93 self.host = HostHandler(self)
95 def _get_config_vars(self, config_file):
96 return dict(hosts=self.hosts.values(), **self.params)
99 if __name__ == '__main__':
102 level = logging.DEBUG,
103 format = '%(asctime)s %(levelname)-8s %(message)s',
104 datefmt = '%H:%M:%S',
113 print 'Variables:', h.list()
116 print 'Hosts:', h.host.list()
122 h.host.add('my_name','192.168.0.102','00:12:ff:56')
124 h.host.update('my_name','192.168.0.192','00:12:ff:56')
126 h.host.add('nico','192.168.0.188','00:00:00:00')
128 h.set('domain_name','baryon.com.ar')
131 h.set('sarasa','baryon.com.ar')
139 os.system('rm -f *.pkl ' + ' '.join(h._config_writer_files))