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
10 __ALL__ = ('DhcpHandler', 'Error', 'HostError', 'HostAlreadyExistsError',
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.
24 class HostError(Error, KeyError):
26 HostError(hostname) -> HostError instance
28 This is the base exception for all host related errors.
31 def __init__(self, hostname):
32 r"Initialize the object. See class documentation for more info."
33 self.message = u'Host error: "%s"' % hostname
35 class HostAlreadyExistsError(HostError):
37 HostAlreadyExistsError(hostname) -> HostAlreadyExistsError instance
39 This exception is raised when trying to add a hostname that already exists.
42 def __init__(self, hostname):
43 r"Initialize the object. See class documentation for more info."
44 self.message = u'Host already exists: "%s"' % hostname
46 class HostNotFoundError(HostError):
48 HostNotFoundError(hostname) -> HostNotFoundError instance
50 This exception is raised when trying to operate on a hostname that doesn't
54 def __init__(self, hostname):
55 r"Initialize the object. See class documentation for more info."
56 self.message = u'Host not found: "%s"' % hostname
60 r"""Host(name, ip, mac) -> Host instance :: Class representing a host.
62 name - Host name, should be a fully qualified name, but no checks are done.
63 ip - IP assigned to the hostname.
64 mac - MAC address to associate to the hostname.
67 def __init__(self, name, ip, mac):
68 r"Initialize Host object, see class documentation for details."
74 r"Return a tuple representing the host."
75 return (self.name, self.ip, self.mac)
77 class HostHandler(Handler):
78 r"""HostHandler(hosts) -> HostHandler instance :: Handle a list of hosts.
80 This class is a helper for DhcpHandler to do all the work related to hosts
83 hosts - A dictionary with string keys (hostnames) and Host instances values.
86 handler_help = u"Manage DHCP hosts"
88 def __init__(self, parent):
89 r"Initialize HostHandler object, see class documentation for details."
92 @handler(u'Add a new host')
93 def add(self, name, ip, mac):
94 r"add(name, ip, mac) -> None :: Add a host to the hosts list."
95 if name in self.parent.hosts:
96 raise HostAlreadyExistsError(name)
97 self.parent.hosts[name] = Host(name, ip, mac)
99 @handler(u'Update a host')
100 def update(self, name, ip=None, mac=None):
101 r"update(name[, ip[, mac]]) -> None :: Update a host of the hosts list."
102 if not name in self.parent.hosts:
103 raise HostNotFoundError(name)
105 self.parent.hosts[name].ip = ip
107 self.parent.hosts[name].mac = mac
109 @handler(u'Delete a host')
110 def delete(self, name):
111 r"delete(name) -> None :: Delete a host of the hosts list."
112 if not name in self.parent.hosts:
113 raise HostNotFoundError(name)
114 del self.parent.hosts[name]
116 @handler(u'Get information about a host')
118 r"get(name) -> Host :: List all the information of a host."
119 if not name in self.parent.hosts:
120 raise HostNotFoundError(name)
121 return self.parent.hosts[name]
123 @handler(u'List hosts')
125 r"list() -> tuple :: List all the hostnames."
126 return self.parent.hosts.keys()
128 @handler(u'Get information about all hosts')
130 r"show() -> list of Hosts :: List all the complete hosts information."
131 return self.parent.hosts.values()
134 class DhcpHandler(Restorable, ConfigWriter, InitdHandler, TransactionalHandler,
136 r"""DhcpHandler([pickle_dir[, config_dir]]) -> DhcpHandler instance.
138 Handles DHCP service commands for the dhcpd program.
140 pickle_dir - Directory where to write the persistent configuration data.
142 config_dir - Directory where to store de generated configuration files.
144 Both defaults to the current working directory.
147 handler_help = u"Manage DHCP service"
149 _initd_name = 'dhcpd'
151 _persistent_attrs = ('params', 'hosts')
153 _restorable_defaults = dict(
156 domain_name = 'example.com',
157 dns_1 = 'ns1.example.com',
158 dns_2 = 'ns2.example.com',
159 net_address = '192.168.0.0',
160 net_mask = '255.255.255.0',
161 net_start = '192.168.0.100',
162 net_end = '192.168.0.200',
163 net_gateway = '192.168.0.1',
167 _config_writer_files = 'dhcpd.conf'
168 _config_writer_tpl_dir = path.join(path.dirname(__file__), 'templates')
170 def __init__(self, pickle_dir='.', config_dir='.'):
171 r"Initialize DhcpHandler object, see class documentation for details."
172 self._persistent_dir = pickle_dir
173 self._config_writer_cfg_dir = config_dir
174 self._config_build_templates()
176 self.host = HostHandler(self)
178 def _get_config_vars(self, config_file):
179 return dict(hosts=self.hosts.values(), **self.params)
182 if __name__ == '__main__':
190 print 'Variables:', h.list()
193 print 'Hosts:', h.host.list()
199 h.host.add('my_name','192.168.0.102','00:12:ff:56')
201 h.host.update('my_name','192.168.0.192','00:12:ff:56')
203 h.host.add('nico','192.168.0.188','00:00:00:00')
205 h.set('domain_name','baryon.com.ar')
208 h.set('sarasa','baryon.com.ar')
216 os.system('rm -f *.pkl ' + ' '.join(h._config_writer_files))