-
Notifications
You must be signed in to change notification settings - Fork 1
/
control.py
83 lines (57 loc) · 1.66 KB
/
control.py
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# -*- coding: utf-8 -*-
from __future__ import absolute_import
import os
import sys
import time
import signal
app_name = os.path.basename(os.path.abspath(os.path.join(__file__)))
def start():
try:
os.remove('./control.log')
except Exception:
pass
print('create venv ...')
if not os.path.exists('./venv'):
os.system('virtualenv --clear -p python2.7 ./venv >> ./control.log')
print('install required packages ...')
os.system('./venv/bin/pip install -q -r ./requirements.txt >> '
'./control.log')
print('start app ...')
os.system('nohup ./venv/bin/gunicorn -c gunicorn.py wsgi:app >> '
'./control.log 2>&1 & echo $! > %s.pid' % app_name)
def restart():
stop()
sleep_time = 0
while True:
sleep_time += 1
if sleep_time > 300:
raise Exception('wait too long')
if os.path.exists('%s.pid' % app_name):
time.sleep(1)
break
start()
def stop():
if os.path.exists('%s.pid' % app_name):
with open('%s.pid' % app_name, 'r') as f:
try:
pid = int(f.read())
print('pid is %d' % pid)
os.kill(pid, signal.SIGTERM)
except OSError as e:
if e.errno == 3:
print('no such process')
print(e)
else:
print('pid file not exist')
if len(sys.argv) != 2:
print('only accept start/restart/stop as argument')
exit()
flag = sys.argv[1]
if 'start' == flag:
start()
elif 'restart' == flag:
restart()
elif 'stop' == flag:
stop()
else:
print('only accept start/restart/stop as argument')