main #11

Merged
mstoeck3 merged 66 commits from main into forensic-ai 2025-08-11 12:02:56 +00:00
Showing only changes of commit ec0ee12ae5 - Show all commits

271
deploy.sh
View File

@ -11,6 +11,277 @@ 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 ""
# Check if running as root
if [ "$EUID" -ne 0 ]; then
echo "❌ Error: This script must be run as root (use sudo)"
exit 1
fi
# Function to run commands as original user
run_as_user() {
if [ "$ORIGINAL_USER" != "root" ]; then
sudo -u "$ORIGINAL_USER" -i bash -c "cd '$PWD' && $1"
else
bash -c "$1"
fi
}
# Function to check for npm/node in user environment
check_node_env() {
echo "🔍 Checking Node.js environment..."
# Check if npm/node are available in original user's environment
if run_as_user "command -v npm" &> /dev/null && run_as_user "command -v node" &> /dev/null; then
NPM_VERSION=$(run_as_user "npm --version")
NODE_VERSION=$(run_as_user "node --version")
echo "✅ npm found: $NPM_VERSION"
echo "✅ Node.js found: $NODE_VERSION"
return 0
fi
# Check common nvm installation paths
NVM_PATHS=(
"$ORIGINAL_HOME/.nvm/versions/node/*/bin"
"$ORIGINAL_HOME/.volta/bin"
"/usr/local/bin"
"/usr/bin"
)
for nvm_path in "${NVM_PATHS[@]}"; do
if [ -d "$nvm_path" ] && [ -x "$nvm_path/npm" ] && [ -x "$nvm_path/node" ]; then
echo "✅ Found Node.js in: $nvm_path"
export PATH="$nvm_path:$PATH"
NPM_VERSION=$("$nvm_path/npm" --version)
NODE_VERSION=$("$nvm_path/node" --version)
echo "✅ npm found: $NPM_VERSION"
echo "✅ Node.js found: $NODE_VERSION"
return 0
fi
done
echo "❌ Error: npm/node not found in user environment"
echo ""
echo "💡 Solutions:"
echo " 1. Run 'npm run build' as user first, then run this script"
echo " 2. Install Node.js system-wide"
echo " 3. Or run: cd '$PWD' && npm run build && sudo ./deploy.sh"
echo ""
return 1
}
# Check Node.js environment
if ! check_node_env; then
exit 1
fi
# Check if dist directory exists or can be built
if [ ! -d "dist" ]; then
echo "📦 Building application as user $ORIGINAL_USER..."
if ! run_as_user "npm run build"; then
echo "❌ Error: Build failed"
echo "💡 Try running: npm run build (as user) before deployment"
exit 1
fi
echo "✅ Build completed successfully"
else
echo "📦 Found existing dist/ directory"
read -p "🤔 Rebuild application? (y/N): " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
echo "📦 Rebuilding application as user $ORIGINAL_USER..."
if ! run_as_user "npm run build"; then
echo "❌ Error: Rebuild failed"
exit 1
fi
echo "✅ Rebuild completed successfully"
else
echo "📦 Using existing build"
fi
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)
cd "$BACKUP_DIR"
ls -1t | tail -n +6 | xargs -r rm -rf
echo "🧹 Cleaned old backups (keeping last 5)"
fi
# Create webroot and subdirectories
echo "📁 Setting up directory structure..."
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"
echo "✅ Directory structure created"
# Copy built application
echo "📋 Copying application files..."
if [ -d "dist" ]; then
cp -r dist/* "$WEBROOT/"
echo "✅ Application files copied"
else
echo "❌ Error: dist/ directory not found"
exit 1
fi
# Copy essential 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"
else
echo "❌ Error: src/data/tools.yaml not found"
exit 1
fi
# Copy any existing knowledgebase content
if [ -d "src/content/knowledgebase" ]; then
mkdir -p "$WEBROOT/src/content"
cp -r src/content/knowledgebase "$WEBROOT/src/content/"
echo "✅ Knowledgebase content copied"
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"
else
echo "🔧 Setting up new environment configuration..."
cp .env.example "$WEBROOT/.env"
echo "⚠️ IMPORTANT: Edit $WEBROOT/.env with your configuration"
fi
# Create additional required files and directories
echo "📝 Creating additional files..."
# Create embeddings data directory
mkdir -p "$DATA_DIR/embeddings"
# Create logs directory with proper structure
mkdir -p "$LOG_DIR/access"
mkdir -p "$LOG_DIR/error"
mkdir -p "$LOG_DIR/ai"
# Create placeholder 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
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
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 ""
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 ""
# Final validation
echo "🔍 Post-deployment validation..."
if [ -f "$WEBROOT/.env" ]; then
echo "✅ Environment configuration exists"
else
echo "❌ Environment configuration missing"
fi
if [ -f "$WEBROOT/src/data/tools.yaml" ]; then
echo "✅ Tools database exists"
else
echo "❌ Tools database missing"
fi
if [ -d "$WEBROOT/server" ]; then
echo "✅ Server directory exists"
else
echo "❌ Server directory missing"
fi
echo ""
echo "🎉 Deployment script completed at $(date '+%Y-%m-%d %H:%M:%S')"#!/bin/bash
# ForensicPathways Deployment Script
# Usage: sudo ./deploy.sh
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"
echo "🚀 ForensicPathways Deployment Starting..."
echo "📅 $(date '+%Y-%m-%d %H:%M:%S')"
echo ""