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.
23 def __init__(self, message):
24 r"Initialize the Error object. See class documentation for more info."
25 self.message = message
30 class HostError(Error, KeyError):
32 HostError(hostname) -> HostError instance
34 This is the base exception for all host related errors.
37 def __init__(self, hostname):
38 r"Initialize the object. See class documentation for more info."
39 self.message = 'Host error: "%s"' % hostname
41 class HostAlreadyExistsError(HostError):
43 HostAlreadyExistsError(hostname) -> HostAlreadyExistsError instance
45 This exception is raised when trying to add a hostname that already exists.
48 def __init__(self, hostname):
49 r"Initialize the object. See class documentation for more info."
50 self.message = 'Host already exists: "%s"' % hostname
52 class HostNotFoundError(HostError):
54 HostNotFoundError(hostname) -> HostNotFoundError instance
56 This exception is raised when trying to operate on a hostname that doesn't
60 def __init__(self, hostname):
61 r"Initialize the object. See class documentation for more info."
62 self.message = 'Host not found: "%s"' % hostname
66 r"""Host(name, ip, mac) -> Host instance :: Class representing a host.
68 name - Host name, should be a fully qualified name, but no checks are done.
69 ip - IP assigned to the hostname.
70 mac - MAC address to associate to the hostname.
73 def __init__(self, name, ip, mac):
74 r"Initialize Host object, see class documentation for details."
80 r"Return a tuple representing the host."
81 return (self.name, self.ip, self.mac)
83 class HostHandler(Handler):
84 r"""HostHandler(hosts) -> HostHandler instance :: Handle a list of hosts.
86 This class is a helper for DhcpHandler to do all the work related to hosts
89 hosts - A dictionary with string keys (hostnames) and Host instances values.
92 def __init__(self, hosts):
93 r"Initialize HostHandler object, see class documentation for details."
96 @handler(u'Add a new host.')
97 def add(self, name, ip, mac):
98 r"add(name, ip, mac) -> None :: Add a host to the hosts list."
99 if name in self.hosts:
100 raise HostAlreadyExistsError(name)
101 self.hosts[name] = Host(name, ip, mac)
103 @handler(u'Update a host.')
104 def update(self, name, ip=None, mac=None):
105 r"update(name[, ip[, mac]]) -> None :: Update a host of the hosts list."
106 if not name in self.hosts:
107 raise HostNotFoundError(name)
109 self.hosts[name].ip = ip
111 self.hosts[name].mac = mac
113 @handler(u'Delete a host.')
114 def delete(self, name):
115 r"delete(name) -> None :: Delete a host of the hosts list."
116 if not name in self.hosts:
117 raise HostNotFoundError(name)
120 @handler(u'Get information about a host.')
122 r"get(name) -> Host :: List all the information of a host."
123 if not name in self.hosts:
124 raise HostNotFoundError(name)
125 return self.hosts[name]
127 @handler(u'List hosts.')
129 r"list() -> tuple :: List all the hostnames."
130 return self.hosts.keys()
132 @handler(u'Get information about all hosts.')
134 r"show() -> list of Hosts :: List all the complete hosts information."
135 return self.hosts.values()
138 class DhcpHandler(Restorable, ConfigWriter, InitdHandler, TransactionalHandler,
140 r"""DhcpHandler([pickle_dir[, config_dir]]) -> DhcpHandler instance.
142 Handles DHCP service commands for the dhcpd program.
144 pickle_dir - Directory where to write the persistent configuration data.
146 config_dir - Directory where to store de generated configuration files.
148 Both defaults to the current working directory.
151 _initd_name = 'dhcpd'
153 _persistent_attrs = ('params', 'hosts')
155 _restorable_defaults = dict(
158 domain_name = 'example.com',
159 dns_1 = 'ns1.example.com',
160 dns_2 = 'ns2.example.com',
161 net_address = '192.168.0.0',
162 net_mask = '255.255.255.0',
163 net_start = '192.168.0.100',
164 net_end = '192.168.0.200',
165 net_gateway = '192.168.0.1',
169 _config_writer_files = 'dhcpd.conf'
170 _config_writer_tpl_dir = path.join(path.dirname(__file__), 'templates')
172 def __init__(self, pickle_dir='.', config_dir='.'):
173 r"Initialize DhcpHandler object, see class documentation for details."
174 self._persistent_dir = pickle_dir
175 self._config_writer_cfg_dir = config_dir
176 self._config_build_templates()
178 self.host = HostHandler(self.hosts)
180 def _get_config_vars(self, config_file):
181 return dict(hosts=self.hosts.values(), **self.params)
184 if __name__ == '__main__':
192 print 'Variables:', h.list()
195 print 'Hosts:', h.host.list()
201 h.host.add('my_name','192.168.0.102','00:12:ff:56')
203 h.host.update('my_name','192.168.0.192','00:12:ff:56')
205 h.host.add('nico','192.168.0.188','00:00:00:00')
207 h.set('domain_name','baryon.com.ar')
210 h.set('sarasa','baryon.com.ar')
218 os.system('rm -f *.pkl ' + ' '.join(h._config_writer_files))