diff --git a/ci/aspblogbrainordure-setup.ps1 b/ci/aspblogbrainordure-setup.ps1 new file mode 100644 index 0000000..9183669 --- /dev/null +++ b/ci/aspblogbrainordure-setup.ps1 @@ -0,0 +1,83 @@ +# aspblogbrainordure-setup.ps1 +# One-time setup: creates IIS site, app pool, and CI/CD scheduled task for ASPBlogBrainOrdure test site + +$SITENAME = "aspblogbrainordure-test" +$APPPOOL = "aspblogbrainordure-test" +$WEBROOT = "C:\inetpub\wwwroot\aspblogbrainordure-test" +$PORT = 8960 +$SCRIPTS = "C:\Scripts" + +Import-Module WebAdministration + +Write-Host "=== ASPBlogBrainOrdure Test Site Setup ===" + +# Create directories +New-Item -ItemType Directory -Force -Path $WEBROOT | Out-Null +New-Item -ItemType Directory -Force -Path $SCRIPTS | Out-Null +Write-Host "Directories created." + +# Create app pool (32-bit required for ACE OLEDB) +if (-not (Get-WebConfiguration "system.applicationHost/applicationPools/add[@name='$APPPOOL']")) { + New-WebAppPool -Name $APPPOOL + Write-Host "App pool $APPPOOL created." +} else { + Write-Host "App pool $APPPOOL already exists." +} +Set-ItemProperty "IIS:\AppPools\$APPPOOL" managedRuntimeVersion "v4.0" +Set-ItemProperty "IIS:\AppPools\$APPPOOL" enable32BitAppOnWin64 $true +Set-ItemProperty "IIS:\AppPools\$APPPOOL" startMode "AlwaysRunning" +Write-Host "App pool configured (32-bit, v4.0)." + +# Create IIS site +$existingSite = Get-Website -Name $SITENAME -ErrorAction SilentlyContinue +if ($existingSite) { + Write-Host "Site $SITENAME already exists — skipping creation." +} else { + New-Website -Name $SITENAME -PhysicalPath $WEBROOT -Port $PORT -ApplicationPool $APPPOOL + Write-Host "IIS site $SITENAME created on port $PORT." +} + +# Enable Parent Paths for ASP +Set-WebConfigurationProperty -Filter "system.webServer/asp" -PSPath "IIS:\Sites\$SITENAME" -Name "enableParentPaths" -Value $true +Write-Host "Parent paths enabled." + +# Create a placeholder Default.asp so the site doesn't 500 before first deploy +$placeholder = "<%Response.Write ""ASPBlogBrainOrdure - Awaiting first deploy..."" %>" +[System.IO.File]::WriteAllText("$WEBROOT\Default.asp", $placeholder) +Write-Host "Placeholder Default.asp created." + +# Copy the deploy script to Scripts folder +$deployScriptSrc = Split-Path $PSScriptRoot -Parent +$deployScript = "$SCRIPTS\deploy-aspblogbrainordure-test.ps1" + +# Write the deploy script inline (will be updated by CI/CD from repo) +# For now, copy from the repo path passed in or the current location +if (Test-Path "$PSScriptRoot\deploy-aspblogbrainordure-test.ps1") { + Copy-Item "$PSScriptRoot\deploy-aspblogbrainordure-test.ps1" $deployScript -Force + Write-Host "Deploy script copied to $deployScript." +} + +# Register scheduled task (runs every 5 minutes) +$taskName = "ASPBlogBrainOrdure-Test-Deploy" +$existing = Get-ScheduledTask -TaskName $taskName -ErrorAction SilentlyContinue +if ($existing) { + Unregister-ScheduledTask -TaskName $taskName -Confirm:$false + Write-Host "Removed existing scheduled task." +} +$action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-NonInteractive -ExecutionPolicy Bypass -File `"$deployScript`"" +$trigger = New-ScheduledTaskTrigger -RepetitionInterval (New-TimeSpan -Minutes 5) -Once -At (Get-Date) +$settings = New-ScheduledTaskSettingsSet -ExecutionTimeLimit (New-TimeSpan -Minutes 10) -MultipleInstances IgnoreNew +Register-ScheduledTask -TaskName $taskName -Action $action -Trigger $trigger -Settings $settings -RunLevel Highest -User "SYSTEM" | Out-Null +Write-Host "Scheduled task '$taskName' created (every 5 min)." + +# Run first deploy immediately +Write-Host "Running first deploy..." +& powershell.exe -NonInteractive -ExecutionPolicy Bypass -File $deployScript +Write-Host "First deploy complete." + +Write-Host "" +Write-Host "=== Setup complete ===" +Write-Host "Test site: http://win-rfvbip1nu8m:$PORT" +Write-Host "Webroot: $WEBROOT" +Write-Host "App Pool: $APPPOOL (32-bit, v4.0)" +Write-Host "CI/CD: $taskName (every 5 min)" diff --git a/ci/deploy-aspblogbrainordure-test.ps1 b/ci/deploy-aspblogbrainordure-test.ps1 new file mode 100644 index 0000000..96ac5ba --- /dev/null +++ b/ci/deploy-aspblogbrainordure-test.ps1 @@ -0,0 +1,134 @@ +# deploy-aspblogbrainordure-test.ps1 +# Polls Gitea for new commits on master; deploys ASPBlogBrainOrdure to test site if changed +# Scheduled Task: ASPBlogBrainOrdure-Test-Deploy (every 5 minutes) + +$GITEA = "https://onefortheroadgit.sytes.net" +$REPO = "dcovington/ASPBlogBrainOrdure" +$TOKEN = "bac7c4befba3f0428e8786020cddb5e9595a6838" +$WEBROOT = "C:\inetpub\wwwroot\aspblogbrainordure-test" +$APPPOOL = "aspblogbrainordure-test" +$STATEFILE = "C:\Scripts\.aspblog-last-commit" +$LOGFILE = "C:\Scripts\aspblog-deploy.log" + +function Log($msg) { + $ts = (Get-Date).ToString("yyyy-MM-dd HH:mm:ss") + "$ts $msg" | Out-File -Append -FilePath $LOGFILE + Write-Host "$ts $msg" +} + +# SSL bypass for self-signed cert +Add-Type -TypeDefinition "using System.Net; using System.Security.Cryptography.X509Certificates; public class TrustAll : ICertificatePolicy { public bool CheckValidationResult(ServicePoint sp, X509Certificate cert, WebRequest req, int problem) { return true; } }" +[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAll +[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12 + +Log "Checking for new commits on $REPO master..." + +# Get current master commit hash +try { + $headers = @{ "Authorization" = "token $TOKEN" } + $branchUrl = "$GITEA/api/v1/repos/$REPO/branches/master" + $resp = Invoke-WebRequest -Uri $branchUrl -Headers $headers -UseBasicParsing + $branch = $resp.Content | ConvertFrom-Json + $latestCommit = $branch.commit.id +} catch { + Log "ERROR fetching branch info: $_" + exit 1 +} + +# Compare with last deployed commit +$lastCommit = "" +if (Test-Path $STATEFILE) { $lastCommit = (Get-Content $STATEFILE -Raw).Trim() } + +if ($latestCommit -eq $lastCommit) { + Log "No changes (commit $($latestCommit.Substring(0,8))). Nothing to do." + exit 0 +} + +Log "New commit detected: $($latestCommit.Substring(0,8)). Deploying..." + +# Download archive +$archiveUrl = "$GITEA/api/v1/repos/$REPO/archive/master.zip" +$zipPath = "C:\Scripts\aspblog-deploy.zip" +try { + $wc = New-Object System.Net.WebClient + $wc.Headers.Add("Authorization", "token $TOKEN") + $wc.DownloadFile($archiveUrl, $zipPath) + Log "Downloaded archive." +} catch { + Log "ERROR downloading archive: $_" + exit 1 +} + +# Extract to temp folder +$tmpDir = "C:\Scripts\aspblog-tmp" +if (Test-Path $tmpDir) { Remove-Item $tmpDir -Recurse -Force } +New-Item -ItemType Directory -Path $tmpDir | Out-Null +Add-Type -AssemblyName System.IO.Compression.FileSystem +[System.IO.Compression.ZipFile]::ExtractToDirectory($zipPath, $tmpDir) + +# Find the extracted subfolder (Gitea puts files in a subfolder) +$extracted = Get-ChildItem $tmpDir -Directory | Select-Object -First 1 +if (-not $extracted) { Log "ERROR: No directory found in archive."; exit 1 } +$srcPath = $extracted.FullName + +Log "Extracted to $srcPath" + +# Copy public/ folder contents to webroot (this is the IIS-served content) +$publicSrc = Join-Path $srcPath "public" +if (Test-Path $publicSrc) { + # Copy all files from public/ to webroot root + Copy-Item "$publicSrc\*" $WEBROOT -Recurse -Force + Log "Copied public/ to webroot." +} else { + # If no public folder, copy everything + Copy-Item "$srcPath\*" $WEBROOT -Recurse -Force + Log "Copied all files to webroot." +} + +# Copy core/, app/ alongside Default.asp (needed for ../core/ relative includes) +foreach ($folder in @("core", "app", "db")) { + $folderSrc = Join-Path $srcPath $folder + $folderDst = Join-Path $WEBROOT $folder + if (Test-Path $folderSrc) { + if (Test-Path $folderDst) { Remove-Item $folderDst -Recurse -Force } + Copy-Item $folderSrc $WEBROOT -Recurse -Force + Log "Copied $folder/ to webroot." + } +} + +# Update web.config ConnectionString to point to correct DB path +$webConfigPath = Join-Path $WEBROOT "web.config" +if (Test-Path $webConfigPath) { + $wc = [System.IO.File]::ReadAllText($webConfigPath) + $newConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=$WEBROOT\db\webdata.accdb;Persist Security Info=False;" + $wc = [regex]::Replace($wc, 'Provider=Microsoft\.ACE\.OLEDB[^"]+', $newConn) + [System.IO.File]::WriteAllText($webConfigPath, $wc) + Log "Updated web.config ConnectionString." +} + +# Grant IIS_IUSRS write access to db folder +$dbPath = Join-Path $WEBROOT "db" +if (Test-Path $dbPath) { + $acl = Get-Acl $dbPath + $rule = New-Object System.Security.AccessControl.FileSystemAccessRule("IIS_IUSRS", "Modify", "ContainerInherit,ObjectInherit", "None", "Allow") + $acl.SetAccessRule($rule) + Set-Acl $dbPath $acl + Log "Set IIS_IUSRS Modify on db folder." +} + +# Recycle app pool +Import-Module WebAdministration -ErrorAction SilentlyContinue +try { + Restart-WebAppPool $APPPOOL + Log "Recycled app pool $APPPOOL." +} catch { + Log "WARNING: Could not recycle app pool: $_" +} + +# Cleanup +Remove-Item $tmpDir -Recurse -Force +Remove-Item $zipPath -Force + +# Save new commit hash +$latestCommit | Out-File -FilePath $STATEFILE -NoNewline +Log "Deploy complete. Commit: $($latestCommit.Substring(0,8))"