forked from MozillaFoundation/donate-wagtail
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tasks.py
167 lines (133 loc) · 5.51 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
from sys import platform
import re
from invoke import task
# Workaround for homebrew installation of Python (https://bugs.python.org/issue22490)
import os
os.environ.pop('__PYVENV_LAUNCHER__', None)
ROOT = os.path.dirname(os.path.realpath(__file__))
# 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)
def create_docker_env_file(env_file):
"""Create or update an .env to work with a docker environment"""
with open(env_file, 'r') as f:
env_vars = f.read()
# update the DATABASE_URL env
new_db_url = "DATABASE_URL=postgres://donate:mozilla@postgres:5432/donate"
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 env
new_hosts = "ALLOWED_HOSTS=*"
old_hosts = re.search('ALLOWED_HOSTS=.*', env_vars)
env_vars = env_vars.replace(old_hosts.group(0), new_hosts)
# Update REDIS_URL env
new_redis_url = "REDIS_URL=redis://redis:6379/0"
old_redis_url = re.search('REDIS_URL=.*', env_vars)
env_vars = env_vars.replace(old_redis_url.group(0), new_redis_url)
# create the new env file
with open('.env', 'w') as f:
f.write(env_vars)
def docker_create_super_user(ctx):
# Windows doesn't support pty, skipping this step
if platform == 'win32':
print("\nPTY is not supported on Windows.\n"
"To create an admin user:\n"
"docker-compose run --rm backend pipenv run python manage.py createsuperuser\n")
else:
print("* Creating superuser.")
ctx.run(
"docker-compose run --rm backend pipenv run python manage.py createsuperuser",
pty=True
)
@task
def docker_manage(ctx, command):
"""Shorthand to manage.py. inv docker-manage \"[COMMAND] [ARG]\""""
with ctx.cd(ROOT):
ctx.run(f"docker-compose run --rm backend pipenv run python manage.py {command}", **PLATFORM_ARG)
@task
def docker_pipenv(ctx, command):
"""Shorthand to pipenv. inv docker-pipenv \"[COMMAND] [ARG]\""""
with ctx.cd(ROOT):
ctx.run(f"docker-compose run --rm backend pipenv {command}")
@task
def docker_npm(ctx, command):
"""Shorthand to npm. inv docker-npm \"[COMMAND] [ARG]\""""
with ctx.cd(ROOT):
ctx.run(f"docker-compose run --rm watch-static-files npm {command}")
@task
def docker_migrate(ctx):
"""Updates database schema"""
docker_manage(ctx, "migrate --no-input")
@task
def docker_makemigrations(ctx):
"""Creates new migration(s) for apps"""
docker_manage(ctx, "makemigrations")
@task
def docker_makemessages(ctx):
"""Extract all template messages in .po files for localization"""
docker_manage(ctx, "makemessages --keep-pot --no-wrap")
docker_manage(ctx, "makemessages -d djangojs --keep-pot --no-wrap --ignore=node_modules")
os.replace("donate/locale/django.pot", "donate/locale/templates/LC_MESSAGES/django.pot")
os.replace("donate/locale/djangojs.pot", "donate/locale/templates/LC_MESSAGES/djangojs.pot")
@task
def docker_compilemessages(ctx):
"""Compile the latest translations"""
docker_manage(ctx, "compilemessages")
@task
def docker_test_python(ctx):
"""Run python tests"""
print("* Running flake8")
ctx.run("docker-compose run --rm backend pipenv run flake8 tasks.py donate/", **PLATFORM_ARG)
print("* Running tests")
docker_manage(ctx, "test --settings=donate.settings --configuration=Testing")
@task
def docker_test_node(ctx):
"""Run node tests"""
print("* Running tests")
ctx.run("docker-compose run --rm watch-static-files npm run test", **PLATFORM_ARG)
@task
def docker_new_db(ctx):
"""Delete your database and create a new one with fake data"""
print("* Stopping services first")
ctx.run("docker-compose down")
print("* Deleting database")
ctx.run("docker volume rm donate-wagtail_postgres_data")
print("* Applying database migrations.")
docker_migrate(ctx)
print("* Creating fake data")
docker_manage(ctx, "load_fake_data")
docker_create_super_user(ctx)
@task(aliases=["docker-catchup"])
def docker_catch_up(ctx):
"""Rebuild images and apply migrations"""
print("* Stopping services first")
ctx.run("docker-compose down")
print("* Rebuilding images and install dependencies")
ctx.run("docker-compose build")
print("* Applying database migrations.")
docker_migrate(ctx)
@task
def docker_new_env(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("* Updating your .env")
create_docker_env_file(".env")
else:
print("* Creating a new .env")
create_docker_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 --no-cache backend watch-static-files", **PLATFORM_ARG)
ctx.run("docker-compose build backend-worker", **PLATFORM_ARG)
print("* Applying database migrations.")
docker_migrate(ctx)
print("* Creating fake data")
docker_manage(ctx, "load_fake_data")
docker_create_super_user(ctx)
print("\n* Start your dev server with:\n docker-compose up")