Skip to content

Commit

Permalink
wip toward fixing for all systems
Browse files Browse the repository at this point in the history
  • Loading branch information
samrose committed Jan 7, 2025
1 parent d04658b commit 924ce92
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 12 deletions.
30 changes: 23 additions & 7 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,7 @@
pkgs.runCommand "start-postgres-server" {
inherit migrationsDir postgresqlSchemaSql pgbouncerAuthSchemaSql statExtensionSql;
} ''
set -x
mkdir -p $out/bin $out/etc/postgresql-custom $out/etc/postgresql $out/extension-custom-scripts
cp ${supautilsConfigFile} $out/etc/postgresql-custom/supautils.conf || { echo "Failed to copy supautils.conf"; exit 1; }
cp ${pgconfigFile} $out/etc/postgresql/postgresql.conf || { echo "Failed to copy postgresql.conf"; exit 1; }
Expand All @@ -452,6 +453,7 @@
chmod 644 $out/etc/postgresql-custom/logging.conf
chmod 644 $out/etc/postgresql/pg_hba.conf
substitute ${./nix/tools/run-server.sh.in} $out/bin/start-postgres-server \
--subst-var-by 'SHELL_PATH' '${pkgs.bash}/bin/bash' \
--subst-var-by 'PGSQL_DEFAULT_PORT' '${pgsqlDefaultPort}' \
--subst-var-by 'PGSQL_SUPERUSER' '${pgsqlSuperuser}' \
--subst-var-by 'PSQL15_BINDIR' '${basePackages.psql_15.bin}' \
Expand Down Expand Up @@ -594,8 +596,7 @@
sqlTests = ./nix/tests/smoke;
pg_prove = pkgs.perlPackages.TAPParserSourceHandlerpgTAP;
pg_regress = basePackages.pg_regress;
getkeyScript = ./nix/tests/util/pgsodium_getkey.sh;

start-postgres-server-bin = basePackages.start-server;
getVersionArg = pkg:
let
name = pkg.version;
Expand All @@ -609,13 +610,29 @@
{
nativeBuildInputs = with pkgs; [
coreutils bash pgpkg pg_prove pg_regress procps
basePackages.start-server
start-postgres-server-bin which
];
} ''
set -e
export KEY_FILE="${getkeyScript}"
start-postgres-server ${getVersionArg pgpkg} --daemonize
echo "Contents of nativeBuildInputs PATH:"
echo $PATH
echo "Checking start-postgres-server:"
which start-postgres-server
ls -l $(which start-postgres-server)
file $(which start-postgres-server)
# echo "Creating the getKeyScript..."
# cat > getKeyScript.sh <<'EOF'
# #!/usr/bin/env bash
# set -euo pipefail
# if [[ ! -f "$KEY_FILE" ]]; then
# head -c 32 /dev/urandom | od -A n -t x1 | tr -d ' \n' > "$KEY_FILE"
# fi
# cat "$KEY_FILE"
# EOF
# Make the script executable
${start-postgres-server-bin}/bin/start-postgres-server ${getVersionArg pgpkg} --daemonize
for i in {1..60}; do
if pg_isready -h localhost -p 5435 -U supabase_admin -q; then
Expand Down Expand Up @@ -692,7 +709,6 @@
pg-restore = mkApp "pg-restore" "pg-restore";
local-infra-bootstrap = mkApp "local-infra-bootstrap" "local-infra-bootstrap";
dbmate-tool = mkApp "dbmate-tool" "dbmate-tool";
migration-unit-tests = mkApp "migration-unit-tests" "migration-unit-tests";
};

# 'devShells.default' lists the set of packages that are included in the
Expand Down
2 changes: 1 addition & 1 deletion nix/tests/util/pgsodium_getkey.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash
#!/usr/bin/env bash

set -euo pipefail

Expand Down
70 changes: 66 additions & 4 deletions nix/tools/run-server.sh.in
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env bash
#!@SHELL_PATH@
# shellcheck shell=bash
[ ! -z "$DEBUG" ] && set -x

Expand All @@ -7,6 +7,7 @@ SKIP_MIGRATIONS=false
PSQL_USER="postgres"
MIGRATION_FILE=""
DAEMONIZE=false
GETKEY_SCRIPT=""

