Skip to content

Commit 90f8953

Browse files
committed
feat: if file exists, run prestart version switch scripts
1 parent a101761 commit 90f8953

File tree

2 files changed

+176
-1
lines changed

2 files changed

+176
-1
lines changed

ansible/files/postgres_prestart.sh.j2

Lines changed: 95 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
#!/bin/bash
22

3+
set -x # Print commands
4+
5+
log() {
6+
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1"
7+
}
8+
39
check_orioledb_enabled() {
410
local pg_conf="/etc/postgresql/postgresql.conf"
511
if [ ! -f "$pg_conf" ]; then
@@ -26,7 +32,93 @@ update_orioledb_buffers() {
2632
fi
2733
}
2834

35+
check_extensions_file() {
36+
local extensions_file="/etc/adminapi/pg-extensions.json"
37+
if [ ! -f "$extensions_file" ]; then
38+
log "extensions: No extensions file found, skipping extensions versions check"
39+
return 1
40+
fi
41+
if [ ! -r "$extensions_file" ]; then
42+
log "extensions: Cannot read extensions file"
43+
return 1
44+
fi
45+
return 0
46+
}
47+
48+
switch_extension_version() {
49+
local extension_name="$1"
50+
local version="$2"
51+
52+
# Use BIN_PATH environment variable or default to /var/lib/postgresql/.nix-profile
53+
: ${BIN_PATH:="/var/lib/postgresql/.nix-profile"}
54+
55+
local switch_script="$BIN_PATH/bin/switch_${extension_name}_version"
56+
57+
if [ ! -x "$switch_script" ]; then
58+
log "$extension_name: No version switch script available at $switch_script, skipping"
59+
return 0
60+
fi
61+
62+
log "$extension_name: Switching to version $version"
63+
# Run directly as root since we're already running as root
64+
"$switch_script" "$version"
65+
local exit_code=$?
66+
if [ $exit_code -eq 0 ]; then
67+
log "$extension_name: Version switch completed successfully"
68+
else
69+
log "$extension_name: Version switch failed with exit code $exit_code"
70+
fi
71+
return $exit_code
72+
}
73+
74+
handle_extension_versions() {
75+
if ! check_extensions_file; then
76+
return
77+
fi
78+
79+
local extensions_file="/etc/adminapi/pg-extensions.json"
80+
81+
# Get all extension names from the JSON file
82+
local extensions
83+
extensions=$(jq -r 'keys[]' "$extensions_file" 2>/dev/null)
84+
85+
if [ -z "$extensions" ]; then
86+
log "extensions: No extensions found in configuration"
87+
return
88+
fi
89+
90+
# Iterate through each extension
91+
while IFS= read -r extension_name; do
92+
# Get the version for this extension
93+
local version
94+
version=$(jq -r --arg ext "$extension_name" '.[$ext] // empty' "$extensions_file")
95+
96+
if [ -z "$version" ]; then
97+
log "$extension_name: No version specified, skipping"
98+
continue
99+
fi
100+
101+
# Basic version format validation (semantic versioning)
102+
if ! [[ "$version" =~ ^[0-9]+\.[0-9]+(\.[0-9]+)?$ ]]; then
103+
log "$extension_name: Invalid version format: $version, skipping"
104+
continue
105+
fi
106+
107+
log "$extension_name: Found version $version in extensions file"
108+
109+
# Don't fail if version switch fails - just log and continue
110+
switch_extension_version "$extension_name" "$version" || log "$extension_name: Version switch failed but continuing"
111+
112+
done <<< "$extensions"
113+
}
114+
29115
main() {
116+
log "Starting prestart script"
117+
118+
# 1. Handle all extension versions from config file
119+
handle_extension_versions
120+
121+
# 2. orioledb handling
30122
local has_orioledb=$(check_orioledb_enabled)
31123
if [ "$has_orioledb" -lt 1 ]; then
32124
return 0
@@ -35,6 +127,8 @@ main() {
35127
if [ ! -z "$shared_buffers_value" ]; then
36128
update_orioledb_buffers "$shared_buffers_value"
37129
fi
130+
131+
log "Prestart script completed"
38132
}
39133

40134
# Initial locale setup
@@ -46,4 +140,4 @@ if [ $(locale -a | grep -c en_US.utf8) -eq 0 ]; then
46140
locale-gen
47141
fi
48142

49-
main
143+
main

nix/ext/pg_net.nix

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,87 @@ pkgs.buildEnv {
100100
} > $out/share/postgresql/extension/${pname}.control
101101
ln -sfn ${pname}-${latestVersion}${postgresql.dlSuffix} $out/lib/${pname}${postgresql.dlSuffix}
102102
103+
# Create version switcher script
104+
mkdir -p $out/bin
105+
cat > $out/bin/switch_pg_net_version <<'EOF'
106+
#!/bin/sh
107+
set -e
108+
109+
if [ $# -ne 1 ]; then
110+
echo "Usage: $0 <version>"
111+
echo "Example: $0 0.10.0"
112+
echo ""
113+
echo "Optional environment variables:"
114+
echo " NIX_PROFILE - Path to nix profile (default: /var/lib/postgresql/.nix-profile)"
115+
echo " LIB_DIR - Override library directory"
116+
echo " EXTENSION_DIR - Override extension directory"
117+
exit 1
118+
fi
119+
120+
VERSION=$1
121+
122+
# Set defaults, allow environment variable overrides
123+
: ''${NIX_PROFILE:="/var/lib/postgresql/.nix-profile"}
124+
: ''${LIB_DIR:=""}
125+
: ''${EXTENSION_DIR:=""}
126+
127+
# If LIB_DIR not explicitly set, auto-detect it
128+
if [ -z "$LIB_DIR" ]; then
129+
# Follow the complete chain of symlinks to find the multi-version directory
130+
CURRENT_LINK="$NIX_PROFILE/lib/pg_net-$VERSION${postgresql.dlSuffix}"
131+
echo "Starting with link: $CURRENT_LINK"
132+
133+
# Follow first two symlinks to get to the multi-version directory
134+
for i in 1 2; do
135+
if [ -L "$CURRENT_LINK" ]; then
136+
NEXT_LINK=$(readlink "$CURRENT_LINK")
137+
echo "Following link: $NEXT_LINK"
138+
if echo "$NEXT_LINK" | grep -q '^/'; then
139+
CURRENT_LINK="$NEXT_LINK"
140+
else
141+
CURRENT_LINK="$(dirname "$CURRENT_LINK")/$NEXT_LINK"
142+
fi
143+
echo "Current link is now: $CURRENT_LINK"
144+
fi
145+
done
146+
147+
# The multi-version directory should be the parent of the current link
148+
MULTI_VERSION_DIR=$(dirname "$CURRENT_LINK")
149+
echo "Found multi-version directory: $MULTI_VERSION_DIR"
150+
LIB_DIR="$MULTI_VERSION_DIR"
151+
else
152+
echo "Using provided LIB_DIR: $LIB_DIR"
153+
fi
154+
155+
# If EXTENSION_DIR not explicitly set, use default
156+
if [ -z "$EXTENSION_DIR" ]; then
157+
EXTENSION_DIR="$NIX_PROFILE/share/postgresql/extension"
158+
fi
159+
echo "Using EXTENSION_DIR: $EXTENSION_DIR"
160+
161+
echo "Looking for file: $LIB_DIR/pg_net-$VERSION${postgresql.dlSuffix}"
162+
ls -la "$LIB_DIR" || true
163+
164+
# Check if version exists
165+
if [ ! -f "$LIB_DIR/pg_net-$VERSION${postgresql.dlSuffix}" ]; then
166+
echo "Error: Version $VERSION not found in $LIB_DIR"
167+
echo "Available versions:"
168+
ls "$LIB_DIR"/pg_net-*${postgresql.dlSuffix} 2>/dev/null | sed 's/.*pg_net-/ /' | sed 's/${postgresql.dlSuffix}$//' || echo " No versions found"
169+
exit 1
170+
fi
171+
172+
# Update library symlink
173+
ln -sfnv "pg_net-$VERSION${postgresql.dlSuffix}" "$LIB_DIR/pg_net${postgresql.dlSuffix}"
174+
175+
# Update control file
176+
echo "default_version = '$VERSION'" > "$EXTENSION_DIR/pg_net.control"
177+
cat "$EXTENSION_DIR/pg_net--$VERSION.control" >> "$EXTENSION_DIR/pg_net.control"
178+
179+
echo "Successfully switched pg_net to version $VERSION"
180+
EOF
181+
182+
chmod +x $out/bin/switch_pg_net_version
183+
103184
# checks
104185
(set -x
105186
test "$(ls -A $out/lib/${pname}*${postgresql.dlSuffix} | wc -l)" = "${

0 commit comments

Comments
 (0)