-
Notifications
You must be signed in to change notification settings - Fork 3
/
fabfile.py
307 lines (238 loc) · 8.9 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
# -*- coding: utf-8 -*-
"""
Manage Tribe instances.
This provides the fab commands required to setup and
update imstances on a remote environment (e.g. the amazon
cloud).
"""
from fabric.api import abort
from fabric.api import cd
from fabric.api import env
from fabric.api import execute
from fabric.api import prefix
from fabric.api import run
from fabric.api import settings
from fabric.api import task
from fabric.api import put
from fabric.contrib.console import confirm
from fabric.operations import prompt
from django.utils.crypto import get_random_string
class Helpers(object):
"""
Utility methods for tribe tasks.
This class provides methods that perform tasks that are useful in
server setup and maintainence.
"""
class Django(object):
@staticmethod
def generate_secret_key():
values = 'abcdefghijklmnopqrstuvwxyz0123456789!@#$^&*(-_=+)'
key = get_random_string(50, values)
return key
@task
def get_secret_key():
"""
Generate a secret key.
This utility method generates a secret key for django. Use this key
when required.
"""
print Helpers.Django.generate_secret_key()
# Where the app lives on the server
WEB_LOCATION = '/home/tribe/tribe'
# Configuration for the production envuronment
PROD_CFG = {
'dir': WEB_LOCATION,
'virt_env': '/home/tribe/.virtualenvs/tribe',
'service': 'tribe',
}
def _check_env():
"""
Ensure that this is an existing deployment that we are updating.
Pass in directory via env.dir.
"""
if not env.dir:
abort('Set env.dir')
with settings(warn_only=True):
if run('test -d {0}'.format(env.dir)).failed:
abort('Not yet set up - set up environment')
else:
run('python {0}/manage.py check'.format(WEB_LOCATION))
print('Environment seems to exist - good!')
@task
def initial_setup_and_check():
"""
Setup initial needs for server.
This command executes an initial pip install from the production
environment and then checks the current environment to make sure that
there is an existing django project.
"""
env.dir = PROD_CFG['dir']
env.virt_env = PROD_CFG['virt_env']
with cd(env.dir), prefix('source {0}/bin/activate'.format(env.virt_env)):
execute(_pip_install)
execute(_make_static)
execute(_check_env)
def _git_pull():
"""
Pull using git.
Pull code from repo to specified 'branch' or 'tag', if supplied,
or else to 'master' branch.
"""
tag_name = prompt(
'To what branch or tag should we pull (default "master")?',
default="master",)
run('git pull && git checkout -f {tag}'.format(
tag=tag_name,
))
def _pip_install():
"""
Run pip install for production.
Run pip install using the requirements file associated with
the production environment (requirements/prod.txt).
"""
run('pip install -q -r requirements/prod.txt')
def _make_static():
"""
Symlink for staticfiles.
For deployment, this should point to interface/bin (essentially if DEBUG=False)
For development, this should point to interface/build (DEBUG=True)
"""
run('ln -s interface/bin static')
def _migrate():
"""
Run django's migrate command.
Ensure that all database changes are applied.
"""
run('python {0}/manage.py migrate'.format(WEB_LOCATION))
def _restart():
"""
Restart the service on the server.
You must have setup sudo for the supervisorctl restart tribe command.
See the setup fab file.
"""
run('sudo supervisorctl restart tribe')
run('sudo supervisorctl restart tribe-celery')
def _bower():
"""
Run bower install.
Install any new required or potentially updated packages from packages.json.
"""
run('bower install')
def _grunt():
"""
Run grunt commands.
Grunt allows us to build the source files from the individual components. The
compile command combines angular files to a single javascript file and creates
the integrated index.html file (used when DEBUG = False).
"""
run('grunt build --force') # Use force b/c testing environment not around.
run('grunt compile --force') # Use force b/c testing environment not around.
def _update_search_indexes(app_name=None):
"""
Update haystack search indexes. If an app name is passed, it will only
update the indexes for that app. If no app name is passed, it will update
the indexes for ALL apps that have search indexes.
"""
if app_name:
run('python {0}/manage.py update_index {1}'.format(WEB_LOCATION, app_name))
else:
run('python {0}/manage.py update_index'.format(WEB_LOCATION))
def _run_deploy(_cfg=None):
"""
Run the specified deployment.
Wrapper around a set of private methods for each step of the deploy.
"""
if not _cfg:
abort('Must set parameters for deploy, use particular script')
env.dir = _cfg['dir']
env.virt_env = _cfg['virt_env']
env.service = _cfg['service']
with cd(env.dir), prefix('source {0}/bin/activate'.format(env.virt_env)):
execute(_check_env)
execute(_git_pull)
execute(_pip_install)
execute(_migrate)
import os
with cd(os.path.join(env.dir, 'interface')):
execute(_bower)
execute(_grunt)
with cd(env.dir), prefix('source {0}/bin/activate'.format(env.virt_env)):
execute(_restart)
@task
def deploy():
"""
Deploy to production.
This runs the set of deployment methods. This checks the environment,
pulls updates from git repository, pip-installs production, migrates,
and then restarts the tribe instance using supervisorctl.
"""
if not confirm('You are deploying to production. Do you mean to be?'):
abort('Aborting at user request')
_run_deploy(PROD_CFG)
@task
def add_organism(_cfg=PROD_CFG, taxid=None, name=None, species_name=None):
"""
Add an organism to the database.
Executes the management command to add an organism on the remote
server.
"""
if not _cfg:
abort('Must set parameters for deploy, use particular script')
env.dir = _cfg['dir']
env.virt_env = _cfg['virt_env']
env.service = _cfg['service']
if not taxid and name and species_name:
abort('Must set all three options (taxid, name, species_name)')
with cd(env.dir), prefix('source {0}/bin/activate'.format(env.virt_env)):
run('python manage.py organisms_add_organism --tax_id={0} --name="{1}" --species_name="{2}"'.format(taxid, name, species_name))
@task
def add_xrdb(_cfg=PROD_CFG, name=None, url=None):
"""
Add a crossreference database.
Executes the management command to add a crossreference database
on the remote server.
"""
print(url)
if not _cfg:
abort('Must set parameters for deploy, use particular script')
env.dir = _cfg['dir']
env.virt_env = _cfg['virt_env']
env.service = _cfg['service']
if not name and url:
abort('Must set both options (name, url)')
with cd(env.dir), prefix('source {0}/bin/activate'.format(env.virt_env)):
run('python manage.py genes_add_xrdb --name={0} --URL="{1}"'.format(name, url))
@task
def load_geneinfo(_cfg=PROD_CFG, geneinfo=None, genestxt=None, taxid=None, gi_taxid=None, symbol=None, systematic=None, alias=None, uniquexrdb=None):
"""
Load genes from ncbi on the remote server.
Upload a geneinfo file, load its contents using the associated management
command.
"""
if not _cfg:
abort('Must set parameters for deploy, use particular script')
env.dir = _cfg['dir']
env.virt_env = _cfg['virt_env']
env.service = _cfg['service']
if geneinfo is None:
abort('No gene_info file was passed. Please download one from: ftp://ftp.ncbi.nih.gov/gene/DATA/GENE_INFO/')
run('mkdir -p ~/uploads')
gi = put(geneinfo, '~/uploads/')[0]
if genestxt is not None:
gt = put(genestxt, '~/uploads/')[0]
if gi_taxid is None:
gi_taxid = taxid
with cd(env.dir), prefix('source {0}/bin/activate'.format(env.virt_env)):
if genestxt is not None:
run('python manage.py genes_load_geneinfo --geneinfo_file={0} --genestxt_file={1} --tax_id={2} --gi_tax_id={3} --symbol_col={4} --systematic_col={5} --alias_col={6} --unique_xrdb={7}'.format(gi, gt, taxid, gi_taxid, symbol, systematic, alias, uniquexrdb))
else:
run('python manage.py genes_load_geneinfo --geneinfo_file={0} --tax_id={1} --gi_tax_id={2} --symbol_col={3} --systematic_col={4} --alias_col={5} --unique_xrdb={6}'.format(gi, taxid, gi_taxid, symbol, systematic, alias, uniquexrdb))
@task
def update_haystack_indexes(app_name=None):
"""
Task that only runs update_haystack_indexes
"""
env.dir = PROD_CFG['dir']
env.virt_env = PROD_CFG['virt_env']
with cd(env.dir), prefix('source {0}/bin/activate'.format(env.virt_env)):
execute(_update_search_indexes(app_name))