forked from bookwyrm-social/bookwyrm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bw-dev
executable file
·337 lines (319 loc) · 9.47 KB
/
bw-dev
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
#!/usr/bin/env bash
# exit on errors
set -e
# check if we're in DEBUG mode
DEBUG=$(sed <.env -ne 's/^DEBUG=//p')
# disallow certain commands when debug is false
function prod_error {
if [ "$DEBUG" != "true" ]; then
echo "This command is not safe to run in production environments"
exit 1
fi
}
# import our ENV variables
# catch exits and give a friendly error message
function showerr {
echo "Failed to load configuration! You may need to update your .env and quote values with special characters in them."
}
trap showerr EXIT
source .env
trap - EXIT
if docker compose &> /dev/null ; then
DOCKER_COMPOSE="docker compose"
else
DOCKER_COMPOSE="docker-compose"
fi
function clean {
$DOCKER_COMPOSE stop
$DOCKER_COMPOSE rm -f
}
function runweb {
$DOCKER_COMPOSE run --rm web "$@"
}
function execdb {
$DOCKER_COMPOSE exec db $@
}
function execweb {
$DOCKER_COMPOSE exec web "$@"
}
function initdb {
runweb python manage.py initdb "$@"
}
function migrate {
runweb python manage.py migrate "$@"
}
function admin_code {
runweb python manage.py admin_code
}
function awscommand {
# expose env vars
export AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID}
export AWS_SECRET_ACCESS_KEY=${AWS_SECRET_ACCESS_KEY}
export AWS_DEFAULT_REGION=${AWS_S3_REGION_NAME}
# first arg is mountpoint, second is the whole aws command
docker run --rm -it -v $1\
-e AWS_ACCESS_KEY_ID -e AWS_SECRET_ACCESS_KEY -e AWS_DEFAULT_REGION\
amazon/aws-cli $2
}
CMD=$1
if [ -n "$CMD" ]; then
shift
fi
# show commands as they're executed
set -x
case "$CMD" in
up)
$DOCKER_COMPOSE up --build "$@"
;;
down)
$DOCKER_COMPOSE down
;;
service_ports_web)
prod_error
$DOCKER_COMPOSE run --rm --service-ports web
;;
initdb)
initdb "$@"
;;
resetdb)
prod_error
$DOCKER_COMPOSE rm -svf
docker volume rm -f bookwyrm_media_volume bookwyrm_pgdata bookwyrm_redis_activity_data bookwyrm_redis_broker_data bookwyrm_static_volume
$DOCKER_COMPOSE build
migrate
migrate django_celery_beat
initdb
runweb python manage.py compile_themes
runweb python manage.py collectstatic --no-input
admin_code
;;
makemigrations)
prod_error
runweb python manage.py makemigrations "$@"
;;
migrate)
migrate "$@"
;;
bash)
runweb bash
;;
shell)
runweb python manage.py shell
;;
dbshell)
execdb psql -U ${POSTGRES_USER} ${POSTGRES_DB}
;;
restart_celery)
$DOCKER_COMPOSE restart celery_worker
;;
pytest)
prod_error
runweb pytest --no-cov-on-fail "$@"
;;
pytest_coverage_report)
prod_error
runweb pytest -n 3 --cov-report term-missing "$@"
;;
compile_themes)
runweb python manage.py compile_themes
;;
collectstatic)
runweb python manage.py collectstatic --no-input
;;
makemessages)
prod_error
runweb django-admin makemessages --no-wrap --ignore=venv -l en_US $@
;;
compilemessages)
runweb django-admin compilemessages --ignore venv $@
;;
update_locales)
prod_error
git fetch origin l10n_main:l10n_main
git checkout l10n_main locale/ca_ES
git checkout l10n_main locale/de_DE
git checkout l10n_main locale/eo_UY
git checkout l10n_main locale/es_ES
git checkout l10n_main locale/eu_ES
git checkout l10n_main locale/fi_FI
git checkout l10n_main locale/fr_FR
git checkout l10n_main locale/gl_ES
git checkout l10n_main locale/it_IT
git checkout l10n_main locale/lt_LT
git checkout l10n_main locale/nl_NL
git checkout l10n_main locale/no_NO
git checkout l10n_main locale/pl_PL
git checkout l10n_main locale/pt_PT
git checkout l10n_main locale/pt_BR
git checkout l10n_main locale/ro_RO
git checkout l10n_main locale/sv_SE
git checkout l10n_main locale/zh_Hans
git checkout l10n_main locale/zh_Hant
runweb django-admin makemessages --no-wrap --ignore=venv -l en_US $@
runweb django-admin compilemessages --ignore venv
;;
build)
$DOCKER_COMPOSE build
;;
clean)
prod_error
clean
;;
black)
prod_error
$DOCKER_COMPOSE run --rm dev-tools black celerywyrm bookwyrm
;;
pylint)
prod_error
# pylint depends on having the app dependencies in place, so we run it in the web container
runweb pylint bookwyrm/
;;
prettier)
prod_error
$DOCKER_COMPOSE run --rm dev-tools prettier --write bookwyrm/static/js/*.js
;;
eslint)
prod_error
$DOCKER_COMPOSE run --rm dev-tools eslint bookwyrm/static --ext .js
;;
stylelint)
prod_error
$DOCKER_COMPOSE run --rm dev-tools stylelint --fix bookwyrm/static/css \
--config dev-tools/.stylelintrc.js --ignore-path dev-tools/.stylelintignore
;;
formatters)
prod_error
runweb pylint bookwyrm/
$DOCKER_COMPOSE run --rm dev-tools black celerywyrm bookwyrm
$DOCKER_COMPOSE run --rm dev-tools prettier --write bookwyrm/static/js/*.js
$DOCKER_COMPOSE run --rm dev-tools eslint bookwyrm/static --ext .js
$DOCKER_COMPOSE run --rm dev-tools stylelint --fix bookwyrm/static/css \
--config dev-tools/.stylelintrc.js --ignore-path dev-tools/.stylelintignore
;;
mypy)
prod_error
runweb mypy celerywyrm bookwyrm
;;
collectstatic_watch)
prod_error
npm run --prefix dev-tools watch:static
;;
update)
git pull
$DOCKER_COMPOSE build
# ./update.sh
runweb python manage.py migrate
runweb python manage.py compile_themes
runweb python manage.py collectstatic --no-input
$DOCKER_COMPOSE up -d
$DOCKER_COMPOSE restart web
$DOCKER_COMPOSE restart celery_worker
;;
populate_streams)
runweb python manage.py populate_streams "$@"
;;
populate_lists_streams)
runweb python manage.py populate_lists_streams $@
;;
populate_suggestions)
runweb python manage.py populate_suggestions
;;
generate_thumbnails)
runweb python manage.py generateimages
;;
generate_preview_images)
runweb python manage.py generate_preview_images "$@"
;;
remove_remote_user_preview_images)
runweb python manage.py remove_remote_user_preview_images
;;
copy_media_to_s3)
awscommand "bookwyrm_media_volume:/images"\
"s3 cp /images s3://${AWS_STORAGE_BUCKET_NAME}/images\
--endpoint-url ${AWS_S3_ENDPOINT_URL}\
--recursive --acl public-read" "$@"
;;
sync_media_to_s3)
awscommand "bookwyrm_media_volume:/images"\
"s3 sync /images s3://${AWS_STORAGE_BUCKET_NAME}/images\
--endpoint-url ${AWS_S3_ENDPOINT_URL}\
--acl public-read" "$@"
;;
set_cors_to_s3)
set +x
config_file=$1
if [ -z "$config_file" ]; then
echo "This command requires a JSON file containing a CORS configuration as an argument"
exit 1
fi
set -x
awscommand "$(pwd):/bw"\
"s3api put-bucket-cors\
--bucket ${AWS_STORAGE_BUCKET_NAME}\
--endpoint-url ${AWS_S3_ENDPOINT_URL}\
--cors-configuration file:///bw/$config_file" "$@"
;;
admin_code)
admin_code
;;
setup)
migrate
migrate django_celery_beat
initdb
runweb python manage.py compile_themes
runweb python manage.py collectstatic --no-input
admin_code
;;
runweb)
runweb "$@"
;;
remove_2fa)
runweb python manage.py remove_2fa "$@"
;;
confirm_email)
runweb python manage.py confirm_email "$@"
;;
*)
set +x # No need to echo echo
echo "Unrecognised command. Try:"
echo " setup"
echo " up [container]"
echo " down"
echo " service_ports_web"
echo " initdb"
echo " resetdb"
echo " makemigrations [migration]"
echo " migrate [migration]"
echo " bash"
echo " shell"
echo " dbshell"
echo " restart_celery"
echo " pytest [path]"
echo " compile_themes"
echo " collectstatic"
echo " makemessages"
echo " compilemessages [locale]"
echo " update_locales"
echo " build"
echo " clean"
echo " black"
echo " prettier"
echo " eslint"
echo " stylelint"
echo " formatters"
echo " mypy"
echo " collectstatic_watch"
echo " populate_streams [--stream=<stream name>]"
echo " populate_lists_streams"
echo " populate_suggestions"
echo " generate_thumbnails"
echo " generate_preview_images [--all]"
echo " remove_remote_user_preview_images"
echo " copy_media_to_s3"
echo " sync_media_to_s3"
echo " set_cors_to_s3 [cors file]"
echo " runweb [command]"
echo " remove_2fa"
echo " confirm_email"
;;
esac