X-Git-Url: https://git.llucax.com/software/pymin.git/blobdiff_plain/296d853cc95fd5bef262248cfe21b507abd26a4f..066b7ef8b45149aec09d41ca74b08d0b8711cb13:/pymin/services/dns/__init__.py diff --git a/pymin/services/dns/__init__.py b/pymin/services/dns/__init__.py index 624dfc4..32e1772 100644 --- a/pymin/services/dns/__init__.py +++ b/pymin/services/dns/__init__.py @@ -8,7 +8,8 @@ from new import instancemethod from pymin.seqtools import Sequence from pymin.dispatcher import handler, HandlerError, Handler from pymin.services.util import Restorable, ConfigWriter, InitdHandler, \ - TransactionalHandler, ParametersHandler, call + TransactionalHandler, ParametersHandler, \ + SubHandler, call __ALL__ = ('DnsHandler', 'Error', 'ZoneError', 'ZoneNotFoundError', 'ZoneAlreadyExistsError', @@ -29,13 +30,7 @@ class Error(HandlerError): 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 + pass class ZoneError(Error, KeyError): r""" @@ -46,7 +41,7 @@ class ZoneError(Error, KeyError): def __init__(self, zonename): r"Initialize the object. See class documentation for more info." - self.message = 'Zone error: "%s"' % zonename + self.message = u'Zone error: "%s"' % zonename class ZoneNotFoundError(ZoneError): r""" @@ -58,7 +53,7 @@ class ZoneNotFoundError(ZoneError): def __init__(self, zonename): r"Initialize the object. See class documentation for more info." - self.message = 'zone not found: "%s"' % zonename + self.message = u'zone not found: "%s"' % zonename class ZoneAlreadyExistsError(ZoneError): r""" @@ -69,7 +64,7 @@ class ZoneAlreadyExistsError(ZoneError): def __init__(self, zonename): r"Initialize the object. See class documentation for more info." - self.message = 'Zone already exists: "%s"' % zonename + self.message = u'Zone already exists: "%s"' % zonename class HostError(Error, KeyError): r""" @@ -80,7 +75,7 @@ class HostError(Error, KeyError): def __init__(self, hostname): r"Initialize the object. See class documentation for more info." - self.message = 'Host error: "%s"' % hostname + self.message = u'Host error: "%s"' % hostname class HostAlreadyExistsError(HostError): r""" @@ -91,11 +86,11 @@ class HostAlreadyExistsError(HostError): def __init__(self, hostname): r"Initialize the object. See class documentation for more info." - self.message = 'Host already exists: "%s"' % hostname + self.message = u'Host already exists: "%s"' % hostname class HostNotFoundError(HostError): r""" - HostNotFoundError(hostname) -> HostNotFoundError instance + HostNotFoundError(hostname) -> HostNotFoundError instance. This exception is raised when trying to operate on a hostname that doesn't exists. @@ -103,36 +98,36 @@ class HostNotFoundError(HostError): def __init__(self, hostname): r"Initialize the object. See class documentation for more info." - self.message = 'Host not found: "%s"' % hostname + self.message = u'Host not found: "%s"' % hostname class MailExchangeError(Error, KeyError): r""" - MailExchangeError(hostname) -> MailExchangeError instance + MailExchangeError(hostname) -> MailExchangeError instance. This is the base exception for all mail exchange related errors. """ def __init__(self, mx): r"Initialize the object. See class documentation for more info." - self.message = 'Mail Exchange error: "%s"' % mx + self.message = u'Mail Exchange error: "%s"' % mx class MailExchangeAlreadyExistsError(MailExchangeError): r""" - MailExchangeAlreadyExistsError(hostname) -> MailExchangeAlreadyExistsError instance + MailExchangeAlreadyExistsError(hostname) -> MailExchangeAlreadyExistsError. This exception is raised when trying to add a mail exchange that already exists. """ def __init__(self, mx): r"Initialize the object. See class documentation for more info." - self.message = 'Mail Exchange already exists: "%s"' % mx + self.message = u'Mail Exchange already exists: "%s"' % mx class MailExchangeNotFoundError(MailExchangeError): r""" - MailExchangeNotFoundError(hostname) -> MailExchangeNotFoundError instance + MailExchangeNotFoundError(hostname) -> MailExchangeNotFoundError instance. - This exception is raised when trying to operate on a mail exchange that doesn't - exists. + This exception is raised when trying to operate on a mail exchange that + doesn't exists. """ def __init__(self, mx): @@ -152,9 +147,10 @@ class NameServerError(Error, KeyError): class NameServerAlreadyExistsError(NameServerError): r""" - NameServerAlreadyExistsError(hostname) -> NameServerAlreadyExistsError instance + NameServerAlreadyExistsError(hostname) -> NameServerAlreadyExistsError. - This exception is raised when trying to add a name server that already exists. + This exception is raised when trying to add a name server that already + exists. """ def __init__(self, ns): @@ -163,10 +159,10 @@ class NameServerAlreadyExistsError(NameServerError): class NameServerNotFoundError(NameServerError): r""" - NameServerNotFoundError(hostname) -> NameServerNotFoundError instance + NameServerNotFoundError(hostname) -> NameServerNotFoundError instance. - This exception is raised when trying to operate on a name server that doesn't - exists. + This exception is raised when trying to operate on a name server that + doesn't exists. """ def __init__(self, ns): @@ -182,44 +178,44 @@ class Host(Sequence): def as_tuple(self): return (self.name, self.ip) -class HostHandler(Handler): - def __init__(self,zones): - self.zones = zones +class HostHandler(SubHandler): + + handler_help = u"Manage DNS hosts" @handler(u'Adds a host to a zone') def add(self, name, hostname, ip): - if not name in self.zones: + if not name in self.parent.zones: raise ZoneNotFoundError(name) - if hostname in self.zones[name].hosts: + if hostname in self.parent.zones[name].hosts: raise HostAlreadyExistsError(hostname) - self.zones[name].hosts[hostname] = Host(hostname, ip) - self.zones[name].mod = True + self.parent.zones[name].hosts[hostname] = Host(hostname, ip) + self.parent.zones[name].mod = True @handler(u'Updates a host ip in a zone') def update(self, name, hostname, ip): - if not name in self.zones: + if not name in self.parent.zones: raise ZoneNotFoundError(name) - if not hostname in self.zones[name].hosts: + if not hostname in self.parent.zones[name].hosts: raise HostNotFoundError(name) - self.zones[name].hosts[hostname].ip = ip - self.zones[name].mod = True + self.parent.zones[name].hosts[hostname].ip = ip + self.parent.zones[name].mod = True @handler(u'Deletes a host from a zone') def delete(self, name, hostname): - if not name in self.zones: + if not name in self.parent.zones: raise ZoneNotFoundError(name) - if not hostname in self.zones[name].hosts: + if not hostname in self.parent.zones[name].hosts: raise HostNotFoundError(name) - del self.zones[name].hosts[hostname] - self.zones[name].mod = True + del self.parent.zones[name].hosts[hostname] + self.parent.zones[name].mod = True @handler(u'Lists hosts') def list(self): - return self.zones.keys() + return self.parent.zones.keys() @handler(u'Get insormation about all hosts') def show(self): - return self.zones.values() + return self.parent.zones.values() class MailExchange(Sequence): @@ -231,45 +227,44 @@ class MailExchange(Sequence): def as_tuple(self): return (self.mx, self.prio) -class MailExchangeHandler(Handler): +class MailExchangeHandler(SubHandler): - def __init__(self, zones): - self.zones = zones + handler_help = u"Manage DNS mail exchangers (MX)" @handler(u'Adds a mail exchange to a zone') def add(self, zonename, mx, prio): - if not zonename in self.zones: + if not zonename in self.parent.zones: raise ZoneNotFoundError(zonename) - if mx in self.zones[zonename].mxs: + if mx in self.parent.zones[zonename].mxs: raise MailExchangeAlreadyExistsError(mx) - self.zones[zonename].mxs[mx] = MailExchange(mx, prio) - self.zones[zonename].mod = True + self.parent.zones[zonename].mxs[mx] = MailExchange(mx, prio) + self.parent.zones[zonename].mod = True @handler(u'Updates a mail exchange priority') def update(self, zonename, mx, prio): - if not zonename in self.zones: + if not zonename in self.parent.zones: raise ZoneNotFoundError(zonename) - if not mx in self.zones[zonename].mxs: + if not mx in self.parent.zones[zonename].mxs: raise MailExchangeNotFoundError(mx) - self.zones[zonename].mxs[mx].prio = prio - self.zones[zonename].mod = True + self.parent.zones[zonename].mxs[mx].prio = prio + self.parent.zones[zonename].mod = True @handler(u'Deletes a mail exchange from a zone') def delete(self, zonename, mx): - if not zonename in self.zones: + if not zonename in self.parent.zones: raise ZoneNotFoundError(zonename) - if not mx in self.zones[zonename].mxs: + if not mx in self.parent.zones[zonename].mxs: raise MailExchangeNotFoundError(mx) - del self.zones[zonename].mxs[mx] - self.zones[zonename].mod = True + del self.parent.zones[zonename].mxs[mx] + self.parent.zones[zonename].mod = True @handler(u'Lists mail exchangers') def list(self): - return self.zones.keys() + return self.parent.zones.keys() @handler(u'Get information about all mail exchangers') def show(self): - return self.zones.values() + return self.parent.zones.values() class NameServer(Sequence): @@ -280,36 +275,35 @@ class NameServer(Sequence): def as_tuple(self): return (self.name) -class NameServerHandler(Handler): +class NameServerHandler(SubHandler): - def __init__(self, zones): - self.zones = zones + handler_help = u"Manage DNS name servers (NS)" @handler(u'Adds a name server to a zone') def add(self, zone, ns): - if not zone in self.zones: + if not zone in self.parent.zones: raise ZoneNotFoundError(zone) - if ns in self.zones[zone].nss: + if ns in self.parent.zones[zone].nss: raise NameServerAlreadyExistsError(ns) - self.zones[zone].nss[ns] = NameServer(ns) - self.zones[zone].mod = True + self.parent.zones[zone].nss[ns] = NameServer(ns) + self.parent.zones[zone].mod = True @handler(u'Deletes a name server from a zone') def delete(self, zone, ns): - if not zone in self.zones: + if not zone in self.parent.zones: raise ZoneNotFoundError(zone) - if not ns in self.zones[zone].nss: + if not ns in self.parent.zones[zone].nss: raise NameServerNotFoundError(ns) - del self.zones[zone].nss[ns] - self.zones[zone].mod = True + del self.parent.zones[zone].nss[ns] + self.parent.zones[zone].mod = True @handler(u'Lists name servers') def list(self): - return self.zones.keys() + return self.parent.zones.keys() @handler(u'Get information about all name servers') def show(self): - return self.zones.values() + return self.parent.zones.values() class Zone(Sequence): @@ -325,44 +319,42 @@ class Zone(Sequence): def as_tuple(self): return (self.name, self.hosts, self.mxs, self.nss) -class ZoneHandler(Handler): - - r"""ZoneHandler(zones) -> ZoneHandler instance :: Handle a list of zones. +class ZoneHandler(SubHandler): + r"""ZoneHandler(parent.zones) -> ZoneHandler instance :: Handle a list of zones. This class is a helper for DnsHandler to do all the work related to zone administration. - zones - A dictionary with string keys (zone name) and Zone instances values. + parent - The parent service handler. """ - def __init__(self, zones): - self.zones = zones + + handler_help = u"Manage DNS zones" @handler(u'Adds a zone') def add(self, name): - if name in self.zones: - if self.zones[name].dele == True: - self.zones[name].dele = False + if name in self.parent.zones: + if self.parent.zones[name].dele == True: + self.parent.zones[name].dele = False else: raise ZoneAlreadyExistsError(name) - self.zones[name] = Zone(name) - self.zones[name].mod = True - self.zones[name].new = True - + self.parent.zones[name] = Zone(name) + self.parent.zones[name].mod = True + self.parent.zones[name].new = True @handler(u'Deletes a zone') def delete(self, name): r"delete(name) -> None :: Delete a zone from the zone list." - if not name in self.zones: + if not name in self.parent.zones: raise ZoneNotFoundError(name) - self.zones[name].dele = True + self.parent.zones[name].dele = True @handler(u'Lists zones') def list(self): - return self.zones.keys() + return self.parent.zones.keys() @handler(u'Get information about all zones') def show(self): - return self.zones.values() + return self.parent.zones.values() class DnsHandler(Restorable, ConfigWriter, InitdHandler, TransactionalHandler, @@ -378,7 +370,9 @@ class DnsHandler(Restorable, ConfigWriter, InitdHandler, TransactionalHandler, Both defaults to the current working directory. """ - _initd_name = 'bind' + handler_help = u"Manage DNS service" + + _initd_name = 'named' _persistent_attrs = ('params', 'zones') @@ -395,17 +389,23 @@ class DnsHandler(Restorable, ConfigWriter, InitdHandler, TransactionalHandler, _config_writer_files = ('named.conf', 'zoneX.zone') _config_writer_tpl_dir = path.join(path.dirname(__file__), 'templates') - def __init__(self, pickle_dir='.', config_dir='.'): + def __init__(self, pickle_dir='.', config_dir={'named.conf':'.', 'zoneX.zone':'.'}): r"Initialize DnsHandler object, see class documentation for details." self._persistent_dir = pickle_dir self._config_writer_cfg_dir = config_dir self.mod = False self._config_build_templates() self._restore() - self.host = HostHandler(self.zones) - self.zone = ZoneHandler(self.zones) - self.mx = MailExchangeHandler(self.zones) - self.ns = NameServerHandler(self.zones) + # FIXME self.mod = True + #if not self._restore(): + #r = self._restore() + #print r + #if not r: + # self.mod = True + self.host = HostHandler(self) + self.zone = ZoneHandler(self) + self.mx = MailExchangeHandler(self) + self.ns = NameServerHandler(self) def _zone_filename(self, zone): return zone.name + '.zone' @@ -420,7 +420,7 @@ class DnsHandler(Restorable, ConfigWriter, InitdHandler, TransactionalHandler, if a_zone.mod: if not a_zone.new: # TODO freeze de la zona - call(('dns', 'freeze', a_zone.name)) + call(('rndc', 'freeze', a_zone.name)) vars = dict( zone = a_zone, hosts = a_zone.hosts.values(), @@ -432,7 +432,7 @@ class DnsHandler(Restorable, ConfigWriter, InitdHandler, TransactionalHandler, a_zone.mod = False if not a_zone.new: # TODO unfreeze de la zona - call(('dns', 'unfreeze', a_zone.name)) + call(('rndc', 'thaw', a_zone.name)) else : self.mod = True a_zone.new = False