From a7409489150a78140afa387b8916811877bb33bb Mon Sep 17 00:00:00 2001 From: Alexandre <44178713+alexbelgium@users.noreply.github.com> Date: Tue, 16 Jun 2026 21:32:10 +0200 Subject: [PATCH 1/6] Fix wger startup permissions and nginx init --- wger/rootfs/etc/cont-init.d/90-run.sh | 94 ++++++++++++++++++--------- 1 file changed, 63 insertions(+), 31 deletions(-) diff --git a/wger/rootfs/etc/cont-init.d/90-run.sh b/wger/rootfs/etc/cont-init.d/90-run.sh index 0e9f441465..8df6067557 100755 --- a/wger/rootfs/etc/cont-init.d/90-run.sh +++ b/wger/rootfs/etc/cont-init.d/90-run.sh @@ -1,5 +1,37 @@ #!/usr/bin/env bashio +set -u + +prepare_data_root() { + mkdir -p /data + chown wger /data || bashio::log.warning "Unable to set /data ownership to wger" + chmod a+rwx /data || bashio::log.warning "Unable to make /data writable" +} + +prepare_writable_dir() { + local path="$1" + + mkdir -p "$path" + chown -R wger "$path" || bashio::log.warning "Unable to set $path ownership to wger" + chmod -R a+rwX "$path" || bashio::log.warning "Unable to make $path writable" +} + +move_persistent_dir() { + local source="$1" + local target="$2" + + prepare_writable_dir "$target" + + if [ -d "$source" ] && [ ! -L "$source" ]; then + if [ -n "$(ls -A "$source" 2> /dev/null)" ]; then + cp -rnf "$source"/. "$target"/ + fi + rm -rf "$source" + fi + + ln -sfn "$target" "$source" +} + ############################ # Change database location # ############################ @@ -11,56 +43,56 @@ if [ "${#SETTINGS_FILES[@]}" -gt 0 ]; then sed -i "s|/home/wger/db/database.sqlite|/data/database.sqlite|g" "$settings_file" done else - bashio::log.warning "Unable to find settings.py containing database path under /home, skipping rewrite" + bashio::log.warning "Unable to find Python settings containing database path under /home, skipping rewrite" fi ##################### # Adapt directories # ##################### echo "... create directories" -mkdir -p /data/static -if [ -d /home/wger/static ] && [ ! -L /home/wger/static ]; then - if [ -n "$(ls -A /home/wger/static 2> /dev/null)" ]; then - cp -rnf /home/wger/static/* /data/static/ - fi - rm -r /home/wger/static -fi -ln -sf /data/static /home/wger - -mkdir -p /data/media -if [ -d /home/wger/media ] && [ ! -L /home/wger/media ]; then - if [ -n "$(ls -A /home/wger/media 2> /dev/null)" ]; then - cp -rnf /home/wger/media/* /data/media/ - fi - rm -r /home/wger/media -fi -ln -sf /data/media /home/wger +prepare_data_root +move_persistent_dir /home/wger/static /data/static +move_persistent_dir /home/wger/media /data/media ##################### # Align permissions # ##################### echo "... align permissions" -chown -R wger /data -chown -R wger /home/wger -chmod -R 777 /data +prepare_writable_dir /data/static +prepare_writable_dir /data/media +if [ -f /data/database.sqlite ]; then + chown wger /data/database.sqlite || bashio::log.warning "Unable to set /data/database.sqlite ownership to wger" + chmod a+rw /data/database.sqlite || bashio::log.warning "Unable to make /data/database.sqlite writable" +fi echo "... add env variables" ( set -o posix export -p ) > /data/env.sh -while IFS= read -r line; do - [[ -z "$line" || "$line" =~ ^[[:space:]]*# ]] && continue - var="${line%%=*}" - sed -i "/^export[[:space:]]\+$var=/d" /data/env.sh - echo "export $line" >> /data/env.sh -done < /.env -chown wger /data/env.sh -chmod +x /data/env.sh +if [ -f /.env ]; then + while IFS= read -r line; do + [[ -z "$line" || "$line" =~ ^[[:space:]]*# ]] && continue + var="${line%%=*}" + [[ "$var" =~ ^[A-Za-z_][A-Za-z0-9_]*$ ]] || continue + sed -i "/^export[[:space:]]\+$var=/d" /data/env.sh + echo "export $line" >> /data/env.sh + done < /.env +fi + +chown wger /data/env.sh || bashio::log.warning "Unable to set /data/env.sh ownership to wger" +chmod 0644 /data/env.sh || bashio::log.warning "Unable to make /data/env.sh readable" bashio::log.info "Starting nginx" -nginx || true & -true +mkdir -p /run/nginx /var/log/nginx +if [ -f /run/nginx.pid ] && kill -0 "$(cat /run/nginx.pid)" 2> /dev/null; then + bashio::log.info "nginx is already running" +elif nginx -t; then + nginx +else + bashio::log.error "nginx configuration test failed" + exit 1 +fi bashio::log.info "Starting entrypoint" From f5ff8c9fbe177e26204c2f586f8cae1b1913068a Mon Sep 17 00:00:00 2001 From: Alexandre <44178713+alexbelgium@users.noreply.github.com> Date: Tue, 16 Jun 2026 21:32:27 +0200 Subject: [PATCH 2/6] Bump wger add-on version --- wger/config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wger/config.yaml b/wger/config.yaml index 2fed4f7e74..4128ade28d 100644 --- a/wger/config.yaml +++ b/wger/config.yaml @@ -23,5 +23,5 @@ schema: slug: wger udev: true url: https://github.com/alexbelgium/hassio-addons -version: "2.6-dev-2" +version: "2.6-dev-3" webui: "[PROTO:ssl]://[HOST]:[PORT:80]" From 49fa5b2ab4a1452282b2cf101d861cbdc77fee07 Mon Sep 17 00:00:00 2001 From: Alexandre <44178713+alexbelgium@users.noreply.github.com> Date: Tue, 16 Jun 2026 21:32:57 +0200 Subject: [PATCH 3/6] Document wger startup fix --- wger/CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/wger/CHANGELOG.md b/wger/CHANGELOG.md index d2880c2f0b..95a74731e1 100644 --- a/wger/CHANGELOG.md +++ b/wger/CHANGELOG.md @@ -1,3 +1,6 @@ +## 2.6-dev-3 (2026-06-16) +- Fix fresh-install startup by making the persistent `/data/static` and `/data/media` directories writable by the `wger` user without recursively changing all of `/data`. +- Make nginx startup idempotent and fail loudly if its configuration is invalid, instead of masking nginx startup errors that can leave the add-on web port closed. ## 2.6-dev-2 (2026-06-16) - Fix startup script database path rewrite by switching the `settings.py` matching with `*.py` since last update moved the settings.py file into several files within the settings folder From 1f644e6870fde6a098913c923576bd8b9baf0431 Mon Sep 17 00:00:00 2001 From: Alexandre <44178713+alexbelgium@users.noreply.github.com> Date: Tue, 16 Jun 2026 21:36:42 +0200 Subject: [PATCH 4/6] Preserve existing wger database on startup --- wger/rootfs/etc/cont-init.d/90-run.sh | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/wger/rootfs/etc/cont-init.d/90-run.sh b/wger/rootfs/etc/cont-init.d/90-run.sh index 8df6067557..4f7d93eeb4 100755 --- a/wger/rootfs/etc/cont-init.d/90-run.sh +++ b/wger/rootfs/etc/cont-init.d/90-run.sh @@ -32,6 +32,25 @@ move_persistent_dir() { ln -sfn "$target" "$source" } +migrate_database() { + local legacy_db=/home/wger/db/database.sqlite + local persistent_db=/data/database.sqlite + + if [ -f "$persistent_db" ]; then + bashio::log.info "Using existing persistent database at $persistent_db" + elif [ -f "$legacy_db" ]; then + bashio::log.info "Migrating legacy database from $legacy_db to $persistent_db" + cp -n "$legacy_db" "$persistent_db" + else + bashio::log.info "No existing database found; wger will create a new persistent database at $persistent_db" + fi + + if [ -f "$persistent_db" ]; then + chown wger "$persistent_db" || bashio::log.warning "Unable to set $persistent_db ownership to wger" + chmod a+rw "$persistent_db" || bashio::log.warning "Unable to make $persistent_db writable" + fi +} + ############################ # Change database location # ############################ @@ -51,6 +70,7 @@ fi ##################### echo "... create directories" prepare_data_root +migrate_database move_persistent_dir /home/wger/static /data/static move_persistent_dir /home/wger/media /data/media @@ -60,10 +80,7 @@ move_persistent_dir /home/wger/media /data/media echo "... align permissions" prepare_writable_dir /data/static prepare_writable_dir /data/media -if [ -f /data/database.sqlite ]; then - chown wger /data/database.sqlite || bashio::log.warning "Unable to set /data/database.sqlite ownership to wger" - chmod a+rw /data/database.sqlite || bashio::log.warning "Unable to make /data/database.sqlite writable" -fi +migrate_database echo "... add env variables" ( From 4c023c6cb81cb5ce7d3d7f953011abd89c9e17a1 Mon Sep 17 00:00:00 2001 From: Alexandre <44178713+alexbelgium@users.noreply.github.com> Date: Tue, 16 Jun 2026 21:37:01 +0200 Subject: [PATCH 5/6] Document wger database preservation --- wger/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/wger/CHANGELOG.md b/wger/CHANGELOG.md index 95a74731e1..0702de9db0 100644 --- a/wger/CHANGELOG.md +++ b/wger/CHANGELOG.md @@ -1,5 +1,6 @@ ## 2.6-dev-3 (2026-06-16) - Fix fresh-install startup by making the persistent `/data/static` and `/data/media` directories writable by the `wger` user without recursively changing all of `/data`. +- Preserve existing persistent data by reusing `/data/database.sqlite` when present, and migrating a legacy `/home/wger/db/database.sqlite` only if no persistent database exists yet. - Make nginx startup idempotent and fail loudly if its configuration is invalid, instead of masking nginx startup errors that can leave the add-on web port closed. ## 2.6-dev-2 (2026-06-16) From 1a44fa92215d0c7f0d46127ae9ec97b4697b2503 Mon Sep 17 00:00:00 2001 From: Alexandre <44178713+alexbelgium@users.noreply.github.com> Date: Tue, 16 Jun 2026 21:51:52 +0200 Subject: [PATCH 6/6] Keep nginx backgrounded during wger startup --- wger/rootfs/etc/cont-init.d/90-run.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wger/rootfs/etc/cont-init.d/90-run.sh b/wger/rootfs/etc/cont-init.d/90-run.sh index 4f7d93eeb4..c4bef22433 100755 --- a/wger/rootfs/etc/cont-init.d/90-run.sh +++ b/wger/rootfs/etc/cont-init.d/90-run.sh @@ -106,7 +106,7 @@ mkdir -p /run/nginx /var/log/nginx if [ -f /run/nginx.pid ] && kill -0 "$(cat /run/nginx.pid)" 2> /dev/null; then bashio::log.info "nginx is already running" elif nginx -t; then - nginx + nginx & else bashio::log.error "nginx configuration test failed" exit 1