From da0bcda185835803f759046dce953747525fd4f7 Mon Sep 17 00:00:00 2001 From: Difrex Date: Wed, 17 Aug 2016 12:13:02 +0300 Subject: [PATCH] Logging --- surok.py | 11 ++++++++--- surok/discovery.py | 8 +++++--- surok/system.py | 26 +++++++++++++++++--------- surok/templates.py | 4 ++-- 4 files changed, 32 insertions(+), 17 deletions(-) diff --git a/surok.py b/surok.py index f7657b9..4bea4d7 100755 --- a/surok.py +++ b/surok.py @@ -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']) diff --git a/surok/discovery.py b/surok/discovery.py index 36eb77a..3ad583a 100644 --- a/surok/discovery.py +++ b/surok/discovery.py @@ -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 diff --git a/surok/system.py b/surok/system.py index 87ca63c..13b4bd6 100644 --- a/surok/system.py +++ b/surok/system.py @@ -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 diff --git a/surok/templates.py b/surok/templates.py index e504898..c7a1ad4 100644 --- a/surok/templates.py +++ b/surok/templates.py @@ -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()