-
-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathdocker-entrypoint.js
More file actions
198 lines (163 loc) · 5.82 KB
/
docker-entrypoint.js
File metadata and controls
198 lines (163 loc) · 5.82 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#!/usr/bin/env node
const fs = require('fs')
const path = require('path')
const { spawn } = require('child_process')
const CONFIG_DIR = process.env.WEBUI_CONFIG_DIR || '/app/config'
const CONFIG_DOTENV = path.join(CONFIG_DIR, '.env')
const APP_DOTENV = path.join('/app', '.env')
const log = (...args) => console.log('[entrypoint]', ...args)
const warn = (...args) => console.warn('[entrypoint]', ...args)
const ensureConfigDir = () => {
try {
fs.mkdirSync(CONFIG_DIR, { recursive: true })
} catch (e) {
warn(`Could not create ${CONFIG_DIR}: ${e.message}`)
}
}
const loadDotenvFile = (filePath) => {
try {
if (!fs.existsSync(filePath)) return false
require('dotenv').config({ path: filePath, override: false })
log(`Loaded environment from ${filePath}`)
return true
} catch (e) {
warn(`Could not load ${filePath}: ${e.message}`)
return false
}
}
const loadEnv = () => {
if (process.env.DOTENV_CONFIG_PATH) loadDotenvFile(process.env.DOTENV_CONFIG_PATH)
loadDotenvFile(CONFIG_DOTENV)
loadDotenvFile(APP_DOTENV)
}
const persistEnv = (changes, target = CONFIG_DOTENV) => {
const editDotenv = require('edit-dotenv')
let existing = ''
try {
if (fs.existsSync(target)) existing = fs.readFileSync(target, 'utf8')
} catch (e) {
warn(`Could not read ${target}: ${e.message}`)
}
const next = editDotenv(existing, changes)
try {
fs.writeFileSync(target, next, { mode: 0o600 })
log(`Persisted ${Object.keys(changes).join(', ')} to ${target}`)
} catch (e) {
warn(`Could not write ${target}: ${e.message}. Generated keys WILL NOT survive restart unless persisted manually.`)
}
}
const ensureKeys = async () => {
const { generateKeys, isValidHexKey } = require('./server/setup/keys')
const existingEncryption = isValidHexKey(process.env.ENCRYPTION_KEY) ? process.env.ENCRYPTION_KEY : undefined
const existingSession = isValidHexKey(process.env.SESSION_KEY) ? process.env.SESSION_KEY : undefined
const existingVapidPublic = process.env.NOTIFICATION_VAPID_PUBLIC_KEY || undefined
const existingVapidPrivate = process.env.NOTIFICATION_VAPID_PRIVATE_KEY || undefined
const allPresent = existingEncryption && existingSession && existingVapidPublic && existingVapidPrivate
if (allPresent) {
log('All keys present in environment, skipping generation')
return
}
log('Generating missing keys...')
const keys = await generateKeys({
existing: {
encryptionKey: existingEncryption,
sessionKey: existingSession,
vapidPublicKey: existingVapidPublic,
vapidPrivateKey: existingVapidPrivate
}
})
const changes = {}
if (!existingEncryption) {
process.env.ENCRYPTION_KEY = keys.encryptionKey
changes.ENCRYPTION_KEY = keys.encryptionKey
}
if (!existingSession) {
process.env.SESSION_KEY = keys.sessionKey
changes.SESSION_KEY = keys.sessionKey
}
if (!existingVapidPublic) {
process.env.NOTIFICATION_VAPID_PUBLIC_KEY = keys.vapidPublicKey
changes.NOTIFICATION_VAPID_PUBLIC_KEY = keys.vapidPublicKey
}
if (!existingVapidPrivate) {
process.env.NOTIFICATION_VAPID_PRIVATE_KEY = keys.vapidPrivateKey
changes.NOTIFICATION_VAPID_PRIVATE_KEY = keys.vapidPrivateKey
}
if (Object.keys(changes).length) persistEnv(changes)
}
const hasDbConfig = () => Boolean(process.env.DB_HOST && process.env.DB_USER && process.env.DB_NAME)
const waitForDatabase = async ({ retries = 30, delayMs = 2000 } = {}) => {
if (!hasDbConfig()) return false
const { validateDbConnection } = require('./server/setup/db')
for (let attempt = 1; attempt <= retries; attempt++) {
const result = await validateDbConnection({
host: process.env.DB_HOST,
port: process.env.DB_PORT,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD || '',
database: process.env.DB_NAME
})
if (result.ok) {
log(`Database reachable after attempt ${attempt}`)
return true
}
if (attempt === 1) {
log(`Database not reachable yet (will retry every ${delayMs}ms): ${result.error.message}`)
}
await new Promise((resolve) => setTimeout(resolve, delayMs))
}
warn(`Database still unreachable after ${retries} attempts. The server will start in setup mode.`)
return false
}
const runMigrationsIfPossible = async () => {
if (!hasDbConfig()) {
log('DB env vars missing — skipping migrations. Visit /setup to complete installation.')
return
}
const { runMigrations } = require('./server/setup/migrations')
try {
log('Running database migrations...')
await runMigrations({
host: process.env.DB_HOST,
port: process.env.DB_PORT,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD || '',
database: process.env.DB_NAME
})
log('Database migrations complete')
} catch (e) {
warn(`Migration failed: ${e.message}. The server will start anyway; visit /setup or run \`npx bmwebui doctor\` for details.`)
}
}
const runDoctor = () => new Promise((resolve) => {
if (!hasDbConfig()) {
log('Skipping doctor preflight (DB not configured yet)')
return resolve()
}
log('Running preflight (npx bmwebui doctor)...')
const child = spawn(process.execPath, [path.join(__dirname, 'bin', 'run.js'), 'doctor'], {
stdio: 'inherit',
env: process.env
})
child.on('exit', (code) => {
if (code !== 0) {
warn(`Doctor reported issues (exit ${code}). Continuing startup so /setup remains reachable.`)
}
resolve()
})
})
const main = async () => {
ensureConfigDir()
loadEnv()
await ensureKeys()
loadEnv() // re-load so newly persisted keys are visible if other tooling reads .env
await waitForDatabase()
await runMigrationsIfPossible()
await runDoctor()
log('Starting WebUI server...')
await require('./server.js').main()
}
main().catch((err) => {
console.error('[entrypoint] Startup failed:', err)
process.exit(1)
})