bw-dev 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. #!/usr/bin/env bash
  2. # exit on errors
  3. set -e
  4. # check if we're in DEBUG mode
  5. DEBUG=$(sed <.env -ne 's/^DEBUG=//p')
  6. # disallow certain commands when debug is false
  7. function prod_error {
  8. if [ "$DEBUG" != "true" ]; then
  9. echo "This command is not safe to run in production environments"
  10. exit 1
  11. fi
  12. }
  13. # import our ENV variables
  14. # catch exits and give a friendly error message
  15. function showerr {
  16. echo "Failed to load configuration! You may need to update your .env and quote values with special characters in them."
  17. }
  18. trap showerr EXIT
  19. source .env
  20. trap - EXIT
  21. if docker compose &> /dev/null ; then
  22. DOCKER_COMPOSE="docker compose"
  23. else
  24. DOCKER_COMPOSE="docker-compose"
  25. fi
  26. function clean {
  27. $DOCKER_COMPOSE stop
  28. $DOCKER_COMPOSE rm -f
  29. }
  30. function runweb {
  31. $DOCKER_COMPOSE run --rm web "$@"
  32. }
  33. function execdb {
  34. $DOCKER_COMPOSE exec db $@
  35. }
  36. function execweb {
  37. $DOCKER_COMPOSE exec web "$@"
  38. }
  39. function initdb {
  40. runweb python manage.py initdb "$@"
  41. }
  42. function migrate {
  43. runweb python manage.py migrate "$@"
  44. }
  45. function admin_code {
  46. runweb python manage.py admin_code
  47. }
  48. function awscommand {
  49. # expose env vars
  50. export AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID}
  51. export AWS_SECRET_ACCESS_KEY=${AWS_SECRET_ACCESS_KEY}
  52. export AWS_DEFAULT_REGION=${AWS_S3_REGION_NAME}
  53. # first arg is mountpoint, second is the whole aws command
  54. docker run --rm -it -v $1\
  55. -e AWS_ACCESS_KEY_ID -e AWS_SECRET_ACCESS_KEY -e AWS_DEFAULT_REGION\
  56. amazon/aws-cli $2
  57. }
  58. CMD=$1
  59. if [ -n "$CMD" ]; then
  60. shift
  61. fi
  62. # show commands as they're executed
  63. set -x
  64. case "$CMD" in
  65. up)
  66. $DOCKER_COMPOSE up --build "$@"
  67. ;;
  68. down)
  69. $DOCKER_COMPOSE down
  70. ;;
  71. service_ports_web)
  72. prod_error
  73. $DOCKER_COMPOSE run --rm --service-ports web
  74. ;;
  75. initdb)
  76. initdb "$@"
  77. ;;
  78. resetdb)
  79. prod_error
  80. $DOCKER_COMPOSE rm -svf
  81. docker volume rm -f bookwyrm_media_volume bookwyrm_pgdata bookwyrm_redis_activity_data bookwyrm_redis_broker_data bookwyrm_static_volume
  82. $DOCKER_COMPOSE build
  83. migrate
  84. migrate django_celery_beat
  85. initdb
  86. runweb python manage.py compile_themes
  87. runweb python manage.py collectstatic --no-input
  88. admin_code
  89. ;;
  90. makemigrations)
  91. prod_error
  92. runweb python manage.py makemigrations "$@"
  93. ;;
  94. migrate)
  95. migrate "$@"
  96. ;;
  97. bash)
  98. runweb bash
  99. ;;
  100. shell)
  101. runweb python manage.py shell
  102. ;;
  103. dbshell)
  104. execdb psql -U ${POSTGRES_USER} ${POSTGRES_DB}
  105. ;;
  106. restart_celery)
  107. $DOCKER_COMPOSE restart celery_worker
  108. ;;
  109. pytest)
  110. prod_error
  111. runweb pytest --no-cov-on-fail "$@"
  112. ;;
  113. pytest_coverage_report)
  114. prod_error
  115. runweb pytest -n 3 --cov-report term-missing "$@"
  116. ;;
  117. compile_themes)
  118. runweb python manage.py compile_themes
  119. ;;
  120. collectstatic)
  121. runweb python manage.py collectstatic --no-input
  122. ;;
  123. makemessages)
  124. prod_error
  125. runweb django-admin makemessages --no-wrap --ignore=venv -l en_US $@
  126. ;;
  127. compilemessages)
  128. runweb django-admin compilemessages --ignore venv $@
  129. ;;
  130. update_locales)
  131. prod_error
  132. git fetch origin l10n_main:l10n_main
  133. git checkout l10n_main locale/ca_ES
  134. git checkout l10n_main locale/de_DE
  135. git checkout l10n_main locale/eo_UY
  136. git checkout l10n_main locale/es_ES
  137. git checkout l10n_main locale/eu_ES
  138. git checkout l10n_main locale/fi_FI
  139. git checkout l10n_main locale/fr_FR
  140. git checkout l10n_main locale/gl_ES
  141. git checkout l10n_main locale/it_IT
  142. git checkout l10n_main locale/lt_LT
  143. git checkout l10n_main locale/nl_NL
  144. git checkout l10n_main locale/no_NO
  145. git checkout l10n_main locale/pl_PL
  146. git checkout l10n_main locale/pt_PT
  147. git checkout l10n_main locale/pt_BR
  148. git checkout l10n_main locale/ro_RO
  149. git checkout l10n_main locale/sv_SE
  150. git checkout l10n_main locale/uk_UA
  151. git checkout l10n_main locale/zh_Hans
  152. git checkout l10n_main locale/zh_Hant
  153. runweb django-admin makemessages --no-wrap --ignore=venv -l en_US $@
  154. runweb django-admin compilemessages --ignore venv
  155. ;;
  156. build)
  157. $DOCKER_COMPOSE build
  158. ;;
  159. clean)
  160. prod_error
  161. clean
  162. ;;
  163. black)
  164. prod_error
  165. $DOCKER_COMPOSE run --rm dev-tools black celerywyrm bookwyrm
  166. ;;
  167. pylint)
  168. prod_error
  169. # pylint depends on having the app dependencies in place, so we run it in the web container
  170. runweb pylint bookwyrm/
  171. ;;
  172. prettier)
  173. prod_error
  174. $DOCKER_COMPOSE run --rm dev-tools prettier --write bookwyrm/static/js/*.js
  175. ;;
  176. eslint)
  177. prod_error
  178. $DOCKER_COMPOSE run --rm dev-tools eslint bookwyrm/static --ext .js
  179. ;;
  180. stylelint)
  181. prod_error
  182. $DOCKER_COMPOSE run --rm dev-tools stylelint --fix bookwyrm/static/css \
  183. --config dev-tools/.stylelintrc.js --ignore-path dev-tools/.stylelintignore
  184. ;;
  185. formatters)
  186. prod_error
  187. runweb pylint bookwyrm/
  188. $DOCKER_COMPOSE run --rm dev-tools black celerywyrm bookwyrm
  189. $DOCKER_COMPOSE run --rm dev-tools prettier --write bookwyrm/static/js/*.js
  190. $DOCKER_COMPOSE run --rm dev-tools eslint bookwyrm/static --ext .js
  191. $DOCKER_COMPOSE run --rm dev-tools stylelint --fix bookwyrm/static/css \
  192. --config dev-tools/.stylelintrc.js --ignore-path dev-tools/.stylelintignore
  193. ;;
  194. mypy)
  195. prod_error
  196. runweb mypy celerywyrm bookwyrm
  197. ;;
  198. collectstatic_watch)
  199. prod_error
  200. npm run --prefix dev-tools watch:static
  201. ;;
  202. update)
  203. git pull
  204. $DOCKER_COMPOSE build
  205. # ./update.sh
  206. runweb python manage.py migrate
  207. runweb python manage.py compile_themes
  208. runweb python manage.py collectstatic --no-input
  209. $DOCKER_COMPOSE up -d
  210. $DOCKER_COMPOSE restart web
  211. $DOCKER_COMPOSE restart celery_worker
  212. ;;
  213. populate_streams)
  214. runweb python manage.py populate_streams "$@"
  215. ;;
  216. populate_lists_streams)
  217. runweb python manage.py populate_lists_streams $@
  218. ;;
  219. populate_suggestions)
  220. runweb python manage.py populate_suggestions
  221. ;;
  222. generate_thumbnails)
  223. runweb python manage.py generateimages
  224. ;;
  225. generate_preview_images)
  226. runweb python manage.py generate_preview_images "$@"
  227. ;;
  228. remove_remote_user_preview_images)
  229. runweb python manage.py remove_remote_user_preview_images
  230. ;;
  231. erase_deleted_user_data)
  232. runweb python manage.py erase_deleted_user_data "$@"
  233. ;;
  234. copy_media_to_s3)
  235. awscommand "bookwyrm_media_volume:/images"\
  236. "s3 cp /images s3://${AWS_STORAGE_BUCKET_NAME}/images\
  237. --endpoint-url ${AWS_S3_ENDPOINT_URL}\
  238. --recursive --acl public-read" "$@"
  239. ;;
  240. sync_media_to_s3)
  241. awscommand "bookwyrm_media_volume:/images"\
  242. "s3 sync /images s3://${AWS_STORAGE_BUCKET_NAME}/images\
  243. --endpoint-url ${AWS_S3_ENDPOINT_URL}\
  244. --acl public-read" "$@"
  245. ;;
  246. set_cors_to_s3)
  247. set +x
  248. config_file=$1
  249. if [ -z "$config_file" ]; then
  250. echo "This command requires a JSON file containing a CORS configuration as an argument"
  251. exit 1
  252. fi
  253. set -x
  254. awscommand "$(pwd):/bw"\
  255. "s3api put-bucket-cors\
  256. --bucket ${AWS_STORAGE_BUCKET_NAME}\
  257. --endpoint-url ${AWS_S3_ENDPOINT_URL}\
  258. --cors-configuration file:///bw/$config_file" "$@"
  259. ;;
  260. admin_code)
  261. admin_code
  262. ;;
  263. setup)
  264. migrate
  265. migrate django_celery_beat
  266. initdb
  267. runweb python manage.py compile_themes
  268. runweb python manage.py collectstatic --no-input
  269. admin_code
  270. ;;
  271. runweb)
  272. runweb "$@"
  273. ;;
  274. remove_2fa)
  275. runweb python manage.py remove_2fa "$@"
  276. ;;
  277. confirm_email)
  278. runweb python manage.py confirm_email "$@"
  279. ;;
  280. *)
  281. set +x # No need to echo echo
  282. echo "Unrecognised command. Try:"
  283. echo " setup"
  284. echo " up [container]"
  285. echo " down"
  286. echo " service_ports_web"
  287. echo " initdb"
  288. echo " resetdb"
  289. echo " makemigrations [migration]"
  290. echo " migrate [migration]"
  291. echo " bash"
  292. echo " shell"
  293. echo " dbshell"
  294. echo " restart_celery"
  295. echo " pytest [path]"
  296. echo " compile_themes"
  297. echo " collectstatic"
  298. echo " makemessages"
  299. echo " compilemessages [locale]"
  300. echo " update_locales"
  301. echo " build"
  302. echo " clean"
  303. echo " black"
  304. echo " prettier"
  305. echo " eslint"
  306. echo " stylelint"
  307. echo " formatters"
  308. echo " mypy"
  309. echo " collectstatic_watch"
  310. echo " populate_streams [--stream=<stream name>]"
  311. echo " populate_lists_streams"
  312. echo " populate_suggestions"
  313. echo " generate_thumbnails"
  314. echo " generate_preview_images [--all]"
  315. echo " remove_remote_user_preview_images"
  316. echo " copy_media_to_s3"
  317. echo " sync_media_to_s3"
  318. echo " set_cors_to_s3 [cors file]"
  319. echo " runweb [command]"
  320. echo " remove_2fa"
  321. echo " confirm_email"
  322. ;;
  323. esac