Skip to content

Commit 058b3ea

Browse files
committed
Refactor PowerShell script for Docker installation to improve variable handling and ensure proper resource disposal.
1 parent 191a130 commit 058b3ea

File tree

1 file changed

+31
-26
lines changed

1 file changed

+31
-26
lines changed

install_docker.ps1

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,12 @@ function Assert-Docker {
4343

4444
function New-RandomHex($bytes) {
4545
$buffer = New-Object byte[] $bytes
46-
[System.Security.Cryptography.RandomNumberGenerator]::Fill($buffer)
46+
$rng = [System.Security.Cryptography.RandomNumberGenerator]::Create()
47+
try {
48+
$rng.GetBytes($buffer)
49+
} finally {
50+
$rng.Dispose()
51+
}
4752
($buffer | ForEach-Object { $_.ToString('x2') }) -join ''
4853
}
4954

@@ -251,49 +256,49 @@ function Start-Stack($apiPort, $ui) {
251256
# Create convenience startup script for Windows
252257
$start = @(
253258
"Set-StrictMode -Version Latest",
254-
"$ErrorActionPreference = 'Stop'",
259+
"`$ErrorActionPreference = 'Stop'",
255260
"",
256261
'function Write-Info($msg) { Write-Host "[INFO] $msg" -ForegroundColor Cyan }',
257262
'function Write-Warn($msg) { Write-Host "[WARN] $msg" -ForegroundColor Yellow }',
258263
"",
259-
"$apiPortVar = \"$apiPort\"",
264+
"`$apiPortVar = `"$apiPort`"",
260265
"# Read desired port from morphik.toml if available",
261-
"$desired = $apiPortVar",
266+
"`$desired = `$apiPortVar",
262267
"if (Test-Path 'morphik.toml') {",
263-
" $lines = Get-Content morphik.toml",
264-
" $inApi = $false; $p = $null",
265-
" foreach ($l in $lines) {",
266-
" if ($l -match '^\\s*\\[api\\]\\s*$') { $inApi = $true; continue }",
267-
" if ($inApi -and $l -match '^\\s*\\[') { break }",
268-
" if ($inApi -and $l -match '^\\s*port\\s*=\\s*\"?(\\\\d+)\"?') {",
269-
" $p = $Matches[1]; break } }",
270-
" if ($p) { $desired = $p }",
268+
" `$lines = Get-Content morphik.toml",
269+
" `$inApi = `$false; `$p = `$null",
270+
" foreach (`$l in `$lines) {",
271+
" if (`$l -match '^\\s*\\[api\\]\\s*`$') { `$inApi = `$true; continue }",
272+
" if (`$inApi -and `$l -match '^\\s*\\[') { break }",
273+
" if (`$inApi -and `$l -match '^\\s*port\\s*=\\s*`"?(\\d+)`"?') {",
274+
" `$p = `$Matches[1]; break } }",
275+
" if (`$p) { `$desired = `$p }",
271276
"}",
272277
"",
273278
"# Update port mapping in compose file if needed",
274-
"$compose = Get-Content 'docker-compose.run.yml' -Raw",
275-
"if ($compose -match '\"(\\\d+):(\\\d+)\"') {",
276-
" $current = $Matches[1]",
277-
" if ($current -ne $desired) {",
278-
' $compose = $compose -replace '"' + $current + ':' + $current + '"', '"' + $desired + ':' + $desired + '"'",
279-
" Set-Content 'docker-compose.run.yml' -Value $compose -Encoding UTF8 } }",
279+
"`$compose = Get-Content 'docker-compose.run.yml' -Raw",
280+
"if (`$compose -match '`"(\\d+):(\\d+)`"') {",
281+
" `$current = `$Matches[1]",
282+
" if (`$current -ne `$desired) {",
283+
" `$compose = `$compose -replace `"`$current:`$current`", `"`$(`$desired):`$(`$desired)`"",
284+
" Set-Content 'docker-compose.run.yml' -Value `$compose -Encoding UTF8 } }",
280285
"",
281286
"# Warn if multimodal embeddings disabled",
282287
"if (Test-Path 'morphik.toml') {",
283-
" $cfg = Get-Content morphik.toml -Raw",
284-
" if ($cfg -match '(?m)^enable_colpali\\s*=\\s*false') {",
288+
" `$cfg = Get-Content morphik.toml -Raw",
289+
" if (`$cfg -match '(?m)^enable_colpali\\s*=\\s*false') {",
285290
" Write-Warn 'Multimodal embeddings are disabled. Enable in morphik.toml if you have a GPU.' } }",
286291
"",
287292
"# Include UI profile if installed",
288-
"$ui = $false",
293+
"`$ui = `$false",
289294
"if (Test-Path '.env') {",
290-
" $envText = Get-Content .env -Raw",
291-
" if ($envText -match 'UI_INSTALLED=true') { $ui = $true } }",
295+
" `$envText = Get-Content .env -Raw",
296+
" if (`$envText -match 'UI_INSTALLED=true') { `$ui = `$true } }",
292297
"",
293-
"$args = @('-f','docker-compose.run.yml')",
294-
"if ($ui) { $args += @('--profile','ui') }",
298+
"`$args = @('-f','docker-compose.run.yml')",
299+
"if (`$ui) { `$args += @('--profile','ui') }",
295300
"docker compose @args up -d",
296-
'Write-Host ("Morphik is running on http://localhost:${desired}")'
301+
"Write-Host `"Morphik is running on http://localhost:`$(`$desired)`""
297302
) -join [Environment]::NewLine
298303
Set-Content -Path 'start-morphik.ps1' -Value $start -Encoding UTF8
299304
}

0 commit comments

Comments
 (0)