فهرست منبع

feat: add systemd install and uninstall scripts

Add automated installation scripts for systemd service management:
- install.sh: Automatically detects paths and creates systemd service
- uninstall.sh: Removes systemd service and cleans up
- Updated SYSTEMD.md with installation instructions

Resolves #9

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
Claude 9 ماه پیش
والد
کامیت
1f39ddaa15
3فایلهای تغییر یافته به همراه329 افزوده شده و 4 حذف شده
  1. 30 4
      SYSTEMD.md
  2. 193 0
      install.sh
  3. 106 0
      uninstall.sh

+ 30 - 4
SYSTEMD.md

@@ -10,7 +10,33 @@ This document explains how to install and run the Agent Manager as a systemd ser
 
 ## Installation Steps
 
-### 1. Prepare the application
+### Quick Installation (Recommended)
+
+The easiest way to install the systemd service is using the automated installation script:
+
+```bash
+cd /home/claude/agent-manager
+./install.sh
+```
+
+The script will automatically:
+- Detect the project directory and user
+- Verify Node.js version (>= 18.0.0)
+- Create .env and commands.json from examples if needed
+- Substitute correct paths in the service file
+- Install and enable the systemd service
+- Optionally start the service
+
+To uninstall the service:
+```bash
+./uninstall.sh
+```
+
+### Manual Installation
+
+If you prefer to install manually:
+
+#### 1. Prepare the application
 
 Clone the repository to the user's home directory:
 ```bash
@@ -19,7 +45,7 @@ git clone <repository-url> agent-manager
 cd agent-manager
 ```
 
-### 2. Configure the application
+#### 2. Configure the application
 
 Create your `.env` file from the example:
 ```bash
@@ -41,7 +67,7 @@ Edit `commands.json` to configure your webhook commands:
 nano commands.json
 ```
 
-### 3. Install the systemd service
+#### 3. Install the systemd service
 
 Copy the service file to systemd directory (requires sudo):
 ```bash
@@ -59,7 +85,7 @@ Update these fields:
 - `Group`: The group to run as
 - `ReadWritePaths`: Path to your installation (for writing queue.json)
 
-### 4. Enable and start the service
+#### 4. Enable and start the service
 
 Reload systemd to recognize the new service:
 ```bash

+ 193 - 0
install.sh

