perf(web-app-friendica,web-app-matomo): report config patches changed only on change

Friendica's local.config.php patch (two sed -ri tasks) and Matomo's
config.ini.php database patch (four sed -i tasks) carried no changed_when
and notified compose-up, so every deploy reported them changed and
force-recreated the stack: a second compose flush for Friendica (about
19 s plus a restart) and pull, up and a health wait for Matomo (about
15 s plus a restart). Matomo's trusted-hosts script was already
idempotent but reported changed every run as well.

Each patch is now one script under files/shell/ that dry-runs its sed
expressions, compares cksum with the file, rewrites it only on a
difference and prints PATCHED; the tasks set changed_when on that marker.
The trusted-hosts script compares cksum before and after. Values reach
the scripts through the environment and are written literally, so a
password containing $ or a backtick is no longer mangled by host-shell
double quotes.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
kevinveenbirkenbachandClaude Opus 5.5 committed 2026-10-24 19:58:29 +02:00
1 parent dba6aa7407
commit 269a15863f
6 files changed
+80 -45

No files matched your search

@@ -0,0 +1,25 @@
#!/bin/sh
# Param: $1 local.config.php to rewrite in place.
# Param: PATCH_HOSTNAME database host:port, escaped for the / delimiter.
# Param: PATCH_DATABASE database name, escaped for the / delimiter.
# Param: PATCH_USERNAME database user, escaped for the / delimiter.
# Param: PATCH_PASSWORD database password, escaped for the / delimiter.
# Param: PATCH_URL system.url, escaped for the # delimiter.
# Output: "PATCHED" when the content of $1 changed, nothing when it already matched.
set -eu
config="$1"
rewrite() {
sed -E "$@" \
-e "s/('hostname'\s*=>\s*')[^']*(',)/\1${PATCH_HOSTNAME}\2/" \
-e "s/('database'\s*=>\s*')[^']*(',)/\1${PATCH_DATABASE}\2/" \
-e "s/('username'\s*=>\s*')[^']*(',)/\1${PATCH_USERNAME}\2/" \
-e "s/('password'\s*=>\s*')[^']*(',)/\1${PATCH_PASSWORD}\2/" \
-e "s#('url'\s*=>\s*')[^']*(',)#\1${PATCH_URL}\2#" \
"${config}"
}
[ "$(rewrite | cksum)" = "$(cksum <"${config}")" ] && exit 0
rewrite -i
echo PATCHED
@@ -1,21 +1,19 @@
---
- name: 🩹 Patch Friendica local.config.php (DB host, name, user, system.url)
shell: > # nocheck: container-exec-resolver FRIENDICA_LOCAL_CID resolved at delegate_to runtime
container exec --user {{ FRIENDICA_USER }} {{ FRIENDICA_LOCAL_CID | quote }}
sed -ri
-e "s/('hostname'\s*=>\s*')[^']*(',)/\1{{ lookup('database', application_id, 'host') | sed_escape('/') }}:{{ lookup('database', application_id, 'port') | sed_escape('/')
}}\2/"
-e "s/('database'\s*=>\s*')[^']*(',)/\1{{ lookup('database', application_id, 'name') | sed_escape('/') }}\2/"
-e "s/('username'\s*=>\s*')[^']*(',)/\1{{ lookup('database', application_id, 'username') | sed_escape('/') }}\2/"
-e "s#('url'\s*=>\s*')[^']*(',)#\1{{ FRIENDICA_BASE_URL | sed_escape('#') }}\2#"
{{ FRIENDICA_CONFIG_FILE }}
delegate_to: "{{ FRIENDICA_HOST_NODE }}" # nocheck: include-uniform-apply-keyword
- name: 🩹 Patch Friendica local.config.php (DB password)
shell: >
container exec --user {{ FRIENDICA_USER }}
-e PATCH_PASSWORD={{ lookup('database', application_id, 'password') | sed_escape('/') | quote }}
{{ FRIENDICA_LOCAL_CID | quote }}
sh -c "sed -ri -e \"s/('password'\\s*=>\\s*')[^']*(',)/\\1\${PATCH_PASSWORD}\\2/\" {{ FRIENDICA_CONFIG_FILE }}"
- name: 🩹 Patch Friendica local.config.php (DB credentials, system.url)
ansible.builtin.shell:
cmd: >- # nocheck: container-exec-resolver FRIENDICA_LOCAL_CID resolved at delegate_to runtime
container exec -i --user {{ FRIENDICA_USER }}
-e PATCH_HOSTNAME -e PATCH_DATABASE -e PATCH_USERNAME -e PATCH_PASSWORD -e PATCH_URL
{{ FRIENDICA_LOCAL_CID | quote }}
sh -s -- {{ FRIENDICA_CONFIG_FILE | quote }}
stdin: "{{ lookup('file', 'shell/patch_local_config.sh') }}"
environment:
PATCH_HOSTNAME: "{{ lookup('database', application_id, 'host') | sed_escape('/') }}:{{ lookup('database', application_id, 'port') | sed_escape('/') }}"
PATCH_DATABASE: "{{ lookup('database', application_id, 'name') | sed_escape('/') }}"
PATCH_USERNAME: "{{ lookup('database', application_id, 'username') | sed_escape('/') }}"
PATCH_PASSWORD: "{{ lookup('database', application_id, 'password') | sed_escape('/') }}"
PATCH_URL: "{{ FRIENDICA_BASE_URL | sed_escape('#') }}"
delegate_to: "{{ FRIENDICA_HOST_NODE }}"
register: friendica_config_patch
changed_when: "'PATCHED' in friendica_config_patch.stdout"
no_log: "{{ MASK_CREDENTIALS_IN_LOGS | bool }}"
@@ -0,0 +1,23 @@
#!/bin/sh
# Param: $1 config.ini.php to rewrite in place.
# Param: PATCH_HOST database host, escaped for the / delimiter.
# Param: PATCH_NAME database name, escaped for the / delimiter.
# Param: PATCH_USERNAME database user, escaped for the / delimiter.
# Param: PATCH_PASSWORD database password, escaped for the / delimiter.
# Output: "PATCHED" when the content of $1 changed, nothing when it already matched.
set -eu
config="$1"
rewrite() {
sed "$@" \
-e "s/^host *=.*/host = ${PATCH_HOST}/" \
-e "s/^dbname *=.*/dbname = ${PATCH_NAME}/" \
-e "s/^username *=.*/username = ${PATCH_USERNAME}/" \
-e "s/^password *=.*/password = ${PATCH_PASSWORD}/" \
"${config}"
}
[ "$(rewrite | cksum)" = "$(cksum <"${config}")" ] && exit 0
rewrite -i
echo PATCHED
@@ -3,8 +3,11 @@
# Env: DOMAIN Public canonical domain to land in the first trusted_hosts slot.
# Env: SERVICE Stack-internal service name (added as a trusted host).
# Env: LOCAL host:port the role uses for in-host probes (added as a trusted host).
# Output: "PATCHED" when the content of CFG changed, nothing when it already matched.
set -eu
before="$(cksum <"$CFG")"
ensure_host() {
host="$1"
if ! grep -qE "^trusted_hosts\[\][[:space:]]*= \"${host}\"$" "$CFG"; then
@@ -39,3 +42,5 @@ if ! grep -qE "^enable_framed_pages[[:space:]]*=" "$CFG"; then
{ print }
' "$CFG" > "$CFG.tmp" && mv "$CFG.tmp" "$CFG"
fi
[ "$(cksum <"$CFG")" = "$before" ] || echo PATCHED
@@ -2,33 +2,15 @@
- name: "🧭 Resolve host path to config.ini.php"
ansible.builtin.include_tasks: utils/resolve/config_host.yml
- name: "🔄 Update DB host"
ansible.builtin.command:
argv:
- sed
- -i
- "s/^host *=.*/host = {{ lookup('database', application_id, 'host') | sed_escape('/') }}/"
- "{{ MATOMO_CONFIG_HOST }}"
- name: "🔄 Update DB name"
ansible.builtin.command:
argv:
- sed
- -i
- "s/^dbname *=.*/dbname = {{ lookup('database', application_id, 'name') | sed_escape('/') }}/"
- "{{ MATOMO_CONFIG_HOST }}"
- name: "🔄 Update DB user"
ansible.builtin.command:
argv:
- sed
- -i
- "s/^username *=.*/username = {{ lookup('database', application_id, 'username') | sed_escape('/') }}/"
- "{{ MATOMO_CONFIG_HOST }}"
- name: "🔄 Update DB password"
ansible.builtin.shell:
cmd: sed -i "s/^password *=.*/password = ${PATCH_PASSWORD}/" {{ MATOMO_CONFIG_HOST | quote }}
- name: "🔄 Update DB host, name, user and password"
ansible.builtin.shell: sh -s -- {{ MATOMO_CONFIG_HOST | quote }}
args:
stdin: "{{ lookup('file', 'shell/patch_database.sh') }}"
environment:
PATCH_HOST: "{{ lookup('database', application_id, 'host') | sed_escape('/') }}"
PATCH_NAME: "{{ lookup('database', application_id, 'name') | sed_escape('/') }}"
PATCH_USERNAME: "{{ lookup('database', application_id, 'username') | sed_escape('/') }}"
PATCH_PASSWORD: "{{ lookup('database', application_id, 'password') | sed_escape('/') }}"
register: matomo_database_patch
changed_when: "'PATCHED' in matomo_database_patch.stdout"
no_log: "{{ MASK_CREDENTIALS_IN_LOGS | bool }}"
@@ -18,4 +18,6 @@
SERVICE: "{{ MATOMO_SERVICE }}"
LOCAL: "{{ DOCKER_REACH_HOST }}:{{ http_port }}"
delegate_to: "{{ MATOMO_HOST_NODE }}"
register: matomo_trusted_hosts_patch
changed_when: "'PATCHED' in matomo_trusted_hosts_patch.stdout"
notify: compose-up