1 # vim: set encoding=utf-8 et sw=4 sts=4 :
5 from seqtools import Sequence
6 from dispatcher import Handler, handler, HandlerError
7 from services.util import Restorable, ConfigWriter
8 from services.util import InitdHandler, 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()
137 class DhcpHandler(Restorable, ConfigWriter, InitdHandler, TransactionalHandler,
139 r"""DhcpHandler([pickle_dir[, config_dir]]) -> DhcpHandler instance.
141 Handles DHCP service commands for the dhcpd program.
143 pickle_dir - Directory where to write the persistent configuration data.
145 config_dir - Directory where to store de generated configuration files.
147 Both defaults to the current working directory.
150 _initd_name = 'dhcpd'
152 _persistent_vars = ('params', 'hosts')
154 _restorable_defaults = dict(
157 domain_name = 'example.com',
158 dns_1 = 'ns1.example.com',
159 dns_2 = 'ns2.example.com',
160 net_address = '192.168.0.0',
161 net_mask = '255.255.255.0',
162 net_start = '192.168.0.100',
163 net_end = '192.168.0.200',
164 net_gateway = '192.168.0.1',
168 _config_writer_files = 'dhcpd.conf'
169 _config_writer_tpl_dir = path.join(path.dirname(__file__), 'templates')
171 def __init__(self, pickle_dir='.', config_dir='.'):
172 r"Initialize DhcpHandler object, see class documentation for details."
173 self._persistent_dir = pickle_dir
174 self._config_writer_cfg_dir = config_dir
175 self._config_build_templates()
177 self.host = HostHandler(self.hosts)
179 def _get_config_vars(self, config_file):
180 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))