diff --git a/roles/web-app-friendica/files/shell/patch_local_config.sh b/roles/web-app-friendica/files/shell/patch_local_config.sh new file mode 100644 index 0000000000..97503095d2 --- /dev/null +++ b/roles/web-app-friendica/files/shell/patch_local_config.sh @@ -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 diff --git a/roles/web-app-friendica/tasks/utils/patch/config.yml b/roles/web-app-friendica/tasks/utils/patch/config.yml index 5f0bc0e29d..83bd41d96c 100644 --- a/roles/web-app-friendica/tasks/utils/patch/config.yml +++ b/roles/web-app-friendica/tasks/utils/patch/config.yml @@ -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 }}" diff --git a/roles/web-app-matomo/files/shell/patch_database.sh b/roles/web-app-matomo/files/shell/patch_database.sh new file mode 100644 index 0000000000..a455d44c6b --- /dev/null +++ b/roles/web-app-matomo/files/shell/patch_database.sh @@ -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 diff --git a/roles/web-app-matomo/files/shell/patch_trusted_hosts.sh b/roles/web-app-matomo/files/shell/patch_trusted_hosts.sh index be4cf1d636..8014a8db73 100755 --- a/roles/web-app-matomo/files/shell/patch_trusted_hosts.sh +++ b/roles/web-app-matomo/files/shell/patch_trusted_hosts.sh @@ -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 diff --git a/roles/web-app-matomo/tasks/05_patch_database.yml b/roles/web-app-matomo/tasks/05_patch_database.yml index 1099f50aef..41e4568752 100644 --- a/roles/web-app-matomo/tasks/05_patch_database.yml +++ b/roles/web-app-matomo/tasks/05_patch_database.yml @@ -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 }}" diff --git a/roles/web-app-matomo/tasks/06_patch_trusted_hosts.yml b/roles/web-app-matomo/tasks/06_patch_trusted_hosts.yml index ec5c8eb1b5..75578e23e6 100644 --- a/roles/web-app-matomo/tasks/06_patch_trusted_hosts.yml +++ b/roles/web-app-matomo/tasks/06_patch_trusted_hosts.yml @@ -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