From 1beefb93bb45fe708590b958c5b3b7eb85340c93 Mon Sep 17 00:00:00 2001 From: overcuriousity Date: Thu, 7 Aug 2025 10:39:51 +0200 Subject: [PATCH] script --- deploy.sh | 341 ++++++++++++++++++++++++------------------------------ 1 file changed, 154 insertions(+), 187 deletions(-) diff --git a/deploy.sh b/deploy.sh index 34b24af..037fc10 100755 --- a/deploy.sh +++ b/deploy.sh @@ -1,6 +1,5 @@ #!/bin/bash - -# ForensicPathways Deployment Script +# ForensicPathways Deployment Script – *ownership-aware* # Usage: sudo ./deploy.sh set -e @@ -20,245 +19,213 @@ echo "πŸ‘€ Original user: $ORIGINAL_USER" echo "πŸ“ Working directory: $(pwd)" echo "" -# Check if running as root +############################################################################### +# 0. Safety checks +############################################################################### if [ "$EUID" -ne 0 ]; then - echo "❌ Error: This script must be run as root (use sudo)" - exit 1 + 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 + echo "❌ Error: Must run from ForensicPathways project root" + echo "πŸ” Current directory: $(pwd)"; echo "πŸ” Files found: $(ls -la)"; exit 1 fi -# Function to find and use npm +############################################################################### +# 1. Helper – build with whichever npm is available for the original user +############################################################################### find_and_use_npm() { - echo "πŸ” Searching for npm installation..." - - # Try system npm first - if command -v npm &> /dev/null; then - echo "βœ… Found system npm: $(which npm)" - echo "πŸ“¦ Installing dependencies..." - sudo -u "$ORIGINAL_USER" npm install - echo "πŸ“¦ Building application..." - sudo -u "$ORIGINAL_USER" npm run build - return 0 - fi - - # Try nvm-installed npm - echo "πŸ” Checking for nvm installation..." - if sudo -u "$ORIGINAL_USER" bash -c " - export NVM_DIR='$ORIGINAL_HOME/.nvm' - [ -s '\$NVM_DIR/nvm.sh' ] && source '\$NVM_DIR/nvm.sh' - [ -s '$ORIGINAL_HOME/.bashrc' ] && source '$ORIGINAL_HOME/.bashrc' - command -v npm &> /dev/null - "; then - echo "βœ… Found nvm-managed npm" - echo "πŸ“¦ Installing dependencies with nvm..." - sudo -u "$ORIGINAL_USER" bash -c " - export NVM_DIR='$ORIGINAL_HOME/.nvm' - [ -s '\$NVM_DIR/nvm.sh' ] && source '\$NVM_DIR/nvm.sh' - [ -s '$ORIGINAL_HOME/.bashrc' ] && source '$ORIGINAL_HOME/.bashrc' - npm install - " - echo "πŸ“¦ Building application with nvm..." - sudo -u "$ORIGINAL_USER" bash -c " - export NVM_DIR='$ORIGINAL_HOME/.nvm' - [ -s '\$NVM_DIR/nvm.sh' ] && source '\$NVM_DIR/nvm.sh' - [ -s '$ORIGINAL_HOME/.bashrc' ] && source '$ORIGINAL_HOME/.bashrc' - npm run build - " - return 0 - fi - - echo "❌ npm not found in system or user environment" - echo "" - echo "πŸ’‘ Please install Node.js and npm first:" - echo " # Option 1: System package manager" - echo " sudo apt update && sudo apt install nodejs npm" - echo "" - echo " # Option 2: NodeSource repository (recommended)" - echo " curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -" - echo " sudo apt-get install -y nodejs" - echo "" - echo " # Option 3: nvm (as user $ORIGINAL_USER)" - echo " curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash" - echo " source ~/.bashrc" - echo " nvm install 20" - echo "" - return 1 + echo "πŸ” Searching for npm installation..." + + # A) system-wide npm + if command -v npm &>/dev/null; then + echo "βœ… Found system npm: $(which npm)" + echo "πŸ“¦ Installing dependencies…" + sudo -u "$ORIGINAL_USER" npm install + echo "πŸ“¦ Building application…" + sudo -u "$ORIGINAL_USER" npm run build + return 0 + fi + + # B) nvm-managed npm + echo "πŸ” Checking for nvm installation..." + if sudo -u "$ORIGINAL_USER" bash -c " + export NVM_DIR='$ORIGINAL_HOME/.nvm' + [ -s \"\$NVM_DIR/nvm.sh\" ] && source \"\$NVM_DIR/nvm.sh\" + [ -s '$ORIGINAL_HOME/.bashrc' ] && source '$ORIGINAL_HOME/.bashrc' + command -v npm &>/dev/null + "; then + echo "βœ… Found nvm-managed npm" + echo "πŸ“¦ Installing dependencies with nvm…" + sudo -u "$ORIGINAL_USER" bash -c " + export NVM_DIR='$ORIGINAL_HOME/.nvm' + [ -s \"\$NVM_DIR/nvm.sh\" ] && source \"\$NVM_DIR/nvm.sh\" + [ -s '$ORIGINAL_HOME/.bashrc' ] && source '$ORIGINAL_HOME/.bashrc' + npm install + npm run build + " + return 0 + fi + + # C) nothing found + cat <<'EOF' +❌ npm not found in system or user environment + +πŸ’‘ Please install Node.js and npm first: + # Option 1 (apt): + sudo apt update && sudo apt install nodejs npm + # Option 2 (NodeSource – recommended): + curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash - + sudo apt-get install -y nodejs + # Option 3 (nvm – as user): + curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash + source ~/.bashrc && nvm install 20 +EOF + return 1 } -# Check for existing build or build if needed +############################################################################### +# 2. Build (if needed) – runs as ORIGINAL_USER so $PATH is intact +############################################################################### if [ ! -d "dist" ] || [ ! "$(ls -A dist 2>/dev/null)" ]; then - echo "πŸ“¦ No dist/ directory found, building..." - if ! find_and_use_npm; then - exit 1 - fi + echo "πŸ“¦ No dist/ directory found, building…" + find_and_use_npm || exit 1 else - echo "πŸ“¦ Found existing dist/ directory" - read -p "πŸ€” Rebuild application? (y/N): " -n 1 -r - echo - if [[ $REPLY =~ ^[Yy]$ ]]; then - if ! find_and_use_npm; then - echo "" - echo "πŸ’‘ Using existing dist/ due to build failure" - fi - else - echo "πŸ“¦ Using existing build" - fi + echo "πŸ“¦ Found existing dist/ directory" + read -rp "πŸ€” Rebuild application? (y/N): " REPLY; echo + if [[ $REPLY =~ ^[Yy]$ ]]; then + find_and_use_npm || { echo "πŸ’‘ Using existing dist/ due to build failure"; } + else + echo "πŸ“¦ Using existing build" + fi fi -# 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 + echo "❌ Error: Build failed or dist/ is empty"; exit 1 fi - echo "βœ… Build completed successfully" -# Create target directories +############################################################################### +# 3. Prepare 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" "$LOG_DIR" "$DATA_DIR" "$UPLOADS_DIR" "$WEBROOT/src/data" -# Copy application files -echo "πŸ“‹ Copying application files..." +############################################################################### +# 4. Deploy build files +############################################################################### +echo "πŸ“‹ Copying application files…" cp -r dist/. "$WEBROOT/" echo "βœ… Application files copied ($(du -sh dist | cut -f1))" -# Copy package.json for runtime dependencies -echo "πŸ“¦ Setting up runtime dependencies..." cp package.json "$WEBROOT/" echo "βœ… package.json copied" -# Install production dependencies in webroot -echo "πŸ“¦ Installing runtime dependencies..." -cd "$WEBROOT" -if command -v npm &> /dev/null; then +############################################################################### +# 5. **Runtime dependencies** – temporarily chown to ORIGINAL_USER +############################################################################### +echo "πŸ“¦ Installing runtime dependencies…" + +# Temporary hand-off +chown -R "$ORIGINAL_USER":"$ORIGINAL_USER" "$WEBROOT" + +sudo -u "$ORIGINAL_USER" bash -c ' + set -e + cd "'"$WEBROOT"'" + if command -v npm &>/dev/null; then npm install --production - echo "βœ… Runtime dependencies installed" -else - sudo -u "$ORIGINAL_USER" bash -c " - cd '$WEBROOT' - export NVM_DIR='$ORIGINAL_HOME/.nvm' - [ -s '\$NVM_DIR/nvm.sh' ] && source '\$NVM_DIR/nvm.sh' - [ -s '$ORIGINAL_HOME/.bashrc' ] && source '$ORIGINAL_HOME/.bashrc' - npm install --production - " - echo "βœ… Runtime dependencies installed via nvm" -fi + else + export NVM_DIR="'$ORIGINAL_HOME'/.nvm" + [ -s "$NVM_DIR/nvm.sh" ] && source "$NVM_DIR/nvm.sh" + [ -s "'$ORIGINAL_HOME'/.bashrc" ] && source "'$ORIGINAL_HOME'/.bashrc" + npm install --production + fi +' +echo "βœ… Runtime dependencies installed" -# Return to source directory -cd - > /dev/null - -# Copy essential data files -echo "πŸ—‚οΈ Setting up data files..." +############################################################################### +# 6. Additional data & content +############################################################################### +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)" + 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 + 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)" + 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..." +############################################################################### +# 7. Environment configuration +############################################################################### +echo "πŸ”§ Setting up environment configuration…" cp .env.example "$WEBROOT/.env" echo "βœ… Created .env from .env.example template" echo "⚠️ IMPORTANT: Edit $WEBROOT/.env with your configuration" -# Create log files -echo "πŸ“ Creating log files..." -touch "$LOG_DIR/access.log" -touch "$LOG_DIR/error.log" -touch "$LOG_DIR/ai-pipeline.log" +############################################################################### +# 8. Logs +############################################################################### +echo "πŸ“ Creating log files…" +touch "$LOG_DIR/access.log" "$LOG_DIR/error.log" "$LOG_DIR/ai-pipeline.log" -# Set permissions -echo "πŸ” Setting permissions..." +############################################################################### +# 9. FINAL permissions – hand back to www-data +############################################################################### +echo "πŸ” Setting final 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 755 "$DATA_DIR" "$UPLOADS_DIR" "$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" + chmod 755 "$WEBROOT/server/entry.mjs" + echo "βœ… Server entry point permissions set" fi - echo "βœ… Permissions configured" -# Final validation +############################################################################### +# 10. Post-deployment validation +############################################################################### echo "" -echo "πŸ” Post-deployment validation..." +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 +[ -f "$WEBROOT/.env" ] && echo "βœ… Environment configuration exists" || { echo "❌ Environment configuration missing"; ((VALIDATION_ERRORS++)); } +[ -f "$WEBROOT/src/data/tools.yaml" ] && echo "βœ… Tools database exists" || { echo "❌ Tools database missing"; ((VALIDATION_ERRORS++)); } +{ [ -f "$WEBROOT/index.html" ] || [ -d "$WEBROOT/server" ]; } && \ + echo "βœ… Application files deployed" || { echo "❌ Application files missing"; ((VALIDATION_ERRORS++)); } 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" + cat <