script
This commit is contained in:
		
							parent
							
								
									12d3b53fe2
								
							
						
					
					
						commit
						73ace5965f
					
				
							
								
								
									
										276
									
								
								deploy.sh
									
									
									
									
									
								
							
							
						
						
									
										276
									
								
								deploy.sh
									
									
									
									
									
								
							@ -6,7 +6,6 @@
 | 
			
		||||
set -e
 | 
			
		||||
 | 
			
		||||
WEBROOT="/var/www/forensic-pathways"
 | 
			
		||||
BACKUP_DIR="/var/backups/forensic-pathways"
 | 
			
		||||
LOG_DIR="$WEBROOT/logs"
 | 
			
		||||
DATA_DIR="$WEBROOT/data"
 | 
			
		||||
UPLOADS_DIR="$WEBROOT/public/uploads"
 | 
			
		||||
@ -18,6 +17,7 @@ ORIGINAL_HOME=$(eval echo "~$ORIGINAL_USER")
 | 
			
		||||
echo "🚀 ForensicPathways Deployment Starting..."
 | 
			
		||||
echo "📅 $(date '+%Y-%m-%d %H:%M:%S')"
 | 
			
		||||
echo "👤 Original user: $ORIGINAL_USER"
 | 
			
		||||
echo "📁 Working directory: $(pwd)"
 | 
			
		||||
echo ""
 | 
			
		||||
 | 
			
		||||
# Check if running as root
 | 
			
		||||
@ -26,255 +26,161 @@ if [ "$EUID" -ne 0 ]; then
 | 
			
		||||
    exit 1
 | 
			
		||||
fi
 | 
			
		||||
 | 
			
		||||
# Function to build application with nvm support
 | 
			
		||||
