install.sh 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. # Prompt for API key
  22. echo "Please enter the API key for authentication:"
  23. read -s API_KEY
  24. echo ""
  25. if [ -z "$API_KEY" ]; then
  26. echo "Error: API key cannot be empty"
  27. exit 1
  28. fi
  29. # Prompt for host/IP address
  30. echo "Enter the host/IP address to listen on:"
  31. echo " - 0.0.0.0 for all interfaces (accessible from anywhere)"
  32. echo " - 127.0.0.1 for localhost only (accessible only from this machine)"
  33. echo " - or enter a specific IP address"
  34. echo "Default: 0.0.0.0"
  35. read HOST
  36. HOST=${HOST:-0.0.0.0}
  37. # Prompt for port (optional)
  38. echo "Enter the HTTP port (default: 3000):"
  39. read PORT
  40. PORT=${PORT:-3000}
  41. # Prompt for max concurrent jobs (optional)
  42. echo "Enter the maximum number of concurrent jobs (default: 3):"
  43. read MAX_CONCURRENT_JOBS
  44. MAX_CONCURRENT_JOBS=${MAX_CONCURRENT_JOBS:-3}
  45. echo ""
  46. echo "Configuration:"
  47. echo " Host: $HOST"
  48. echo " Port: $PORT"
  49. echo " Max Concurrent Jobs: $MAX_CONCURRENT_JOBS"
  50. echo ""
  51. # Create installation directory
  52. echo "Creating installation directory..."
  53. mkdir -p "$INSTALL_DIR"
  54. # Get the script directory (where install.sh is located)
  55. SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  56. PROJECT_DIR="$(dirname "$SCRIPT_DIR")"
  57. # Check if Node.js is installed
  58. if ! command -v node &> /dev/null; then
  59. echo "Error: Node.js is not installed. Please install Node.js first."
  60. exit 1
  61. fi
  62. # Check if npm is installed
  63. if ! command -v npm &> /dev/null; then
  64. echo "Error: npm is not installed. Please install npm first."
  65. exit 1
  66. fi
  67. # Build application in the source directory first
  68. echo "Building application..."
  69. cd "$PROJECT_DIR"
  70. # Install all dependencies (including dev dependencies for build)
  71. echo "Installing build dependencies..."
  72. npm install
  73. # Build the application
  74. echo "Compiling TypeScript and building web assets..."
  75. npm run build
  76. # Check if build was successful
  77. if [ ! -f "$PROJECT_DIR/dist/index.js" ]; then
  78. echo "Error: Build failed - dist/index.js not found"
  79. exit 1
  80. fi
  81. # Copy application files
  82. echo "Copying application files..."
  83. cp -r package.json "$INSTALL_DIR/"
  84. cp -r dist "$INSTALL_DIR/"
  85. # Change to installation directory
  86. cd "$INSTALL_DIR"
  87. # Install only production dependencies
  88. echo "Installing production dependencies..."
  89. npm install --production
  90. # Create environment file
  91. echo "Creating environment file..."
  92. cat > "$INSTALL_DIR/.env" <<EOF
  93. API_KEY=$API_KEY
  94. HOST=$HOST
  95. PORT=$PORT
  96. MAX_CONCURRENT_JOBS=$MAX_CONCURRENT_JOBS
  97. EOF
  98. # Set permissions
  99. chmod 600 "$INSTALL_DIR/.env"
  100. chown -R $ACTUAL_USER:$ACTUAL_USER "$INSTALL_DIR"
  101. # Create systemd service file
  102. echo "Creating systemd service..."
  103. cat > "/etc/systemd/system/$SERVICE_NAME.service" <<EOF
  104. [Unit]
  105. Description=Webshop Scraper Service
  106. After=network.target
  107. [Service]
  108. Type=simple
  109. User=$ACTUAL_USER
  110. WorkingDirectory=$INSTALL_DIR
  111. EnvironmentFile=$INSTALL_DIR/.env
  112. ExecStart=/usr/bin/node $INSTALL_DIR/dist/index.js
  113. Restart=on-failure
  114. RestartSec=10
  115. # Logging
  116. StandardOutput=journal
  117. StandardError=journal
  118. SyslogIdentifier=$SERVICE_NAME
  119. # Security
  120. NoNewPrivileges=true
  121. PrivateTmp=true
  122. [Install]
  123. WantedBy=multi-user.target
  124. EOF
  125. # Reload systemd
  126. echo "Reloading systemd..."
  127. systemctl daemon-reload
  128. # Enable service
  129. echo "Enabling service..."
  130. systemctl enable "$SERVICE_NAME"
  131. # Start service
  132. echo "Starting service..."
  133. systemctl start "$SERVICE_NAME"
  134. # Check status
  135. echo ""
  136. echo "==================================="
  137. echo "Installation completed successfully!"
  138. echo "==================================="
  139. echo ""
  140. echo "Service status:"
  141. systemctl status "$SERVICE_NAME" --no-pager || true
  142. echo ""
  143. echo "Useful commands:"
  144. echo " Start service: sudo systemctl start $SERVICE_NAME"
  145. echo " Stop service: sudo systemctl stop $SERVICE_NAME"
  146. echo " Restart service: sudo systemctl restart $SERVICE_NAME"
  147. echo " View logs: sudo journalctl -u $SERVICE_NAME -f"
  148. echo " Check status: sudo systemctl status $SERVICE_NAME"
  149. echo ""
  150. echo "API is available at: http://localhost:$PORT"
  151. echo "Health check: http://localhost:$PORT/health"
  152. echo ""