2014-10-03 17:37:36 +00:00
|
|
|
import textwrap
|
2014-05-08 16:59:35 +00:00
|
|
|
|
2014-10-03 17:37:36 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2014-05-08 16:59:35 +00:00
|
|
|
|
2014-07-09 16:17:43 +00:00
|
|
|
from orchestra.apps.orchestration import ServiceController
|
2014-10-04 13:23:04 +00:00
|
|
|
from orchestra.utils.python import AttrDict
|
2014-05-08 16:59:35 +00:00
|
|
|
|
2014-10-03 17:37:36 +00:00
|
|
|
from . import settings
|
|
|
|
|
2014-05-08 16:59:35 +00:00
|
|
|
|
2014-07-09 16:17:43 +00:00
|
|
|
class Bind9MasterDomainBackend(ServiceController):
|
2014-05-08 16:59:35 +00:00
|
|
|
verbose_name = _("Bind9 master domain")
|
|
|
|
model = 'domains.Domain'
|
|
|
|
related_models = (
|
|
|
|
('domains.Record', 'domain__origin'),
|
|
|
|
('domains.Domain', 'origin'),
|
|
|
|
)
|
2014-10-10 14:39:46 +00:00
|
|
|
ignore_fields = ['serial']
|
2014-05-08 16:59:35 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def is_main(cls, obj):
|
|
|
|
""" work around Domain.top self relationship """
|
|
|
|
if super(Bind9MasterDomainBackend, cls).is_main(obj):
|
|
|
|
return not obj.top
|
|
|
|
|
|
|
|
def save(self, domain):
|
|
|
|
context = self.get_context(domain)
|
|
|
|
domain.refresh_serial()
|
|
|
|
context['zone'] = ';; %(banner)s\n' % context
|
|
|
|
context['zone'] += domain.render_zone()
|
2014-10-17 13:41:08 +00:00
|
|
|
self.append(textwrap.dedent("""\
|
|
|
|
{
|
|
|
|
echo -e '%(zone)s' | diff -N -I'^\s*;;' %(zone_path)s -
|
|
|
|
} || {
|
|
|
|
echo -e '%(zone)s' > %(zone_path)s
|
|
|
|
UPDATED=1
|
|
|
|
}""" % context
|
|
|
|
))
|
2014-05-08 16:59:35 +00:00
|
|
|
self.update_conf(context)
|
|
|
|
|
|
|
|
def update_conf(self, context):
|
2014-10-17 13:41:08 +00:00
|
|
|
self.append(textwrap.dedent("""\
|
2014-10-17 13:51:33 +00:00
|
|
|
cat -s <(sed -e 's/^};/};\\n/' %(conf_path)s) | \\
|
2014-10-17 13:41:08 +00:00
|
|
|
awk -v s=pangea.cat 'BEGIN { RS=""; s="zone \\""s"\\"" } $0~s{ print }' | \\
|
|
|
|
diff -I"^\s*//" - <(echo '%(conf)s') || {
|
2014-10-17 13:51:33 +00:00
|
|
|
cat -s <(sed -e 's/^};/};\\n/' %(conf_path)s) | \\
|
|
|
|
awk -v s="%(name)s" 'BEGIN { RS=""; s="zone \\""s"\\"" } $0!~s{ print $0"\\n" }' \\
|
|
|
|
> %(conf_path)s.tmp
|
|
|
|
echo -e '%(conf)s' >> %(conf_path)s.tmp
|
|
|
|
mv %(conf_path)s.tmp %(conf_path)s
|
2014-10-17 13:41:08 +00:00
|
|
|
UPDATED=1
|
|
|
|
}""" % context
|
|
|
|
))
|
2014-05-08 16:59:35 +00:00
|
|
|
for subdomain in context['subdomains']:
|
|
|
|
context['name'] = subdomain.name
|
2014-10-03 14:02:11 +00:00
|
|
|
self.delete(subdomain)
|
2014-05-08 16:59:35 +00:00
|
|
|
|
|
|
|
def delete(self, domain):
|
|
|
|
context = self.get_context(domain)
|
|
|
|
self.append('rm -f %(zone_path)s;' % context)
|
|
|
|
self.delete_conf(context)
|
|
|
|
|
|
|
|
def delete_conf(self, context):
|
2014-10-17 13:09:56 +00:00
|
|
|
if context['name'][0] in ('*', '_'):
|
|
|
|
# These can never be top level domains
|
|
|
|
return
|
2014-10-17 13:41:08 +00:00
|
|
|
self.append(textwrap.dedent("""\
|
2014-10-17 13:51:33 +00:00
|
|
|
cat -s <(sed -e 's/^};/};\\n/' %(conf_path)s) | \\
|
|
|
|
awk -v s="%(name)s" 'BEGIN { RS=""; s="zone \\""s"\\"" } $0!~s{ print $0"\\n" }' \\
|
|
|
|
> %(conf_path)s.tmp""" % context
|
2014-10-04 09:29:18 +00:00
|
|
|
))
|
2014-05-08 16:59:35 +00:00
|
|
|
self.append('diff -I"^\s*//" %(conf_path)s.tmp %(conf_path)s || UPDATED=1' % context)
|
|
|
|
self.append('mv %(conf_path)s.tmp %(conf_path)s' % context)
|
|
|
|
|
|
|
|
def commit(self):
|
|
|
|
""" reload bind if needed """
|
|
|
|
self.append('[[ $UPDATED == 1 ]] && service bind9 reload')
|
|
|
|
|
2014-10-03 17:37:36 +00:00
|
|
|
def get_servers(self, domain, backend):
|
2014-10-04 13:23:04 +00:00
|
|
|
from orchestra.apps.orchestration.models import Route
|
|
|
|
operation = AttrDict(backend=backend, action='save', instance=domain)
|
2014-10-03 17:37:36 +00:00
|
|
|
servers = []
|
|
|
|
for server in Route.get_servers(operation):
|
|
|
|
servers.append(server.get_ip())
|
|
|
|
return servers
|
|
|
|
|
2014-10-04 09:29:18 +00:00
|
|
|
def get_slaves(self, domain):
|
|
|
|
return self.get_servers(domain, Bind9SlaveDomainBackend)
|
|
|
|
|
2014-05-08 16:59:35 +00:00
|
|
|
def get_context(self, domain):
|
|
|
|
context = {
|
|
|
|
'name': domain.name,
|
|
|
|
'zone_path': settings.DOMAINS_ZONE_PATH % {'name': domain.name},
|
2014-10-03 14:02:11 +00:00
|
|
|
'subdomains': domain.subdomains.all(),
|
2014-05-08 16:59:35 +00:00
|
|
|
'banner': self.get_banner(),
|
2014-10-04 09:29:18 +00:00
|
|
|
'slaves': '; '.join(self.get_slaves(domain)) or 'none',
|
2014-05-08 16:59:35 +00:00
|
|
|
}
|
|
|
|
context.update({
|
|
|
|
'conf_path': settings.DOMAINS_MASTERS_PATH,
|
2014-10-03 17:37:36 +00:00
|
|
|
'conf': textwrap.dedent("""
|
|
|
|
zone "%(name)s" {
|
|
|
|
// %(banner)s
|
|
|
|
type master;
|
|
|
|
file "%(zone_path)s";
|
|
|
|
allow-transfer { %(slaves)s; };
|
|
|
|
};""" % context)
|
2014-05-08 16:59:35 +00:00
|
|
|
})
|
|
|
|
return context
|
|
|
|
|
|
|
|
|
|
|
|
class Bind9SlaveDomainBackend(Bind9MasterDomainBackend):
|
|
|
|
verbose_name = _("Bind9 slave domain")
|
2014-09-24 20:09:41 +00:00
|
|
|
related_models = (
|
|
|
|
('domains.Domain', 'origin'),
|
|
|
|
)
|
2014-05-13 13:46:40 +00:00
|
|
|
|
2014-05-08 16:59:35 +00:00
|
|
|
def save(self, domain):
|
|
|
|
context = self.get_context(domain)
|
|
|
|
self.update_conf(context)
|
|
|
|
|
|
|
|
def delete(self, domain):
|
|
|
|
context = self.get_context(domain)
|
|
|
|
self.delete_conf(context)
|
2014-10-04 09:29:18 +00:00
|
|
|
|
2014-05-08 16:59:35 +00:00
|
|
|
def commit(self):
|
|
|
|
""" ideally slave should be restarted after master """
|
|
|
|
self.append('[[ $UPDATED == 1 ]] && { sleep 1 && service bind9 reload; } &')
|
|
|
|
|
2014-10-04 09:29:18 +00:00
|
|
|
def get_masters(self, domain):
|
|
|
|
return self.get_servers(domain, Bind9MasterDomainBackend)
|
|
|
|
|
2014-05-08 16:59:35 +00:00
|
|
|
def get_context(self, domain):
|
|
|
|
context = {
|
|
|
|
'name': domain.name,
|
2014-10-03 17:37:36 +00:00
|
|
|
'banner': self.get_banner(),
|
|
|
|
'subdomains': domain.subdomains.all(),
|
2014-10-04 09:29:18 +00:00
|
|
|
'masters': '; '.join(self.get_masters(domain)) or 'none',
|
2014-05-08 16:59:35 +00:00
|
|
|
}
|
|
|
|
context.update({
|
|
|
|
'conf_path': settings.DOMAINS_SLAVES_PATH,
|
2014-10-03 17:37:36 +00:00
|
|
|
'conf': textwrap.dedent("""
|
|
|
|
zone "%(name)s" {
|
|
|
|
// %(banner)s
|
|
|
|
type slave;
|
|
|
|
file "%(name)s";
|
|
|
|
masters { %(masters)s; };
|
|
|
|
allow-notify { %(masters)s; };
|
|
|
|
};""" % context)
|
2014-05-08 16:59:35 +00:00
|
|
|
})
|
|
|
|
return context
|