This repository has been archived by the owner on Mar 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
tasks.py
248 lines (205 loc) · 8.61 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
import os
from sys import platform
import re
from invoke import task
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_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()
# 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', 'r') as d:
docker_compose = d.read()
username = re.search('POSTGRES_USER=(.*)', docker_compose).group(1) or username
# password = re.search('POSTGRES_PASSWORD=(.*)', docker_compose).group(1) or password
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 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 create_super_user(ctx):
preamble = "from django.contrib.auth import get_user_model;User = get_user_model();"
create = "User.objects.create_superuser('admin', '[email protected]', 'admin')"
manage(ctx, f'shell -c "{preamble} {create}"')
print("\nCreated superuser `admin` with password `admin`.")
# Project setup and update
@task(aliases=["docker-new-db"])
def new_db(ctx):
"""Delete your database and create a new one with fake data"""
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 donate -h postgres -U donate")
print("* Create the database")
ctx.run("docker-compose run --rm postgres createdb donate -h postgres -U donate")
print("* Applying database migrations.")
migrate(ctx)
print("* Creating fake data")
manage(ctx, "load_fake_data")
create_super_user(ctx)
print("Stop postgres service")
ctx.run("docker-compose down")
@task(aliases=["docker-catchup", "docker-catch-up", "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")
ctx.run("docker-compose build")
print("* Install Node dependencies")
npm_install(ctx)
print("* Sync Python dependencies")
pip_sync(ctx)
print("* Applying database migrations.")
migrate(ctx)
print("\n* Start your dev server with:\n docker-compose up")
@task(aliases=["docker-new-env", "setup"])
def 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_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 backend watch-static-files")
ctx.run("docker-compose build backend-worker")
print("* Install Node dependencies")
npm_install(ctx)
print("* Creating a Python virtualenv")
ctx.run(
"docker-compose run --rm backend python -m venv dockerpythonvenv",
**PLATFORM_ARG,
)
print("Done!")
print("* Updating pip")
ctx.run(
"docker-compose run --rm backend ./dockerpythonvenv/bin/pip install -U pip==20.0.2",
**PLATFORM_ARG,
)
print("* Installing pip-tools")
ctx.run(
"docker-compose run --rm backend ./dockerpythonvenv/bin/pip install pip-tools",
**PLATFORM_ARG,
)
print("* Sync Python dependencies")
pip_sync(ctx)
print("* Applying database migrations.")
migrate(ctx)
print("* Creating fake data")
manage(ctx, "load_fake_data")
create_super_user(ctx)
print("\n* Start your dev server with:\n docker-compose up")
# Django shorthands
@task(aliases=["docker_manage"])
def manage(ctx, command):
"""Shorthand to manage.py. inv docker-manage \"[COMMAND] [ARG]\""""
with ctx.cd(ROOT):
ctx.run(f"docker-compose run --rm backend ./dockerpythonvenv/bin/python manage.py {command}", **PLATFORM_ARG) # noqa
@task(aliases=["docker_migrate"])
def migrate(ctx):
"""Updates database schema"""
manage(ctx, "migrate --no-input")
@task(aliases=["docker_makemigrations"])
def makemigrations(ctx):
"""Creates new migration(s) for apps"""
manage(ctx, "makemigrations")
# Javascript shorthands
@task(aliases=["docker_npm"])
def 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(aliases=["docker-npm-install"])
def npm_install(ctx):
"""Install Node dependencies"""
with ctx.cd(ROOT):
ctx.run("docker-compose run --rm watch-static-files npm install")
# Localisation
@task(aliases=["docker_makemessages"])
def makemessages(ctx):
"""Extract all template messages in .po files for localization"""
ctx.run("./translation-management.sh import")
manage(ctx, "makemessages --all --keep-pot --no-wrap --ignore=dockerpythonvenv/* --ignore=donate/thunderbird/*")
manage(ctx, "makemessages -d djangojs --all --keep-pot --no-wrap --ignore=node_modules --ignore=dockerpythonvenv/* --ignore=donate/thunderbird/*") # noqa
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")
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/donate backend "
"../dockerpythonvenv/bin/python /app/manage.py compilemessages",
**PLATFORM_ARG
)
# Tests
@task(aliases=["docker-test"])
def test(ctx):
"""Run both Node and Python tests"""
test_node(ctx)
test_python(ctx)
@task(aliases=["docker_test_python"])
def test_python(ctx):
"""Run python tests"""
print("* Running flake8")
ctx.run("docker-compose run --rm backend ./dockerpythonvenv/bin/python -m flake8 tasks.py donate/", **PLATFORM_ARG) # noqa
print("* Running tests")
manage(ctx, "test --settings=donate.settings --configuration=Testing")
@task(aliases=["docker_test_node"])
def test_node(ctx):
"""Run node tests"""
print("* Running tests")
ctx.run("docker-compose run --rm watch-static-files npm run test", **PLATFORM_ARG)
# Pip-tools
@task(aliases=["docker-pip-compile"])
def pip_compile(ctx, command):
"""Shorthand to pip-tools. inv pip-compile \"[COMMAND] [ARG]\""""
with ctx.cd(ROOT):
ctx.run(
f"docker-compose run --rm backend ./dockerpythonvenv/bin/pip-compile {command}",
**PLATFORM_ARG,
)
@task(aliases=["docker-pip-compile-lock"])
def pip_compile_lock(ctx):
"""Lock prod and dev dependencies"""
with ctx.cd(ROOT):
ctx.run(
"docker-compose run --rm backend ./dockerpythonvenv/bin/pip-compile",
**PLATFORM_ARG,
)
ctx.run(
"docker-compose run --rm backend ./dockerpythonvenv/bin/pip-compile dev-requirements.in",
**PLATFORM_ARG,
)
@task(aliases=["docker-pip-sync"])
def pip_sync(ctx):
"""Sync your python virtualenv"""
with ctx.cd(ROOT):
ctx.run(
"docker-compose run --rm backend ./dockerpythonvenv/bin/pip-sync requirements.txt dev-requirements.txt", # noqa
**PLATFORM_ARG,
)