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
18
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.
Open
Changes from all commits
Commits
Show all changes
18 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 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
140 changes: 130 additions & 10 deletions
140
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,135 @@ | ||
| #! /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") | ||
|
|
||
| # The log file is never truncated: when the script is retried by the caller, | ||
| # each attempt appends to the same file. It is created right away so that it | ||
| # exists even if a later step fails. | ||
| 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 | ||
| } | ||
|
|
||
| # ship_logs reads these variables, and "set -u" would abort the script on an | ||
| # unset variable. Default them to empty (existing values are kept) so that | ||
| # ship_logs can handle the "no reporting key" case itself. | ||
| : "${REPORTING_ANON_KEY:=}" | ||
| : "${REPORTING_PROJECT_REF:=}" | ||
|
|
||
| # Runs on every exit (success, explicit "exit 1" or set -e failure). | ||
| # - success: final message + single upload of the log file | ||
| # - failure: nothing more is written (the error is already in the log) and | ||
| # nothing is uploaded: the script cannot know whether the caller will | ||
| # retry, so the caller handles the "gave up" case. | ||
| # The script's exit code is always preserved for the caller. | ||
| on_exit() { | ||
| local rc=$? | ||
| trap - EXIT ERR # avoid re-entering the traps from inside this handler | ||
|
|
||
| if [ "$rc" -eq 0 ]; then | ||
| log "prepare.sh ended with no errors." | ||
| ship_logs "$LOG_FILE" || log "Log upload failed (non-blocking)." | ||
| else | ||
| ship_logs "$LOG_FILE" || log "Log upload failed (non-blocking)." | ||
| fi | ||
| exit "$rc" | ||
| } | ||
| trap on_exit EXIT | ||
|
|
||
| # Written at every attempt. It is also the marker used above to detect | ||
| # retries, so do not change its wording. | ||
| 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 still using /data ("|| true" so a fuser failure | ||
| # does not trigger the ERR trap and mask the real cause) | ||
| log "Processes using /data:" | ||
|
|
||
| # activate nullglob to avoid errors if /proc is not accessible | ||
| shopt -s nullglob | ||
|
|
||
| # go thru /proc on numeric PIDs | ||
| for pid_dir in /proc/[0-9]*/; do | ||
|
|
||
| # extract pid from dir name | ||
| pid="${pid_dir#/proc/}" | ||
| pid="${pid%/}" | ||
|
|
||
| # secure read from symbolink link cwd | ||
| if cwd=$(readlink -f "$pid_dir/cwd" 2>/dev/null); then | ||
| if [[ $cwd == "/data" || $cwd == "/data"/* ]]; then | ||
| # get user and name of the current command | ||
| stat_info=$(stat -c "%U" "$pid_dir" 2>/dev/null || true) | ||
| cmd_info=$(cat "$pid_dir/comm" 2>/dev/null || true) | ||
|
|
||
| if [[ -n $stat_info && -n $cmd_info ]]; then | ||
| log "user: ${stat_info} pid: ${pid} command: ${cmd_info}" | ||
| fi | ||
| fi | ||
| fi | ||
| done | ||
|
|
||
| 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.