install.sh 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 port (optional)
  30. echo "Enter the HTTP port (default: 3000):"
  31. read PORT
  32. PORT=${PORT:-3000}
  33. # Prompt for max concurrent jobs (optional)
  34. echo "Enter the maximum number of concurrent jobs (default: 3):"
  35. read MAX_CONCURRENT_JOBS
  36. MAX_CONCURRENT_JOBS=${MAX_CONCURRENT_JOBS:-3}
  37. echo ""
  38. echo "Configuration:"
  39. echo " Port: $PORT"
  40. echo " Max Concurrent Jobs: $MAX_CONCURRENT_JOBS"
  41. echo ""
  42. # Create installation directory
  43. echo "Creating installation directory..."
  44. mkdir -p "$INSTALL_DIR"
  45. # Copy application files
  46. echo "Copying application files..."
  47. cp -r package.json tsconfig.json src "$INSTALL_DIR/"
  48. # Change to installation directory
  49. cd "$INSTALL_DIR"
  50. # Check if Node.js is installed
  51. if ! command -v node &> /dev/null; then
  52. echo "Error: Node.js is not installed. Please install Node.js first."
  53. exit 1
  54. fi
  55. # Check if npm is installed
  56. if ! command -v npm &> /dev/null; then
  57. echo "Error: npm is not installed. Please install npm first."
  58. exit 1
  59. fi
  60. # Install dependencies
  61. echo "Installing dependencies..."
  62. npm install --production
  63. # Build TypeScript
  64. echo "Building application..."
  65. npm run build
  66. # Create environment file
  67. echo "Creating environment file..."
  68. cat > "$INSTALL_DIR/.env" <<EOF
  69. API_KEY=$API_KEY
  70. PORT=$PORT
  71. MAX_CONCURRENT_JOBS=$MAX_CONCURRENT_JOBS
  72. EOF
  73. # Set permissions
  74. chmod 600 "$INSTALL_DIR/.env"
  75. chown -R $ACTUAL_USER:$ACTUAL_USER "$INSTALL_DIR"
  76. # Create systemd service file
  77. echo "Creating systemd service..."
  78. cat > "/etc/systemd/system/$SERVICE_NAME.service" <<EOF
  79. [Unit]
  80. Description=Webshop Scraper Service
  81. After=network.target
  82. [Service]
  83. Type=simple
  84. User=$ACTUAL_USER
  85. WorkingDirectory=$INSTALL_DIR
  86. EnvironmentFile=$INSTALL_DIR/.env
  87. ExecStart=/usr/bin/node $INSTALL_DIR/dist/index.js
  88. Restart=on-failure
  89. RestartSec=10
  90. # Logging
  91. StandardOutput=journal
  92. StandardError=journal
  93. SyslogIdentifier=$SERVICE_NAME
  94. # Security
  95. NoNewPrivileges=true
  96. PrivateTmp=true
  97. [Install]
  98. WantedBy=multi-user.target
  99. EOF
  100. # Reload systemd
  101. echo "Reloading systemd..."
  102. systemctl daemon-reload
  103. # Enable service
  104. echo "Enabling service..."
  105. systemctl enable "$SERVICE_NAME"
  106. # Start service
  107. echo "Starting service..."
  108. systemctl start "$SERVICE_NAME"
  109. # Check status
  110. echo ""
  111. echo "==================================="
  112. echo "Installation completed successfully!"
  113. echo "==================================="
  114. echo ""
  115. echo "Service status:"
  116. systemctl status "$SERVICE_NAME" --no-pager || true
  117. echo ""
  118. echo "Useful commands:"
  119. echo " Start service: sudo systemctl start $SERVICE_NAME"
  120. echo " Stop service: sudo systemctl stop $SERVICE_NAME"
  121. echo " Restart service: sudo systemctl restart $SERVICE_NAME"
  122. echo " View logs: sudo journalctl -u $SERVICE_NAME -f"
  123. echo " Check status: sudo systemctl status $SERVICE_NAME"
  124. echo ""
  125. echo "API is available at: http://localhost:$PORT"
  126. echo "Health check: http://localhost:$PORT/health"
  127. echo ""