summaryrefslogtreecommitdiffstats
path: root/tools/scripts/config-watcher
blob: fda69be5720db31770d5534888a98088f4f723c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env python3

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():
    settings_file = "/etc/tower/settings.py"
    hash_file = "/var/lib/awx/.configsha"

    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:
            current_hash = hash(settings_file)
        except:
            sys.stderr.write("Could not open settings.py, skipping config watcher")
            childutils.listener.ok(sys.stdout)
            continue
        try:
            if current_hash == last_hash(hash_file):
                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")

        write_hash(hash_file, current_hash)
        childutils.listener.ok(sys.stdout)

if __name__ == '__main__':
    main()