This commit is contained in:
Difrex 2016-08-17 12:13:02 +03:00
parent 97c9e04d54
commit da0bcda185
4 changed files with 32 additions and 17 deletions

View File

@ -24,7 +24,6 @@ if args.config:
# Read config file
f = open(surok_conf, 'r')
conf = json.loads(f.read())
print(conf)
f.close()
@ -61,6 +60,13 @@ while 1:
for app in confs:
app_conf = load_app_conf(app)
# Will be removed later
# For old configs
try:
loglevel = conf['loglevel']
except:
conf['loglevel'] = 'info'
# Resolve services
app_hosts = resolve(app_conf, conf)
@ -71,7 +77,6 @@ while 1:
# Generate config from template
service_conf = gen(my, app_conf['template'])
stdout, first = reload_conf(service_conf, app_conf, first)
print(stdout)
first = reload_conf(service_conf, app_conf, first, conf)
sleep(conf['wait_time'])

View File

@ -1,6 +1,7 @@
import dns.resolver
import dns.query
from dns.exception import DNSException
import logging
# Resolve service from mesos-dns SRV record
@ -26,14 +27,14 @@ def resolve(app, conf):
group = service['group']
fqdn = '_' + service['name'] + '.' + group + '._tcp.' + domain
hosts[service['name']] = do_query(fqdn)
hosts[service['name']] = do_query(fqdn, conf['loglevel'])
return hosts
# Do SRV queries
# Return array: [{"name": "f.q.d.n", "port": 8876}]
def do_query(fqdn):
def do_query(fqdn, loglevel):
servers = []
try:
query = dns.resolver.query(fqdn, 'SRV')
@ -44,6 +45,7 @@ def do_query(fqdn):
server = {'name': info[3][:-1], 'port': info[2]}
servers.append(server)
except DNSException:
print("Could not resolve " + fqdn)
if loglevel != 'info':
logging.warning("Could not resolve " + fqdn)
return servers

View File

@ -1,8 +1,10 @@
import os
import logging
# Get old configuration
def get_old(name, service_conf):
try:
path = '/var/tmp/surok.' + name
f = open(path, 'r')
@ -16,7 +18,7 @@ def get_old(name, service_conf):
return 1
else:
return 0
def write_lock(name, service_conf):
path = '/var/tmp/surok.' + name
@ -26,7 +28,7 @@ def write_lock(name, service_conf):
def do_reload(service_conf, app_conf):
print( 'Write new configuration of ' + app_conf['conf_name'] )
logging.warning('Write new configuration of ' + app_conf['conf_name'])
f = open(app_conf['dest'], 'w')
f.write(service_conf)
@ -39,16 +41,22 @@ def do_reload(service_conf, app_conf):
return stdout
def reload_conf(service_conf, app_conf, first):
def reload_conf(service_conf, app_conf, first, conf):
# Check first loop
if first == True:
if first is True:
stdout = do_reload(service_conf, app_conf)
first = False
return stdout, first
logging.info(stdout)
return first
if get_old(app_conf['conf_name'], service_conf) != 1:
stdout = do_reload(service_conf, app_conf)
return stdout, first
logging.info(stdout)
return first
else:
return 'Same config ' + app_conf['conf_name'] + ' Skip reload', first
if conf['loglevel'] == 'debug':
logging.debug('Same config ' +
app_conf['conf_name'] +
' Skip reload')
return first

View File

@ -1,8 +1,8 @@
from jinja2 import Environment, PackageLoader, Template
from jinja2 import Template
import os
# Return rendered configuration
# Return rendered configuration
def gen(my, jj2):
f = open(jj2, 'r')
temp = f.read()