-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentrypoint.sh
More file actions
117 lines (97 loc) · 3.38 KB
/
Copy pathentrypoint.sh
File metadata and controls
117 lines (97 loc) · 3.38 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
110
111
112
113
114
115
116
117
#!/bin/sh
set -e
DATA_DIR="/app/data"
DB_PASSWORD_FILE="$DATA_DIR/.db_password"
DB_PASS=$(openssl rand -hex 16)
DB_USER="flexurl"
DB_NAME="flexurl"
DB_HOST="localhost"
DB_PORT="5432"
export POSTGRES_URL="postgresql://${DB_USER}:${DB_PASS}@${DB_HOST}:${DB_PORT}/${DB_NAME}"
# --- Generate secrets if missing ---
mkdir -p "$DATA_DIR"
# DB password
if [ -f "$DB_PASSWORD_FILE" ]; then
DB_PASS=$(cat "$DB_PASSWORD_FILE")
export POSTGRES_URL="postgresql://${DB_USER}:${DB_PASS}@${DB_HOST}:${DB_PORT}/${DB_NAME}"
echo "[FlexURL] Loaded existing DB password"
else
echo "$DB_PASS" > "$DB_PASSWORD_FILE"
chmod 600 "$DB_PASSWORD_FILE"
echo "[FlexURL] Generated new DB password"
fi
# Encryption key
KEY_FILE="$DATA_DIR/.encryption_key"
if [ -z "$ENCRYPTION_KEY" ]; then
if [ -f "$KEY_FILE" ]; then
export ENCRYPTION_KEY=$(cat "$KEY_FILE")
echo "[FlexURL] Loaded existing encryption key"
else
ENCRYPTION_KEY=$(openssl rand -hex 32)
export ENCRYPTION_KEY
echo "$ENCRYPTION_KEY" > "$KEY_FILE"
chmod 600 "$KEY_FILE"
echo "[FlexURL] Generated new encryption key"
fi
else
echo "$ENCRYPTION_KEY" > "$KEY_FILE"
chmod 600 "$KEY_FILE"
fi
# --- Init PostgreSQL ---
PGDATA="$DATA_DIR/pg"
PGLOG="$DATA_DIR/pg.log"
# First run: init database
if [ ! -f "$PGDATA/PG_VERSION" ]; then
echo "[FlexURL] Initializing PostgreSQL..."
rm -rf "$PGDATA"
mkdir -p "$PGDATA"
chown -R postgres:postgres "$PGDATA"
printf '%s\n%s\n' "$DB_PASS" "$DB_PASS" > /tmp/.pwfile
su-exec postgres initdb -D "$PGDATA" --auth=password --username=postgres --pwfile=/tmp/.pwfile
rm -f /tmp/.pwfile
echo "local all all trust" > "$PGDATA/pg_hba.conf"
echo "host all all 127.0.0.1/32 md5" >> "$PGDATA/pg_hba.conf"
echo "host all all ::1/128 md5" >> "$PGDATA/pg_hba.conf"
chown -R postgres:postgres "$PGDATA"
echo "[FlexURL] PostgreSQL initialized"
fi
# Ensure permissions and runtime dir
chown -R postgres:postgres "$PGDATA"
mkdir -p /run/postgresql
chown postgres:postgres /run/postgresql
touch "$PGLOG" && chown postgres:postgres "$PGLOG"
# Start PostgreSQL in background
echo "[FlexURL] Starting PostgreSQL..."
su-exec postgres postgres -D "$PGDATA" -c log_destination=stderr > "$PGLOG" 2>&1 &
PG_PID=$!
# Wait for PostgreSQL to be ready
echo "[FlexURL] Waiting for PostgreSQL..."
for i in $(seq 1 30); do
if su-exec postgres pg_isready -q 2>/dev/null; then
break
fi
if ! kill -0 $PG_PID 2>/dev/null; then
echo "[FlexURL] PostgreSQL failed to start. Log:"
cat "$PGLOG"
exit 1
fi
sleep 1
done
if ! su-exec postgres pg_isready -q 2>/dev/null; then
echo "[FlexURL] PostgreSQL did not become ready. Log:"
cat "$PGLOG"
exit 1
fi
echo "[FlexURL] PostgreSQL is ready"
# Create user/database if first run
su-exec postgres psql -tc "SELECT 1 FROM pg_roles WHERE rolname='$DB_USER'" | grep -q 1 || \
su-exec postgres psql -c "CREATE USER $DB_USER WITH PASSWORD '$DB_PASS';"
su-exec postgres psql -tc "SELECT 1 FROM pg_database WHERE datname='$DB_NAME'" | grep -q 1 || \
su-exec postgres psql -c "CREATE DATABASE $DB_NAME OWNER $DB_USER;"
su-exec postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE $DB_NAME TO $DB_USER;" 2>/dev/null || true
# Run init.sql
su-exec postgres psql -U "$DB_USER" -d "$DB_NAME" -f /app/init.sql 2>/dev/null || true
echo "[FlexURL] Database ready"
# --- Start Next.js ---
echo "[FlexURL] Starting FlexURL on port ${PORT:-3000}..."
exec su-exec nextjs node /app/server.js