| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- #!/bin/bash
- set -e
- echo "==================================="
- echo "Webshop Scraper Installation Script"
- echo "==================================="
- echo ""
- # Check if running as root
- if [ "$EUID" -ne 0 ]; then
- echo "Error: Please run as root (use sudo)"
- exit 1
- fi
- # Get the actual user who ran sudo
- ACTUAL_USER=${SUDO_USER:-$USER}
- ACTUAL_HOME=$(eval echo ~$ACTUAL_USER)
- # Installation directory
- INSTALL_DIR="/opt/webshop-scraper"
- SERVICE_NAME="webshop-scraper"
- echo "Installation directory: $INSTALL_DIR"
- echo "Service name: $SERVICE_NAME"
- echo ""
- # Prompt for API key
- echo "Please enter the API key for authentication:"
- read -s API_KEY
- echo ""
- if [ -z "$API_KEY" ]; then
- echo "Error: API key cannot be empty"
- exit 1
- fi
- # Prompt for host/IP address
- echo "Enter the host/IP address to listen on:"
- echo " - 0.0.0.0 for all interfaces (accessible from anywhere)"
- echo " - 127.0.0.1 for localhost only (accessible only from this machine)"
- echo " - or enter a specific IP address"
- echo "Default: 0.0.0.0"
- read HOST
- HOST=${HOST:-0.0.0.0}
- # Prompt for port (optional)
- echo "Enter the HTTP port (default: 3000):"
- read PORT
- PORT=${PORT:-3000}
- # Prompt for max concurrent jobs (optional)
- echo "Enter the maximum number of concurrent jobs (default: 3):"
- read MAX_CONCURRENT_JOBS
- MAX_CONCURRENT_JOBS=${MAX_CONCURRENT_JOBS:-3}
- echo ""
- echo "Configuration:"
- echo " Host: $HOST"
- echo " Port: $PORT"
- echo " Max Concurrent Jobs: $MAX_CONCURRENT_JOBS"
- echo ""
- # Create installation directory
- echo "Creating installation directory..."
- mkdir -p "$INSTALL_DIR"
- # Get the script directory (where install.sh is located)
- SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
- PROJECT_DIR="$(dirname "$SCRIPT_DIR")"
- # Check if Node.js is installed
- if ! command -v node &> /dev/null; then
- echo "Error: Node.js is not installed. Please install Node.js first."
- exit 1
- fi
- # Check if npm is installed
- if ! command -v npm &> /dev/null; then
- echo "Error: npm is not installed. Please install npm first."
- exit 1
- fi
- # Build application in the source directory first
- echo "Building application..."
- cd "$PROJECT_DIR"
- # Install all dependencies (including dev dependencies for build)
- echo "Installing build dependencies..."
- npm install
- # Build the application
- echo "Compiling TypeScript and building web assets..."
- npm run build
- # Check if build was successful
- if [ ! -f "$PROJECT_DIR/dist/index.js" ]; then
- echo "Error: Build failed - dist/index.js not found"
- exit 1
- fi
- # Copy application files
- echo "Copying application files..."
- cp -r package.json "$INSTALL_DIR/"
- cp -r dist "$INSTALL_DIR/"
- # Change to installation directory
- cd "$INSTALL_DIR"
- # Install only production dependencies
- echo "Installing production dependencies..."
- npm install --production
- # Create environment file
- echo "Creating environment file..."
- cat > "$INSTALL_DIR/.env" <<EOF
- API_KEY=$API_KEY
- HOST=$HOST
- PORT=$PORT
- MAX_CONCURRENT_JOBS=$MAX_CONCURRENT_JOBS
- EOF
- # Set permissions
- chmod 600 "$INSTALL_DIR/.env"
- chown -R $ACTUAL_USER:$ACTUAL_USER "$INSTALL_DIR"
- # Create systemd service file
- echo "Creating systemd service..."
- cat > "/etc/systemd/system/$SERVICE_NAME.service" <<EOF
- [Unit]
- Description=Webshop Scraper Service
- After=network.target
- [Service]
- Type=simple
- User=$ACTUAL_USER
- WorkingDirectory=$INSTALL_DIR
- EnvironmentFile=$INSTALL_DIR/.env
- ExecStart=/usr/bin/node $INSTALL_DIR/dist/index.js
- Restart=on-failure
- RestartSec=10
- # Logging
- StandardOutput=journal
- StandardError=journal
- SyslogIdentifier=$SERVICE_NAME
- # Security
- NoNewPrivileges=true
- PrivateTmp=true
- [Install]
- WantedBy=multi-user.target
- EOF
- # Reload systemd
- echo "Reloading systemd..."
- systemctl daemon-reload
- # Enable service
- echo "Enabling service..."
- systemctl enable "$SERVICE_NAME"
- # Start service
- echo "Starting service..."
- systemctl start "$SERVICE_NAME"
- # Check status
- echo ""
- echo "==================================="
- echo "Installation completed successfully!"
- echo "==================================="
- echo ""
- echo "Service status:"
- systemctl status "$SERVICE_NAME" --no-pager || true
- echo ""
- echo "Useful commands:"
- echo " Start service: sudo systemctl start $SERVICE_NAME"
- echo " Stop service: sudo systemctl stop $SERVICE_NAME"
- echo " Restart service: sudo systemctl restart $SERVICE_NAME"
- echo " View logs: sudo journalctl -u $SERVICE_NAME -f"
- echo " Check status: sudo systemctl status $SERVICE_NAME"
- echo ""
- echo "API is available at: http://localhost:$PORT"
- echo "Health check: http://localhost:$PORT/health"
- echo ""
|