install.sh 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. # Get the script directory (where install.sh is located)
  46. SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  47. PROJECT_DIR="$(dirname "$SCRIPT_DIR")"
  48. # Check if Node.js is installed
  49. if ! command -v node &> /dev/null; then
  50. echo "Error: Node.js is not installed. Please install Node.js first."
  51. exit 1
  52. fi
  53. # Check if npm is installed
  54. if ! command -v npm &> /dev/null; then
  55. echo "Error: npm is not installed. Please install npm first."
  56. exit 1
  57. fi
  58. # Build application in the source directory first
  59. echo "Building application..."
  60. cd "$PROJECT_DIR"
  61. # Install all dependencies (including dev dependencies for build)
  62. echo "Installing build dependencies..."
  63. npm install
  64. # Build the application
  65. echo "Compiling TypeScript and building web assets..."
  66. npm run build
  67. # Check if build was successful
  68. if [ ! -f "$PROJECT_DIR/dist/index.js" ]; then
  69. echo "Error: Build failed - dist/index.js not found"
  70. exit 1
  71. fi
  72. # Copy application files
  73. echo "Copying application files..."
  74. cp -r package.json "$INSTALL_DIR/"
  75. cp -r dist "$INSTALL_DIR/"
  76. # Change to installation directory
  77. cd "$INSTALL_DIR"
  78. # Install only production dependencies
  79. echo "Installing production dependencies..."
  80. npm install --production
  81. # Create environment file
  82. echo "Creating environment file..."
  83. cat > "$INSTALL_DIR/.env" <<EOF
  84. API_KEY=$API_KEY
  85. PORT=$PORT
  86. MAX_CONCURRENT_JOBS=$MAX_CONCURRENT_JOBS
  87. EOF
  88. # Set permissions
  89. chmod 600 "$INSTALL_DIR/.env"
  90. chown -R $ACTUAL_USER:$ACTUAL_USER "$INSTALL_DIR"
  91. # Create systemd service file
  92. echo "Creating systemd service..."
  93. cat > "/etc/systemd/system/$SERVICE_NAME.service" <<EOF
  94. [Unit]
  95. Description=Webshop Scraper Service
  96. After=network.target
  97. [Service]
  98. Type=simple
  99. User=$ACTUAL_USER
  100. WorkingDirectory=$INSTALL_DIR
  101. EnvironmentFile=$INSTALL_DIR/.env
  102. ExecStart=/usr/bin/node $INSTALL_DIR/dist/index.js
  103. Restart=on-failure
  104. RestartSec=10
  105. # Logging
  106. StandardOutput=journal
  107. StandardError=journal
  108. SyslogIdentifier=$SERVICE_NAME
  109. # Security
  110. NoNewPrivileges=true
  111. PrivateTmp=true
  112. [Install]
  113. WantedBy=multi-user.target
  114. EOF
  115. # Reload systemd
  116. echo "Reloading systemd..."
  117. systemctl daemon-reload
  118. # Enable service
  119. echo "Enabling service..."
  120. systemctl enable "$SERVICE_NAME"
  121. # Start service
  122. echo "Starting service..."
  123. systemctl start "$SERVICE_NAME"
  124. # Check status
  125. echo ""
  126. echo "==================================="
  127. echo "Installation completed successfully!"
  128. echo "==================================="
  129. echo ""
  130. echo "Service status:"
  131. systemctl status "$SERVICE_NAME" --no-pager || true
  132. echo ""
  133. echo "Useful commands:"
  134. echo " Start service: sudo systemctl start $SERVICE_NAME"
  135. echo " Stop service: sudo systemctl stop $SERVICE_NAME"
  136. echo " Restart service: sudo systemctl restart $SERVICE_NAME"
  137. echo " View logs: sudo journalctl -u $SERVICE_NAME -f"
  138. echo " Check status: sudo systemctl status $SERVICE_NAME"
  139. echo ""
  140. echo "API is available at: http://localhost:$PORT"
  141. echo "Health check: http://localhost:$PORT/health"
  142. echo ""