-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathfabfile.py
More file actions
365 lines (284 loc) · 12 KB
/
fabfile.py
File metadata and controls
365 lines (284 loc) · 12 KB
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
import os
from fabric.api import *
from unipath import Path
from contextlib import contextmanager
from fabric.contrib.files import exists
from fabric.api import settings, hosts
from fabric.operations import local
from fabric.context_managers import lcd
class FabricException(Exception):
pass
PROJECT = 'vision'
LOCAL_ROOT_DIR = Path(__file__).ancestor(2)
LOCAL_PROJECT_DIR = Path(LOCAL_ROOT_DIR, 'project')
env.directory = Path('/home', PROJECT, 'www')
env.venv = env.directory + '/env'
env.activate = 'source ' + env.venv + '/bin/activate'
env.pip = env.venv + '/bin/pip'
env.python = env.venv + '/bin/python'
env.home = '/home/' + PROJECT
# bbflask
env.bbenv = env.directory + '/bbenv'
env.bbactivate = 'source ' + env.bbenv + '/bin/activate'
env.bbpip = env.bbenv + '/bin/pip'
env.bbpython = env.bbenv + '/bin/python'
env.redis_conf = Path(LOCAL_PROJECT_DIR, "dep", "redis", "redis.conf")
@contextmanager
def source_virtualenv():
with prefix(env.activate):
yield
@contextmanager
def source_bb_virtualenv():
with prefix(env.bbactivate):
yield
def setup_dev():
sudo("mkdir -p %s/var/logs" % env.directory, user="vision")
sudo("mkdir -p %s/var/uploads" % env.directory, user="vision")
sudo("chmod -R g+wrx %s/var/logs" % env.directory, user="vision")
sudo('apt-get install -y git')
sudo('apt-get install -y postgresql-server-dev-9.3 python-dev libjpeg-dev')
sudo('apt-get install -y libpcre3 libpcre3-dev')
sudo('apt-get install -y nginx')
sudo('apt-get install -y postgresql')
sudo('apt-get install -y supervisor')
sudo('apt-get install -y python-pip')
sudo("pip install virtualenv")
sudo("pip install gunicorn")
with settings(abort_exception=FabricException):
try:
sudo("rm -rf %s" % env.venv)
except FabricException:
pass
sudo("mkdir -p %s" % env.directory, user="vision")
deploy_nginx()
deploy_supervisor()
setup_dev_app()
setup_blogging()
setup_flaskbb()
setup_celery()
restart_services()
deploy()
def restart_services():
sudo("service supervisor stop")
sudo('service redis-server stop')
sudo("service nginx stop")
with settings(abort_exception=FabricException):
try:
sudo('killall uwsgi')
except FabricException:
pass
sudo("service nginx reload")
sudo("service nginx start")
sudo("service supervisor force-reload")
sudo("service supervisor start")
sudo('service redis-server start', pty=False)
def deploy_dev_image():
# create vagrant box
nginx_ppth = "/home/%s/conf" % PROJECT
box = os.path.join(LOCAL_PROJECT_DIR, 'package.box')
remotebox = Path(env.home, 'www', 'app', 'devbox')
sudo("rm -rf %s" % remotebox, user="vision")
sudo("mkdir -p %s" % remotebox, user="vision")
sudo("mkdir -p %s" % nginx_ppth, user="vision")
local(
'cd %s && vagrant halt && vagrant package --base "vision_development" '
% LOCAL_PROJECT_DIR
)
local('cd %s && vagrant up' % LOCAL_PROJECT_DIR)
put(box, os.path.join(remotebox, 'package.box'))
local('rm -f %s' % box)
def deploy_supervisor():
super_conf = "/etc/supervisor/conf.d/%s.conf" % PROJECT
sudo("rm -f %s" % super_conf)
put(Path(LOCAL_PROJECT_DIR, 'dep', 'supervisor', 'template.conf'), super_conf, use_sudo=True)
sudo("chown root:root %s" % super_conf)
restart_services()
def deploy_nginx():
nginx_conf = "/etc/nginx/sites-available/%s.conf" % PROJECT
sudo("rm -f %s" % nginx_conf)
put(Path(LOCAL_PROJECT_DIR, 'dep', 'nginx', 'template.conf'), nginx_conf, use_sudo=True)
sudo("chown root:root %s" % nginx_conf)
# recreate symlink
sudo("rm -f /etc/nginx/sites-enabled/%s.conf" % PROJECT)
sudo("ln -s %s /etc/nginx/sites-enabled/%s.conf" %
(nginx_conf, PROJECT))
sudo("service nginx restart")
def setup_dev_app():
with cd(env.directory):
local('git pull origin master')
run('virtualenv env --always-copy')
with source_virtualenv():
run(env.pip + ' install uwsgi')
run(env.pip + ' install -r requirements.txt')
with cd(env.directory):
run('cp config.py.dist config.py')
with source_virtualenv():
run('find . -name "*.pyc" -exec rm -rf {} \;')
# run('python manage.py db init')
run('python manage.py db upgrade')
def provision():
with cd(env.directory):
with source_virtualenv():
run('python manage.py db migrate')
def first_deploy():
if 'vagrant' in env.user:
local('cd %s && vagrant up' % LOCAL_PROJECT_DIR)
with cd(env.directory):
run('git pull origin master')
sudo("rm -f %s/config.py" % env.directory, user="vision")
sudo("cp %s/config.py.dist %s/config.py" % (env.directory, env.directory), user="vision")
with source_virtualenv():
nginx_conf = "/etc/nginx/sites-available/%s.conf" % PROJECT
flaskbb_conf = "/etc/supervisor/conf.d/flaskbb.conf"
flaskbb_xml = "%s/flask_bb.xml" % PROJECT
vision_xml = "%s/vision.xml" % PROJECT
uwsgi_local = LOCAL_PROJECT_DIR.child('dep').child('uwsgi')
sudo("rm -f %s/vision.xml" % (env.directory))
sudo("rm -f %s/flask_bb.xml" % (env.directory))
put(Path(uwsgi_local, "flask_bb.xml"), "%s/flask_bb.xml" % env.directory)
put(Path(uwsgi_local, "vision.xml"), "%s/vision.xml" % env.directory)
sudo("rm -f %s" % nginx_conf)
sudo("rm -f %s" % flaskbb_conf)
put(Path(LOCAL_PROJECT_DIR, 'dep', 'nginx', 'template.conf'), nginx_conf, use_sudo=True)
put(Path(LOCAL_PROJECT_DIR, 'dep', 'supervisor', 'flaskbb.conf'), flaskbb_conf, use_sudo=True)
run(env.pip + ' install -r requirements.txt')
setup_trans()
update_trans()
compile_trans()
update_flaskbb()
run('find . -name "*.pyc" -exec rm -rf {} \;')
run('python -c "from app import db;db.create_all()"')
sudo('service supervisor stop')
if 'vagrant' not in env.user:
sudo('service apache2 stop')
deploy_supervisor()
restart_services()
def deploy(branch='master'):
with cd(env.directory):
run('git reset --hard origin/{}'.format(branch))
with source_virtualenv():
run(env.pip + ' install -r requirements.txt')
update_trans()
compile_trans()
run('find . -name "*.pyc" -exec rm -rf {} \;')
# run('python -c "from app import db;db.create_all()"')
run('python manage.py db upgrade')
restart_services()
def update_local():
with cd(env.directory):
with source_virtualenv():
run(env.pip + ' install -r requirements.txt')
run('python manage.py db upgrade')
update_static()
restart_services()
def update_flaskbb():
with cd(env.directory):
with source_virtualenv():
with cd('./flaskbb'):
flaskbb_dir = env.directory + '/flaskbb/flaskbb/configs'
# sudo('apt-get install -y uwsgi-plugin-python')
run(env.bbpip + ' install uwsgi')
run(env.bbpip + ' install -r requirements.txt')
def update_remote(branch='master'):
sudo('service supervisor stop')
with cd(env.directory):
run('git pull origin %s' % branch)
with source_virtualenv():
run(env.pip + ' install -r requirements.txt')
run('find . -name "*.pyc" -exec rm -rf {} \;')
# run('python -c "from app import db;db.create_all()"')
run('python manage.py db upgrade')
update_static()
restart_services()
def setup_redis():
sudo("apt-get install -y redis-server")
put(env.redis_conf, '/etc/redis/redis.conf', use_sudo=True)
sudo("update-rc.d redis-server defaults")
sudo("service redis-server start", pty=False)
def setup_trans():
with cd(env.directory):
with source_virtualenv():
run('./env/bin/pybabel extract -F babel.cfg -o app/messages.pot app')
run('./env/bin/pybabel init -i app/messages.pot -d app/translations -l es')
run('./env/bin/pybabel init -i app/messages.pot -d app/translations -l fr')
def compile_trans():
with cd(env.directory):
with source_virtualenv():
run('./env/bin/pybabel compile -f -d app/translations')
def update_trans():
with cd(env.directory):
with source_virtualenv():
run('pybabel update -i app/messages.pot -d app/translations')
run('rm -f ./messages.pot')
def create_root():
with cd(env.directory):
with source_virtualenv():
run(
'python -c "from app import db;from app.tree.models import TreeNode;node = TreeNode(text = u\'Vision Diagnostic\' , disabled=True , selected=True , icon=\'../app/static/img/root.png\' , type=\'default\');db.session.add(node);db.session.commit()"')
def setup_blogging():
with cd(env.directory):
with source_virtualenv():
run('rm -rf Flask-Blogging')
run('git clone https://github.com/optimum-web/Flask-Blogging.git Flask-Blogging')
with cd('Flask-Blogging'):
run('git fetch && git pull origin master')
run('python setup.py install')
def setup_flaskbb():
setup_redis()
sudo("apt-get install -y uwsgi uwsgi-plugin-python ")
FLASKBB_DIR = env.directory + '/flaskbb'
with cd(env.directory):
run('rm -rf flaskbb')
run('git clone https://github.com/sh4nks/flaskbb.git')
run('virtualenv ' + env.bbenv)
with source_bb_virtualenv():
run('rm -f ' + FLASKBB_DIR + '/configs/production.py')
put(Path(LOCAL_ROOT_DIR, 'flaskbb', 'production.py'), FLASKBB_DIR + '/flaskbb/configs')
put(Path(LOCAL_ROOT_DIR, 'flaskbb', 'templates', 'navigation.html'), FLASKBB_DIR + '/flaskbb/templates')
with cd('./flaskbb'):
run(env.bbpip + ' install -r requirements.txt')
run(env.bbpython + ' %s/manage.py initdb' % FLASKBB_DIR)
run(env.bbpython + ' %s/manage.py populate' % FLASKBB_DIR)
def setup_api():
uwsgi_local = LOCAL_PROJECT_DIR.child('dep').child('uwsgi')
put(Path(uwsgi_local, "vision-api.xml"), "%s/vision-api.xml" % env.directory)
deploy_supervisor()
deploy_nginx()
restart_services()
def menu_root():
with cd(env.directory):
with source_virtualenv():
run(
'python -c "from app import db;from app.admin.models import MenuItemsNode;node = MenuItemsNode(text = u\'Vision Diagnostic\', disabled = True, selected = True, type = \'parent\' );top_node = MenuItemsNode( text = u\'Top Menu\' , parent = node , disabled = True, selected = True , type = \'parent\' );db.session.add(node);db.session.commit()"'
)
def setup_static():
sudo('apt-get -y install npm')
sudo('npm install npm -g')
sudo('apt-get install nodejs-legacy')
static = Path(env.directory, 'app', 'static')
with cd(static):
run('npm install')
run('npm start')
def update_nodejs():
sudo('npm cache clean -f')
sudo('npm install -g n')
sudo('n stable')
sudo('ln -sf /usr/local/n/versions/node/{}/bin/node /usr/bin/node '.format('6.4.0'))
def update_static():
static = Path(env.directory, 'app', 'static')
with cd(static):
run('npm install')
run('npm start')
def generate_apidoc():
run('apidoc -i /home/vision/www/app -f api_doc.py --output /home/vision/www/app/static/docs')
def test():
with lcd('tests'):
# run('python -m unittest test_unittest')
# run('python -m unittest test_unittest.TestStringMethods')
# run('python -m unittest test_unittest.TestStringMethods.test_upper')
local('python test_api.py')
def setup_celery():
celery_conf = "/etc/supervisor/conf.d/send_mail_worker.conf"
sudo('service supervisor stop')
put(Path(LOCAL_PROJECT_DIR, 'dep', 'supervisor', 'send_mail_worker.conf'), celery_conf, use_sudo=True)
restart_services()