fix(shared-ols): watcher reloads for .htaccess in the real vhost docRoot

The watcher only matched */public_html/.htaccess, so sites with a
document-root folder (docRoot public_html/<sub>) never triggered a reload.
Match .htaccess directly inside any docRoot taken from the rendered
$SITES_ROOT/*/vhconf.conf (set refreshed only when httpd_config.conf changes);
legacy match kept as fallback when the set is empty. Deep plugin .htaccess
files still do not trigger. Adds a stubbed-lswsctrl test and CI wiring.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0185ukXi4YX5FxJGqhi6qab6
This commit is contained in:
shadowdaoandClaude Sonnet 5.5 committed 2026-11-23 06:13:06 -08:00
1 parent 9ce8b9a595
commit 427246b886
3 files changed
+145 -11

No files matched your search

+65 -11
View File
@@ -32,9 +32,64 @@ set -uo pipefail
WATCH_ROOT="${OLS_WATCH_ROOT:-/mnt/users}"
DEBOUNCE="${OLS_HTACCESS_DEBOUNCE:-15}" # coalesce window (s)
FLOOR="${OLS_HTACCESS_FLOOR:-60}" # min seconds between restarts
LSWSCTRL=/usr/local/lsws/bin/lswsctrl
LSWSCTRL="${LSWSCTRL:-/usr/local/lsws/bin/lswsctrl}"
## Rendered config + per-site files (defaults match entrypoint-shared-ols.sh /
## render-shared-ols-config.sh; overridable so tests can run without OLS).
LSWS_CONF="${OLS_LSWS_CONF:-/usr/local/lsws/conf}"
SITES_ROOT="${OLS_SITES_ROOT:-${SITES_ROOT:-$LSWS_CONF/shared-sites}}"
last_restart=0
## Set of vhost docRoot directories (absolute, no trailing slash), keyed by
## path. Rebuilt from $SITES_ROOT/*/vhconf.conf only when the rendered
## httpd_config.conf changes (the panel re-renders it atomically on every site
## change), and only when a candidate .htaccess event arrives.
declare -A DOCROOTS=()
docroots_sig="unset"
config_sig() {
stat -c '%i:%Y:%s' "$LSWS_CONF/httpd_config.conf" 2>/dev/null || echo missing
}
refresh_docroots() {
local sig; sig=$(config_sig)
[ "$sig" = "$docroots_sig" ] && return
docroots_sig="$sig"
DOCROOTS=()
local files=() d
shopt -s nullglob
files=("$SITES_ROOT"/*/vhconf.conf)
shopt -u nullglob
[ "${#files[@]}" -gt 0 ] || return 0
## Panel data is only ever READ as text (awk), never sourced. Only absolute
## docRoot values without variables/'..'/whitespace are accepted. Process
## substitution (not a pipeline) so an early reader exit cannot SIGPIPE.
while read -r d; do
d="${d%/}"
case "$d" in
/*/..|/*/../*|*'$'*|*' '*|"") continue ;;
/*) DOCROOTS["$d"]=1 ;;
esac
done < <(awk '$1=="docRoot" && NF==2 && substr($2,1,1)=="/" {print $2}' "${files[@]}" 2>/dev/null)
log "docroot set refreshed: ${#DOCROOTS[@]} vhost docroot(s)"
}
## 0 if $1 (full path of a changed file) is a .htaccess directly inside a vhost
## docRoot. If the docRoot set is empty/unreadable, fall back to the legacy
## */public_html/.htaccess match so a parse failure is never LESS sensitive.
is_docroot_htaccess() {
case "$1" in
*/.htaccess) ;;
*) return 1 ;;
esac
refresh_docroots
if [ "${#DOCROOTS[@]}" -gt 0 ]; then
[ -n "${DOCROOTS[${1%/.htaccess}]:-}" ]
else
case "$1" in */public_html/.htaccess) return 0 ;; esac
return 1
fi
}
log() { echo "ols-htaccess-watcher: $*" >&2; }
do_restart() {
@@ -56,16 +111,17 @@ if ! command -v inotifywait >/dev/null 2>&1; then
log "FATAL: inotifywait not installed (inotify-tools)"; exit 1
fi
mkdir -p "$WATCH_ROOT"
log "watching $WATCH_ROOT for docroot (public_html) .htaccess changes (debounce=${DEBOUNCE}s floor=${FLOOR}s)"
log "watching $WATCH_ROOT for vhost-docroot .htaccess changes (debounce=${DEBOUNCE}s floor=${FLOOR}s)"
## -m monitor, -r recursive. We filter in the read loop rather than --include
## so this works on older inotify-tools too. modify/create/delete/move all
## matter (delete of .htaccess also changes rewrite behavior).
##
## --format '%w%f' (full path), NOT '%f' (basename only). OLS reads .htaccess
## (RewriteFile) only from a vhost's DOCROOT — VHROOT, i.e.
## /mnt/users/<user>/<domain>/public_html (see render-shared-ols-config.sh /
## entrypoint-lsphp.sh) — never anything below it. A basename-only match fires
## (RewriteFile) only from a vhost's DOCROOT — /mnt/users/<user>/<domain>/
## public_html, or public_html/<sub> when the site has a document-root folder
## (taken from the rendered $SITES_ROOT/*/vhconf.conf docRoot lines) — never
## anything below it. A basename-only match fires
## for ANY .htaccess anywhere under a tenant, at any depth, and WordPress
## plugins write plenty of those that OLS never opens: measured on whp01 over
## 24h, this watcher fired 63 restarts, of which the docroot .htaccess actually
@@ -75,14 +131,12 @@ log "watching $WATCH_ROOT for docroot (public_html) .htaccess changes (debounce=
## WPForms/Gravity Forms/UpdraftPlus/Groundhogg/WP Staging upload guards — and
## most of those tenants are on the shared Apache tier (cac-fpm), not this OLS
## tier at all, so their cache churn was restarting the OLS serving 15 unrelated
## tenants for no reason. Matching the full path down to /public_html/.htaccess
## is what actually ties a change to something OLS will reread.
## tenants for no reason. Matching the full path against the set of real docRoot
## directories is what ties a change to something OLS will reread. (Legacy
## */public_html/.htaccess match is kept only when that set is empty.)
inotifywait -m -r -e modify,create,delete,move "$WATCH_ROOT" --format '%w%f' 2>/dev/null |
while read -r path; do
case "$path" in
*/public_html/.htaccess) ;;
*) continue ;;
esac
is_docroot_htaccess "$path" || continue
## A tenant DOCROOT .htaccess changed. Coalesce the save-burst, then restart ONCE.
##
## The coalesce is HARD-BOUNDED to DEBOUNCE seconds: a previous version blocked
+74
View File
@@ -0,0 +1,74 @@
#!/usr/bin/env bash
## ols-htaccess-watcher.test.sh — runs scripts/ols-htaccess-watcher.sh against a
## temp tree with a stub lswsctrl (no OLS needed). Checks that a restart fires
## for .htaccess directly inside any vhost docRoot (including a document-root
## folder docRoot like public_html/public), and NOT for deeper plugin-style
## .htaccess files or an unrelated dir. Skips if inotifywait is unavailable.
set -euo pipefail
HERE=$(cd "$(dirname "$0")/.." && pwd)
W="$HERE/ols-htaccess-watcher.sh"
if ! command -v inotifywait >/dev/null 2>&1; then
echo "SKIP - inotifywait (inotify-tools) not installed; watcher test not run"; exit 0
fi
T=$(mktemp -d)
WPID=""
cleanup() { [ -n "$WPID" ] && kill "$WPID" 2>/dev/null || true; pkill -P "${WPID:-0}" 2>/dev/null || true; rm -rf "$T"; }
trap cleanup EXIT
fail=0
ok() { echo "ok - $1"; }
bad() { echo "FAIL - $1"; fail=1; }
U="$T/users"; CONF="$T/conf"; SITES="$CONF/shared-sites"
mkdir -p "$U/u1/a.example/public_html/wp-content/cache/x" "$U/u2/b.example/public_html/public" \
"$U/u2/b.example/public_html/web/app" "$U/u3/c.example/public_html" "$SITES/a" "$SITES/b" "$SITES/c" "$SITES/bad"
printf 'docRoot %s\n' "$U/u1/a.example/public_html" >"$SITES/a/vhconf.conf"
printf 'docRoot %s\n' "$U/u2/b.example/public_html/public" >"$SITES/b/vhconf.conf"
printf 'docRoot %s\n' "$U/u2/b.example/public_html/web/app/" >"$SITES/c/vhconf.conf"
printf 'docRoot $VH_ROOT/html\ndocRoot relative/path\ndocRoot /x/../etc\n' >"$SITES/bad/vhconf.conf"
echo '# cfg' >"$CONF/httpd_config.conf"
cat >"$T/lswsctrl" <<EOS
#!/bin/sh
echo x >>"$T/restarts"
EOS
chmod +x "$T/lswsctrl"
: >"$T/restarts"; : >"$T/log"
start() {
env OLS_WATCH_ROOT="$U" OLS_LSWS_CONF="$CONF" OLS_SITES_ROOT="$SITES" LSWSCTRL="$T/lswsctrl" \
OLS_HTACCESS_DEBOUNCE=1 OLS_HTACCESS_FLOOR=0 "$W" 2>"$T/log" &
WPID=$!
sleep 1.5
}
count() { wc -l <"$T/restarts" | tr -d ' '; }
## touch file, wait, expect restart count delta (want 1 or 0)
check() { # name want path
local before after; before=$(count)
echo "# $(date +%s%N)" >>"$3"
sleep 5
after=$(count)
if [ $((after - before)) -eq "$2" ]; then ok "$1"; else bad "$1 (restarts +$((after - before)), want +$2)"; fi
}
start
check "plain site docRoot .htaccess triggers" 1 "$U/u1/a.example/public_html/.htaccess"
check "folder docRoot (public_html/public) triggers" 1 "$U/u2/b.example/public_html/public/.htaccess"
check "trailing-slash docRoot (public_html/web/app) triggers" 1 "$U/u2/b.example/public_html/web/app/.htaccess"
check "plugin deep file (wp-content/cache) does NOT" 0 "$U/u1/a.example/public_html/wp-content/cache/x/.htaccess"
check "public_html/.htaccess of a site with folder docRoot does NOT (OLS ignores it)" 0 "$U/u2/b.example/public_html/.htaccess"
check "unregistered tenant public_html/.htaccess does NOT while set is non-empty" 0 "$U/u3/c.example/public_html/.htaccess"
## New site provisioned live + atomic re-render -> set refreshed
mkdir -p "$U/u4/d.example/public_html/pub"
mkdir -p "$SITES/d"; printf 'docRoot %s\n' "$U/u4/d.example/public_html/pub" >"$SITES/d/vhconf.conf"
echo '# cfg2' >"$CONF/.tmp" && mv "$CONF/.tmp" "$CONF/httpd_config.conf"
check "site added after start (re-render) triggers" 1 "$U/u4/d.example/public_html/pub/.htaccess"
kill "$WPID" 2>/dev/null || true; pkill -P "$WPID" 2>/dev/null || true; WPID=""
## Fallback: no vhconf files -> legacy */public_html/.htaccess match
rm -rf "$SITES"; mkdir -p "$SITES"
start
check "fallback (empty docRoot set): public_html/.htaccess triggers" 1 "$U/u3/c.example/public_html/.htaccess"
check "fallback: deep plugin file still does NOT" 0 "$U/u1/a.example/public_html/wp-content/cache/x/.htaccess"
[ "$fail" -eq 0 ] && echo "ALL PASS" || { echo "FAILURES"; cat "$T/log"; exit 1; }