surok/surok/system.py

127 lines
3.6 KiB
Python
Raw Normal View History

2016-08-01 15:24:13 +03:00
import os
2016-10-14 11:50:16 +03:00
import sys
2016-08-17 12:13:02 +03:00
import logging
2016-10-14 11:25:53 +03:00
import requests
2016-11-06 22:24:26 +03:00
import memcache
2016-08-01 15:24:13 +03:00
2016-08-01 16:09:07 +03:00
2016-08-17 12:13:02 +03:00
# Get old configuration
2016-08-01 16:09:07 +03:00
def get_old(name, service_conf):
2016-08-17 12:13:02 +03:00
2016-08-01 16:09:07 +03:00
try:
path = '/var/tmp/surok.' + name
f = open(path, 'r')
old = f.read()
f.close()
except Exception as e:
print(str(e))
return 0
if old == service_conf:
return 1
else:
return 0
2016-08-17 12:13:02 +03:00
2016-08-01 16:09:07 +03:00
2016-11-06 22:24:26 +03:00
# Get old discovered servers from memcache
2016-11-06 23:03:39 +03:00
def get_old_from_memcache(mc, name, app_hosts):
2016-11-06 22:24:26 +03:00
mc_servers_key = 'surok_' + name + '_servers'
2016-11-06 23:03:39 +03:00
new_servers = []
2016-11-06 22:24:26 +03:00
old_servers = mc.get(mc_servers_key)
2016-11-06 23:03:39 +03:00
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)
2016-11-06 22:24:26 +03:00
2016-08-01 16:09:07 +03:00
def write_lock(name, service_conf):
path = '/var/tmp/surok.' + name
f = open(path, 'w')
2016-08-01 15:24:13 +03:00
f.write(service_conf)
f.close()
2016-08-01 16:09:07 +03:00
def do_reload(service_conf, app_conf):
2016-08-17 12:13:02 +03:00
logging.warning('Write new configuration of ' + app_conf['conf_name'])
2016-08-01 16:09:07 +03:00
f = open(app_conf['dest'], 'w')
f.write(service_conf)
f.close()
2016-08-01 16:09:07 +03:00
write_lock(app_conf['conf_name'], service_conf)
2016-08-01 16:09:07 +03:00
# Reload conf
stdout = os.popen(app_conf['reload_cmd']).read()
return stdout
2016-11-06 23:03:39 +03:00
# !!! NEED REFACTORING !!!
2016-11-06 22:24:26 +03:00
def reload_conf(service_conf, app_conf, conf, app_hosts):
2016-10-14 11:25:53 +03:00
# Check marathon enabled in configuration
if conf['marathon']['enabled'] is True:
2016-11-06 23:03:39 +03:00
if get_old(app_conf['conf_name'], service_conf) != 1:
restart_self_in_marathon(conf['marathon'])
2016-11-06 22:24:26 +03:00
# Check memcache
# Need rewriting
################
if 'memcached' in conf:
if conf['memcached']['enabled'] is True:
# Check old servers
if conf['memcached']['discovery']['enabled'] is True:
2016-11-09 15:31:57 +03:00
logging.warning('Discovery of Memcached not implpemented')
try:
mc = memcache.Client(conf['memcached']['hosts'])
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
except Exception as e:
logging.error('Cannot connect to memcached: ' + str(e))
2016-11-06 22:24:26 +03:00
else:
logging.warning('DEPRECATED main conf file. Please use new syntax!')
2016-11-09 15:31:57 +03:00
# End of memcache block
#######################
if get_old(app_conf['conf_name'], service_conf) != 1:
stdout = do_reload(service_conf, app_conf)
2016-08-17 12:13:02 +03:00
logging.info(stdout)
2016-11-06 23:03:39 +03:00
return True
2016-08-01 16:09:07 +03:00
else:
2016-08-17 12:13:02 +03:00
if conf['loglevel'] == 'debug':
logging.debug('Same config ' +
app_conf['conf_name'] +
' Skip reload')
2016-11-06 23:03:39 +03:00
return False
2016-10-14 11:25:53 +03:00
# Do POST request to marathon API
# /v2/apps//app/name/restart
def restart_self_in_marathon(marathon):
host = marathon['host']
# Check MARATHON_APP_ID environment varible
if os.environ.get('MARATHON_APP_ID') is not True:
logging.error('Cannot find MARATHON_APP_ID. Not in Mesos?')
sys.exit(2)
2016-11-09 15:31:57 +03:00
app_id = os.environ['MARATHON_APP_ID']
uri = 'http://' + host + '/v2/apps/' + app_id + '/restart'
2016-10-14 11:25:53 +03:00
# Ok. In this step we made restart request to Marathon
if marathon['force'] is True:
r = requests.post(uri, data = {'force': 'true'})
else:
2016-10-14 12:41:19 +03:00
r = requests.post(uri, data = {'force': 'false'})