aki-008
commited on
Commit
Β·
1954de6
1
Parent(s):
3540614
fix: robust postgres startup, wait loop, and permission fixes
Browse files
start.sh
CHANGED
|
@@ -1,32 +1,70 @@
|
|
| 1 |
#!/bin/bash
|
|
|
|
| 2 |
|
| 3 |
-
# --- 1.
|
| 4 |
-
#
|
| 5 |
-
|
| 6 |
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
export DATABASE_URL="postgresql+asyncpg://prepuser:password@127.0.0.1:5432/studentdb"
|
|
|
|
|
|
|
|
|
|
| 9 |
export chroma_host="127.0.0.1"
|
| 10 |
export chroma_port="8080"
|
| 11 |
export chroma_collection="prepai_collection"
|
| 12 |
-
export PGDATA=/home/user/postgres_data
|
| 13 |
|
| 14 |
-
# --- 3.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
if [ ! -d "$PGDATA" ]; then
|
| 16 |
-
echo "Initializing database..."
|
| 17 |
-
|
|
|
|
| 18 |
fi
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
pg_ctl -D "$PGDATA" -l /home/user/postgres.log start
|
| 23 |
-
sleep 3
|
| 24 |
|
| 25 |
-
#
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
-
# ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
# Create temp directories for Nginx to write to (avoids permission errors)
|
| 31 |
mkdir -p /tmp/nginx/body \
|
| 32 |
/tmp/nginx/proxy \
|
|
@@ -34,7 +72,7 @@ mkdir -p /tmp/nginx/body \
|
|
| 34 |
/tmp/nginx/uwsgi \
|
| 35 |
/tmp/nginx/scgi
|
| 36 |
|
| 37 |
-
# Generate a
|
| 38 |
cat <<EOF > /tmp/nginx.conf
|
| 39 |
worker_processes 1;
|
| 40 |
daemon off;
|
|
@@ -70,15 +108,10 @@ http {
|
|
| 70 |
}
|
| 71 |
EOF
|
| 72 |
|
| 73 |
-
# Start Nginx
|
| 74 |
-
echo "Starting Nginx..."
|
| 75 |
nginx -c /tmp/nginx.conf &
|
| 76 |
|
| 77 |
-
# ---
|
| 78 |
-
echo "Starting
|
| 79 |
-
chroma run --host 0.0.0.0 --port 8080 --path ./chroma_store &
|
| 80 |
-
|
| 81 |
-
# --- 6. Start Backend ---
|
| 82 |
-
echo "Starting Backend..."
|
| 83 |
cd Backend
|
| 84 |
python run.py
|
|
|
|
| 1 |
#!/bin/bash
|
| 2 |
+
set -e # Exit immediately if any command fails
|
| 3 |
|
| 4 |
+
# --- 1. Dynamic PostgreSQL Path Detection ---
|
| 5 |
+
# Find the directory containing pg_ctl (works for any postgres version)
|
| 6 |
+
PG_BIN_DIR=$(find /usr/lib/postgresql -name pg_ctl | head -n 1 | xargs dirname)
|
| 7 |
|
| 8 |
+
if [ -z "$PG_BIN_DIR" ]; then
|
| 9 |
+
echo "β Error: Could not find PostgreSQL binaries."
|
| 10 |
+
exit 1
|
| 11 |
+
fi
|
| 12 |
+
|
| 13 |
+
echo "β
Found PostgreSQL binaries at $PG_BIN_DIR"
|
| 14 |
+
export PATH="$PG_BIN_DIR:$PATH"
|
| 15 |
+
|
| 16 |
+
# --- 2. Set Critical Environment Variables ---
|
| 17 |
export DATABASE_URL="postgresql+asyncpg://prepuser:password@127.0.0.1:5432/studentdb"
|
| 18 |
+
export PGDATA=/home/user/postgres_data
|
| 19 |
+
export HOME=/home/user
|
| 20 |
+
# ChromaDB settings
|
| 21 |
export chroma_host="127.0.0.1"
|
| 22 |
export chroma_port="8080"
|
| 23 |
export chroma_collection="prepai_collection"
|
|
|
|
| 24 |
|
| 25 |
+
# --- 3. Database Initialization ---
|
| 26 |
+
# If PGDATA exists but is missing the version file, it's corrupt. Wipe it.
|
| 27 |
+
if [ -d "$PGDATA" ] && [ ! -f "$PGDATA/PG_VERSION" ]; then
|
| 28 |
+
echo "β οΈ $PGDATA exists but is not a valid cluster. Wiping to start fresh..."
|
| 29 |
+
rm -rf "$PGDATA"
|
| 30 |
+
fi
|
| 31 |
+
|
| 32 |
if [ ! -d "$PGDATA" ]; then
|
| 33 |
+
echo "βοΈ Initializing database..."
|
| 34 |
+
# Initialize with trust auth for local connections to avoid password issues during setup
|
| 35 |
+
initdb -D "$PGDATA" --auth-local=trust --no-locale --encoding=UTF8
|
| 36 |
fi
|
| 37 |
|
| 38 |
+
# --- 4. Start PostgreSQL ---
|
| 39 |
+
echo "π Starting PostgreSQL..."
|
| 40 |
pg_ctl -D "$PGDATA" -l /home/user/postgres.log start
|
|
|
|
| 41 |
|
| 42 |
+
# CRITICAL: Wait for Postgres to be ready before continuing
|
| 43 |
+
echo "β³ Waiting for PostgreSQL to accept connections..."
|
| 44 |
+
until pg_isready -h 127.0.0.1 -p 5432; do
|
| 45 |
+
echo " ...waiting for DB..."
|
| 46 |
+
sleep 2
|
| 47 |
+
done
|
| 48 |
+
echo "β
PostgreSQL is up!"
|
| 49 |
|
| 50 |
+
# --- 5. User & DB Setup ---
|
| 51 |
+
echo "π οΈ Configuring Database..."
|
| 52 |
+
# Idempotent creation: Only create if they don't exist
|
| 53 |
+
psql -h 127.0.0.1 -d postgres -tAc "SELECT 1 FROM pg_roles WHERE rolname='prepuser'" | grep -q 1 || \
|
| 54 |
+
psql -h 127.0.0.1 -d postgres -c "CREATE USER prepuser WITH PASSWORD 'password';"
|
| 55 |
+
|
| 56 |
+
psql -h 127.0.0.1 -d postgres -tAc "SELECT 1 FROM pg_database WHERE datname='studentdb'" | grep -q 1 || \
|
| 57 |
+
createdb -h 127.0.0.1 -O prepuser studentdb
|
| 58 |
+
|
| 59 |
+
# --- 6. ChromaDB Setup ---
|
| 60 |
+
echo "π¨ Setting up ChromaDB..."
|
| 61 |
+
# Ensure directory exists and we own it
|
| 62 |
+
mkdir -p ./chroma_store
|
| 63 |
+
# Start Chroma in the background
|
| 64 |
+
chroma run --host 0.0.0.0 --port 8080 --path ./chroma_store &
|
| 65 |
+
|
| 66 |
+
# --- 7. Nginx Setup (Non-root Mode) ---
|
| 67 |
+
echo "π Starting Nginx..."
|
| 68 |
# Create temp directories for Nginx to write to (avoids permission errors)
|
| 69 |
mkdir -p /tmp/nginx/body \
|
| 70 |
/tmp/nginx/proxy \
|
|
|
|
| 72 |
/tmp/nginx/uwsgi \
|
| 73 |
/tmp/nginx/scgi
|
| 74 |
|
| 75 |
+
# Generate a temporary nginx.conf that listens on port 7860
|
| 76 |
cat <<EOF > /tmp/nginx.conf
|
| 77 |
worker_processes 1;
|
| 78 |
daemon off;
|
|
|
|
| 108 |
}
|
| 109 |
EOF
|
| 110 |
|
| 111 |
+
# Start Nginx using the temp config
|
|
|
|
| 112 |
nginx -c /tmp/nginx.conf &
|
| 113 |
|
| 114 |
+
# --- 8. Start Backend ---
|
| 115 |
+
echo "π Starting Backend..."
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
cd Backend
|
| 117 |
python run.py
|