From 3fa821e2dc2b0fa1fb6f5a999f318412c2c9dc3a Mon Sep 17 00:00:00 2001 From: Difrex Date: Wed, 10 Aug 2016 12:07:03 +0300 Subject: [PATCH] Some refactoring --- surok/discovery.py | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/surok/discovery.py b/surok/discovery.py index cbde725..689463b 100644 --- a/surok/discovery.py +++ b/surok/discovery.py @@ -25,18 +25,25 @@ def resolve(app, conf): # /etc/surok/conf.d/service_conf.json group = service['group'] - try: - fqdn = '_' + service['name'] + '.' + group + '._tcp.' + domain - query = dns.resolver.query(fqdn, 'SRV') - query.lifetime = 1.0 - - for rdata in query: - info = str(rdata).split() - server = {'name': info[3], 'port': info[2]} - hosts[service['name']].append(server) - except DNSException: - print("Could not resolve " + - service['name'] + '.' + - group + '._tcp.' + domain) + fqdn = '_' + service['name'] + '.' + group + '._tcp.' + domain + hosts[service['name']] = do_query(fqdn) return hosts + + +# Do SRV queries +# Return array: [{"name": "f.q.d.n", "port": 8876}] +def do_query(fqdn): + servers = [] + try: + query = dns.resolver.query(fqdn, 'SRV') + query.lifetime = 1.0 + + for rdata in query: + info = str(rdata).split() + server = {'name': info[3], 'port': info[2]} + servers.append(server) + except DNSException: + print("Could not resolve " + fqdn) + + return servers