168 lines
6.4 KiB
PowerShell
168 lines
6.4 KiB
PowerShell
# ScriptShare Production Deployment with Database (PowerShell)
|
|
|
|
Write-Host "🚀 Deploying ScriptShare with Database..." -ForegroundColor Green
|
|
|
|
# Check if Docker is available
|
|
try {
|
|
docker --version | Out-Null
|
|
} catch {
|
|
Write-Host "❌ Docker is not installed. Please install Docker Desktop first." -ForegroundColor Red
|
|
exit 1
|
|
}
|
|
|
|
# Check if Docker Compose is available
|
|
try {
|
|
docker compose version | Out-Null
|
|
} catch {
|
|
try {
|
|
docker-compose --version | Out-Null
|
|
} catch {
|
|
Write-Host "❌ Docker Compose is not available. Please install Docker Compose." -ForegroundColor Red
|
|
exit 1
|
|
}
|
|
}
|
|
|
|
# Check if environment file exists
|
|
if (-not (Test-Path "env.production.example")) {
|
|
Write-Host "❌ Environment example file 'env.production.example' not found." -ForegroundColor Red
|
|
exit 1
|
|
}
|
|
|
|
# Copy environment file if it doesn't exist
|
|
if (-not (Test-Path ".env")) {
|
|
Write-Host "📋 Creating .env file from example..." -ForegroundColor Cyan
|
|
Copy-Item "env.production.example" ".env"
|
|
Write-Host "⚠️ Please edit .env file with your production settings before continuing!" -ForegroundColor Yellow
|
|
Write-Host " - Update database passwords" -ForegroundColor Yellow
|
|
Write-Host " - Set your domain URL" -ForegroundColor Yellow
|
|
Write-Host " - Change JWT secret" -ForegroundColor Yellow
|
|
Read-Host "Press Enter after editing .env file"
|
|
}
|
|
|
|
# Create necessary directories
|
|
Write-Host "📁 Creating required directories..." -ForegroundColor Cyan
|
|
New-Item -ItemType Directory -Force -Path "logs" | Out-Null
|
|
New-Item -ItemType Directory -Force -Path "backups" | Out-Null
|
|
|
|
# Pull latest images
|
|
Write-Host "📥 Pulling Docker images..." -ForegroundColor Cyan
|
|
docker compose -f docker-compose.production.yml pull mysql:8.0
|
|
|
|
# Build application images
|
|
Write-Host "🔨 Building application images..." -ForegroundColor Cyan
|
|
docker compose -f docker-compose.production.yml build --no-cache
|
|
|
|
# Stop existing containers if running
|
|
Write-Host "🛑 Stopping existing containers..." -ForegroundColor Yellow
|
|
docker compose -f docker-compose.production.yml down
|
|
|
|
# Start the database first
|
|
Write-Host "🗄️ Starting database..." -ForegroundColor Cyan
|
|
docker compose -f docker-compose.production.yml up -d scriptshare-db
|
|
|
|
# Wait for database to be ready
|
|
Write-Host "⏳ Waiting for database to be ready..." -ForegroundColor Cyan
|
|
Start-Sleep -Seconds 20
|
|
|
|
# Check database health
|
|
Write-Host "🏥 Checking database health..." -ForegroundColor Cyan
|
|
$dbReady = $false
|
|
$attempts = 0
|
|
$maxAttempts = 30
|
|
|
|
while (-not $dbReady -and $attempts -lt $maxAttempts) {
|
|
try {
|
|
$result = docker compose -f docker-compose.production.yml exec -T scriptshare-db mysqladmin ping -h"localhost" -u"root" -p"ScriptShare_Root_2024_Secure" --silent 2>$null
|
|
if ($LASTEXITCODE -eq 0) {
|
|
$dbReady = $true
|
|
}
|
|
} catch {
|
|
# Continue waiting
|
|
}
|
|
|
|
if (-not $dbReady) {
|
|
Write-Host "Database is starting up - waiting..." -ForegroundColor Gray
|
|
Start-Sleep -Seconds 5
|
|
$attempts++
|
|
}
|
|
}
|
|
|
|
if (-not $dbReady) {
|
|
Write-Host "❌ Database failed to start within timeout period" -ForegroundColor Red
|
|
docker compose -f docker-compose.production.yml logs scriptshare-db
|
|
exit 1
|
|
}
|
|
|
|
Write-Host "✅ Database is ready!" -ForegroundColor Green
|
|
|
|
# Start API server
|
|
Write-Host "🚀 Starting API server..." -ForegroundColor Cyan
|
|
docker compose -f docker-compose.production.yml up -d scriptshare-api
|
|
|
|
# Wait for API to be ready
|
|
Write-Host "⏳ Waiting for API to be ready..." -ForegroundColor Cyan
|
|
Start-Sleep -Seconds 30
|
|
|
|
# Start frontend
|
|
Write-Host "🌐 Starting frontend..." -ForegroundColor Cyan
|
|
docker compose -f docker-compose.production.yml up -d scriptshare-frontend
|
|
|
|
# Wait for all services to be healthy
|
|
Write-Host "🏥 Checking service health..." -ForegroundColor Cyan
|
|
Start-Sleep -Seconds 30
|
|
|
|
# Check service status
|
|
Write-Host "📊 Checking service status..." -ForegroundColor Cyan
|
|
$services = @("scriptshare-db", "scriptshare-api", "scriptshare-frontend")
|
|
|
|
foreach ($service in $services) {
|
|
$status = docker compose -f docker-compose.production.yml ps | Select-String $service
|
|
if ($status -and $status.ToString() -match "Up") {
|
|
Write-Host "✅ $service is running" -ForegroundColor Green
|
|
} else {
|
|
Write-Host "❌ $service failed to start" -ForegroundColor Red
|
|
Write-Host "Checking logs for $service:" -ForegroundColor Yellow
|
|
docker compose -f docker-compose.production.yml logs $service
|
|
exit 1
|
|
}
|
|
}
|
|
|
|
# Display deployment information
|
|
Write-Host ""
|
|
Write-Host "🎉 ScriptShare deployment completed successfully!" -ForegroundColor Green
|
|
Write-Host ""
|
|
Write-Host "📊 Service URLs:" -ForegroundColor Cyan
|
|
|
|
# Get ports from .env file
|
|
$envContent = Get-Content ".env" -ErrorAction SilentlyContinue
|
|
$apiPort = "3001"
|
|
$frontendPort = "80"
|
|
|
|
if ($envContent) {
|
|
$apiPortLine = $envContent | Where-Object { $_ -match "API_PORT=" }
|
|
$frontendPortLine = $envContent | Where-Object { $_ -match "FRONTEND_PORT=" }
|
|
|
|
if ($apiPortLine) {
|
|
$apiPort = ($apiPortLine -split "=")[1].Trim('"')
|
|
}
|
|
if ($frontendPortLine) {
|
|
$frontendPort = ($frontendPortLine -split "=")[1].Trim('"')
|
|
}
|
|
}
|
|
|
|
Write-Host " Frontend: http://localhost:$frontendPort" -ForegroundColor White
|
|
Write-Host " API: http://localhost:$apiPort/api/health" -ForegroundColor White
|
|
Write-Host " Database: localhost:3306" -ForegroundColor White
|
|
Write-Host ""
|
|
Write-Host "🔧 Management commands:" -ForegroundColor Cyan
|
|
Write-Host " View logs: docker compose -f docker-compose.production.yml logs -f" -ForegroundColor Gray
|
|
Write-Host " Stop: docker compose -f docker-compose.production.yml down" -ForegroundColor Gray
|
|
Write-Host " Restart: docker compose -f docker-compose.production.yml restart" -ForegroundColor Gray
|
|
Write-Host " Database shell: docker compose -f docker-compose.production.yml exec scriptshare-db mysql -u scriptshare_user -p scriptshare" -ForegroundColor Gray
|
|
Write-Host ""
|
|
Write-Host "📝 Next steps:" -ForegroundColor Cyan
|
|
Write-Host " 1. Configure your domain DNS to point to this server" -ForegroundColor White
|
|
Write-Host " 2. Set up SSL/HTTPS if needed" -ForegroundColor White
|
|
Write-Host " 3. Configure automated backups" -ForegroundColor White
|
|
Write-Host " 4. Set up monitoring and alerting" -ForegroundColor White
|