2019-10-30 21:57:56 +03:00
#!/usr/bin/env python3
2017-10-12 21:14:30 +03:00
import os
import sys
import hashlib
from supervisor import childutils
def hash(f):
s = hashlib.sha1()
with open(f, "rb") as fd:
for chunk in iter(lambda: fd.read(4096), b""):
s.update(chunk)
return s.hexdigest()
def last_hash(f):
with open(f, "r") as fd:
return fd.read().strip()
def write_hash(f, h):
with open(f, "w") as fd:
fd.write(h)
def main():
2018-03-01 23:17:15 +03:00
settings_file = "/etc/tower/settings.py"
hash_file = "/var/lib/awx/.configsha"
2017-10-12 21:14:30 +03:00
while 1:
rpc = childutils.getRPCInterface(os.environ)
headers, payload = childutils.listener.wait(sys.stdin, sys.stdout)
if not headers['eventname'].startswith('TICK'):
childutils.listener.ok(sys.stdout)
continue
try:
2018-03-01 23:17:15 +03:00
current_hash = hash(settings_file)
2017-10-12 21:14:30 +03:00
except:
sys.stderr.write("Could not open settings.py, skipping config watcher")
childutils.listener.ok(sys.stdout)
continue
try:
2018-03-01 23:17:15 +03:00
if current_hash == last_hash(hash_file):
2017-10-12 21:14:30 +03:00
childutils.listener.ok(sys.stdout)
continue
else:
sys.stderr.write("Config changed, reloading services")
for proc in rpc.supervisor.getAllProcessInfo():
group = proc['group']
name = proc['name']
program = "{}:{}".format(group, name)
if group == "tower-processes":
sys.stderr.write('Restarting %s\n' % program)
rpc.supervisor.stopProcess(program)
rpc.supervisor.startProcess(program)
except:
sys.stderr.write("No previous hash found")
2018-03-01 23:17:15 +03:00
write_hash(hash_file, current_hash)
2017-10-12 21:14:30 +03:00
childutils.listener.ok(sys.stdout)
2018-03-01 23:17:15 +03:00
if __name__ == '__main__':
main()