Repository navigation
jpa/MPG-1254-1283-1531 : initiate.sh rewritten + add checksums + ship logs no matter what #2625
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jpargudo
wants to merge
28
commits into
develop
Choose a base branch
from
jpa/mpg-1253-enhance_pg_upgrade_scripts
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+198
−43
Open
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
3188ce3
https://linear.app/supabase/issue/MPG-1283/harden-a-bit-preparesh
jpargudo 44b4ce6
https://linear.app/supabase/issue/MPG-1283/harden-a-bit-preparesh
jpargudo c073699
feat: adds checksums on SOURCE PG cluster gated to size <20Gb (max 10…
jpargudo 147e9ef
Replacing "fuser" (not disponible on AMIs) by "ls -l /proc/.."
jpargudo 9f73c93
Merge branch 'develop' into jpa/mpg-1253-enhance_pg_upgrade_scripts
jpargudo 550b969
Trying to pass nix builds
jpargudo 11be969
Merge branch 'develop' into jpa/mpg-1253-enhance_pg_upgrade_scripts
jpargudo a162cb6
Update ansible/files/admin_api_scripts/pg_upgrade_scripts/initiate.sh
jpargudo 1141fd7
Update ansible/files/admin_api_scripts/pg_upgrade_scripts/prepare.sh
jpargudo 9fe2788
Update ansible/files/admin_api_scripts/pg_upgrade_scripts/prepare.sh
jpargudo 2dd9c01
Update ansible/files/admin_api_scripts/pg_upgrade_scripts/prepare.sh
jpargudo ba64169
prepare.sh and initiate.sh reviewed with Marton's comments
jpargudo 9bafb6d
initiate.sh : remove useless comments
jpargudo 33e5293
Merge branch 'develop' into jpa/mpg-1253-enhance_pg_upgrade_scripts
jpargudo 03549bb
initiate.sh: remove unnecessary 2nd argument to ship_logs (its hardco…
jpargudo 06dad44
Merge branch 'develop' into jpa/mpg-1253-enhance_pg_upgrade_scripts
jpargudo a9270b4
Apply suggestion from @brainrake
jpargudo b8048a0
Apply suggestion from @brainrake
jpargudo 505dca8
Apply suggestion from @mmlb
jpargudo e58bacb
Apply suggestion from @mmlb
jpargudo 4944edb
Apply suggestion from @brainrake
jpargudo 2793c58
Apply suggestion from @brainrake
jpargudo 9fa8256
Apply suggestion from @brainrake
jpargudo 9d238b7
Apply batched suggestions from code review
jpargudo 0559285
Merge branch 'develop' into jpa/mpg-1253-enhance_pg_upgrade_scripts
jpargudo f6723aa
Merge branch 'develop' into jpa/mpg-1253-enhance_pg_upgrade_scripts
jpargudo 0fd2701
Apply suggestion from @brainrake
jpargudo bb81eef
Apply suggestion from @brainrake
jpargudo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 95 additions & 10 deletions
105
ansible/files/admin_api_scripts/pg_upgrade_scripts/prepare.sh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,100 @@ | ||
| #! /usr/bin/env bash | ||
| ## This script is runs in advance of the database version upgrade, on the newly | ||
| ## launched instance which will eventually be promoted to become the primary | ||
| ## database instance once the upgrade successfully completes, terminating the | ||
| ## previous (source) instance. | ||
| ## The following commands safely stop the Postgres service and unmount | ||
| ## the data disk off the newly launched instance, to be re-attached to the | ||
| ## source instance and run the upgrade there. | ||
|
|
||
| set -euo pipefail | ||
| # This script runs in advance of the database version upgrade, on the newly | ||
| # launched instance which will eventually be promoted to become the primary | ||
| # database instance once the upgrade successfully completes, terminating the | ||
| # previous (source) instance. The following commands safely stop the Postgres | ||
| # service and unmount the data disk off the newly launched instance, to be | ||
| # re-attached to the source instance and run the upgrade there. | ||
|
|
||
| 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-prepare.log" | ||
| touch "$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. | ||
| 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 "prepare.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" || { | ||
| log "cannot source $SCRIPT_DIR/common.sh" | ||
| exit 1 | ||
| } | ||
|
|
||
| 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)." | ||
| exit "$rc" | ||
| } | ||
| trap on_exit EXIT | ||
|
|
||
| log "prepare.sh started" | ||
|
|
||
| # check that /data is actually mounted | ||
| mountpoint -q /data || { | ||
| log "/data is not mounted" | ||
| exit 1 | ||
| } | ||
|
|
||
| # stop postgresql: "systemctl stop blocks until the unit has fully stopped" | ||
| # so there is no need to check pg_isready afterwards... | ||
| systemctl stop postgresql | ||
|
|
||
| cp /etc/postgresql-custom/pgsodium_root.key /data/pgsodium_root.key | ||
| umount /data | ||
| # ...but let's still verify that the service is explicitly stopped | ||
| if systemctl is-active --quiet postgresql; then | ||
| log "postgresql service is still active!" | ||
| exit 1 | ||
| fi | ||
|
|
||
| # ...and that there are no remaining postgres processes | ||
| if pgrep -x postgres >/dev/null; then | ||
| log "postgres processes are still alive!" | ||
| exit 1 | ||
| 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 | ||
|
|
||
| # flush cached writes to persistent storage | ||
| sync | ||
|
|
||
| # Unmount /data. Capture the native umount error message and route it through | ||
| # "log" so that it is timestamped like the rest of the log entries. | ||
| if ! umount_out=$(umount /data 2>&1); then | ||
| log "Unable to unmount /data: $umount_out" | ||
| # List the processes whose cwd is under /data. | ||
| log "Processes using /data:" | ||
|
|
||
| # activate nullglob to avoid errors if /proc is not accessible | ||
| shopt -s nullglob | ||
|
|
||
| QUOTING_STYLE=literal stat -c '%U %N' /proc/[0-9]*/cwd 2>/dev/null | | ||
| sed -nE 's|^(\S+) /proc/([0-9]+)/cwd -> /data(/.*)?$|\1 \2|p' | | ||
| while read -r user pid; do | ||
| comm=$(cat "/proc/$pid/comm" 2>/dev/null) || continue | ||
| log "user: ${user} pid: ${pid} command: ${comm}" | ||
| done || true | ||
|
|
||
| exit 1 | ||
| fi | ||
|
|
||
| # Reaching this point means success: the final message and the log upload | ||
| # are handled by the EXIT trap (on_exit). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.