# Function to display help
print_help() {
Expand All @@ -16,16 +17,61 @@ print_help() {
echo " --skip-migrations Skip running migrations and SQL statements"
echo " --migration-file FILE Provide a custom migration script"
echo " --user USER Specify the user/role to use (default: postgres)"
echo " --getkey-script SCRIPT Provide a custom path to the PGSODIUM_GETKEY_SCRIPT"
echo " -h, --help Show this help message"
echo
echo "VERSION must be one of: 15, 16, orioledb-17"
echo "PORT is optional (default: @PGSQL_DEFAULT_PORT@)"
}

# start_postgres() {
# local mode=$1
# local LOG_DIR="${DATDIR}_logs"
# mkdir -p "$LOG_DIR"
# local LOG_FILE="$LOG_DIR/postgres.log"
# touch "$LOG_FILE"

# if [ "$mode" = "daemon" ]; then
# # Start in background but follow logs
# pg_ctl start -D "$DATDIR" -l "$LOG_FILE" -W \
# -o "--config-file=$DATDIR/postgresql.conf -p $PORTNO -k $DATDIR/tmp"
# local PG_STATUS=$?

# echo "PostgreSQL startup output:"
# cat "$LOG_FILE"

# return $PG_STATUS
# else
# # Foreground mode
# exec postgres --config-file="$DATDIR/postgresql.conf" -p "$PORTNO" -D "$DATDIR" -k "/tmp" -F
# fi
# }

start_postgres() {
local mode=$1
local LOG_DIR="${DATDIR}_logs"
mkdir -p "$LOG_DIR"
local LOG_FILE="$LOG_DIR/postgres.log"
touch "$LOG_FILE"
if [ "$mode" = "daemon" ]; then
pg_ctl start -D "$DATDIR" -l "$DATDIR/logfile" -o "--config-file=$DATDIR/postgresql.conf -p $PORTNO -k $DATDIR/tmp"
# Start the server
pg_ctl start -D "$DATDIR" -l "$LOG_FILE" \
-o "--config-file=$DATDIR/postgresql.conf -p $PORTNO -k $DATDIR/tmp"

# Give it a moment to write logs
sleep 1

# Check server status and logs
if ! pg_ctl status -D "$DATDIR"; then
echo "PostgreSQL failed to start. Full logs:"
cat "$LOG_FILE"
# You might also want to see the postmaster.pid if it exists
if [ -f "$DATDIR/postmaster.pid" ]; then
echo "postmaster.pid contents:"
cat "$DATDIR/postmaster.pid"
fi
return 1
fi
else
# Foreground mode
exec postgres --config-file="$DATDIR/postgresql.conf" -p "$PORTNO" -D "$DATDIR" -k "/tmp" -F
Expand All @@ -38,6 +84,7 @@ stop_postgres() {

trap 'stop_postgres' SIGINT SIGTERM

# Parse arguments
# Parse arguments
while [[ "$#" -gt 0 ]]; do
case "$1" in
Expand All @@ -63,6 +110,15 @@ while [[ "$#" -gt 0 ]]; do
exit 1
fi
;;
--getkey-script)
if [[ -n "$2" && ! "$2" =~ ^- ]]; then
GETKEY_SCRIPT="$2"
shift 2
else
echo "Error: --getkey-script requires a script path"
exit 1
fi
;;
--daemonize)
DAEMONIZE=true
shift
Expand Down Expand Up @@ -90,7 +146,11 @@ while [[ "$#" -gt 0 ]]; do
;;
esac
done

if [[ -n "${GETKEY_SCRIPT:-}" ]]; then
export PGSODIUM_GETKEY_SCRIPT="$GETKEY_SCRIPT"
else
PGSODIUM_GETKEY_SCRIPT="${PGSODIUM_GETKEY_SCRIPT:-@PGSODIUM_GETKEY@}"
fi
# Verify version and set binary directory
if [ "$VERSION" == "15" ]; then
echo "Starting server for PSQL 15"
Expand All @@ -113,7 +173,6 @@ fi
export PATH=$BINDIR/bin:$PATH
PGSQL_SUPERUSER=@PGSQL_SUPERUSER@
PSQL_CONF_FILE=@PSQL_CONF_FILE@
PGSODIUM_GETKEY_SCRIPT=@PGSODIUM_GETKEY@
PORTNO="${PORTNO:-@PGSQL_DEFAULT_PORT@}"
SUPAUTILS_CONFIG_FILE=@SUPAUTILS_CONF_FILE@
LOGGING_CONFIG_FILE=@LOGGING_CONF_FILE@
Expand All @@ -140,6 +199,9 @@ export LANGUAGE=en_US.UTF-8
export LC_ALL=en_US.UTF-8
export LC_CTYPE=en_US.UTF-8
export KEY_FILE="$DATDIR/pgsodium.key"
echo "KEY_FILE: $KEY_FILE"
echo "KEY_FILE contents:"
cat "$KEY_FILE"

echo "PGSODIUM_GETKEY_SCRIPT: $PGSODIUM_GETKEY_SCRIPT"
echo "NOTE: using port $PORTNO for server"
Expand Down

0 comments on commit 924ce92

Please sign in to comment.