Skip to content

Commit 7610c6a

Browse files
committed
chore: move out of tmp
1 parent ba5db1c commit 7610c6a

File tree

4 files changed

+103
-61
lines changed

4 files changed

+103
-61
lines changed

ansible/files/admin_api_scripts/pg_upgrade_scripts/check.sh

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
#! /usr/bin/env bash
22
## This script provides a method to check the status of the database upgrade
3-
## process, which is updated in /tmp/pg-upgrade-status by initiate.sh
3+
## process, which is updated in /root/pg_upgrade/status by initiate.sh
44
## This runs on the old (source) instance.
55

66
set -euo pipefail
77

8-
STATUS_FILE="/tmp/pg-upgrade-status"
8+
SCRIPT_DIR=$(dirname -- "$0")
9+
# shellcheck disable=SC1091
10+
source "$SCRIPT_DIR/common.sh"
11+
12+
STATUS_FILE="/root/pg_upgrade/status"
913

1014
if [ -f "${STATUS_FILE}" ]; then
1115
STATUS=$(cat "${STATUS_FILE}")
1216
echo -n "${STATUS}"
1317
else
1418
echo -n "unknown"
1519
fi
16-

ansible/files/admin_api_scripts/pg_upgrade_scripts/common.sh

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
REPORTING_PROJECT_REF="ihmaxnjpcccasmrbkpvo"
66
REPORTING_CREDENTIALS_FILE="/root/upgrade-reporting-credentials"
77

