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()
113 # FIXME self._update = True
114 #if not self._restore():
118 # self._update = True
119 self.host = HostHandler(self)
120 self.zone = ZoneHandler(self)
121 self.mx = MailExchangeHandler(self)
122 self.ns = NameServerHandler(self)
124 def _zone_filename(self, zone):
125 return zone.name + '.zone'
127 def _get_config_vars(self, config_file):
128 return dict(zones=self.zones.values(), **self.params)
130 def _write_config(self):
131 r"_write_config() -> None :: Generate all the configuration files."
132 delete_zones = list()
133 for a_zone in self.zones.values():
134 if a_zone._update or a_zone._add:
136 call(('rndc', 'freeze', a_zone.name))
139 hosts = a_zone.hosts.values(),
140 mxs = a_zone.mxs.values(),
141 nss = a_zone.nss.values()
143 self._write_single_config('zoneX.zone',
144 self._zone_filename(a_zone), vars)
145 a_zone._update = False
147 call(('rndc', 'thaw', a_zone.name))
152 #borro el archivo .zone
155 unlink(self._zone_filename(a_zone))
157 #la excepcion pude darse en caso que haga un add de una zona y
158 #luego el del, como no hice commit, no se crea el archivo
160 delete_zones.append(a_zone.name)
162 for z in delete_zones:
166 self._write_single_config('named.conf')
171 if __name__ == '__main__':
175 dns.set('isp_dns1','la_garcha.com')
176 dns.set('bind_addr1','localhost')
177 dns.zone.add('zona_loca.com')
178 #dns.zone.update('zona_loca.com','ns1.dominio.com')
180 dns.host.add('zona_loca.com','hostname_loco','192.168.0.23')
181 dns.host.update('zona_loca.com','hostname_loco','192.168.0.66')
183 dns.host.add('zona_loca.com','hostname_kuak','192.168.0.23')
184 dns.host.delete('zona_loca.com','hostname_kuak')
186 dns.host.add('zona_loca.com','hostname_kuang','192.168.0.24')
187 dns.host.add('zona_loca.com','hostname_chan','192.168.0.25')
188 dns.host.add('zona_loca.com','hostname_kaine','192.168.0.26')
190 dns.mx.add('zona_loca.com','mx1.sarasa.com',10)
191 dns.mx.update('zona_loca.com','mx1.sarasa.com',20)
192 dns.mx.add('zona_loca.com','mx2.sarasa.com',30)
193 dns.mx.add('zona_loca.com','mx3.sarasa.com',40)
194 dns.mx.delete('zona_loca.com','mx3.sarasa.com')
196 dns.ns.add('zona_loca.com','ns1.jua.com')
197 dns.ns.add('zona_loca.com','ns2.jua.com')
198 dns.ns.add('zona_loca.com','ns3.jua.com')
199 dns.ns.delete('zona_loca.com','ns3.jua.com')
201 dns.zone.add('zona_oscura')
203 dns.host.add('zona_oscura','hostname_a','192.168.0.24')
204 dns.host.add('zona_oscura','hostname_b','192.168.0.25')
205 dns.host.add('zona_oscura','hostname_c','192.168.0.26')
207 dns.zone.delete('zona_oscura')
211 print 'ZONAS :', dns.zone.show()
213 print 'HOSTS from', z, ':', dns.host.show(z)
217 # dns.zone.update('zone-sarasa','lalal')
218 #except ZoneNotFoundError, inst:
219 # print 'Error: ', inst
221 from pymin.services.util import ItemNotFoundError, ItemAlreadyExistsError, \
222 ContainerNotFoundError
225 dns.zone.delete('zone-sarasa')
226 except ItemNotFoundError, inst:
227 print 'Error: ', inst
230 # dns.zone.add('zona_loca.com','ns1.dom.com','ns2.dom.com')
231 #except ZoneAlreadyExistsError, inst:
232 # print 'Error: ', inst
237 dns.host.update('zone-sarasa','kuak','192.68')
238 except ContainerNotFoundError, inst:
239 print 'Error: ', inst
242 dns.host.update('zona_loca.com','kuak','192.68')
243 except ItemNotFoundError, inst:
244 print 'Error: ', inst
247 dns.host.delete('zone-sarasa','lala')
248 except ContainerNotFoundError, inst:
249 print 'Error: ', inst
252 dns.host.delete('zona_loca.com','lala')
253 except ItemNotFoundError, inst:
254 print 'Error: ', inst
257 dns.host.add('zona','hostname_loco','192.168.0.23')
258 except ContainerNotFoundError, inst:
259 print 'Error: ', inst
262 dns.host.add('zona_loca.com','hostname_loco','192.168.0.23')
263 except ItemAlreadyExistsError, inst:
264 print 'Error: ', inst