hansaka1 commited on
Commit
43b2075
Β·
verified Β·
1 Parent(s): d1cbd0a

Update start.sh

Browse files
Files changed (1) hide show
  1. start.sh +24 -25
start.sh CHANGED
@@ -1,49 +1,48 @@
1
  #!/bin/bash
2
 
3
- # 1. Initialize PostgreSQL cluster if it doesn't exist inside the bucket
 
 
 
 
 
 
4
  if [ ! -d "$PGDATA/base" ]; then
5
- echo "πŸš€ Initializing empty database inside the storage bucket..."
6
  initdb -D "$PGDATA"
 
 
7
  echo "unix_socket_directories = '/tmp'" >> "$PGDATA/postgresql.conf"
8
  fi
9
 
10
- # 2. Start PostgreSQL daemon background process
11
  echo "🐘 Launching PostgreSQL engine..."
12
  pg_ctl -D "$PGDATA" -l "$HOME/app/postgres.log" start
13
 
14
- # Wait for database to wake up completely
15
  until pg_isready -h localhost -p 5432; do
16
  sleep 1
17
  done
18
 
19
- # 3. Dynamic Credential Injection (Runs only on initial setup)
20
  if [ ! -f "$PGDATA/.credentials_configured" ]; then
21
- echo "πŸ”’ Applying secure user credentials from environment secrets..."
22
 
23
- # Use fallback defaults if the environment variables weren't passed
24
- USER_NAME=${DB_USER:-node}
25
  USER_PASS=${DB_PASSWORD:-password123}
26
 
27
- # Alter the default superuser account with your secret custom settings
28
- psql -h localhost -p 5432 -d postgres -c "ALTER USER node WITH SUPERUSER PASSWORD '$USER_PASS';"
29
 
30
- # Optional: If your custom user isn't 'node', create it explicitly
31
- if [ "$USER_NAME" != "node" ]; then
32
  psql -h localhost -p 5432 -d postgres -c "CREATE USER $USER_NAME WITH SUPERUSER PASSWORD '$USER_PASS';"
33
  fi
34
-
35
  touch "$PGDATA/.credentials_configured"
36
- echo "βœ… Admin credentials successfully generated!"
37
  fi
38
 
39
- # 4. Fire up the public TCP tunnel to expose port 5432
40
- echo "🌐 Opening public TCP channel..."
41
- bore local 5432 --to bore.pub > "$HOME/app/tunnel.log" 2>&1 &
42
-
43
- # Brief pause to let the tunnel assign its connection routes
44
- sleep 2
45
- echo "πŸ“’ Check your Space log files or 'tunnel.log' to discover your assigned public port!"
46
-
47
- # 5. Start your primary web application interface
48
- echo "🟒 Starting application server..."
49
- npm start
 
1
  #!/bin/bash
2
 
3
+ # 1. Start a zero-RAM background responder to pass Hugging Face health checks
4
+ echo "πŸ“‘ Starting platform routing responder on port 7860..."
5
+ while true; do
6
+ echo -e "HTTP/1.1 200 OK\nContent-Length: 22\n\nPostgreSQL Server Live" | nc -l -p 7860
7
+ done &
8
+
9
+ # 2. Initialize PostgreSQL cluster if the bucket storage is completely empty
10
  if [ ! -d "$PGDATA/base" ]; then
11
+ echo "πŸš€ Initializing fresh database inside the storage bucket..."
12
  initdb -D "$PGDATA"
13
+
14
+ # Force Postgres to use the /tmp folder for local locks to sidestep permission blocks
15
  echo "unix_socket_directories = '/tmp'" >> "$PGDATA/postgresql.conf"
16
  fi
17
 
18
+ # 3. Spin up the background PostgreSQL engine daemon
19
  echo "🐘 Launching PostgreSQL engine..."
20
  pg_ctl -D "$PGDATA" -l "$HOME/app/postgres.log" start
21
 
22
+ # Block execution until the local database port is fully awake
23
  until pg_isready -h localhost -p 5432; do
24
  sleep 1
25
  done
26
 
27
+ # 4. Inject Secret Credentials dynamically on initial initialization
28
  if [ ! -f "$PGDATA/.credentials_configured" ]; then
29
+ echo "πŸ”’ Configuring admin database users..."
30
 
31
+ USER_NAME=${DB_USER:-postgres}
 
32
  USER_PASS=${DB_PASSWORD:-password123}
33
 
34
+ # Apply configuration to the core runtime user
35
+ psql -h localhost -p 5432 -d postgres -c "ALTER USER current_user WITH SUPERUSER PASSWORD '$USER_PASS';"
36
 
37
+ # Create the custom user if a unique one was passed via Hugging Face Secrets
38
+ if [ "$USER_NAME" != "postgres" ]; then
39
  psql -h localhost -p 5432 -d postgres -c "CREATE USER $USER_NAME WITH SUPERUSER PASSWORD '$USER_PASS';"
40
  fi
41
+
42
  touch "$PGDATA/.credentials_configured"
43
+ echo "βœ… Admin credentials successfully established!"
44
  fi
45
 
46
+ # 5. Fire up the public TCP tunnel in the foreground to keep the container running
47
+ echo "🌐 Opening public TCP channel via Bore..."
48
+ bore local 5432 --to bore.pub