forked from dnephin/assembl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fabfile.py
1160 lines (968 loc) · 34.7 KB
/
fabfile.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
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/env python
# -*- coding:utf-8 -*-
from __future__ import with_statement
from os import getenv
from platform import system
from time import sleep, strftime
import pipes
from ConfigParser import ConfigParser, NoOptionError
from StringIO import StringIO
# Importing the "safe" os.path commands
from os.path import join, dirname, split, normpath
# Other calls to os.path rarely mostly don't work remotely. Use locally only.
import os.path
import fabric.operations
from fabric.operations import put, get
from fabric.api import *
from fabric.colors import cyan, red, green
from fabric.contrib.files import *
def realpath(path):
return run("python -c 'import os,sys;print os.path.realpath(sys.argv[1])' "+path)
def is_file(path):
return run("test -f "+path, quiet=True).succeeded
def getmtime(path):
if env.mac:
return int(run("/usr/bin/stat -f '%m' "+path))
else:
return int(run("/usr/bin/stat -c '%Y' "+path))
def listdir(path):
return run("ls "+path).split()
@task
def database_start():
"""
Makes sure the database server is running
"""
execute(supervisor_process_start, 'virtuoso')
@task
def supervisor_restart():
with hide('running', 'stdout'):
supervisord_cmd_result = venvcmd("supervisorctl shutdown")
#Another supervisor,upstart, etc may be watching it, give it a little while
sleep(2);
#If supervisor is already started, this will do nothing
execute(supervisor_process_start, 'virtuoso')
def supervisor_process_start(process_name):
"""
Starts a supervisord process, and waits till it started to return
"""
print(cyan('Asking supervisor to start %s' % process_name))
supervisor_pid_regex = re.compile('^\d+')
status_regex = re.compile('^%s\s*(\S*)' % process_name)
with hide('running', 'stdout'):
supervisord_cmd_result = venvcmd("supervisorctl pid")
match = supervisor_pid_regex.match(supervisord_cmd_result)
if not match:
if env.uses_global_supervisor:
print(red('Supervisord doesn\'t seem to be running, aborting'))
exit()
else:
print(red('Supervisord doesn\'t seem to be running, trying to start it'))
supervisord_cmd_result = venvcmd("supervisord")
if supervisord_cmd_result.failed:
print(red('Failed starting supervisord'))
exit()
for try_num in range(20):
with hide('running', 'stdout'):
status_cmd_result = venvcmd("supervisorctl status %s" % process_name)
match = status_regex.match(status_cmd_result)
if match:
status = match.group(1)
if(status == 'RUNNING'):
print(green("%s is running" % process_name))
break
elif(status == 'STOPPED'):
venvcmd("supervisorctl start %s" % process_name)
elif(status == 'STARTING'):
print(status)
else:
print("unexpected status: %s" % status)
sleep(1)
else:
print(red('Unable to parse status (bad regex?)'))
print(status_cmd_result)
exit()
def supervisor_process_stop(process_name):
"""
Assuming the supervisord process is running, stop one of its processes
"""
print(cyan('Asking supervisor to stop %s' % process_name))
supervisor_pid_regex = re.compile('^\d+')
status_regex = re.compile('^%s\s*(\S*)' % process_name)
with hide('running', 'stdout'):
supervisord_cmd_result = venvcmd("supervisorctl pid")
match = supervisor_pid_regex.match(supervisord_cmd_result)
if not match:
print(cyan('Supervisord doesn\'t seem to be running, nothing to stop'))
return
for try_num in range(20):
venvcmd("supervisorctl stop %s" % process_name)
with hide('running', 'stdout'):
status_cmd_result = venvcmd("supervisorctl status %s" % process_name)
match = status_regex.match(status_cmd_result)
if match:
status = match.group(1)
if(status == 'STOPPED'):
print(green("%s is stopped" % process_name))
break
elif(status == 'RUNNING'):
venvcmd("supervisorctl stop %s" % process_name)
elif(status == 'STOPPING'):
print(status)
else:
print("unexpected status: %s" % status)
sleep(1)
else:
print(red('Unable to parse status (bad regex?)'))
print(status_cmd_result)
exit()
@task
def app_reload():
"""
Restart all necessary processes after an update
"""
if env.uses_global_supervisor:
print(cyan('Asking supervisor to restart %(projectname)s' % env))
run("sudo /usr/bin/supervisorctl restart %(projectname)s" % env)
else:
venvcmd("supervisorctl restart celery_imap changes_router celery_notification_dispatch celery_notify celery_notify_beat")
if env.uses_uwsgi:
venvcmd("supervisorctl restart prod:uwsgi")
""" This will log everyone out, hopefully the code is now resilient enough
that it isn't necessary
if env.uses_memcache:
flushmemcache()
"""
def venvcmd(cmd, shell=True, user=None, pty=False):
if not user:
user = env.user
with cd(env.projectpath):
return run('source %(venvpath)s/bin/activate && ' % env + cmd, shell=shell, pty=pty)
def venv_prefix():
return 'source %(venvpath)s/bin/activate' % env
def get_db_dump_name():
return 'assembl-virtuoso-backup.bp'
def remote_db_path():
return join(env.projectpath, get_db_dump_name())
def printenv():
"""
Print shell env
"""
venvcmd('env')
## Virtualenv
@task
def build_virtualenv():
"""
Build the virtualenv
"""
print(cyan('Creating a fresh virtualenv'))
require('venvpath', provided_by=('commonenv'))
import sys
if hasattr(sys, 'real_prefix'):
print(cyan('The virtualenv seems to already exist, so we don\'t try to create it again'))
print(cyan('(otherwise the virtualenv command would produce an error)'))
return
run('virtualenv --no-site-packages --distribute %(venvpath)s' % env)
run('rm /tmp/distribute* || echo "ok"') # clean after myself
@task
def update_requirements(force=False):
"""
update external dependencies on remote host
"""
print(cyan('Updating requirements using PIP'))
venvcmd('pip install -U "pip>=6" --download-cache ~/.pip/cache')
if force:
cmd = "%(venvpath)s/bin/pip install -I -r %(projectpath)s/requirements.txt --download-cache ~/.pip/cache" % env
else:
cmd = "%(venvpath)s/bin/pip install -r %(projectpath)s/requirements.txt --download-cache ~/.pip/cache" % env
run("yes w | %s" % cmd)
@task
def app_db_update():
"""
Migrates database using south
"""
execute(database_start)
print(cyan('Migrating database'))
venvcmd('alembic -c %s upgrade head' % (env.ini_file))
def app_db_install():
"""
Install db the first time and fake migrations
"""
execute(database_create)
print(cyan('Installing database'))
venvcmd('assembl-db-manage %s bootstrap' % (env.ini_file))
@task
def make_messages():
"""
Run *.po file generation for translation
"""
cmd = "python setup.py extract_messages"
venvcmd(cmd)
cmd = "python setup.py update_catalog"
venvcmd(cmd)
@task
def compile_messages():
"""
Run compile *.mo file from *.po
"""
cmd = "python setup.py compile_catalog"
venvcmd(cmd)
venvcmd("python assembl/scripts/po2json.py")
@task
def compile_stylesheets():
"""
Generate *.css files from *.scss
"""
execute(update_compass)
with cd(env.projectpath):
run('bundle exec compass compile --force', shell=True)
with cd('assembl/static/widget/card/app'):
run('bundle exec compass compile --force --sass-dir scss --css-dir css', shell=True)
with cd('assembl/static/widget/session'):
run('bundle exec compass compile --force --sass-dir scss --css-dir css', shell=True)
with cd('assembl/static/widget/video/app'):
run('bundle exec compass compile --force --sass-dir scss --css-dir css', shell=True)
@task
def minify_javascript_maybe():
config = get_config()
try:
minify = config.get('app:assembl', 'minified_js')
print minify
if minify == 'debug':
execute(minify_javascript_debug)
elif minify and minify != 'false':
execute(minify_javascript)
except NoOptionError:
pass
@task
def minify_javascript():
with cd(join(env.projectpath, 'assembl', 'static', 'js')):
run('node r.js -o build.js')
@task
def minify_javascript_debug():
with cd(join(env.projectpath, 'assembl', 'static', 'js')):
run('node r.js -o build_with_map.js')
def tests():
"""
Run all tests on remote
"""
print(cyan('Running TDD tests'))
venvcmd('./manage.py test')
print(cyan('Running BDD tests'))
venvcmd('./manage.py harvest --verbosity=2')
@task
def bootstrap():
"""
Creates the virtualenv and install the app from env URL
"""
execute(clone_repository)
execute(bootstrap_from_checkout)
@task
def bootstrap_from_checkout():
"""
Creates the virtualenv and install the app from git checkout
"""
execute(updatemaincode)
execute(build_virtualenv)
execute(app_compile_nodbupdate)
execute(app_db_install)
execute(app_reload)
execute(webservers_reload)
def clone_repository():
"""
Clone repository
"""
print(cyan('Cloning Git repository'))
# Remove dir if necessary
if exists("%(projectpath)s/.git" % env):
abort("%(projectpath)s/.git already exists" % env)
# Clone
run("git clone --branch {0} {1} {2}".format(env.gitbranch,
env.gitrepo,
env.projectpath))
def updatemaincode():
"""
Update code and/or switch branch
"""
print(cyan('Updating Git repository'))
with cd(join(env.projectpath)):
run('git fetch')
run('git checkout %s' % env.gitbranch)
run('git pull %s %s' % (env.gitrepo, env.gitbranch))
run('git submodule update --init')
def app_setup():
venvcmd('pip install -U "pip>=6" --download-cache ~/.pip/cache')
venvcmd('pip install -e ./')
venvcmd('assembl-ini-files %s' % (env.ini_file))
@task
def app_fullupdate():
"""
Full Update: Update to latest git, update dependencies and compile app.
You need internet connectivity, and can't run this on a branch.
"""
execute(updatemaincode)
execute(app_compile)
@task
def app_update():
"""
Fast Update: Update to latest git, compile app but don't update requirements
Useful for deploying hotfixes. You need internet connectivity, and can't
run this on a branch.
"""
execute(updatemaincode)
execute(app_compile_noupdate)
@task
def app_update_dependencies():
"""
Updates all python and javascript dependencies
"""
execute(update_requirements, force=False)
execute(update_compass)
execute(update_bower)
execute(bower_install)
@task
def app_compile():
"""
Full Update: This is what you normally run after a git pull.
Doesn't touch git state, but updates requirements, rebuilds all
generated files annd restarts whatever needs restarting.
You need internet connectivity. If you are on a plane, use
app_compile_noupdate instead.
"""
execute(app_update_dependencies)
execute(app_compile_noupdate)
@task
def app_compile_noupdate():
"""
Fast Update: Doesn't touch git state, don't update requirements, and rebuild
all generated files. You normally do not need to have internet connectivity.
"""
execute(app_compile_nodbupdate)
execute(app_db_update)
# tests()
execute(app_reload)
execute(webservers_reload)
@task
def app_compile_nodbupdate():
"Separated mostly for tests, which need to run alembic manually"
execute(virtuoso_install_if_absent)
execute(app_setup)
execute(compile_stylesheets)
execute(compile_messages)
execute(minify_javascript_maybe)
@task
def webservers_reload():
"""
Reload the webserver stack.
"""
if env.uses_apache:
print(cyan("Reloading apache"))
# Apache (sudo is part of command line here because we don't have full
# sudo access
for f in apache_files:
if exists(f):
run('sudo %s reload' % (f,))
break
if env.uses_ngnix:
# Nginx (sudo is part of command line here because we don't have full
# sudo access
print(cyan("Reloading nginx"))
if exists('/etc/init.d/nginx'):
run('sudo /etc/init.d/nginx reload')
elif env.mac:
sudo('killall -HUP nginx')
def webservers_stop():
"""
Stop all webservers
"""
if env.uses_apache:
# Apache
for f in apache_files:
if exists(f):
run('sudo %s stop' % (f,))
break
if env.uses_ngnix:
# Nginx
if exists('/etc/init.d/nginx'):
sudo('/etc/init.d/nginx stop')
elif env.mac:
sudo('killall nginx')
def webservers_start():
"""
Start all webservers
"""
if env.uses_apache:
# Apache
for f in apache_files:
if exists(f):
run('sudo %s start' % (f,))
break
if env.uses_ngnix:
# Nginx
if exists('/etc/init.d/nginx'):
sudo('/etc/init.d/nginx start')
elif env.mac and exists('/usr/local/nginx/sbin/nginx'):
sudo('/usr/local/nginx/sbin/nginx')
## Server packages
def install_basetools():
"""
Install required base tools
"""
print(cyan('Installing base tools'))
if env.mac:
run('cd /tmp; curl -O https://raw.github.com/pypa/pip/master/contrib/get-pip.py')
sudo('python /tmp/get-pip.py')
sudo('pip install virtualenv --download-cache ~/.pip/cache')
else:
sudo('apt-get install -y python-virtualenv python-pip')
sudo('apt-get install -y git')
#sudo('apt-get install -y gettext')
def install_bower():
with cd(env.projectpath):
run('npm install bower po2json requirejs')
def update_bower():
with cd(env.projectpath):
run('npm update bower po2json requirejs')
def bower_cmd(cmd, relative_path='.'):
with settings(warn_only=True), hide('warnings', 'running', 'stdout', 'stderr'):
node_cmd = run('which nodejs')
if node_cmd.failed:
node_cmd = run('which node')
with cd(env.projectpath):
bower_cmd = normpath(join(
env.projectpath, 'node_modules', 'bower', 'bin', 'bower'))
po2json_cmd = normpath(join(
env.projectpath, 'node_modules', 'po2json', 'bin', 'po2json'))
if not exists(bower_cmd) or not exists(po2json_cmd):
print "Bower not present, installing..."
execute(install_bower)
with cd(relative_path):
run(' '.join((node_cmd, bower_cmd, cmd)))
@task
def bower_install():
""" Normally not called manually """
bower_cmd('install')
bower_cmd('install', 'assembl/static/widget/card')
bower_cmd('install', 'assembl/static/widget/session')
bower_cmd('install', 'assembl/static/widget/video')
bower_cmd('install', 'assembl/static/widget/vote')
bower_cmd('install', 'assembl/static/widget/creativity')
@task
def install_builddeps():
"""
Will install commonly needed build deps for pip django virtualenvs.
"""
execute(install_basetools)
print(cyan('Installing compilers and required libraries'))
if env.mac:
if not exists('/usr/local/bin/brew'):
sudo('ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go/install)"')
run('brew install memcached zeromq redis')
if not exists('/usr/local/bin/node'):
run('brew install nodejs npm')
if not exists('/usr/local/bin/autoconf'):
run('brew install autoconf')
if not exists('/usr/local/bin/automake'):
run('brew install automake')
# glibtoolize, bison, flex, gperf are on osx by default.
# brew does not know aclocal, autoheader... They exist on macports, but do we want to install that?
else:
sudo('apt-get install -y build-essential python-dev ruby-builder')
sudo('apt-get install -y nodejs nodejs-legacy npm')
sudo('apt-get install -y automake bison flex gperf libxml2-dev libssl-dev libreadline-dev')
#Runtime requirements (even in develop)
sudo('apt-get install -y redis-server memcached unixodbc-dev')
execute(update_python_package_builddeps)
@task
def update_python_package_builddeps():
print(cyan('Installing/Updating python package native binary dependencies'))
#For specific python packages in requirements.txt
if env.mac:
#I presume the runtime packages in install_builddeps come with headers on mac?
pass
else:
sudo('apt-get install -y libmemcached-dev libzmq3-dev libxslt1-dev libffi-dev phantomjs')
@task
def configure_rbenv():
execute(install_rbenv)
with cd(env.projectpath), settings(warn_only=True):
rbenv_local = run('rbenv local %(ruby_version)s' % env)
if(rbenv_local.failed):
execute(install_ruby_build)
# Install Ruby specified in env.ruby_version:
run('rbenv install %(ruby_version)s' % env)
# Rehash:
run('rbenv rehash')
run('rbenv local %(ruby_version)s' % env)
with settings(warn_only=True):
bundle_version = run('bundle --version')
if(bundle_version.failed):
#install bundler
run('gem install bundler')
run('rbenv rehash')
def install_ruby_build():
version_regex = re.compile('^ruby-build\s*(\S*)')
install = False
with settings(warn_only=True):
run_output = run('ruby-build --version')
if not run_output.failed:
match = version_regex.match(run_output)
version = float(match.group(1))
if version < env.ruby_build_min_version:
print(red("ruby-build %s is too old (%s is required), reinstalling..." % (version, env.ruby_build_min_version)))
install = True
else:
print(green("ruby-build version %s is recent enough (%s is required)" % (version, env.ruby_build_min_version)))
else:
print(red("ruby-build is not installed, installing..."))
install = True
if install:
# Install ruby-build:
run('rm -rf /tmp/ruby-build')
with cd('/tmp'):
run('git clone https://github.com/sstephenson/ruby-build.git')
with cd('/tmp/ruby-build'):
sudo('./install.sh')
@task
def install_rbenv():
"""
Install the appropriate ruby environment for compass.
"""
with settings(warn_only=True):
rbenv_failed = run('rbenv version').failed
if rbenv_failed:
with settings(warn_only=True):
rbenv_missing = run('ls ~/.rbenv').failed
if rbenv_missing:
# Install rbenv:
run('git clone https://github.com/sstephenson/rbenv.git ~/.rbenv')
# Add rbenv to the path:
run('echo \'export PATH="$HOME/.rbenv/bin:$PATH"\' >> .bash_profile')
run('echo \'eval "$(rbenv init -)"\' >> .bash_profile')
run('source ~/.bash_profile')
# The above will work fine on a shell (such as on the server accessed using
# ssh for a developement machine running a GUI, you may need to run the
# following from a shell (with your local user):
# echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.profile;
# echo 'eval "$(rbenv init -)"' >> ~/.profile;
# source ~/.profile;
run('echo \'export PATH="$HOME/.rbenv/bin:$PATH"\' >> .profile')
run('echo \'eval "$(rbenv init -)"\' >> .profile')
run('source ~/.profile')
@task
def install_compass():
"""
(Re)Install compass, deleting current version
"""
with cd(env.projectpath):
run('rm -rf vendor/bundle')
execute(update_compass)
@task
def update_compass():
"""
Make sure compass version is up to date
"""
execute(configure_rbenv)
with cd(env.projectpath):
run('bundle install --path=vendor/bundle')
@task
def start_edit_fontello_fonts():
assert env.hosts == ['localhost'], "Meant to be run locally"
try:
import requests
except ImportError:
raise RuntimeError(
"Please 'pip install requests' in your main environment")
font_dir = join(
env.projectpath, 'assembl', 'static', 'css', 'fonts')
config_file = join(font_dir, 'config.json')
id_file = join(font_dir, 'fontello.id')
r=requests.post("http://fontello.com",
files={'config': open(config_file)})
if not r.ok:
raise RuntimeError("Could not get the ID")
fid = r.text
with open(id_file, 'w') as f:
f.write(fid)
if (env.host_string == 'localhost'):
import webbrowser
webbrowser.open('http://fontello.com/'+fid)
@task
def compile_fontello_fonts():
from zipfile import ZipFile
from StringIO import StringIO
assert env.hosts == ['localhost'], "Meant to be run locally"
try:
import requests
except ImportError:
raise RuntimeError(
"Please 'pip install requests' in your main environment")
font_dir = join(
env.projectpath, 'assembl', 'static', 'css', 'fonts')
config_file = join(font_dir, 'config.json')
id_file = join(font_dir, 'fontello.id')
assert os.path.exists(id_file)
with open(id_file) as f:
fid = f.read()
r = requests.get("http://fontello.com/%s/get" % fid)
if not r.ok:
raise RuntimeError("Could not get the data")
with ZipFile(StringIO(r.content)) as data:
for name in data.namelist():
dirname, fname = split(name)
dirname, subdir = split(dirname)
if fname and (subdir == 'font' or fname == 'config.json'):
with data.open(name) as fdata:
with open(join(font_dir, fname), 'wb') as ffile:
ffile.write(fdata.read())
def database_create():
"""
"""
execute(database_start)
def virtuoso_db_directory():
return join(env.projectpath, 'var/db')
@task
def database_dump():
"""
Dumps the database on remote site
"""
if not exists(env.dbdumps_dir):
run('mkdir -m700 %s' % env.dbdumps_dir)
execute(supervisor_process_start, 'virtuoso')
filename = 'db_%s.bp' % strftime('%Y%m%d')
absolute_path = join(env.dbdumps_dir, filename)
# Dump
with prefix(venv_prefix()), cd(env.projectpath):
backup_output = venvcmd('assembl-db-manage %s backup' % (env.ini_file)
)
if backup_output.failed:
print(red('Failed virtuoso backup'))
exit()
backup_file_path = join(virtuoso_db_directory(), backup_output)
#Move to dbdumps_dir
with settings(warn_only=True):
move_result = run('mv %s %s' % (backup_file_path, absolute_path))
if move_result.failed:
print(red('Virtuoso backup did not error, but unable to move the file from %s to %s.\nYou may need to clear the file %s manually or your next backup will fail.' % (backup_file_path, absolute_path, backup_file_path)))
exit()
# Make symlink to latest
with cd(env.dbdumps_dir):
run('ln -sf %s %s' % (absolute_path, remote_db_path()))
@task
def database_download():
"""
Dumps and downloads the database from the target server
"""
destination = join('./',get_db_dump_name())
if is_link(destination):
print('Clearing symlink at %s to make way for downloaded file' % (destination))
local('rm %s' % (destination))
execute(database_dump)
get(remote_db_path(), destination)
@task
def database_upload():
"""
Uploads a local database backup to the target environment's server
"""
if(env.wsginame != 'dev.wsgi'):
put(get_db_dump_name(), remote_db_path())
@task
def database_restore():
"""
Restores the database backed up on the remote server
"""
if(env.is_production_env is True):
abort(red("You are not allowed to restore a database to a production " +
"environment. If this is a server restore situation, you " +
"have to temporarily declare env.is_production_env = False " +
"in the environment"))
env.debug = True
#if(env.wsginame != 'dev.wsgi'):
# execute(webservers_stop)
with prefix(venv_prefix()), cd(virtuoso_db_directory()):
venvcmd("supervisorctl stop virtuoso")
# Drop db
with cd(virtuoso_db_directory()), settings(warn_only=True):
run('rm -f *.db *.trx')
# Make symlink to latest
#this MUST match the code in db_manage or virtuoso will refuse to restore
restore_dump_prefix = "assembl-virtuoso-backup"
restore_dump_name = restore_dump_prefix+"1.bp"
with cd(virtuoso_db_directory()):
run('cat %s > %s' % (remote_db_path(), join(virtuoso_db_directory(), restore_dump_name)))
# Restore data
with prefix(venv_prefix()), cd(virtuoso_db_directory()):
venvcmd("supervisorctl stop virtuoso")
run("%s +configfile %s +restore-backup %s" % (
get_virtuoso_exec(), join(virtuoso_db_directory(), 'virtuoso.ini'),
restore_dump_prefix))
#clean up
with cd(virtuoso_db_directory()):
run('rm %s' % (join(virtuoso_db_directory(),restore_dump_name)))
execute(supervisor_process_start, 'virtuoso')
def get_config():
if env.get('config', None):
return env.config
ini_file = join(env.projectpath, env.ini_file)
if not exists(ini_file):
return
config_s = StringIO()
get(ini_file, config_s)
config_s.seek(0)
config = ConfigParser()
config.readfp(config_s)
env.config = config
return config
def get_virtuoso_root():
config = get_config()
assert config
vroot = config.get('virtuoso', 'virtuoso_root')
if vroot == 'system':
return '/usr/local/virtuoso-opensource' if env.mac else '/usr'
if vroot[0] != '/':
return normpath(join(env.projectpath, vroot))
return vroot
def get_virtuoso_exec():
virtuoso_exec = os.path.join(get_virtuoso_root(), 'bin', 'virtuoso-t')
return virtuoso_exec
def get_virtuoso_src():
config = get_config()
vsrc = config.get('virtuoso', 'virtuoso_src')
if vsrc[0] != '/':
return normpath(join(env.projectpath, vsrc))
return vsrc
@task
def flushmemcache():
"""
Resetting all data in memcached
"""
if env.uses_memcache:
print(cyan('Resetting all data in memcached :'))
wait_str = "" if env.mac else "-q 2"
run('echo "flush_all" | nc %s 127.0.0.1 11211' % wait_str)
def ensure_virtuoso_not_running():
# We do not want to start supervisord if not already running
pidfile = join(env.projectpath, 'var/run/supervisord.pid')
if not exists(pidfile):
return
pid = run('cat '+ pidfile)
# Really running or stale?
ps = run('ps '+pid, quiet=True)
if ps.failed:
run('rm '+pidfile)
return
execute(supervisor_process_stop, 'virtuoso')
@task
def virtuoso_reconstruct_save_db():
execute(ensure_virtuoso_not_running)
with cd(virtuoso_db_directory()):
backup = run('%s +backup-dump +foreground' % (
get_virtuoso_exec(),), quiet=True)
if backup.failed:
print "ERROR: Normal backup failed."
# these were created by previous attempt
run('rm virtuoso-temp.db virtuoso.pxa virtuoso.trx virtuoso.lck')
run('%s +crash-dump +foreground' % (get_virtuoso_exec(),))
@task
def virtuoso_reconstruct_restore_db(transition_6_to_7=False):
execute(ensure_virtuoso_not_running)
with cd(virtuoso_db_directory()):
run('mv virtuoso.db virtuoso_backup.db')
trflag = '+log6' if transition_6_to_7 else ''
with cd(virtuoso_db_directory()):
r = run('%s +restore-crash-dump +foreground %s' % (
get_virtuoso_exec(), trflag), timeout=30)
execute(supervisor_process_start, 'virtuoso')
with cd(virtuoso_db_directory()):
run('rm virtuoso_backup.db')
@task
def virtuoso_reconstruct_db():
execute(virtuoso_reconstruct_save_db)
execute(virtuoso_reconstruct_restore_db)
def virtuoso_install_if_absent():
with settings(warn_only=True), hide('warnings', 'stdout', 'stderr'):
ls_cmd = run("ls %s" % get_virtuoso_exec())
if ls_cmd.failed:
print(red("Virtuso not installed, installing."))
execute(virtuoso_source_install)
@task
def virtuoso_source_install():
"Install the virtuoso server locally"
virtuoso_root = get_virtuoso_root()
virtuoso_src = get_virtuoso_src()
branch = get_config().get('virtuoso', 'virtuoso_branch')
if exists(virtuoso_src):
with cd(virtuoso_src):
run('git fetch')
run('git checkout '+branch)
else:
run('mkdir -p ' + dirname(virtuoso_src))
virtuso_github = 'https://github.com/openlink/virtuoso-opensource.git'
run('git clone %s %s' %(virtuso_github, virtuoso_src))
with cd(virtuoso_src):
run('git checkout '+branch)
with cd(virtuoso_src):
if not exists(join(virtuoso_src, 'configure')):
run('./autogen.sh')
#This does not work if we change the path or anything else in local.ini
#if exists(join(virtuoso_src, 'config.status')):
# run('./config.status --recheck')
#else:
run('./configure --with-readline --prefix '+virtuoso_root)
run('make -j4')
need_sudo = False
if not exists(virtuoso_root):
if not run('mkdir -p ' + virtuoso_root, quiet=True).succeeded:
need_sudo = True
sudo('mkdir -p ' + virtuoso_root)
else:
need_sudo = run('touch '+virtuoso_root, quiet=True).failed
execute(ensure_virtuoso_not_running)
if need_sudo:
sudo('checkinstall')
else:
run('make install')
#If we ran this, there is a strong chance we just reconfigured the ini file
# Make sure the virtuoso.ini and supervisor.ini reflects the changes
execute(app_setup)
execute(supervisor_restart)
## Server scenarios
def commonenv(projectpath, venvpath=None):