Skip to content

Commit ed981ba

Browse files
committed
feat(start-container): improve whatif support
1 parent 00d03fd commit ed981ba

2 files changed

Lines changed: 70 additions & 12 deletions

File tree

scripts/pwsh/devops/start-container.ps1

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ function Get-PlainTextFromSecure {
242242
}
243243

244244
function Initialize-DataPath {
245+
[CmdletBinding(SupportsShouldProcess = $true)]
245246
param(
246247
[string]$Path
247248
)
@@ -251,7 +252,14 @@ function Initialize-DataPath {
251252
elseif ($IsMacOS) { $Path = "/Volumes/Data/docker_data" }
252253
}
253254
try {
254-
if (-not (Test-Path -LiteralPath $Path)) { New-Item -ItemType Directory -Path $Path -Force | Out-Null }
255+
if (-not (Test-Path -LiteralPath $Path)) {
256+
if ($PSCmdlet.ShouldProcess($Path, 'Create data directory')) {
257+
New-Item -ItemType Directory -Path $Path -Force | Out-Null
258+
}
259+
else {
260+
return $Path
261+
}
262+
}
255263
$resolved = (Resolve-Path -LiteralPath $Path).Path
256264
return $resolved
257265
}
@@ -297,6 +305,7 @@ function Test-DockerAvailable {
297305
}
298306

299307
function Invoke-DockerCompose {
308+
[CmdletBinding(SupportsShouldProcess = $true)]
300309
param(
301310
[string]$File,
302311
[string]$Project,
@@ -308,7 +317,7 @@ function Invoke-DockerCompose {
308317
[hashtable]$Environment
309318
)
310319
$mode = $null
311-
if (-not $DryRun) { $mode = Test-DockerAvailable } else { $mode = 'sub' }
320+
if (-not $DryRun -and -not $WhatIfPreference) { $mode = Test-DockerAvailable } else { $mode = 'sub' }
312321
$dcArgs = @()
313322
if ($mode -eq 'sub') { $dcArgs += 'compose' }
314323
if ($File) { $dcArgs += @('-f', $File) }
@@ -317,9 +326,10 @@ function Invoke-DockerCompose {
317326
if ($Action) { $dcArgs += ($Action -split ' ') }
318327
if ($Services) { $dcArgs += $Services }
319328
if ($ExtraArgs) { $dcArgs += $ExtraArgs }
329+
$preview = if ($mode -eq 'sub') { "docker " + ($dcArgs -join ' ') } else { "docker-compose " + ($dcArgs -join ' ') }
320330
$invokeCompose = {
321331
if ($DryRun) {
322-
if ($mode -eq 'sub') { "docker " + ($dcArgs -join ' ') } else { "docker-compose " + ($dcArgs -join ' ') }
332+
$preview
323333
}
324334
elseif ($mode -eq 'sub') {
325335
& docker @dcArgs
@@ -329,6 +339,10 @@ function Invoke-DockerCompose {
329339
}
330340
}
331341

342+
if (-not $DryRun -and -not $PSCmdlet.ShouldProcess($preview, 'Execute Docker Compose')) {
343+
return
344+
}
345+
332346
if ($Environment -and $Environment.Count -gt 0) {
333347
return Invoke-WithScopedEnvironment -Variables $Environment -ScriptBlock $invokeCompose
334348
}
@@ -365,10 +379,18 @@ function Import-EnvFile {
365379
}
366380

367381
function New-NetworkIfMissing {
382+
[CmdletBinding(SupportsShouldProcess = $true)]
368383
param([string]$Name)
369384
if ([string]::IsNullOrWhiteSpace($Name)) { return }
370385
$exists = Get-NetworkExists -Name $Name
371-
if ([string]::IsNullOrWhiteSpace($exists)) { try { & docker network create $Name | Out-Null } catch {} }
386+
if ([string]::IsNullOrWhiteSpace($exists)) {
387+
try {
388+
if ($PSCmdlet.ShouldProcess($Name, 'Create Docker network')) {
389+
& docker network create $Name | Out-Null
390+
}
391+
}
392+
catch {}
393+
}
372394
}
373395

374396
function Wait-ServiceHealthy {
@@ -392,16 +414,21 @@ function Wait-ServiceHealthy {
392414
}
393415

394416
function Initialize-ServiceEnvironment {
417+
[CmdletBinding(SupportsShouldProcess = $true)]
395418
param([string]$ServiceName, [string]$DataPath)
396419
if ($ServiceName -eq 'rustfs') {
397420
$rustfsDataPath = Join-Path $DataPath "rustfs/data"
398421
# Ensure parent directory exists first
399422
$parent = Split-Path $rustfsDataPath -Parent
400423
if (-not (Test-Path -LiteralPath $parent)) {
401-
New-Item -ItemType Directory -Path $parent -Force | Out-Null
424+
if ($PSCmdlet.ShouldProcess($parent, 'Create RustFS parent directory')) {
425+
New-Item -ItemType Directory -Path $parent -Force | Out-Null
426+
}
402427
}
403428
if (-not (Test-Path -LiteralPath $rustfsDataPath)) {
404-
New-Item -ItemType Directory -Path $rustfsDataPath -Force | Out-Null
429+
if ($PSCmdlet.ShouldProcess($rustfsDataPath, 'Create RustFS data directory')) {
430+
New-Item -ItemType Directory -Path $rustfsDataPath -Force | Out-Null
431+
}
405432
}
406433

407434
if ($IsLinux) {
@@ -416,7 +443,9 @@ function Initialize-ServiceEnvironment {
416443

417444
if ($needsChown) {
418445
Write-Host "Setting ownership of $rustfsDataPath to 10001:10001 for RustFS..." -ForegroundColor Cyan
419-
& sudo chown -R 10001:10001 $rustfsDataPath
446+
if ($PSCmdlet.ShouldProcess($rustfsDataPath, 'Set RustFS directory ownership to 10001:10001')) {
447+
& sudo chown -R 10001:10001 $rustfsDataPath
448+
}
420449
}
421450
}
422451
catch {
@@ -816,7 +845,7 @@ try {
816845
if ([string]::IsNullOrWhiteSpace(${env:MONGO_USER})) { ${env:MONGO_USER} = $DefaultUser }
817846
if ([string]::IsNullOrWhiteSpace(${env:MONGO_PASSWORD})) { ${env:MONGO_PASSWORD} = $plainPwd }
818847
Invoke-DockerCompose -File $mongoReplComposePath -Project $projectName -Action 'up -d' -DryRun:$DryRun
819-
if (-not $DryRun) { [void](Wait-ServiceHealthy -Service 'mongo1' -Project $projectName) }
848+
if (-not $DryRun -and -not $WhatIfPreference) { [void](Wait-ServiceHealthy -Service 'mongo1' -Project $projectName) }
820849
return
821850
}
822851
if (Test-Path $composePath) {
@@ -833,7 +862,7 @@ try {
833862
if (-not [string]::IsNullOrWhiteSpace($databaseWarning)) { Write-Warning $databaseWarning }
834863
Invoke-DockerCompose -File $composePath -Project $projectName -Profiles $targetProfiles -Action 'pull' -Environment $composeEnvironment -DryRun:$DryRun
835864
Invoke-DockerCompose -File $composePath -Project $projectName -Profiles $targetProfiles -Action 'up -d' -Environment $composeEnvironment -DryRun:$DryRun
836-
if (-not $DryRun -and $ServiceName) {
865+
if (-not $DryRun -and -not $WhatIfPreference -and $ServiceName) {
837866
# 简单检查每个服务
838867
foreach ($tp in $targetProfiles) {
839868
[void](Wait-ServiceHealthy -Service $tp -Project $projectName)
@@ -849,7 +878,7 @@ try {
849878
$databaseWarning = Get-DatabaseStateWarningMessage -ServiceName $ServiceName -DataPath $composeEnvironment.DATA_PATH
850879
if (-not [string]::IsNullOrWhiteSpace($databaseWarning)) { Write-Warning $databaseWarning }
851880
Invoke-DockerCompose -File $composePath -Project $projectName -Profiles $targetProfiles -Action 'up -d' -ExtraArgs @('--pull', 'always') -Environment $composeEnvironment -DryRun:$DryRun
852-
if (-not $DryRun -and $ServiceName) {
881+
if (-not $DryRun -and -not $WhatIfPreference -and $ServiceName) {
853882
foreach ($tp in $targetProfiles) {
854883
[void](Wait-ServiceHealthy -Service $tp -Project $projectName)
855884
}
@@ -862,7 +891,7 @@ try {
862891
if (-not [string]::IsNullOrWhiteSpace($databaseWarning)) { Write-Warning $databaseWarning }
863892
Invoke-DockerCompose -File $composePath -Project $projectName -Profiles $targetProfiles -Action 'pull' -Environment $composeEnvironment -DryRun:$DryRun
864893
Invoke-DockerCompose -File $composePath -Project $projectName -Profiles $targetProfiles -Action 'up -d' -Environment $composeEnvironment -DryRun:$DryRun
865-
if (-not $DryRun -and $ServiceName) {
894+
if (-not $DryRun -and -not $WhatIfPreference -and $ServiceName) {
866895
foreach ($tp in $targetProfiles) {
867896
[void](Wait-ServiceHealthy -Service $tp -Project $projectName)
868897
}
@@ -901,7 +930,7 @@ try {
901930
$databaseWarning = Get-DatabaseStateWarningMessage -ServiceName $ServiceName -DataPath $composeEnvironment.DATA_PATH
902931
if (-not [string]::IsNullOrWhiteSpace($databaseWarning)) { Write-Warning $databaseWarning }
903932
Invoke-DockerCompose -File $composePath -Project $projectName -Profiles $targetProfiles -Action 'up -d' -Environment $composeEnvironment -DryRun:$DryRun
904-
if (-not $DryRun) {
933+
if (-not $DryRun -and -not $WhatIfPreference) {
905934
foreach ($tp in $targetProfiles) {
906935
[void](Wait-ServiceHealthy -Service $tp -Project $projectName)
907936
}

tests/StartContainer.ConfigIsolation.Tests.ps1

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,35 @@ Describe 'Invoke-DockerCompose' {
8181

8282
Test-Path Env:\DEFAULT_USER | Should -Be $false
8383
}
84+
85+
It 'does not leak scoped env values under WhatIf' {
86+
Remove-Item Env:\DEFAULT_USER -ErrorAction SilentlyContinue
87+
88+
$result = $null
89+
{
90+
$result = Invoke-DockerCompose `
91+
-File (Join-Path $TestDrive 'demo-compose.yml') `
92+
-Project 'demo-project' `
93+
-Profiles @('paradedb') `
94+
-Action 'up -d' `
95+
-Environment @{ DEFAULT_USER = 'postgres' } `
96+
-WhatIf
97+
} | Should -Not -Throw
98+
99+
$result | Should -BeNullOrEmpty
100+
Test-Path Env:\DEFAULT_USER | Should -Be $false
101+
}
102+
}
103+
104+
Describe 'Initialize-DataPath' {
105+
It 'does not create directories under WhatIf' {
106+
$path = Join-Path $TestDrive 'whatif-data-path'
107+
108+
$resolved = Initialize-DataPath -Path $path -WhatIf
109+
110+
$resolved | Should -Be $path
111+
Test-Path -LiteralPath $path | Should -Be $false
112+
}
84113
}
85114

86115
Describe 'Get-DatabaseStateWarningMessage' {

0 commit comments

Comments
 (0)