From 71263f1a8cd687cfad4b29f75f9f5d5aa03b62db Mon Sep 17 00:00:00 2001 From: Alexander Graf Date: Fri, 4 Nov 2022 23:21:11 +0100 Subject: [PATCH] Add more env variables and restyle code --- core/admin/run_dev.sh | 154 +++++++++++++++++++++++++++--------------- 1 file changed, 100 insertions(+), 54 deletions(-) diff --git a/core/admin/run_dev.sh b/core/admin/run_dev.sh index 03003348..a2e32912 100755 --- a/core/admin/run_dev.sh +++ b/core/admin/run_dev.sh @@ -1,93 +1,139 @@ #!/usr/bin/env bash -set -euxo pipefail +set -euo pipefail ### CONFIG DEV_NAME="${DEV_NAME:-mailu-dev}" +DEV_DB="${DEV_DB:-}" DEV_PROFILER="${DEV_PROFILER:-false}" DEV_LISTEN="${DEV_LISTEN:-127.0.0.1:8080}" [[ "${DEV_LISTEN}" == *:* ]] || DEV_LISTEN="127.0.0.1:${DEV_LISTEN}" +DEV_ADMIN="${DEV_ADMIN:-admin@example.com}" +DEV_PASSWORD="${DEV_PASSWORD:-admin}" ### MAIN -docker="$(command -v podman || command -v docker || echo false)" -[[ "${docker}" == "false" ]] && { - echo "Sorry, you'll need podman or docker to run this." - exit 1 +[[ -n "${DEV_DB}" ]] && { + [[ -f "${DEV_DB}" ]] || { + echo "Sorry, can't find DEV_DB: '${DEV_DB}'" + exit 1 + } + DEV_DB="$(realpath "${DEV_DB}")" } -here="$(realpath "$(pwd)/${0%/*}")" -cd "${here}" +docker="$(command -v podman || command -v docker || echo false)" +[[ "${docker}" == "false" ]] && { + echo "Sorry, you'll need podman or docker to run this." + exit 1 +} -# TODO: use /tmp/... folder -[[ -d dev ]] && rm -rf dev -mkdir -p dev/data || exit 1 +tmp="$(mktemp -d)" +[[ -n "${tmp}" && -d "${tmp}" ]] || { + echo "Sorry, can't create temporary folder." + exit 1 +} +trap "rm -rf '${tmp}'" INT TERM EXIT + +admin="$(realpath "$(pwd)/${0%/*}")" +base="${admin}/../base" +assets="${admin}/assets" + +cd "${tmp}" # base -cp ../base/requirements-* dev/ -cp -r ../base/libs dev/ -sed -E '/^#/d;s:^FROM system$:FROM system AS base:' ../base/Dockerfile > dev/Dockerfile +cp "${base}"/requirements-* . +cp -r "${base}"/libs . +sed -E '/^#/d;s:^FROM system$:FROM system AS base:' "${base}/Dockerfile" >Dockerfile # assets -cp -r assets/content dev/ -sed -E '/^#/d;s:^(FROM [^ ]+$):\1 AS assets:' assets/Dockerfile >> dev/Dockerfile +cp -r "${assets}/content" . +sed -E '/^#/d;s:^(FROM [^ ]+$):\1 AS assets:' "${assets}/Dockerfile" >>Dockerfile -cat >> dev/Dockerfile <>Dockerfile <> dev/Dockerfile +sed -E '/^#/d;/^(COPY|EXPOSE|HEALTHCHECK|VOLUME|CMD) /d; s:^(.* )[^ ]*pybabel[^\\]*(.*):\1true \2:' "${admin}/Dockerfile" >>Dockerfile -MSG="\\n======================================================================\\nUI can be found here: http://${DEV_LISTEN}/sso/login\\nLog in with: admin@example.com and password admin if this is a new DB.\\n======================================================================\\n" - -cat >> dev/Dockerfile <>Dockerfile </dev/null && flask mailu admin admin example.com admin --mode ifmissing >/dev/null && echo -e '${MSG}' 1>&2 && flask run --host=0.0.0.0 --port=8080"] +CMD ["/bin/bash", "-c", "flask db upgrade &>/dev/null && flask mailu admin '${DEV_ADMIN/@*}' '${DEV_ADMIN#*@}' '${DEV_PASSWORD}' --mode ifmissing >/dev/null && flask run --host=0.0.0.0 --port=8080"] EOF -# TODO: re-compile assets on change? -# TODO: re-run babel on change? - # build -chmod -R u+rwX,go+rX dev/ -"${docker}" build --tag "${DEV_NAME}:latest" dev/ +chmod -R u+rwX,go+rX . +cat Dockerfile +"${docker}" build --tag "${DEV_NAME}:latest" . + +# gather volumes to map into container +volumes=() + +if [[ -n "${DEV_DB}" ]]; then + volumes+=( --volume "${DEV_DB}:/data/main.db" ) +else + mkdir -p "data" + volumes=( --volume "${tmp}/data/:/data/" ) +fi + +for vol in audit.py start.py mailu/ migrations/; do + volumes+=( --volume "${admin}/${vol}:/app/${vol}" ) +done + +for file in "${assets}/content/assets"/*; do + [[ "${file}" == */vendor.js ]] && continue + volumes+=( --volume "${file}:/app/static/${file/*\//}" ) +done + +# show configuration +cat <