# Run Database Setup on Ubuntu Server # This script uploads files to Ubuntu Server and executes the setup $ErrorActionPreference = "Stop" Write-Host "========================================" -ForegroundColor Cyan Write-Host " HR Portal Database Setup Launcher" -ForegroundColor Cyan Write-Host "========================================" -ForegroundColor Cyan Write-Host "" $REMOTE_HOST = "ubuntu@10.1.0.254" $REMOTE_DIR = "/tmp/hr-portal-setup" $LOCAL_SCRIPT_DIR = "W:\DevOps-Workspace\hr-portal\scripts" # Step 1: Create remote directory Write-Host "[1/4] Preparing remote directory..." -ForegroundColor Yellow ssh $REMOTE_HOST "mkdir -p $REMOTE_DIR && chmod 755 $REMOTE_DIR" if ($LASTEXITCODE -eq 0) { Write-Host " [OK] Remote directory ready: $REMOTE_DIR" -ForegroundColor Green } else { Write-Host " [FAIL] Could not create remote directory" -ForegroundColor Red Write-Host "" Write-Host "Please ensure SSH key-based authentication is configured." -ForegroundColor Yellow Write-Host "Run: type `$env:USERPROFILE\.ssh\id_rsa.pub | ssh ubuntu@10.1.0.254 'cat >> ~/.ssh/authorized_keys'" -ForegroundColor Gray exit 1 } Write-Host "" # Step 2: Upload files Write-Host "[2/4] Uploading setup files..." -ForegroundColor Yellow $filesToUpload = @( "setup-hr-portal-db.sh", "init-db.sql", "insert-test-data.sql" ) foreach ($file in $filesToUpload) { $localPath = Join-Path $LOCAL_SCRIPT_DIR $file if (Test-Path $localPath) { Write-Host " Uploading $file..." -ForegroundColor Gray scp $localPath "${REMOTE_HOST}:${REMOTE_DIR}/" if ($LASTEXITCODE -ne 0) { Write-Host " [FAIL] Failed to upload $file" -ForegroundColor Red exit 1 } } else { Write-Host " [WARNING] File not found: $file" -ForegroundColor Yellow } } Write-Host " [OK] Files uploaded successfully" -ForegroundColor Green Write-Host "" # Step 3: Make script executable Write-Host "[3/4] Setting permissions..." -ForegroundColor Yellow ssh $REMOTE_HOST "chmod +x $REMOTE_DIR/setup-hr-portal-db.sh" if ($LASTEXITCODE -eq 0) { Write-Host " [OK] Script is executable" -ForegroundColor Green } else { Write-Host " [FAIL] Could not set permissions" -ForegroundColor Red exit 1 } Write-Host "" # Step 4: Execute setup script Write-Host "[4/4] Executing database setup on remote server..." -ForegroundColor Yellow Write-Host " You will be prompted for database passwords" -ForegroundColor Gray Write-Host "" ssh -t $REMOTE_HOST "cd $REMOTE_DIR && bash setup-hr-portal-db.sh" if ($LASTEXITCODE -eq 0) { Write-Host "" Write-Host "========================================" -ForegroundColor Cyan Write-Host " Database Setup Completed!" -ForegroundColor Cyan Write-Host "========================================" -ForegroundColor Cyan Write-Host "" Write-Host "Next steps:" -ForegroundColor Green Write-Host " 1. The connection string has been displayed above" -ForegroundColor White Write-Host " 2. Update W:\DevOps-Workspace\hr-portal\backend\.env" -ForegroundColor White Write-Host " 3. Run insert-test-data.sql for sample data (optional)" -ForegroundColor White Write-Host "" } else { Write-Host "" Write-Host "[ERROR] Setup script failed" -ForegroundColor Red Write-Host "Check the error messages above for details" -ForegroundColor Yellow exit 1 } # Cleanup prompt Write-Host "" $cleanup = Read-Host "Clean up remote setup files? (y/N)" if ($cleanup -eq "y" -or $cleanup -eq "Y") { Write-Host "Cleaning up..." -ForegroundColor Gray ssh $REMOTE_HOST "rm -rf $REMOTE_DIR" Write-Host " [OK] Cleanup complete" -ForegroundColor Green } Write-Host ""