Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(scripts): use the data-dir media folder for a host-run leftover ./uploads (#1825)
Yudhi Armyndharisc84377b85c61
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 45322077d..d9fd98337 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -29,6 +29,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0### Fixed- Drop whatsapp-web.js inbound media over `MEDIA_DOWNLOAD_MAX_BYTES` in the page when the sender understates its size.+- Use the `OPENWA_DATA_DIR` volume's `media/`, even before it exists, for a leftover `STORAGE_LOCAL_PATH=./uploads` in host-side `backup.sh` and `restore.sh` runs, instead of the working directory's `./uploads`.- Read the `./data` database defaults and `./data/...` paths in `./.env` under `OPENWA_DATA_DIR` in host-side `backup.sh` and `restore.sh` runs, instead of the working directory's `./data`.- Probe the configured `PORT` in the Docker image healthcheck instead of a fixed 2785.- Cap the total bytes one storage import writes at `STORAGE_IMPORT_MAX_TOTAL_BYTES`.diff --git a/docs/11-operational-runbooks.md b/docs/11-operational-runbooks.mdindex 59872302d..d9d8cfa80 100644--- a/docs/11-operational-runbooks.md+++ b/docs/11-operational-runbooks.md@@ -783,6 +783,22 @@ OPENWA_DATA_DIR=/srv/openwa/data \> host mount, a path recorded inside the container (`/app/data/...`) is not host-visible, so override> it in the environment.>+> A leftover `STORAGE_LOCAL_PATH=./uploads` (which v0.2.0 to v0.7.3 wrote to `.env.generated`), in+> `./.env` or `.env.generated`, is read as `<OPENWA_DATA_DIR>/media` whenever `OPENWA_DATA_DIR` is+> not the current directory's own `./data`, since the current directory's `./uploads` is then not+> the app's. A `./data` symlink counts as that directory even before its target exists. On bare metal+> with `./data` linking to a data directory kept elsewhere, create the link before restoring, or pass+> an absolute `STORAGE_LOCAL_PATH="$PWD/uploads"` in the environment; otherwise the media goes to the+> data directory's `media/` while the app keeps serving `./uploads`. A container that mounts a host+> `./uploads` at `/app/uploads` as its media directory, which no shipped compose file or chart does,+> needs the same absolute `STORAGE_LOCAL_PATH` in the environment; the scripts warn when they skip a+> non-empty `./uploads` while the data directory's `media/` is missing or empty, as it is in a volume+> such a container has booted on. The scripts cannot tell bare metal from a checkout whose `./data` is+> bind-mounted into the container, as in `docker-compose.dev.yml`, or from a root run inside a+> container with a writable root filesystem, and keep `./uploads` there; pass+> `STORAGE_LOCAL_PATH=./data/media` in the environment when restoring into an empty data directory in+> those layouts.+>> On every install, inside a container or not, the scripts stop with an error naming the key, before> anything is archived or restored, when the last line setting it in `./.env` or `.env.generated`,> which is the one the app keeps, has one of these forms:diff --git a/scripts/lib-env.sh b/scripts/lib-env.shindex ca76ca197..672910bee 100644--- a/scripts/lib-env.sh+++ b/scripts/lib-env.sh@@ -372,20 +372,72 @@ openwa_writable() {[ -d "$p" ] && [ -w "$p" ]}+# openwa_physical <path> - <path> without trailing / or /., following a final symlink even when its+# target is missing, and with its longest existing leading directory resolved by pwd -P.+openwa_physical() {+ local p="$1" t parent n=0+ while :; do+ while case "$p" in ?*/ | ?*/.) true ;; *) false ;; esac; do+ p="${p%/}"+ p="${p%/.}"+ done+ if [ "$n" -ge 40 ] || [ ! -L "$p" ]; then break; fi+ t="$(readlink -- "$p")"+ case "$t" in+ /*) p="$t" ;;+ *) p="$(dirname -- "$p")/$t" ;;+ esac+ n=$((n + 1))+ done+ if [ -d "$p" ] && parent="$(CDPATH='' cd -P -- "$p" 2>/dev/null && pwd -P)"; then+ printf '%s' "$parent"+ return+ fi+ parent="$(openwa_physical "$(dirname -- "$p")")"+ printf '%s/%s' "${parent%/}" "$(basename -- "$p")"+}++# openwa_is_cwd_data_dir - whether DATA_DIR is the working directory's ./data, however it is spelled:+# the same directory when either exists (by device and inode, so through a symlink or a bind mount),+# else the same physical path, so a ./data link whose target is not created yet counts when DATA_DIR+# names that target.+openwa_is_cwd_data_dir() {+ if [ -e "$DATA_DIR" ] || [ -e ./data ]; then+ [ "$DATA_DIR" -ef ./data ]+ return+ fi+ [ "$(openwa_physical "$DATA_DIR")" = "$(openwa_physical ./data)" ]+}+# openwa_media_dir - STORAGE_LOCAL_PATH as the app settles it (src/config/storage-root.ts). v0.2.0 to# v0.7.3 persisted ./uploads into .env.generated; where that cannot be created, as under the image's# root-owned /app, the app keeps media in ./data/media (<data dir>/media) instead, so the scripts have# to look there too. Writability alone cannot tell: `docker exec` runs these as root, which can create# /app/uploads while the app's own user cannot. The app creates a ./uploads it uses at boot, so a-# missing one beside an existing <data dir>/media means that one is in use.+# missing one beside an existing <data dir>/media means that one is in use. With OPENWA_DATA_DIR naming+# another directory than the working directory's ./data, such as a volume's mountpoint on the host, the+# working directory is not the app's: its ./uploads, present or not, is not the one the app would use,+# which lies outside the volume, so <data dir>/media is the target even before it exists, as when+# restoring into an empty volume. A value from the environment is the caller's own and skips that rule.openwa_media_dir() {local dirdir="$(openwa_resolve STORAGE_LOCAL_PATH "$DATA_DIR/media" path)" || returncase "$dir" in./uploads | uploads)- if ! openwa_writable "$dir" || { [ ! -d "$dir" ] && [ -d "$DATA_DIR/media" ]; }; then+ if ! openwa_writable "$dir" || { [ ! -d "$dir" ] && [ -d "$DATA_DIR/media" ]; } ||+ { [ -z "$(printenv STORAGE_LOCAL_PATH)" ] && ! openwa_is_cwd_data_dir; }; then+ local fix="Remove the line from ./.env or .env.generated."+ [ -z "$(printenv STORAGE_LOCAL_PATH)" ] || fix="Unset it in the environment."echo "[config] WARN: STORAGE_LOCAL_PATH=$dir is a leftover the app does not use here, so it keeps" >&2- echo "[config] media in ./data/media; using $DATA_DIR/media. Remove the line from .env.generated." >&2+ echo "[config] media in ./data/media; using $DATA_DIR/media. $fix" >&2+ # A non-empty ./uploads beside a missing or empty <data dir>/media (the entrypoint creates an+ # empty one at boot) may be a host folder a container mounts at /app/uploads; that layout cannot+ # be told apart from here, so name the override for it.+ if [ -z "$(find -H "$DATA_DIR/media" -mindepth 1 -maxdepth 1 -print -quit 2>/dev/null)" ] &&+ [ -n "$(find -H "$dir" -mindepth 1 -maxdepth 1 -print -quit 2>/dev/null)" ]; then+ echo "[config] WARN: $dir is not empty but is skipped. If it is mounted at /app/uploads as the app's media" >&2+ echo "[config] dir, pass STORAGE_LOCAL_PATH=\"$PWD/uploads\" in the environment instead." >&2+ fidir="$DATA_DIR/media"fi;;diff --git a/scripts/smoke-test-backup-restore.sh b/scripts/smoke-test-backup-restore.shindex 2b9bb05c0..e0c330f8d 100755--- a/scripts/smoke-test-backup-restore.sh+++ b/scripts/smoke-test-backup-restore.sh@@ -55,8 +55,11 @@# without node), connects as before when it is not, and never starts past a DATABASE_SSL or# DATABASE_SSL_REJECT_UNAUTHORIZED line the scripts cannot parse# (ai) the ./data defaults and ./data paths in ./.env and .env.generated follow OPENWA_DATA_DIR in a-# run on the host, in both scripts, while non-path settings and a ./data path from the environment-# are read as written+# run on the host, in both scripts, as does a leftover ./uploads, also into an empty volume, while a+# bare-metal ./data named by another spelling or a symlink keeps ./uploads, non-path settings and+# a ./data path or absolute media path from the environment are read as written, an+# environment ./uploads is exempt from the empty-volume rule, and a skipped non-empty ./uploads+# is named in a warning## Usage: ./scripts/smoke-test-backup-restore.sh# Requires: bash, tar, node (restore.sh path resolution). sqlite3 is optional (see (c) and (k)).@@ -1999,6 +2002,103 @@ printf 'STORAGE_LOCAL_PATH=./uploads\n' >"$AI/vol/.env.generated"if ! tar -tzf "$(ls "$AI"/out-uploads/openwa-backup-*.tar.gz)" | grep -qx './media/a.jpg'; thenfail "(ai) a leftover ./uploads did not fall back to the volume's media"fi+# Restored into an empty volume, whose media dir does not exist yet, the media lands there too rather+# than in the host's ./uploads, which the app in the container never reads, whether or not one exists.+ARCHIVE_AI_UP="$(ls "$AI"/out-uploads/openwa-backup-*.tar.gz)"+for AI_VOL in vol3 vol3b; do+ [ "$AI_VOL" = vol3b ] && mkdir -p "$AI/host/uploads"+ mkdir -p "$AI/$AI_VOL"+ (cd "$AI/host" && OPENWA_DATA_DIR="$AI/$AI_VOL" "$RESTORE" "$ARCHIVE_AI_UP" >/dev/null 2>"$AI/$AI_VOL.err") ||+ fail "(ai) restore into $AI_VOL with a leftover ./uploads failed"+ if [ "$(cat "$AI/$AI_VOL/media/a.jpg" 2>/dev/null || true)" != "ai-media" ] ||+ [ -n "$(ls -A "$AI/host/uploads" 2>/dev/null)" ]; then+ fail "(ai) a leftover ./uploads restored the media outside the empty volume $AI_VOL"+ fi+ if grep -q 'is not empty but is skipped' "$AI/$AI_VOL.err"; then+ fail "(ai) restore into $AI_VOL warned about an empty or missing ./uploads"+ fi+done+# A non-empty host ./uploads skipped that way, as one a container mounts at /app/uploads would be, is+# named in a warning with the environment override that keeps it.+mkdir -p "$AI/hostup/uploads" "$AI/vol3c"+printf 'in-use\n' >"$AI/hostup/uploads/old.jpg"+(cd "$AI/hostup" && OPENWA_DATA_DIR="$AI/vol3c" "$RESTORE" "$ARCHIVE_AI_UP" >/dev/null 2>"$AI/vol3c.err") ||+ fail "(ai) restore into vol3c beside a non-empty ./uploads failed"+if ! grep -qF "STORAGE_LOCAL_PATH=\"$AI/hostup/uploads\" in the environment" "$AI/vol3c.err"; then+ fail "(ai) a skipped non-empty ./uploads was not named in a warning: $(cat "$AI/vol3c.err")"+fi+# So is one beside the empty media/ the entrypoint creates in every volume it boots on, as when backing+# up a live install from the host.+cp -R "$AI/vol3c" "$AI/vol3d"+rm -rf "$AI/vol3d/media" && mkdir "$AI/vol3d/media"+(cd "$AI/hostup" && OPENWA_DATA_DIR="$AI/vol3d" BACKUP_DIR="$AI/out-vol3d" "$BACKUP" >/dev/null 2>"$AI/vol3d.err") ||+ fail "(ai) backup from vol3d beside a non-empty ./uploads failed"+if ! grep -qF "STORAGE_LOCAL_PATH=\"$AI/hostup/uploads\" in the environment" "$AI/vol3d.err"; then+ fail "(ai) a non-empty ./uploads skipped beside an empty media dir was not named in a warning: $(cat "$AI/vol3d.err")"+fi+# A fresh bare-metal restore naming its own ./data by absolute path, or with a trailing /./ or /.,+# keeps the ./uploads the restored app creates and uses at boot, as with the default ./data.+for AI_SPELL in "$AI/bare/data" "$AI/bare-slash/data/./" ./data/.; do+ AI_BARE="$AI/bare"+ case "$AI_SPELL" in */data/./) AI_BARE="$AI/bare-slash" ;; ./*) AI_BARE="$AI/bare-dot" ;; esac+ mkdir -p "$AI_BARE"+ (cd "$AI_BARE" && OPENWA_DATA_DIR="$AI_SPELL" "$RESTORE" "$ARCHIVE_AI_UP" >/dev/null 2>&1) ||+ fail "(ai) bare-metal restore into $AI_SPELL with a leftover ./uploads failed"+ if [ "$(cat "$AI_BARE/uploads/a.jpg" 2>/dev/null || true)" != "ai-media" ] || [ -e "$AI_BARE/data/media" ]; then+ fail "(ai) a bare-metal restore into $AI_SPELL did not restore the media into ./uploads"+ fi+done+# A backup of that install, its ./data named by absolute or symlinked path, archives the ./uploads in use,+# as does one whose ./data is itself a symlink to a data dir kept elsewhere, named by its target.+ln -s "$AI/bare" "$AI/bare-link"+mkdir -p "$AI/linked"+cp -R "$AI/bare/data" "$AI/linked-target"+cp -R "$AI/bare/uploads" "$AI/linked/uploads"+ln -s "$AI/linked-target" "$AI/linked/data"+for AI_SPELL in "$AI/bare:$AI/bare/data" "$AI/bare:$AI/bare-link/data" "$AI/linked:$AI/linked-target"; do+ rm -rf "$AI/out-bare"+ (cd "${AI_SPELL%%:*}" && OPENWA_DATA_DIR="${AI_SPELL#*:}" BACKUP_DIR="$AI/out-bare" "$BACKUP" >/dev/null 2>&1) ||+ fail "(ai) bare-metal backup from ${AI_SPELL#*:} with a leftover ./uploads failed"+ if [ "$(tar -xzOf "$(ls "$AI"/out-bare/openwa-backup-*.tar.gz)" ./media/a.jpg 2>/dev/null)" != "ai-media" ]; then+ fail "(ai) a bare-metal backup from ${AI_SPELL#*:} did not archive the media in ./uploads"+ fi+done+# STORAGE_LOCAL_PATH=./uploads passed in the environment is the caller's own and skips the empty-volume rule.+mkdir -p "$AI/envup" "$AI/vol4"+(cd "$AI/envup" && OPENWA_DATA_DIR="$AI/vol4" STORAGE_LOCAL_PATH=./uploads "$RESTORE" "$ARCHIVE_AI_UP" \+ >/dev/null 2>&1) || fail "(ai) restore with STORAGE_LOCAL_PATH=./uploads in the environment failed"+if [ "$(cat "$AI/envup/uploads/a.jpg" 2>/dev/null || true)" != "ai-media" ] || [ -e "$AI/vol4/media" ]; then+ fail "(ai) STORAGE_LOCAL_PATH=./uploads from the environment was not read as written"+fi+# The runbook's ways to keep ./uploads for a bare-metal restore with OPENWA_DATA_DIR at the data dir+# ./data links to: create that dir and the link first, or pass an absolute STORAGE_LOCAL_PATH, which the+# fallback never touches, even beside an existing <data dir>/media with no ./uploads.+mkdir -p "$AI/prelink/inst" "$AI/prelink/disk/data" "$AI/envabs" "$AI/vol5/media"+ln -s "$AI/prelink/disk/data" "$AI/prelink/inst/data"+(cd "$AI/prelink/inst" && OPENWA_DATA_DIR="$AI/prelink/disk/data" "$RESTORE" "$ARCHIVE_AI_UP" >/dev/null 2>&1) ||+ fail "(ai) bare-metal restore through a pre-created ./data link failed"+if [ "$(cat "$AI/prelink/inst/uploads/a.jpg" 2>/dev/null || true)" != "ai-media" ] ||+ [ -e "$AI/prelink/disk/data/media" ]; then+ fail "(ai) a bare-metal restore through a pre-created ./data link did not restore the media into ./uploads"+fi+# A ./data link created before its target, absolute or relative, is followed too when OPENWA_DATA_DIR+# names that target.+for AI_LINK in "$AI/danglink-abs/disk/data" ../disk/data; do+ AI_BARE="$AI/danglink-abs"+ [ "$AI_LINK" = ../disk/data ] && AI_BARE="$AI/danglink-rel"+ mkdir -p "$AI_BARE/inst"+ ln -s "$AI_LINK" "$AI_BARE/inst/data"+ (cd "$AI_BARE/inst" && OPENWA_DATA_DIR="$AI_BARE/disk/data" "$RESTORE" "$ARCHIVE_AI_UP" >/dev/null 2>&1) ||+ fail "(ai) bare-metal restore through a ./data link to $AI_LINK, not yet created, failed"+ if [ "$(cat "$AI_BARE/inst/uploads/a.jpg" 2>/dev/null || true)" != "ai-media" ] || [ -e "$AI_BARE/disk/data/media" ]; then+ fail "(ai) a restore through a ./data link to $AI_LINK, not yet created, did not restore the media into ./uploads"+ fi+done+(cd "$AI/envabs" && OPENWA_DATA_DIR="$AI/vol5" STORAGE_LOCAL_PATH="$AI/envabs/uploads" "$RESTORE" \+ "$ARCHIVE_AI_UP" >/dev/null 2>&1) || fail "(ai) restore with an absolute STORAGE_LOCAL_PATH failed"+if [ "$(cat "$AI/envabs/uploads/a.jpg" 2>/dev/null || true)" != "ai-media" ] || [ -e "$AI/vol5/media/a.jpg" ]; then+ fail "(ai) an absolute STORAGE_LOCAL_PATH from the environment was not read as written"+fi# Only path settings are mapped: a dashboard-provisioned Postgres named, owned or reached as "data", or# a password under "data/", reaches pg_dump as written, with the default data dir too.mkdir -p "$AI/pg/data" "$AI/pg/bin"