forked from mozilla/webpay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fabfile.py
135 lines (101 loc) · 3.3 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
import os
from fabric.api import (env, execute, lcd, local, parallel,
run, roles, task)
from fabdeploytools.rpm import RPMBuild
from fabdeploytools import helpers
import fabdeploytools.envs
import deploysettings as settings
env.key_filename = settings.SSH_KEY
fabdeploytools.envs.loadenv(settings.CLUSTER)
ROOT, WEBPAY = helpers.get_app_dirs(__file__)
VIRTUALENV = os.path.join(ROOT, 'venv')
PYTHON = os.path.join(VIRTUALENV, 'bin', 'python')
def managecmd(cmd):
with lcd(WEBPAY):
local('%s manage.py %s' % (PYTHON, cmd))
@task
def create_virtualenv():
with lcd(WEBPAY):
# Compare the last reflog change with the latest change we introduced.
status = local('git diff HEAD@{1} HEAD --name-only', capture=True)
if 'requirements/' in status:
venv = VIRTUALENV
if not venv.startswith('/data'):
raise Exception('venv must start with /data')
local('rm -rf %s' % venv)
helpers.create_venv(venv, settings.PYREPO,
'%s/requirements/prod.txt' % WEBPAY)
@task
def update_locales():
with lcd(os.path.join(WEBPAY, 'locale')):
local("./compile-mo.sh .")
@task
def compress_assets(arg=''):
managecmd('compress_assets -t %s' % arg)
@task
def schematic():
with lcd(WEBPAY):
local("%s %s/bin/schematic migrations" %
(PYTHON, VIRTUALENV))
@task
def update_info(ref='origin/master'):
helpers.git_info(WEBPAY)
with lcd(WEBPAY):
local("/bin/bash -c "
"'source /etc/bash_completion.d/git && __git_ps1'")
local('git show -s {0} --pretty="format:%h" '
'> media/git-rev.txt'.format(ref))
@task
@roles('web')
@parallel
def restart_workers():
for gservice in settings.GUNICORN:
run("/sbin/service %s graceful" % gservice)
restarts = []
for g in settings.MULTI_GUNICORN:
restarts.append('( supervisorctl restart {0}-a; '
'supervisorctl restart {0}-b )&'.format(g))
if restarts:
run('%s wait' % ' '.join(restarts))
@task
@roles('celery')
@parallel
def update_celery():
if getattr(settings, 'CELERY_SERVICE', False):
run("/sbin/service %s restart" %
settings.CELERY_SERVICE)
@task
def deploy():
helpers.deploy(name='webpay',
env=settings.ENV,
cluster=settings.CLUSTER,
domain=settings.DOMAIN,
root=ROOT,
deploy_roles=['web', 'celery'],
package_dirs=['webpay', 'venv'])
execute(restart_workers)
helpers.restart_uwsgi(getattr(settings, 'UWSGI', []))
execute(update_celery)
@task
def pre_update(ref=settings.UPDATE_REF):
local('date')
execute(helpers.git_update, WEBPAY, ref)
execute(update_info, ref)
@task
def update():
execute(create_virtualenv)
execute(update_locales)
execute(compress_assets)
execute(schematic)
@task
def pre_update_latest_tag():
current_tag_file = os.path.join(WEBPAY, '.tag')
latest_tag = helpers.git_latest_tag(WEBPAY)
with open(current_tag_file, 'r+') as f:
if f.read() == latest_tag:
print 'Environemnt is at %s' % latest_tag
else:
pre_update(latest_tag)
f.seek(0)
f.write(latest_tag)
f.truncate()