@@ -0,0 +1,193 @@
+#!/bin/bash
+
+# Agent Manager - Systemd Service Installation Script
+# This script installs the agent-manager as a systemd service
+
+set -e  # Exit on error
+
+# Colors for output
+RED='\033[0;31m'
+GREEN='\033[0;32m'
+YELLOW='\033[1;33m'
+NC='\033[0m' # No Color
+
+# Print functions
+print_info() {
+    echo -e "${GREEN}[INFO]${NC} $1"
+}
+
+print_warning() {
+    echo -e "${YELLOW}[WARNING]${NC} $1"
+}
+
+print_error() {
+    echo -e "${RED}[ERROR]${NC} $1"
+}
+
+# Check if running as root
+if [ "$EUID" -eq 0 ]; then
+    print_error "Please do not run this script as root or with sudo"
+    print_info "The script will prompt for sudo password when needed"
+    exit 1
+fi
+
+# Detect the project directory (where this script is located)
+SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
+PROJECT_DIR="$SCRIPT_DIR"
+
+print_info "Project directory detected: $PROJECT_DIR"
+
+# Detect current user and group
+CURRENT_USER="$USER"
+CURRENT_GROUP="$(id -gn)"
+
+print_info "Service will run as user: $CURRENT_USER"
+print_info "Service will run as group: $CURRENT_GROUP"
+
+# Check if systemd is available
+if ! command -v systemctl &> /dev/null; then
+    print_error "systemd is not available on this system"
+    print_error "This script requires a systemd-based Linux distribution"
+    exit 1
+fi
+
+# Check if Node.js is installed
+if ! command -v node &> /dev/null; then
+    print_error "Node.js is not installed"
+    print_error "Please install Node.js >= 18.0.0 before running this script"
+    exit 1
+fi
+
+# Check Node.js version
+NODE_VERSION=$(node --version | cut -d'v' -f2)
+NODE_MAJOR_VERSION=$(echo "$NODE_VERSION" | cut -d'.' -f1)
+
+if [ "$NODE_MAJOR_VERSION" -lt 18 ]; then
+    print_error "Node.js version $NODE_VERSION is not supported"
+    print_error "Please install Node.js >= 18.0.0"
+    exit 1
+fi
+
+print_info "Node.js version: $NODE_VERSION (OK)"
+
+# Detect node path
+NODE_PATH=$(which node)
+print_info "Node.js path: $NODE_PATH"
+
+# Check if service file exists
+if [ ! -f "$PROJECT_DIR/agent-manager.service" ]; then
+    print_error "Service file not found: $PROJECT_DIR/agent-manager.service"
+    exit 1
+fi
+
+# Check if .env file exists
+if [ ! -f "$PROJECT_DIR/.env" ]; then
+    print_warning ".env file not found"
+    if [ -f "$PROJECT_DIR/.env.example" ]; then
+        print_info "Creating .env from .env.example"
+        cp "$PROJECT_DIR/.env.example" "$PROJECT_DIR/.env"
+        print_warning "Please edit .env file to configure your settings"
+    else
+        print_error "Neither .env nor .env.example found"
+        print_error "Please create .env file before installing the service"
+        exit 1
+    fi
+fi
+
+# Check if commands.json exists
+if [ ! -f "$PROJECT_DIR/commands.json" ]; then
+    print_warning "commands.json file not found"
+    if [ -f "$PROJECT_DIR/commands.json.example" ]; then
+        print_info "Creating commands.json from commands.json.example"
+        cp "$PROJECT_DIR/commands.json.example" "$PROJECT_DIR/commands.json"
+        print_warning "Please edit commands.json to configure your webhook commands"
+    else
+        print_error "Neither commands.json nor commands.json.example found"
+        print_error "Please create commands.json before installing the service"
+        exit 1
+    fi
+fi
+
+# Create temporary service file with substitutions
+TEMP_SERVICE_FILE=$(mktemp)
+print_info "Creating service file with correct paths..."
+
+# Read the template and substitute variables
+sed -e "s|WorkingDirectory=.*|WorkingDirectory=$PROJECT_DIR|g" \
+    -e "s|User=.*|User=$CURRENT_USER|g" \
+    -e "s|Group=.*|Group=$CURRENT_GROUP|g" \
+    -e "s|ExecStart=.*|ExecStart=$NODE_PATH src/index.js|g" \
+    -e "s|ReadWritePaths=.*|ReadWritePaths=$PROJECT_DIR|g" \
+    "$PROJECT_DIR/agent-manager.service" > "$TEMP_SERVICE_FILE"
+
+print_info "Service file prepared with the following settings:"
+echo "  - WorkingDirectory: $PROJECT_DIR"
+echo "  - User: $CURRENT_USER"
+echo "  - Group: $CURRENT_GROUP"
+echo "  - ExecStart: $NODE_PATH src/index.js"
+echo "  - ReadWritePaths: $PROJECT_DIR"
+echo ""
+
+# Confirm installation
+read -p "Do you want to proceed with the installation? (y/N): " -n 1 -r
+echo
+if [[ ! $REPLY =~ ^[Yy]$ ]]; then
+    print_warning "Installation cancelled"
+    rm "$TEMP_SERVICE_FILE"
+    exit 0
+fi
+
+# Install the service file
+print_info "Installing service file..."
+sudo cp "$TEMP_SERVICE_FILE" /etc/systemd/system/agent-manager.service
+rm "$TEMP_SERVICE_FILE"
+
+# Set correct permissions
+sudo chmod 644 /etc/systemd/system/agent-manager.service
+
+# Reload systemd
+print_info "Reloading systemd daemon..."
+sudo systemctl daemon-reload
+
+# Enable the service
+print_info "Enabling agent-manager service..."
+sudo systemctl enable agent-manager
+
+# Ask if user wants to start the service now
+echo ""
+read -p "Do you want to start the service now? (Y/n): " -n 1 -r
+echo
+if [[ ! $REPLY =~ ^[Nn]$ ]]; then
+    print_info "Starting agent-manager service..."
+    sudo systemctl start agent-manager
+
+    # Wait a moment for the service to start
+    sleep 2
+
+    # Check service status
+    if sudo systemctl is-active --quiet agent-manager; then
+        print_info "✓ Service installed and started successfully!"
+        echo ""
+        print_info "Service status:"
+        sudo systemctl status agent-manager --no-pager -l
+    else
+        print_error "Service failed to start. Checking logs..."
+        echo ""
+        sudo journalctl -u agent-manager -n 50 --no-pager
+        exit 1
+    fi
+else
+    print_info "✓ Service installed successfully (not started)"
+    print_info "To start the service later, run: sudo systemctl start agent-manager"
+fi
+
+echo ""
+print_info "Useful commands:"
+echo "  - Check status:    sudo systemctl status agent-manager"
+echo "  - View logs:       journalctl -u agent-manager -f -n100"
+echo "  - Stop service:    sudo systemctl stop agent-manager"
+echo "  - Restart service: sudo systemctl restart agent-manager"
+echo "  - Disable service: sudo systemctl disable agent-manager"
+echo "  - Uninstall:       ./uninstall.sh"
+echo ""
+print_info "Installation complete!"

