From c7a18267ccd16d2bca2ca040a26dc3ffe6e611f1 Mon Sep 17 00:00:00 2001 From: Difrex Date: Sun, 6 Nov 2016 23:03:39 +0300 Subject: [PATCH] Memcache support. !!! NOT TESTED !!! --- debian/changelog | 9 +++++++++ debian/control | 2 +- surok/system.py | 32 +++++++++++++++++++++++++++----- 3 files changed, 37 insertions(+), 6 deletions(-) diff --git a/debian/changelog b/debian/changelog index 09b2806..62cf5d1 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,3 +1,12 @@ +surok (0.7.4~rc1) testing; urgency=medium + + * !!! WARNING !!! NOT TESTED + * Memcache support + * Some fixes + * New dependencies + + -- Denis Zheleztsov Sun, 06 Nov 2016 23:01:05 +0300 + surok (0.7-3) jessie; urgency=medium * Small fix in marathon restart diff --git a/debian/control b/debian/control index 2cba53d..51e217a 100644 --- a/debian/control +++ b/debian/control @@ -8,5 +8,5 @@ Vcs-Git: https://github.com/Difrex/surok Package: surok Architecture: all -Depends: python3-jinja2, python3-dnsq, python3-requests +Depends: python3-jinja2, python3-dnsq, python3-requests, python3-memcache Description: Service discovery for Apache Mesos clusters diff --git a/surok/system.py b/surok/system.py index c7c1364..a1906d5 100644 --- a/surok/system.py +++ b/surok/system.py @@ -24,9 +24,25 @@ def get_old(name, service_conf): # Get old discovered servers from memcache -def get_old_from_memcache(mc, name, service_conf): +def get_old_from_memcache(mc, name, app_hosts): mc_servers_key = 'surok_' + name + '_servers' + new_servers = [] old_servers = mc.get(mc_servers_key) + for service in app_hosts: + for server in app_hosts[service]: + new_servers.append(server['name'] + ':' + server['port']) + + for server in new_servers: + if server not in old_servers: + write_confs_to_memcache(mc, new_servers, mc_servers_key) + return 0 + + return 1 + + +# Write to memcache +def write_confs_to_memcache(mc, servers, key): + mc.set(key, servers) def write_lock(name, service_conf): @@ -50,6 +66,7 @@ def do_reload(service_conf, app_conf): return stdout +# !!! NEED REFACTORING !!! def reload_conf(service_conf, app_conf, conf, app_hosts): # Check first loop @@ -61,7 +78,8 @@ def reload_conf(service_conf, app_conf, conf, app_hosts): # Check marathon enabled in configuration if conf['marathon']['enabled'] is True: - restart_self_in_marathon(conf['marathon']) + if get_old(app_conf['conf_name'], service_conf) != 1: + restart_self_in_marathon(conf['marathon']) # Check memcache # Need rewriting @@ -73,7 +91,11 @@ def reload_conf(service_conf, app_conf, conf, app_hosts): logging.info('Discovery of Memcached not implemented') mc = memcache.Client(conf['memcached']['hosts']) - get_old_from_memcache(mc, name, service_conf) + if get_old_from_memcache(mc, app_conf['conf_name'], service_conf) != 1: + stdout = do_reload(service_conf, app_conf) + logging.info(stdout) + return True + else: logging.warning('DEPRECATED main conf file. Please use new syntax!') # End of memcache block @@ -82,13 +104,13 @@ def reload_conf(service_conf, app_conf, conf, app_hosts): if get_old(app_conf['conf_name'], service_conf) != 1: stdout = do_reload(service_conf, app_conf) logging.info(stdout) - return + return True else: if conf['loglevel'] == 'debug': logging.debug('Same config ' + app_conf['conf_name'] + ' Skip reload') - return + return False # Do POST request to marathon API