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', 'get_service')
15 def get_service(config):
16 return DhcpHandler(config.dhcp.pickle_dir, config.dhcp.config_dir)
20 r"""Host(name, ip, mac) -> Host instance :: Class representing a host.
22 name - Host name, should be a fully qualified name, but no checks are done.
23 ip - IP assigned to the hostname.
24 mac - MAC address to associate to the hostname.
27 def __init__(self, name, ip, mac):
28 r"Initialize Host object, see class documentation for details."
34 r"Return a tuple representing the host."
35 return (self.name, self.ip, self.mac)
37 def update(self, ip=None, mac=None):
43 class HostHandler(DictSubHandler):
44 r"""HostHandler(parent) -> HostHandler instance :: Handle a list of hosts.
46 This class is a helper for DhcpHandler to do all the work related to hosts
50 handler_help = u"Manage DHCP hosts"
52 _cont_subhandler_attr = 'hosts'
53 _cont_subhandler_class = Host
55 class DhcpHandler(Restorable, ConfigWriter, ReloadHandler, TransactionalHandler,
56 ParametersHandler, InitdHandler):
57 r"""DhcpHandler([pickle_dir[, config_dir]]) -> DhcpHandler instance.
59 Handles DHCP service commands for the dhcpd program.
61 pickle_dir - Directory where to write the persistent configuration data.
63 config_dir - Directory where to store de generated configuration files.
65 Both defaults to the current working directory.
68 handler_help = u"Manage DHCP service"
72 _persistent_attrs = ('params', 'hosts')
74 _restorable_defaults = dict(
77 domain_name = 'example.com',
78 dns_1 = 'ns1.example.com',
79 dns_2 = 'ns2.example.com',
80 net_address = '192.168.0.0',
81 net_mask = '255.255.255.0',
82 net_start = '192.168.0.100',
83 net_end = '192.168.0.200',
84 net_gateway = '192.168.0.1',
88 _config_writer_files = 'dhcpd.conf'
89 _config_writer_tpl_dir = path.join(path.dirname(__file__), 'templates')
91 def __init__(self, pickle_dir='.', config_dir='.'):
92 r"Initialize DhcpHandler object, see class documentation for details."
93 log.debug(u'DhcpHandler(%r, %r)', pickle_dir, config_dir)
94 self._persistent_dir = pickle_dir
95 self._config_writer_cfg_dir = config_dir
96 self._config_build_templates()
97 InitdHandler.__init__(self)
98 self.host = HostHandler(self)
100 def _get_config_vars(self, config_file):
101 return dict(hosts=self.hosts.values(), **self.params)
104 if __name__ == '__main__':
107 level = logging.DEBUG,
108 format = '%(asctime)s %(levelname)-8s %(message)s',
109 datefmt = '%H:%M:%S',
118 print 'Variables:', h.list()
121 print 'Hosts:', h.host.list()
127 h.host.add('my_name','192.168.0.102','00:12:ff:56')
129 h.host.update('my_name','192.168.0.192','00:12:ff:56')
131 h.host.add('nico','192.168.0.188','00:00:00:00')
133 h.set('domain_name','baryon.com.ar')
136 h.set('sarasa','baryon.com.ar')
144 os.system('rm -f *.pkl ' + ' '.join(h._config_writer_files))