install.sh 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. #!/bin/bash
  2. set -e
  3. echo "==================================="
  4. echo "Webshop Scraper Installation Script"
  5. echo "==================================="
  6. echo ""
  7. # Check if running as root
  8. if [ "$EUID" -ne 0 ]; then
  9. echo "Error: Please run as root (use sudo)"
  10. exit 1
  11. fi
  12. # Get the actual user who ran sudo
  13. ACTUAL_USER=${SUDO_USER:-$USER}
  14. ACTUAL_HOME=$(eval echo ~$ACTUAL_USER)
  15. # Installation directory
  16. INSTALL_DIR="/opt/webshop-scraper"
  17. SERVICE_NAME="webshop-scraper"
  18. # Get the script directory and project directory
  19. SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  20. PROJECT_DIR="$(dirname "$SCRIPT_DIR")"
  21. SOURCE_ENV_FILE="$PROJECT_DIR/.env"
  22. echo "Installation directory: $INSTALL_DIR"
  23. echo "Service name: $SERVICE_NAME"
  24. echo "Source directory: $PROJECT_DIR"
  25. echo ""
  26. # Check for source .env file first
  27. if [ -f "$SOURCE_ENV_FILE" ]; then
  28. echo "✓ Found .env file in source directory: $SOURCE_ENV_FILE"
  29. echo " Using configuration from source .env file"
  30. echo " All environment variables will be automatically configured"
  31. echo ""
  32. # Load the source .env to show summary
  33. source "$SOURCE_ENV_FILE" 2>/dev/null || true
  34. echo "Configuration preview:"
  35. [ -n "$API_KEY" ] && echo " ✓ API_KEY: ${API_KEY:0:8}... (configured)"
  36. [ -n "$HOST" ] && echo " ✓ HOST: $HOST"
  37. [ -n "$PORT" ] && echo " ✓ PORT: $PORT"
  38. [ -n "$QDRANT_ENABLED" ] && echo " ✓ QDRANT_ENABLED: $QDRANT_ENABLED"
  39. [ -n "$QDRANT_API_URL" ] && echo " ✓ QDRANT_API_URL: $QDRANT_API_URL"
  40. [ -n "$OPENROUTER_API_KEY" ] && echo " ✓ OPENROUTER_API_KEY: ${OPENROUTER_API_KEY:0:15}... (configured)"
  41. [ -n "$MCP_ENABLED" ] && echo " ✓ MCP_ENABLED: $MCP_ENABLED"
  42. echo ""
  43. USE_SOURCE_ENV=true
  44. SKIP_CONFIG_PROMPTS=true
  45. else
  46. echo "⚠ No .env file found in source directory"
  47. echo " Expected location: $SOURCE_ENV_FILE"
  48. echo ""
  49. echo "Please create a .env file in the project directory before running the installer."
  50. echo "You can copy .env.example and configure it:"
  51. echo " cp .env.example .env"
  52. echo " nano .env"
  53. echo ""
  54. exit 1
  55. fi
  56. # Create installation directory
  57. echo "Creating installation directory..."
  58. mkdir -p "$INSTALL_DIR"
  59. # Check if Node.js is installed
  60. if ! command -v node &> /dev/null; then
  61. echo "Error: Node.js is not installed. Please install Node.js first."
  62. exit 1
  63. fi
  64. # Check if npm is installed
  65. if ! command -v npm &> /dev/null; then
  66. echo "Error: npm is not installed. Please install npm first."
  67. exit 1
  68. fi
  69. # Build application in the source directory first
  70. echo "Building application..."
  71. cd "$PROJECT_DIR"
  72. # Install all dependencies (including dev dependencies for build)
  73. echo "Installing build dependencies..."
  74. npm install
  75. # Build the application
  76. echo "Compiling TypeScript and building web assets..."
  77. npm run build
  78. # Check if build was successful
  79. if [ ! -f "$PROJECT_DIR/dist/index.js" ]; then
  80. echo "Error: Build failed - dist/index.js not found"
  81. exit 1
  82. fi
  83. # Copy application files
  84. echo "Copying application files..."
  85. cp -r package.json "$INSTALL_DIR/"
  86. cp -r dist "$INSTALL_DIR/"
  87. # Change to installation directory
  88. cd "$INSTALL_DIR"
  89. # Install only production dependencies
  90. echo "Installing production dependencies..."
  91. npm install --production
  92. # Copy environment file from source
  93. echo "Copying environment file..."
  94. cp "$SOURCE_ENV_FILE" "$INSTALL_DIR/.env"
  95. echo "✓ Environment file copied from source to $INSTALL_DIR/.env"
  96. # Set permissions
  97. chmod 600 "$INSTALL_DIR/.env"
  98. chown -R $ACTUAL_USER:$ACTUAL_USER "$INSTALL_DIR"
  99. # Create systemd service file
  100. echo "Creating systemd service..."
  101. cat > "/etc/systemd/system/$SERVICE_NAME.service" <<EOF
  102. [Unit]
  103. Description=Webshop Scraper Service
  104. After=network.target
  105. [Service]
  106. Type=simple
  107. User=$ACTUAL_USER
  108. WorkingDirectory=$INSTALL_DIR
  109. EnvironmentFile=$INSTALL_DIR/.env
  110. ExecStart=/usr/bin/node $INSTALL_DIR/dist/index.js
  111. Restart=on-failure
  112. RestartSec=10
  113. # Logging
  114. StandardOutput=journal
  115. StandardError=journal
  116. SyslogIdentifier=$SERVICE_NAME
  117. # Security
  118. NoNewPrivileges=true
  119. PrivateTmp=true
  120. [Install]
  121. WantedBy=multi-user.target
  122. EOF
  123. # Reload systemd
  124. echo "Reloading systemd..."
  125. systemctl daemon-reload
  126. # Enable service
  127. echo "Enabling service..."
  128. systemctl enable "$SERVICE_NAME"
  129. # Handle service startup
  130. if systemctl is-active --quiet "$SERVICE_NAME"; then
  131. echo "Service is currently running. Restarting with new configuration..."
  132. systemctl restart "$SERVICE_NAME"
  133. else
  134. echo "Starting service..."
  135. systemctl start "$SERVICE_NAME"
  136. fi
  137. # Check status
  138. echo ""
  139. echo "==================================="
  140. echo "Installation completed successfully!"
  141. echo "==================================="
  142. echo ""
  143. echo "Service status:"
  144. systemctl status "$SERVICE_NAME" --no-pager || true
  145. echo ""
  146. echo "Useful commands:"
  147. echo " Start service: sudo systemctl start $SERVICE_NAME"
  148. echo " Stop service: sudo systemctl stop $SERVICE_NAME"
  149. echo " Restart service: sudo systemctl restart $SERVICE_NAME"
  150. echo " View logs: sudo journalctl -u $SERVICE_NAME -f"
  151. echo " Check status: sudo systemctl status $SERVICE_NAME"
  152. echo ""
  153. # Load .env to get actual values for display
  154. source "$INSTALL_DIR/.env" 2>/dev/null || true
  155. # Display appropriate URL based on host configuration
  156. DISPLAY_HOST="${HOST:-localhost}"
  157. if [ "$DISPLAY_HOST" = "0.0.0.0" ]; then
  158. DISPLAY_HOST="localhost"
  159. fi
  160. echo "Configuration loaded from: $SOURCE_ENV_FILE"
  161. echo "API is available at: http://$DISPLAY_HOST:${PORT:-3000}"
  162. echo "Health check: http://$DISPLAY_HOST:${PORT:-3000}/health"
  163. # Show Qdrant status if enabled
  164. if [ "$QDRANT_ENABLED" = "true" ]; then
  165. echo ""
  166. echo "Qdrant Integration: ENABLED"
  167. echo " Qdrant URL: $QDRANT_API_URL"
  168. echo " OpenRouter: ${OPENROUTER_API_KEY:+CONFIGURED}"
  169. echo " MCP Server: ${MCP_ENABLED}"
  170. fi
  171. echo ""