-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathentrypoint.sh
More file actions
executable file
·109 lines (90 loc) · 3.86 KB
/
entrypoint.sh
File metadata and controls
executable file
·109 lines (90 loc) · 3.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/bin/bash
set -e
PGDATA="${PGDATA:-/data/pgdata}"
PGRUNDIR="/tmp/pgsocket"
echo "=== Roots of The Valley ==="
echo "Starting up..."
# Create PostgreSQL socket directory with correct ownership
mkdir -p "$PGRUNDIR"
chown postgres:postgres "$PGRUNDIR"
chmod 755 "$PGRUNDIR"
# Ensure data directory ownership is correct
# Both tmpfs and bind mounts need ownership fixed since container runs as root
PGDATA_OWNER=$(stat -c '%u' "$PGDATA" 2>/dev/null || echo "unknown")
if [ "$PGDATA_OWNER" != "70" ]; then
echo "Fixing data directory permissions..."
chown -R postgres:postgres "$PGDATA"
chmod 700 "$PGDATA"
fi
# Remove stale PID file if it exists (from previous unclean shutdown)
rm -f "$PGDATA/postmaster.pid" 2>/dev/null || true
# Initialize PostgreSQL if needed
if [ ! -f "$PGDATA/PG_VERSION" ]; then
echo "Initializing PostgreSQL database..."
# Initialize as postgres user (PostgreSQL refuses to run as root)
# Use runuser to preserve environment variables
runuser -u postgres -- initdb -D "$PGDATA" -U postgres
# Configure PostgreSQL for local connections
cat >> "$PGDATA/pg_hba.conf" << 'EOF'
host all all 127.0.0.1/32 trust
host all all ::1/128 trust
local all all trust
EOF
# Configure PostgreSQL to listen on localhost and use custom socket dir
cat >> "$PGDATA/postgresql.conf" << EOF
listen_addresses = 'localhost'
unix_socket_directories = '$PGRUNDIR'
EOF
# Start PostgreSQL temporarily to create databases (as postgres user)
runuser -u postgres -- pg_ctl -D "$PGDATA" -l /tmp/pg_init.log start -o "-k $PGRUNDIR"
sleep 3
echo "Creating databases..."
psql -h "$PGRUNDIR" -U postgres -d postgres -c "CREATE DATABASE rotv;" 2>/dev/null || true
psql -h "$PGRUNDIR" -U postgres -d postgres -c "CREATE DATABASE rotv_test;" 2>/dev/null || true
runuser -u postgres -- pg_ctl -D "$PGDATA" stop
sleep 2
fi
# Start PostgreSQL as postgres user (container runs as root, but PostgreSQL as postgres)
echo "Starting PostgreSQL..."
runuser -u postgres -- pg_ctl -D "$PGDATA" -l "$PGDATA/postgresql.log" start -o "-k $PGRUNDIR"
# Wait for PostgreSQL to be ready
echo "Waiting for PostgreSQL to be ready..."
for i in {1..30}; do
if pg_isready -h "$PGRUNDIR" -q; then
echo "PostgreSQL is ready"
break
fi
sleep 1
done
# Ensure rotv_test database exists (for testing)
psql -h "$PGRUNDIR" -U postgres -d postgres -c "CREATE DATABASE rotv_test;" 2>/dev/null || true
# Import seed data if available (before app starts to avoid schema conflicts)
if [ -f /tmp/seed-data.sql ]; then
echo "Importing seed data..."
psql -h "$PGRUNDIR" -U postgres -d rotv -f /tmp/seed-data.sql 2>&1 | grep -c "^COPY" | xargs echo "Imported rows from tables:"
echo "✓ Seed data imported"
fi
# Create test admin user for auth bypass (after seed data which may overwrite users table)
if [ "$BYPASS_AUTH" = "true" ] || [ "$NODE_ENV" = "test" ]; then
psql -h "$PGRUNDIR" -U postgres -d rotv -c "
INSERT INTO users (id, email, name, oauth_provider, oauth_provider_id, is_admin, role)
VALUES (999, 'test-admin@rotv.local', 'Test Admin', 'test', '999', true, 'admin')
ON CONFLICT (id) DO UPDATE SET email = EXCLUDED.email, is_admin = true, role = 'admin';
" > /dev/null 2>&1
echo "✓ Test admin user created (ID 999)"
fi
# Run all numbered SQL migrations (after seed data import)
# Migrations are idempotent (IF NOT EXISTS, etc.) so safe to re-run
echo "Running database migrations..."
MIGRATION_COUNT=0
for migration in /app/migrations/[0-9]*.sql; do
[ -f "$migration" ] || continue
MIGRATION_NAME=$(basename "$migration")
psql -h "$PGRUNDIR" -U postgres -d rotv -f "$migration" > /tmp/migration_output.txt 2>&1
MIGRATION_COUNT=$((MIGRATION_COUNT + 1))
done
echo "✓ $MIGRATION_COUNT migrations applied"
# Start the Node.js application
echo "Starting Roots of The Valley application..."
cd /app
exec node server.js