diff --git a/deploy.sh b/deploy.sh index ba00aa2..cb637b3 100755 --- a/deploy.sh +++ b/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')" \ No newline at end of file +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')" \ No newline at end of file