forked from inspirehep/inspire-next
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfabfile.py
538 lines (440 loc) · 17.8 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
# -*- coding: utf-8 -*-
"""Deployment of INSPIRE Labs."""
import json
import os
from fabric.api import *
from fabric.utils import error
from fabric.contrib.files import exists
env.directory = '/opt' # remote directory
env.conf_directory = "/afs/cern.ch/project/inspire/repo/inspire-configuration.git"
env.backend_node = False
env.roledefs = {
'prod': ['inspirelabsvm01', 'inspirelabsvm02'],
'workers': ['inspirevm24', 'inspirevm25'],
'prod1': ['inspirelabsvm01'],
'prod2': ['inspirelabsvm02'],
'worker1': ['inspirevm24'],
'worker2': ['inspirevm25'],
'dev01': ['inspirevm08.cern.ch'],
'dev02': ['inspirevm11.cern.ch'],
'proxy': ['inspirelb1.cern.ch'],
'builder': ["inspirevm24"]
}
labs_backends = [
"inspirelabs",
"inspirelabs-ssl",
]
env.proxybackends = {
'prod1': ['inspirelabsvm01', labs_backends],
'prod2': ['inspirelabsvm02', labs_backends],
}
@task
def proxy():
"""Activate connection to proxy server."""
env.hosts = env.roledefs['proxy']
@task
def prod():
"""Activate connection to labs production servers."""
env.roles = ['prod']
env.site_url = "http://labs.inspirehep.net"
env.site_secure_url = "https://labs.inspirehep.net"
env.conf_branch = "prod"
env.tmp_shared = "/afs/cern.ch/project/inspire/LABS/var/tmp-shared"
env.data = "/afs/cern.ch/project/inspire/LABS/var/data"
env.log_bibsched = "/afs/cern.ch/project/inspire/LABS/var/log/bibsched"
@task
def prod1():
"""Activate connection to labs production server 1."""
env.roles = ['prod1']
env.site_url = "http://labs.inspirehep.net"
env.site_secure_url = "https://labs.inspirehep.net"
env.conf_branch = "prod"
env.tmp_shared = "/afs/cern.ch/project/inspire/LABS/var/tmp-shared"
env.data = "/afs/cern.ch/project/inspire/LABS/var/data"
env.log_bibsched = "/afs/cern.ch/project/inspire/LABS/var/log/bibsched"
@task
def prod2():
"""Activate connection to labs production server 2."""
env.roles = ['prod2']
env.site_url = "http://labs.inspirehep.net"
env.site_secure_url = "https://labs.inspirehep.net"
env.conf_branch = "prod"
env.tmp_shared = "/afs/cern.ch/project/inspire/LABS/var/tmp-shared"
env.data = "/afs/cern.ch/project/inspire/LABS/var/data"
env.log_bibsched = "/afs/cern.ch/project/inspire/LABS/var/log/bibsched"
@task
def workers():
"""Activate connection to labs production servers."""
env.roles = ['workers']
env.site_url = "http://labs.inspirehep.net"
env.site_secure_url = "https://labs.inspirehep.net"
env.conf_branch = "prod"
env.tmp_shared = "/afs/cern.ch/project/inspire/LABS/var/tmp-shared"
env.data = "/afs/cern.ch/project/inspire/LABS/var/data"
env.log_bibsched = "/afs/cern.ch/project/inspire/LABS/var/log/bibsched"
env.backend_node = True
@task
def worker1():
"""Activate connection to labs production servers."""
env.roles = ['worker1']
env.site_url = "http://labs.inspirehep.net"
env.site_secure_url = "https://labs.inspirehep.net"
env.conf_branch = "prod"
env.tmp_shared = "/afs/cern.ch/project/inspire/LABS/var/tmp-shared"
env.data = "/afs/cern.ch/project/inspire/LABS/var/data"
env.log_bibsched = "/afs/cern.ch/project/inspire/LABS/var/log/bibsched"
env.backend_node = True
@task
def worker2():
"""Activate connection to labs production servers."""
env.roles = ['worker2']
env.site_url = "http://labs.inspirehep.net"
env.site_secure_url = "https://labs.inspirehep.net"
env.conf_branch = "prod"
env.tmp_shared = "/afs/cern.ch/project/inspire/LABS/var/tmp-shared"
env.data = "/afs/cern.ch/project/inspire/LABS/var/data"
env.log_bibsched = "/afs/cern.ch/project/inspire/LABS/var/log/bibsched"
env.backend_node = True
@task
def vm08():
"""Activate connection to labs dev server 1."""
env.roles = ['dev01']
env.site_url = "http://inspirelabstest.cern.ch"
env.site_secure_url = "https://inspirelabstest.cern.ch"
env.conf_branch = "dev"
env.tmp_shared = ""
env.data = ""
env.log_bibsched = ""
env.backend_node = True
@task
def vm11():
"""Activate connection to labs dev server 2."""
env.roles = ['dev02']
env.site_url = "http://inspirevm11.cern.ch"
env.site_secure_url = "https://inspirevm11.cern.ch"
env.tmp_shared = ""
env.data = ""
env.log_bibsched = ""
env.backend_node = True
@task
def dry():
"""Activate dry run."""
env.dry = True
@task
def pack():
"""Create a new source distribution as tarball for Inspire and Invenio."""
with open(".bowerrc") as fp:
bower = json.load(fp)
choice = prompt("Clean and reinstall local assets? (y/N)", default="no")
if choice.lower() in ["y", "ye", "yes"]:
clean_assets()
choice = prompt("Build assets to gen? (Y/n)", default="yes")
if choice.lower() not in ["n", "no"]:
local("inveniomanage assets build --directory {directory}/../gen".format(**bower))
pack_invenio()
return pack_inspire()
@task
def init_password():
"""Kickstart the password prompt."""
sudo("echo 'hello world'")
@task
def pack_inspire():
"""Create a new source distribution as tarball without assets building (advanced use)."""
return local("python setup.py sdist --formats=gztar", capture=False) \
.succeeded
@task
def pack_invenio():
"""Create a new source distribution as tarball of Invenio."""
virtualenv = os.environ["VIRTUAL_ENV"]
with lcd(os.path.join(virtualenv, "src/invenio")):
return local("python setup.py sdist --formats=gztar", capture=False).succeeded
@task
def create_virtualenv():
"""Create the virtualenv for the current Inspire version."""
package = local("python setup.py --fullname", capture=True).strip()
pythonpath = "/opt/rh/python27/root/usr/bin/python"
with cd(env.directory):
if exists(package):
return error("This version {0} is already installed."
.format(package))
with settings(sudo_user="invenio"):
return sudo("virtualenv {0} --python={1}".format(package, pythonpath)).succeeded
@task
def install():
"""Install all the things."""
package = local("python setup.py --fullname", capture=True).strip()
venv = "{0}/{1}".format(env.directory, package)
if not exists(venv):
return error("Meh? I need a virtualenv first.")
if not env.backend_node:
execute(disable, env.host, False)
else:
stop_celery()
stop_bibsched()
success = 1
invenio_package = ""
# Upload the packages and put it into our virtualenv.
virtualenv = os.environ["VIRTUAL_ENV"]
put("dist/{0}.tar.gz".format(package), "/tmp/app.tgz")
with lcd(os.path.join(virtualenv, "src/invenio")):
invenio_package = local("python setup.py --fullname", capture=True).strip()
put("dist/{0}.tar.gz".format(invenio_package), "/tmp/invenio.tgz")
remove_build()
with settings(sudo_user="invenio"):
sudo("mkdir -p {0}/src".format(venv))
with cd("{0}/src".format(venv)):
sudo("tar xzf /tmp/app.tgz")
run("rm -rf /tmp/app.tgz")
# Also Invenio
sudo("tar xzf /tmp/invenio.tgz")
run("rm -rf /tmp/invenio.tgz")
# Invenio installation
with cd("{0}/src/{1}".format(venv, invenio_package)):
with prefix('source {0}/bin/activate'.format(venv)):
sudo("pip install Babel")
sudo("pip install numpy")
sudo("pip install git+git://github.com/mrjoes/flask-admin.git#egg=Flask-Admin-1.0.9.dev0")
sudo("pip install . --upgrade")
# Inspire installation
with cd("{0}/src/{1}".format(venv, package)):
with prefix('source {0}/bin/activate'.format(venv)):
sudo("pip install git+https://github.com/inspirehep/python-rt#egg=rt")
success = sudo("python setup.py install")
if success:
# INSPIRE specific configuration
with cd(env.conf_directory):
config_location = "/tmp/inspire-configuration"
sudo("mkdir -p {0}".format(config_location))
sudo("GIT_WORK_TREE={0} git checkout -f {1}".format(config_location, env.conf_branch))
sudo("pip install {0} --upgrade".format(config_location))
# post install
sudo("inveniomanage collect")
with warn_only():
# Set Flask Host configuration
if "{0}" in env.site_url:
url = env.site_url.format(env.host)
secure_url = env.site_secure_url.format(env.host)
else:
url = env.site_url
secure_url = env.site_secure_url
sudo("inveniomanage config set CFG_SITE_URL {0}".format(url))
sudo("inveniomanage config set CFG_SITE_SECURE_URL {0}".format(secure_url))
# Create Apache configuration
sudo("inveniomanage apache create-config")
create_symlink("/opt/invenio", venv)
if env.tmp_shared:
create_symlink(os.path.join(venv, "var/tmp-shared"), env.tmp_shared)
if env.data:
create_symlink(os.path.join(venv, "var/data"), env.data)
if env.log_bibsched:
create_symlink(os.path.join(venv, "var/log/bibsched"), env.log_bibsched)
with cd("{0}/var".format(venv)):
sudo("mkdir -p log/bibsched")
if success:
restart_apache()
if not env.backend_node:
execute(enable, env.host, True)
else:
choice = prompt("Enable backend (Celery, BibSched)? (Y/n)", default="yes")
if choice.lower() in ["y", "ye", "yes"]:
start_celery()
start_bibsched()
return success
@roles(['builder'])
@task
def sync(host=None):
"""Sync Invenio to given host (or all)."""
package = local("python setup.py --fullname", capture=True).strip()
venv = "{0}/{1}".format(env.directory, package)
if not exists(venv):
return error("Meh? I need a virtualenv first.")
execute(disable, host, False)
sudo(
'rsync -az --force --delete --progress '
'--exclude "var/tmp-shared" --exclude "src" '
'--exclude "var/log" --exclude "var/data" '
'--exclude "var/tmp" --exclude "var/run" '
'--exclude "var/cache" --exclude "var/batchupload" '
'--exclude "etc" --exclude "includes" '
'-e "ssh -p22" {venv}/ {server}:/opt/invenio'.format(venv=venv, server=env.roledefs[host])
)
@task
def remove_build():
"""Remove the leftover build directory."""
package = local("python setup.py --fullname", capture=True).strip()
venv = "{0}/{1}".format(env.directory, package)
with settings(sudo_user="invenio"):
with prefix('source {0}/bin/activate'.format(venv)):
build_dir = os.path.join(venv, "build")
src_dir = os.path.join(venv, "src")
sudo("rm -rf {0}".format(src_dir))
sudo("rm -rf {0}".format(build_dir))
@task
def restart_celery():
"""Restart celery workers."""
return sudo("supervisorctl restart celeryd")
@task
def stop_celery():
"""Restart celery workers."""
return sudo("supervisorctl stop celeryd")
@task
def start_celery():
"""Restart celery workers."""
return sudo("supervisorctl start celeryd")
@task
def stop_bibsched():
"""Restart celery workers."""
package = local("python setup.py --fullname", capture=True).strip()
venv = "{0}/{1}".format(env.directory, package)
with settings(sudo_user="invenio"):
with prefix('source {0}/bin/activate'.format(venv)):
return sudo("bibsched stop")
@task
def start_bibsched():
"""Restart celery workers."""
package = local("python setup.py --fullname", capture=True).strip()
venv = "{0}/{1}".format(env.directory, package)
with settings(sudo_user="invenio"):
with prefix('source {0}/bin/activate'.format(venv)):
return sudo("bibsched start")
@task
def restart_apache():
"""Restart celery workers."""
return sudo("service httpd graceful")
@task
def graceful_apache():
"""Restart apache gracefully."""
target = env.host
execute(disable, target, False)
execute(restart_apache)
execute(enable, target, True)
@task
def refresh_config():
"""Re-install the inspire-configuration ONLY."""
package = local("python setup.py --fullname", capture=True).strip()
venv = "{0}/{1}".format(env.directory, package)
if not exists(venv):
return error("Meh? I need a virtualenv first.")
with settings(sudo_user="invenio"):
with cd(venv):
with prefix('source {0}/bin/activate'.format(venv)):
# INSPIRE specific configuration
with cd(env.conf_directory):
config_location = "/tmp/inspire-configuration"
sudo("mkdir -p {0}".format(config_location))
sudo("GIT_WORK_TREE={0} git checkout -f {1}".format(config_location, env.conf_branch))
sudo("pip install {0} --upgrade".format(config_location))
restart_celery()
graceful_apache()
@task
def refresh_invenio():
"""Re-install the Invenio package ONLY (also builds it)."""
virtualenv = os.environ["VIRTUAL_ENV"]
package = ""
with lcd(os.path.join(virtualenv, "src/invenio")):
package = local("python setup.py --fullname", capture=True).strip()
local("python setup.py sdist --formats=gztar", capture=False).succeeded
put("dist/{0}.tar.gz".format(package), "/tmp/invenio.tgz")
with settings(sudo_user="invenio"):
# Invenio installation
with cd("{0}/src/{1}".format(venv, invenio_package)):
with prefix('source {0}/bin/activate'.format(venv)):
sudo("pip install Babel")
sudo("pip install numpy")
sudo("pip install git+git://github.com/mrjoes/flask-admin.git#egg=Flask-Admin-1.0.9.dev0")
sudo("pip install . --upgrade")
@task
def clean_assets():
"""Helper to ensure all assets are up to date."""
local("rm -rf $VIRTUAL_ENV/var/invenio.base-instance/static/")
local("rm -rf inspire/base/static/gen/")
local("rm -rf inspire/base/static/vendors/")
local("inveniomanage bower -i bower-base.json > bower.json")
local("bower install")
local("inveniomanage collect")
@task
def compile_translations():
"""Compile the Invenio translations."""
package = local("python setup.py --fullname", capture=True).strip()
venv = "{0}/{1}".format(env.directory, package)
if not exists(venv):
return error("Meh? I need a virtualenv first.")
with settings(sudo_user="invenio"):
with cd("{0}/src/{1}".format(venv, package)):
with prefix('source {0}/bin/activate'.format(venv)):
do_compilation()
def do_compilation():
"""Compile base Invenio translatation to avoid translation error."""
prefix_folder = sudo('python -c "import invenio; print(invenio.__path__[0])"')
prefix_folder = prefix_folder.split("\n")
sudo("pybabel compile -fd {0}/base/translations".format(prefix_folder[-1]))
@roles(['proxy'])
@task
def disable(host, interactive=True):
"""Disable a server in the haproxy configuration. Use with proxy."""
if isinstance(host, basestring):
host = [host]
backends = env.proxybackends
if not backends:
print("No backends defined")
return
server = None
for alias, item in backends.items():
# item = ('hostname', [list of backends])
hostname, dummy_list_of_backends = item
if hostname in host or alias in host:
server = alias
break
if not server:
print("No server defined")
return
servername, backends = backends[server]
if interactive:
choice = prompt("Disable the following server? %s (Y/n)" % (servername, ), default="yes")
if choice.lower() not in ["y", "ye", "yes"]:
return
proxy_action(servername, backends, action="disable")
@roles(['proxy'])
@task
def enable(host, interactive=True):
"""Enable a server in the haproxy configuration. Use with proxy."""
if isinstance(host, basestring):
host = [host]
backends = env.proxybackends
if not backends:
print("No backends defined")
return
server = None
for alias, item in backends.items():
# item = ('hostname', [list of backends])
hostname, dummy_list_of_backends = item
if hostname in host or alias in host:
server = alias
break
if not server:
print("No server defined")
return
servername, backends = backends[server]
if interactive:
choice = prompt("Enable the following server? %s (Y/n)" % (servername, ), default="yes")
if choice.lower() not in ["y", "ye", "yes"]:
return
proxy_action(servername, backends, action="enable")
def proxy_action(server, backends, action="enable"):
"""Using sockets, communicate with haproxy."""
for backend in backends:
if 'ssl' in backend:
# special ssl suffix
current_server_suffix = '-ssl'
else:
current_server_suffix = ''
current_server = server + current_server_suffix
cmd = 'echo "%s server %s/%s" | nc -U /var/lib/haproxy/stats' \
% (action, backend, current_server)
sudo(cmd, shell=True)
def create_symlink(link, target):
"""Create a symlink if required."""
existing_link = sudo("readlink {0}".format(link))
if existing_link and existing_link != target:
sudo("rm {0}".format(link))
sudo("ln -sf {0} {1}".format(target, link))