-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Expand file tree
/
Copy pathinstall.ps1
More file actions
354 lines (323 loc) · 13 KB
/
Copy pathinstall.ps1
File metadata and controls
354 lines (323 loc) · 13 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# QuantDinger interactive installer for Windows PowerShell.
#
# Usage:
# irm https://raw.githubusercontent.com/OpenByteInc/QuantDinger/main/install.ps1 | iex
#
# Optional environment overrides:
# $env:QUANTDINGER_INSTALL_REF = "main"
# $env:QUANTDINGER_INSTALL_DIR = "C:\QuantDinger"
$ErrorActionPreference = "Stop"
$ProgressPreference = "SilentlyContinue"
$InstallDir = if ($env:QUANTDINGER_INSTALL_DIR) { $env:QUANTDINGER_INSTALL_DIR } else { Join-Path $HOME "quantdinger" }
$InstallRef = if ($env:QUANTDINGER_INSTALL_REF) { $env:QUANTDINGER_INSTALL_REF } else { "main" }
$GithubRaw = "https://raw.githubusercontent.com/OpenByteInc/QuantDinger/$InstallRef"
$ComposeFile = "docker-compose.yml"
$EnvFile = ".env"
$EnvTemplate = ".env.example"
$LegacyEnv = "backend.env"
function Fail($Message) {
Write-Host "Error: $Message" -ForegroundColor Red
exit 1
}
function Get-EnvValue($Path, $Key) {
if (-not (Test-Path $Path)) { return "" }
$line = Get-Content $Path | Where-Object { $_ -match "^$([regex]::Escape($Key))=" } | Select-Object -Last 1
if (-not $line) { return "" }
return $line.Substring($Key.Length + 1)
}
function Set-EnvValue($Path, $Key, $Value) {
if (-not (Test-Path $Path)) { New-Item -ItemType File -Path $Path | Out-Null }
[string[]]$lines = @(Get-Content -LiteralPath $Path)
$found = $false
[string[]]$next = @(
foreach ($line in $lines) {
if ($line -match "^$([regex]::Escape($Key))=") {
"$Key=$Value"
$found = $true
} else {
$line
}
}
)
if (-not $found) { $next += "$Key=$Value" }
Set-Content -LiteralPath $Path -Value $next -Encoding UTF8
}
function Repair-EnvLayout($Path, [string[]]$KnownKeys) {
if (-not (Test-Path $Path)) { return }
$content = Get-Content -LiteralPath $Path -Raw
if (-not $content -or -not $KnownKeys) { return }
$keyPattern = (($KnownKeys | ForEach-Object { [regex]::Escape($_) }) -join "|")
$pattern = "(?<=[^`r`n])(?=(?:$keyPattern)=)"
$repaired = [regex]::Replace($content, $pattern, [Environment]::NewLine)
if ($repaired -ne $content) {
Set-Content -LiteralPath $Path -Value $repaired.TrimEnd("`r", "`n") -Encoding UTF8
}
}
function Sync-MissingEnvKeys($Target, $Template, $Legacy = "") {
if (-not (Test-Path -LiteralPath $Template)) {
Fail "Environment template not found: $Template"
}
if (-not (Test-Path -LiteralPath $Target)) {
if ($Legacy -and (Test-Path -LiteralPath $Legacy)) {
Copy-Item -LiteralPath $Legacy -Destination $Target
Write-Host "Created $Target from legacy $Legacy."
$Legacy = ""
} else {
Copy-Item -LiteralPath $Template -Destination $Target
Write-Host "Created $Target from $Template."
}
}
$assignment = '^\s*(?:export\s+)?(?<key>[A-Za-z_][A-Za-z0-9_]*)\s*='
$known = [System.Collections.Generic.HashSet[string]]::new([System.StringComparer]::Ordinal)
foreach ($line in (Get-Content -LiteralPath $Target)) {
if ($line -match $assignment) { [void]$known.Add($Matches.key) }
}
[System.Collections.Generic.List[string]]$additions = @()
[System.Collections.Generic.List[string]]$sources = @()
if ($Legacy -and (Test-Path -LiteralPath $Legacy)) { $sources.Add($Legacy) }
$sources.Add($Template)
foreach ($source in $sources) {
foreach ($line in (Get-Content -LiteralPath $source)) {
if ($line -match $assignment -and $known.Add($Matches.key)) {
$additions.Add($line)
}
}
}
if ($additions.Count -eq 0) {
Write-Host "Environment is up to date: $Target"
return
}
$existingContent = Get-Content -LiteralPath $Target -Raw
if ($existingContent) {
$stamp = [DateTime]::UtcNow.ToString("yyyyMMddTHHmmssZ")
$backup = "$Target.bak.$stamp"
$backupIndex = 1
while (Test-Path -LiteralPath $backup) {
$backup = "$Target.bak.$stamp.$backupIndex"
$backupIndex += 1
}
Copy-Item -LiteralPath $Target -Destination $backup
Write-Host "Environment backup created: $backup"
}
[System.Collections.Generic.List[string]]$next = @()
if ($existingContent) {
$next.Add($existingContent.TrimEnd("`r", "`n"))
$next.Add("")
}
$next.Add("# Added automatically during QuantDinger install/update")
$next.AddRange($additions)
[System.IO.File]::WriteAllLines(
[System.IO.Path]::GetFullPath($Target),
$next,
[System.Text.UTF8Encoding]::new($false)
)
Write-Host "Added $($additions.Count) missing environment key(s) to $Target; existing values were preserved."
}
function New-HexSecret([int]$Bytes) {
$buffer = New-Object byte[] $Bytes
$rng = [System.Security.Cryptography.RandomNumberGenerator]::Create()
try {
$rng.GetBytes($buffer)
} finally {
$rng.Dispose()
}
return (($buffer | ForEach-Object { $_.ToString("x2") }) -join "")
}
function Read-Value($Prompt, $Default = "") {
if ($Default) {
$value = Read-Host "$Prompt [$Default]"
if (-not $value) { return $Default }
return $value
}
return (Read-Host $Prompt)
}
function Read-SecretPlain($Prompt) {
$secure = Read-Host $Prompt -AsSecureString
$ptr = [Runtime.InteropServices.Marshal]::SecureStringToBSTR($secure)
try {
return [Runtime.InteropServices.Marshal]::PtrToStringBSTR($ptr)
} finally {
[Runtime.InteropServices.Marshal]::ZeroFreeBSTR($ptr)
}
}
function Check-Prerequisites {
Write-Host "QuantDinger installer" -ForegroundColor Blue
Write-Host "Install directory: $InstallDir"
Write-Host "Source ref: $InstallRef"
Write-Host ""
if (-not (Get-Command docker -ErrorAction SilentlyContinue)) {
Fail "Docker is required. Install Docker Desktop first."
}
docker info *> $null
if ($LASTEXITCODE -ne 0) {
Fail "Docker is installed but the Docker daemon is not running."
}
docker compose version *> $null
if ($LASTEXITCODE -ne 0) {
Fail "Docker Compose v2 is required."
}
}
function Prepare-Directory {
New-Item -ItemType Directory -Force -Path $InstallDir | Out-Null
Set-Location $InstallDir
}
function Download-Files {
Write-Host "Downloading compose and unified environment template..." -ForegroundColor Yellow
Invoke-WebRequest -Uri "$GithubRaw/docker-compose.ghcr.yml" -OutFile $ComposeFile
Invoke-WebRequest -Uri "$GithubRaw/.env.example" -OutFile $EnvTemplate
Sync-MissingEnvKeys $EnvFile $EnvTemplate $LegacyEnv
Repair-EnvLayout $EnvFile @(
"FRONTEND_PORT",
"MOBILE_PORT",
"BACKEND_PORT",
"POSTGRES_PASSWORD",
"IMAGE_PREFIX"
)
}
function Collect-Settings {
$existingUser = Get-EnvValue $EnvFile "ADMIN_USER"
$existingPassword = Get-EnvValue $EnvFile "ADMIN_PASSWORD"
$script:AdminCredentialsReused = $false
if (
$existingPassword -and
$existingPassword -ne "123456" -and
$existingUser.Length -ge 3 -and
$existingUser.Length -le 50 -and
$existingPassword.Length -ge 6
) {
$script:AdminUser = $existingUser
$script:AdminPassword = $existingPassword
$script:AdminCredentialsReused = $true
Write-Host "Existing administrator credentials detected; keeping the configured username and password." -ForegroundColor Yellow
Write-Host "Change administrator credentials from Profile after signing in."
} else {
while ($true) {
$script:AdminUser = Read-Value "Admin username" ($existingUser -replace '^$', 'quantdinger')
if ($AdminUser.Length -lt 3 -or $AdminUser.Length -gt 50) {
Write-Host "Admin username must be 3-50 characters." -ForegroundColor Red
continue
}
break
}
while ($true) {
$pass1 = Read-SecretPlain "Admin password"
$pass2 = Read-SecretPlain "Confirm admin password"
if ($pass1.Length -lt 6) {
Write-Host "Admin password must be at least 6 characters." -ForegroundColor Red
continue
}
if ($pass1 -eq "123456") {
Write-Host "Do not use the built-in default password 123456." -ForegroundColor Red
continue
}
if ($pass1 -ne $pass2) {
Write-Host "Passwords do not match." -ForegroundColor Red
continue
}
$script:AdminPassword = $pass1
break
}
}
$script:AdminEmail = Read-Value "Admin email (optional)" (Get-EnvValue $EnvFile "ADMIN_EMAIL")
$script:FrontendPort = Read-Value "Frontend port" ((Get-EnvValue $EnvFile "FRONTEND_PORT") -replace '^$', '8888')
$script:MobilePort = Read-Value "Mobile H5 port" ((Get-EnvValue $EnvFile "MOBILE_PORT") -replace '^$', '8889')
$script:BackendPort = Read-Value "Backend bind address" ((Get-EnvValue $EnvFile "BACKEND_PORT") -replace '^$', '127.0.0.1:5000')
$existingPgPassword = Get-EnvValue $EnvFile "POSTGRES_PASSWORD"
if ($existingPgPassword) { $script:PostgresPassword = $existingPgPassword } else { $script:PostgresPassword = New-HexSecret 18 }
Write-Host ""
Write-Host "Image source:"
Write-Host " 1) global/default"
Write-Host " 2) mainland China mirror (docker.m.daocloud.io/library/)"
$choice = Read-Value "Select image source" "1"
$existingImagePrefix = Get-EnvValue $EnvFile "IMAGE_PREFIX"
if ($existingImagePrefix) {
$script:ImagePrefix = $existingImagePrefix
} elseif ($choice -eq "2") {
$script:ImagePrefix = "docker.m.daocloud.io/library/"
} else {
$script:ImagePrefix = ""
}
$existingSecret = Get-EnvValue $EnvFile "SECRET_KEY"
if ($existingSecret -and $existingSecret -ne "quantdinger-secret-key-change-me" -and $existingSecret.Length -ge 10) {
$script:SecretKey = $existingSecret
} else {
$script:SecretKey = New-HexSecret 32
}
}
function Write-Settings {
Set-EnvValue $EnvFile "SECRET_KEY" $SecretKey
Set-EnvValue $EnvFile "ADMIN_USER" $AdminUser
Set-EnvValue $EnvFile "ADMIN_PASSWORD" $AdminPassword
Set-EnvValue $EnvFile "ADMIN_EMAIL" $AdminEmail
Set-EnvValue $EnvFile "FRONTEND_URL" "http://localhost:$FrontendPort,http://localhost:$MobilePort"
Set-EnvValue $EnvFile "FRONTEND_PORT" $FrontendPort
Set-EnvValue $EnvFile "MOBILE_PORT" $MobilePort
Set-EnvValue $EnvFile "BACKEND_PORT" $BackendPort
Set-EnvValue $EnvFile "POSTGRES_PASSWORD" $PostgresPassword
Set-EnvValue $EnvFile "IMAGE_PREFIX" $ImagePrefix
}
function Start-Stack {
Write-Host "Pulling images..." -ForegroundColor Yellow
docker compose -f $ComposeFile pull
if ($LASTEXITCODE -ne 0) {
Fail "Docker Compose could not pull the required images."
}
Write-Host "Starting services..." -ForegroundColor Yellow
docker compose -f $ComposeFile up -d
if ($LASTEXITCODE -ne 0) {
Fail "Docker Compose could not start the services."
}
}
function Wait-ForBackend {
Write-Host "Waiting for backend health check..." -ForegroundColor Yellow
$apiPort = ($BackendPort -split ':')[-1]
$url = "http://127.0.0.1:$apiPort/api/health"
for ($i = 1; $i -le 45; $i++) {
try {
Invoke-WebRequest -Uri $url -UseBasicParsing -TimeoutSec 2 | Out-Null
Write-Host "Backend is ready." -ForegroundColor Green
return
} catch {
Write-Host " waiting... ($i/45)"
Start-Sleep -Seconds 2
}
}
Write-Host "Backend did not become healthy within the expected startup window. Check logs with:" -ForegroundColor Red
Write-Host " cd $InstallDir"
Write-Host " docker compose -f $ComposeFile logs -f backend"
Fail "Installation did not complete successfully."
}
function Print-Summary {
$apiPort = ($BackendPort -split ':')[-1]
Write-Host ""
Write-Host "QuantDinger is ready." -ForegroundColor Green
Write-Host ""
Write-Host "Web UI: http://localhost:$FrontendPort"
Write-Host "Mobile H5: http://localhost:$MobilePort"
Write-Host "API: http://127.0.0.1:$apiPort"
Write-Host "Directory: $InstallDir"
Write-Host "Username: $AdminUser"
if ($AdminCredentialsReused) {
Write-Host "Password: existing administrator password"
} else {
Write-Host "Password: the password you entered during installation"
}
Write-Host ""
Write-Host "Useful commands:"
Write-Host " cd $InstallDir"
Write-Host " docker compose -f $ComposeFile ps"
Write-Host " docker compose -f $ComposeFile logs -f backend"
Write-Host " Rerun this installer to update images and append newly added .env keys safely."
Write-Host ""
Write-Host "Trading involves substantial risk. Start with paper trading and small test accounts." -ForegroundColor Yellow
}
if ($env:QUANTDINGER_INSTALL_LIB_ONLY -ne "true") {
Check-Prerequisites
Prepare-Directory
Download-Files
Collect-Settings
Write-Settings
Start-Stack
Wait-ForBackend
Print-Summary
}