1 # vim: set encoding=utf-8 et sw=4 sts=4 :
7 from pymin.seqtools import Sequence
8 from pymin.dispatcher import handler, HandlerError, Handler
9 from pymin.services.util import Restorable, ConfigWriter, InitdHandler, \
10 TransactionalHandler, ParametersHandler, \
11 DictComposedSubHandler, DictSubHandler, call
13 __ALL__ = ('DnsHandler',)
16 def __init__(self, name, ip):
19 def update(self, ip=None):
20 if ip is not None: self.ip = ip
22 return (self.name, self.ip)
24 class HostHandler(DictComposedSubHandler):
25 handler_help = u"Manage DNS hosts"
26 _comp_subhandler_cont = 'zones'
27 _comp_subhandler_attr = 'hosts'
28 _comp_subhandler_class = Host
30 class MailExchange(Sequence):
31 def __init__(self, mx, prio):
34 def update(self, prio=None):
35 if prio is not None: self.prio = prio
37 return (self.mx, self.prio)
39 class MailExchangeHandler(DictComposedSubHandler):
40 handler_help = u"Manage DNS mail exchangers (MX)"
41 _comp_subhandler_cont = 'zones'
42 _comp_subhandler_attr = 'mxs'
43 _comp_subhandler_class = MailExchange
45 class NameServer(Sequence):
46 def __init__(self, name):
51 class NameServerHandler(DictComposedSubHandler):
52 handler_help = u"Manage DNS name servers (NS)"
53 _comp_subhandler_cont = 'zones'
54 _comp_subhandler_attr = 'nss'
55 _comp_subhandler_class = NameServer
58 def __init__(self, name):
67 return (self.name, self.hosts, self.mxs, self.nss)
69 class ZoneHandler(DictSubHandler):
70 handler_help = u"Manage DNS zones"
71 _cont_subhandler_attr = 'zones'
72 _cont_subhandler_class = Zone
74 class DnsHandler(Restorable, ConfigWriter, InitdHandler, TransactionalHandler,
76 r"""DnsHandler([pickle_dir[, config_dir]]) -> DnsHandler instance.
78 Handles DNS service commands for the dns program.
80 pickle_dir - Directory where to write the persistent configuration data.
82 config_dir - Directory where to store de generated configuration files.
84 Both defaults to the current working directory.
87 handler_help = u"Manage DNS service"
91 _persistent_attrs = ('params', 'zones')
93 _restorable_defaults = dict(
103 _config_writer_files = ('named.conf', 'zoneX.zone')
104 _config_writer_tpl_dir = path.join(path.dirname(__file__), 'templates')
106 def __init__(self, pickle_dir='.', config_dir='.'):
107 r"Initialize DnsHandler object, see class documentation for details."
108 self._persistent_dir = pickle_dir
109 self._config_writer_cfg_dir = config_dir
111 self._config_build_templates()
112 InitdHandler.__init__(self)
113 self.host = HostHandler(self)
114 self.zone = ZoneHandler(self)
115 self.mx = MailExchangeHandler(self)
116 self.ns = NameServerHandler(self)
118 def _zone_filename(self, zone):
119 return zone.name + '.zone'
121 def _get_config_vars(self, config_file):
122 return dict(zones=self.zones.values(), **self.params)
124 def _write_config(self):
125 r"_write_config() -> None :: Generate all the configuration files."
126 delete_zones = list()
127 for a_zone in self.zones.values():
128 if a_zone._update or a_zone._add:
129 if not a_zone._add and self._service_running:
130 call(('rndc', 'freeze', a_zone.name))
133 hosts = a_zone.hosts.values(),
134 mxs = a_zone.mxs.values(),
135 nss = a_zone.nss.values()
137 self._write_single_config('zoneX.zone',
138 self._zone_filename(a_zone), vars)
139 a_zone._update = False
140 if not a_zone._add and self._service_running:
141 call(('rndc', 'thaw', a_zone.name))
146 #borro el archivo .zone
149 unlink(self._zone_filename(a_zone))
151 #la excepcion pude darse en caso que haga un add de una zona y
152 #luego el del, como no hice commit, no se crea el archivo
154 delete_zones.append(a_zone.name)
156 for z in delete_zones:
160 self._write_single_config('named.conf')
162 return False # Do reload
163 return True # we don't need to reload
166 def handle_timer(self):
168 p = subprocess.Popen(('pgrep', '-f', '/usr/sbin/named'),
169 stdout=subprocess.PIPE)
170 pid = p.communicate()[0]
171 if p.returncode == 0 and len(pid) > 0:
172 self._service_running = True
174 self._service_running = False
178 if __name__ == '__main__':
182 dns.set('isp_dns1','la_garcha.com')
183 dns.set('bind_addr1','localhost')
184 dns.zone.add('zona_loca.com')
185 #dns.zone.update('zona_loca.com','ns1.dominio.com')
187 dns.host.add('zona_loca.com','hostname_loco','192.168.0.23')
188 dns.host.update('zona_loca.com','hostname_loco','192.168.0.66')
190 dns.host.add('zona_loca.com','hostname_kuak','192.168.0.23')
191 dns.host.delete('zona_loca.com','hostname_kuak')
193 dns.host.add('zona_loca.com','hostname_kuang','192.168.0.24')
194 dns.host.add('zona_loca.com','hostname_chan','192.168.0.25')
195 dns.host.add('zona_loca.com','hostname_kaine','192.168.0.26')
197 dns.mx.add('zona_loca.com','mx1.sarasa.com',10)
198 dns.mx.update('zona_loca.com','mx1.sarasa.com',20)
199 dns.mx.add('zona_loca.com','mx2.sarasa.com',30)
200 dns.mx.add('zona_loca.com','mx3.sarasa.com',40)
201 dns.mx.delete('zona_loca.com','mx3.sarasa.com')
203 dns.ns.add('zona_loca.com','ns1.jua.com')
204 dns.ns.add('zona_loca.com','ns2.jua.com')
205 dns.ns.add('zona_loca.com','ns3.jua.com')
206 dns.ns.delete('zona_loca.com','ns3.jua.com')
208 dns.zone.add('zona_oscura')
210 dns.host.add('zona_oscura','hostname_a','192.168.0.24')
211 dns.host.add('zona_oscura','hostname_b','192.168.0.25')
212 dns.host.add('zona_oscura','hostname_c','192.168.0.26')
214 dns.zone.delete('zona_oscura')
218 print 'ZONAS :', dns.zone.show()
220 print 'HOSTS from', z, ':', dns.host.show(z)
224 # dns.zone.update('zone-sarasa','lalal')
225 #except ZoneNotFoundError, inst:
226 # print 'Error: ', inst
228 from pymin.services.util import ItemNotFoundError, ItemAlreadyExistsError, \
229 ContainerNotFoundError
232 dns.zone.delete('zone-sarasa')
233 except ItemNotFoundError, inst:
234 print 'Error: ', inst
237 # dns.zone.add('zona_loca.com','ns1.dom.com','ns2.dom.com')
238 #except ZoneAlreadyExistsError, inst:
239 # print 'Error: ', inst
244 dns.host.update('zone-sarasa','kuak','192.68')
245 except ContainerNotFoundError, inst:
246 print 'Error: ', inst
249 dns.host.update('zona_loca.com','kuak','192.68')
250 except ItemNotFoundError, inst:
251 print 'Error: ', inst
254 dns.host.delete('zone-sarasa','lala')
255 except ContainerNotFoundError, inst:
256 print 'Error: ', inst
259 dns.host.delete('zona_loca.com','lala')
260 except ItemNotFoundError, inst:
261 print 'Error: ', inst
264 dns.host.add('zona','hostname_loco','192.168.0.23')
265 except ContainerNotFoundError, inst:
266 print 'Error: ', inst
269 dns.host.add('zona_loca.com','hostname_loco','192.168.0.23')
270 except ItemAlreadyExistsError, inst:
271 print 'Error: ', inst