| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 |
- #!/bin/bash
- set -e
- echo "==================================="
- echo "Web Scraper Installation Script"
- echo "==================================="
- echo ""
- # Check if running as root
- if [ "$EUID" -ne 0 ]; then
- echo "Error: Please run as root (use sudo)"
- exit 1
- fi
- # Get the actual user who ran sudo
- ACTUAL_USER=${SUDO_USER:-$USER}
- ACTUAL_HOME=$(eval echo ~$ACTUAL_USER)
- # Installation directory
- INSTALL_DIR="/opt/webshop-scraper"
- SERVICE_NAME="webshop-scraper"
- # Get the script directory and project directory
- SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
- PROJECT_DIR="$(dirname "$SCRIPT_DIR")"
- SOURCE_ENV_FILE="$PROJECT_DIR/.env"
- echo "Installation directory: $INSTALL_DIR"
- echo "Service name: $SERVICE_NAME"
- echo "Source directory: $PROJECT_DIR"
- echo ""
- # Check for source .env file first
- if [ -f "$SOURCE_ENV_FILE" ]; then
- echo "✓ Found .env file in source directory: $SOURCE_ENV_FILE"
- echo " Using configuration from source .env file"
- echo " All environment variables will be automatically configured"
- echo ""
- # Load the source .env to show summary
- source "$SOURCE_ENV_FILE" 2>/dev/null || true
- echo "Configuration preview:"
- [ -n "$API_KEY" ] && echo " ✓ API_KEY: ${API_KEY:0:8}... (configured)"
- [ -n "$HOST" ] && echo " ✓ HOST: $HOST"
- [ -n "$PORT" ] && echo " ✓ PORT: $PORT"
- [ -n "$QDRANT_ENABLED" ] && echo " ✓ QDRANT_ENABLED: $QDRANT_ENABLED"
- [ -n "$QDRANT_API_URL" ] && echo " ✓ QDRANT_API_URL: $QDRANT_API_URL"
- [ -n "$OPENROUTER_API_KEY" ] && echo " ✓ OPENROUTER_API_KEY: ${OPENROUTER_API_KEY:0:15}... (configured)"
- [ -n "$MCP_ENABLED" ] && echo " ✓ MCP_ENABLED: $MCP_ENABLED"
- echo ""
- USE_SOURCE_ENV=true
- SKIP_CONFIG_PROMPTS=true
- else
- echo "⚠ No .env file found in source directory"
- echo " Expected location: $SOURCE_ENV_FILE"
- echo ""
- echo "Please create a .env file in the project directory before running the installer."
- echo "You can copy .env.example and configure it:"
- echo " cp .env.example .env"
- echo " nano .env"
- echo ""
- exit 1
- fi
- # Create installation directory
- echo "Creating installation directory..."
- mkdir -p "$INSTALL_DIR"
- # Check if Node.js is installed
- if ! command -v node &> /dev/null; then
- echo "Error: Node.js is not installed. Please install Node.js first."
- exit 1
- fi
- # Check if npm is installed
- if ! command -v npm &> /dev/null; then
- echo "Error: npm is not installed. Please install npm first."
- exit 1
- fi
- # Build application in the source directory first
- echo "Building application..."
- cd "$PROJECT_DIR"
- # Install all dependencies (including dev dependencies for build)
- echo "Installing build dependencies..."
- npm install
- # Build the application
- echo "Compiling TypeScript and building web assets..."
- npm run build
- # Check if build was successful
- if [ ! -f "$PROJECT_DIR/dist/index.js" ]; then
- echo "Error: Build failed - dist/index.js not found"
- exit 1
- fi
- # Copy application files
- echo "Copying application files..."
- cp -r package.json "$INSTALL_DIR/"
- cp -r dist "$INSTALL_DIR/"
- # Change to installation directory
- cd "$INSTALL_DIR"
- # Install only production dependencies
- echo "Installing production dependencies..."
- npm install --production
- # Copy environment file from source
- echo "Copying environment file..."
- cp "$SOURCE_ENV_FILE" "$INSTALL_DIR/.env"
- echo "✓ Environment file copied from source to $INSTALL_DIR/.env"
- # Set permissions
- chmod 600 "$INSTALL_DIR/.env"
- chown -R $ACTUAL_USER:$ACTUAL_USER "$INSTALL_DIR"
- # Create systemd service file
- echo "Creating systemd service..."
- cat > "/etc/systemd/system/$SERVICE_NAME.service" <<EOF
- [Unit]
- Description=Web Scraper Service
- After=network.target
- [Service]
- Type=simple
- User=$ACTUAL_USER
- WorkingDirectory=$INSTALL_DIR
- EnvironmentFile=$INSTALL_DIR/.env
- ExecStart=/usr/bin/node $INSTALL_DIR/dist/index.js
- Restart=on-failure
- RestartSec=10
- # Logging
- StandardOutput=journal
- StandardError=journal
- SyslogIdentifier=$SERVICE_NAME
- # Security
- NoNewPrivileges=true
- PrivateTmp=true
- [Install]
- WantedBy=multi-user.target
- EOF
- # Reload systemd
- echo "Reloading systemd..."
- systemctl daemon-reload
- # Enable service
- echo "Enabling service..."
- systemctl enable "$SERVICE_NAME"
- # Handle service startup
- if systemctl is-active --quiet "$SERVICE_NAME"; then
- echo "Service is currently running. Restarting with new configuration..."
- systemctl restart "$SERVICE_NAME"
- else
- echo "Starting service..."
- systemctl start "$SERVICE_NAME"
- fi
- # Check status
- echo ""
- echo "==================================="
- echo "Installation completed successfully!"
- echo "==================================="
- echo ""
- echo "Service status:"
- systemctl status "$SERVICE_NAME" --no-pager || true
- echo ""
- echo "Useful commands:"
- echo " Start service: sudo systemctl start $SERVICE_NAME"
- echo " Stop service: sudo systemctl stop $SERVICE_NAME"
- echo " Restart service: sudo systemctl restart $SERVICE_NAME"
- echo " View logs: sudo journalctl -u $SERVICE_NAME -f"
- echo " Check status: sudo systemctl status $SERVICE_NAME"
- echo ""
- # Load .env to get actual values for display
- source "$INSTALL_DIR/.env" 2>/dev/null || true
- # Display appropriate URL based on host configuration
- DISPLAY_HOST="${HOST:-localhost}"
- if [ "$DISPLAY_HOST" = "0.0.0.0" ]; then
- DISPLAY_HOST="localhost"
- fi
- echo "Configuration loaded from: $SOURCE_ENV_FILE"
- echo "API is available at: http://$DISPLAY_HOST:${PORT:-3000}"
- echo "Health check: http://$DISPLAY_HOST:${PORT:-3000}/health"
- # Show Qdrant status if enabled
- if [ "$QDRANT_ENABLED" = "true" ]; then
- echo ""
- echo "Qdrant Integration: ENABLED"
- echo " Qdrant URL: $QDRANT_API_URL"
- echo " OpenRouter: ${OPENROUTER_API_KEY:+CONFIGURED}"
- echo " MCP Server: ${MCP_ENABLED}"
- fi
- echo ""
|