|
@@ -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!"
|