deploy.py 2.15 KB
Newer Older
1 2
import os
import subprocess
3 4 5
import sys

PROCESS_NAME = 'meteorologger'
6 7

BASE_DIR = os.path.abspath(os.path.dirname(__file__))
8
BASE_DIR = BASE_DIR[:BASE_DIR.rfind(os.path.sep)]
9 10 11
SUPERVISOR_CONFIG_FILENAME = '/etc/supervisor/conf.d/{}.conf'\
                             .format(PROCESS_NAME)

Nelso Jost's avatar
Nelso Jost committed
12
PID_FILENAME = 'logger/logs/pid_{}'.format(PROCESS_NAME)
13

14 15 16 17 18 19 20 21 22 23 24 25
TEMPLATE_SUPERVISOR_CONF =\
"""\
[program:{PROCESS_NAME}]
command={BASE_DIR}/.venv/bin/python3 {BASE_DIR}/logger/run.py -s
directory={BASE_DIR}
user=root
autostart=true
autorestart=true
redirect_stderr=true
stdout_logfile={BASE_DIR}/logger/logs/stdout.log
logfile={BASE_DIR}/logger/logs/supervisor-{PROCESS_NAME}.log
""".format(**globals())
26

27
def deploy_supervisor():
28

29 30 31
    print('\nRegistering supervisor config at \n    {}\n'
          .format(SUPERVISOR_CONFIG_FILENAME) + '='*60 + '\n'
          + TEMPLATE_SUPERVISOR_CONF + '='*60)
32

33 34
    with open(SUPERVISOR_CONFIG_FILENAME, 'w') as f:
        f.write(TEMPLATE_SUPERVISOR_CONF + '\n')
35 36 37

    print('\nRestarting supervisor..')
    proc = subprocess.Popen('supervisorctl update', shell=True)
38
    proc.wait()
39

40 41
    proc = subprocess.Popen('supervisorctl pid {}'.format(PROCESS_NAME),
                            shell=True, stdout=subprocess.PIPE)
42
    proc.wait()
43
    pid = proc.stdout.read().decode('ascii').strip()
44

45 46
    try:
        pid = int(pid)
47

48 49 50 51 52 53 54 55
        with open(PID_FILENAME, 'w') as f:
            f.write(str(pid) + '\n')

        print("\nPID: {} (saved at '{}')".format(pid, PID_FILENAME))
        print('\n[{} process is running]'.format(PROCESS_NAME))
        print('\nYou can manage it with supervisorctl tool.')
    except:
        print("\nSomething went wrong and the daemon process was NOT created.")
56

57 58 59 60 61 62 63 64 65 66 67 68

def undeploy_supervisor():
    print('\nRemoving supervisor config file at\n   {}'
          .format(SUPERVISOR_CONFIG_FILENAME))
    os.system('rm -f {}'.format(SUPERVISOR_CONFIG_FILENAME))
    os.system('rm -f {}'.format(PID_FILENAME))

    print('\nRestarting supervisor..')
    proc = subprocess.Popen('supervisorctl update', shell=True)
    proc.wait()


69
if __name__ == '__main__':
70 71 72 73
    if '-u' in sys.argv:
        undeploy_supervisor()
    else:
        deploy_supervisor()