deploy.py 2.06 KB
Newer Older
1
2
3
import jinja2
import os
import subprocess
4
5
6
import sys

PROCESS_NAME = 'meteorologger'
7
8

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

PID_FILENAME = 'logs/pid_{}'.format(PROCESS_NAME)
14
15
16

def deploy_supervisor():

17
    with open('app/supervisor.conf') as f_temp:
18
        template = jinja2.Template(f_temp.read())
19
20
        config_file_str = template.render(base_dir=BASE_DIR,
                                          process_name=PROCESS_NAME)
21
22
23
24
25
26
27
28
29
30
31
32
33

        print('\nRegistering supervisor config at \n    {}'
              .format(SUPERVISOR_CONFIG_FILENAME))

        print('='*60)
        print(config_file_str)
        print('='*60)

        with open(SUPERVISOR_CONFIG_FILENAME, 'w') as f:
            f.write(config_file_str + '\n')

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

36
37
    proc = subprocess.Popen('supervisorctl pid {}'.format(PROCESS_NAME),
                            shell=True, stdout=subprocess.PIPE)
38
    proc.wait()
39
    pid = proc.stdout.read().decode('ascii').strip()
40

41
42
    try:
        pid = int(pid)
43

44
45
46
47
48
49
50
51
        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.")
52

53
54
55
56
57
58
59
60
61
62
63
64

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()


65
if __name__ == '__main__':
66
67
68
69
    if '-u' in sys.argv:
        undeploy_supervisor()
    else:
        deploy_supervisor()