feat: Complete production deployment pipeline with admin dashboard
Some checks failed
ParentFlow CI/CD Pipeline / Backend Tests (push) Has been cancelled
ParentFlow CI/CD Pipeline / Frontend Tests (push) Has been cancelled
ParentFlow CI/CD Pipeline / Security Scanning (push) Has been cancelled
ParentFlow CI/CD Pipeline / Build Docker Images (map[context:maternal-app/maternal-app-backend dockerfile:Dockerfile.production name:backend]) (push) Has been cancelled
ParentFlow CI/CD Pipeline / Build Docker Images (map[context:maternal-web dockerfile:Dockerfile.production name:frontend]) (push) Has been cancelled
ParentFlow CI/CD Pipeline / Deploy to Development (push) Has been cancelled
ParentFlow CI/CD Pipeline / Deploy to Production (push) Has been cancelled
CI/CD Pipeline / Lint and Test (push) Has been cancelled
CI/CD Pipeline / E2E Tests (push) Has been cancelled
CI/CD Pipeline / Build Application (push) Has been cancelled
Some checks failed
ParentFlow CI/CD Pipeline / Backend Tests (push) Has been cancelled
ParentFlow CI/CD Pipeline / Frontend Tests (push) Has been cancelled
ParentFlow CI/CD Pipeline / Security Scanning (push) Has been cancelled
ParentFlow CI/CD Pipeline / Build Docker Images (map[context:maternal-app/maternal-app-backend dockerfile:Dockerfile.production name:backend]) (push) Has been cancelled
ParentFlow CI/CD Pipeline / Build Docker Images (map[context:maternal-web dockerfile:Dockerfile.production name:frontend]) (push) Has been cancelled
ParentFlow CI/CD Pipeline / Deploy to Development (push) Has been cancelled
ParentFlow CI/CD Pipeline / Deploy to Production (push) Has been cancelled
CI/CD Pipeline / Lint and Test (push) Has been cancelled
CI/CD Pipeline / E2E Tests (push) Has been cancelled
CI/CD Pipeline / Build Application (push) Has been cancelled
- Add unified deployment script with Node.js 22 installation - Create comprehensive database migration script (28 migrations + admin tables) - Add production start/stop scripts for all services - Integrate admin dashboard (parentflow-admin) into PM2 ecosystem - Configure all services: Backend (3020), Frontend (3030), Admin (3335) - Update ecosystem.config.js with admin dashboard configuration - Add invite codes module for user registration management
This commit is contained in:
@@ -1,235 +1,214 @@
|
||||
#!/bin/bash
|
||||
|
||||
# ParentFlow Production Startup Script
|
||||
# Uses PM2 for frontend/backend, Docker for databases
|
||||
# Ports: Backend 3020, Frontend 3030
|
||||
# Server: 10.0.0.240 (or localhost for local production testing)
|
||||
# ParentFlow Production Start Script
|
||||
# Starts all production services including backend, frontend, and admin dashboard
|
||||
|
||||
set -e # Exit on any error
|
||||
set -e
|
||||
|
||||
# Configuration
|
||||
DEPLOY_DIR="/root/parentflow-production"
|
||||
DB_HOST="10.0.0.207"
|
||||
DB_PORT="5432"
|
||||
|
||||
# Color codes
|
||||
RED='\033[0;31m'
|
||||
GREEN='\033[0;32m'
|
||||
YELLOW='\033[1;33m'
|
||||
BLUE='\033[0;34m'
|
||||
NC='\033[0m' # No Color
|
||||
CYAN='\033[0;36m'
|
||||
NC='\033[0m'
|
||||
|
||||
log() {
|
||||
echo -e "${BLUE}[$(date +'%Y-%m-%d %H:%M:%S')]${NC} $1"
|
||||
}
|
||||
|
||||
error() {
|
||||
echo -e "${RED}[ERROR]${NC} $1" >&2
|
||||
exit 1
|
||||
}
|
||||
|
||||
success() {
|
||||
echo -e "${GREEN}✓${NC} $1"
|
||||
}
|
||||
|
||||
warning() {
|
||||
echo -e "${YELLOW}⚠${NC} $1"
|
||||
}
|
||||
|
||||
# Header
|
||||
echo ""
|
||||
echo "=========================================="
|
||||
echo "ParentFlow Production Startup"
|
||||
echo " Starting ParentFlow Production "
|
||||
echo "=========================================="
|
||||
echo ""
|
||||
|
||||
# Check if PM2 is installed
|
||||
if ! command -v pm2 &> /dev/null; then
|
||||
echo -e "${RED}ERROR: PM2 is not installed!${NC}"
|
||||
echo "Install PM2 globally with: npm install -g pm2"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Check if Docker is installed
|
||||
if ! command -v docker &> /dev/null; then
|
||||
echo -e "${RED}ERROR: Docker is not installed!${NC}"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Check if docker-compose is installed
|
||||
if ! command -v docker-compose &> /dev/null && ! docker compose version &> /dev/null; then
|
||||
echo -e "${RED}ERROR: Docker Compose is not installed!${NC}"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Step 1: Start Docker services (databases)
|
||||
echo -e "${BLUE}Step 1: Starting Docker services (databases)...${NC}"
|
||||
if docker compose version &> /dev/null; then
|
||||
docker compose -f docker-compose.production.yml up -d
|
||||
else
|
||||
docker-compose -f docker-compose.production.yml up -d
|
||||
# Check if we're in the right directory
|
||||
if [ "$PWD" != "$DEPLOY_DIR" ] && [ -d "$DEPLOY_DIR" ]; then
|
||||
cd "$DEPLOY_DIR"
|
||||
fi
|
||||
|
||||
# Step 1: Check database connectivity
|
||||
log "${CYAN}Step 1: Checking database connectivity...${NC}"
|
||||
PGPASSWORD=a3ppq psql -h $DB_HOST -p $DB_PORT -U postgres -d parentflow \
|
||||
-c "SELECT version();" > /dev/null 2>&1
|
||||
if [ $? -eq 0 ]; then
|
||||
echo -e "${GREEN}✓ Docker services started${NC}"
|
||||
success "Database connection successful"
|
||||
else
|
||||
echo -e "${RED}✗ Failed to start Docker services${NC}"
|
||||
exit 1
|
||||
error "Cannot connect to database at $DB_HOST:$DB_PORT"
|
||||
fi
|
||||
|
||||
# Wait for databases to be ready
|
||||
echo -e "${YELLOW}Waiting for databases to be healthy...${NC}"
|
||||
sleep 10
|
||||
|
||||
# Check PostgreSQL connectivity (dedicated server)
|
||||
echo -e "${BLUE}Checking PostgreSQL connectivity on 10.0.0.207...${NC}"
|
||||
PGPASSWORD=a3ppq psql -h 10.0.0.207 -p 5432 -U postgres -d parentflow -c "SELECT version();" > /dev/null 2>&1
|
||||
if [ $? -eq 0 ]; then
|
||||
echo -e "${GREEN}✓ PostgreSQL connection successful${NC}"
|
||||
else
|
||||
echo -e "${RED}✗ Cannot connect to PostgreSQL on 10.0.0.207${NC}"
|
||||
echo "Please ensure PostgreSQL is running and accessible"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Check Docker services health
|
||||
echo -e "${BLUE}Checking Docker services health...${NC}"
|
||||
MAX_RETRIES=30
|
||||
RETRY_COUNT=0
|
||||
|
||||
while [ $RETRY_COUNT -lt $MAX_RETRIES ]; do
|
||||
REDIS_HEALTHY=$(docker inspect parentflow-redis-prod --format='{{.State.Health.Status}}' 2>/dev/null || echo "starting")
|
||||
MONGO_HEALTHY=$(docker inspect parentflow-mongodb-prod --format='{{.State.Health.Status}}' 2>/dev/null || echo "starting")
|
||||
MINIO_HEALTHY=$(docker inspect parentflow-minio-prod --format='{{.State.Health.Status}}' 2>/dev/null || echo "starting")
|
||||
|
||||
if [ "$REDIS_HEALTHY" = "healthy" ] && [ "$MONGO_HEALTHY" = "healthy" ] && [ "$MINIO_HEALTHY" = "healthy" ]; then
|
||||
echo -e "${GREEN}✓ All Docker services are healthy${NC}"
|
||||
break
|
||||
# Step 2: Start Docker services
|
||||
log "${CYAN}Step 2: Starting Docker services...${NC}"
|
||||
if [ -f "docker-compose.production.yml" ]; then
|
||||
if docker compose version &> /dev/null; then
|
||||
docker compose -f docker-compose.production.yml up -d
|
||||
else
|
||||
docker-compose -f docker-compose.production.yml up -d
|
||||
fi
|
||||
|
||||
echo -e "${YELLOW}Waiting for Docker services... ($RETRY_COUNT/$MAX_RETRIES)${NC}"
|
||||
sleep 2
|
||||
((RETRY_COUNT++))
|
||||
done
|
||||
|
||||
if [ $RETRY_COUNT -eq $MAX_RETRIES ]; then
|
||||
echo -e "${RED}✗ Docker services failed to become healthy${NC}"
|
||||
echo "Check Docker logs with: docker logs parentflow-redis-prod"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Step 2: Run database migrations
|
||||
echo ""
|
||||
echo -e "${BLUE}Step 2: Running database migrations...${NC}"
|
||||
cd /root/maternal-app/maternal-app/maternal-app-backend
|
||||
|
||||
# Check if migration script exists
|
||||
if [ -f "./scripts/master-migration.sh" ]; then
|
||||
echo -e "${YELLOW}Running master migration script...${NC}"
|
||||
DATABASE_HOST=10.0.0.207 \
|
||||
DATABASE_PORT=5432 \
|
||||
DATABASE_NAME=parentflow \
|
||||
DATABASE_USER=postgres \
|
||||
DATABASE_PASSWORD=a3ppq \
|
||||
./scripts/master-migration.sh || {
|
||||
echo -e "${YELLOW}Warning: Migrations may have partially failed. Continuing...${NC}"
|
||||
}
|
||||
sleep 5
|
||||
success "Docker services started (Redis, MongoDB, MinIO)"
|
||||
else
|
||||
echo -e "${YELLOW}Warning: Migration script not found. Skipping migrations.${NC}"
|
||||
warning "Docker compose file not found, skipping..."
|
||||
fi
|
||||
|
||||
# Step 3: Build backend (if needed)
|
||||
echo ""
|
||||
echo -e "${BLUE}Step 3: Building backend...${NC}"
|
||||
cd /root/maternal-app/maternal-app/maternal-app-backend
|
||||
# Step 3: Verify Docker services
|
||||
log "${CYAN}Step 3: Verifying Docker services...${NC}"
|
||||
docker ps --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}" | grep -E "redis|mongo|minio" || warning "Some Docker services may not be running"
|
||||
|
||||
if [ ! -d "dist" ]; then
|
||||
echo -e "${YELLOW}Building backend for the first time...${NC}"
|
||||
npm run build
|
||||
if [ $? -ne 0 ]; then
|
||||
echo -e "${RED}✗ Backend build failed${NC}"
|
||||
exit 1
|
||||
fi
|
||||
echo -e "${GREEN}✓ Backend built successfully${NC}"
|
||||
else
|
||||
echo -e "${GREEN}✓ Backend dist directory exists${NC}"
|
||||
fi
|
||||
# Step 4: Start PM2 processes
|
||||
log "${CYAN}Step 4: Starting PM2 application services...${NC}"
|
||||
|
||||
# Step 4: Build frontend (if needed)
|
||||
echo ""
|
||||
echo -e "${BLUE}Step 4: Building frontend...${NC}"
|
||||
cd /root/maternal-app/maternal-web
|
||||
|
||||
if [ ! -d ".next" ]; then
|
||||
echo -e "${YELLOW}Building frontend for the first time...${NC}"
|
||||
npm run build
|
||||
if [ $? -ne 0 ]; then
|
||||
echo -e "${RED}✗ Frontend build failed${NC}"
|
||||
exit 1
|
||||
fi
|
||||
echo -e "${GREEN}✓ Frontend built successfully${NC}"
|
||||
else
|
||||
echo -e "${GREEN}✓ Frontend .next directory exists${NC}"
|
||||
fi
|
||||
|
||||
# Step 5: Start PM2 processes
|
||||
echo ""
|
||||
echo -e "${BLUE}Step 5: Starting PM2 processes...${NC}"
|
||||
|
||||
# Check if ecosystem.config.js exists
|
||||
if [ ! -f "/root/maternal-app/ecosystem.config.js" ]; then
|
||||
echo -e "${RED}✗ ecosystem.config.js not found${NC}"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Stop any existing PM2 processes
|
||||
echo -e "${YELLOW}Stopping any existing PM2 processes...${NC}"
|
||||
# Delete any existing PM2 processes
|
||||
pm2 delete all 2>/dev/null || true
|
||||
|
||||
# Start PM2 with ecosystem config (production environment)
|
||||
cd /root/maternal-app
|
||||
pm2 start ecosystem.config.js --env production
|
||||
|
||||
if [ $? -eq 0 ]; then
|
||||
echo -e "${GREEN}✓ PM2 processes started${NC}"
|
||||
# Start using ecosystem file
|
||||
if [ -f "ecosystem.config.js" ]; then
|
||||
pm2 start ecosystem.config.js --env production
|
||||
success "PM2 services started from ecosystem config"
|
||||
else
|
||||
echo -e "${RED}✗ Failed to start PM2 processes${NC}"
|
||||
exit 1
|
||||
warning "PM2 ecosystem config not found, starting services manually..."
|
||||
|
||||
# Start Backend API
|
||||
log "Starting Backend API..."
|
||||
cd "$DEPLOY_DIR/maternal-app/maternal-app-backend"
|
||||
pm2 start dist/main.js \
|
||||
--name "parentflow-api" \
|
||||
--instances 2 \
|
||||
--exec-mode cluster \
|
||||
--env production \
|
||||
--max-memory-restart 500M \
|
||||
--error /var/log/parentflow/api-error.log \
|
||||
--output /var/log/parentflow/api-out.log \
|
||||
--merge-logs \
|
||||
--time \
|
||||
-- --port 3020
|
||||
|
||||
# Start Frontend
|
||||
log "Starting Frontend..."
|
||||
cd "$DEPLOY_DIR/maternal-web"
|
||||
pm2 start npm \
|
||||
--name "parentflow-frontend" \
|
||||
--instances 2 \
|
||||
--exec-mode cluster \
|
||||
--max-memory-restart 400M \
|
||||
--error /var/log/parentflow/frontend-error.log \
|
||||
--output /var/log/parentflow/frontend-out.log \
|
||||
--merge-logs \
|
||||
--time \
|
||||
-- run start
|
||||
|
||||
# Start Admin Dashboard
|
||||
log "Starting Admin Dashboard..."
|
||||
cd "$DEPLOY_DIR/parentflow-admin"
|
||||
pm2 start npm \
|
||||
--name "parentflow-admin" \
|
||||
--instances 1 \
|
||||
--max-memory-restart 300M \
|
||||
--error /var/log/parentflow/admin-error.log \
|
||||
--output /var/log/parentflow/admin-out.log \
|
||||
--merge-logs \
|
||||
--time \
|
||||
-- run start
|
||||
fi
|
||||
|
||||
# Save PM2 process list
|
||||
# Save PM2 configuration
|
||||
pm2 save
|
||||
pm2 startup systemd -u root --hp /root || true
|
||||
|
||||
# Step 5: Wait for services to start
|
||||
log "${CYAN}Step 5: Waiting for services to initialize...${NC}"
|
||||
sleep 10
|
||||
|
||||
# Step 6: Verify services are running
|
||||
echo ""
|
||||
echo -e "${BLUE}Step 6: Verifying services...${NC}"
|
||||
sleep 5
|
||||
log "${CYAN}Step 6: Verifying services...${NC}"
|
||||
|
||||
# Check PM2 status
|
||||
echo -e "${YELLOW}PM2 Status:${NC}"
|
||||
verify_service() {
|
||||
local name=$1
|
||||
local port=$2
|
||||
|
||||
if lsof -i:$port > /dev/null 2>&1; then
|
||||
success "$name is running on port $port"
|
||||
return 0
|
||||
else
|
||||
warning "$name is not detected on port $port"
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
# Check each service
|
||||
ALL_GOOD=true
|
||||
verify_service "Backend API" 3020 || ALL_GOOD=false
|
||||
verify_service "Frontend" 3030 || ALL_GOOD=false
|
||||
verify_service "Admin Dashboard" 3335 || ALL_GOOD=false
|
||||
verify_service "Redis" 6379 || ALL_GOOD=false
|
||||
verify_service "MongoDB" 27017 || ALL_GOOD=false
|
||||
verify_service "MinIO" 9000 || ALL_GOOD=false
|
||||
|
||||
# Step 7: Show PM2 status
|
||||
log "${CYAN}Step 7: PM2 Process Status${NC}"
|
||||
echo ""
|
||||
pm2 list
|
||||
|
||||
# Check if ports are listening
|
||||
echo ""
|
||||
echo -e "${YELLOW}Port Status:${NC}"
|
||||
if lsof -i:3020 > /dev/null 2>&1; then
|
||||
echo -e "${GREEN}✓ Backend is running on port 3020${NC}"
|
||||
|
||||
# Step 8: Test API health
|
||||
log "${CYAN}Step 8: Testing API health endpoint...${NC}"
|
||||
sleep 3
|
||||
if curl -s -o /dev/null -w "%{http_code}" http://localhost:3020/health | grep -q "200\|401"; then
|
||||
success "API is responding"
|
||||
else
|
||||
echo -e "${RED}✗ Backend not detected on port 3020${NC}"
|
||||
warning "API health check failed - may still be starting"
|
||||
fi
|
||||
|
||||
if lsof -i:3030 > /dev/null 2>&1; then
|
||||
echo -e "${GREEN}✓ Frontend is running on port 3030${NC}"
|
||||
# Final summary
|
||||
echo ""
|
||||
echo "=========================================="
|
||||
if [ "$ALL_GOOD" = true ]; then
|
||||
echo -e "${GREEN} All Services Started Successfully! ${NC}"
|
||||
else
|
||||
echo -e "${RED}✗ Frontend not detected on port 3030${NC}"
|
||||
echo -e "${YELLOW} Services Started (Check Warnings) ${NC}"
|
||||
fi
|
||||
|
||||
# Check Docker containers
|
||||
echo ""
|
||||
echo -e "${YELLOW}Docker Containers:${NC}"
|
||||
docker ps --filter name=parentflow --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}"
|
||||
|
||||
# Summary
|
||||
echo ""
|
||||
echo "=========================================="
|
||||
echo -e "${GREEN}Production Environment Started!${NC}"
|
||||
echo "=========================================="
|
||||
echo ""
|
||||
echo "Services:"
|
||||
echo " Backend API: http://localhost:3020"
|
||||
echo " Frontend: http://localhost:3030"
|
||||
echo " PostgreSQL: localhost:5432"
|
||||
echo " Redis: localhost:6379"
|
||||
echo " MongoDB: localhost:27017"
|
||||
echo " MinIO: localhost:9000"
|
||||
echo " MinIO Console: localhost:9001"
|
||||
echo "Service URLs:"
|
||||
echo " Backend API: http://localhost:3020"
|
||||
echo " Frontend: http://localhost:3030"
|
||||
echo " Admin Dashboard: http://localhost:3335"
|
||||
echo " MinIO Console: http://localhost:9001"
|
||||
echo ""
|
||||
echo "Management Commands:"
|
||||
echo " PM2 status: pm2 status"
|
||||
echo " PM2 logs: pm2 logs"
|
||||
echo " PM2 restart: pm2 restart all"
|
||||
echo " PM2 stop: pm2 stop all"
|
||||
echo " Docker logs: docker logs parentflow-postgres-prod"
|
||||
echo " Stop all: pm2 stop all && docker-compose -f docker-compose.production.yml down"
|
||||
echo " View logs: pm2 logs"
|
||||
echo " Monitor: pm2 monit"
|
||||
echo " List processes: pm2 list"
|
||||
echo " Restart all: pm2 restart all"
|
||||
echo " Stop all: ./stop-production.sh"
|
||||
echo ""
|
||||
echo "Domains (configure in Nginx/DNS):"
|
||||
echo " Frontend: web.parentflowapp.com → localhost:3030"
|
||||
echo " Backend: api.parentflowapp.com → localhost:3020"
|
||||
echo "Log files:"
|
||||
echo " /var/log/parentflow/api-*.log"
|
||||
echo " /var/log/parentflow/frontend-*.log"
|
||||
echo " /var/log/parentflow/admin-*.log"
|
||||
echo ""
|
||||
echo -e "${GREEN}✓ Startup complete!${NC}"
|
||||
|
||||
# Create log directory if it doesn't exist
|
||||
mkdir -p /var/log/parentflow
|
||||
|
||||
log "Services started at $(date)"
|
||||
Reference in New Issue
Block a user