X-Git-Url: https://git.llucax.com/software/pymin.git/blobdiff_plain/7d2f3da6aee023c991c9669a6a4664bd8be53a97..f5a081744ba31beed226db828e0e16a01ec77c83:/pymin/services/dns/__init__.py?ds=inline diff --git a/pymin/services/dns/__init__.py b/pymin/services/dns/__init__.py index cf44ea4..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', @@ -177,47 +178,44 @@ class Host(Sequence): def as_tuple(self): return (self.name, self.ip) -class HostHandler(Handler): +class HostHandler(SubHandler): handler_help = u"Manage DNS hosts" - def __init__(self,zones): - self.zones = zones - @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): @@ -229,47 +227,44 @@ class MailExchange(Sequence): def as_tuple(self): return (self.mx, self.prio) -class MailExchangeHandler(Handler): +class MailExchangeHandler(SubHandler): handler_help = u"Manage DNS mail exchangers (MX)" - def __init__(self, zones): - self.zones = zones - @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,38 +275,35 @@ class NameServer(Sequence): def as_tuple(self): return (self.name) -class NameServerHandler(Handler): +class NameServerHandler(SubHandler): handler_help = u"Manage DNS name servers (NS)" - def __init__(self, zones): - self.zones = zones - @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): @@ -327,46 +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. """ handler_help = u"Manage DNS zones" - def __init__(self, zones): - self.zones = 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, @@ -384,7 +372,7 @@ class DnsHandler(Restorable, ConfigWriter, InitdHandler, TransactionalHandler, handler_help = u"Manage DNS service" - _initd_name = 'bind' + _initd_name = 'named' _persistent_attrs = ('params', 'zones') @@ -401,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' @@ -426,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(), @@ -438,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