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, \
11 __ALL__ = ('DhcpHandler', 'Error')
13 class Error(HandlerError):
15 Error(message) -> Error instance :: Base DhcpHandler exception class.
17 All exceptions raised by the DhcpHandler inherits from this one, so you can
18 easily catch any DhcpHandler exception.
20 message - A descriptive error message.
25 r"""Host(name, ip, mac) -> Host instance :: Class representing a host.
27 name - Host name, should be a fully qualified name, but no checks are done.
28 ip - IP assigned to the hostname.
29 mac - MAC address to associate to the hostname.
32 def __init__(self, name, ip, mac):
33 r"Initialize Host object, see class documentation for details."
39 r"Return a tuple representing the host."
40 return (self.name, self.ip, self.mac)
42 def update(self, ip=None, mac=None):
48 class HostHandler(DictSubHandler):
49 r"""HostHandler(parent) -> HostHandler instance :: Handle a list of hosts.
51 This class is a helper for DhcpHandler to do all the work related to hosts
55 handler_help = u"Manage DHCP hosts"
57 _cont_subhandler_attr = 'hosts'
58 _cont_subhandler_class = Host
60 class DhcpHandler(Restorable, ConfigWriter, InitdHandler, TransactionalHandler,
62 r"""DhcpHandler([pickle_dir[, config_dir]]) -> DhcpHandler instance.
64 Handles DHCP service commands for the dhcpd program.
66 pickle_dir - Directory where to write the persistent configuration data.
68 config_dir - Directory where to store de generated configuration files.
70 Both defaults to the current working directory.
73 handler_help = u"Manage DHCP service"
77 _persistent_attrs = ('params', 'hosts')
79 _restorable_defaults = dict(
82 domain_name = 'example.com',
83 dns_1 = 'ns1.example.com',
84 dns_2 = 'ns2.example.com',
85 net_address = '192.168.0.0',
86 net_mask = '255.255.255.0',
87 net_start = '192.168.0.100',
88 net_end = '192.168.0.200',
89 net_gateway = '192.168.0.1',
93 _config_writer_files = 'dhcpd.conf'
94 _config_writer_tpl_dir = path.join(path.dirname(__file__), 'templates')
96 def __init__(self, pickle_dir='.', config_dir='.'):
97 r"Initialize DhcpHandler object, see class documentation for details."
98 self._persistent_dir = pickle_dir
99 self._config_writer_cfg_dir = config_dir
100 self._config_build_templates()
102 self.host = HostHandler(self)
104 def _get_config_vars(self, config_file):
105 return dict(hosts=self.hosts.values(), **self.params)
108 if __name__ == '__main__':
116 print 'Variables:', h.list()
119 print 'Hosts:', h.host.list()
125 h.host.add('my_name','192.168.0.102','00:12:ff:56')
127 h.host.update('my_name','192.168.0.192','00:12:ff:56')
129 h.host.add('nico','192.168.0.188','00:00:00:00')
131 h.set('domain_name','baryon.com.ar')
134 h.set('sarasa','baryon.com.ar')
142 os.system('rm -f *.pkl ' + ' '.join(h._config_writer_files))