# ============================================================================== # ClassNotify Systemd Unit Deployment & Verification Script # Target: Tokyo VPS (/etc/systemd/system/classnotify.service) # ============================================================================== param ( [string]$VpsHost = "158.179.179.29", [string]$SshKey = "C:\Users\Administrator\.ssh\id_rsa_tokyo", [switch]$FaultTest = $false, [switch]$SyntaxFaultTest = $false ) $ErrorActionPreference = "Stop" $root = $PSScriptRoot if (-not $root) { $root = Get-Location } Write-Host "==========================================================" -ForegroundColor Cyan Write-Host " ClassNotify Systemd Unit Deployment Starting " -ForegroundColor Cyan Write-Host "==========================================================" -ForegroundColor Cyan $sourceUnit = Join-Path $root "..\src\Server\classnotify.service" if (-not (Test-Path $sourceUnit)) { $sourceUnit = Join-Path $root "src\Server\classnotify.service" } if (-not (Test-Path $sourceUnit)) { throw "Source systemd unit file not found at: $sourceUnit" } $deployScript = Join-Path $root "deploy_systemd_unit.sh" if (-not (Test-Path $deployScript)) { $deployScript = Join-Path $root "ops\deploy_systemd_unit.sh" } if (-not (Test-Path $deployScript)) { throw "Deploy script not found at: $deployScript" } $stagingCandidate = Join-Path $root ".systemd_candidate" if (Test-Path $stagingCandidate) { Remove-Item $stagingCandidate -Force } if ($SyntaxFaultTest) { Write-Host "`n[FAULT INJECTION] Generating Candidate with INVALID SYNTAX..." -ForegroundColor Red $faultContent = @" INVALID_SECTION_DIRECTIVE_SYNTAX_ERROR MissingSection=true "@ [System.IO.File]::WriteAllText($stagingCandidate, $faultContent, [System.Text.Encoding]::UTF8) } elseif ($FaultTest) { Write-Host "`n[FAULT INJECTION] Generating Candidate with INVALID ExecStart (Broken Binary)..." -ForegroundColor Red $normalContent = [System.IO.File]::ReadAllText($sourceUnit, [System.Text.Encoding]::UTF8) $faultContent = $normalContent.Replace("ClassNotify.Server.dll", "ClassNotify.Broken.dll") [System.IO.File]::WriteAllText($stagingCandidate, $faultContent, [System.Text.Encoding]::UTF8) } else { Copy-Item -Path $sourceUnit -Destination $stagingCandidate -Force } $candidateHash = (Get-FileHash $stagingCandidate -Algorithm SHA256).Hash.ToUpper() Write-Host "Candidate unit SHA256: $candidateHash" -ForegroundColor Cyan # 1. Upload candidate unit and deploy script Write-Host "`n[1/3] Uploading candidate unit and deploy script to VPS..." -ForegroundColor Yellow scp -i $SshKey -o StrictHostKeyChecking=no $stagingCandidate "root@${VpsHost}:/tmp/classnotify_candidate.service" scp -i $SshKey -o StrictHostKeyChecking=no $deployScript "root@${VpsHost}:/tmp/deploy_systemd_unit.sh" if (Test-Path $stagingCandidate) { Remove-Item $stagingCandidate -Force } # 2. Execute remote deploy script Write-Host "`n[2/3] Executing candidate validation and deployment on VPS..." -ForegroundColor Yellow $remoteCmd = "chmod +x /tmp/deploy_systemd_unit.sh && /tmp/deploy_systemd_unit.sh /tmp/classnotify_candidate.service" $prevEap = $ErrorActionPreference $ErrorActionPreference = "Continue" $unitOutput = & ssh -i $SshKey -o StrictHostKeyChecking=no "root@${VpsHost}" $remoteCmd 2>&1 $unitExitCode = $LASTEXITCODE $ErrorActionPreference = $prevEap Write-Host ($unitOutput -join "`n") # 3. Interpret results based on test flags if ($SyntaxFaultTest) { if ($unitExitCode -eq 32 -and ($unitOutput -match "SYSTEMD_PREVALIDATION=FAILED")) { Write-Host "`n[SYNTAX FAULT TEST VERIFIED] Invalid candidate rejected by systemd-analyze verify (exit code 32), production unit untouched!" -ForegroundColor Green return } else { throw "Syntax fault test FAILED: Expected exit code 32, got $unitExitCode" } } if ($FaultTest) { if ($unitExitCode -eq 12 -and ($unitOutput -match "SYSTEMD_ROLLBACK=SUCCESS") -and ($unitOutput -match "FINAL_HEALTH=Healthy")) { Write-Host "`n[FAULT TEST VERIFIED] Activation health gate failed (exit code 12), automated rollback executed, previous unit restored and verified Healthy!" -ForegroundColor Green return } elseif ($unitExitCode -eq 32 -and ($unitOutput -match "SYSTEMD_PREVALIDATION=FAILED")) { Write-Host "`n[FAULT TEST VERIFIED] Pre-validation caught invalid candidate (exit code 32), production unit untouched and verified Healthy!" -ForegroundColor Green return } elseif ($unitExitCode -eq 22) { throw "CRITICAL: Systemd deployment failed and rollback ALSO failed (exit code 22)!" } else { throw "Fault test FAILED: Expected exit code 12 (or 32) with verified rollback, got exit code $unitExitCode" } } if ($unitExitCode -eq 0) { Write-Host "`n[3/3] Systemd unit deployed, reloaded, and verified successfully!" -ForegroundColor Green } else { throw "Systemd unit deployment failed with exit code $unitExitCode" }