-
Notifications
You must be signed in to change notification settings - Fork 6
/
fabfile.py
287 lines (252 loc) · 9.12 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
from fabric.api import *
from fabric.contrib.console import confirm
from fabric.contrib.files import comment, uncomment
from contextlib import contextmanager
import getpass
import os
git_repo = 'git@github.com:OpenPhilology/phaidra.git'
env.directory = os.path.dirname(os.path.abspath(__file__))
env.activate = '. %s/env/bin/activate' % env.directory
env.hosts = ['localhost']
@contextmanager
def virtualenv():
with lcd(env.directory):
with prefix(env.activate):
yield
###############################
# Frontend Utility tasks #
###############################
@task
def prepare_locale(lang):
"""
Gathers translation strings based on provided locale.
"""
with virtualenv():
local("django-admin.py makemessages --locale=%s --extension=html --ignore=env --ignore=*.py" % lang)
local("django-admin.py makemessages -d djangojs --locale=%s --ignore=env --ignore=static/admin/* --ignore=static/js/components --ignore=static/collected/*" % lang)
@task
def compile_locale(lang):
"""
Compiles translations strings into .mo files
"""
with virtualenv():
local("django-admin.py compilemessages --locale=%s" % lang)
@task
def propagate_db():
"""
Creates a dump of the postgres database
"""
with virtualenv():
local("./manage.py dumpdata --natural --indent=4 --exclude=contenttypes --exclude=auth --exclude=tastypie > app/fixtures/db.json")
local('git add app/fixtures/db.json')
@task
def load_db():
"""
Migrates the database, then loads db.json
"""
with virtualenv():
local('./manage.py migrate')
local("./manage.py loaddata app/fixtures/db.json")
@task
def import_neo4j():
"""
Load Neo4j database dump.
"""
with virtualenv():
local('python %s/common/utils/neo4j_import/pentecontaetia_import.py' % env.directory)
@task
def import_alignment(lang):
"""
Load alignment data.
Language parameter required.
E.g.: fab import_alignment:lang='en' for English or
fab import_alignment:lang='all' for saving the alignments of all available languages.
To get a list of all avaialable languages type: fab import_alignment:lang='lookup'
"""
with virtualenv():
local('python %s/common/utils/neo4j_import/import_alignment.py %s' % (env.directory, lang))
###############################
# Backend Utility tasks #
###############################
def restart_uwsgi():
"""
Restarts uwsgi.
"""
with virtualenv(), settings(warn_only=True):
local('killall uwsgi')
local('uwsgi %s/extras/uwsgi/phaidra.ini' % env.directory)
@task
def debug_uwsgi():
"""
Task for un-daemonizing uwsgi, for debugging purposes
"""
with virtualenv():
with settings(warn_only=True):
local('killall uwsgi')
comment('%s/extras/uwsgi/phaidra.ini' % env.directory,
r'^daemonize'
)
restart_uwsgi()
@task
def restart(full=False):
"""
Reboots neo4j, nginx, and uwsgi.
"""
with virtualenv():
# Only restart neo4j if needed
if full:
local('service neo4j-service stop && service neo4j-service start')
local('service nginx stop && service nginx start')
uncomment('%s/extras/uwsgi/phaidra.ini' % env.directory,
r'daemonize'
)
restart_uwsgi()
@task
def destroy_neo4j():
"""
Removes the entire neo4j database and restarts the service
"""
if confirm('WARNING: Are you sure you want to permanently destroy all data in Neo4j?'):
neo4j_dir = '/var/lib/neo4j'
local('sudo service neo4j-service stop')
local('sudo rm -rf %s/data' % neo4j_dir)
local('sudo mkdir %s/data' % neo4j_dir)
local('sudo mkdir %s/data/log' % neo4j_dir)
local("sudo chown -R neo4j:root %s/data" % neo4j_dir)
local('sudo service neo4j-service start')
###############################
# Installation tasks #
###############################
@task
def init():
"""
Most fundamental steps to initialize Phaidra install.
"""
local('apt-get install python-software-properties python python-virtualenv python-dev libpq-dev')
with lcd(env.directory):
# Prevent attempts to create second env when running install more than once
if not os.path.isfile('/opt/phaidra/env/bin/activate'):
local('virtualenv --no-site-packages env')
with virtualenv():
local('pip install -r requirements.txt')
@task
def setup_postgres():
"""
Installs postgres and creates database for Django.
"""
# IMPORTANT: Postgres comes pre-installed on Ubuntu 12.04, but in some borked fashion
# It must be purged, then re-installed fresh (or else /etc/postgresql/ folder won't exist,
# and socket issues crop up.)
local('sudo apt-get remove --purge postgresql postgresql-9.1')
local("sudo apt-get build-dep python-psycopg2")
local("sudo apt-get install postgresql-9.1 postgresql-contrib")
with settings(warn_only=True):
result = local('sudo -u postgres createdb phaidra', capture=True)
if result.failed:
if confirm("WARNING: Database 'phaidra' already exists. Delete database contents?"):
local('sudo -u postgres dropdb phaidra')
local('sudo -u postgres createdb phaidra')
else:
abort("Failed for unknown reason. Investigate postgres.")
password = prompt('Enter a new database password for user `postgres`:')
local('sudo -u postgres psql template1 -c "ALTER USER postgres with encrypted password \'%s\';"' % password)
@task
def setup_frontend():
"""
Installs frontend requirements.
"""
with lcd(env.directory):
# Github distributes submodules as blank folders. Remove, clear cache, then pull
submodules = ['daphnejs', 'moreajs', 'typegeek']
for module in submodules:
if os.path.exists('static/js/lib/%s' % module):
local('rm -rf static/js/lib/%s' % module)
local('git rm --cached static/js/lib/%s' % module)
local('git submodule add https://github.com/mlent/%(s)s.git static/js/lib/%(s)s' % { "s": module })
# Install Node
local("add-apt-repository ppa:chris-lea/node.js")
local("apt-get update")
local("apt-get install nodejs")
# Install Frontend Depdencies
local("npm install")
local("npm install bower -g")
local("bower install --allow-root")
@task
def setup_django():
"""
Sets up django, prompts to create super user.
"""
with virtualenv():
local("./manage.py collectstatic --noinput")
local('./manage.py migrate')
load_db()
local('source env/bin/activate') # Don't know why, but this command has issues with 'with virtualenv()'
local('./manage.py createsuperuser')
@task
def setup_server():
"""
Installs and configures Nginx and Uwsgi
"""
local("apt-get install nginx uwsgi uwsgi-plugin-python python2.7-dev")
local("pip install -U uwsgi")
with lcd('/usr/bin'):
local('mv uwsgi uwsgi-old')
local('ln -s /usr/local/bin/uwsgi uwsgi')
# Symlink our config files if the links don't exist already
nginx_conf = "%s/extras/nginx/phaidra.conf" % env.directory
uwsgi_conf = "%s/extras/uwsgi/phaidra.ini" % env.directory
if not os.path.isfile('/etc/nginx/sites-enabled/phaidra.conf'):
local('ln -s %s /etc/nginx/sites-enabled/phaidra.conf' % nginx_conf)
if not os.path.isfile('/etc/uwsgi/apps-enabled/phaidra.ini'):
local('ln -s %s /etc/uwsgi/apps-enabled/phaidra.ini' % uwsgi_conf)
# Create a safe place for the uwsgi socket to exist
if not os.path.exists('/var/uwsgi'):
local("mkdir /var/uwsgi")
# Make user www-data (nginx's user) can access our socket
local("chown www-data:www-data /var/uwsgi")
# Create directories and folders needed by uwsgi/nginx
directories = [
'%s/logs' % env.directory,
'/var/log/uwsgi'
]
files = [
'%s/logs/master.pid' % env.directory,
'/var/log/uwsgi/phaidra_daemon.log'
]
for d in directories:
if not os.path.exists(d):
local('mkdir %s' % d)
for f in files:
if not os.path.isfile(f):
local('touch %s' % f)
@task
def setup_neo4j():
"""
Adds and installs java and neo4j repositories
"""
# Install Java
local('add-apt-repository ppa:webupd8team/java')
local('apt-get update')
local('apt-get install oracle-java7-installer')
local('update-java-alternatives -s java-7-oracle')
# Install neo4j
local('wget -O - http://debian.neo4j.org/neotechnology.gpg.key | apt-key add -')
local("echo 'deb http://debian.neo4j.org/repo stable/' > /etc/apt/sources.list.d/neo4j.list")
local('apt-get update && apt-get install neo4j && service neo4j-service start')
@task
def install():
"""
Our beautiful install task for Phaidra.
"""
code_dir = '/opt/phaidra'
with cd(code_dir):
env.password = getpass.getpass()
init()
setup_postgres()
setup_frontend()
setup_django()
setup_server()
setup_neo4j()
restart()
import_neo4j()
import_alignment()