-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathfabfile.py
293 lines (223 loc) · 7.55 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
import datetime
import os
from shlex import quote
import subprocess
from invoke import run as local
from invoke.tasks import task
# Process .env file
if os.path.exists(".env"):
with open(".env") 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)
PROJECT_DIR = "/app"
LOCAL_DUMP_DIR = "database_dumps"
PRODUCTION_APP_INSTANCE = "wagtail-org-production"
STAGING_APP_INSTANCE = "wagtail-org-staging"
DEVELOPMENT_APP_INSTANCE = "wagtail-org-dev"
LOCAL_MEDIA_DIR = "media"
LOCAL_IMAGES_DIR = LOCAL_MEDIA_DIR + "/original_images"
LOCAL_DATABASE_NAME = PROJECT_NAME = "wagtailorg"
LOCAL_DATABASE_USERNAME = "wagtailorg"
############
# Production
############
def dexec(cmd, service="web"):
return local(f"docker-compose exec -T {quote(service)} bash -c {quote(cmd)}")
@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)
@task
def delete_docker_database(c, local_database_name=LOCAL_DATABASE_NAME):
dexec(
f"dropdb --if-exists --host db --username={PROJECT_NAME} {LOCAL_DATABASE_NAME}",
"db",
)
dexec(
f"createdb --host db --username={PROJECT_NAME} {LOCAL_DATABASE_NAME}",
"db",
)
# Create extension schema, for error-free restores from Heroku backups
# (see https://devcenter.heroku.com/changelog-items/2446)
psql(c, "CREATE SCHEMA heroku_ext;")
@task(
help={
"new_default_site_hostname": "Pass an empty string to skip the default site's hostname replacement"
" - default is 'localhost:8000'"
}
)
def import_data(
c, database_filename: str, new_default_site_hostname: str = "localhost:8000"
):
"""
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(
f"pg_restore --clean --no-acl --if-exists --no-owner --host db \
--username={PROJECT_NAME} -d {LOCAL_DATABASE_NAME} {database_filename}",
service="db",
)
# When pulling data from a heroku environment, the hostname in wagtail > sites is not updated.
# This means when browsing the site locally with this pulled data you can end up with links to staging, or even
# the live site.
# --> let's update the default site hostname values
if new_default_site_hostname:
if ":" in new_default_site_hostname:
hostname, port = new_default_site_hostname.split(":")
else:
hostname, port = new_default_site_hostname, "8000"
assert hostname and port and port.isdigit()
dexec(
f"psql -c \"UPDATE wagtailcore_site SET hostname = '{hostname}', port = {port} WHERE is_default_site IS TRUE;\""
)
print(f"Default site's hostname was updated to '{hostname}:{port}'.")
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
#############
def delete_local_database(c, local_database_name=LOCAL_DATABASE_NAME):
local(f"dropdb --if-exists {LOCAL_DATABASE_NAME}")
####
# S3
####
def aws(c, command, aws_access_key_id, aws_secret_access_key):
return local(
f"aws {command}",
env={
"AWS_ACCESS_KEY_ID": aws_access_key_id,
"AWS_SECRET_ACCESS_KEY": aws_secret_access_key,
},
)
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 = f"s3 sync --delete s3://{aws_storage_bucket_name} {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 = f"s3 sync --delete s3://{aws_storage_bucket_name}/original_images {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(
f"heroku pg:backups:download --output={LOCAL_DUMP_DIR}/{datestamp}.dump --app {app_instance}",
)
import_data(c, f"/app/{LOCAL_DUMP_DIR}/{datestamp}.dump")
local(
f"rm {LOCAL_DUMP_DIR}/{datestamp}.dump",
)
def open_heroku_shell(c, app_instance, shell_command="bash"):
subprocess.call(
[
"heroku",
"run",
shell_command,
"-a",
app_instance,
]
)
#######
# Utils
#######
def get_heroku_variable(c, app_instance, variable):
return local(
f"heroku config:get {variable} --app {app_instance}",
hide=True,
).stdout.strip()