From 26be72393f793c29fdad31211e8e915f01dc4c80 Mon Sep 17 00:00:00 2001 From: Jean-Paul Argudo Date: Mon, 23 Nov 2026 13:39:40 +0100 Subject: [PATCH 1/8] Use /root/upgrade-reporting-credentials from TARGET on SOURCE to ensure the ANON key it holds is up-to-date --- .../pg_upgrade_scripts/initiate.sh | 57 ++++++++++++++++++- .../pg_upgrade_scripts/prepare.sh | 8 ++- 2 files changed, 60 insertions(+), 5 deletions(-) diff --git a/ansible/files/admin_api_scripts/pg_upgrade_scripts/initiate.sh b/ansible/files/admin_api_scripts/pg_upgrade_scripts/initiate.sh index 4914c0a98d..f9fb223fec 100755 --- a/ansible/files/admin_api_scripts/pg_upgrade_scripts/initiate.sh +++ b/ansible/files/admin_api_scripts/pg_upgrade_scripts/initiate.sh @@ -29,8 +29,30 @@ PG13_EXTENSIONS_TO_DISABLE=( set -eEuo pipefail SCRIPT_DIR=$(dirname -- "$0") + +# Each attempt overwrites the log. Failed attempts are shipped before the retry. +LOG_FILE="/var/log/pg-upgrade-initiate.log" +touch "$LOG_FILE" +chmod 644 "$LOG_FILE" + +# From here on, everything written to stdout/stderr (log messages, but also +# errors from systemctl, install, sync, etc.) is written to the log file. +exec >"$LOG_FILE" 2>&1 + +# Minimal fallback so that "log" exists even if common.sh cannot be sourced. +# common.sh redefines it with the real implementation. +log() { echo "$(date -u '+%Y-%m-%d %H:%M:%S UTC') $*"; } + +# Log any unexpected failure (set -e) with the line number and failing command, +# so that no error can end the script silently. +trap 'log "initiate.sh FAILED at line $LINENO: \"$BASH_COMMAND\" (exit code $?)"' ERR + +# Source the shared helpers (log and ship_logs functions) # shellcheck disable=SC1091 -source "$SCRIPT_DIR/common.sh" +source "$SCRIPT_DIR/common.sh" || { + log "cannot source $SCRIPT_DIR/common.sh" + exit 1 +} IS_CI=${IS_CI:-} IS_LOCAL_UPGRADE=${IS_LOCAL_UPGRADE:-} @@ -40,7 +62,6 @@ IS_NIX_BASED_SYSTEM="false" PGVERSION=$1 POSTGRES_SHARE_DIR="/usr/share/postgresql/${PGVERSION}" MOUNT_POINT="/data_migration" -LOG_FILE="/var/log/pg-upgrade-initiate.log" POST_UPGRADE_EXTENSION_SCRIPT="/tmp/pg_upgrade/pg_upgrade_extensions.sql" POST_UPGRADE_POSTGRES_PERMS_SCRIPT="/tmp/pg_upgrade/pg_upgrade_postgres_perms.sql" @@ -220,6 +241,8 @@ on_exit() { fi unblock_postgres_start || log "WARNING: failed to lift the postgres start block; check /run/systemd/system/postgresql.service.d" + log "initiate.sh ended" + exit "$exit_code" } @@ -560,12 +583,39 @@ EXTRA_NIX_CONF mkdir -p "$MOUNT_POINT" fi + # in prepare.sh script we copy 2 files in the /data of the TARGET + # to somehow 'ship them' here on the SOURCE + # pgsodium_root.key to be used with the pgsodium extension + # upgrade-reporting-credentials is the ANON jwt key used to ship_logs + + # get the pgsodium_root.key from the TARGET + # and copy it on the SOURCE (local) + if [ -f "$MOUNT_POINT/pgsodium_root.key" ]; then cp "$MOUNT_POINT/pgsodium_root.key" /etc/postgresql-custom/pgsodium_root.key chown postgres:postgres /etc/postgresql-custom/pgsodium_root.key chmod 640 /etc/postgresql-custom/pgsodium_root.key fi + # get the upgrade-reporting-credentials.target from the TARGET + # and move it on the SOURCE (local) only if one is existing prior + # if not, delete it from the MOUNT_POINT + + if [ -f "$MOUNT_POINT/upgrade-reporting-credentials.target" ]; then + if [ -f /root/upgrade-reporting-credentials ]; then + + # the fresh ANON key will replace a potentially old and unvalid one + # preventing the logs to be shipped (curl post would then fail) + + mv /root/upgrade-reporting-credentials /root/upgrade-reporting-credentials.bkp + mv "$MOUNT_POINT/upgrade-reporting-credentials.target" /root/upgrade-reporting-credentials + chown root:root /root/upgrade-reporting-credentials + chmod 600 /root/upgrade-reporting-credentials + else + rm $MOUNT_POINT/upgrade-reporting-credentials.target + fi + fi + log "7. Disabling extensions and generating post-upgrade script" handle_extensions @@ -698,7 +748,7 @@ EOF else - log "9.1. did *not* add checksum to SOURCE PG: size $PGDATAOLD_SIZE > 20 Gb!" + log "9.1. did *not* add checksums to SOURCE PG: size $PGDATAOLD_SIZE > 20 Gb!" fi @@ -768,6 +818,7 @@ trap cleanup ERR echo "running" >/tmp/pg-upgrade-status if [ -z "$IS_CI" ] && [ -z "$IS_LOCAL_UPGRADE" ]; then + log "initiate.sh started" initiate_upgrade >>"$LOG_FILE" 2>&1 & log "Upgrade initiate job completed" else diff --git a/ansible/files/admin_api_scripts/pg_upgrade_scripts/prepare.sh b/ansible/files/admin_api_scripts/pg_upgrade_scripts/prepare.sh index 87f16c2a9d..3d30c5d609 100755 --- a/ansible/files/admin_api_scripts/pg_upgrade_scripts/prepare.sh +++ b/ansible/files/admin_api_scripts/pg_upgrade_scripts/prepare.sh @@ -14,7 +14,7 @@ SCRIPT_DIR=$(dirname -- "$0") # Each attempt overwrites the log. Failed attempts are shipped before the retry. LOG_FILE="/var/log/pg-upgrade-prepare.log" touch "$LOG_FILE" -chmod 640 "$LOG_FILE" +chmod 644 "$LOG_FILE" # From here on, everything written to stdout/stderr (log messages, but also # errors from systemctl, install, sync, etc.) is written to the log file. @@ -39,7 +39,7 @@ on_exit() { local rc=$? trap - EXIT ERR - [ "$rc" -eq 0 ] && log "prepare.sh ended with no errors." + [ "$rc" -eq 0 ] && log "prepare.sh ended with no errors" ship_logs "$LOG_FILE" || log "Log upload failed (non-blocking)." exit "$rc" } @@ -73,6 +73,10 @@ fi install -m 600 -o postgres -g postgres \ /etc/postgresql-custom/pgsodium_root.key /data/pgsodium_root.key +# copy the anon key used to ship logs to the error_logs project +install -m 600 -o root -g root \ + /root/upgrade-reporting-credentials /data/upgrade-reporting-credentials.target + # flush cached writes to persistent storage sync From 4cb4c41cbcf302a965a2607e8f34bafecb9f2395 Mon Sep 17 00:00:00 2001 From: Jean-Paul Argudo Date: Mon, 23 Nov 2026 17:27:13 +0100 Subject: [PATCH 2/8] Better and more log outputs, removing the exceeding trap ERR --- .../pg_upgrade_scripts/initiate.sh | 25 +++++++++++-------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/ansible/files/admin_api_scripts/pg_upgrade_scripts/initiate.sh b/ansible/files/admin_api_scripts/pg_upgrade_scripts/initiate.sh index f9fb223fec..66b4d54a26 100755 --- a/ansible/files/admin_api_scripts/pg_upgrade_scripts/initiate.sh +++ b/ansible/files/admin_api_scripts/pg_upgrade_scripts/initiate.sh @@ -43,10 +43,6 @@ exec >"$LOG_FILE" 2>&1 # common.sh redefines it with the real implementation. log() { echo "$(date -u '+%Y-%m-%d %H:%M:%S UTC') $*"; } -# Log any unexpected failure (set -e) with the line number and failing command, -# so that no error can end the script silently. -trap 'log "initiate.sh FAILED at line $LINENO: \"$BASH_COMMAND\" (exit code $?)"' ERR - # Source the shared helpers (log and ship_logs functions) # shellcheck disable=SC1091 source "$SCRIPT_DIR/common.sh" || { @@ -154,16 +150,20 @@ cleanup() { # can fail under set -e, or the restart below would be refused. unblock_postgres_start || log "WARNING: failed to lift the postgres start block; check /run/systemd/system/postgresql.service.d" - # ship $LOG_FILE content to reporting project (see common.sh) - ship_logs "$LOG_FILE" || true - if [ -d "${MOUNT_POINT}/pgdata/pg_upgrade_output.d/" ]; then log "Copying pg_upgrade output to /var/log" cp -R "${MOUNT_POINT}/pgdata/pg_upgrade_output.d/" /var/log/ || true chown -R postgres:postgres /var/log/pg_upgrade_output.d/ chmod -R 0750 /var/log/pg_upgrade_output.d/ - tail -n +1 /var/log/pg_upgrade_output.d/*/* >/var/log/pg_upgrade_output.d/pg_upgrade.log || true - ship_logs "/var/log/pg_upgrade_output.d/pg_upgrade.log" || true + for file in /var/log/pg_upgrade_output.d/*/*; do + log "pg_upgrade_output file: $file" + if [ -f "$file" ]; then + cat "$file" >>"$LOG_FILE" 2>/dev/null || true + fi + done + + log "end of pg_upgrade outputs" + fi log "Restarting postgresql" @@ -213,6 +213,9 @@ EOF fi echo "$UPGRADE_STATUS" >/tmp/pg-upgrade-status + # ship $LOG_FILE content to reporting project (see common.sh) + ship_logs "$LOG_FILE" || true + if [ -z "$IS_CI" ]; then exit "$EXIT_CODE" else @@ -241,7 +244,8 @@ on_exit() { fi unblock_postgres_start || log "WARNING: failed to lift the postgres start block; check /run/systemd/system/postgresql.service.d" - log "initiate.sh ended" + # ship $LOG_FILE content to reporting project (see common.sh) + ship_logs "$LOG_FILE" || true exit "$exit_code" } @@ -820,7 +824,6 @@ echo "running" >/tmp/pg-upgrade-status if [ -z "$IS_CI" ] && [ -z "$IS_LOCAL_UPGRADE" ]; then log "initiate.sh started" initiate_upgrade >>"$LOG_FILE" 2>&1 & - log "Upgrade initiate job completed" else rm -f /tmp/pg-upgrade-status initiate_upgrade From 275ac0d6525f95388db8c9af22f1ff2a9d094f42 Mon Sep 17 00:00:00 2001 From: Jean-Paul Argudo Date: Tue, 24 Nov 2026 09:35:46 +0100 Subject: [PATCH 3/8] Refined when to ship log. Added bits in prepare.sh too --- .../pg_upgrade_scripts/initiate.sh | 11 ++--- .../pg_upgrade_scripts/prepare.sh | 44 ++++++++++++++++--- 2 files changed, 43 insertions(+), 12 deletions(-) diff --git a/ansible/files/admin_api_scripts/pg_upgrade_scripts/initiate.sh b/ansible/files/admin_api_scripts/pg_upgrade_scripts/initiate.sh index 66b4d54a26..5d26070d8d 100755 --- a/ansible/files/admin_api_scripts/pg_upgrade_scripts/initiate.sh +++ b/ansible/files/admin_api_scripts/pg_upgrade_scripts/initiate.sh @@ -156,9 +156,13 @@ cleanup() { chown -R postgres:postgres /var/log/pg_upgrade_output.d/ chmod -R 0750 /var/log/pg_upgrade_output.d/ for file in /var/log/pg_upgrade_output.d/*/*; do - log "pg_upgrade_output file: $file" if [ -f "$file" ]; then - cat "$file" >>"$LOG_FILE" 2>/dev/null || true + if [ -s "$file" ]; then + log "pg_upgrade_output file: $file (empty)" + else + log "pg_upgrade_output file: $file:" + cat "$file" >>"$LOG_FILE" 2>/dev/null || true + fi fi done @@ -213,9 +217,6 @@ EOF fi echo "$UPGRADE_STATUS" >/tmp/pg-upgrade-status - # ship $LOG_FILE content to reporting project (see common.sh) - ship_logs "$LOG_FILE" || true - if [ -z "$IS_CI" ]; then exit "$EXIT_CODE" else diff --git a/ansible/files/admin_api_scripts/pg_upgrade_scripts/prepare.sh b/ansible/files/admin_api_scripts/pg_upgrade_scripts/prepare.sh index 3d30c5d609..6a33489687 100755 --- a/ansible/files/admin_api_scripts/pg_upgrade_scripts/prepare.sh +++ b/ansible/files/admin_api_scripts/pg_upgrade_scripts/prepare.sh @@ -39,8 +39,15 @@ on_exit() { local rc=$? trap - EXIT ERR - [ "$rc" -eq 0 ] && log "prepare.sh ended with no errors" - ship_logs "$LOG_FILE" || log "Log upload failed (non-blocking)." + # ship the log + + if [ "$rc" -eq 0 ]; then + ship_logs "$LOG_FILE" || log "Log upload failed (non-blocking)." + log "prepare.sh ended with no errors" + else + log "prepare.sh ended with errors" + ship_logs "$LOG_FILE" || log "Log upload failed (non-blocking)." + fi exit "$rc" } trap on_exit EXIT @@ -70,12 +77,22 @@ if pgrep -x postgres >/dev/null; then fi # copy the pgsodium root key with proper owner and permissions -install -m 600 -o postgres -g postgres \ - /etc/postgresql-custom/pgsodium_root.key /data/pgsodium_root.key +# (optional: initiate.sh also treats it as optional) +if [ -f /etc/postgresql-custom/pgsodium_root.key ]; then + install -m 600 -o postgres -g postgres \ + /etc/postgresql-custom/pgsodium_root.key /data/pgsodium_root.key +else + log "WARNING: /etc/postgresql-custom/pgsodium_root.key not found; not copying it" +fi # copy the anon key used to ship logs to the error_logs project -install -m 600 -o root -g root \ - /root/upgrade-reporting-credentials /data/upgrade-reporting-credentials.target +# (optional: without it logs are simply not shipped, which must not block the upgrade) +if [ -f /root/upgrade-reporting-credentials ]; then + install -m 600 -o root -g root \ + /root/upgrade-reporting-credentials /data/upgrade-reporting-credentials.target +else + log "WARNING: /root/upgrade-reporting-credentials not found; not copying it (logs will not be shipped)" +fi # flush cached writes to persistent storage sync @@ -87,7 +104,20 @@ if ! umount_out=$(umount /data 2>&1); then # List the processes whose cwd is under /data. log "Processes using /data:" - # activate nullglob to avoid errors if /proc is not accessible + # Processes with open files (or any other use) on /data, not only those whose + # cwd is there. Best effort: the tools may be absent, never fail because of it. + if command -v fuser >/dev/null 2>&1; then + log "fuser -vm /data:" + fuser -vm /data 2>&1 || true + elif command -v lsof >/dev/null 2>&1; then + log "lsof +f -- /data:" + lsof +f -- /data 2>&1 || true + else + log "neither fuser nor lsof available; listing cwd only" + fi + + # nullglob: with no match, the /proc/[0-9]*/cwd pattern expands to nothing + # instead of being passed literally to stat shopt -s nullglob QUOTING_STYLE=literal stat -c '%U %N' /proc/[0-9]*/cwd 2>/dev/null | From 71841898cada96a386fff01adb3d7a6e1f8ebd4c Mon Sep 17 00:00:00 2001 From: Jean-Paul Argudo Date: Tue, 24 Nov 2026 12:12:11 +0100 Subject: [PATCH 4/8] Inverted order of log then ship_log!! --- ansible/files/admin_api_scripts/pg_upgrade_scripts/prepare.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ansible/files/admin_api_scripts/pg_upgrade_scripts/prepare.sh b/ansible/files/admin_api_scripts/pg_upgrade_scripts/prepare.sh index 6a33489687..da70e63519 100755 --- a/ansible/files/admin_api_scripts/pg_upgrade_scripts/prepare.sh +++ b/ansible/files/admin_api_scripts/pg_upgrade_scripts/prepare.sh @@ -42,8 +42,8 @@ on_exit() { # ship the log if [ "$rc" -eq 0 ]; then - ship_logs "$LOG_FILE" || log "Log upload failed (non-blocking)." log "prepare.sh ended with no errors" + ship_logs "$LOG_FILE" || log "Log upload failed (non-blocking)." else log "prepare.sh ended with errors" ship_logs "$LOG_FILE" || log "Log upload failed (non-blocking)." From c430c0ef5702cb34642c8b0fe64515a2dca0e802 Mon Sep 17 00:00:00 2001 From: Jean-Paul Argudo Date: Tue, 24 Nov 2026 15:08:47 +0100 Subject: [PATCH 5/8] pg_upgrade_output files now added to logs correctly --- .../pg_upgrade_scripts/initiate.sh | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/ansible/files/admin_api_scripts/pg_upgrade_scripts/initiate.sh b/ansible/files/admin_api_scripts/pg_upgrade_scripts/initiate.sh index 5d26070d8d..780d5ae6fd 100755 --- a/ansible/files/admin_api_scripts/pg_upgrade_scripts/initiate.sh +++ b/ansible/files/admin_api_scripts/pg_upgrade_scripts/initiate.sh @@ -153,15 +153,17 @@ cleanup() { if [ -d "${MOUNT_POINT}/pgdata/pg_upgrade_output.d/" ]; then log "Copying pg_upgrade output to /var/log" cp -R "${MOUNT_POINT}/pgdata/pg_upgrade_output.d/" /var/log/ || true - chown -R postgres:postgres /var/log/pg_upgrade_output.d/ - chmod -R 0750 /var/log/pg_upgrade_output.d/ - for file in /var/log/pg_upgrade_output.d/*/*; do + chown -R postgres:postgres /var/log/pg_upgrade_output.d/ || true + chmod -R 0750 /var/log/pg_upgrade_output.d/ || true + # Layout: pg_upgrade_output.d//{log,dump}/. Only log/ is + # appended (dump/ holds the schema dumps: large and not useful in the log). + for file in /var/log/pg_upgrade_output.d/*/log/*; do if [ -f "$file" ]; then if [ -s "$file" ]; then - log "pg_upgrade_output file: $file (empty)" - else log "pg_upgrade_output file: $file:" cat "$file" >>"$LOG_FILE" 2>/dev/null || true + else + log "pg_upgrade_output file: $file (empty)" fi fi done @@ -616,6 +618,13 @@ EXTRA_NIX_CONF mv "$MOUNT_POINT/upgrade-reporting-credentials.target" /root/upgrade-reporting-credentials chown root:root /root/upgrade-reporting-credentials chmod 600 /root/upgrade-reporting-credentials + + # reload the ANON key since it is loaded when common.sh is included + # at the start of this script + + # shellcheck disable=SC2034 + REPORTING_ANON_KEY=$(cat /root/upgrade-reporting-credentials) + else rm $MOUNT_POINT/upgrade-reporting-credentials.target fi From e442a7138d1bf75eb281363dcf4c23708dd98271 Mon Sep 17 00:00:00 2001 From: Jean-Paul Argudo Date: Tue, 24 Nov 2026 17:03:26 +0100 Subject: [PATCH 6/8] Backup any former failed pg_upgrade outputs. Only (send to) log the last ones --- .../pg_upgrade_scripts/initiate.sh | 60 ++++++++++++++++++- 1 file changed, 58 insertions(+), 2 deletions(-) diff --git a/ansible/files/admin_api_scripts/pg_upgrade_scripts/initiate.sh b/ansible/files/admin_api_scripts/pg_upgrade_scripts/initiate.sh index 780d5ae6fd..0290a6c592 100755 --- a/ansible/files/admin_api_scripts/pg_upgrade_scripts/initiate.sh +++ b/ansible/files/admin_api_scripts/pg_upgrade_scripts/initiate.sh @@ -152,18 +152,74 @@ cleanup() { if [ -d "${MOUNT_POINT}/pgdata/pg_upgrade_output.d/" ]; then log "Copying pg_upgrade output to /var/log" + + # Previous (failed) attempts may have left their timestamped directories in + # /var/log: archive them in pg_upgrade_output.d.previous// (never + # deleted) so that only this attempt's output is read below. + PG_UPGRADE_OUTPUT_DIR="/var/log/pg_upgrade_output.d" + PG_UPGRADE_PREVIOUS_DIR="${PG_UPGRADE_OUTPUT_DIR}.previous" + LAST_ARCHIVED_DIR="" + + if [ -d "$PG_UPGRADE_OUTPUT_DIR" ]; then + mkdir -p "$PG_UPGRADE_PREVIOUS_DIR" || true + + # The glob is sorted and the names are timestamps: the last directory + # archived is the most recent previous attempt. + for dir in "$PG_UPGRADE_OUTPUT_DIR"/*/; do + if [ -d "$dir" ]; then + dir="${dir%/}" + dest="$PG_UPGRADE_PREVIOUS_DIR/${dir##*/}" + # never nest into / overwrite an already archived directory + if [ -e "$dest" ]; then + dest="${dest}.$(date +%s)" + fi + if mv "$dir" "$dest"; then + LAST_ARCHIVED_DIR="$dest" + else + log "WARNING: failed to archive $dir to $dest" + fi + fi + done + + # *.txt files at the root of pg_upgrade_output.d come from a previous + # attempt: attach them to the most recent archived timestamp directory + # (or to the root of the archive if there is none). + for file in "$PG_UPGRADE_OUTPUT_DIR"/*.txt; do + if [ -f "$file" ]; then + mv "$file" "${LAST_ARCHIVED_DIR:-$PG_UPGRADE_PREVIOUS_DIR}/" || log "WARNING: failed to archive $file" + fi + done + fi + cp -R "${MOUNT_POINT}/pgdata/pg_upgrade_output.d/" /var/log/ || true chown -R postgres:postgres /var/log/pg_upgrade_output.d/ || true chmod -R 0750 /var/log/pg_upgrade_output.d/ || true + # Layout: pg_upgrade_output.d//{log,dump}/. Only log/ is # appended (dump/ holds the schema dumps: large and not useful in the log). + for file in /var/log/pg_upgrade_output.d/*/log/*; do if [ -f "$file" ]; then if [ -s "$file" ]; then - log "pg_upgrade_output file: $file:" + log "pg_upgrade_output log file: $file:" + cat "$file" >>"$LOG_FILE" 2>/dev/null || true + else + log "pg_upgrade_output log file: $file (empty)" + fi + fi + done + + # Layout: pg_upgrade_output.d//*txt. Special checks during + # pg_upgrade will create special TXT files at the root of this directory + # adding them since this is useful for debugging too + + for file in /var/log/pg_upgrade_output.d/*/*.txt; do + if [ -f "$file" ]; then + if [ -s "$file" ]; then + log "pg_upgrade_output report filename : $file:" cat "$file" >>"$LOG_FILE" 2>/dev/null || true else - log "pg_upgrade_output file: $file (empty)" + log "pg_upgrade_output report filename : $file (empty)" fi fi done From e7b40c21ecd8050e9bf86c8f2e5e0df2c00b5fd5 Mon Sep 17 00:00:00 2001 From: Jean-Paul Argudo Date: Thu, 26 Nov 2026 16:05:20 +0100 Subject: [PATCH 7/8] Fixes after @valigula comments (4 out of 6) --- .../pg_upgrade_scripts/initiate.sh | 110 +++++++++++------- .../pg_upgrade_scripts/prepare.sh | 20 ++-- 2 files changed, 80 insertions(+), 50 deletions(-) diff --git a/ansible/files/admin_api_scripts/pg_upgrade_scripts/initiate.sh b/ansible/files/admin_api_scripts/pg_upgrade_scripts/initiate.sh index 0290a6c592..eba5555acb 100755 --- a/ansible/files/admin_api_scripts/pg_upgrade_scripts/initiate.sh +++ b/ansible/files/admin_api_scripts/pg_upgrade_scripts/initiate.sh @@ -33,11 +33,24 @@ SCRIPT_DIR=$(dirname -- "$0") # Each attempt overwrites the log. Failed attempts are shipped before the retry. LOG_FILE="/var/log/pg-upgrade-initiate.log" touch "$LOG_FILE" -chmod 644 "$LOG_FILE" +chmod 640 "$LOG_FILE" + +IS_CI=${IS_CI:-} +IS_LOCAL_UPGRADE=${IS_LOCAL_UPGRADE:-} + +# Truncate once (each attempt overwrites the log), then always append so the +# foreground and the background job never fight over file offsets. +: >"$LOG_FILE" # From here on, everything written to stdout/stderr (log messages, but also # errors from systemctl, install, sync, etc.) is written to the log file. -exec >"$LOG_FILE" 2>&1 +if [ -z "$IS_CI" ] && [ -z "$IS_LOCAL_UPGRADE" ]; then + # Background (production) mode: nobody reads the console. + exec >>"$LOG_FILE" 2>&1 +else + # Foreground CI / local mode: keep diagnostics on the console as well. + exec > >(tee -a "$LOG_FILE") 2>&1 +fi # Minimal fallback so that "log" exists even if common.sh cannot be sourced. # common.sh redefines it with the real implementation. @@ -50,8 +63,6 @@ source "$SCRIPT_DIR/common.sh" || { exit 1 } -IS_CI=${IS_CI:-} -IS_LOCAL_UPGRADE=${IS_LOCAL_UPGRADE:-} IS_NIX_UPGRADE=${IS_NIX_UPGRADE:-} IS_NIX_BASED_SYSTEM="false" @@ -173,7 +184,8 @@ cleanup() { if [ -e "$dest" ]; then dest="${dest}.$(date +%s)" fi - if mv "$dir" "$dest"; then + # fall back to copy + remove if a plain mv fails + if mv "$dir" "$dest" 2>/dev/null || { cp -a "$dir" "$dest" && rm -rf "$dir"; }; then LAST_ARCHIVED_DIR="$dest" else log "WARNING: failed to archive $dir to $dest" @@ -183,14 +195,29 @@ cleanup() { # *.txt files at the root of pg_upgrade_output.d come from a previous # attempt: attach them to the most recent archived timestamp directory - # (or to the root of the archive if there is none). + # (or to a dedicated dated directory if there is none, never to the + # root of the archive). + if [ -z "$LAST_ARCHIVED_DIR" ]; then + LAST_ARCHIVED_DIR="$PG_UPGRADE_PREVIOUS_DIR/unknown-$(date +%s)" + mkdir -p "$LAST_ARCHIVED_DIR" || true + fi for file in "$PG_UPGRADE_OUTPUT_DIR"/*.txt; do if [ -f "$file" ]; then - mv "$file" "${LAST_ARCHIVED_DIR:-$PG_UPGRADE_PREVIOUS_DIR}/" || log "WARNING: failed to archive $file" + mv "$file" "$LAST_ARCHIVED_DIR/" || log "WARNING: failed to archive $file" fi done fi + # Names of this attempt's timestamp directories: only these are read below, + # even if an older directory could not be archived and is still in place. + CURRENT_ATTEMPT_DIRS=() + for dir in "${MOUNT_POINT}"/pgdata/pg_upgrade_output.d/*/; do + if [ -d "$dir" ]; then + dir="${dir%/}" + CURRENT_ATTEMPT_DIRS+=("${dir##*/}") + fi + done + cp -R "${MOUNT_POINT}/pgdata/pg_upgrade_output.d/" /var/log/ || true chown -R postgres:postgres /var/log/pg_upgrade_output.d/ || true chmod -R 0750 /var/log/pg_upgrade_output.d/ || true @@ -198,30 +225,34 @@ cleanup() { # Layout: pg_upgrade_output.d//{log,dump}/. Only log/ is # appended (dump/ holds the schema dumps: large and not useful in the log). - for file in /var/log/pg_upgrade_output.d/*/log/*; do - if [ -f "$file" ]; then - if [ -s "$file" ]; then - log "pg_upgrade_output log file: $file:" - cat "$file" >>"$LOG_FILE" 2>/dev/null || true - else - log "pg_upgrade_output log file: $file (empty)" + for name in "${CURRENT_ATTEMPT_DIRS[@]}"; do + for file in "/var/log/pg_upgrade_output.d/$name"/log/*; do + if [ -f "$file" ]; then + if [ -s "$file" ]; then + log "pg_upgrade_output log file: $file:" + cat "$file" >>"$LOG_FILE" 2>/dev/null || true + else + log "pg_upgrade_output log file: $file (empty)" + fi fi - fi + done done # Layout: pg_upgrade_output.d//*txt. Special checks during # pg_upgrade will create special TXT files at the root of this directory # adding them since this is useful for debugging too - for file in /var/log/pg_upgrade_output.d/*/*.txt; do - if [ -f "$file" ]; then - if [ -s "$file" ]; then - log "pg_upgrade_output report filename : $file:" - cat "$file" >>"$LOG_FILE" 2>/dev/null || true - else - log "pg_upgrade_output report filename : $file (empty)" + for name in "${CURRENT_ATTEMPT_DIRS[@]}"; do + for file in "/var/log/pg_upgrade_output.d/$name"/*.txt; do + if [ -f "$file" ]; then + if [ -s "$file" ]; then + log "pg_upgrade_output report filename : $file:" + cat "$file" >>"$LOG_FILE" 2>/dev/null || true + else + log "pg_upgrade_output report filename : $file (empty)" + fi fi - fi + done done log "end of pg_upgrade outputs" @@ -666,23 +697,24 @@ EXTRA_NIX_CONF if [ -f "$MOUNT_POINT/upgrade-reporting-credentials.target" ]; then if [ -f /root/upgrade-reporting-credentials ]; then - # the fresh ANON key will replace a potentially old and unvalid one - # preventing the logs to be shipped (curl post would then fail) - - mv /root/upgrade-reporting-credentials /root/upgrade-reporting-credentials.bkp - mv "$MOUNT_POINT/upgrade-reporting-credentials.target" /root/upgrade-reporting-credentials - chown root:root /root/upgrade-reporting-credentials - chmod 600 /root/upgrade-reporting-credentials - - # reload the ANON key since it is loaded when common.sh is included - # at the start of this script - - # shellcheck disable=SC2034 - REPORTING_ANON_KEY=$(cat /root/upgrade-reporting-credentials) - + # preventing the logs to be shipped (curl post would then fail). + # Optional feature: never fail the upgrade because of it. + if mv -f /root/upgrade-reporting-credentials /root/upgrade-reporting-credentials.bkp && + mv "$MOUNT_POINT/upgrade-reporting-credentials.target" /root/upgrade-reporting-credentials; then + chown root:root /root/upgrade-reporting-credentials || true + chmod 600 /root/upgrade-reporting-credentials || true + # shellcheck disable=SC2034 + REPORTING_ANON_KEY=$(cat /root/upgrade-reporting-credentials) || true + else + log "WARNING: could not install the new reporting credentials; keeping the previous ones" + # restore the old key if the first mv succeeded but the second failed + if [ ! -f /root/upgrade-reporting-credentials ] && [ -f /root/upgrade-reporting-credentials.bkp ]; then + mv /root/upgrade-reporting-credentials.bkp /root/upgrade-reporting-credentials || true + fi + fi else - rm $MOUNT_POINT/upgrade-reporting-credentials.target + rm -f "$MOUNT_POINT/upgrade-reporting-credentials.target" || true fi fi @@ -889,7 +921,7 @@ trap cleanup ERR echo "running" >/tmp/pg-upgrade-status if [ -z "$IS_CI" ] && [ -z "$IS_LOCAL_UPGRADE" ]; then log "initiate.sh started" - initiate_upgrade >>"$LOG_FILE" 2>&1 & + initiate_upgrade & else rm -f /tmp/pg-upgrade-status initiate_upgrade diff --git a/ansible/files/admin_api_scripts/pg_upgrade_scripts/prepare.sh b/ansible/files/admin_api_scripts/pg_upgrade_scripts/prepare.sh index da70e63519..ddfde27a5d 100755 --- a/ansible/files/admin_api_scripts/pg_upgrade_scripts/prepare.sh +++ b/ansible/files/admin_api_scripts/pg_upgrade_scripts/prepare.sh @@ -14,7 +14,7 @@ SCRIPT_DIR=$(dirname -- "$0") # Each attempt overwrites the log. Failed attempts are shipped before the retry. LOG_FILE="/var/log/pg-upgrade-prepare.log" touch "$LOG_FILE" -chmod 644 "$LOG_FILE" +chmod 640 "$LOG_FILE" # From here on, everything written to stdout/stderr (log messages, but also # errors from systemctl, install, sync, etc.) is written to the log file. @@ -43,11 +43,12 @@ on_exit() { if [ "$rc" -eq 0 ]; then log "prepare.sh ended with no errors" - ship_logs "$LOG_FILE" || log "Log upload failed (non-blocking)." else log "prepare.sh ended with errors" - ship_logs "$LOG_FILE" || log "Log upload failed (non-blocking)." fi + + ship_logs "$LOG_FILE" || log "Log upload failed (non-blocking)." + exit "$rc" } trap on_exit EXIT @@ -76,14 +77,11 @@ if pgrep -x postgres >/dev/null; then exit 1 fi -# copy the pgsodium root key with proper owner and permissions -# (optional: initiate.sh also treats it as optional) -if [ -f /etc/postgresql-custom/pgsodium_root.key ]; then - install -m 600 -o postgres -g postgres \ - /etc/postgresql-custom/pgsodium_root.key /data/pgsodium_root.key -else - log "WARNING: /etc/postgresql-custom/pgsodium_root.key not found; not copying it" -fi +# copy the pgsodium root key with proper owner and permissions not optional! +# If the key is absent the upgrade proceeds without it on the target, and +# pgsodium/Vault secrets become undecryptable. +install -m 600 -o postgres -g postgres \ + /etc/postgresql-custom/pgsodium_root.key /data/pgsodium_root.key # copy the anon key used to ship logs to the error_logs project # (optional: without it logs are simply not shipped, which must not block the upgrade) From 89adebe15f194778c346a8e564ba37796e4d37f8 Mon Sep 17 00:00:00 2001 From: Jean-Paul Argudo Date: Fri, 27 Nov 2026 11:55:52 +0100 Subject: [PATCH 8/8] Fixes after @valigula comments (6 out of 6) --- .../pg_upgrade_scripts/common.sh | 47 +++++++++++++++++-- .../pg_upgrade_scripts/initiate.sh | 19 ++------ 2 files changed, 47 insertions(+), 19 deletions(-) diff --git a/ansible/files/admin_api_scripts/pg_upgrade_scripts/common.sh b/ansible/files/admin_api_scripts/pg_upgrade_scripts/common.sh index 1cbe9f29df..e80578e292 100755 --- a/ansible/files/admin_api_scripts/pg_upgrade_scripts/common.sh +++ b/ansible/files/admin_api_scripts/pg_upgrade_scripts/common.sh @@ -6,9 +6,6 @@ #staging/prod REPORTING_PROJECT_REF="ihmaxnjpcccasmrbkpvo" -#localdev -#REPORTING_PROJECT_REF="ditigwgjchkdxjxijzza" - REPORTING_CREDENTIALS_FILE="/root/upgrade-reporting-credentials" REPORTING_ANON_KEY="" @@ -147,6 +144,48 @@ function conninfo_for_db { printf "dbname='%s'" "$d" } +# Append a pg_upgrade output file to a destination file (normally the script's +# log file, which ship_logs later POSTs in a single request). +# +# Usage: append_output_file