#!/bin/bash # Service setup module for Transmission RSS Manager Installation # Setup systemd service function setup_service() { log "INFO" "Setting up systemd service..." # Ensure required variables are set if [ -z "$SERVICE_NAME" ]; then log "ERROR" "SERVICE_NAME variable is not set" exit 1 fi if [ -z "$USER" ]; then log "ERROR" "USER variable is not set" exit 1 fi if [ -z "$INSTALL_DIR" ]; then log "ERROR" "INSTALL_DIR variable is not set" exit 1 fi if [ -z "$PORT" ]; then log "ERROR" "PORT variable is not set" exit 1 fi # Check if systemd is available if ! command -v systemctl &> /dev/null; then log "ERROR" "systemd is not available on this system" log "INFO" "Please set up the service manually using your system's service manager" return 1 fi # Create backup of existing service file if it exists if [ -f "/etc/systemd/system/$SERVICE_NAME.service" ]; then backup_file "/etc/systemd/system/$SERVICE_NAME.service" fi # Create systemd service file SERVICE_FILE="/etc/systemd/system/$SERVICE_NAME.service" cat > "$SERVICE_FILE" << EOF [Unit] Description=Transmission RSS Manager After=network.target transmission-daemon.service Wants=network-online.target [Service] Type=simple User=$USER WorkingDirectory=$INSTALL_DIR ExecStart=/usr/bin/node $INSTALL_DIR/server.js Restart=always RestartSec=10 StandardOutput=journal StandardError=journal Environment=PORT=$PORT Environment=NODE_ENV=production Environment=DEBUG_ENABLED=false Environment=LOG_FILE=$INSTALL_DIR/logs/transmission-rss-manager.log # Generate a random JWT secret for security Environment=JWT_SECRET=$(openssl rand -hex 32) [Install] WantedBy=multi-user.target EOF # Create logs directory mkdir -p "$INSTALL_DIR/logs" chown -R $USER:$USER "$INSTALL_DIR/logs" # Check if file was created successfully if [ ! -f "$SERVICE_FILE" ]; then log "ERROR" "Failed to create systemd service file" return 1 fi log "INFO" "Setting up Nginx reverse proxy..." # Check if nginx is installed if ! command -v nginx &> /dev/null; then log "ERROR" "Nginx is not installed" log "INFO" "Skipping Nginx configuration. Please configure your web server manually." # Reload systemd and enable service systemctl daemon-reload systemctl enable "$SERVICE_NAME" log "INFO" "Systemd service has been created and enabled." log "INFO" "The service will start automatically after installation." return 0 fi # Detect nginx configuration directory NGINX_AVAILABLE_DIR="" NGINX_ENABLED_DIR="" if [ -d "/etc/nginx/sites-available" ] && [ -d "/etc/nginx/sites-enabled" ]; then # Debian/Ubuntu style NGINX_AVAILABLE_DIR="/etc/nginx/sites-available" NGINX_ENABLED_DIR="/etc/nginx/sites-enabled" elif [ -d "/etc/nginx/conf.d" ]; then # CentOS/RHEL style NGINX_AVAILABLE_DIR="/etc/nginx/conf.d" NGINX_ENABLED_DIR="/etc/nginx/conf.d" else log "WARN" "Unable to determine Nginx configuration directory" log "INFO" "Please configure Nginx manually" # Reload systemd and enable service systemctl daemon-reload systemctl enable "$SERVICE_NAME" log "INFO" "Systemd service has been created and enabled." log "INFO" "The service will start automatically after installation." return 0 fi # Check if default nginx file exists, back it up if it does if [ -f "$NGINX_ENABLED_DIR/default" ]; then backup_file "$NGINX_ENABLED_DIR/default" if [ -f "$NGINX_ENABLED_DIR/default.bak" ]; then log "INFO" "Backed up default nginx configuration." fi fi # Create nginx configuration file NGINX_CONFIG_FILE="$NGINX_AVAILABLE_DIR/$SERVICE_NAME.conf" cat > "$NGINX_CONFIG_FILE" << EOF server { listen 80; server_name _; location / { proxy_pass http://127.0.0.1:$PORT; proxy_http_version 1.1; proxy_set_header Upgrade \$http_upgrade; proxy_set_header Connection 'upgrade'; proxy_set_header Host \$host; proxy_cache_bypass \$http_upgrade; proxy_set_header X-Real-IP \$remote_addr; proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto \$scheme; } } EOF # Check if Debian/Ubuntu style (need symlink between available and enabled) if [ "$NGINX_AVAILABLE_DIR" != "$NGINX_ENABLED_DIR" ]; then # Create symbolic link to enable the site (if it doesn't already exist) if [ ! -h "$NGINX_ENABLED_DIR/$SERVICE_NAME.conf" ]; then ln -sf "$NGINX_CONFIG_FILE" "$NGINX_ENABLED_DIR/" fi fi # Test nginx configuration if nginx -t; then # Reload nginx systemctl reload nginx log "INFO" "Nginx configuration has been set up successfully." else log "ERROR" "Nginx configuration test failed. Please check the configuration manually." log "WARN" "You may need to correct the configuration before the web interface will be accessible." fi # Check for port conflicts if ss -lnt | grep ":$PORT " &> /dev/null; then log "WARN" "Port $PORT is already in use. This may cause conflicts with the service." log "WARN" "Consider changing the port if you encounter issues." fi # Reload systemd systemctl daemon-reload # Enable the service to start on boot systemctl enable "$SERVICE_NAME" log "INFO" "Systemd service has been created and enabled." log "INFO" "The service will start automatically after installation." }