+ 106 - 0
uninstall.sh

@@ -0,0 +1,106 @@
+#!/bin/bash
+
+# Agent Manager - Systemd Service Uninstallation Script
+# This script removes the agent-manager systemd service
+
+set -e  # Exit on error
+
+# Colors for output
+RED='\033[0;31m'
+GREEN='\033[0;32m'
+YELLOW='\033[1;33m'
+NC='\033[0m' # No Color
+
+# Print functions
+print_info() {
+    echo -e "${GREEN}[INFO]${NC} $1"
+}
+
+print_warning() {
+    echo -e "${YELLOW}[WARNING]${NC} $1"
+}
+
+print_error() {
+    echo -e "${RED}[ERROR]${NC} $1"
+}
+
+# Check if running as root
+if [ "$EUID" -eq 0 ]; then
+    print_error "Please do not run this script as root or with sudo"
+    print_info "The script will prompt for sudo password when needed"
+    exit 1
+fi
+
+# Check if systemd is available
+if ! command -v systemctl &> /dev/null; then
+    print_error "systemd is not available on this system"
+    exit 1
+fi
+
+# Check if service file exists
+if [ ! -f /etc/systemd/system/agent-manager.service ]; then
+    print_warning "Service file not found: /etc/systemd/system/agent-manager.service"
+    print_info "The service may not be installed, or it was already removed"
+    exit 0
+fi
+
+print_warning "This will remove the agent-manager systemd service"
+echo "The following actions will be performed:"
+echo "  1. Stop the agent-manager service"
+echo "  2. Disable the agent-manager service"
+echo "  3. Remove the service file from /etc/systemd/system/"
+echo "  4. Reload systemd daemon"
+echo ""
+print_info "Note: This will NOT remove:"
+echo "  - The project directory and files"
+echo "  - Configuration files (.env, commands.json)"
+echo "  - Queue data (queue.json)"
+echo ""
+
+# Confirm uninstallation
+read -p "Do you want to proceed with the uninstallation? (y/N): " -n 1 -r
+echo
+if [[ ! $REPLY =~ ^[Yy]$ ]]; then
+    print_warning "Uninstallation cancelled"
+    exit 0
+fi
+
+# Check if service is running
+if sudo systemctl is-active --quiet agent-manager; then
+    print_info "Stopping agent-manager service..."
+    sudo systemctl stop agent-manager
+    print_info "✓ Service stopped"
+else
+    print_info "Service is not running"
+fi
+
+# Check if service is enabled
+if sudo systemctl is-enabled --quiet agent-manager 2>/dev/null; then
+    print_info "Disabling agent-manager service..."
+    sudo systemctl disable agent-manager
+    print_info "✓ Service disabled"
+else
+    print_info "Service is not enabled"
+fi
+
+# Remove the service file
+print_info "Removing service file..."
+sudo rm -f /etc/systemd/system/agent-manager.service
+print_info "✓ Service file removed"
+
+# Reload systemd
+print_info "Reloading systemd daemon..."
+sudo systemctl daemon-reload
+
+# Reset failed state if any
+sudo systemctl reset-failed agent-manager 2>/dev/null || true
+
+echo ""
+print_info "✓ Uninstallation complete!"
+echo ""
+print_info "The project files remain in place. If you want to remove them:"
+echo "  - Configuration: .env, commands.json"
+echo "  - Queue data: queue.json"
+echo "  - Project directory: $PWD"
+echo ""
+print_info "To reinstall the service, run: ./install.sh"