forensic-pathways/deploy.sh
overcuriousity 73ace5965f script
2025-08-07 10:05:01 +02:00

186 lines
5.6 KiB
Bash
Executable File

#!/bin/bash
# ForensicPathways Deployment Script
# Usage: sudo ./deploy.sh
set -e
WEBROOT="/var/www/forensic-pathways"
LOG_DIR="$WEBROOT/logs"
DATA_DIR="$WEBROOT/data"
UPLOADS_DIR="$WEBROOT/public/uploads"
# Get original user who called sudo
ORIGINAL_USER="${SUDO_USER:-$USER}"
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
if [ "$EUID" -ne 0 ]; then
echo "❌ Error: This script must be run as root (use sudo)"
exit 1
fi
# 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
# Build application
echo "📦 Building application..."
if [ ! -d "node_modules" ]; then
echo "📦 Installing dependencies..."
sudo -u "$ORIGINAL_USER" npm install
fi
# 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"
# Copy application files
echo "📋 Copying application files..."
cp -r dist/. "$WEBROOT/"
echo "✅ Application files copied ($(du -sh dist | cut -f1))"
# Copy essential data files
echo "🗂️ Setting up data files..."
if [ -f "src/data/tools.yaml" ]; then
cp src/data/tools.yaml "$WEBROOT/src/data/"
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 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" 2>/dev/null | wc -l)
echo "✅ Knowledgebase content copied ($KB_COUNT articles)"
fi
# Setup environment configuration
echo "🔧 Setting up environment configuration..."
if [ -f "$WEBROOT/.env" ]; then
echo "📝 Existing .env found, keeping current configuration"
else
echo "📝 Creating new .env from template..."
cp .env.example "$WEBROOT/.env"
echo "⚠️ IMPORTANT: Edit $WEBROOT/.env with your configuration"
fi
# Create log files
echo "📝 Creating log files..."
touch "$LOG_DIR/access.log"
touch "$LOG_DIR/error.log"
touch "$LOG_DIR/ai-pipeline.log"
# Set permissions
echo "🔐 Setting permissions..."
chown -R www-data:www-data "$WEBROOT"
chmod -R 755 "$WEBROOT"
chmod 600 "$WEBROOT/.env"
chmod 755 "$DATA_DIR"
chmod 755 "$UPLOADS_DIR"
chmod 755 "$LOG_DIR"
chmod 644 "$LOG_DIR"/*.log
# 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"
# 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
echo "✅ Tools database exists"
else
echo "❌ Tools database missing"
((VALIDATION_ERRORS++))
fi
if [ -f "$WEBROOT/index.html" ] || [ -d "$WEBROOT/server" ]; then
echo "✅ Application files deployed"
else
echo "❌ Application files missing"
((VALIDATION_ERRORS++))
fi
echo ""
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')"