1 # vim: set encoding=utf-8 et sw=4 sts=4 :
3 # TODO documentation, validation
7 import logging ; log = logging.getLogger('pymin.services.dns')
9 from pymin.service.util import Restorable, ConfigWriter, \
10 TransactionalHandler, ParametersHandler, \
13 from host import HostHandler
14 from mx import MailExchangeHandler
15 from ns import NameServerHandler
16 from zone import ZoneHandler
18 __all__ = ('DnsHandler',)
21 class DnsHandler(Restorable, ConfigWriter, InitdHandler, TransactionalHandler,
23 r"""DnsHandler([pickle_dir[, config_dir]]) -> DnsHandler instance.
25 Handles DNS service commands for the dns program.
27 pickle_dir - Directory where to write the persistent configuration data.
29 config_dir - Directory where to store de generated configuration files.
31 Both defaults to the current working directory.
34 handler_help = u"Manage DNS service"
38 _persistent_attrs = ('params', 'zones')
40 _restorable_defaults = dict(
50 _config_writer_files = ('named.conf', 'zoneX.zone')
51 _config_writer_tpl_dir = path.join(path.dirname(__file__), 'templates')
53 def __init__(self, pickle_dir='.', config_dir='.'):
54 r"Initialize DnsHandler object, see class documentation for details."
55 log.debug(u'DnsHandler(%r, %r)', pickle_dir, config_dir)
56 self._persistent_dir = pickle_dir
57 self._config_writer_cfg_dir = config_dir
59 self._config_build_templates()
60 InitdHandler.__init__(self)
61 self.host = HostHandler(self)
62 self.zone = ZoneHandler(self)
63 self.mx = MailExchangeHandler(self)
64 self.ns = NameServerHandler(self)
66 def _zone_filename(self, zone):
67 return zone.name + '.zone'
69 def _get_config_vars(self, config_file):
70 return dict(zones=self.zones.values(), **self.params)
72 def _write_config(self):
73 r"_write_config() -> None :: Generate all the configuration files."
74 log.debug(u'DnsHandler._write_config()')
76 for a_zone in self.zones.values():
77 log.debug(u'DnsHandler._write_config: processing zone %s', a_zone)
78 if a_zone._update or a_zone._add:
79 if not a_zone._add and self._service_running:
80 log.debug(u'DnsHandler._write_config: zone updated and '
81 u'the service is running, freezing zone')
82 call(('rndc', 'freeze', a_zone.name))
85 hosts = a_zone.hosts.values(),
86 mxs = a_zone.mxs.values(),
87 nss = a_zone.nss.values()
89 self._write_single_config('zoneX.zone',
90 self._zone_filename(a_zone), vars)
91 a_zone._update = False
92 if not a_zone._add and self._service_running:
93 log.debug(u'DnsHandler._write_config: unfreezing zone')
94 call(('rndc', 'thaw', a_zone.name))
99 #borro el archivo .zone
100 log.debug(u'DnsHandler._write_config: zone deleted, removing '
101 u'the file %r', self._zone_filename(a_zone))
104 unlink(self._zone_filename(a_zone))
106 #la excepcion pude darse en caso que haga un add de una zona y
107 #luego el del, como no hice commit, no se crea el archivo
108 log.debug(u'DnsHandler._write_config: file not found')
110 delete_zones.append(a_zone.name)
112 for z in delete_zones:
116 self._write_single_config('named.conf')
118 return False # Do reload
119 return True # we don't need to reload
122 def handle_timer(self):
123 log.debug(u'DnsHandler.handle_timer()')
125 p = subprocess.Popen(('pgrep', '-f', '/usr/sbin/named'),
126 stdout=subprocess.PIPE)
127 pid = p.communicate()[0]
128 if p.returncode == 0 and len(pid) > 0:
129 log.debug(u'DnsHandler.handle_timer: pid present, running')
130 self._service_running = True
132 log.debug(u'DnsHandler.handle_timer: pid absent, NOT running')
133 self._service_running = False
137 if __name__ == '__main__':
140 level = logging.DEBUG,
141 format = '%(asctime)s %(levelname)-8s %(message)s',
142 datefmt = '%H:%M:%S',
147 dns.set('isp_dns1','la_garcha.com')
148 dns.set('bind_addr1','localhost')
149 dns.zone.add('zona_loca.com')
150 #dns.zone.update('zona_loca.com','ns1.dominio.com')
152 dns.host.add('zona_loca.com','hostname_loco','192.168.0.23')
153 dns.host.update('zona_loca.com','hostname_loco','192.168.0.66')
155 dns.host.add('zona_loca.com','hostname_kuak','192.168.0.23')
156 dns.host.delete('zona_loca.com','hostname_kuak')
158 dns.host.add('zona_loca.com','hostname_kuang','192.168.0.24')
159 dns.host.add('zona_loca.com','hostname_chan','192.168.0.25')
160 dns.host.add('zona_loca.com','hostname_kaine','192.168.0.26')
162 dns.mx.add('zona_loca.com','mx1.sarasa.com',10)
163 dns.mx.update('zona_loca.com','mx1.sarasa.com',20)
164 dns.mx.add('zona_loca.com','mx2.sarasa.com',30)
165 dns.mx.add('zona_loca.com','mx3.sarasa.com',40)
166 dns.mx.delete('zona_loca.com','mx3.sarasa.com')
168 dns.ns.add('zona_loca.com','ns1.jua.com')
169 dns.ns.add('zona_loca.com','ns2.jua.com')
170 dns.ns.add('zona_loca.com','ns3.jua.com')
171 dns.ns.delete('zona_loca.com','ns3.jua.com')
173 dns.zone.add('zona_oscura')
175 dns.host.add('zona_oscura','hostname_a','192.168.0.24')
176 dns.host.add('zona_oscura','hostname_b','192.168.0.25')
177 dns.host.add('zona_oscura','hostname_c','192.168.0.26')
179 dns.zone.delete('zona_oscura')
183 print 'ZONAS :', dns.zone.show()
185 print 'HOSTS from', z, ':', dns.host.show(z)
187 from pymin.services.util import ItemNotFoundError, ItemAlreadyExistsError, \
188 ContainerNotFoundError
191 dns.zone.delete('zone-sarasa')
192 except ItemNotFoundError, inst:
193 print 'Error: ', inst
198 dns.host.update('zone-sarasa','kuak','192.68')
199 except ContainerNotFoundError, inst:
200 print 'Error: ', inst
203 dns.host.update('zona_loca.com','kuak','192.68')
204 except ItemNotFoundError, inst:
205 print 'Error: ', inst
208 dns.host.delete('zone-sarasa','lala')
209 except ContainerNotFoundError, inst:
210 print 'Error: ', inst
213 dns.host.delete('zona_loca.com','lala')
214 except ItemNotFoundError, inst:
215 print 'Error: ', inst
218 dns.host.add('zona','hostname_loco','192.168.0.23')
219 except ContainerNotFoundError, inst:
220 print 'Error: ', inst
223 dns.host.add('zona_loca.com','hostname_loco','192.168.0.23')
224 except ItemAlreadyExistsError, inst:
225 print 'Error: ', inst