-
Notifications
You must be signed in to change notification settings - Fork 22
/
tasks.py
307 lines (245 loc) · 7.67 KB
/
tasks.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
# Third Party
from invoke import task
DOCKER_COMPOSE_RUN_OPT = "docker compose -f local.yml run {opt} --rm {service} {cmd}"
DOCKER_COMPOSE_RUN_OPT_USER = DOCKER_COMPOSE_RUN_OPT.format(
opt="-u $(id -u):$(id -g) {opt}", service="{service}", cmd="{cmd}"
)
DOCKER_COMPOSE_RUN = DOCKER_COMPOSE_RUN_OPT.format(
opt="", service="{service}", cmd="{cmd}"
)
DJANGO_RUN = DOCKER_COMPOSE_RUN.format(service="muckrock_django", cmd="{cmd}")
DJANGO_RUN_USER = DOCKER_COMPOSE_RUN_OPT_USER.format(
opt="", service="muckrock_django", cmd="{cmd}"
)
# Release
# --------------------------------------------------------------------------------
@task(aliases=["prod", "p"])
def production(c):
"""Merge your dev branch into master and push to production"""
c.run("git pull origin dev")
c.run("git checkout master")
c.run("git pull origin master")
c.run("git merge dev")
c.run("git push origin master")
c.run("git checkout dev")
c.run("git push origin dev")
@task
def staging(c):
"""Push out staging"""
c.run("git push origin staging")
# Test
# --------------------------------------------------------------------------------
@task
def test(c, test_path="", reuse="0", capture=False, warning=False, failed=False):
"""Run all tests, or a specific subset of tests"""
cmd = DOCKER_COMPOSE_RUN_OPT_USER.format(
opt="-e REUSE_DB={reuse}".format(reuse=reuse),
service="muckrock_django",
cmd="python {warn} manage.py test {test_path} {capture} {failed} "
"--settings=muckrock.settings.test".format(
warn="-Wd" if warning else "",
test_path=test_path,
failed="--failed" if failed else "",
capture="--nologcapture" if not capture else "",
),
)
c.run(cmd)
@task
def test_codeship(c, v=1):
c.run(f"python manage.py test --settings=muckrock.settings.codeship -v {v}")
@task
def coverage(c, settings="test", reuse="0", codeship=False):
"""Run the tests and generate a coverage report"""
if codeship:
settings = "codeship"
cmds = [
"coverage erase",
'coverage run --branch --source muckrock --omit="*/migrations/*" '
f"manage.py test --settings=muckrock.settings.{settings}",
# "coverage html -i",
]
if codeship:
for cmd in cmds:
c.run(cmd)
else:
sh_cmd = " && ".join(cmds)
cmd = DOCKER_COMPOSE_RUN_OPT_USER.format(
opt="-e REUSE_DB={reuse}".format(reuse=reuse),
service="muckrock_django",
cmd=f"sh -c '{sh_cmd}'",
)
c.run(cmd)
# Code Quality
# --------------------------------------------------------------------------------
@task
def pylint(c, codeship=False):
"""Run the linter"""
cmd = "pylint muckrock"
if codeship:
c.run(cmd)
else:
c.run(
DOCKER_COMPOSE_RUN_OPT.format(
opt="-e DJANGO_SETTINGS_MODULE=muckrock.settings.local",
service="muckrock_django",
cmd=cmd,
)
)
@task
def format(c):
"""Format your code"""
c.run(
DJANGO_RUN_USER.format(
cmd="black muckrock --exclude migrations\\|vendor\\|gloo && "
"isort --recursive muckrock --skip ./muckrock/gloo"
)
)
@task(name="format-check")
def format_check(c):
"""Check your code format"""
c.run(
DJANGO_RUN_USER.format(
cmd="black --check muckrock --exclude migrations\\|vendor\\|gloo && "
"isort --check-only --recursive muckrock --skip ./muckrock/gloo"
)
)
# Run
# --------------------------------------------------------------------------------
@task
def up(c):
"""Start the docker images"""
c.run("docker compose up -d")
@task
def runserver(c):
"""Run the development server"""
c.run(
DOCKER_COMPOSE_RUN_OPT.format(
opt="--service-ports --use-aliases", service="muckrock_django", cmd=""
),
pty=True,
)
@task
def celeryworker(c):
"""Run a celery worker"""
c.run(
DOCKER_COMPOSE_RUN_OPT.format(
opt="--use-aliases", service="muckrock_celeryworker", cmd=""
)
)
@task
def celerybeat(c):
"""Run the celery scheduler"""
c.run(
DOCKER_COMPOSE_RUN_OPT.format(
opt="--use-aliases", service="muckrock_celerybeat", cmd=""
)
)
@task
def shell(c, opts=""):
"""Run an interactive python shell"""
c.run(
DJANGO_RUN.format(cmd="python manage.py shell_plus {opts}".format(opts=opts)),
pty=True,
)
@task
def sh(c):
"""Run an interactive shell"""
c.run(
DOCKER_COMPOSE_RUN_OPT.format(
opt="--use-aliases", service="muckrock_django", cmd="sh"
),
pty=True,
)
@task
def dbshell(c, opts=""):
"""Run an interactive db shell"""
c.run(
DJANGO_RUN.format(cmd="python manage.py dbshell {opts}".format(opts=opts)),
pty=True,
)
@task(aliases=["m"])
def manage(c, cmd):
"""Run a Django management command"""
c.run(DJANGO_RUN_USER.format(cmd="python manage.py {cmd}".format(cmd=cmd)))
@task
def run(c, cmd):
"""Run a command directly on the docker instance"""
c.run(DJANGO_RUN_USER.format(cmd=cmd))
@task
def npm(c, cmd):
"""Run an NPM command"""
c.run(DJANGO_RUN.format(cmd="npm {cmd}".format(cmd=cmd)), pty=True)
@task
def heroku(c, staging=False):
"""Run commands on heroku"""
if staging:
app = "muckrock-staging"
else:
app = "muckrock"
c.run(
"heroku run --app {app} python manage.py shell_plus".format(app=app), pty=True
)
# Dependency Management
# --------------------------------------------------------------------------------
@task(name="pip-compile")
def pip_compile(c, upgrade=False, package=None):
"""Run pip compile"""
if package:
upgrade_flag = "--upgrade-package {package}".format(package=package)
elif upgrade:
upgrade_flag = "--upgrade"
else:
upgrade_flag = ""
c.run(
DJANGO_RUN.format(
cmd="pip-compile --annotation-style line {upgrade_flag} pip/requirements.in &&"
"pip-compile --annotation-style line {upgrade_flag} pip/dev-requirements.in".format(
upgrade_flag=upgrade_flag
)
)
)
@task
def build(c):
"""Build the docker images"""
c.run("docker compose build")
@task(name="update-staging-db")
def update_staging_db(c):
"""Update the staging database"""
c.run("heroku maintenance:on --app muckrock-staging")
c.run("heroku pg:copy muckrock::DATABASE_URL DATABASE_URL --app muckrock-staging")
c.run("heroku maintenance:off --app muckrock-staging")
# Static file populating
# --------------------------------------------------------------------------------
@task(name="sync-aws")
def sync_aws(c):
"""Sync images from AWS to match the production database"""
folders = [
"account_images",
"agency_images",
"jurisdiction_images",
"news_images",
"news_photos/2019",
"project_images",
]
for folder in folders:
c.run(
"aws s3 sync s3://muckrock/{folder} ./muckrock/static/media/{folder}".format(
folder=folder
)
)
@task(name="sync-aws-staging")
def sync_aws_staging(c):
"""Sync images from AWS to match the production database"""
folders = [
"account_images",
"agency_images",
"jurisdiction_images",
"news_images",
"news_photos",
"project_images",
]
for folder in folders:
c.run(
"aws s3 sync s3://muckrock/{folder} "
"s3://muckrock-staging//{folder} --acl public-read".format(folder=folder)
)