]> git.llucax.com Git - software/pymin.git/blob - services/dhcp/handler.py
Add validation to vpn service (refs #20)
[software/pymin.git] / services / dhcp / handler.py
1 # vim: set encoding=utf-8 et sw=4 sts=4 :
2
3 from os import path
4 import logging ; log = logging.getLogger('pymin.services.dhcp')
5
6 from pymin.service.util import Restorable, ConfigWriter, ReloadHandler, \
7                                TransactionalHandler, ParametersHandler, \
8                                InitdHandler
9
10 from host import HostHandler
11
12 __all__ = ('DhcpHandler',)
13
14 class DhcpHandler(Restorable, ConfigWriter, ReloadHandler, TransactionalHandler,
15                   ParametersHandler, InitdHandler):
16     r"""DhcpHandler([pickle_dir[, config_dir]]) -> DhcpHandler instance.
17
18     Handles DHCP service commands for the dhcpd program.
19
20     pickle_dir - Directory where to write the persistent configuration data.
21
22     config_dir - Directory where to store de generated configuration files.
23
24     Both defaults to the current working directory.
25     """
26
27     handler_help = u"Manage DHCP service"
28
29     _initd_name = 'dhcpd'
30
31     _persistent_attrs = ('params', 'hosts')
32
33     _restorable_defaults = dict(
34             hosts = dict(),
35             params  = 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',
44             ),
45     )
46
47     _config_writer_files = 'dhcpd.conf'
48     _config_writer_tpl_dir = path.join(path.dirname(__file__), 'templates')
49
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)
58
59     def _get_config_vars(self, config_file):
60         return dict(hosts=self.hosts.values(), **self.params)
61
62
63 if __name__ == '__main__':
64
65     logging.basicConfig(
66         level   = logging.DEBUG,
67         format  = '%(asctime)s %(levelname)-8s %(message)s',
68         datefmt = '%H:%M:%S',
69     )
70
71     import os
72
73     h = DhcpHandler()
74
75     def dump():
76         print '-' * 80
77         print 'Variables:', h.list()
78         print h.show()
79         print
80         print 'Hosts:', h.host.list()
81         print h.host.show()
82         print '-' * 80
83
84     dump()
85
86     h.host.add('my_name','192.168.0.102','00:12:ff:56')
87
88     h.host.update('my_name','192.168.0.192','00:12:ff:56')
89
90     h.host.add('nico','192.168.0.188','00:00:00:00')
91
92     h.set('domain_name','baryon.com.ar')
93
94     try:
95         h.set('sarasa','baryon.com.ar')
96     except KeyError, e:
97         print 'Error:', e
98
99     h.commit()
100
101     dump()
102
103     os.system('rm -f *.pkl ' + ' '.join(h._config_writer_files))
104