install.sh 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. echo "Installation directory: $INSTALL_DIR"
  19. echo "Service name: $SERVICE_NAME"
  20. echo ""
  21. # Check if application is already installed
  22. EXISTING_INSTALLATION=false
  23. EXISTING_ENV_FILE="$INSTALL_DIR/.env"
  24. if [ -f "$EXISTING_ENV_FILE" ]; then
  25. EXISTING_INSTALLATION=true
  26. echo "Existing installation detected!"
  27. echo "Found existing configuration at: $EXISTING_ENV_FILE"
  28. echo ""
  29. # Load existing configuration
  30. source "$EXISTING_ENV_FILE" 2>/dev/null || true
  31. # Set defaults from existing config or fallback to hardcoded defaults
  32. EXISTING_API_KEY="$API_KEY"
  33. EXISTING_HOST="${HOST:-0.0.0.0}"
  34. EXISTING_PORT="${PORT:-3000}"
  35. EXISTING_MAX_CONCURRENT_JOBS="${MAX_CONCURRENT_JOBS:-3}"
  36. echo "Current configuration:"
  37. echo " API Key: ${EXISTING_API_KEY:0:8}... (masked)"
  38. echo " Host: $EXISTING_HOST"
  39. echo " Port: $EXISTING_PORT"
  40. echo " Max Concurrent Jobs: $EXISTING_MAX_CONCURRENT_JOBS"
  41. echo ""
  42. # Ask if user wants to keep existing configuration
  43. echo "Do you want to keep the existing configuration? [Y/n]:"
  44. read KEEP_CONFIG
  45. KEEP_CONFIG=${KEEP_CONFIG:-Y}
  46. if [[ "$KEEP_CONFIG" =~ ^[Yy]([Ee][Ss])?$ ]]; then
  47. echo "Using existing configuration..."
  48. API_KEY="$EXISTING_API_KEY"
  49. HOST="$EXISTING_HOST"
  50. PORT="$EXISTING_PORT"
  51. MAX_CONCURRENT_JOBS="$EXISTING_MAX_CONCURRENT_JOBS"
  52. SKIP_CONFIG_PROMPTS=true
  53. else
  54. echo "Will prompt for new configuration..."
  55. SKIP_CONFIG_PROMPTS=false
  56. fi
  57. echo ""
  58. else
  59. echo "No existing installation found. Starting fresh installation..."
  60. echo ""
  61. SKIP_CONFIG_PROMPTS=false
  62. fi
  63. # Configuration prompts (skip if using existing config)
  64. if [ "$SKIP_CONFIG_PROMPTS" != "true" ]; then
  65. # Prompt for API key
  66. if [ "$EXISTING_INSTALLATION" = "true" ]; then
  67. echo "Enter new API key for authentication (current: ${EXISTING_API_KEY:0:8}...):"
  68. echo "Press Enter to keep current API key, or enter new one:"
  69. read -s NEW_API_KEY
  70. if [ -n "$NEW_API_KEY" ]; then
  71. API_KEY="$NEW_API_KEY"
  72. else
  73. API_KEY="$EXISTING_API_KEY"
  74. fi
  75. else
  76. echo "Please enter the API key for authentication:"
  77. read -s API_KEY
  78. fi
  79. echo ""
  80. if [ -z "$API_KEY" ]; then
  81. echo "Error: API key cannot be empty"
  82. exit 1
  83. fi
  84. # Prompt for host/IP address
  85. echo "Enter the host/IP address to listen on:"
  86. echo " - 0.0.0.0 for all interfaces (accessible from anywhere)"
  87. echo " - 127.0.0.1 for localhost only (accessible only from this machine)"
  88. echo " - or enter a specific IP address"
  89. if [ "$EXISTING_INSTALLATION" = "true" ]; then
  90. echo "Current: $EXISTING_HOST, Default: $EXISTING_HOST"
  91. read HOST
  92. HOST=${HOST:-$EXISTING_HOST}
  93. else
  94. echo "Default: 0.0.0.0"
  95. read HOST
  96. HOST=${HOST:-0.0.0.0}
  97. fi
  98. # Prompt for port (optional)
  99. if [ "$EXISTING_INSTALLATION" = "true" ]; then
  100. echo "Enter the HTTP port (current: $EXISTING_PORT):"
  101. read PORT
  102. PORT=${PORT:-$EXISTING_PORT}
  103. else
  104. echo "Enter the HTTP port (default: 3000):"
  105. read PORT
  106. PORT=${PORT:-3000}
  107. fi
  108. # Prompt for max concurrent jobs (optional)
  109. if [ "$EXISTING_INSTALLATION" = "true" ]; then
  110. echo "Enter the maximum number of concurrent jobs (current: $EXISTING_MAX_CONCURRENT_JOBS):"
  111. read MAX_CONCURRENT_JOBS
  112. MAX_CONCURRENT_JOBS=${MAX_CONCURRENT_JOBS:-$EXISTING_MAX_CONCURRENT_JOBS}
  113. else
  114. echo "Enter the maximum number of concurrent jobs (default: 3):"
  115. read MAX_CONCURRENT_JOBS
  116. MAX_CONCURRENT_JOBS=${MAX_CONCURRENT_JOBS:-3}
  117. fi
  118. fi
  119. echo ""
  120. echo "Configuration:"
  121. echo " Host: $HOST"
  122. echo " Port: $PORT"
  123. echo " Max Concurrent Jobs: $MAX_CONCURRENT_JOBS"
  124. echo ""
  125. # Create installation directory
  126. echo "Creating installation directory..."
  127. mkdir -p "$INSTALL_DIR"
  128. # Get the script directory (where install.sh is located)
  129. SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  130. PROJECT_DIR="$(dirname "$SCRIPT_DIR")"
  131. # Check if Node.js is installed
  132. if ! command -v node &> /dev/null; then
  133. echo "Error: Node.js is not installed. Please install Node.js first."
  134. exit 1
  135. fi
  136. # Check if npm is installed
  137. if ! command -v npm &> /dev/null; then
  138. echo "Error: npm is not installed. Please install npm first."
  139. exit 1
  140. fi
  141. # Build application in the source directory first
  142. echo "Building application..."
  143. cd "$PROJECT_DIR"
  144. # Install all dependencies (including dev dependencies for build)
  145. echo "Installing build dependencies..."
  146. npm install
  147. # Build the application
  148. echo "Compiling TypeScript and building web assets..."
  149. npm run build
  150. # Check if build was successful
  151. if [ ! -f "$PROJECT_DIR/dist/index.js" ]; then
  152. echo "Error: Build failed - dist/index.js not found"
  153. exit 1
  154. fi
  155. # Copy application files
  156. echo "Copying application files..."
  157. cp -r package.json "$INSTALL_DIR/"
  158. cp -r dist "$INSTALL_DIR/"
  159. # Change to installation directory
  160. cd "$INSTALL_DIR"
  161. # Install only production dependencies
  162. echo "Installing production dependencies..."
  163. npm install --production
  164. # Create environment file
  165. echo "Creating environment file..."
  166. # Check if source .env file exists
  167. SOURCE_ENV_FILE="$PROJECT_DIR/.env"
  168. if [ -f "$SOURCE_ENV_FILE" ]; then
  169. echo "Found .env file in source directory, using it..."
  170. # Copy the entire .env file from source
  171. cp "$SOURCE_ENV_FILE" "$INSTALL_DIR/.env"
  172. echo ".env file copied from source directory"
  173. else
  174. # Fallback to creating from prompted values
  175. cat > "$INSTALL_DIR/.env" <<EOF
  176. API_KEY=$API_KEY
  177. HOST=$HOST
  178. PORT=$PORT
  179. MAX_CONCURRENT_JOBS=$MAX_CONCURRENT_JOBS
  180. EOF
  181. echo ".env file created with basic configuration"
  182. echo ""
  183. echo "NOTE: For Qdrant and OpenRouter integration, please add these variables to $INSTALL_DIR/.env:"
  184. echo " QDRANT_ENABLED=true"
  185. echo " QDRANT_API_URL=http://your-qdrant-server:6333"
  186. echo " QDRANT_API_KEY=your-qdrant-api-key"
  187. echo " OPENROUTER_API_KEY=your-openrouter-api-key"
  188. echo " MCP_ENABLED=true"
  189. echo " MCP_PORT=3001"
  190. echo ""
  191. fi
  192. # Set permissions
  193. chmod 600 "$INSTALL_DIR/.env"
  194. chown -R $ACTUAL_USER:$ACTUAL_USER "$INSTALL_DIR"
  195. # Create systemd service file
  196. echo "Creating systemd service..."
  197. cat > "/etc/systemd/system/$SERVICE_NAME.service" <<EOF
  198. [Unit]
  199. Description=Webshop Scraper Service
  200. After=network.target
  201. [Service]
  202. Type=simple
  203. User=$ACTUAL_USER
  204. WorkingDirectory=$INSTALL_DIR
  205. EnvironmentFile=$INSTALL_DIR/.env
  206. ExecStart=/usr/bin/node $INSTALL_DIR/dist/index.js
  207. Restart=on-failure
  208. RestartSec=10
  209. # Logging
  210. StandardOutput=journal
  211. StandardError=journal
  212. SyslogIdentifier=$SERVICE_NAME
  213. # Security
  214. NoNewPrivileges=true
  215. PrivateTmp=true
  216. [Install]
  217. WantedBy=multi-user.target
  218. EOF
  219. # Reload systemd
  220. echo "Reloading systemd..."
  221. systemctl daemon-reload
  222. # Enable service
  223. echo "Enabling service..."
  224. systemctl enable "$SERVICE_NAME"
  225. # Handle service startup based on existing installation
  226. if [ "$EXISTING_INSTALLATION" = "true" ]; then
  227. # Check if service is already running
  228. if systemctl is-active --quiet "$SERVICE_NAME"; then
  229. echo "Service is currently running. Restarting with new configuration..."
  230. systemctl restart "$SERVICE_NAME"
  231. else
  232. echo "Starting service..."
  233. systemctl start "$SERVICE_NAME"
  234. fi
  235. else
  236. # Fresh installation - start service
  237. echo "Starting service..."
  238. systemctl start "$SERVICE_NAME"
  239. fi
  240. # Check status
  241. echo ""
  242. echo "==================================="
  243. if [ "$EXISTING_INSTALLATION" = "true" ]; then
  244. echo "Update completed successfully!"
  245. else
  246. echo "Installation completed successfully!"
  247. fi
  248. echo "==================================="
  249. echo ""
  250. echo "Service status:"
  251. systemctl status "$SERVICE_NAME" --no-pager || true
  252. echo ""
  253. echo "Useful commands:"
  254. echo " Start service: sudo systemctl start $SERVICE_NAME"
  255. echo " Stop service: sudo systemctl stop $SERVICE_NAME"
  256. echo " Restart service: sudo systemctl restart $SERVICE_NAME"
  257. echo " View logs: sudo journalctl -u $SERVICE_NAME -f"
  258. echo " Check status: sudo systemctl status $SERVICE_NAME"
  259. echo ""
  260. # Display appropriate URL based on host configuration
  261. if [ "$HOST" = "0.0.0.0" ]; then
  262. DISPLAY_HOST="localhost"
  263. else
  264. DISPLAY_HOST="$HOST"
  265. fi
  266. echo "API is available at: http://$DISPLAY_HOST:$PORT"
  267. echo "Health check: http://$DISPLAY_HOST:$PORT/health"
  268. echo ""