build_with_nvm() {
 | 
			
		||||
    echo "📦 Building application as user $ORIGINAL_USER..."
 | 
			
		||||
    
 | 
			
		||||
    if sudo -u "$ORIGINAL_USER" bash -c "
 | 
			
		||||
        cd '$PWD'
 | 
			
		||||
        
 | 
			
		||||
        # Load nvm if available
 | 
			
		||||
        export NVM_DIR='$ORIGINAL_HOME/.nvm'
 | 
			
		||||
        [ -s '\$NVM_DIR/nvm.sh' ] && source '\$NVM_DIR/nvm.sh'
 | 
			
		||||
        
 | 
			
		||||
        # Load user shell profile
 | 
			
		||||
        [ -s '$ORIGINAL_HOME/.bashrc' ] && source '$ORIGINAL_HOME/.bashrc'
 | 
			
		||||
        [ -s '$ORIGINAL_HOME/.profile' ] && source '$ORIGINAL_HOME/.profile'
 | 
			
		||||
        
 | 
			
		||||
        # Verify npm is available
 | 
			
		||||
        if ! command -v npm &> /dev/null; then
 | 
			
		||||
            echo 'npm not found in user environment'
 | 
			
		||||
            exit 1
 | 
			
		||||
        fi
 | 
			
		||||
        
 | 
			
		||||
        # Show versions for debugging
 | 
			
		||||
        echo \"npm version: \$(npm --version)\"
 | 
			
		||||
        echo \"node version: \$(node --version)\"
 | 
			
		||||
        
 | 
			
		||||
        # Run the build
 | 
			
		||||
        npm run build
 | 
			
		||||
    "; then
 | 
			
		||||
        echo "✅ Build completed successfully"
 | 
			
		||||
        return 0
 | 
			
		||||
    else
 | 
			
		||||
        echo "❌ Build failed"
 | 
			
		||||
        return 1
 | 
			
		||||
    fi
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
# Check for existing dist or build if needed
 | 
			
		||||
if [ ! -d "dist" ]; then
 | 
			
		||||
    echo "📦 No dist/ directory found, building..."
 | 
			
		||||
    if ! build_with_nvm; then
 | 
			
		||||
        echo ""
 | 
			
		||||
        echo "💡 Alternative: Build manually first:"
 | 
			
		||||
        echo "   npm run build"
 | 
			
		||||
        echo "   sudo ./deploy.sh"
 | 
			
		||||
        exit 1
 | 
			
		||||
    fi
 | 
			
		||||
else
 | 
			
		||||
    echo "📦 Found existing dist/ directory"
 | 
			
		||||
    read -p "🤔 Rebuild application? (y/N): " -n 1 -r
 | 
			
		||||
    echo
 | 
			
		||||
    if [[ $REPLY =~ ^[Yy]$ ]]; then
 | 
			
		||||
        if ! build_with_nvm; then
 | 
			
		||||
            echo ""
 | 
			
		||||
            echo "💡 Using existing dist/ due to build failure"
 | 
			
		||||
        fi
 | 
			
		||||
    else
 | 
			
		||||
        echo "📦 Using existing build"
 | 
			
		||||
    fi
 | 
			
		||||
fi
 | 
			
		||||
 | 
			
		||||
# Verify dist exists before proceeding
 | 
			
		||||
if [ ! -d "dist" ]; then
 | 
			
		||||
    echo "❌ Error: No dist/ directory available for deployment"
 | 
			
		||||
# Verify we're in the right directory
 | 
			
		||||
if [ ! -f "package.json" ] || [ ! -f "astro.config.mjs" ]; then
 | 
			
		||||
    echo "❌ Error: Must run from ForensicPathways project root"
 | 
			
		||||
    echo "🔍 Current directory: $(pwd)"
 | 
			
		||||
    echo "🔍 Files found: $(ls -la)"
 | 
			
		||||
    exit 1
 | 
			
		||||
fi
 | 
			
		||||
 | 
			
		||||
# Create backup if existing deployment exists
 | 
			
		||||
if [ -d "$WEBROOT" ]; then
 | 
			
		||||
    BACKUP_TIMESTAMP=$(date +%Y%m%d_%H%M%S)
 | 
			
		||||
    BACKUP_PATH="$BACKUP_DIR/$BACKUP_TIMESTAMP"
 | 
			
		||||
    
 | 
			
		||||
    echo "💾 Creating backup at $BACKUP_PATH..."
 | 
			
		||||
    mkdir -p "$BACKUP_DIR"
 | 
			
		||||
    cp -r "$WEBROOT" "$BACKUP_PATH"
 | 
			
		||||
    echo "✅ Backup created successfully"
 | 
			
		||||
    
 | 
			
		||||
    # Preserve existing .env if it exists
 | 
			
		||||
    if [ -f "$WEBROOT/.env" ]; then
 | 
			
		||||
        cp "$WEBROOT/.env" "/tmp/forensic-pathways.env.backup"
 | 
			
		||||
        echo "💾 Preserved existing .env configuration"
 | 
			
		||||
    fi
 | 
			
		||||
    
 | 
			
		||||
    # Clean old backups (keep last 5)
 | 
			
		||||
    if [ -d "$BACKUP_DIR" ]; then
 | 
			
		||||
        cd "$BACKUP_DIR"
 | 
			
		||||
        ls -1t | tail -n +6 | xargs -r rm -rf
 | 
			
		||||
        echo "🧹 Cleaned old backups (keeping last 5)"
 | 
			
		||||
    fi
 | 
			
		||||
# Build application
 | 
			
		||||
echo "📦 Building application..."
 | 
			
		||||
if [ ! -d "node_modules" ]; then
 | 
			
		||||
    echo "📦 Installing dependencies..."
 | 
			
		||||
    sudo -u "$ORIGINAL_USER" npm install
 | 
			
		||||
fi
 | 
			
		||||
 | 
			
		||||
# Create webroot and subdirectories
 | 
			
		||||
echo "📁 Setting up directory structure..."
 | 
			
		||||
# Build with proper user context
 | 
			
		||||
sudo -u "$ORIGINAL_USER" bash -c "
 | 
			
		||||
    # Load user environment
 | 
			
		||||
    [ -s '$ORIGINAL_HOME/.bashrc' ] && source '$ORIGINAL_HOME/.bashrc'
 | 
			
		||||
    [ -s '$ORIGINAL_HOME/.profile' ] && source '$ORIGINAL_HOME/.profile'
 | 
			
		||||
    
 | 
			
		||||
    # Load nvm if available
 | 
			
		||||
    export NVM_DIR='$ORIGINAL_HOME/.nvm'
 | 
			
		||||
    [ -s '\$NVM_DIR/nvm.sh' ] && source '\$NVM_DIR/nvm.sh'
 | 
			
		||||
    
 | 
			
		||||
    # Build
 | 
			
		||||
    npm run build
 | 
			
		||||
"
 | 
			
		||||
 | 
			
		||||
# Verify build succeeded
 | 
			
		||||
if [ ! -d "dist" ] || [ ! "$(ls -A dist 2>/dev/null)" ]; then
 | 
			
		||||
    echo "❌ Error: Build failed or dist/ is empty"
 | 
			
		||||
    echo "🔍 Dist contents: $(ls -la dist/ 2>/dev/null || echo 'dist/ not found')"
 | 
			
		||||
    exit 1
 | 
			
		||||
fi
 | 
			
		||||
 | 
			
		||||
echo "✅ Build completed successfully"
 | 
			
		||||
 | 
			
		||||
# Create target directories
 | 
			
		||||
echo "📁 Setting up target directories..."
 | 
			
		||||
mkdir -p "$WEBROOT"
 | 
			
		||||
mkdir -p "$LOG_DIR"
 | 
			
		||||
mkdir -p "$DATA_DIR"
 | 
			
		||||
mkdir -p "$UPLOADS_DIR"
 | 
			
		||||
mkdir -p "$WEBROOT/src/data"
 | 
			
		||||
mkdir -p "$WEBROOT/public"
 | 
			
		||||
mkdir -p "$WEBROOT/server"
 | 
			
		||||
mkdir -p "$DATA_DIR/embeddings"
 | 
			
		||||
mkdir -p "$LOG_DIR/access"
 | 
			
		||||
mkdir -p "$LOG_DIR/error"
 | 
			
		||||
mkdir -p "$LOG_DIR/ai"
 | 
			
		||||
echo "✅ Directory structure created"
 | 
			
		||||
 | 
			
		||||
# Copy built application
 | 
			
		||||
# Copy application files
 | 
			
		||||
echo "📋 Copying application files..."
 | 
			
		||||
if [ -d "dist" ] && [ "$(ls -A dist)" ]; then
 | 
			
		||||
    # Use rsync for better copying, or fallback to cp
 | 
			
		||||
    if command -v rsync &> /dev/null; then
 | 
			
		||||
        rsync -av --delete dist/ "$WEBROOT/"
 | 
			
		||||
        echo "✅ Application files copied via rsync ($(du -sh dist | cut -f1))"
 | 
			
		||||
    else
 | 
			
		||||
        # More reliable copy method
 | 
			
		||||
        cp -r dist/. "$WEBROOT/"
 | 
			
		||||
        echo "✅ Application files copied via cp ($(du -sh dist | cut -f1))"
 | 
			
		||||
    fi
 | 
			
		||||
    
 | 
			
		||||
    # Verify copy was successful
 | 
			
		||||
    if [ ! -f "$WEBROOT/index.html" ] && [ ! -d "$WEBROOT/server" ]; then
 | 
			
		||||
        echo "❌ Error: Application files not properly copied"
 | 
			
		||||
        echo "🔍 Dist contents: $(ls -la dist/)"
 | 
			
		||||
        echo "🔍 Webroot contents: $(ls -la $WEBROOT/)"
 | 
			
		||||
        exit 1
 | 
			
		||||
    fi
 | 
			
		||||
else
 | 
			
		||||
    echo "❌ Error: dist/ directory is empty or doesn't exist"
 | 
			
		||||
    echo "🔍 Current directory: $(pwd)"
 | 
			
		||||
    echo "🔍 Contents: $(ls -la)"
 | 
			
		||||
    exit 1
 | 
			
		||||
fi
 | 
			
		||||
cp -r dist/. "$WEBROOT/"
 | 
			
		||||
echo "✅ Application files copied ($(du -sh dist | cut -f1))"
 | 
			
		||||
 | 
			
		||||
# Copy essential data files
 | 
			
		||||
echo "🗂️  Setting up data files..."
 | 
			
		||||
echo "🗂️ Setting up data files..."
 | 
			
		||||
if [ -f "src/data/tools.yaml" ]; then
 | 
			
		||||
    cp src/data/tools.yaml "$WEBROOT/src/data/"
 | 
			
		||||
    echo "✅ tools.yaml copied ($(wc -l < src/data/tools.yaml) lines)"
 | 
			
		||||
    TOOL_COUNT=$(grep -c "^  - name:" "src/data/tools.yaml" || echo "unknown")
 | 
			
		||||
    echo "✅ tools.yaml copied ($TOOL_COUNT tools)"
 | 
			
		||||
else
 | 
			
		||||
    echo "❌ Error: src/data/tools.yaml not found"
 | 
			
		||||
    exit 1
 | 
			
		||||
fi
 | 
			
		||||
 | 
			
		||||
# Copy any existing knowledgebase content
 | 
			
		||||
# Copy knowledgebase content if it exists
 | 
			
		||||
if [ -d "src/content/knowledgebase" ]; then
 | 
			
		||||
    mkdir -p "$WEBROOT/src/content"
 | 
			
		||||
    cp -r src/content/knowledgebase "$WEBROOT/src/content/"
 | 
			
		||||
    KB_COUNT=$(find src/content/knowledgebase -name "*.md" | wc -l)
 | 
			
		||||
    KB_COUNT=$(find src/content/knowledgebase -name "*.md" 2>/dev/null | wc -l)
 | 
			
		||||
    echo "✅ Knowledgebase content copied ($KB_COUNT articles)"
 | 
			
		||||
fi
 | 
			
		||||
 | 
			
		||||
# Handle environment configuration
 | 
			
		||||
if [ -f "/tmp/forensic-pathways.env.backup" ]; then
 | 
			
		||||
    echo "🔧 Restoring existing .env configuration..."
 | 
			
		||||
    cp "/tmp/forensic-pathways.env.backup" "$WEBROOT/.env"
 | 
			
		||||
    rm "/tmp/forensic-pathways.env.backup"
 | 
			
		||||
    echo "✅ Existing configuration restored"
 | 
			
		||||
# Setup environment configuration
 | 
			
		||||
echo "🔧 Setting up environment configuration..."
 | 
			
		||||
if [ -f "$WEBROOT/.env" ]; then
 | 
			
		||||
    echo "📝 Existing .env found, keeping current configuration"
 | 
			
		||||
else
 | 
			
		||||
    echo "🔧 Setting up new environment configuration..."
 | 
			
		||||
    echo "📝 Creating new .env from template..."
 | 
			
		||||
    cp .env.example "$WEBROOT/.env"
 | 
			
		||||
    echo "⚠️  IMPORTANT: Edit $WEBROOT/.env with your configuration"
 | 
			
		||||
fi
 | 
			
		||||
 | 
			
		||||
# Create additional required files
 | 
			
		||||
echo "📝 Creating additional files..."
 | 
			
		||||
 | 
			
		||||
# Create placeholder log files
 | 
			
		||||
# Create log files
 | 
			
		||||
echo "📝 Creating log files..."
 | 
			
		||||
touch "$LOG_DIR/access.log"
 | 
			
		||||
touch "$LOG_DIR/error.log"
 | 
			
		||||
touch "$LOG_DIR/ai-pipeline.log"
 | 
			
		||||
 | 
			
		||||
echo "✅ Additional files and directories created"
 | 
			
		||||
 | 
			
		||||
# Set proper permissions
 | 
			
		||||
# Set permissions
 | 
			
		||||
echo "🔐 Setting permissions..."
 | 
			
		||||
chown -R www-data:www-data "$WEBROOT"
 | 
			
		||||
chmod -R 755 "$WEBROOT"
 | 
			
		||||
chmod 600 "$WEBROOT/.env"
 | 
			
		||||
 | 
			
		||||
# Specific permissions for data directories
 | 
			
		||||
chmod 755 "$DATA_DIR"
 | 
			
		||||
chmod 755 "$UPLOADS_DIR"
 | 
			
		||||
chmod 755 "$LOG_DIR"
 | 
			
		||||
chmod 644 "$LOG_DIR"/*.log
 | 
			
		||||
 | 
			
		||||
# Make server entry point executable
 | 
			
		||||
# Make server executable if it exists
 | 
			
		||||
if [ -f "$WEBROOT/server/entry.mjs" ]; then
 | 
			
		||||
    chmod 755 "$WEBROOT/server/entry.mjs"
 | 
			
		||||
    echo "✅ Server entry point permissions set"
 | 
			
		||||
fi
 | 
			
		||||
 | 
			
		||||
echo "✅ Permissions configured successfully"
 | 
			
		||||
 | 
			
		||||
# Display deployment summary
 | 
			
		||||
echo ""
 | 
			
		||||
echo "═══════════════════════════════════════════════════════════════"
 | 
			
		||||
echo "✅ ForensicPathways Deployment Complete!"
 | 
			
		||||
echo "═══════════════════════════════════════════════════════════════"
 | 
			
		||||
echo ""
 | 
			
		||||
echo "📊 Deployment Summary:"
 | 
			
		||||
echo "   🎯 Target: $WEBROOT"
 | 
			
		||||
echo "   💾 Backup: $BACKUP_DIR"
 | 
			
		||||
echo "   📁 Logs: $LOG_DIR"
 | 
			
		||||
echo "   📤 Uploads: $UPLOADS_DIR"
 | 
			
		||||
echo "   🗃️  Data: $DATA_DIR"
 | 
			
		||||
echo "   📏 Size: $(du -sh $WEBROOT | cut -f1)"
 | 
			
		||||
echo ""
 | 
			
		||||
echo "📋 Required Next Steps:"
 | 
			
		||||
echo "   1. 🔧 Edit $WEBROOT/.env with your configuration"
 | 
			
		||||
echo "      - Set PUBLIC_BASE_URL to your domain"
 | 
			
		||||
echo "      - Configure AI_ANALYZER_* settings"
 | 
			
		||||
echo "      - Set AUTH_SECRET to a secure value"
 | 
			
		||||
echo ""
 | 
			
		||||
echo "   2. 🔄 Restart services:"
 | 
			
		||||
echo "      sudo systemctl restart forensic-pathways"
 | 
			
		||||
echo "      sudo systemctl reload nginx"
 | 
			
		||||
echo ""
 | 
			
		||||
echo "   3. 🔍 Check status:"
 | 
			
		||||
echo "      sudo systemctl status forensic-pathways"
 | 
			
		||||
echo "      sudo tail -f $LOG_DIR/error.log"
 | 
			
		||||
echo ""
 | 
			
		||||
echo "🌐 Once configured, access at: http://your-domain.com"
 | 
			
		||||
echo ""
 | 
			
		||||
echo "✅ Permissions configured"
 | 
			
		||||
 | 
			
		||||
# Final validation
 | 
			
		||||
echo ""
 | 
			
		||||
echo "🔍 Post-deployment validation..."
 | 
			
		||||
VALIDATION_ERRORS=0
 | 
			
		||||
 | 
			
		||||
if [ -f "$WEBROOT/.env" ]; then
 | 
			
		||||
    echo "✅ Environment configuration exists"
 | 
			
		||||
else
 | 
			
		||||
    echo "❌ Environment configuration missing"
 | 
			
		||||
    ((VALIDATION_ERRORS++))
 | 
			
		||||
fi
 | 
			
		||||
 | 
			
		||||
if [ -f "$WEBROOT/src/data/tools.yaml" ]; then
 | 
			
		||||
    TOOL_COUNT=$(grep -c "^  - name:" "$WEBROOT/src/data/tools.yaml" || echo "unknown")
 | 
			
		||||
    echo "✅ Tools database exists ($TOOL_COUNT tools)"
 | 
			
		||||
    echo "✅ Tools database exists"
 | 
			
		||||
else
 | 
			
		||||
    echo "❌ Tools database missing"
 | 
			
		||||
    ((VALIDATION_ERRORS++))
 | 
			
		||||
fi
 | 
			
		||||
 | 
			
		||||
if [ -d "$WEBROOT/server" ]; then
 | 
			
		||||
    echo "✅ Server directory exists"
 | 
			
		||||
if [ -f "$WEBROOT/index.html" ] || [ -d "$WEBROOT/server" ]; then
 | 
			
		||||
    echo "✅ Application files deployed"
 | 
			
		||||
else
 | 
			
		||||
    echo "❌ Server directory missing"
 | 
			
		||||
fi
 | 
			
		||||
 | 
			
		||||
if [ -f "$WEBROOT/server/entry.mjs" ]; then
 | 
			
		||||
    echo "✅ Server entry point exists"
 | 
			
		||||
else
 | 
			
		||||
    echo "⚠️  Warning: Server entry point not found"
 | 
			
		||||
    echo "❌ Application files missing"
 | 
			
		||||
    ((VALIDATION_ERRORS++))
 | 
			
		||||
fi
 | 
			
		||||
 | 
			
		||||
echo ""
 | 
			
		||||
echo "🎉 Deployment script completed at $(date '+%Y-%m-%d %H:%M:%S')"
 | 
			
		||||
if [ $VALIDATION_ERRORS -eq 0 ]; then
 | 
			
		||||
    echo "═══════════════════════════════════════════════════════════════"
 | 
			
		||||
    echo "✅ Deployment Successful!"
 | 
			
		||||
    echo "═══════════════════════════════════════════════════════════════"
 | 
			
		||||
    echo ""
 | 
			
		||||
    echo "📋 Next Steps:"
 | 
			
		||||
    echo "   1. 🔧 Configure $WEBROOT/.env:"
 | 
			
		||||
    echo "      - Set PUBLIC_BASE_URL to your domain"
 | 
			
		||||
    echo "      - Configure AI services (AI_ANALYZER_ENDPOINT, etc.)"
 | 
			
		||||
    echo "      - Set AUTH_SECRET to a secure random value"
 | 
			
		||||
    echo ""
 | 
			
		||||
    echo "   2. 🔄 Restart services:"
 | 
			
		||||
    echo "      sudo systemctl restart forensic-pathways"
 | 
			
		||||
    echo "      sudo systemctl reload nginx"
 | 
			
		||||
    echo ""
 | 
			
		||||
    echo "   3. 🔍 Monitor:"
 | 
			
		||||
    echo "      sudo systemctl status forensic-pathways"
 | 
			
		||||
    echo "      sudo tail -f $LOG_DIR/error.log"
 | 
			
		||||
    echo ""
 | 
			
		||||
    echo "🌐 Application deployed to: $WEBROOT"
 | 
			
		||||
else
 | 
			
		||||
    echo "❌ Deployment completed with $VALIDATION_ERRORS errors"
 | 
			
		||||
    echo "📋 Please check the issues above before proceeding"
 | 
			
		||||
fi
 | 
			
		||||
 | 
			
		||||
echo ""
 | 
			
		||||
echo "🎉 Deploy script completed at $(date '+%Y-%m-%d %H:%M:%S')"
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user