forked from MuckRock/squarelet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtasks.py
320 lines (252 loc) · 8.43 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
# Standard Library
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):
"""Run the test suite"""
create_switch = "--create-db" if create_db 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} {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 --source squarelet -m py.test",
)
)
c.run(
DOCKER_COMPOSE_RUN_OPT_USER.format(
opt="-e DJANGO_SETTINGS_MODULE=config.settings.test",
service="squarelet_django",
cmd=f"coverage html",
)
)
# 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 squarelet && "
"isort config/urls.py && "
"isort config/settings"
)
)
# 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="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/frontend", service="squarelet_django", cmd=f"npm {cmd}"
)
)
@task
def heroku(c, staging=False):
"""Run commands on heroku"""
if staging:
app = "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
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"
)