From f6a48f9bd39f35befe722d674153dd5ce5b70304 Mon Sep 17 00:00:00 2001 From: Difrex Date: Mon, 1 Aug 2016 14:56:35 +0300 Subject: [PATCH] Working discovery --- .gitignore | 3 ++- surok.py | 21 +++++++++++++++++++-- surok/discovery.py | 11 +++++++++++ surok/templates.py | 12 ++++++++++++ 4 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 surok/discovery.py create mode 100644 surok/templates.py diff --git a/.gitignore b/.gitignore index 96403d3..ffa90d4 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -__pycache__/* +surok/__pycache__ +__pycache__ diff --git a/surok.py b/surok.py index 626343e..43913f1 100755 --- a/surok.py +++ b/surok.py @@ -4,11 +4,14 @@ from time import sleep from os import listdir from os.path import isfile, join import json +from surok.templates import gen +from surok.discovery import resolve # Load base configurations f = open('conf/surok.json', 'r') conf = json.loads(f.read()) print(conf) +f.close() # Get app configurations @@ -17,10 +20,24 @@ def get_configs(): return confs +# Get Surok App configuration +def load_app_conf(app): + f = open(conf['confd'] + '/' + app) + c = json.loads(f.read()) + f.close() + + return c + + # Main loop while 1: confs = get_configs() - for i in confs: - print(i) + + for app in confs: + app_conf = load_app_conf(app) + app_hosts = resolve(app_conf, conf) + print(app_hosts) + +# gen() sleep(5) diff --git a/surok/discovery.py b/surok/discovery.py new file mode 100644 index 0000000..b43a298 --- /dev/null +++ b/surok/discovery.py @@ -0,0 +1,11 @@ +import dns.resolver + +def resolve(app, conf): + hosts = [] + domain = conf['domain'] + for rdata in dns.resolver.query('_' + app['name'] + '.' + app['group'] + '._tcp.' + domain, 'SRV'): + info = str(rdata).split() + server = { 'name': info[3], 'port': info[2] } + hosts.append(server) + + return hosts diff --git a/surok/templates.py b/surok/templates.py new file mode 100644 index 0000000..b141839 --- /dev/null +++ b/surok/templates.py @@ -0,0 +1,12 @@ +from jinja2 import Environment, PackageLoader, Template + + +# Return rendered configuration +def gen(my, jj2): + f = open(jj2) + temp = f.read() + f.close() + + template = Template(temp) + + print( template.render(my=my) )