Reload if config is changed

This commit is contained in:
Difrex 2016-08-01 16:09:07 +03:00
parent 564e851f66
commit dda33a2eb5
2 changed files with 41 additions and 7 deletions

View File

@ -39,8 +39,8 @@ while 1:
my = {'app': app_conf['name'], 'hosts': app_hosts} my = {'app': app_conf['name'], 'hosts': app_hosts}
service_conf = gen(my, app_conf['template']) service_conf = gen(my, app_conf['template'])
reload_conf(service_conf, app_conf) print(reload_conf(service_conf, app_conf))
sleep(5) sleep(conf['wait_time'])

View File

@ -1,10 +1,44 @@
import os import os
def reload_conf(service_conf, app_conf):
f = open(app_conf['dest'], 'w') def get_old(name, service_conf):
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
def write_lock(name, service_conf):
path = '/var/tmp/surok.' + name
f = open(path, 'w')
f.write(service_conf) f.write(service_conf)
f.close() f.close()
# Reload conf
stdout = os.popopen(app_conf['reload_cmd']).read() def reload_conf(service_conf, app_conf):
return stdout
# Check old config
if get_old(app_conf['name'], service_conf) != 1:
print('Write new configuration')
f = open(app_conf['dest'], 'w')
f.write(service_conf)
f.close()
write_lock(app_conf['name'], service_conf)
# Reload conf
stdout = os.popen(app_conf['reload_cmd']).read()
return stdout
else:
return 'Same config. Skip reload'