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.service.util import Restorable, ConfigWriter, InitdHandler, \
9 TransactionalHandler, ParametersHandler, \
10 DictSubHandler, ReloadHandler
12 __all__ = ('DhcpHandler')
16 r"""Host(name, ip, mac) -> Host instance :: Class representing a host.
18 name - Host name, should be a fully qualified name, but no checks are done.
19 ip - IP assigned to the hostname.
20 mac - MAC address to associate to the hostname.
23 def __init__(self, name, ip, mac):
24 r"Initialize Host object, see class documentation for details."
30 r"Return a tuple representing the host."
31 return (self.name, self.ip, self.mac)
33 def update(self, ip=None, mac=None):
39 class HostHandler(DictSubHandler):
40 r"""HostHandler(parent) -> HostHandler instance :: Handle a list of hosts.
42 This class is a helper for DhcpHandler to do all the work related to hosts
46 handler_help = u"Manage DHCP hosts"
48 _cont_subhandler_attr = 'hosts'
49 _cont_subhandler_class = Host
51 class DhcpHandler(Restorable, ConfigWriter, ReloadHandler, TransactionalHandler,
52 ParametersHandler, InitdHandler):
53 r"""DhcpHandler([pickle_dir[, config_dir]]) -> DhcpHandler instance.
55 Handles DHCP service commands for the dhcpd program.
57 pickle_dir - Directory where to write the persistent configuration data.
59 config_dir - Directory where to store de generated configuration files.
61 Both defaults to the current working directory.
64 handler_help = u"Manage DHCP service"
68 _persistent_attrs = ('params', 'hosts')
70 _restorable_defaults = dict(
73 domain_name = 'example.com',
74 dns_1 = 'ns1.example.com',
75 dns_2 = 'ns2.example.com',
76 net_address = '192.168.0.0',
77 net_mask = '255.255.255.0',
78 net_start = '192.168.0.100',
79 net_end = '192.168.0.200',
80 net_gateway = '192.168.0.1',
84 _config_writer_files = 'dhcpd.conf'
85 _config_writer_tpl_dir = path.join(path.dirname(__file__), 'templates')
87 def __init__(self, pickle_dir='.', config_dir='.'):
88 r"Initialize DhcpHandler object, see class documentation for details."
89 log.debug(u'DhcpHandler(%r, %r)', pickle_dir, config_dir)
90 self._persistent_dir = pickle_dir
91 self._config_writer_cfg_dir = config_dir
92 self._config_build_templates()
93 InitdHandler.__init__(self)
94 self.host = HostHandler(self)
96 def _get_config_vars(self, config_file):
97 return dict(hosts=self.hosts.values(), **self.params)
100 if __name__ == '__main__':
103 level = logging.DEBUG,
104 format = '%(asctime)s %(levelname)-8s %(message)s',
105 datefmt = '%H:%M:%S',
114 print 'Variables:', h.list()
117 print 'Hosts:', h.host.list()
123 h.host.add('my_name','192.168.0.102','00:12:ff:56')
125 h.host.update('my_name','192.168.0.192','00:12:ff:56')
127 h.host.add('nico','192.168.0.188','00:00:00:00')
129 h.set('domain_name','baryon.com.ar')
132 h.set('sarasa','baryon.com.ar')
140 os.system('rm -f *.pkl ' + ' '.join(h._config_writer_files))