8+
UPGRADE_STATUS_FILE="/root/pg_upgrade/status"
9+
810
REPORTING_ANON_KEY=""
911
if [ -f "$REPORTING_CREDENTIALS_FILE" ]; then
1012
REPORTING_ANON_KEY=$(cat "$REPORTING_CREDENTIALS_FILE")
@@ -24,6 +26,11 @@ function ship_logs {
2426
return 0
2527
fi
2628

29+
if [ ! -f "$UPGRADE_STATUS_FILE" ]; then
30+
echo "No upgrade status file found. Skipping log upload."
31+
return 0
32+
fi
33+
2734
if [ ! -f "$LOG_FILE" ]; then
2835
echo "No log file found. Skipping log upload."
2936
return 0
@@ -36,32 +43,47 @@ function ship_logs {
3643

3744
HOSTNAME=$(hostname)
3845
DERIVED_REF="${HOSTNAME##*-}"
46+
STATUS=$(cat "$UPGRADE_STATUS_FILE")
3947

40-
printf -v BODY '{ "ref": "%s", "step": "%s", "content": %s }' "$DERIVED_REF" "completion" "$(cat "$LOG_FILE" | jq -Rs '.')"
48+
printf -v BODY '{ "ref": "%s", "step": "%s", "status": "%", "content": %s }' "$DERIVED_REF" "completion" "$STATUS" "$(cat "$LOG_FILE" | jq -Rs '.')"
4149
curl -sf -X POST "https://$REPORTING_PROJECT_REF.supabase.co/rest/v1/error_logs" \
42-
-H "apikey: ${REPORTING_ANON_KEY}" \
43-
-H 'Content-type: application/json' \
44-
-d "$BODY"
50+
-H "apikey: ${REPORTING_ANON_KEY}" \
51+
-H 'Content-type: application/json' \
52+
-d "$BODY"
4553
}
4654

55+
function create_pgupgrade_files_dir {
56+
if [ ! -d /root/pg_upgrade ]; then
57+
mkdir -p /root/pg_upgrade
58+
chown postgres:postgres /root/pg_upgrade
59+
fi
60+
}
61+
62+
function report_upgrade_status {
63+
create_pgupgrade_files_dir
64+
65+
STATUS=$1
66+
echo "$STATUS" > "$UPGRADE_STATUS_FILE"
67+
}
68+
4769
function retry {
48-
local retries=$1
49-
shift
50-
51-
local count=0
52-
until "$@"; do
53-
exit=$?
54-
wait=$((2 ** (count + 1)))
55-
count=$((count + 1))
56-
if [ $count -lt "$retries" ]; then
57-
echo "Command $* exited with code $exit, retrying..."
58-
sleep $wait
59-
else
60-
echo "Command $* exited with code $exit, no more retries left."
61-
return $exit
62-
fi
63-
done
64-
return 0
70+
local retries=$1
71+
shift
72+
73+
local count=0
74+
until "$@"; do
75+
exit=$?
76+
wait=$((2 ** (count + 1)))
77+
count=$((count + 1))
78+
if [ $count -lt "$retries" ]; then
79+
echo "Command $* exited with code $exit, retrying..."
80+
sleep $wait
81+
else
82+
echo "Command $* exited with code $exit, no more retries left."
83+
return $exit
84+
fi
85+
done
86+
return 0
6587
}
6688

6789
CI_stop_postgres() {

ansible/files/admin_api_scripts/pg_upgrade_scripts/complete.sh

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ function cleanup {
1818
UPGRADE_STATUS=${1:-"failed"}
1919
EXIT_CODE=${?:-0}
2020

21-
echo "$UPGRADE_STATUS" > /tmp/pg-upgrade-status
21+
report_upgrade_status "$UPGRADE_STATUS"
2222

2323
ship_logs "$LOG_FILE" || true
2424

@@ -81,12 +81,12 @@ EOF
8181
}
8282

8383
function complete_pg_upgrade {
84-
if [ -f /tmp/pg-upgrade-status ]; then
84+
if [ -f "$UPGRADE_STATUS_FILE" ]; then
8585
echo "Upgrade job already started. Bailing."
8686
exit 0
8787
fi
8888

89-
echo "running" > /tmp/pg-upgrade-status
89+
report_upgrade_status "running"
9090

9191
echo "1. Mounting data disk"
9292
if [ -z "$IS_CI" ]; then
@@ -169,7 +169,7 @@ function apply_auth_scheme_updates {
169169
}
170170

171171
function start_vacuum_analyze {
172-
echo "complete" > /tmp/pg-upgrade-status
172+
report_upgrade_status "complete"
173173
if ! command -v nix &> /dev/null; then
174174
su -c 'vacuumdb --all --analyze-in-stages' -s "$SHELL" postgres
175175
else
@@ -184,12 +184,14 @@ echo "C.UTF-8 UTF-8" > /etc/locale.gen
184184
echo "en_US.UTF-8 UTF-8" >> /etc/locale.gen
185185
locale-gen
186186

187+
create_pgupgrade_files_dir
188+
187189
if [ -z "$IS_CI" ]; then
188190
complete_pg_upgrade >> $LOG_FILE 2>&1 &
189191
else
190192
CI_stop_postgres || true
191193

192-
rm -f /tmp/pg-upgrade-status
194+
rm -f "$UPGRADE_STATUS_FILE"
193195
mv /data_migration /data
194196

195197
rm -rf /var/lib/postgresql/data

ansible/files/admin_api_scripts/pg_upgrade_scripts/initiate.sh

Lines changed: 47 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
## This script is run on the old (source) instance, mounting the data disk
44
## of the newly launched instance, disabling extensions containing regtypes,
55
## and running pg_upgrade.
6-
## It reports the current status of the upgrade process to /tmp/pg-upgrade-status,
6+
## It reports the current status of the upgrade process to /root/pg_upgrade/status,
77
## which can then be subsequently checked through check.sh.
88

99
# Extensions to disable before running pg_upgrade.
@@ -37,7 +37,11 @@ PGVERSION=$1
3737
MOUNT_POINT="/data_migration"
3838
LOG_FILE="/var/log/pg-upgrade-initiate.log"
3939

40-
POST_UPGRADE_EXTENSION_SCRIPT="/tmp/pg_upgrade/pg_upgrade_extensions.sql"
40+
create_pgupgrade_files_dir
41+
PG_UPGRADE_DIR="/root/pg_upgrade/$(date +%s)"
42+
43+
POST_UPGRADE_EXTENSION_SCRIPT="${PG_UPGRADE_DIR}/pg_upgrade_extensions.sql"
44+
4145
OLD_PGVERSION=$(run_sql -A -t -c "SHOW server_version;")
4246

4347
SERVER_LC_COLLATE=$(run_sql -A -t -c "SHOW lc_collate;")
@@ -48,7 +52,7 @@ POSTGRES_CONFIG_PATH="/etc/postgresql/postgresql.conf"
4852
PGBINOLD="/usr/lib/postgresql/bin"
4953
PGLIBOLD="/usr/lib/postgresql/lib"
5054

51-
PG_UPGRADE_BIN_DIR="/tmp/pg_upgrade_bin/$PGVERSION"
55+
PG_UPGRADE_BIN_DIR="${PG_UPGRADE_DIR}/${PGVERSION}"
5256
NIX_INSTALLER_PATH="/tmp/persistent/nix-installer"
5357
NIX_INSTALLER_PACKAGE_PATH="$NIX_INSTALLER_PATH.tar.gz"
5458

@@ -131,7 +135,7 @@ cleanup() {
131135
echo "Unmounting data disk from ${MOUNT_POINT}"
132136
umount $MOUNT_POINT
133137
fi
134-
echo "$UPGRADE_STATUS" > /tmp/pg-upgrade-status
138+
report_upgrade_status "$UPGRADE_STATUS"
135139

136140
if [ -z "$IS_CI" ]; then
137141
exit "$EXIT_CODE"
@@ -227,6 +231,30 @@ function patch_wrappers {
227231
fi
228232
}
229233

234+
function mount_data_migration_disk {
235+
if [ -z "$IS_CI" ] && [ -z "$IS_LOCAL_UPGRADE" ]; then
236+
# awk NF==3 prints lines with exactly 3 fields, which are the block devices currently not mounted anywhere
237+
# excluding nvme0 since it is the root disk
238+
echo "5. Determining block device to mount"
239+
BLOCK_DEVICE=$(lsblk -dprno name,size,mountpoint,type | grep "disk" | grep -v "nvme0" | awk 'NF==3 { print $1; }')
240+
echo "Block device found: $BLOCK_DEVICE"
241+
242+
mkdir -p "$MOUNT_POINT"
243+
echo "6. Mounting block device"
244+
245+
sleep 5
246+
e2fsck -pf "$BLOCK_DEVICE"
247+
248+
sleep 1
249+
mount "$BLOCK_DEVICE" "$MOUNT_POINT"
250+
251+
sleep 1
252+
resize2fs "$BLOCK_DEVICE"
253+
else
254+
mkdir -p "$MOUNT_POINT"
255+
fi
256+
}
257+
230258
function initiate_upgrade {
231259
mkdir -p "$MOUNT_POINT"
232260
SHARED_PRELOAD_LIBRARIES=$(cat "$POSTGRES_CONFIG_PATH" | grep shared_preload_libraries | sed "s/shared_preload_libraries =\s\{0,1\}'\(.*\)'.*/\1/")
@@ -266,13 +294,17 @@ function initiate_upgrade {
266294
mkdir -p "$PG_UPGRADE_BIN_DIR"
267295
mkdir -p /tmp/persistent/
268296
echo "a7189a68ed4ea78c1e73991b5f271043636cf074" > "$PG_UPGRADE_BIN_DIR/nix_flake_version"
269-
tar -czf "/tmp/persistent/pg_upgrade_bin.tar.gz" -C "/tmp/pg_upgrade_bin" .
297+
tar -czf "/tmp/persistent/pg_upgrade_bin.tar.gz" -C "$PG_UPGRADE_DIR" .
270298
rm -rf /tmp/pg_upgrade_bin/
271299
fi
272300

273301
echo "1. Extracting pg_upgrade binaries"
274-
mkdir -p "/tmp/pg_upgrade_bin"
275-
tar zxf "/tmp/persistent/pg_upgrade_bin.tar.gz" -C "/tmp/pg_upgrade_bin"
302+
mkdir -p "$PG_UPGRADE_DIR"
303+
304+
# upgrade job outputs a log in the cwd; needs write permissions
305+
chown postgres:postgres "$PG_UPGRADE_DIR"
306+
307+
tar zxf "/tmp/persistent/pg_upgrade_bin.tar.gz" -C "$PG_UPGRADE_DIR"
276308

277309
PGSHARENEW="$PG_UPGRADE_BIN_DIR/share"
278310

@@ -289,7 +321,10 @@ function initiate_upgrade {
289321
echo "1.1.1. Installing Nix using the provided installer"
290322
tar -xzf "$NIX_INSTALLER_PACKAGE_PATH" -C /tmp/persistent/
291323
chmod +x "$NIX_INSTALLER_PATH"
292-
"$NIX_INSTALLER_PATH" install --no-confirm
324+
325+
"$NIX_INSTALLER_PATH" install --no-confirm \
326+
--extra-conf "substituters = https://cache.nixos.org https://nix-postgres-artifacts.s3.amazonaws.com" \
327+
--extra-conf "trusted-public-keys = nix-postgres-artifacts:dGZlQOvKcNEjvT7QEAJbcV6b6uk7VF/hWMjhYleiaLI=% cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY="
293328
else
294329
echo "1.1.1. Installing Nix using the official installer"
295330

@@ -323,7 +358,7 @@ function initiate_upgrade {
323358
chown postgres:postgres "/var/lib/postgresql/extension/pgsodium_getkey"
324359
fi
325360

326-
chown -R postgres:postgres "/tmp/pg_upgrade_bin/$PGVERSION"
361+
chown -R postgres:postgres "$PG_UPGRADE_BIN_DIR"
327362

328363
# upgrade job outputs a log in the cwd; needs write permissions
329364
mkdir -p /tmp/pg_upgrade/
@@ -350,27 +385,7 @@ function initiate_upgrade {
350385
fi
351386
locale-gen
352387

353-
if [ -z "$IS_CI" ] && [ -z "$IS_LOCAL_UPGRADE" ]; then
354-
# awk NF==3 prints lines with exactly 3 fields, which are the block devices currently not mounted anywhere
355-
# excluding nvme0 since it is the root disk
356-
echo "5. Determining block device to mount"
357-
BLOCK_DEVICE=$(lsblk -dprno name,size,mountpoint,type | grep "disk" | grep -v "nvme0" | awk 'NF==3 { print $1; }')
358-
echo "Block device found: $BLOCK_DEVICE"
359-
360-
mkdir -p "$MOUNT_POINT"
361-
echo "6. Mounting block device"
362-
363-
sleep 5
364-
e2fsck -pf "$BLOCK_DEVICE"
365-
366-
sleep 1
367-
mount "$BLOCK_DEVICE" "$MOUNT_POINT"
368-
369-
sleep 1
370-
resize2fs "$BLOCK_DEVICE"
371-
else
372-
mkdir -p "$MOUNT_POINT"
373-
fi
388+
retry 3 mount_data_migration_disk
374389

375390
if [ -f "$MOUNT_POINT/pgsodium_root.key" ]; then
376391
cp "$MOUNT_POINT/pgsodium_root.key" /etc/postgresql-custom/pgsodium_root.key
@@ -481,11 +496,11 @@ EOF
481496

482497
trap cleanup ERR
483498

484-
echo "running" > /tmp/pg-upgrade-status
499+
report_upgrade_status "running"
485500
if [ -z "$IS_CI" ] && [ -z "$IS_LOCAL_UPGRADE" ]; then
486501
initiate_upgrade >> "$LOG_FILE" 2>&1 &
487502
echo "Upgrade initiate job completed"
488503
else
489-
rm -f /tmp/pg-upgrade-status
504+
rm -f "$UPGRADE_STATUS_FILE"
490505
initiate_upgrade
491506
fi

0 commit comments

Comments
 (0)