1 # vim: set encoding=utf-8 et sw=4 sts=4 :
5 from pymin.seqtools import Sequence
6 from pymin.dispatcher import Handler, handler, HandlerError
7 from pymin.services.util import Restorable, ConfigWriter, InitdHandler, \
8 TransactionalHandler, ParametersHandler, \
9 DictSubHandler, ReloadHandler
11 __ALL__ = ('DhcpHandler',)
14 r"""Host(name, ip, mac) -> Host instance :: Class representing a host.
16 name - Host name, should be a fully qualified name, but no checks are done.
17 ip - IP assigned to the hostname.
18 mac - MAC address to associate to the hostname.
21 def __init__(self, name, ip, mac):
22 r"Initialize Host object, see class documentation for details."
28 r"Return a tuple representing the host."
29 return (self.name, self.ip, self.mac)
31 def update(self, ip=None, mac=None):
37 class HostHandler(DictSubHandler):
38 r"""HostHandler(parent) -> HostHandler instance :: Handle a list of hosts.
40 This class is a helper for DhcpHandler to do all the work related to hosts
44 handler_help = u"Manage DHCP hosts"
46 _cont_subhandler_attr = 'hosts'
47 _cont_subhandler_class = Host
49 class DhcpHandler(Restorable, ConfigWriter, ReloadHandler, TransactionalHandler,
50 ParametersHandler, InitdHandler):
51 r"""DhcpHandler([pickle_dir[, config_dir]]) -> DhcpHandler instance.
53 Handles DHCP service commands for the dhcpd program.
55 pickle_dir - Directory where to write the persistent configuration data.
57 config_dir - Directory where to store de generated configuration files.
59 Both defaults to the current working directory.
62 handler_help = u"Manage DHCP service"
66 _persistent_attrs = ('params', 'hosts')
68 _restorable_defaults = dict(
71 domain_name = 'example.com',
72 dns_1 = 'ns1.example.com',
73 dns_2 = 'ns2.example.com',
74 net_address = '192.168.0.0',
75 net_mask = '255.255.255.0',
76 net_start = '192.168.0.100',
77 net_end = '192.168.0.200',
78 net_gateway = '192.168.0.1',
82 _config_writer_files = 'dhcpd.conf'
83 _config_writer_tpl_dir = path.join(path.dirname(__file__), 'templates')
85 def __init__(self, pickle_dir='.', config_dir='.'):
86 r"Initialize DhcpHandler object, see class documentation for details."
87 self._persistent_dir = pickle_dir
88 self._config_writer_cfg_dir = config_dir
89 self._config_build_templates()
91 self.host = HostHandler(self)
93 def _get_config_vars(self, config_file):
94 return dict(hosts=self.hosts.values(), **self.params)
97 if __name__ == '__main__':
105 print 'Variables:', h.list()
108 print 'Hosts:', h.host.list()
114 h.host.add('my_name','192.168.0.102','00:12:ff:56')
116 h.host.update('my_name','192.168.0.192','00:12:ff:56')
118 h.host.add('nico','192.168.0.188','00:00:00:00')
120 h.set('domain_name','baryon.com.ar')
123 h.set('sarasa','baryon.com.ar')
131 os.system('rm -f *.pkl ' + ' '.join(h._config_writer_files))