Working discovery
This commit is contained in:
parent
42a032b25b
commit
f6a48f9bd3
3
.gitignore
vendored
3
.gitignore
vendored
@ -1 +1,2 @@
|
||||
__pycache__/*
|
||||
surok/__pycache__
|
||||
__pycache__
|
||||
|
21
surok.py
21
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)
|
||||
|
||||
|
11
surok/discovery.py
Normal file
11
surok/discovery.py
Normal 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
12
surok/templates.py
Normal 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) )
|
Loading…
Reference in New Issue
Block a user