Now accepts one or more secondary nameservers. Works from the web interface.

This commit is contained in:
Brian Bustin 2015-07-01 21:16:28 -04:00
parent c23c1c311e
commit 32a6ebe2ca
3 changed files with 32 additions and 38 deletions

View File

@ -229,7 +229,7 @@ def dns_get_secondary_nameserver():
def dns_set_secondary_nameserver():
from dns_update import set_secondary_dns
try:
return set_secondary_dns(request.form.get('hostname'), env)
return set_secondary_dns(request.form.get('hostname').split(","), env)
except ValueError as e:
return (str(e), 400)

View File

@ -145,12 +145,11 @@ def build_zone(domain, all_domains, additional_records, www_redirect_domains, en
# Define ns2.PRIMARY_HOSTNAME or whatever the user overrides.
# User may provide one or more additional nameservers
if len(get_secondary_dns(additional_records)) > 0:
for secondary_ns in get_secondary_dns(additional_records):
records.append((None, "NS", secondary_ns+'.', False))
if get_secondary_dns(additional_records):
[records.append((None, "NS", secondary_ns+'.', False)) for secondary_ns in get_secondary_dns(additional_records)]
else:
secondary_ns = get_secondary_dns(additional_records) or ("ns2." + env["PRIMARY_HOSTNAME"])
records.append((None, "NS", secondary_ns+'.', False))
secondary_ns = get_secondary_dns(additional_records) or ()
records.append((None, "NS", "ns2." + env["PRIMARY_HOSTNAME"] + '.', False))
# In PRIMARY_HOSTNAME...
@ -467,17 +466,15 @@ zone:
zonefile: %s
""" % (domain, zonefile)
# If a custom secondary nameservers have been set, allow zone transfers
# and notifies to th.
for secondary_ns in get_secondary_dns(additional_records):
# Get the IP address of the nameserver by resolving it.
hostname = get_secondary_dns(additional_records)
resolver = dns.resolver.get_default_resolver()
response = dns.resolver.query(hostname+'.', "A")
ipaddr = str(response[0])
nsdconf += """\tnotify: %s NOKEY
provide-xfr: %s NOKEY
""" % (ipaddr, ipaddr)
# If custom secondary nameservers have been set, allow zone transfers
# and notifies to them.
if get_secondary_dns(additional_records):
for hostname in get_secondary_dns(additional_records):
# Get the IP address of the nameserver by resolving it.
resolver = dns.resolver.get_default_resolver()
response = dns.resolver.query(hostname+'.', "A")
ipaddr = str(response[0])
nsdconf += "\n\tnotify: %s NOKEY\n\tprovide-xfr: %s NOKEY" % (ipaddr, ipaddr)
# Check if the file is changing. If it isn't changing,
# return False to flag that no change was made.
@ -790,37 +787,34 @@ def set_custom_dns_record(qname, rtype, value, action, env):
if made_change:
# serialize & save
write_custom_dns_config(newconfig, env)
return made_change
########################################################################
def get_secondary_dns(custom_dns):
values = []
for qname, rtype, value in custom_dns:
if qname == "_secondary_nameserver":
# always return a list so other parts of code path can iterate
if isinstance(value, str):
return [value]
else:
return value
values.append(value)
if len(values) > 0:
return values
return None
def set_secondary_dns(hostname, env):
if hostname in (None, ""):
# Clear.
set_custom_dns_record("_secondary_nameserver", "A", None, "set", env)
else:
# Validate.
hostname = hostname.strip().lower()
hostnames = [item.strip().lower() for item in hostname]
if len(hostnames) > 0:
resolver = dns.resolver.get_default_resolver()
try:
response = dns.resolver.query(hostname, "A")
except (dns.resolver.NoNameservers, dns.resolver.NXDOMAIN, dns.resolver.NoAnswer):
raise ValueError("Could not resolve the IP address of %s." % hostname)
# Set.
set_custom_dns_record("_secondary_nameserver", "A", hostname, "set", env)
for item in hostnames:
try:
response = dns.resolver.query(item, "A")
except (dns.resolver.NoNameservers, dns.resolver.NXDOMAIN, dns.resolver.NoAnswer):
raise ValueError("Could not resolve the IP address of %s." % item)
# Set.
set_custom_dns_record("_secondary_nameserver", "A", {"A":hostnames}, "set", env)
else:
# Clear.
set_custom_dns_record("_secondary_nameserver", "A", None, "set", env)
# Apply.
return do_dns_update(env)
@ -912,5 +906,5 @@ if __name__ == "__main__":
for zone, records in build_recommended_dns(env):
for record in records:
print("; " + record['explanation'])
print(record['qname'], record['rtype'], record['value'], sep="\t")
print(record['qname'], record['rtype'], record['value'])
print()

View File

@ -67,7 +67,7 @@
<h3>Using a Secondary Nameserver</h3>
<p>If your TLD requires you to have two separate nameservers, you can either set up a secondary (aka &ldquo;slave&rdquo;) nameserver or, alternatively, set up <a href="#" onclick="return show_panel('external_dns')">external DNS</a> and ignore the DNS server on this box. If you choose to use a seconday/slave nameserver, you must find a seconday/slave nameserver service provider. Your domain name registrar or virtual cloud provider may provide this service for you. Once you set up the seconday/slave nameserver service, enter the hostname of <em>their</em> secondary nameserver:</p>
<p>If your TLD requires you to have two separate nameservers, you can either set up a secondary (aka &ldquo;slave&rdquo;) nameserver or, alternatively, set up <a href="#" onclick="return show_panel('external_dns')">external DNS</a> and ignore the DNS server on this box. If you choose to use a seconday/slave nameserver, you must find a seconday/slave nameserver service provider. Your domain name registrar or virtual cloud provider may provide this service for you. Once you set up the seconday/slave nameserver service, enter the hostname of <em>their</em> secondary nameserver (multiple secondaries can be added separated with commas i.e. ns2.hostingcompany.com,ns3.hostingcompany.com):</p>
<form class="form-horizontal" role="form" onsubmit="do_set_secondary_dns(); return false;">
<div class="form-group">