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

View File

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

View File

@ -1,8 +1,10 @@
import os import os
import logging
# Get old configuration
def get_old(name, service_conf): def get_old(name, service_conf):
try: try:
path = '/var/tmp/surok.' + name path = '/var/tmp/surok.' + name
f = open(path, 'r') f = open(path, 'r')
@ -16,7 +18,7 @@ def get_old(name, service_conf):
return 1 return 1
else: else:
return 0 return 0
def write_lock(name, service_conf): def write_lock(name, service_conf):
path = '/var/tmp/surok.' + name path = '/var/tmp/surok.' + name
@ -26,7 +28,7 @@ def write_lock(name, service_conf):
def do_reload(service_conf, app_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 = open(app_conf['dest'], 'w')
f.write(service_conf) f.write(service_conf)
@ -39,16 +41,22 @@ def do_reload(service_conf, app_conf):
return stdout return stdout
def reload_conf(service_conf, app_conf, first): def reload_conf(service_conf, app_conf, first, conf):
# Check first loop # Check first loop
if first == True: if first is True:
stdout = do_reload(service_conf, app_conf) stdout = do_reload(service_conf, app_conf)
first = False first = False
return stdout, first logging.info(stdout)
return first
if get_old(app_conf['conf_name'], service_conf) != 1: if get_old(app_conf['conf_name'], service_conf) != 1:
stdout = do_reload(service_conf, app_conf) stdout = do_reload(service_conf, app_conf)
return stdout, first logging.info(stdout)
return first
else: 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 import os
# Return rendered configuration # Return rendered configuration
def gen(my, jj2): def gen(my, jj2):
f = open(jj2, 'r') f = open(jj2, 'r')
temp = f.read() temp = f.read()