forked from MuckRock/squarelet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtasks.py
More file actions
374 lines (297 loc) · 9.64 KB
/
tasks.py
File metadata and controls
374 lines (297 loc) · 9.64 KB
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
# Standard Library
import os
from pathlib import Path
# 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="squarelet_django", cmd="{cmd}")
DJANGO_RUN_USER = DOCKER_COMPOSE_RUN_OPT_USER.format(
opt="-e HOME=/app", service="squarelet_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,
path="squarelet",
create_db=False,
ipdb=False,
warnings=False,
capture_output=False,
):
"""Run the test suite"""
create_switch = "--create-db" if create_db else ""
capture_switch = "-s" if capture_output else ""
ipdb_switch = "--pdb --pdbcls=IPython.terminal.debugger:Pdb" if ipdb else ""
warnings = "-e PYTHONWARNINGS=always" if warnings else ""
c.run(
DOCKER_COMPOSE_RUN_OPT_USER.format(
opt=f"-e DJANGO_SETTINGS_MODULE=config.settings.test {warnings}",
service="squarelet_django",
cmd=f"pytest {create_switch} {capture_switch} {ipdb_switch} {path}",
),
pty=True,
)
@task
def coverage(c):
"""Run the test suite with coverage report"""
c.run(
DOCKER_COMPOSE_RUN_OPT_USER.format(
opt="-e DJANGO_SETTINGS_MODULE=config.settings.test",
service="squarelet_django",
cmd=f"coverage erase",
)
)
c.run(
DOCKER_COMPOSE_RUN_OPT_USER.format(
opt="-e DJANGO_SETTINGS_MODULE=config.settings.test",
service="squarelet_django",
cmd=f"coverage run -m py.test squarelet",
)
)
c.run(
DOCKER_COMPOSE_RUN_OPT_USER.format(
opt="-e DJANGO_SETTINGS_MODULE=config.settings.test",
service="squarelet_django",
cmd=f"coverage html",
)
)
@task(name="test-frontend")
def test_frontend(c, ui=False, coverage=False):
"""Run the frontend test suite with Vitest"""
cmd_args = []
if ui:
cmd_args.append("--ui")
if coverage:
cmd_args.append("--coverage")
# Run tests (use --run to exit after tests complete, unless --ui is specified)
if not ui:
cmd_args.append("--run")
cmd = f"npm test -- {' '.join(cmd_args)}" if cmd_args else "npm test -- --run"
c.run(
DOCKER_COMPOSE_RUN_OPT.format(
opt="--workdir /app",
service="squarelet_django",
cmd=cmd,
),
pty=True,
)
# Code Quality
# --------------------------------------------------------------------------------
@task
def pylint(c):
"""Run the linter"""
c.run(DJANGO_RUN.format(cmd="pylint squarelet"))
@task
def format(c):
"""Format your code"""
c.run(
DJANGO_RUN_USER.format(
cmd="black squarelet --exclude migrations && "
"black config/urls.py && "
"black config/settings && "
"isort --recursive squarelet && "
"isort config/urls.py && "
"isort --recursive config/settings"
)
)
# Run
# --------------------------------------------------------------------------------
@task
def up(c):
"""Start the docker images"""
c.run("docker compose up -d")
@task
def down(c):
"""Shut down the docker images"""
c.run(f"docker compose down")
@task
def runserver(c):
"""Run the development server"""
c.run(
DOCKER_COMPOSE_RUN_OPT.format(
opt="--service-ports --use-aliases", service="squarelet_django", cmd=""
)
)
@task
def celeryworker(c):
"""Run a celery worker"""
c.run(
DOCKER_COMPOSE_RUN_OPT.format(
opt="--use-aliases", service="squarelet_celeryworker", cmd=""
)
)
@task
def celerybeat(c):
"""Run the celery scheduler"""
c.run(
DOCKER_COMPOSE_RUN_OPT.format(
opt="--use-aliases", service="squarelet_celerybeat", cmd=""
)
)
@task
def shell(c, opts=""):
"""Run an interactive python shell"""
c.run(DJANGO_RUN.format(cmd=f"python manage.py shell_plus {opts}"), pty=True)
@task
def sh(c):
"""Run an interactive shell"""
c.run(
DOCKER_COMPOSE_RUN_OPT_USER.format(
opt="--use-aliases", service="squarelet_django", cmd="sh"
),
pty=True,
)
@task
def dbshell(c, opts=""):
"""Run an interactive db shell"""
c.run(DJANGO_RUN.format(cmd=f"python manage.py dbshell {opts}"), pty=True)
@task(aliases=["m"])
def manage(c, cmd):
"""Run a Django management command"""
c.run(DJANGO_RUN_USER.format(cmd=f"python manage.py {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(
DOCKER_COMPOSE_RUN_OPT.format(
opt="--workdir /app", service="squarelet_django", cmd=f"npm {cmd}"
)
)
@task
def heroku(c, staging=False):
"""Run commands on heroku"""
if staging:
app = os.environ.get("HEROKU_APP_NAME", "squarelet-staging")
else:
app = "squarelet"
c.run(f"heroku run --app {app} python manage.py shell_plus", pty=True)
# Dependency Management
# --------------------------------------------------------------------------------
@task(name="pip-compile")
def pip_compile(c, upgrade=False, package=None):
"""Run pip compile"""
if package:
upgrade_flag = f"--upgrade-package {package}"
elif upgrade:
upgrade_flag = "--upgrade"
else:
upgrade_flag = ""
c.run(
DJANGO_RUN_USER.format(
cmd='sh -c "'
f"pip-compile {upgrade_flag} requirements/base.in && "
f"pip-compile {upgrade_flag} requirements/local.in && "
f"pip-compile {upgrade_flag} requirements/production.in"
'"'
)
)
@task(name="pip-audit")
def pip_audit(c):
"""Run pip-audit"""
c.run(
DJANGO_RUN_USER.format(
cmd='sh -c "'
f"pip-audit -r requirements/base.txt && "
f"pip-audit -r requirements/local.txt && "
f"pip-audit -r requirements/production.txt"
'"'
)
)
@task
def build(c):
"""Build the docker images"""
c.run("docker compose -f local.yml build")
# Database populating
# --------------------------------------------------------------------------------
@task(name="populate-db")
def populate_db(c, db_name="squarelet"):
"""Populate the local DB with the data from heroku"""
# https://devcenter.heroku.com/articles/heroku-postgres-import-export
# this doesnt work due to version mismatch in postgres
# work around is to heroku pg:backups:download
# and manual pg_restore
# pg_restore --verbose --clean --no-acl --no-owner -h $POSTGRES_HOST -U $POSTGRES_USER -d $POSTGRES_DB latest.dump
confirm = input(
f"This will over write your local database ({db_name}). "
"Are you sure you want to continue? [y/N]"
)
if confirm.lower() not in ["y", "yes"]:
return
c.run(
DJANGO_RUN_USER.format(
cmd=f'sh -c "dropdb {db_name} && heroku pg:pull DATABASE {db_name} --app squarelet '
'--exclude-table-data=public.reversion_version"'
)
)
@task(name="update-staging-db")
def update_staging_db(c):
"""Update the staging database"""
c.run("heroku maintenance:on --app squarelet-staging")
c.run("heroku pg:copy squarelet::DATABASE_URL DATABASE_URL --app squarelet-staging")
c.run("heroku maintenance:off --app squarelet-staging")
# Static file populating
# --------------------------------------------------------------------------------
@task(name="sync-aws")
def sync_aws(c):
"""Sync images from AWS to match the production database"""
folders = ["account_images", "avatars", "org_avatars"]
for folder in folders:
c.run(
f"aws s3 sync s3://squarelet/media/{folder} " f"./squarelet/media/{folder}"
)
@task(name="sync-aws-staging")
def sync_aws_staging(c):
"""Sync images from AWS to match the production database"""
folders = ["account_images", "avatars", "org_avatars"]
for folder in folders:
c.run(
f"aws s3 sync s3://squarelet/media/{folder} "
f"s3://squarelet-staging/media/{folder}"
)
# Setup
# --------------------------------------------------------------------------------
@task
def mkcert(c):
"""Make SSL certificates for local development"""
certs_dir = Path("./config/certs/")
if not certs_dir.exists():
certs_dir.mkdir(parents=True, exist_ok=True)
with c.cd(str(certs_dir)):
c.run(
"CAROOT=. mkcert "
"-install "
"-cert-file dev.squarelet.com.pem "
"-key-file dev.squarelet.com-key.pem "
"dev.squarelet.com "
'"*.dev.documentcloud.org" '
"dev.muckrock.com "
"dev.foiamachine.org "
"dev.mailhog.com"
)