aki-008 commited on
Commit
1954de6
Β·
1 Parent(s): 3540614

fix: robust postgres startup, wait loop, and permission fixes

Browse files
Files changed (1) hide show
  1. start.sh +57 -24
start.sh CHANGED
@@ -1,32 +1,70 @@
1
  #!/bin/bash
 
2
 
3
- # --- 1. Fix PostgreSQL Path ---
4
- # Add common PostgreSQL bin paths to $PATH so 'initdb' and 'pg_ctl' are found
5
- export PATH=/usr/lib/postgresql/17/bin:/usr/lib/postgresql/16/bin:/usr/lib/postgresql/15/bin:/usr/lib/postgresql/14/bin:$PATH
6
 
7
- # --- 2. Set Critical Environment Variables (Fixes Pydantic Error) ---
 
 
 
 
 
 
 
 
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. Initialize & Start PostgreSQL ---
 
 
 
 
 
 
15
  if [ ! -d "$PGDATA" ]; then
16
- echo "Initializing database..."
17
- initdb -D "$PGDATA"
 
18
  fi
19
 
20
- echo "Starting PostgreSQL..."
21
- # Start DB in background
22
  pg_ctl -D "$PGDATA" -l /home/user/postgres.log start
23
- sleep 3
24
 
25
- # Create User and Database if they don't exist
26
- psql -h 127.0.0.1 -d postgres -c "CREATE USER prepuser WITH PASSWORD 'password';" || true
27
- createdb -h 127.0.0.1 -O prepuser studentdb || true
 
 
 
 
28
 
29
- # --- 4. Start Nginx (Non-Root Mode) ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 non-root compatible nginx config on the fly
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 in the background using the custom config
74
- echo "Starting Nginx..."
75
  nginx -c /tmp/nginx.conf &
76
 
77
- # --- 5. Start ChromaDB ---
78
- echo "Starting ChromaDB..."
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