From 727ba456263cb2c5b8ed69bc4fc4387b8531c78f Mon Sep 17 00:00:00 2001 From: Kevin Veen-Birkenbach Date: Sat, 7 Nov 2026 06:55:17 +0000 Subject: [PATCH 1/2] fix(web-app-moodle): stand a second bootstrap down and gate the ownership walks on a settled one MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Main run 42235551402 (5d78f3ae7d), job 128223280156 Moodle#2 (swarm): 03_ownership.yml:24 'find -exec chmod 0644' failed with hundreds of 'No such file or directory' under public/lib/yuilib. moodle_code is one NFS export shared by three replicas and cron, all on release 5.3.0. The bootstrap lock lives on that export and does not serialize across NFS clients (workers log 'flock: 9: Input/output error'). So a second same-release bootstrap swapped the tree in while the deploy walked it; the rescue listing at 02:22:34 shows neither .bootstrap.stage nor .bootstrap.done, the window between the stage rmdir and the sentinel write. Port of two store-i18n commits by Stage; 5c3749c437 and 7bc5822421 are already on main: - moodle_bootstrap_code_dir re-reads the sentinel after its copy and stands down when another container finished the same release (cherry picked from commit db1a27b3f2). - The ownership walks wait until the stage is gone and the sentinel holds the expected release, prune the stage and batch through -exec +; stage and sentinel paths come from vars/main.yml via ARG/ENV (cherry picked from commit 9a5965b87c). The rationale header the port added to moodle-entrypoint.sh and a stray comment line in 03_ownership.yml are dropped. test_moodle_entrypoint: 8/8 green; without the stand-down test_a_sibling_that_finishes_first_keeps_its_tree fails. 🎯 Fix: 88 % 💥 Break: 6 % 💸 Cost: 100 % ⏱️ Time: 100 % 🛡️ Security: ~100 % 🏆 Score: 83 🚦 Gate: ⛔ fix 88 % < 90, break 6 % > 5 Co-Authored-By: Claude Opus 5.5 --- roles/web-app-moodle/files/Dockerfile | 6 ++- .../files/build/moodle-entrypoint.sh | 8 +++- roles/web-app-moodle/tasks/03_ownership.yml | 16 ++++++-- roles/web-app-moodle/templates/compose.yml.j2 | 2 + roles/web-app-moodle/vars/main.yml | 2 + .../files/build/test_moodle_entrypoint.py | 40 +++++++++++++++++-- 6 files changed, 64 insertions(+), 10 deletions(-) diff --git a/roles/web-app-moodle/files/Dockerfile b/roles/web-app-moodle/files/Dockerfile index f6aa1f92c2..ca6230f732 100644 --- a/roles/web-app-moodle/files/Dockerfile +++ b/roles/web-app-moodle/files/Dockerfile @@ -4,7 +4,7 @@ # compose.yml.j2, which derives them from roles/web-app-moodle/vars/main.yml. # That vars file is the single source of truth for: # - container paths (MOODLE_CODE_DIR, MOODLE_DATA_DIR, MOODLE_SOURCE_DIR, -# MOODLE_ENTRYPOINT_PATH) +# MOODLE_ENTRYPOINT_PATH, MOODLE_BOOTSTRAP_STAGE) # - build-asset names (MOODLE_BUILD_DIR, MOODLE_BUILD_FILES.*) # # The COPY src paths and the ENTRYPOINT JSON-array literal in this @@ -22,6 +22,8 @@ ARG MOODLE_CODE_DIR ARG MOODLE_DATA_DIR ARG MOODLE_LOCAL_CACHE_DIR ARG MOODLE_SOURCE_DIR +ARG MOODLE_BOOTSTRAP_STAGE +ARG MOODLE_BOOTSTRAP_SENTINEL ARG MOODLE_RUNTIME_USER ARG MOODLE_OIDC_ENABLED ARG MOODLE_OIDC_PLUGIN_RELPATH @@ -39,6 +41,8 @@ ENV MOODLE_CODE_DIR=${MOODLE_CODE_DIR} \ MOODLE_DATA_DIR=${MOODLE_DATA_DIR} \ MOODLE_LOCAL_CACHE_DIR=${MOODLE_LOCAL_CACHE_DIR} \ MOODLE_SOURCE_DIR=${MOODLE_SOURCE_DIR} \ + MOODLE_BOOTSTRAP_STAGE=${MOODLE_BOOTSTRAP_STAGE} \ + MOODLE_BOOTSTRAP_SENTINEL=${MOODLE_BOOTSTRAP_SENTINEL} \ MOODLE_RUNTIME_USER=${MOODLE_RUNTIME_USER} \ MOODLE_OIDC_PLUGIN_RELPATH=${MOODLE_OIDC_PLUGIN_RELPATH} \ MOODLE_MCP_PLUGIN_RELPATH=${MOODLE_MCP_PLUGIN_RELPATH} \ diff --git a/roles/web-app-moodle/files/build/moodle-entrypoint.sh b/roles/web-app-moodle/files/build/moodle-entrypoint.sh index 30083ff2ff..6da08daa4f 100644 --- a/roles/web-app-moodle/files/build/moodle-entrypoint.sh +++ b/roles/web-app-moodle/files/build/moodle-entrypoint.sh @@ -12,13 +12,13 @@ set -euo pipefail : "${MOODLE_DATA_DIR:?required}" : "${MOODLE_LOCAL_CACHE_DIR:?required}" : "${MOODLE_SOURCE_DIR:?required}" +: "${MOODLE_BOOTSTRAP_STAGE:?required}" +: "${MOODLE_BOOTSTRAP_SENTINEL:?required}" : "${MOODLE_RUNTIME_USER:?required}" : "${MOODLE_VERSION_FILE:?required}" : "${MOODLE_RELEASE:?required}" -MOODLE_BOOTSTRAP_SENTINEL="${MOODLE_CODE_DIR}/.bootstrap.done" MOODLE_BOOTSTRAP_LOCK="${MOODLE_CODE_DIR}/.bootstrap.lock" -MOODLE_BOOTSTRAP_STAGE="${MOODLE_CODE_DIR}/.bootstrap.stage" mkdir -p "${MOODLE_DATA_DIR}" chown -R "${MOODLE_RUNTIME_USER}:${MOODLE_RUNTIME_USER}" "${MOODLE_DATA_DIR}" || true # nocheck: shell-or-true -- grandfathered: worked in practice; TODO: sharpen to catch only the exact tolerated error @@ -51,6 +51,10 @@ moodle_bootstrap_code_dir() { rm -rf "${MOODLE_BOOTSTRAP_STAGE}" mkdir "${MOODLE_BOOTSTRAP_STAGE}" cp -a "${MOODLE_SOURCE_DIR}/." "${MOODLE_BOOTSTRAP_STAGE}/" + if moodle_code_dir_is_current; then + rm -rf "${MOODLE_BOOTSTRAP_STAGE}" + return 0 + fi find "${MOODLE_CODE_DIR}" -mindepth 1 -maxdepth 1 ! -name "${MOODLE_BOOTSTRAP_LOCK##*/}" ! -name "${MOODLE_BOOTSTRAP_STAGE##*/}" -exec rm -rf {} + find "${MOODLE_BOOTSTRAP_STAGE}" -mindepth 1 -maxdepth 1 -exec mv -t "${MOODLE_CODE_DIR}" {} + rmdir "${MOODLE_BOOTSTRAP_STAGE}" diff --git a/roles/web-app-moodle/tasks/03_ownership.yml b/roles/web-app-moodle/tasks/03_ownership.yml index 7dd3958eaf..c441e3752d 100644 --- a/roles/web-app-moodle/tasks/03_ownership.yml +++ b/roles/web-app-moodle/tasks/03_ownership.yml @@ -9,8 +9,18 @@ # because only Moodle FPM (running as MOODLE_RUNTIME_USER) needs to # read/write user-uploaded content. +- name: "⏳ Wait until no container is still bootstrapping the shared code volume" + shell: > + container exec --user root {{ MOODLE_CONTAINER_ADDRESS }} + sh -c 'test ! -e {{ MOODLE_BOOTSTRAP_STAGE }} && test "$(cat {{ MOODLE_BOOTSTRAP_SENTINEL }} 2>/dev/null)" = "{{ MOODLE_RELEASE }}"' + register: moodle_bootstrap_settled + until: moodle_bootstrap_settled.rc == 0 + retries: 120 + delay: 5 + changed_when: false + - name: "✅ Ensure ownership is correct on Moodle code dir" - shell: "container exec --user root {{ MOODLE_CONTAINER_ADDRESS }} chown -R {{ MOODLE_RUNTIME_USER_GROUP }} {{ MOODLE_CODE_DIR }}" + shell: "container exec --user root {{ MOODLE_CONTAINER_ADDRESS }} find {{ MOODLE_CODE_DIR }} -path {{ MOODLE_BOOTSTRAP_STAGE }} -prune -o -exec chown -h {{ MOODLE_RUNTIME_USER_GROUP }} {} +" changed_when: false - name: "✅ Ensure ownership is correct on Moodle data dir" @@ -18,11 +28,11 @@ changed_when: false - name: "📁 Code dir: 0755 dirs (nginx-readable)" - shell: "container exec --user root {{ MOODLE_CONTAINER_ADDRESS }} find {{ MOODLE_CODE_DIR }} -type d -exec chmod 0755 {} \\;" + shell: "container exec --user root {{ MOODLE_CONTAINER_ADDRESS }} find {{ MOODLE_CODE_DIR }} -path {{ MOODLE_BOOTSTRAP_STAGE }} -prune -o -type d -exec chmod 0755 {} +" changed_when: false - name: "📁 Code dir: 0644 files (nginx-readable)" - shell: "container exec --user root {{ MOODLE_CONTAINER_ADDRESS }} find {{ MOODLE_CODE_DIR }} -type f -exec chmod 0644 {} \\;" + shell: "container exec --user root {{ MOODLE_CONTAINER_ADDRESS }} find {{ MOODLE_CODE_DIR }} -path {{ MOODLE_BOOTSTRAP_STAGE }} -prune -o -type f -exec chmod 0644 {} +" changed_when: false - name: "📁 Code dir: 0640 config.php (FPM-only)" diff --git a/roles/web-app-moodle/templates/compose.yml.j2 b/roles/web-app-moodle/templates/compose.yml.j2 index 0bb8966b57..f440a872c2 100644 --- a/roles/web-app-moodle/templates/compose.yml.j2 +++ b/roles/web-app-moodle/templates/compose.yml.j2 @@ -14,6 +14,8 @@ MOODLE_DATA_DIR: "{{ MOODLE_DATA_DIR }}" MOODLE_LOCAL_CACHE_DIR: "{{ MOODLE_LOCAL_CACHE_DIR }}" MOODLE_SOURCE_DIR: "{{ MOODLE_SOURCE_DIR }}" + MOODLE_BOOTSTRAP_STAGE: "{{ MOODLE_BOOTSTRAP_STAGE }}" + MOODLE_BOOTSTRAP_SENTINEL: "{{ MOODLE_BOOTSTRAP_SENTINEL }}" MOODLE_RUNTIME_USER: "{{ MOODLE_RUNTIME_USER }}" MOODLE_OIDC_ENABLED: "{{ MOODLE_OIDC_ENABLED | bool | ternary('1','0') }}" MOODLE_OIDC_PLUGIN_RELPATH: "{{ MOODLE_OIDC_PLUGIN_RELPATH }}" diff --git a/roles/web-app-moodle/vars/main.yml b/roles/web-app-moodle/vars/main.yml index e6cab65f67..d9737b3410 100644 --- a/roles/web-app-moodle/vars/main.yml +++ b/roles/web-app-moodle/vars/main.yml @@ -8,6 +8,8 @@ MOODLE_DATA_DIR: "{{ lookup('volume', 'web-app-moodle', 'data').target }}" MOODLE_LOCAL_CACHE_DIR: "{{ lookup('volume', application_id, 'localcache').mounts[0].target }}" MOODLE_SOURCE_DIR: "/opt/moodle-source" MOODLE_ENTRYPOINT_PATH: "/usr/local/bin/moodle-entrypoint.sh" +MOODLE_BOOTSTRAP_STAGE: "{{ [MOODLE_CODE_DIR, '.bootstrap.stage'] | path_join }}" +MOODLE_BOOTSTRAP_SENTINEL: "{{ [MOODLE_CODE_DIR, '.bootstrap.done'] | path_join }}" MOODLE_BUILD_DIR: "build" MOODLE_BUILD_FILES: diff --git a/tests/unit/python/roles/web-app-moodle/files/build/test_moodle_entrypoint.py b/tests/unit/python/roles/web-app-moodle/files/build/test_moodle_entrypoint.py index 9ed0c5bcb0..d75b6510fc 100644 --- a/tests/unit/python/roles/web-app-moodle/files/build/test_moodle_entrypoint.py +++ b/tests/unit/python/roles/web-app-moodle/files/build/test_moodle_entrypoint.py @@ -13,6 +13,12 @@ ENTRYPOINT = PROJECT_ROOT / "roles/web-app-moodle/files/build/moodle-entrypoint. FPM_STUB = "#!/usr/bin/env bash\nexit 0\n" +CP_STUB = ( + "#!/usr/bin/env bash\n" + 'printf %s "$SIBLING_RELEASE" > "${MOODLE_CODE_DIR}/.bootstrap.done"\n' + 'exec @REAL_CP@ "$@"\n' +) + LAYOUT_4 = {"version.php": "4.5.11", "auth/oidc/version.php": "4.5.8"} LAYOUT_5 = {"public/version.php": "5.3.0", "public/auth/oidc/version.php": "5.2.1"} @@ -24,22 +30,38 @@ class TestMoodleEntrypoint(unittest.TestCase): self.root = Path(tmp.name) self.source = self.root / "source" self.code = self.root / "code" - bin_dir = self.root / "bin" - bin_dir.mkdir() - stub = bin_dir / "php-fpm" + self.stage = self.code / ".bootstrap.stage" + self.sentinel = self.code / ".bootstrap.done" + self.bin_dir = self.root / "bin" + self.bin_dir.mkdir() + self.real_cp = shutil.which("cp") + stub = self.bin_dir / "php-fpm" stub.write_text(FPM_STUB) stub.chmod(0o755) self.env = dict(os.environ) self.env.update( - PATH=f"{bin_dir}{os.pathsep}{self.env['PATH']}", + PATH=f"{self.bin_dir}{os.pathsep}{self.env['PATH']}", MOODLE_CODE_DIR=str(self.code), MOODLE_DATA_DIR=str(self.root / "data"), MOODLE_LOCAL_CACHE_DIR=str(self.root / "localcache"), MOODLE_SOURCE_DIR=str(self.source), + MOODLE_BOOTSTRAP_STAGE=str(self.stage), + MOODLE_BOOTSTRAP_SENTINEL=str(self.sentinel), MOODLE_RUNTIME_USER=pwd.getpwuid(os.getuid()).pw_name, MOODLE_VERSION_FILE="public/version.php", ) + def sibling_finishes_during_the_copy(self, release): + """Stub ``cp`` so another container marks the volume done mid-bootstrap. + + Args: + release: release the sibling records in the bootstrap sentinel. + """ + stub = self.bin_dir / "cp" + stub.write_text(CP_STUB.replace("@REAL_CP@", self.real_cp)) + stub.chmod(0o755) + self.env["SIBLING_RELEASE"] = release + def write(self, tree, files): for name, content in files.items(): path = tree / name @@ -109,6 +131,16 @@ class TestMoodleEntrypoint(unittest.TestCase): self.assertEqual(self.shipped_files_in_the_volume(), set(LAYOUT_4)) self.assertEqual(read_text(str(self.code / ".bootstrap.done")), "4.5.11") + def test_a_sibling_that_finishes_first_keeps_its_tree(self): + self.ship(LAYOUT_5) + installed = {"public/local/installed/version.php": "written by the deploy"} + self.write(self.code, installed) + self.sibling_finishes_during_the_copy("5.3.0") + self.start("5.3.0") + self.assertTrue((self.code / "public/local/installed/version.php").is_file()) + self.assertFalse(self.stage.exists()) + self.assertEqual(read_text(str(self.code / ".bootstrap.done")), "5.3.0") + def test_an_image_older_than_the_recorded_release_refuses_to_replace_it(self): self.ship(LAYOUT_5) self.start("5.10.0") From 584f0b99ec2cbc03f2bf7381b537681167ef7878 Mon Sep 17 00:00:00 2001 From: Kevin Veen-Birkenbach Date: Sat, 7 Nov 2026 08:49:32 +0000 Subject: [PATCH 2/2] style(web-app-moodle): wrap the ownership walks as the formatter does MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit make quality-high's autoformat re-wrapped the three long double-quoted shell: lines of 03_ownership.yml that 727ba45626 introduced. A line break inside a double-quoted YAML scalar folds into one space, so the parsed task file at 727ba45626 and after this commit compares equal. 🎯 Fix: 99 % 💥 Break: 1 % 💸 Cost: 100 % ⏱️ Time: – 🛡️ Security: 100 % 🏆 Score: 98 🚦 Gate: ✅ 90/5, cost and time <= 100, security 100 % Co-Authored-By: Claude Opus 5.5 --- roles/web-app-moodle/tasks/03_ownership.yml | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/roles/web-app-moodle/tasks/03_ownership.yml b/roles/web-app-moodle/tasks/03_ownership.yml index c441e3752d..34d28b07a5 100644 --- a/roles/web-app-moodle/tasks/03_ownership.yml +++ b/roles/web-app-moodle/tasks/03_ownership.yml @@ -20,7 +20,8 @@ changed_when: false - name: "✅ Ensure ownership is correct on Moodle code dir" - shell: "container exec --user root {{ MOODLE_CONTAINER_ADDRESS }} find {{ MOODLE_CODE_DIR }} -path {{ MOODLE_BOOTSTRAP_STAGE }} -prune -o -exec chown -h {{ MOODLE_RUNTIME_USER_GROUP }} {} +" + shell: "container exec --user root {{ MOODLE_CONTAINER_ADDRESS }} find {{ MOODLE_CODE_DIR }} -path {{ MOODLE_BOOTSTRAP_STAGE }} -prune -o -exec chown -h {{ MOODLE_RUNTIME_USER_GROUP + }} {} +" changed_when: false - name: "✅ Ensure ownership is correct on Moodle data dir" @@ -28,11 +29,13 @@ changed_when: false - name: "📁 Code dir: 0755 dirs (nginx-readable)" - shell: "container exec --user root {{ MOODLE_CONTAINER_ADDRESS }} find {{ MOODLE_CODE_DIR }} -path {{ MOODLE_BOOTSTRAP_STAGE }} -prune -o -type d -exec chmod 0755 {} +" + shell: "container exec --user root {{ MOODLE_CONTAINER_ADDRESS }} find {{ MOODLE_CODE_DIR }} -path {{ MOODLE_BOOTSTRAP_STAGE }} -prune -o -type d -exec chmod 0755 + {} +" changed_when: false - name: "📁 Code dir: 0644 files (nginx-readable)" - shell: "container exec --user root {{ MOODLE_CONTAINER_ADDRESS }} find {{ MOODLE_CODE_DIR }} -path {{ MOODLE_BOOTSTRAP_STAGE }} -prune -o -type f -exec chmod 0644 {} +" + shell: "container exec --user root {{ MOODLE_CONTAINER_ADDRESS }} find {{ MOODLE_CODE_DIR }} -path {{ MOODLE_BOOTSTRAP_STAGE }} -prune -o -type f -exec chmod 0644 + {} +" changed_when: false - name: "📁 Code dir: 0640 config.php (FPM-only)"