X-Git-Url: https://git.llucax.com/software/pymin.git/blobdiff_plain/c7a9496a78002544cf62358c7d098614c0d0d837..d6dfd46ed31d985e364b9ea9a404c394f15c2348:/services/dhcp/__init__.py?ds=inline diff --git a/services/dhcp/__init__.py b/services/dhcp/__init__.py index 941bccf..7ffadcf 100644 --- a/services/dhcp/__init__.py +++ b/services/dhcp/__init__.py @@ -1,125 +1,263 @@ # vim: set encoding=utf-8 et sw=4 sts=4 : -try: - import cPickle as pickle -except ImportError: - import pickle +from os import path -from mako.template import Template -from mako.runtime import Context +from seqtools import Sequence +from dispatcher import Handler, handler, HandlerError +from services.util import Restorable, ConfigWriter +from services.util import InitdHandler, TransactionalHandler + +__ALL__ = ('DhcpHandler', 'Error', 'HostError', 'HostAlreadyExistsError', + 'HostNotFoundError', 'ParameterError', 'ParameterNotFoundError') + +class Error(HandlerError): + r""" + Error(message) -> Error instance :: Base DhcpHandler exception class. + + All exceptions raised by the DhcpHandler inherits from this one, so you can + easily catch any DhcpHandler exception. + + message - A descriptive error message. + """ + + def __init__(self, message): + r"Initialize the Error object. See class documentation for more info." + self.message = message + + def __str__(self): + return self.message + +class HostError(Error, KeyError): + r""" + HostError(hostname) -> HostError instance + + This is the base exception for all host related errors. + """ + + def __init__(self, hostname): + r"Initialize the object. See class documentation for more info." + self.message = 'Host error: "%s"' % hostname + +class HostAlreadyExistsError(HostError): + r""" + HostAlreadyExistsError(hostname) -> HostAlreadyExistsError instance + + This exception is raised when trying to add a hostname that already exists. + """ + + def __init__(self, hostname): + r"Initialize the object. See class documentation for more info." + self.message = 'Host already exists: "%s"' % hostname + +class HostNotFoundError(HostError): + r""" + HostNotFoundError(hostname) -> HostNotFoundError instance + + This exception is raised when trying to operate on a hostname that doesn't + exists. + """ + + def __init__(self, hostname): + r"Initialize the object. See class documentation for more info." + self.message = 'Host not found: "%s"' % hostname + +class ParameterError(Error, KeyError): + r""" + ParameterError(paramname) -> ParameterError instance + + This is the base exception for all DhcpHandler parameters related errors. + """ + + def __init__(self, paramname): + r"Initialize the object. See class documentation for more info." + self.message = 'Parameter error: "%s"' % paramname + +class ParameterNotFoundError(ParameterError): + r""" + ParameterNotFoundError(hostname) -> ParameterNotFoundError instance + + This exception is raised when trying to operate on a parameter that doesn't + exists. + """ + + def __init__(self, paramname): + r"Initialize the object. See class documentation for more info." + self.message = 'Parameter not found: "%s"' % paramname + + +class Host(Sequence): + r"""Host(name, ip, mac) -> Host instance :: Class representing a host. + + name - Host name, should be a fully qualified name, but no checks are done. + ip - IP assigned to the hostname. + mac - MAC address to associate to the hostname. + """ -class Host: def __init__(self, name, ip, mac): + r"Initialize Host object, see class documentation for details." self.name = name self.ip = ip self.mac = mac - def __repr__(self): - return 'Host(name="%s", ip="%s", mac="%s")' % ( - self.name, self.ip, self.mac) -class HostHandler: + def as_tuple(self): + r"Return a tuple representing the host." + return (self.name, self.ip, self.mac) + +class HostHandler(Handler): + r"""HostHandler(hosts) -> HostHandler instance :: Handle a list of hosts. + + This class is a helper for DhcpHandler to do all the work related to hosts + administration. + + hosts - A dictionary with string keys (hostnames) and Host instances values. + """ def __init__(self, hosts): + r"Initialize HostHandler object, see class documentation for details." self.hosts = hosts + @handler(u'Add a new host.') def add(self, name, ip, mac): - #deberia indexar por hostname o por ip? o por mac? :) - # Mejor por nada... + r"add(name, ip, mac) -> None :: Add a host to the hosts list." + if name in self.hosts: + raise HostAlreadyExistsError(name) self.hosts[name] = Host(name, ip, mac) + @handler(u'Update a host.') def update(self, name, ip=None, mac=None): + r"update(name[, ip[, mac]]) -> None :: Update a host of the hosts list." + if not name in self.hosts: + raise HostNotFoundError(name) if ip is not None: self.hosts[name].ip = ip if mac is not None: self.hosts[name].mac = mac + @handler(u'Delete a host.') def delete(self, name): + r"delete(name) -> None :: Delete a host of the hosts list." + if not name in self.hosts: + raise HostNotFoundError(name) del self.hosts[name] + @handler(u'Get information about a host.') + def get(self, name): + r"get(name) -> Host :: List all the information of a host." + if not name in self.hosts: + raise HostNotFoundError(name) + return self.hosts[name] + + @handler(u'List hosts.') def list(self): - return ','.join(self.hosts) + r"list() -> tuple :: List all the hostnames." + return self.hosts.keys() + @handler(u'Get information about all hosts.') def show(self): - hosts = self.hosts.values() - return '\n'.join('%s,%s,%s' % (h.name, h.ip, h.mac) for h in hosts) - -class DhcpHandler: - r"""class that handles DHCP service using dhcpd program""" - - def __init__(self): - self.hosts = dict() - self.vars = dict( - domain_name = 'my_domain_name', - dns_1 = 'my_ns1', - dns_2 = 'my_ns2', - net_address = '192.168.0.0', - net_mask = '255.255.255.0', - net_start = '192.168.0.100', - net_end = '192.168.0.200', - net_gateway = '192.168.0.1', - ) + r"show() -> list of Hosts :: List all the complete hosts information." + return self.hosts.values() + +class DhcpHandler(Restorable, ConfigWriter, InitdHandler, TransactionalHandler): + r"""DhcpHandler([pickle_dir[, config_dir]]) -> DhcpHandler instance. + + Handles DHCP service commands for the dhcpd program. + + pickle_dir - Directory where to write the persistent configuration data. + + config_dir - Directory where to store de generated configuration files. + + Both defaults to the current working directory. + """ + + _initd_name = 'dhcpd' + + _persistent_vars = ('vars', 'hosts') + + _restorable_defaults = dict( + hosts = dict(), + vars = dict( + domain_name = 'example.com', + dns_1 = 'ns1.example.com', + dns_2 = 'ns2.example.com', + net_address = '192.168.0.0', + net_mask = '255.255.255.0', + net_start = '192.168.0.100', + net_end = '192.168.0.200', + net_gateway = '192.168.0.1', + ), + ) + + _config_writer_files = 'dhcpd.conf' + _config_writer_tpl_dir = path.join(path.dirname(__file__), 'templates') + + def __init__(self, pickle_dir='.', config_dir='.'): + r"Initialize DhcpHandler object, see class documentation for details." + self._persistent_dir = pickle_dir + self._config_writer_cfg_dir = config_dir + self._config_build_templates() + self._restore() self.host = HostHandler(self.hosts) + def _get_config_vars(self, config_file): + return dict(hosts=self.hosts.values(), **self.vars) + + @handler(u'Set a DHCP parameter.') def set(self, param, value): - if param in self.vars: - self.vars[param] = value - else: - raise KeyError("Parameter " + param + " not found") + r"set(param, value) -> None :: Set a DHCP parameter." + if not param in self.vars: + raise ParameterNotFoundError(param) + self.vars[param] = value + @handler(u'Get a DHCP parameter.') + def get(self, param): + r"get(param) -> None :: Get a DHCP parameter." + if not param in self.vars: + raise ParameterNotFoundError(param) + return self.vars[param] + + @handler(u'List all available DHCP parameters.') def list(self): - return ','.join(self.vars) + r"list() -> tuple :: List all the parameter names." + return self.vars.keys() + @handler(u'Get all DHCP parameters, with their values.') def show(self): - return '\n'.join(('%s,%s' % (k, v) for (k, v) in self.vars.items())) - - def start(self): - #esto seria para poner en una interfaz - #y seria el hook para arrancar el servicio - pass - - def stop(self): - #esto seria para poner en una interfaz - #y seria el hook para arrancar el servicio - pass - - def commit(self): - #esto seria para poner en una interfaz - #y seria que hace el pickle deberia llamarse - #al hacerse un commit - pickle.dump(self.vars, file('pickled/vars.pkl', 'wb'), 2) - pickle.dump(self.hosts, file('pickled/hosts.pkl', 'wb'), 2) - tpl = Template(filename='templates/dhcpd.conf') - ctx = Context(file('generated/dhcpd.conf', 'w'), - hosts=self.hosts.values(), **self.vars) - tpl.render_context(ctx) + r"show() -> (key, value) tuples :: List all the parameters." + return self.vars.items() if __name__ == '__main__': - config = DhcpHandler() + import os + + h = DhcpHandler() + + def dump(): + print '-' * 80 + print 'Variables:', h.list() + print h.show() + print + print 'Hosts:', h.host.list() + print h.host.show() + print '-' * 80 - config.host.add('my_name','192.168.0.102','00:12:ff:56') + dump() - config.host.update('my_name','192.168.0.192','00:12:ff:56') + h.host.add('my_name','192.168.0.102','00:12:ff:56') - config.host.add('nico','192.168.0.188','00:00:00:00') + h.host.update('my_name','192.168.0.192','00:12:ff:56') - config.set('domain_name','baryon.com.ar') + h.host.add('nico','192.168.0.188','00:00:00:00') + + h.set('domain_name','baryon.com.ar') try: - config.set('sarasa','baryon.com.ar') + h.set('sarasa','baryon.com.ar') except KeyError, e: print 'Error:', e - config.commit() - - print 'Variables:', config.list() - print config.show() + h.commit() - print 'Hosts:', config.host.list() - print config.host.show() + dump() - vars = pickle.load(file('pickled/vars.pkl')) - hosts = pickle.load(file('pickled/hosts.pkl')) - print 'Pickled vars:', vars - print 'Pickled hosts:', hosts + os.system('rm -f *.pkl ' + ' '.join(h._config_writer_files))