# ============================================================================== # ClassNotify Caddy Configuration Deployment & Validation Script # ============================================================================== param ( [string]$VpsHost = "158.179.179.29", [string]$SshKey = "C:\Users\Administrator\.ssh\id_rsa_tokyo", [switch]$SyntaxFaultTest = $false, [switch]$UpstreamFaultTest = $false ) $ErrorActionPreference = "Stop" $root = $PSScriptRoot if (-not $root) { $root = Get-Location } Write-Host "==========================================================" -ForegroundColor Cyan Write-Host " ClassNotify Caddy Configuration Deployment Starting " -ForegroundColor Cyan Write-Host "==========================================================" -ForegroundColor Cyan $caddySnippet = Join-Path $root "caddy\classnotify.caddy" if (-not (Test-Path $caddySnippet)) { $caddySnippet = Join-Path $root "ops\caddy\classnotify.caddy" } if (-not (Test-Path $caddySnippet)) { throw "Caddy configuration snippet not found: $caddySnippet" } $deployScript = Join-Path $root "deploy_caddy.sh" if (-not (Test-Path $deployScript)) { $deployScript = Join-Path $root "ops\deploy_caddy.sh" } $stagingFile = Join-Path $root ".caddy_candidate" if (Test-Path $stagingFile) { Remove-Item $stagingFile -Force } if ($SyntaxFaultTest) { Write-Host "`n[FAULT INJECTION] Generating Candidate with INVALID SYNTAX..." -ForegroundColor Red $faultContent = @" classnotify.paradise.eu.org { INVALID_DIRECTIVE_BLOCK_SYNTAX_ERROR { unterminated block "@ [System.IO.File]::WriteAllText($stagingFile, $faultContent, [System.Text.Encoding]::UTF8) } elseif ($UpstreamFaultTest) { Write-Host "`n[FAULT INJECTION] Generating Candidate with WRONG UPSTREAM PORT (5999)..." -ForegroundColor Red $normalContent = [System.IO.File]::ReadAllText($caddySnippet, [System.Text.Encoding]::UTF8) $faultContent = $normalContent.Replace("127.0.0.1:5000", "127.0.0.1:5999") [System.IO.File]::WriteAllText($stagingFile, $faultContent, [System.Text.Encoding]::UTF8) } else { Copy-Item -Path $caddySnippet -Destination $stagingFile -Force } $snippetHash = (Get-FileHash $stagingFile -Algorithm SHA256).Hash.ToLower() Write-Host "Candidate snippet SHA256: $snippetHash" -ForegroundColor Cyan # 1. Upload candidate snippet and deploy script Write-Host "`n[1/3] Uploading candidate snippet and deploy script to VPS..." -ForegroundColor Yellow scp -i $SshKey -o StrictHostKeyChecking=no $stagingFile "root@${VpsHost}:/tmp/classnotify.caddy.candidate" scp -i $SshKey -o StrictHostKeyChecking=no $deployScript "root@${VpsHost}:/tmp/deploy_caddy.sh" if (Test-Path $stagingFile) { Remove-Item $stagingFile -Force } # 2. Execute remote deploy script Write-Host "`n[2/3] Executing candidate pre-validation and deployment on VPS..." -ForegroundColor Yellow $remoteCmd = "chmod +x /tmp/deploy_caddy.sh && /tmp/deploy_caddy.sh /tmp/classnotify.caddy.candidate" $prevEap = $ErrorActionPreference $ErrorActionPreference = "Continue" $caddyOutput = & ssh -i $SshKey -o StrictHostKeyChecking=no "root@${VpsHost}" $remoteCmd 2>&1 $caddyExitCode = $LASTEXITCODE $ErrorActionPreference = $prevEap Write-Host ($caddyOutput -join "`n") # 3. Interpret results based on test flags if ($SyntaxFaultTest) { if ($caddyExitCode -eq 31 -and ($caddyOutput -match "CADDY_VALIDATION=FAILED")) { Write-Host "`n[SYNTAX FAULT TEST VERIFIED] Invalid candidate rejected by caddy validate (exit code 31), production configuration untouched!" -ForegroundColor Green return } else { throw "Syntax fault test FAILED: Expected exit code 31, got $caddyExitCode" } } if ($UpstreamFaultTest) { if ($caddyExitCode -eq 11 -and ($caddyOutput -match "CADDY_ROLLBACK=SUCCESS") -and ($caddyOutput -match "FINAL_HEALTH=Healthy")) { Write-Host "`n[UPSTREAM FAULT TEST VERIFIED] Reload health gate failed, automated rollback executed, previous Caddy configuration restored!" -ForegroundColor Green return } elseif ($caddyExitCode -eq 21) { throw "CRITICAL: Caddy reload failed and rollback ALSO failed (exit code 21)!" } else { throw "Upstream fault test FAILED: Expected exit code 11 with verified rollback, got exit code $caddyExitCode" } } if ($caddyExitCode -eq 0) { Write-Host "`n[3/3] Caddy configuration deployed, validated, and verified successfully!" -ForegroundColor Green } else { throw "Caddy deployment failed with exit code $caddyExitCode" }