1 # vim: set encoding=utf-8 et sw=4 sts=4 :
4 import logging ; log = logging.getLogger('pymin.services.dhcp')
6 from pymin.service.util import Restorable, ConfigWriter, ReloadHandler, \
7 TransactionalHandler, ParametersHandler, \
10 from host import HostHandler
12 __all__ = ('DhcpHandler',)
14 class DhcpHandler(Restorable, ConfigWriter, ReloadHandler, TransactionalHandler,
15 ParametersHandler, InitdHandler):
16 r"""DhcpHandler([pickle_dir[, config_dir]]) -> DhcpHandler instance.
18 Handles DHCP service commands for the dhcpd program.
20 pickle_dir - Directory where to write the persistent configuration data.
22 config_dir - Directory where to store de generated configuration files.
24 Both defaults to the current working directory.
27 handler_help = u"Manage DHCP service"
31 _persistent_attrs = ('params', 'hosts')
33 _restorable_defaults = dict(
36 domain_name = 'example.com',
37 dns_1 = 'ns1.example.com',
38 dns_2 = 'ns2.example.com',
39 net_address = '192.168.0.0',
40 net_mask = '255.255.255.0',
41 net_start = '192.168.0.100',
42 net_end = '192.168.0.200',
43 net_gateway = '192.168.0.1',
47 _config_writer_files = 'dhcpd.conf'
48 _config_writer_tpl_dir = path.join(path.dirname(__file__), 'templates')
50 def __init__(self, pickle_dir='.', config_dir='.'):
51 r"Initialize DhcpHandler object, see class documentation for details."
52 log.debug(u'DhcpHandler(%r, %r)', pickle_dir, config_dir)
53 self._persistent_dir = pickle_dir
54 self._config_writer_cfg_dir = config_dir
55 self._config_build_templates()
56 InitdHandler.__init__(self)
57 self.host = HostHandler(self)
59 def _get_config_vars(self, config_file):
60 return dict(hosts=self.hosts.values(), **self.params)
63 if __name__ == '__main__':
66 level = logging.DEBUG,
67 format = '%(asctime)s %(levelname)-8s %(message)s',
77 print 'Variables:', h.list()
80 print 'Hosts:', h.host.list()
86 h.host.add('my_name','192.168.0.102','00:12:ff:56')
88 h.host.update('my_name','192.168.0.192','00:12:ff:56')
90 h.host.add('nico','192.168.0.188','00:00:00:00')
92 h.set('domain_name','baryon.com.ar')
95 h.set('sarasa','baryon.com.ar')
103 os.system('rm -f *.pkl ' + ' '.join(h._config_writer_files))