-
-
Notifications
You must be signed in to change notification settings - Fork 69
/
deploy.py
187 lines (155 loc) · 4.61 KB
/
deploy.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import sys
import os
import socket
APP_NAME = 'poetry-generator'
APP_URL = 'www.poetrygenerator.ninja'
APP_PORT = 8002
try:
print "working to " + sys.argv[1]
except:
print 'usage: sudo python setup.py [install|deploy|uninstall]'
sys.exit()
## CONFIGURATION ##
mainIP = [(s.connect(('8.8.8.8', 80)), s.getsockname()[0], s.close()) for s in [socket.socket(socket.AF_INET, socket.SOCK_DGRAM)]][0][1]
curDir = os.getcwd()
init_conf = """
# This should be placed in /etc/init.d directory
# start with
# sudo /etc/init.d/%(appname)s start
# stop with
# sudo /etc/init.d/%(appname)s start
dir="%(dir)s"
user="nobody"
cmd="%(dir)s/bin/gunicorn -b %(ip)s:%(port)s -w 4 server:application"
name=`basename $0`
pid_file="/var/run/$name.pid"
stdout_log="/var/log/$name.log"
stderr_log="/var/log/$name.err"
get_pid() {
cat "$pid_file"
}
is_running() {
[ -f "$pid_file" ] && ps `get_pid` > /dev/null 2>&1
}
case "$1" in
start)
if is_running; then
echo "Already started"
else
echo "Starting $name"
cd "$dir"
sudo -u "$user" $cmd >> "$stdout_log" 2>> "$stderr_log" &
echo $! > "$pid_file"
if ! is_running; then
echo "Unable to start, see $stdout_log and $stderr_log"
exit 1
fi
fi
;;
stop)
if is_running; then
echo -n "Stopping $name.."
kill `get_pid`
for i in {1..10}
do
if ! is_running; then
break
fi
echo -n "."
sleep 1
done
echo
if is_running; then
echo "Not stopped; may still be shutting down or shutdown may have failed"
exit 1
else
echo "Stopped"
if [ -f "$pid_file" ]; then
rm "$pid_file"
fi
fi
else
echo "Not running"
fi
;;
restart)
$0 stop
if is_running; then
echo "Unable to stop, will not attempt to start"
exit 1
fi
$0 start
;;
status)
if is_running; then
echo "Running"
else
echo "Stopped"
exit 1
fi
;;
*)
echo "Usage: $0 {start|stop|restart|status}"
exit 1
;;
esac
exit 0
""" % {'port':APP_PORT,'dir':curDir,'appname':APP_NAME.replace(' ',''),'ip':mainIP}
nginx_block = """
server {
# SERVER BLOCK FOR %(appname)s
listen 80; ## listen for ipv4; this line is default and implied
root %(dir)s;
index index.html index.htm;
access_log /etc/nginx/logs/access-%(appname)s.log;
error_log /etc/nginx/logs/error-%(appname)s.log;
server_name %(url)s;
location / {
proxy_pass http://%(ip)s:%(port)s;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
}
location /logs {
deny all;
}
location ~ /\.ht {
deny all;
}
}
""" % {'port':APP_PORT,'dir':curDir,'appname':APP_NAME.replace(' ',''),'url':APP_URL + ' ' + APP_URL.replace('www.',''),'ip':mainIP}
####################
if sys.argv[1]=='install' or sys.argv[1]=='deploy':
print 'installing...'
apt_get_packages = ['virtualenv']
for package in apt_get_packages:
print 'getting ' + package
os.system('apt-get install ' + package)
os.system('virtualenv ./ && . bin/activate && pip install gunicorn && deactivate')
apt_get_packages = ['nginx']
for package in apt_get_packages:
print 'getting ' + package
os.system('apt-get install ' + package)
print 'generating init.d configuration file'
with open('/etc/init.d/'+APP_NAME,'w') as f:
f.write(init_conf)
os.system('chmod +x /etc/init.d/'+APP_NAME)
print "starting server..."
os.system('/etc/init.d/' + APP_NAME + ' restart')
print 'generating nginx server block'
with open('/etc/nginx/sites-available/'+APP_NAME,'w') as f:
f.write(nginx_block)
os.system('mkdir /etc/nginx/logs/')
os.system('rm /etc/nginx/sites-enabled/%(app)s' % {'app':APP_NAME})
print('ln -s /etc/nginx/sites-available/%(app)s /etc/nginx/sites-enabled/%(app)s' % {'app':APP_NAME})
os.system('ln -s /etc/nginx/sites-available/%(app)s /etc/nginx/sites-enabled/%(app)s' % {'app':APP_NAME})
os.system('/etc/init.d/nginx reload && /etc/init.d/nginx restart')
print "-"*30
print "To activate:"
print '/etc/init.d/' + APP_NAME + ' [start|stop|restart]'
print 'Currently running on ' + str(mainIP) + ':'+str(APP_PORT)
print "-"*30
else:
print 'usage: sudo python setup.py [install|deploy|uninstall]'