From e8f788d2cbaf55dd95607e3470d38ea52c02db01 Mon Sep 17 00:00:00 2001 From: Kevin Veen-Birkenbach Date: Fri, 20 Nov 2026 14:48:16 +0000 Subject: [PATCH] fix(web-app-joomla): finish the stock installer's cleanup on an NFS webroot MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Docker Hub re-pushed joomla:6.2.0-php8.3-apache on 2026-11-19 with the docker-joomla entrypoint of official-images 82dbb58597. It skips setup for 'apache2 -v', so the wrapper's leader no longer installed anything. When the installer fails it keeps .joomla-initializing for good. On the swarm's NFS webroot the installer cannot delete installation/ ('[ERROR] Unable to delete installation folder!'), and every replica then exits with 'Initialization did not complete (exit 1); server startup is blocked.' docker-entrypoint-joomla.sh now runs setup through the stock entrypoint with 'apache2 -l'. One leader runs it under a mkdir lock with a heartbeat owner file, and a lock whose heartbeat is older than JOOMLA_INIT_LOCK_STALE_SECONDS (180 s) is taken over atomically. When setup stops only at the delete message, the leader finishes what Joomla's CleanupModel does: it deletes installation/ and joomla.xml and renames robots.txt.dist. It records the pending cleanup in .infinito-init.cleanup, so a failed rm is retried on the next start. A site left stuck by the old wrapper fails with the exact manual step. test_docker_entrypoint_joomla.py has 14 cases. It passes on the fix, fails 12 of 14 on the old wrapper, and fails on a mutant without the grep guard. Two independent skeptic rounds: the first refuted the earlier version, the second found no hole. Draft: no run against the real stock entrypoint on a real NFS volume yet; the fork CI swarm run proves it. 🎯 Fix: 85 % 💥 Break: 6 % 💸 Tokens: – ⏱️ Velocity: – 🛡️ Security: ~100 % 🏆 Score: 80 🚦 Gate: ⛔ fix 85 % < 90, break 6 % > 5 Created-with: https://s.veen.world/skills Co-Authored-By: Claude Opus 5.5 --- roles/web-app-joomla/files/Dockerfile | 1 + .../files/shell/docker-entrypoint-joomla.sh | 150 +++++++++-- roles/web-app-joomla/templates/env.j2 | 2 + roles/web-app-joomla/vars/main.yml | 1 + .../roles/web-app-joomla/files/__init__.py | 0 .../web-app-joomla/files/shell/__init__.py | 0 .../shell/test_docker_entrypoint_joomla.py | 255 ++++++++++++++++++ 7 files changed, 384 insertions(+), 25 deletions(-) create mode 100644 tests/unit/python/roles/web-app-joomla/files/__init__.py create mode 100644 tests/unit/python/roles/web-app-joomla/files/shell/__init__.py create mode 100644 tests/unit/python/roles/web-app-joomla/files/shell/test_docker_entrypoint_joomla.py diff --git a/roles/web-app-joomla/files/Dockerfile b/roles/web-app-joomla/files/Dockerfile index 73e682fc6b..fb0c128619 100644 --- a/roles/web-app-joomla/files/Dockerfile +++ b/roles/web-app-joomla/files/Dockerfile @@ -16,5 +16,6 @@ COPY ${JOOMLA_ENTRYPOINT_SCRIPT_HOST_REL} ${JOOMLA_ENTRYPOINT_SCRIPT_DOCKER} RUN chmod +x "${JOOMLA_ENTRYPOINT_SCRIPT_DOCKER}" \ && ln -sf "${JOOMLA_ENTRYPOINT_SCRIPT_DOCKER}" /usr/local/bin/entrypoint-joomla +ENV JOOMLA_STOCK_ENTRYPOINT=/entrypoint.sh ENTRYPOINT ["/usr/local/bin/entrypoint-joomla"] CMD ["apache2-foreground"] diff --git a/roles/web-app-joomla/files/shell/docker-entrypoint-joomla.sh b/roles/web-app-joomla/files/shell/docker-entrypoint-joomla.sh index 49a9d03bbb..4f7c27ff07 100644 --- a/roles/web-app-joomla/files/shell/docker-entrypoint-joomla.sh +++ b/roles/web-app-joomla/files/shell/docker-entrypoint-joomla.sh @@ -1,30 +1,130 @@ #!/bin/bash -# Serialize the stock image's first-boot copy+auto-install across replicas that -# share the /var/www/html NFS volume (mkdir is atomic on NFS: one leader runs it, -# the rest block on the ready marker). The setup only runs when $1 is apache2*/ -# php-fpm, so the leader must drive it with `apache2 -v` (returns after setup) — -# a plain `true` skips the whole setup block and copies nothing. - -target="/var/www/html" -lock="${target}/.infinito-init.lock" +webroot="${JOOMLA_WEBROOT:?JOOMLA_WEBROOT must be set}" +stock="${JOOMLA_STOCK_ENTRYPOINT:?JOOMLA_STOCK_ENTRYPOINT must be set}" ready="${JOOMLA_INIT_READY_FILE:?JOOMLA_INIT_READY_FILE must be set}" +stale_after="${JOOMLA_INIT_LOCK_STALE_SECONDS:?JOOMLA_INIT_LOCK_STALE_SECONDS must be set}" +lock="${webroot}/.infinito-init.lock" +owner="${lock}/owner" +marker="${webroot}/.joomla-initializing" +pending="${webroot}/.infinito-init.cleanup" +config="${webroot}/configuration.php" +token="${HOSTNAME}.$$.$(date +%s%N)" +beat=$((stale_after / 4 + 1)) +heartbeat_pid="" -if [ ! -e "${ready}" ] && [ ! -e "${target}/configuration.php" ]; then - if mkdir "${lock}" 2>/dev/null; then - /entrypoint.sh apache2 -v || true # nocheck: shell-or-true -- grandfathered: worked in practice; TODO: sharpen to catch only the exact tolerated error - if [ -e "${target}/configuration.php" ]; then - chown -R www-data:www-data \ - "${target}/administrator/cache" "${target}/administrator/logs" \ - "${target}/cache" "${target}/tmp" 2>/dev/null || true # nocheck: shell-or-true -- grandfathered: worked in practice; TODO: sharpen to catch only the exact tolerated error - touch "${ready}" - fi - else - tries=0 - while [ ! -e "${ready}" ] && [ ! -e "${target}/configuration.php" ] && [ "${tries}" -lt 300 ]; do - sleep 2 - tries=$((tries + 1)) - done +trap 'exit 143' TERM +trap 'exit 130' INT + +log_error() { + printf '[ERROR] %s\n' "$1" >&2 +} + +initialized() { + [ -e "${ready}" ] || { [ -s "${config}" ] && [ ! -e "${marker}" ]; } +} + +stale() { + local stamp + stamp="$(stat -c %Y "$1/owner" 2>/dev/null)" || + stamp="$(stat -c %Y "$1" 2>/dev/null)" || + return 1 + [ $(($(date +%s) - stamp)) -gt "${stale_after}" ] +} + +take_over_stale() { + local moved="${lock}.stale.${token}" + stale "${lock}" || return 1 + mv -T "${lock}" "${moved}" 2>/dev/null || return 1 + if stale "${moved}"; then + rm -rf "${moved}" + return 0 fi -fi + mv -T "${moved}" "${lock}" + return 1 +} -exec /entrypoint.sh "$@" +heartbeat() { + while sleep "${beat}"; do + touch "${owner}" + done +} + +release() { + if [ -n "${heartbeat_pid}" ]; then + kill "${heartbeat_pid}" 2>/dev/null + fi + heartbeat_pid="" + if [ "$(cat "${owner}" 2>/dev/null)" = "${token}" ]; then + rm -rf "${lock}" + fi +} + +run_setup() { + local log + log="$(mktemp)" + # Exception: since docker-joomla 327d05a9f1 the stock entrypoint skips its + # setup for -v/-V/-h/-t/-T; `apache2 -l` still runs it and then returns. + { "${stock}" apache2 -l 2>&1 | tee "${log}"; } & + wait "$!" + # Exception: Joomla's installer deletes installation/ while it still holds + # installation/joomla.php open; NFS keeps that file as a .nfs silly rename, + # so a complete install reports failure and the stock entrypoint keeps its + # marker. The pending note lets any later start finish that cleanup. + if [ -s "${config}" ] && [ -d "${marker}" ] && + grep -q 'Unable to delete installation folder' "${log}" && + ! grep -qE 'Failed to (correct ownership|restore configuration)|Refusing to change permissions' "${log}"; then + touch "${pending}" + fi + rm -f "${log}" +} + +finish_cleanup() { + rm -rf "${webroot}/installation" || return 1 + rm -f "${webroot}/joomla.xml" || return 1 + if [ ! -e "${webroot}/robots.txt" ] && [ -e "${webroot}/robots.txt.dist" ]; then + mv "${webroot}/robots.txt.dist" "${webroot}/robots.txt" || return 1 + fi + rmdir "${marker}" || return 1 + rm -f "${pending}" +} + +recover() { + if [ ! -e "${marker}" ]; then + run_setup + fi + if [ -e "${marker}" ] && [ -e "${pending}" ] && ! finish_cleanup; then + log_error "Finishing Joomla's installer cleanup in ${webroot} failed; ${pending} stays, so the next start retries it." + return 1 + fi + if initialized; then + touch "${ready}" + return 0 + fi + if [ -e "${marker}" ]; then + log_error "${marker} remains from an unfinished Joomla setup. If that setup stopped at 'Unable to delete installation folder!', run 'touch ${pending}' and restart; otherwise restore the site and database, then remove ${marker}." + fi + return 1 +} + +lead() { + local status + printf '%s\n' "${token}" > "${owner}" + trap release EXIT + heartbeat & + heartbeat_pid=$! + recover + status=$? + release + trap - EXIT + return "${status}" +} + +until initialized; do + if mkdir "${lock}" 2>/dev/null; then + lead || exit 1 + elif ! take_over_stale; then + sleep 2 + fi +done + +exec "${stock}" "$@" diff --git a/roles/web-app-joomla/templates/env.j2 b/roles/web-app-joomla/templates/env.j2 index 0fdedc5d0f..14dd382f92 100644 --- a/roles/web-app-joomla/templates/env.j2 +++ b/roles/web-app-joomla/templates/env.j2 @@ -1,6 +1,8 @@ {% include 'roles/sys-svc-container/templates/env.j2' %} +JOOMLA_INIT_LOCK_STALE_SECONDS={{ JOOMLA_INIT_LOCK_STALE_SECONDS }} JOOMLA_INIT_READY_FILE={{ JOOMLA_INIT_READY_FILE }} +JOOMLA_WEBROOT={{ JOOMLA_DOCROOT }} JOOMLA_SITE_NAME={{ JOOMLA_SITE_NAME }} JOOMLA_ADMIN_USER={{ JOOMLA_USER }} JOOMLA_ADMIN_USERNAME={{ JOOMLA_USER_NAME }} diff --git a/roles/web-app-joomla/vars/main.yml b/roles/web-app-joomla/vars/main.yml index fd24e7d44d..c4744ff5c5 100644 --- a/roles/web-app-joomla/vars/main.yml +++ b/roles/web-app-joomla/vars/main.yml @@ -13,6 +13,7 @@ JOOMLA_CONFIG_FILE: "/var/www/html/configuration.php" JOOMLA_INSTALLER_CLI_FILE: "/var/www/html/installation/joomla.php" JOOMLA_CLI_FILE: "/var/www/html/cli/joomla.php" JOOMLA_INIT_READY_FILE: "/var/www/html/.infinito-init.ready" +JOOMLA_INIT_LOCK_STALE_SECONDS: 180 JOOMLA_CLI_EXTENSION_ENABLE_SINCE: "6.1.0" JOOMLA_DOCROOT: "{{ lookup('volume', application_id, 'data').target }}" diff --git a/tests/unit/python/roles/web-app-joomla/files/__init__.py b/tests/unit/python/roles/web-app-joomla/files/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/unit/python/roles/web-app-joomla/files/shell/__init__.py b/tests/unit/python/roles/web-app-joomla/files/shell/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/unit/python/roles/web-app-joomla/files/shell/test_docker_entrypoint_joomla.py b/tests/unit/python/roles/web-app-joomla/files/shell/test_docker_entrypoint_joomla.py new file mode 100644 index 0000000000..1a4b6c9d93 --- /dev/null +++ b/tests/unit/python/roles/web-app-joomla/files/shell/test_docker_entrypoint_joomla.py @@ -0,0 +1,255 @@ +import contextlib +import os +import signal +import subprocess +import tempfile +import time +import unittest +from pathlib import Path + +from utils import PROJECT_ROOT + +SHELL_DIR = PROJECT_ROOT / "roles/web-app-joomla/files/shell" +ENTRYPOINT = SHELL_DIR / "docker-entrypoint-joomla.sh" +SERVE = "serve apache2-foreground" + +STOCK_STUB = """#!/usr/bin/env bash +printf 'call %s\\n' "$*" >> "${STUB_CALLS}" +case "$1" in apache2*) ;; *) exit 0 ;; esac +case "${2-}" in -v | -V | -h | -t | -T) exit 0 ;; esac +cd "${JOOMLA_WEBROOT}" || exit 9 +if [ -e .joomla-initializing ]; then + echo '[ERROR] Initialization is incomplete or another initializer is active.' >&2 + exit 1 +fi +if [ ! -s configuration.php ]; then + if [ "${STUB_DB}" != up ]; then + echo '[ERROR] Database initialization failed (exit 1).' >&2 + exit 1 + fi + mkdir .joomla-initializing + printf 'init\\n' >> "${STUB_CALLS}" + mkdir -p installation + touch index.php installation/joomla.php joomla.xml robots.txt.dist + sleep "${STUB_SETUP_DELAY}" + if [ "${STUB_INSTALL}" = broken ]; then + echo '[ERROR] Automatic Joomla installation failed (exit 1).' >&2 + exit 1 + fi + echo ' configuration.php + sleep "${STUB_FINISH_DELAY}" + case "${STUB_INSTALL}" in + crash) + echo '[ERROR] Automatic Joomla installation failed (exit 255).' >&2 + exit 1 ;; + nfs) + echo ' [ERROR] Unable to delete installation folder!' + exit 1 ;; + nfs-unowned) + echo ' [ERROR] Unable to delete installation folder!' + echo '[ERROR] Failed to correct ownership after initialization.' >&2 + exit 1 ;; + esac + rm -rf installation joomla.xml + mv robots.txt.dist robots.txt + rmdir .joomla-initializing +fi +printf 'serve %s\\n' "$*" >> "${STUB_CALLS}" +""" + +RM_STUB = """#!/usr/bin/env bash +if [ "${STUB_RM_FAIL}" = 1 ]; then + case "$*" in *installation*) echo 'rm: Device or resource busy' >&2; exit 1 ;; esac +fi +PATH="${STUB_REAL_PATH}" exec rm "$@" +""" + + +class TestJoomlaEntrypoint(unittest.TestCase): + def setUp(self): + tmp = tempfile.TemporaryDirectory() + self.addCleanup(tmp.cleanup) + self.root = Path(tmp.name) + self.webroot = self.root / "html" + self.webroot.mkdir() + bin_dir = self.root / "bin" + bin_dir.mkdir() + self.stock = self.root / "entrypoint.sh" + for path, body in {self.stock: STOCK_STUB, bin_dir / "rm": RM_STUB}.items(): + path.write_text(body) + path.chmod(0o755) + self.ready = self.webroot / ".infinito-init.ready" + self.marker = self.webroot / ".joomla-initializing" + self.lock = self.webroot / ".infinito-init.lock" + self.pending = self.webroot / ".infinito-init.cleanup" + self.env = { + **os.environ, + "PATH": f"{bin_dir}{os.pathsep}{os.environ['PATH']}", + "STUB_REAL_PATH": os.environ["PATH"], + "JOOMLA_WEBROOT": str(self.webroot), + "JOOMLA_STOCK_ENTRYPOINT": str(self.stock), + "JOOMLA_INIT_READY_FILE": str(self.ready), + "JOOMLA_INIT_LOCK_STALE_SECONDS": "60", + "STUB_DB": "up", + "STUB_INSTALL": "ok", + "STUB_SETUP_DELAY": "0", + "STUB_FINISH_DELAY": "0", + "STUB_RM_FAIL": "0", + } + + def spawn(self, name="calls", **env): + log = self.root / f"{name}.log" + log.touch() + with (self.root / f"{name}.err").open("a") as err: + process = subprocess.Popen( + ["bash", str(ENTRYPOINT), "apache2-foreground"], + env={**self.env, "STUB_CALLS": str(log), **env}, + stdout=subprocess.DEVNULL, + stderr=err, + start_new_session=True, + ) + self.addCleanup(self.kill_group, process) + return process + + def kill_group(self, process): + with contextlib.suppress(ProcessLookupError, PermissionError): + os.killpg(process.pid, signal.SIGKILL) + process.wait() + + def run_entrypoint(self, **env): + return self.spawn(**env).wait(timeout=60) + + def text(self, name): + path = self.root / name + return path.read_text(encoding="utf-8") # nocheck: cache-read tempdir + + def calls(self, name="calls"): + return self.text(f"{name}.log").splitlines() + + def wait_for(self, path): + deadline = time.monotonic() + 30 + while not path.exists() and time.monotonic() < deadline: + time.sleep(0.1) + self.assertTrue(path.exists()) + + def leave_stuck_volume(self): + for name in ("installation", ".joomla-initializing", ".infinito-init.lock"): + (self.webroot / name).mkdir() + (self.webroot / "configuration.php").write_text("