Support of Docker host names (for NSD)

This commit is contained in:
Eric van der Vlist 2020-04-18 09:36:48 +02:00
parent f063831578
commit 7172afa4a2
3 changed files with 21 additions and 6 deletions

View File

@ -5,6 +5,7 @@ from django.conf import settings
from jinja2 import Template
import re
import subprocess
from binder import helpers
NSD_CONF_TEMPLATE = """
# nsd.conf for {{hostname}}
@ -16,9 +17,7 @@ remote-control:
"""
ZONE_RE = re.compile("""
zone\:\s+(?P<zone_name>\S+)
\s+state: master""")
ZONE_RE = re.compile("""zone:\s+(?P<zone_name>\S+)\s+state:\s+master""")
class NSDServer(object):
"""Class to manage NSD backend server data."""
@ -53,13 +52,13 @@ class NSDServer(object):
zs_out = subprocess.check_output(
["/usr/sbin/nsd-control",
"-c", self.get_config_path(),
"-s", self.hostname,
"-s", helpers.ip_address(self.hostname),
"zonestatus"],
stderr=subprocess.STDOUT,
).decode('utf-8')
except subprocess.CalledProcessError:
raise
zones = ZONE_RE.findall(zs_out, re.MULTILINE)
zones = ZONE_RE.findall(zs_out)
zone_data = {}
zone_data['stats'] = {}
zone_data['stats']['zone_stats'] = {}

View File

@ -175,6 +175,20 @@ def ip_info(host_name):
return info
def ip_address(host_name):
"""Find an IP address for a host.
"""
try:
for s_family, s_type, s_proto, s_cannoname, s_sockaddr in socket.getaddrinfo(host_name, None):
if s_family == 2 and s_type == 1:
return s_sockaddr[0]
if s_family == 10 and s_type == 1:
return s_sockaddr[0]
except (socket.gaierror, err):
return None
return None
def send_dns_update(dns_message, dns_server, port, key_name):
"""Send DNS message to server and return response.

View File

@ -20,6 +20,8 @@ from binder.backends import nsd
from django.db import models
from django.conf import settings
from django.core.validators import MinValueValidator, MaxValueValidator
from binder import helpers
TSIG_ALGORITHMS = (('HMAC-MD5.SIG-ALG.REG.INT', 'MD5'),
('hmac-sha1', 'SHA1'),
@ -198,7 +200,7 @@ class BindServer(models.Model):
try:
xfr = dns.query.xfr(
self.hostname,
helpers.ip_address(self.hostname),
zone_name,
port=self.dns_port,
keyring=keyring,