forked from MuckRock/muckrock
/
fabfile.py
249 lines (201 loc) · 6.54 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
# Standard Library
import os
# Third Party
from fabric.api import (
cd,
env,
lcd,
local,
prompt,
run,
settings,
task,
warn_only,
)
env.run = local
env.cd = lcd
env.base_path = os.path.dirname(env.real_fabfile)
@task(alias='prod')
def production(force=False):
"""Merge the dev branch into master and push into production"""
status = env.run('git s --porcelain', capture=True)
if '??' in status and not force:
print 'Untracked files, exiting'
exit()
env.run('git pull origin dev', capture=False)
env.run('git co master', capture=False)
env.run('git pull origin master', capture=False)
env.run('git merge dev --no-ff', capture=False)
env.run('git push origin master', capture=False)
env.run('git push origin dev', capture=False)
env.run('git co dev', capture=False)
@task
def staging():
"""Push the staging branch to the staging server"""
env.run('git push origin staging', capture=False)
@task
def test(test_path='', reuse='0', capture=False):
"""Run all tests, or a specific subset of tests"""
cmd = (
'REUSE_DB=%(reuse)s ./manage.py test %(test_path)s %(capture)s '
'--settings=muckrock.settings.test' % {
'reuse': reuse,
'test_path': test_path,
'capture': '--nologcapture' if not capture else ''
}
)
with env.cd(env.base_path):
env.run(cmd)
@task
def coverage(settings='test', reuse='0'):
"""Run the tests and generate a coverage report"""
with env.cd(env.base_path):
env.run('coverage erase')
env.run(
'REUSE_DB=%s coverage run --branch --source muckrock --omit="*/migrations/*" manage.py test --settings=muckrock.settings.%s'
% (reuse, settings)
)
env.run('coverage html')
@task
def pylint():
"""Run pylint"""
with env.cd(env.base_path):
excludes = [
'migrations', '__init__.py', 'manage.py', 'vendor', 'fabfile',
'static', 'node_modules'
]
stmt = (
'find ./muckrock -name "*.py"' +
''.join(' | grep -v %s' % e for e in excludes) +
' | xargs pylint --load-plugins=pylint_django '
'--rcfile=config/pylint.conf -r n'
)
env.run(stmt)
@task
def format():
"""Format all the code"""
with env.cd(env.base_path):
env.run(
'yapf -i -r --style config/style.yapf -e "*/migrations/*" -p muckrock'
)
env.run('isort -sp config -rc muckrock')
@task
def mail():
"""Run the test mail server"""
env.run('python -m smtpd -n -c DebuggingServer localhost:1025')
@task(alias='rs')
def runserver():
"""Run the test server"""
with env.cd(env.base_path):
env.run('./manage.py runserver 0.0.0.0:8000')
@task
def celery():
"""Run the celery worker"""
with env.cd(env.base_path):
env.run('./manage.py celery worker -Q celery,phaxio -l DEBUG')
@task(alias='m')
def manage(cmd):
"""Run a python manage.py command"""
with env.cd(env.base_path):
env.run('./manage.py %s' % cmd)
@task
def pip():
"""Update installed python packages with pip"""
with env.cd(env.base_path):
env.run('pip install -r requirements.txt')
@task(name='populate-db')
def populate_db():
"""Populate the local DB with the data from the latest heroku backup"""
# https://devcenter.heroku.com/articles/heroku-postgres-import-export
confirm = prompt(
'This will over write your local database. '
'Are you sure you want to continue? [y/N]'
)
if confirm.lower() not in ['y', 'yes']:
return
with env.cd(env.base_path):
env.run('dropdb muckrock')
env.run('heroku pg:pull DATABASE muckrock --app muckrock')
@task(name='sync-aws')
def sync_aws():
"""Sync images from AWS to match the production database"""
folders = [
'account_images',
'agency_images',
'jurisdiction_images',
'news_images',
'news_photos',
'project_images',
]
with env.cd(env.base_path), warn_only():
for folder in folders:
env.run(
'aws s3 sync s3://muckrock/{folder} '
'./muckrock/static/media/{folder}'.format(folder=folder)
)
@task
def hello():
"""'Hello world' for testing purposes"""
env.run('echo hello world')
@task(alias='v')
def vagrant(cmd=None):
"""Run a vagrant command or a task on the vagrant VM"""
# run as `fab vagrant:up`
if cmd:
with lcd(os.path.join(env.base_path, 'vm')):
local('vagrant %s' % cmd)
# run as `fab vagrant runserver`
else:
# change from the default user to 'vagrant'
env.user = 'vagrant'
# connect to the port-forwarded ssh
env.hosts = ['127.0.0.1:2222']
# use vagrant ssh key
with lcd(os.path.join(env.base_path, 'vm')):
result = local(
'vagrant ssh-config | grep IdentityFile', capture=True
)
env.key_filename = result.split()[1]
env.run = run
env.cd = cd
env.base_path = '/home/vagrant/muckrock'
@task
def setup():
"""Run to initialize your VM"""
# XXX this doesnt work yet
vagrant('up')
with lcd(os.path.join(env.base_path, 'vm')):
result = local('vagrant ssh-config | grep IdentityFile', capture=True)
with settings(
user='vagrant',
host_string='127.0.0.1:2222',
key_filename=result.split()[1]
):
manage('migrate')
@task(name='update-staging-db')
def update_staging_db():
"""Update the staging database"""
env.run('heroku maintenance:on --app muckrock-staging')
env.run(
'heroku pg:copy muckrock::DATABASE_URL DATABASE_URL --app muckrock-staging'
)
env.run('heroku maintenance:off --app muckrock-staging')
@task(name='pip-compile')
def pip_compile():
"""Update requirements"""
with env.cd(os.path.join(env.base_path, 'pip')):
env.run('pip-compile requirements.in')
env.run('pip-compile dev-requirements.in')
env.run('cp -f requirements.txt ../')
@task(name='pip-upgrade')
def pip_upgrade():
"""Update and upgrade requirements"""
with env.cd(os.path.join(env.base_path, 'pip')):
env.run('pip-compile --upgrade requirements.in')
env.run('pip-compile --upgrade dev-requirements.in')
env.run('cp -f requirements.txt ../')
@task(name='pip-sync')
def pip_sync():
"""sync requirements"""
with env.cd(os.path.join(env.base_path, 'pip')):
env.run('pip-sync requirements.txt dev-requirements.txt')