Working discovery

This commit is contained in:
Difrex 2016-08-01 14:56:35 +03:00
parent 42a032b25b
commit f6a48f9bd3
4 changed files with 44 additions and 3 deletions

3
.gitignore vendored
View File

@ -1 +1,2 @@
__pycache__/*
surok/__pycache__
__pycache__

View File

@ -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)

11
surok/discovery.py Normal file
View File

@ -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

12
surok/templates.py Normal file
View File

@ -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) )