# ============================================================================== # ClassNotify Automated Server Deployment & Atomic Release Publisher # Target: Tokyo VPS (/var/www/classnotify/releases/) # Active: /var/www/classnotify/current -> releases/ # ============================================================================== param ( [string]$VpsHost = "158.179.179.29", [string]$SshKey = "C:\Users\Administrator\.ssh\id_rsa_tokyo", [switch]$BrokenTest = $false, [switch]$HashMismatchTest = $false, [switch]$SkipBuild = $false ) $ErrorActionPreference = "Stop" $dotnet = "C:\Program Files\dotnet\dotnet.exe" $root = $PSScriptRoot if (-not $root) { $root = Get-Location } Write-Host "==========================================================" -ForegroundColor Cyan Write-Host " ClassNotify Server Atomic Release Deployment Starting " -ForegroundColor Cyan Write-Host "==========================================================" -ForegroundColor Cyan $publishDir = Join-Path $root "publish_linux_v2" $timestamp = Get-Date -Format "yyyyMMdd-HHmmss" if (-not $SkipBuild) { Write-Host "`n[1/6] Compiling and publishing server for linux-arm64..." -ForegroundColor Yellow if (Test-Path $publishDir) { Remove-Item $publishDir -Recurse -Force } & $dotnet publish "$root\src\Server\ClassNotify.Server\ClassNotify.Server.csproj" ` -c Release ` -r linux-arm64 ` --self-contained true ` -o $publishDir if ($LASTEXITCODE -ne 0) { throw "Build/Publish failed with exit code $LASTEXITCODE" } } # 2. Package staging $packageStaging = Join-Path $root ".deploy_staging" if (Test-Path $packageStaging) { Remove-Item $packageStaging -Recurse -Force } Copy-Item -Path "$publishDir\*" -Destination (New-Item -ItemType Directory -Path $packageStaging -Force) -Recurse -Force if ($BrokenTest) { Write-Host "`n[FAULT INJECTION] Creating INTENTIONALLY BROKEN release package..." -ForegroundColor Red $targetDll = Join-Path $packageStaging "ClassNotify.Server.dll" # Corrupt the main assembly so Kestrel cannot launch [System.IO.File]::WriteAllBytes($targetDll, [System.Text.Encoding]::UTF8.GetBytes("INVALID_CORRUPTED_DLL_BINARY_HEADER_FOR_ROLLBACK_TEST")) } # 3. Compute SHA256 & Generate ReleaseId Write-Host "`n[2/6] Generating ReleaseId and compressing release tarball..." -ForegroundColor Yellow $dllFile = Join-Path $packageStaging "ClassNotify.Server.dll" $dllHash = (Get-FileHash $dllFile -Algorithm SHA256).Hash.Substring(0, 8) $releaseId = "$timestamp-$dllHash" if ($BrokenTest) { $releaseId = "${releaseId}-BROKEN" } if ($HashMismatchTest) { $releaseId = "${releaseId}-BADHASH" } Write-Host " ReleaseId: $releaseId" -ForegroundColor Cyan Set-Content -Path (Join-Path $packageStaging "release.txt") -Value $releaseId $archiveName = "server_arm64_${releaseId}.tar.gz" $archivePath = Join-Path $root $archiveName if (Test-Path $archivePath) { Remove-Item $archivePath -Force } tar -czf $archivePath -C $packageStaging . $archiveHash = (Get-FileHash $archivePath -Algorithm SHA256).Hash.ToLower() Write-Host " Archive: $archiveName (SHA256: $archiveHash)" -ForegroundColor Cyan # Determine expected hash to send to VPS $hashToSend = $archiveHash if ($HashMismatchTest) { Write-Host "`n[FAULT INJECTION] Sending deliberate MISMATCHED SHA256 to verify VPS rejection..." -ForegroundColor Red $hashToSend = "0000000000000000000000000000000000000000000000000000000000000000" } # 4. Upload deploy script and tarball to VPS (Systemd unit management separated to ops/Deploy-SystemdUnit.ps1) Write-Host "`n[3/6] Uploading release package and scripts to Tokyo VPS via SCP..." -ForegroundColor Yellow $deployScript = Join-Path $root "ops\deploy_server.sh" scp -i $SshKey -o StrictHostKeyChecking=no $deployScript "root@${VpsHost}:/tmp/deploy_server.sh" scp -i $SshKey -o StrictHostKeyChecking=no $archivePath "root@${VpsHost}:/tmp/$archiveName" # 5. Trigger Remote Deployment on VPS Write-Host "`n[4/6] Executing remote atomic deployment on VPS..." -ForegroundColor Yellow $remoteCmd = "chmod +x /tmp/deploy_server.sh && /tmp/deploy_server.sh '$releaseId' '/tmp/$archiveName' '$hashToSend'" $prevEap = $ErrorActionPreference $ErrorActionPreference = "Continue" $deployOutput = & ssh -i $SshKey -o StrictHostKeyChecking=no "root@${VpsHost}" $remoteCmd 2>&1 $deployExitCode = $LASTEXITCODE $ErrorActionPreference = $prevEap Write-Host ($deployOutput -join "`n") # Cleanup local temp files if (Test-Path $packageStaging) { Remove-Item $packageStaging -Recurse -Force } if (Test-Path $archivePath) { Remove-Item $archivePath -Force } # 6. Interpret result strictly based on exit code and semantic output if ($HashMismatchTest) { if ($deployExitCode -eq 30 -and ($deployOutput -match "release archive SHA256 mismatch")) { Write-Host "`n[HASH MISMATCH TEST VERIFIED] VPS rejected corrupted package (exit code 30), release was never unpacked, current unchanged!" -ForegroundColor Green return } else { throw "Hash mismatch test FAILED: Expected exit code 30, got $deployExitCode" } } if ($BrokenTest) { if ($deployExitCode -eq 10 -and ($deployOutput -match "ROLLBACK_STATUS=SUCCESS") -and ($deployOutput -match "FINAL_HEALTH=Healthy")) { Write-Host "`n[ROLLBACK TEST VERIFIED] Broken release deployment failed (exit code 10), automated rollback executed, previous release verified Healthy!" -ForegroundColor Green return } elseif ($deployExitCode -eq 20) { throw "CRITICAL: Deployment failed and automated rollback ALSO failed (exit code 20)!" } else { throw "Broken deployment test FAILED: Expected exit code 10 with verified rollback, got exit code $deployExitCode" } } if ($deployExitCode -eq 0) { Write-Host "`n[5/6] Tokyo VPS atomic deployment completed successfully!" -ForegroundColor Green } elseif ($deployExitCode -eq 10) { throw "DEPLOYMENT FAILED: Deployment failed on VPS and rolled back to previous release (exit code 10)" } elseif ($deployExitCode -eq 20) { throw "CRITICAL: Deployment failed on VPS and rollback ALSO failed (exit code 20)!" } elseif ($deployExitCode -eq 30) { throw "DEPLOYMENT FAILED: Package SHA256 mismatch rejected by VPS (exit code 30)" } else { throw "Deployment failed on VPS with exit code $deployExitCode" }