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 if __name__ == '__main__':
170 dns.set('isp_dns1','la_garcha.com')
171 dns.set('bind_addr1','localhost')
172 dns.zone.add('zona_loca.com')
173 #dns.zone.update('zona_loca.com','ns1.dominio.com')
175 dns.host.add('zona_loca.com','hostname_loco','192.168.0.23')
176 dns.host.update('zona_loca.com','hostname_loco','192.168.0.66')
178 dns.host.add('zona_loca.com','hostname_kuak','192.168.0.23')
179 dns.host.delete('zona_loca.com','hostname_kuak')
181 dns.host.add('zona_loca.com','hostname_kuang','192.168.0.24')
182 dns.host.add('zona_loca.com','hostname_chan','192.168.0.25')
183 dns.host.add('zona_loca.com','hostname_kaine','192.168.0.26')
185 dns.mx.add('zona_loca.com','mx1.sarasa.com',10)
186 dns.mx.update('zona_loca.com','mx1.sarasa.com',20)
187 dns.mx.add('zona_loca.com','mx2.sarasa.com',30)
188 dns.mx.add('zona_loca.com','mx3.sarasa.com',40)
189 dns.mx.delete('zona_loca.com','mx3.sarasa.com')
191 dns.ns.add('zona_loca.com','ns1.jua.com')
192 dns.ns.add('zona_loca.com','ns2.jua.com')
193 dns.ns.add('zona_loca.com','ns3.jua.com')
194 dns.ns.delete('zona_loca.com','ns3.jua.com')
196 dns.zone.add('zona_oscura')
198 dns.host.add('zona_oscura','hostname_a','192.168.0.24')
199 dns.host.add('zona_oscura','hostname_b','192.168.0.25')
200 dns.host.add('zona_oscura','hostname_c','192.168.0.26')
202 dns.zone.delete('zona_oscura')
206 print 'ZONAS :', dns.zone.show()
208 print 'HOSTS from', z, ':', dns.host.show(z)
212 # dns.zone.update('zone-sarasa','lalal')
213 #except ZoneNotFoundError, inst:
214 # print 'Error: ', inst
216 from pymin.services.util import ItemNotFoundError, ItemAlreadyExistsError, \
217 ContainerNotFoundError
220 dns.zone.delete('zone-sarasa')
221 except ItemNotFoundError, inst:
222 print 'Error: ', inst
225 # dns.zone.add('zona_loca.com','ns1.dom.com','ns2.dom.com')
226 #except ZoneAlreadyExistsError, inst:
227 # print 'Error: ', inst
232 dns.host.update('zone-sarasa','kuak','192.68')
233 except ContainerNotFoundError, inst:
234 print 'Error: ', inst
237 dns.host.update('zona_loca.com','kuak','192.68')
238 except ItemNotFoundError, inst:
239 print 'Error: ', inst
242 dns.host.delete('zone-sarasa','lala')
243 except ContainerNotFoundError, inst:
244 print 'Error: ', inst
247 dns.host.delete('zona_loca.com','lala')
248 except ItemNotFoundError, inst:
249 print 'Error: ', inst
252 dns.host.add('zona','hostname_loco','192.168.0.23')
253 except ContainerNotFoundError, inst:
254 print 'Error: ', inst
257 dns.host.add('zona_loca.com','hostname_loco','192.168.0.23')
258 except ItemAlreadyExistsError, inst:
259 print 'Error: ', inst