This commit is contained in:
overcuriousity 2025-08-07 09:52:22 +02:00
parent 9d6ba83378
commit 1ff437b7e6

384
deploy.sh
View File

@ -26,125 +26,50 @@ if [ "$EUID" -ne 0 ]; then
exit 1 exit 1
fi fi
# Function to run commands as original user # Function to build application with nvm support
run_as_user() { build_with_nvm() {
if [ "$ORIGINAL_USER" != "root" ]; then echo "📦 Building application as user $ORIGINAL_USER..."
sudo -u "$ORIGINAL_USER" -i bash -c "cd '$PWD' && $1"
else
bash -c "$1"
fi
}
# Function to check for npm/node in user environment if sudo -u "$ORIGINAL_USER" bash -c "
check_node_env() { cd '$PWD'
echo "🔍 Checking Node.js environment..."
# First, try to source nvm and check # Load nvm if available
if [ "$ORIGINAL_USER" != "root" ]; then export NVM_DIR='$ORIGINAL_HOME/.nvm'
echo "🔍 Checking user environment for nvm..." [ -s '\$NVM_DIR/nvm.sh' ] && source '\$NVM_DIR/nvm.sh'
# Try to source nvm and check npm/node # Load user shell profile
if sudo -u "$ORIGINAL_USER" -i bash -c "
# Source nvm if it exists
[ -s '$ORIGINAL_HOME/.nvm/nvm.sh' ] && source '$ORIGINAL_HOME/.nvm/nvm.sh'
[ -s '$ORIGINAL_HOME/.bashrc' ] && source '$ORIGINAL_HOME/.bashrc' [ -s '$ORIGINAL_HOME/.bashrc' ] && source '$ORIGINAL_HOME/.bashrc'
[ -s '$ORIGINAL_HOME/.profile' ] && source '$ORIGINAL_HOME/.profile' [ -s '$ORIGINAL_HOME/.profile' ] && source '$ORIGINAL_HOME/.profile'
# Check if npm and node are available # Verify npm is available
command -v npm &> /dev/null && command -v node &> /dev/null if ! command -v npm &> /dev/null; then
"; then echo 'npm not found in user environment'
# Get versions using the same approach exit 1
NPM_VERSION=$(sudo -u "$ORIGINAL_USER" -i bash -c "
[ -s '$ORIGINAL_HOME/.nvm/nvm.sh' ] && source '$ORIGINAL_HOME/.nvm/nvm.sh'
[ -s '$ORIGINAL_HOME/.bashrc' ] && source '$ORIGINAL_HOME/.bashrc'
npm --version 2>/dev/null || echo 'unknown'
")
NODE_VERSION=$(sudo -u "$ORIGINAL_USER" -i bash -c "
[ -s '$ORIGINAL_HOME/.nvm/nvm.sh' ] && source '$ORIGINAL_HOME/.nvm/nvm.sh'
[ -s '$ORIGINAL_HOME/.bashrc' ] && source '$ORIGINAL_HOME/.bashrc'
node --version 2>/dev/null || echo 'unknown'
")
echo "✅ npm found: $NPM_VERSION"
echo "✅ Node.js found: $NODE_VERSION"
return 0
fi
fi fi
# Fallback: Check specific nvm paths directly # Show versions for debugging
echo "🔍 Checking nvm installation paths..." echo \"npm version: \$(npm --version)\"
NVM_DIRS=$(find "$ORIGINAL_HOME/.nvm/versions/node" -name "bin" -type d 2>/dev/null | head -5) echo \"node version: \$(node --version)\"
for nvm_bin in $NVM_DIRS; do # Run the build
if [ -x "$nvm_bin/npm" ] && [ -x "$nvm_bin/node" ]; then
echo "✅ Found Node.js in: $nvm_bin"
NPM_VERSION=$("$nvm_bin/npm" --version 2>/dev/null || echo "unknown")
NODE_VERSION=$("$nvm_bin/node" --version 2>/dev/null || echo "unknown")
echo "✅ npm found: $NPM_VERSION"
echo "✅ Node.js found: $NODE_VERSION"
# Set PATH for the rest of the script
export PATH="$nvm_bin:$PATH"
return 0
fi
done
# Check system-wide installation
echo "🔍 Checking system-wide installation..."
if command -v npm &> /dev/null && command -v node &> /dev/null; then
NPM_VERSION=$(npm --version)
NODE_VERSION=$(node --version)
echo "✅ npm found (system): $NPM_VERSION"
echo "✅ Node.js found (system): $NODE_VERSION"
return 0
fi
echo "❌ Error: npm/node not found in any location"
echo ""
echo "🔍 Checked locations:"
echo " - User environment with nvm sourcing"
echo " - $ORIGINAL_HOME/.nvm/versions/node/*/bin"
echo " - System PATH: /usr/local/bin, /usr/bin"
echo ""
echo "💡 Solutions:"
echo " 1. Build first: npm run build (as $ORIGINAL_USER)"
echo " 2. Then run: sudo ./deploy.sh"
echo " 3. Or install Node.js system-wide: curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash - && sudo apt-get install -y nodejs"
echo ""
return 1
}
# Enhanced build function
build_application() {
echo "📦 Building application as user $ORIGINAL_USER..."
# Use nvm-aware build command
if sudo -u "$ORIGINAL_USER" -i bash -c "
cd '$PWD'
# Source nvm if available
[ -s '$ORIGINAL_HOME/.nvm/nvm.sh' ] && source '$ORIGINAL_HOME/.nvm/nvm.sh'
[ -s '$ORIGINAL_HOME/.bashrc' ] && source '$ORIGINAL_HOME/.bashrc'
# Run build
npm run build npm run build
"; then "; then
echo "✅ Build completed successfully" echo "✅ Build completed successfully"
return 0 return 0
else else
echo "❌ Error: Build failed" echo "❌ Build failed"
echo "💡 Try running manually: npm run build"
return 1 return 1
fi fi
} }
# Check Node.js environment # Check for existing dist or build if needed
if ! check_node_env; then
exit 1
fi
# Check if dist directory exists or can be built
if [ ! -d "dist" ]; then if [ ! -d "dist" ]; then
if ! build_application; 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 exit 1
fi fi
else else
@ -152,14 +77,21 @@ else
read -p "🤔 Rebuild application? (y/N): " -n 1 -r read -p "🤔 Rebuild application? (y/N): " -n 1 -r
echo echo
if [[ $REPLY =~ ^[Yy]$ ]]; then if [[ $REPLY =~ ^[Yy]$ ]]; then
if ! build_application; then if ! build_with_nvm; then
exit 1 echo ""
echo "💡 Using existing dist/ due to build failure"
fi fi
else else
echo "📦 Using existing build" echo "📦 Using existing build"
fi fi
fi fi
# Verify dist exists before proceeding
if [ ! -d "dist" ]; then
echo "❌ Error: No dist/ directory available for deployment"
exit 1
fi
# Create backup if existing deployment exists # Create backup if existing deployment exists
if [ -d "$WEBROOT" ]; then if [ -d "$WEBROOT" ]; then
BACKUP_TIMESTAMP=$(date +%Y%m%d_%H%M%S) BACKUP_TIMESTAMP=$(date +%Y%m%d_%H%M%S)
@ -177,9 +109,11 @@ if [ -d "$WEBROOT" ]; then
fi fi
# Clean old backups (keep last 5) # Clean old backups (keep last 5)
if [ -d "$BACKUP_DIR" ]; then
cd "$BACKUP_DIR" cd "$BACKUP_DIR"
ls -1t | tail -n +6 | xargs -r rm -rf ls -1t | tail -n +6 | xargs -r rm -rf
echo "🧹 Cleaned old backups (keeping last 5)" echo "🧹 Cleaned old backups (keeping last 5)"
fi
fi fi
# Create webroot and subdirectories # Create webroot and subdirectories
@ -191,23 +125,22 @@ mkdir -p "$UPLOADS_DIR"
mkdir -p "$WEBROOT/src/data" mkdir -p "$WEBROOT/src/data"
mkdir -p "$WEBROOT/public" mkdir -p "$WEBROOT/public"
mkdir -p "$WEBROOT/server" 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" echo "✅ Directory structure created"
# Copy built application # Copy built application
echo "📋 Copying application files..." echo "📋 Copying application files..."
if [ -d "dist" ]; then cp -r dist/* "$WEBROOT/"
cp -r dist/* "$WEBROOT/" echo "✅ Application files copied ($(du -sh dist | cut -f1))"
echo "✅ Application files copied"
else
echo "❌ Error: dist/ directory not found"
exit 1
fi
# Copy essential data files # Copy essential data files
echo "🗂️ Setting up data files..." echo "🗂️ Setting up data files..."
if [ -f "src/data/tools.yaml" ]; then if [ -f "src/data/tools.yaml" ]; then
cp src/data/tools.yaml "$WEBROOT/src/data/" cp src/data/tools.yaml "$WEBROOT/src/data/"
echo "✅ tools.yaml copied" echo "✅ tools.yaml copied ($(wc -l < src/data/tools.yaml) lines)"
else else
echo "❌ Error: src/data/tools.yaml not found" echo "❌ Error: src/data/tools.yaml not found"
exit 1 exit 1
@ -217,7 +150,8 @@ fi
if [ -d "src/content/knowledgebase" ]; then if [ -d "src/content/knowledgebase" ]; then
mkdir -p "$WEBROOT/src/content" mkdir -p "$WEBROOT/src/content"
cp -r src/content/knowledgebase "$WEBROOT/src/content/" cp -r src/content/knowledgebase "$WEBROOT/src/content/"
echo "✅ Knowledgebase content copied" KB_COUNT=$(find src/content/knowledgebase -name "*.md" | wc -l)
echo "✅ Knowledgebase content copied ($KB_COUNT articles)"
fi fi
# Handle environment configuration # Handle environment configuration
@ -232,17 +166,9 @@ else
echo "⚠️ IMPORTANT: Edit $WEBROOT/.env with your configuration" echo "⚠️ IMPORTANT: Edit $WEBROOT/.env with your configuration"
fi fi
# Create additional required files and directories # Create additional required files
echo "📝 Creating additional files..." 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 # Create placeholder log files
touch "$LOG_DIR/access.log" touch "$LOG_DIR/access.log"
touch "$LOG_DIR/error.log" touch "$LOG_DIR/error.log"
@ -282,6 +208,7 @@ echo " 💾 Backup: $BACKUP_DIR"
echo " 📁 Logs: $LOG_DIR" echo " 📁 Logs: $LOG_DIR"
echo " 📤 Uploads: $UPLOADS_DIR" echo " 📤 Uploads: $UPLOADS_DIR"
echo " 🗃️ Data: $DATA_DIR" echo " 🗃️ Data: $DATA_DIR"
echo " 📏 Size: $(du -sh $WEBROOT | cut -f1)"
echo "" echo ""
echo "📋 Required Next Steps:" echo "📋 Required Next Steps:"
echo " 1. 🔧 Edit $WEBROOT/.env with your configuration" echo " 1. 🔧 Edit $WEBROOT/.env with your configuration"
@ -309,7 +236,8 @@ else
fi fi
if [ -f "$WEBROOT/src/data/tools.yaml" ]; then if [ -f "$WEBROOT/src/data/tools.yaml" ]; then
echo "✅ Tools database exists" TOOL_COUNT=$(grep -c "^ - name:" "$WEBROOT/src/data/tools.yaml" || echo "unknown")
echo "✅ Tools database exists ($TOOL_COUNT tools)"
else else
echo "❌ Tools database missing" echo "❌ Tools database missing"
fi fi
@ -320,220 +248,10 @@ else
echo "❌ Server directory missing" echo "❌ Server directory missing"
fi 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 ""
# Check if running as root
if [ "$EUID" -ne 0 ]; then
echo "❌ Error: This script must be run as root (use sudo)"
exit 1
fi
# Check prerequisites
echo "🔍 Checking prerequisites..."
if ! command -v npm &> /dev/null; then
echo "❌ Error: npm is not installed"
exit 1
fi
echo "✅ npm found: $(npm --version)"
if ! command -v node &> /dev/null; then
echo "❌ Error: Node.js is not installed"
exit 1
fi
echo "✅ Node.js found: $(node --version)"
# Check if dist directory exists or can be built
if [ ! -d "dist" ]; then
echo "📦 Building application..."
npm run build
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..."
npm run build
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 if [ -f "$WEBROOT/server/entry.mjs" ]; then
chmod 755 "$WEBROOT/server/entry.mjs" echo "✅ Server entry point exists"
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 else
echo "❌ Environment configuration missing" echo "⚠️ Warning: Server entry point not found"
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 fi
echo "" echo ""