-
Notifications
You must be signed in to change notification settings - Fork 153
/
tasks.py
597 lines (462 loc) · 17.4 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
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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
import os
import re
import subprocess
from sys import platform
from invoke import exceptions, task
ROOT = os.path.dirname(os.path.realpath(__file__))
LOCALE_DIR = os.path.realpath(os.path.abspath("network-api/locale"))
# Python commands's outputs are not rendering properly. Setting pty for *Nix system and
# "PYTHONUNBUFFERED" env var for Windows at True.
if platform == "win32":
PLATFORM_ARG = dict(env={"PYTHONUNBUFFERED": "True"})
else:
PLATFORM_ARG = dict(pty=True)
# The command for locale string abstraction is long and elaborate,
# so we build it here rather so that we don't clutter up the tasks.
locale_abstraction_instructions = " ".join(
[
"makemessages",
"--all",
"--keep-pot",
"--no-wrap",
"--ignore=network-api/networkapi/wagtailcustomization/*",
"--ignore=network-api/networkapi/settings.py",
"--ignore=network-api/networkapi/wagtailpages/templates/wagtailpages/pages/dear_internet_page.html",
"--ignore=dockerpythonvenv/*",
]
)
locale_abstraction_instructions_js = " ".join(
[
"makemessages",
"-d djangojs",
"--all",
"--extension js,jsx",
"--keep-pot",
"--no-wrap",
"--ignore=node_modules",
"--ignore=dockerpythonvenv/*",
"--ignore=network-api",
"--ignore=cypress",
]
)
def create_env_file(env_file):
"""Create or update an .env to work with a docker environment"""
with open(env_file) as f:
env_vars = f.read()
# We also need to make sure to use the correct db values based on our docker settings.
username = dbname = "postgres"
with open("docker-compose.yml") as d:
docker_compose = d.read()
username = re.search("POSTGRES_USER=(.*)", docker_compose).group(1) or username
dbname = re.search("POSTGRES_DB=(.*)", docker_compose).group(1) or dbname
# Update the DATABASE_URL env
new_db_url = f"DATABASE_URL=postgresql://{username}@postgres:5432/{dbname}"
old_db_url = re.search("DATABASE_URL=.*", env_vars)
env_vars = env_vars.replace(old_db_url.group(0), new_db_url)
# update the ALLOWED_HOSTS
new_hosts = "ALLOWED_HOSTS=*"
old_hosts = re.search("ALLOWED_HOSTS=.*", env_vars)
env_vars = env_vars.replace(old_hosts.group(0), new_hosts)
# create the new env file
with open(".env", "w") as f:
f.write(env_vars)
# Project setup and update
def l10n_block_inventory(ctx, stop=False):
"""
Update the block inventory.
To stop the containers after the command has run, pass `stop=True`.
"""
print("* Updating block information")
manage(ctx, "block_inventory", stop=stop)
@task(aliases=["create-super-user"])
def createsuperuser(ctx, stop=False):
"""
Create a superuser with username and password 'admin'.
To stop the containers after the command is run, pass the `--stop` flag.
"""
manage(ctx, "create_admin", stop=stop)
def initialize_database(ctx, slow=False):
"""
Initialize the database.
To stop all containers after each management command, pass `slow=True`.
"""
print("* Applying database migrations.")
migrate(ctx, stop=slow)
print("* Creating fake data")
manage(ctx, "load_fake_data", stop=slow)
print("* Sync locales")
manage(ctx, "sync_locale_trees", stop=slow)
l10n_block_inventory(ctx, stop=slow)
createsuperuser(ctx, stop=slow)
@task(aliases=["docker-new-db"])
def new_db(ctx, slow=False):
"""
Delete your database and create a new one with fake data.
If you are experiencing 'too many clients' errors while running this command, try
to pass the `--slow` flag. This will make sure that the containers are stopped
between the management commands and prevent that issue.
"""
print("* Starting the postgres service")
ctx.run("docker-compose up -d postgres")
print("* Delete the database")
ctx.run("docker-compose run --rm postgres dropdb --if-exists wagtail -hpostgres -Ufoundation")
print("* Create the database")
ctx.run("docker-compose run --rm postgres createdb wagtail -hpostgres -Ufoundation")
initialize_database(ctx, slow=slow)
print("Stop postgres service")
ctx.run("docker-compose down")
@task(aliases=["docker-catchup", "catchup"])
def catch_up(ctx):
"""Rebuild images, install dependencies, and apply migrations"""
print("* Stopping services first")
ctx.run("docker-compose down")
print("* Rebuilding images and install dependencies")
# The docker image build will install node and python dependencies.
ctx.run("docker-compose build")
print("* Applying database migrations.")
migrate(ctx)
print("* Updating block information.")
l10n_block_inventory(ctx)
print("\n* Start your dev server with:\n inv start or docker-compose up")
@task(aliases=["new-env", "docker-new-env"])
def setup(ctx):
"""Get a new dev environment and a new database with fake data"""
with ctx.cd(ROOT):
print("* Setting default environment variables")
if os.path.isfile(".env"):
print("* Stripping quotes and making sure your DATABASE_URL and ALLOWED_HOSTS are properly setup")
create_env_file(".env")
else:
print("* Creating a new .env")
create_env_file("env.default")
print("* Stopping project's containers and delete volumes if necessary")
ctx.run("docker-compose down --volumes")
print("* Building Docker images")
ctx.run("docker-compose build")
initialize_database(ctx)
print("\n* Start your dev server with:\n inv start or docker-compose up.")
@task(aliases=["start", "docker-start"])
def start_dev(ctx):
"""Start the dev server"""
with ctx.cd(ROOT):
ctx.run("docker-compose up")
@task(aliases=["start-lean", "docker-start-lean"])
def start_lean_dev(ctx):
"""Start the dev server without rebuilding frontend assets for a faster start up."""
print("Starting the dev server without rebuilding frontend assets...")
print("WARNING: Frontend assets may be outdated or missing if they haven't been built yet.")
ctx.run("docker-compose -f docker-compose.yml -f docker-compose-lean.yml up")
@task
def sh(c, service="backend"):
"""
Run bash in a local container
"""
subprocess.run(["docker-compose", "exec", service, "bash"])
# Javascript shorthands
@task(aliases=["docker-npm"])
def npm(ctx, command):
"""Shorthand to npm. inv docker-npm \"[COMMAND] [ARG]\" """
with ctx.cd(ROOT):
# Tell user to use npm_install instead if command includes 'install' or 'ci'
if "install" in command or "ci" in command:
print("Please use 'inv npm-install' instead.")
return
ctx.run(f"docker-compose run --rm backend npm {command}")
@task(aliases=["docker-npm-exec"])
def npm_exec(ctx, command):
"""Run npm in running container, e.g for npm install."""
with ctx.cd(ROOT):
# Using 'exec' instead of 'run --rm' as /node_modules is not mounted.
# To make this persistent, use 'exec' to run in the running container.
try:
ctx.run(f"docker-compose exec --user=root backend npm {command}")
except exceptions.UnexpectedExit:
print("This command requires a running container.\n")
print("Please run 'inv start' or 'inv start-lean' in a separate terminal window first.")
@task(aliases=["docker-npm-install"])
def npm_install(ctx):
"""Install Node dependencies"""
with ctx.cd(ROOT):
npm_exec(ctx, "ci")
@task(aliases=["copy-stage-db"])
def copy_staging_database(ctx):
with ctx.cd(ROOT):
ctx.run("node copy-db.js")
@task(aliases=["copy-prod-db"])
def copy_production_database(ctx):
with ctx.cd(ROOT):
ctx.run("node copy-db.js --prod")
# Python shorthands
@task
def pyrun(ctx, command, stop=False):
"""
Shorthand to commands with the activated Python virutalenv.
To stop the containers after the command has been run, pass the `--stop` flag.
"""
with ctx.cd(ROOT):
ctx.run(
f'docker-compose run --rm backend bash -c "source ./dockerpythonvenv/bin/activate && {command}"',
**PLATFORM_ARG,
)
if stop:
ctx.run("docker-compose stop")
@task(aliases=["docker-manage"])
def manage(ctx, command, stop=False):
"""
Shorthand to manage.py.
inv docker-manage \"[COMMAND] [ARG]\"
To stop the containers after the command has been run, pass the `--stop` flag.
"""
command = f"python network-api/manage.py {command}"
pyrun(ctx, command, stop=stop)
@task(aliases=["docker-djcheck"])
def djcheck(ctx, stop=False):
"""
Django system check framework.
To stop the containers after the command has run, pass the `--stop` flag.
"""
print("Running system check framework...")
manage(ctx, "check", stop=stop)
@task(aliases=["docker-migrate"])
def migrate(ctx, stop=False):
"""
Update the database schema.
To stop the containers after the command has run, pass the `--stop` flag.
"""
manage(ctx, "migrate --no-input", stop=stop)
@task(aliases=["docker-makemigrations"])
def makemigrations(ctx, args=""):
"""
Creates new migration(s) for apps. Optional: --args=""
"""
manage(ctx, f"makemigrations {args}")
@task(aliases=["docker-makemigrations-dryrun"])
def makemigrations_dryrun(ctx, args=""):
"""
Show new migration(s) for apps without creating them. Optional: --args=""
"""
manage(ctx, f"makemigrations {args} --dry-run")
# Tests
@task(aliases=["docker-test"])
def test(ctx):
"""Run tests."""
djcheck(ctx)
makemigrations_dryrun(ctx, args="--check")
test_python(ctx)
@task(aliases=["docker-test-python"])
def test_python(ctx, file="", n="auto", verbose=False):
"""
Run python tests.
Example calls:
- test_python(ctx)
- test_python(ctx, file="test_something.py")
- test_python(ctx, n=4, verbose=True)
Parameters:
- ctx: Context object (provided by Invoke)
- file: Optional string representing the path to a specific test file to run.
- n: Optional integer or string 'auto' representing the number of parallel tests to run.
Default is 'auto' which allows pytest to automatically determine the optimal number.
- verbose: Optional boolean flag indicating whether to print verbose output during testing. Default is False.
"""
parallel = f"-n {n}" if n != "1" else ""
v = "-v" if verbose else ""
# Don't run coverage if a file is specified
cov = "" if file else "--cov=network-api/networkapi --cov-report=term-missing"
command = f"pytest {v} {parallel} {file} --reuse-db {cov}"
pyrun(ctx, command)
# Linting
@task
def lint(ctx):
"""Run linting."""
lint_html(ctx)
lint_css(ctx)
lint_js(ctx)
lint_python(ctx)
@task
def lint_html(ctx):
"""Run HTML linting."""
# Skipping djlint format checking because it has consistency issues and issues with blocktrans.
# This should change when formatting is moved to a version using and AST.
# See also: https://github.com/Riverside-Healthcare/djLint/issues/493
# djlint_check(ctx)
#
# Use djhtml indent checking until format checking with djlint becomes possible.
djhtml_check(ctx)
djlint_lint(ctx)
@task
def lint_css(ctx):
"""Run CSS linting."""
npm(ctx, "run lint:css")
@task
def lint_js(ctx):
"""Run JavaScript linting."""
npm(ctx, "run lint:js")
@task
def lint_python(ctx):
"""Run Python linting."""
flake8(ctx)
isort_check(ctx)
black_check(ctx)
# Formatting
@task
def format(ctx):
"""Run formatters."""
format_html(ctx)
format_css(ctx)
format_js(ctx)
format_python(ctx)
@task
def format_html(ctx):
"""Run HTML formatting."""
# Skipping djlint formatting because it has consistency issues and issues with blocktrans.
# This should change when formatting is moved to a version using and AST.
# See also: https://github.com/Riverside-Healthcare/djLint/issues/493
# djlint_format(ctx)
#
# Indent HTML until full formatting with djlint becomes possible
djhtml_format(ctx)
@task
def format_css(ctx):
"""Run css formatting."""
npm(ctx, "run fix:css")
@task
def format_js(ctx):
"""Run javascript formatting."""
npm(ctx, "run fix:js")
@task
def format_python(ctx):
"""Run python formatting."""
isort(ctx)
black(ctx)
# Tooling
@task(help={"args": "Override the arguments passed to black."})
def black(ctx, args=None):
"""Run black code formatter."""
args = args or "."
pyrun(ctx, command=f"black {args}")
@task
def black_check(ctx):
"""Run black code formatter in check mode."""
black(ctx, ". --check")
@task(help={"args": "Override the arguments passed to djhtml."})
def djhtml(ctx, args=None):
"""Run djhtml code indenter."""
args = args or "-h"
pyrun(ctx, command=f"djhtml {args}")
@task
def djhtml_check(ctx):
"""Run djhtml code indenter in check mode."""
djhtml(ctx, args="-c maintenance/ network-api/")
@task
def djhtml_format(ctx):
"""Run djhtml code indenter in formatting mode."""
djhtml(ctx, args="maintenance/ network-api/")
@task(help={"args": "Override the arguments passed to djlint."})
def djlint(ctx, args=None):
"""Run djlint code formatter and linter."""
args = args or "."
pyrun(ctx, command=f"djlint {args}")
@task
def djlint_check(ctx):
"""Run djlint in format checking mode."""
djlint(ctx, ". --check")
@task
def djlint_format(ctx):
"""Run djlint formatting mode."""
djlint(ctx, ". --reformat --quiet")
@task
def djlint_lint(ctx):
"""Run djlint in linting mode."""
djlint(ctx, ". --lint")
@task
def flake8(ctx):
"""Run flake8."""
pyrun(ctx, "flake8 .")
@task(help={"args": "Override the arguments passed to isort."})
def isort(ctx, args=None):
"""Run isort code formatter."""
args = args or "."
pyrun(ctx, command=f"isort {args}")
@task
def isort_check(ctx):
"""Run isort code formatter in check mode."""
isort(ctx, ". --check-only")
@task(help={"args": "Override the arguments passed to mypy."})
def mypy(ctx, args=None):
"""Run mypy type checking on the project."""
args = args or "network-api"
pyrun(ctx, command=f"mypy {args}")
# Pip-tools
def _pip_compile_workaround(requirement_file, additional_commands=""):
"""
A workaround to fix 'Device or resource busy' error when running
pip-compile in docker container where the output file is a mounted volume.
This is because pip-compile tries to replace the output file, and you can't replace
mount points. However, you can write to them. So we work around this by using an
unmounted .tmp file as intermediary, and then write the changes to the mounted file.
"""
output_file = requirement_file.replace(".in", ".txt")
temp_file = output_file + ".tmp"
return (
f"cp {output_file} {temp_file}&&pip-compile {requirement_file}"
+ " "
+ additional_commands
+ f" --output-file={temp_file}&&cp {temp_file} {output_file}&&rm {temp_file}"
)
@task(aliases=["docker-pip-compile"], optional=["command"])
def pip_compile(ctx, filename="requirements.in", command=""):
"""Shorthand to pip-tools. inv pip-compile \"[filename]\" \"[COMMAND] [ARG]\" """
with ctx.cd(ROOT):
ctx.run(
f"""docker-compose run --rm backend bash -c '{_pip_compile_workaround(filename, command)}'""",
**PLATFORM_ARG,
)
@task(aliases=["docker-pip-compile-lock"])
def pip_compile_lock(ctx):
"""Lock prod and dev dependencies"""
with ctx.cd(ROOT):
# Running in separate steps as the dev-requirements.in needs to read requirements.txt
command = _pip_compile_workaround("requirements.in") + "&&"
command = command + _pip_compile_workaround("dev-requirements.in")
ctx.run(
f"""docker-compose run --rm backend bash -c '{command}'""",
**PLATFORM_ARG,
)
@task(aliases=["docker-pip-sync"])
def pip_sync(ctx):
"""Sync your python virtualenv"""
with ctx.cd(ROOT):
try:
ctx.run(
# Using 'exec' instead of 'run --rm' as /dockerpythonvenv is not mounted.
# To make this persistent, use 'exec' to run in the running container.
"docker-compose exec backend ./dockerpythonvenv/bin/pip-sync requirements.txt dev-requirements.txt",
**PLATFORM_ARG,
)
except exceptions.UnexpectedExit:
print("This command requires a running container.\n")
print("Please run 'inv start' or 'inv start-lean' in a separate terminal window first.")
# Translation
@task(aliases=["docker-makemessages"])
def makemessages(ctx):
"""Extract all template messages in .po files for localization"""
ctx.run("./translation-management.sh import")
manage(ctx, locale_abstraction_instructions)
manage(ctx, locale_abstraction_instructions_js)
ctx.run("./translation-management.sh export")
@task(aliases=["docker-compilemessages"])
def compilemessages(ctx):
"""Compile the latest translations"""
with ctx.cd(ROOT):
ctx.run(
"docker-compose run --rm -w /app/network-api backend "
"../dockerpythonvenv/bin/python manage.py compilemessages",
**PLATFORM_ARG,
)
@task(aliases=["staging-to-review-app"])
def staging_db_to_review_app(ctx, review_app_name):
"""
Copy Staging DB to a specific Review App. inv staging-to-review-app \"[REVIEW_APP_NAME]\"
"""
from copy_staging_db_to_review_app import main
main(ctx, review_app_name)