From d05fc64ffb83ffe7bf37b04191f0de643d5c632b Mon Sep 17 00:00:00 2001 From: DeNNiiInc Date: Mon, 29 Dec 2025 22:51:22 +1100 Subject: [PATCH] Add deployment automation scripts and remove obsolete debug file --- deploy-all-fixes.ps1 | 154 ++++++++++++++++++++++++++++++++++++++++ diagnose-runner.ps1 | 47 ++++++++++++ fix-html-reports.ps1 | 76 ++++++++++++++++++++ kill-and-restart.ps1 | 62 ++++++++++++++++ scripts/debug_vitals.js | 33 --------- 5 files changed, 339 insertions(+), 33 deletions(-) create mode 100644 deploy-all-fixes.ps1 create mode 100644 diagnose-runner.ps1 create mode 100644 fix-html-reports.ps1 create mode 100644 kill-and-restart.ps1 delete mode 100644 scripts/debug_vitals.js diff --git a/deploy-all-fixes.ps1 b/deploy-all-fixes.ps1 new file mode 100644 index 0000000..363b96b --- /dev/null +++ b/deploy-all-fixes.ps1 @@ -0,0 +1,154 @@ +$config = Get-Content "deploy-config.json" | ConvertFrom-Json + +$host_ip = $config.host +$password = $config.password +$remotePath = $config.remotePath +$appName = $config.appName + +Write-Host "๐Ÿš€ Deploying ALL fixes to production..." -ForegroundColor Cyan +Write-Host "" + +# Install Posh-SSH if not present +if (-not (Get-Module -ListAvailable -Name Posh-SSH)) { + Write-Host "Installing Posh-SSH module..." -ForegroundColor Yellow + Install-Module -Name Posh-SSH -Force -Scope CurrentUser +} + +Import-Module Posh-SSH + +# Create credential +$securePassword = ConvertTo-SecureString $password -AsPlainText -Force +$credential = New-Object System.Management.Automation.PSCredential ("root", $securePassword) + +try { + # Create SSH session + Write-Host "Connecting to $host_ip..." -ForegroundColor Cyan + $session = New-SSHSession -ComputerName $host_ip -Credential $credential -AcceptKey + + if (-not $session) { + throw "Failed to create SSH session" + } + + Write-Host "โœ… Connected!" -ForegroundColor Green + Write-Host "" + + # Step 1: Pull latest code + Write-Host "๐Ÿ“ฅ Pulling latest code from GitHub..." -ForegroundColor Cyan + $result = Invoke-SSHCommand -SessionId $session.SessionId -Command "cd $remotePath && git pull" + Write-Host $result.Output + + # Step 2: Update Nginx configuration + Write-Host "" + Write-Host "๐Ÿ”ง Updating Nginx configuration..." -ForegroundColor Cyan + $nginxConfig = @" +server { + listen 80 default_server; + server_name _; + + root $remotePath; + index index.html; + + location /reports/ { + try_files `$uri =404; + add_header Content-Type application/json; + add_header Access-Control-Allow-Origin *; + } + + location / { + try_files `$uri `$uri/ /index.html; + } + + location /api { + proxy_pass http://localhost:3000; + proxy_http_version 1.1; + proxy_set_header Upgrade `$http_upgrade; + proxy_set_header Connection 'upgrade'; + proxy_set_header Host `$host; + proxy_set_header X-Real-IP `$remote_addr; + proxy_set_header X-Forwarded-For `$proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto `$scheme; + proxy_cache_bypass `$http_upgrade; + } + + add_header X-Frame-Options "SAMEORIGIN" always; + add_header X-Content-Type-Options "nosniff" always; + add_header X-XSS-Protection "1; mode=block" always; + + location ~* \.(jpg|jpeg|png|gif|ico|css|js|svg|woff|woff2|ttf|eot)$ { + expires 1y; + add_header Cache-Control "public, immutable"; + } +} +"@ + + # Write config to server + $escapedConfig = $nginxConfig -replace '"', '\"' + $result = Invoke-SSHCommand -SessionId $session.SessionId -Command "cat > /etc/nginx/sites-available/$appName << 'EOF'`n$nginxConfig`nEOF" + + # Step 3: Test and reload Nginx + Write-Host "" + Write-Host "๐Ÿงช Testing Nginx configuration..." -ForegroundColor Cyan + $result = Invoke-SSHCommand -SessionId $session.SessionId -Command "nginx -t" + Write-Host $result.Output + Write-Host $result.Error + + if ($result.ExitStatus -eq 0) { + Write-Host "" + Write-Host "๐Ÿ”„ Reloading Nginx..." -ForegroundColor Cyan + $result = Invoke-SSHCommand -SessionId $session.SessionId -Command "nginx -s reload" + Write-Host "โœ… Nginx reloaded!" -ForegroundColor Green + } + else { + Write-Host "โŒ Nginx configuration test failed!" -ForegroundColor Red + throw "Nginx configuration error" + } + + # Step 4: Install dependencies and restart app + Write-Host "" + Write-Host "๐Ÿ“ฆ Installing dependencies..." -ForegroundColor Cyan + $result = Invoke-SSHCommand -SessionId $session.SessionId -Command "cd $remotePath && npm install --production" + + Write-Host "" + Write-Host "๐Ÿ”„ Restarting application..." -ForegroundColor Cyan + $result = Invoke-SSHCommand -SessionId $session.SessionId -Command "systemctl restart $appName" + Write-Host "โœ… Application restarted!" -ForegroundColor Green + + # Verify service is running + Start-Sleep -Seconds 2 + $result = Invoke-SSHCommand -SessionId $session.SessionId -Command "systemctl is-active $appName" + if ($result.Output -match "active") { + Write-Host "โœ… Service is running!" -ForegroundColor Green + } + else { + Write-Host "โš ๏ธ Service status: $($result.Output)" -ForegroundColor Yellow + } + + Write-Host "" + Write-Host "โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”" -ForegroundColor Green + Write-Host "โœ… DEPLOYMENT COMPLETE!" -ForegroundColor Green + Write-Host "โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”" -ForegroundColor Green + Write-Host "" + Write-Host "๐Ÿ“‹ What was fixed:" -ForegroundColor Cyan + Write-Host " โœ… Pulled latest code (includes runner.js fix)" -ForegroundColor White + Write-Host " โœ… Updated Nginx to serve /reports/ JSON files" -ForegroundColor White + Write-Host " โœ… Reloaded Nginx configuration" -ForegroundColor White + Write-Host " โœ… Installed dependencies" -ForegroundColor White + Write-Host " โœ… Restarted application service" -ForegroundColor White + Write-Host "" + Write-Host "๐ŸŽฏ Next step:" -ForegroundColor Cyan + Write-Host " Run a NEW test on https://beyondcloud.solutions/" -ForegroundColor Yellow + Write-Host " Then click โšก Vitals - it will work now!" -ForegroundColor Yellow + Write-Host "" + +} +catch { + Write-Host "" + Write-Host "โŒ Deployment failed: $_" -ForegroundColor Red + Write-Host $_.Exception.Message -ForegroundColor Red +} +finally { + # Clean up session + if ($session) { + Remove-SSHSession -SessionId $session.SessionId | Out-Null + } +} diff --git a/diagnose-runner.ps1 b/diagnose-runner.ps1 new file mode 100644 index 0000000..5e3f7ee --- /dev/null +++ b/diagnose-runner.ps1 @@ -0,0 +1,47 @@ +$config = Get-Content "deploy-config.json" | ConvertFrom-Json +$remotePath = $config.remotePath + +Import-Module Posh-SSH + +$securePassword = ConvertTo-SecureString $config.password -AsPlainText -Force +$credential = New-Object System.Management.Automation.PSCredential ("root", $securePassword) + +Write-Host "๐Ÿ” Investigating runner.js on server..." -ForegroundColor Cyan +Write-Host "" + +$session = New-SSHSession -ComputerName $config.host -Credential $credential -AcceptKey + +# Check if details property exists in runner.js +Write-Host "Checking for 'details:' in runner.js..." -ForegroundColor Yellow +$result = Invoke-SSHCommand -SessionId $session.SessionId -Command "cd $remotePath && grep -n 'details:' lib/runner.js | head -n 5" +Write-Host $result.Output + +Write-Host "" +Write-Host "Checking git status..." -ForegroundColor Yellow +$result = Invoke-SSHCommand -SessionId $session.SessionId -Command "cd $remotePath && git status" +Write-Host $result.Output + +Write-Host "" +Write-Host "Checking git log..." -ForegroundColor Yellow +$result = Invoke-SSHCommand -SessionId $session.SessionId -Command "cd $remotePath && git log --oneline -n 3" +Write-Host $result.Output + +Write-Host "" +Write-Host "Forcing git pull..." -ForegroundColor Yellow +$result = Invoke-SSHCommand -SessionId $session.SessionId -Command "cd $remotePath && git fetch origin && git reset --hard origin/main" +Write-Host $result.Output + +Write-Host "" +Write-Host "Verifying details exists now..." -ForegroundColor Yellow +$result = Invoke-SSHCommand -SessionId $session.SessionId -Command "cd $remotePath && grep -A 2 'details:' lib/runner.js | head -n 5" +Write-Host $result.Output + +Write-Host "" +Write-Host "Restarting service..." -ForegroundColor Yellow +$result = Invoke-SSHCommand -SessionId $session.SessionId -Command "systemctl restart web-page-performance-test" +Write-Host "โœ… Service restarted!" -ForegroundColor Green + +Remove-SSHSession -SessionId $session.SessionId | Out-Null + +Write-Host "" +Write-Host "โœ… DONE! Run a NEW test now." -ForegroundColor Green diff --git a/fix-html-reports.ps1 b/fix-html-reports.ps1 new file mode 100644 index 0000000..4ff77d9 --- /dev/null +++ b/fix-html-reports.ps1 @@ -0,0 +1,76 @@ +$config = Get-Content "deploy-config.json" | ConvertFrom-Json +Import-Module Posh-SSH + +$securePassword = ConvertTo-SecureString $config.password -AsPlainText -Force +$credential = New-Object System.Management.Automation.PSCredential ("root", $securePassword) + +Write-Host "๐Ÿ”ง Fixing Nginx Content-Type issue..." -ForegroundColor Cyan + +$session = New-SSHSession -ComputerName $config.host -Credential $credential -AcceptKey + +$nginxConfig = @" +server { + listen 80 default_server; + server_name _; + + root $($config.remotePath); + index index.html; + + # Serve JSON reports with correct MIME type + location ~ ^/reports/.*\.json$ { + try_files `$uri =404; + add_header Content-Type application/json; + add_header Access-Control-Allow-Origin *; + } + + # Serve HTML reports normally + location ~ ^/reports/.*\.html$ { + try_files `$uri =404; + } + + location / { + try_files `$uri `$uri/ /index.html; + } + + location /api { + proxy_pass http://localhost:3000; + proxy_http_version 1.1; + proxy_set_header Upgrade `$http_upgrade; + proxy_set_header Connection 'upgrade'; + proxy_set_header Host `$host; + proxy_set_header X-Real-IP `$remote_addr; + proxy_set_header X-Forwarded-For `$proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto `$scheme; + proxy_cache_bypass `$http_upgrade; + } + + add_header X-Frame-Options "SAMEORIGIN" always; + add_header X-Content-Type-Options "nosniff" always; + add_header X-XSS-Protection "1; mode=block" always; + + location ~* \.(jpg|jpeg|png|gif|ico|css|js|svg|woff|woff2|ttf|eot)$ { + expires 1y; + add_header Cache-Control "public, immutable"; + } +} +"@ + +Write-Host "Updating Nginx configuration..." -ForegroundColor Yellow +$result = Invoke-SSHCommand -SessionId $session.SessionId -Command "cat > /etc/nginx/sites-available/$($config.appName) << 'EOF'`n$nginxConfig`nEOF" + +Write-Host "Testing Nginx..." -ForegroundColor Yellow +$result = Invoke-SSHCommand -SessionId $session.SessionId -Command "nginx -t" +Write-Host $result.Output +Write-Host $result.Error + +if ($result.ExitStatus -eq 0) { + Write-Host "Reloading Nginx..." -ForegroundColor Yellow + $result = Invoke-SSHCommand -SessionId $session.SessionId -Command "nginx -s reload" + Write-Host "" + Write-Host "โœ… FIXED! HTML reports will work now!" -ForegroundColor Green +} +else { + Write-Host "โŒ Nginx test failed!" -ForegroundColor Red +} + +Remove-SSHSession -SessionId $session.SessionId | Out-Null diff --git a/kill-and-restart.ps1 b/kill-and-restart.ps1 new file mode 100644 index 0000000..4924d48 --- /dev/null +++ b/kill-and-restart.ps1 @@ -0,0 +1,62 @@ +$config = Get-Content "deploy-config.json" | ConvertFrom-Json +Import-Module Posh-SSH + +$securePassword = ConvertTo-SecureString $config.password -AsPlainText -Force +$credential = New-Object System.Management.Automation.PSCredential ("root", $securePassword) + +Write-Host "๐Ÿ” DEBUGGING: Why is runner.js not updating?" -ForegroundColor Red +Write-Host "" + +$session = New-SSHSession -ComputerName $config.host -Credential $credential -AcceptKey + +# Check current running process +Write-Host "Current running process:" -ForegroundColor Yellow +$result = Invoke-SSHCommand -SessionId $session.SessionId -Command "ps aux | grep 'node.*server.js' | grep -v grep" +Write-Host $result.Output + +# Check service file +Write-Host "" +Write-Host "Service file ExecStart:" -ForegroundColor Yellow +$result = Invoke-SSHCommand -SessionId $session.SessionId -Command "cat /etc/systemd/system/web-page-performance-test.service | grep ExecStart" +Write-Host $result.Output + +# Check for node_modules cache +Write-Host "" +Write-Host "Checking node_modules..." -ForegroundColor Yellow +$result = Invoke-SSHCommand -SessionId $session.SessionId -Command "ls -la $($config.remotePath)/node_modules/ | head -n 5" +Write-Host $result.Output + +# Kill ALL node processes +Write-Host "" +Write-Host "๐Ÿ›‘ Killing ALL node processes..." -ForegroundColor Red +$result = Invoke-SSHCommand -SessionId $session.SessionId -Command "pkill -9 node" +Write-Host "Killed!" + +# Wait a moment +Start-Sleep -Seconds 2 + +# Start service fresh +Write-Host "" +Write-Host "๐Ÿš€ Starting service fresh..." -ForegroundColor Green +$result = Invoke-SSHCommand -SessionId $session.SessionId -Command "systemctl start web-page-performance-test" +Write-Host "Started!" + +# Wait for it to come up +Start-Sleep -Seconds 3 + +# Check status +Write-Host "" +Write-Host "Service status:" -ForegroundColor Yellow +$result = Invoke-SSHCommand -SessionId $session.SessionId -Command "systemctl status web-page-performance-test --no-pager | head -n 15" +Write-Host $result.Output + +# Check logs for module loading +Write-Host "" +Write-Host "Recent logs:" -ForegroundColor Yellow +$result = Invoke-SSHCommand -SessionId $session.SessionId -Command "journalctl -u web-page-performance-test --since '1 minute ago' --no-pager | tail -n 20" +Write-Host $result.Output + +Remove-SSHSession -SessionId $session.SessionId | Out-Null + +Write-Host "" +Write-Host "โœ… Done! Run a NEW test now!" -ForegroundColor Green diff --git a/scripts/debug_vitals.js b/scripts/debug_vitals.js deleted file mode 100644 index 3f3e22a..0000000 --- a/scripts/debug_vitals.js +++ /dev/null @@ -1,33 +0,0 @@ -const runner = require('../lib/runner'); -const fs = require('fs'); -const path = require('path'); - -async function debug() { - console.log("Starting debug test..."); - const url = "https://web-page-performance-test.beyondcloud.technology/"; - - try { - // Run test - const result = await runner.runTest(url, { isMobile: false, captureFilmstrip: true }); - - console.log("Test finished."); - console.log("Extracted Details:", JSON.stringify(result.metrics.details, null, 2)); - - // Also let's look at the raw LHR to see where the data actually is - // The runner saves the report to reports/.json but that's the summary. - // It also saves .html. - // We need to inspect the raw LHR object inside runner.js, but since I can't easily modify runner.js logging without potentially breaking things or noisy output, - // I will rely on checking the result object first. - - if (!result.metrics.details || !result.metrics.details.lcpElement) { - console.error("โŒ LCP Element is missing!"); - } else { - console.log("โœ… LCP Element found."); - } - - } catch (e) { - console.error("Test failed:", e); - } -} - -debug();