# deploy.ps1 -- Sawa Control Panel Deployment Script # Run from the project root: .\deploy.ps1 -All # Or specific steps: .\deploy.ps1 -Certs -Frontend -Backend param( [switch]$All, [switch]$Certs, [switch]$Backend, [switch]$Frontend, [switch]$Nginx, [switch]$Recipes ) # CONFIG $SERVER_IP = "10.0.0.10" $SERVER_USER = "root" $PROJECT_DIR = $PSScriptRoot $SERVER = "$SERVER_USER@$SERVER_IP" # HELPERS function Pass { param($msg) Write-Host " PASS: $msg" -ForegroundColor Green } function Fail { param($msg) Write-Host " FAIL: $msg" -ForegroundColor Red; exit 1 } function Step { param($msg) Write-Host "" ; Write-Host ">> $msg" -ForegroundColor Cyan } function Run { param($cmd) Invoke-Expression $cmd if ($LASTEXITCODE -ne 0) { Fail "Command failed: $cmd" } } function Deploy-Certs { Step "Deploying CA certificate to server..." $caFile = "$PROJECT_DIR\certs\ca.crt" if (-not (Test-Path $caFile)) { Fail "ca.crt not found. Run create-ca.sh first." } Run "ssh ${SERVER} 'mkdir -p /etc/nginx/ssl'" Run "scp `"$caFile`" ${SERVER}:/etc/nginx/ssl/ca.crt" Pass "CA certificate deployed" } function Deploy-Backend { Step "Bundling backend into single tar.gz..." $TMP_TAR = "$env:TEMP\sawa-backend.tar.gz" Push-Location "$PROJECT_DIR\backend" tar -czf $TMP_TAR --exclude="*.sh" --exclude="*.md" . if ($LASTEXITCODE -ne 0) { Fail "tar failed. Requires Windows 10 build 17063+" } Pass "Backend bundled" Pop-Location Step "Uploading archive to server (1 file)..." Run "scp `"$TMP_TAR`" ${SERVER}:/tmp/sawa-backend.tar.gz" Pass "Archive uploaded" Step "Extracting and restarting PM2 on server..." $remoteCmd = "mkdir -p /opt/sawa-panel && tar -xzf /tmp/sawa-backend.tar.gz -C /opt/sawa-panel && rm /tmp/sawa-backend.tar.gz && cd /opt/sawa-panel && npm install --omit=dev --silent && pm2 restart panel-api 2>/dev/null || pm2 start index.js --name panel-api && pm2 save" Run "ssh ${SERVER} `"$remoteCmd`"" Pass "Backend running via PM2" Remove-Item $TMP_TAR -ErrorAction SilentlyContinue } function Deploy-Frontend { Step "Building React frontend..." Push-Location "$PROJECT_DIR\frontend" Run "npm run build" Pass "Frontend built" Pop-Location Step "Bundling frontend dist into single tar.gz..." $TMP_FRONT = "$env:TEMP\sawa-frontend.tar.gz" Push-Location "$PROJECT_DIR\frontend\dist" tar -czf $TMP_FRONT . Pop-Location Pass "Frontend bundled" Step "Uploading and extracting on server (1 file)..." Run "scp `"$TMP_FRONT`" ${SERVER}:/tmp/sawa-frontend.tar.gz" $remoteCmd = "mkdir -p /var/www/panel && tar -xzf /tmp/sawa-frontend.tar.gz -C /var/www/panel && rm /tmp/sawa-frontend.tar.gz" Run "ssh ${SERVER} `"$remoteCmd`"" Pass "Frontend deployed to /var/www/panel/" Remove-Item $TMP_FRONT -ErrorAction SilentlyContinue } function Deploy-Nginx { Step "Deploying nginx config..." Run "scp `"$PROJECT_DIR\nginx\sawa-panel.conf`" ${SERVER}:/etc/nginx/conf.d/sawa-panel.conf" Pass "nginx config copied" Step "Testing and reloading nginx..." Run "ssh ${SERVER} `"nginx -t && rc-service nginx reload`"" Pass "nginx reloaded" } function Deploy-Recipes { Step "Deploying recipes..." Run "ssh ${SERVER} `"mkdir -p /opt/sawa-panel/recipes`"" Run "scp -r `"$PROJECT_DIR\backend\recipes\*`" ${SERVER}:/opt/sawa-panel/recipes/" Pass "Recipes deployed" } # MAIN Write-Host "============================================" -ForegroundColor Yellow Write-Host " Sawa Control Panel -- Deploy Script" -ForegroundColor Yellow Write-Host " Target: $SERVER" -ForegroundColor Yellow Write-Host "============================================" -ForegroundColor Yellow if (-not ($All -or $Certs -or $Backend -or $Frontend -or $Nginx -or $Recipes)) { Write-Host "" Write-Host "Usage:" Write-Host " .\deploy.ps1 -All # Full deployment" Write-Host " .\deploy.ps1 -Certs # Certs only" Write-Host " .\deploy.ps1 -Backend # Backend only" Write-Host " .\deploy.ps1 -Frontend # Frontend only" Write-Host " .\deploy.ps1 -Nginx # nginx config only" Write-Host " .\deploy.ps1 -Recipes # Recipes only" Write-Host " .\deploy.ps1 -Backend -Frontend # Backend + Frontend" Write-Host "" exit 0 } if ($All -or $Certs) { Deploy-Certs } if ($All -or $Nginx) { Deploy-Nginx } if ($All -or $Backend) { Deploy-Backend } if ($All -or $Frontend) { Deploy-Frontend } if ($All -or $Recipes) { Deploy-Recipes } Write-Host "" Write-Host "============================================" -ForegroundColor Green Write-Host " DEPLOYMENT COMPLETE!" -ForegroundColor Green Write-Host " Panel: https://$SERVER_IP" -ForegroundColor Green Write-Host "============================================" -ForegroundColor Green