-
Notifications
You must be signed in to change notification settings - Fork 3
/
fabfile.py
451 lines (346 loc) · 10.7 KB
/
fabfile.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
import datetime
import os
import subprocess
from shlex import quote
from invoke import run as local
from invoke.tasks import task
# Process .env file
if os.path.exists(".env"):
with open(".env", "r") as f:
for line in f.readlines():
if not line or line.startswith("#") or "=" not in line:
continue
var, value = line.strip().split("=", 1)
os.environ.setdefault(var, value)
FRONTEND = os.getenv("FRONTEND", "docker")
PROJECT_DIR = "/app"
LOCAL_DUMP_DIR = "database_dumps"
PRODUCTION_APP_INSTANCE = "tnaomegauiprototypes-production"
STAGING_APP_INSTANCE = "tnaomegauiprototypes-staging"
DEVELOPMENT_APP_INSTANCE = "tnaomegauiprototypes-dev"
LOCAL_MEDIA_DIR = "media"
LOCAL_IMAGES_DIR = LOCAL_MEDIA_DIR + "/original_images"
LOCAL_DATABASE_NAME = PROJECT_NAME = "tnaomegauiprototypes"
LOCAL_DATABASE_USERNAME = "tnaomegauiprototypes"
############
# Production
############
def dexec(cmd, service="web"):
return local(
"docker-compose exec -T {} bash -c {}".format(quote(service), quote(cmd))
)
@task
def build(c):
"""
Build the development environment (call this first)
"""
directories_to_init = [LOCAL_DUMP_DIR, LOCAL_MEDIA_DIR]
directories_arg = " ".join(directories_to_init)
group = subprocess.check_output(["id", "-gn"], encoding="utf-8").strip()
local("mkdir -p " + directories_arg)
local("chown -R $USER:{} {}".format(group, directories_arg))
local("chmod -R 775 " + directories_arg)
local("docker-compose pull")
local("docker-compose build")
@task
def start(c):
"""
Start the development environment
"""
if FRONTEND == "local":
local("docker-compose up -d")
else:
local(
"docker-compose -f docker-compose.yml -f docker/docker-compose-frontend.yml up -d"
)
@task
def stop(c):
"""
Stop the development environment
"""
local("docker-compose stop")
@task
def restart(c):
"""
Restart the development environment
"""
stop(c)
start(c)
@task
def destroy(c):
"""
Destroy development environment containers (database will lost!)
"""
local("docker-compose down")
@task
def sh(c, service="web"):
"""
Run bash in a local container
"""
subprocess.run(["docker-compose", "exec", service, "bash"])
@task
def psql(c, command=None):
"""
Connect to the local postgres DB using psql
"""
cmd_list = [
"docker-compose",
"exec",
"db",
"psql",
*["-d", LOCAL_DATABASE_NAME],
*["-U", LOCAL_DATABASE_USERNAME],
]
if command:
cmd_list.extend(["-c", command])
subprocess.run(cmd_list)
# TODO check the rest of these work correctly from here down
@task
def delete_docker_database(c, local_database_name=LOCAL_DATABASE_NAME):
dexec(
"dropdb --if-exists --host db --username={project_name} {database_name}".format(
project_name=PROJECT_NAME, database_name=LOCAL_DATABASE_NAME
),
"db",
)
dexec(
"createdb --host db --username={project_name} {database_name}".format(
project_name=PROJECT_NAME, database_name=LOCAL_DATABASE_NAME
),
"db",
)
@task
def import_data(c, database_filename):
"""
Import local data file to the db container.
"""
# Copy the data file to the db container
delete_docker_database(c)
# Import the database file to the db container
dexec(
"pg_restore --clean --no-acl --if-exists --no-owner --host db \
--username={project_name} -d {database_name} {database_filename}".format(
project_name=PROJECT_NAME,
database_name=LOCAL_DATABASE_NAME,
database_filename=database_filename,
),
service="db",
)
print(
"Any superuser accounts you previously created locally will have been wiped and will need to be recreated."
)
def delete_local_renditions(c, local_database_name=LOCAL_DATABASE_NAME):
psql(c, "DELETE FROM images_rendition;")
#########
# Production
#########
@task
def pull_production_media(c):
"""Pull media from production AWS S3"""
pull_media_from_s3_heroku(c, PRODUCTION_APP_INSTANCE)
@task
def pull_production_images(c):
"""Pull images from production AWS S3"""
pull_images_from_s3_heroku(c, PRODUCTION_APP_INSTANCE)
@task
def pull_production_data(c):
"""Pull database from production Heroku Postgres"""
pull_database_from_heroku(c, PRODUCTION_APP_INSTANCE)
@task
def production_shell(c):
"""Spin up a one-time Heroku production dyno and connect to shell"""
open_heroku_shell(c, PRODUCTION_APP_INSTANCE)
#########
# Staging
#########
@task
def pull_staging_media(c):
"""Pull media from staging AWS S3"""
pull_media_from_s3_heroku(c, STAGING_APP_INSTANCE)
@task
def pull_staging_images(c):
"""Pull images from staging AWS S3"""
pull_images_from_s3_heroku(c, STAGING_APP_INSTANCE)
@task
def pull_staging_data(c):
"""Pull database from staging Heroku Postgres"""
pull_database_from_heroku(c, STAGING_APP_INSTANCE)
@task
def staging_shell(c):
"""Spin up a one-time Heroku staging dyno and connect to shell"""
open_heroku_shell(c, STAGING_APP_INSTANCE)
#############
# Development
#############
@task
def pull_dev_media(c):
"""Pull media from development AWS S3"""
pull_media_from_s3_heroku(c, DEVELOPMENT_APP_INSTANCE)
@task
def pull_dev_images(c):
"""Pull images from development AWS S3"""
pull_images_from_s3_heroku(c, DEVELOPMENT_APP_INSTANCE)
@task
def pull_dev_data(c):
"""Pull database from development Heroku Postgres"""
pull_database_from_heroku(c, DEVELOPMENT_APP_INSTANCE)
@task
def dev_shell(c):
"""Spin up a one-time Heroku development dyno and connect to shell"""
open_heroku_shell(c, DEVELOPMENT_APP_INSTANCE)
def delete_local_database(c, local_database_name=LOCAL_DATABASE_NAME):
local(
"dropdb --if-exists {database_name}".format(database_name=LOCAL_DATABASE_NAME)
)
####
# S3
####
def aws(c, command, aws_access_key_id, aws_secret_access_key):
return local(
"AWS_ACCESS_KEY_ID={access_key_id} AWS_SECRET_ACCESS_KEY={secret_key} "
"aws {command}".format(
access_key_id=aws_access_key_id,
secret_key=aws_secret_access_key,
command=command,
)
)
def pull_media_from_s3(
c,
aws_access_key_id,
aws_secret_access_key,
aws_storage_bucket_name,
local_media_dir=LOCAL_MEDIA_DIR,
):
aws_cmd = "s3 sync --delete s3://{bucket_name} {local_media}".format(
bucket_name=aws_storage_bucket_name,
local_media=local_media_dir,
)
aws(c, aws_cmd, aws_access_key_id, aws_secret_access_key)
def pull_images_from_s3_heroku(c, app_instance):
aws_access_key_id = get_heroku_variable(c, app_instance, "AWS_ACCESS_KEY_ID")
aws_secret_access_key = get_heroku_variable(
c, app_instance, "AWS_SECRET_ACCESS_KEY"
)
aws_storage_bucket_name = get_heroku_variable(
c, app_instance, "AWS_STORAGE_BUCKET_NAME"
)
pull_images_from_s3(
c, aws_access_key_id, aws_secret_access_key, aws_storage_bucket_name
)
def pull_images_from_s3(
c,
aws_access_key_id,
aws_secret_access_key,
aws_storage_bucket_name,
local_images_dir=LOCAL_IMAGES_DIR,
):
aws_cmd = (
"s3 sync --delete s3://{bucket_name}/original_images {local_media}".format(
bucket_name=aws_storage_bucket_name, local_media=local_images_dir
)
)
aws(c, aws_cmd, aws_access_key_id, aws_secret_access_key)
# The above command just syncs the original images, so we need to drop the wagtailimages_renditions
# table so that the renditions will be re-created when requested on the local build.
delete_local_renditions(c)
########
# Heroku
########
def pull_media_from_s3_heroku(c, app_instance):
aws_access_key_id = get_heroku_variable(c, app_instance, "AWS_ACCESS_KEY_ID")
aws_secret_access_key = get_heroku_variable(
c, app_instance, "AWS_SECRET_ACCESS_KEY"
)
aws_storage_bucket_name = get_heroku_variable(
c, app_instance, "AWS_STORAGE_BUCKET_NAME"
)
pull_media_from_s3(
c, aws_access_key_id, aws_secret_access_key, aws_storage_bucket_name
)
def pull_database_from_heroku(c, app_instance):
datestamp = datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
local(
"heroku pg:backups:download --output={dump_folder}/{datestamp}.dump --app {app}".format(
app=app_instance, dump_folder=LOCAL_DUMP_DIR, datestamp=datestamp
),
)
import_data(c, f"/app/{LOCAL_DUMP_DIR}/{datestamp}.dump")
local(
"rm {dump_folder}/{datestamp}.dump".format(
dump_folder=LOCAL_DUMP_DIR,
datestamp=datestamp,
),
)
def open_heroku_shell(c, app_instance, shell_command="bash"):
subprocess.call(
[
"heroku",
"run",
shell_command,
"-a",
app_instance,
]
)
#######
# Utils
#######
def make_bold(msg):
return "\033[1m{}\033[0m".format(msg)
@task
def dellar_snapshot(c, filename):
"""Snapshot the database, files will be stored in the db container"""
dexec(
"pg_dump -d {database_name} -U {database_username} > {filename}.psql".format(
database_name=LOCAL_DATABASE_NAME,
database_username=LOCAL_DATABASE_USERNAME,
filename=filename,
),
service="db",
),
print("Database snapshot created")
@task
def dellar_restore(c, filename):
""" Restore the database from a snapshot in the db container """
delete_docker_database(c)
dexec(
"psql -U {database_username} -d {database_name} < {filename}.psql".format(
database_name=LOCAL_DATABASE_NAME,
database_username=LOCAL_DATABASE_USERNAME,
filename=filename,
),
service="db",
),
print("Database restored.")
@task
def docker_coverage(c):
return dexec(
"coverage erase && coverage run manage.py test \
--settings=tnaomegauiprototypes.settings.test && coverage report",
)
def get_heroku_variable(c, app_instance, variable):
return local(
"heroku config:get {var} --app {app}".format(app=app_instance, var=variable)
).stdout.strip()
@task
def run_test(c):
"""
Run python tests in the web container
"""
subprocess.call(
[
"docker-compose",
"exec",
"web",
"python",
"manage.py",
"test",
"--settings=tnaomegauiprototypes.settings.test",
"--parallel",
]
)
@task
def migrate(c):
"""
Run database migrations
"""
subprocess.run(["docker-compose", "run", "--rm", "web", "./manage.py", "migrate"])