install.sh 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. #!/bin/bash
  2. # Agent Manager - Systemd Service Installation Script
  3. # This script installs the agent-manager as a systemd service
  4. set -e # Exit on error
  5. # Colors for output
  6. RED='\033[0;31m'
  7. GREEN='\033[0;32m'
  8. YELLOW='\033[1;33m'
  9. NC='\033[0m' # No Color
  10. # Print functions
  11. print_info() {
  12. echo -e "${GREEN}[INFO]${NC} $1"
  13. }
  14. print_warning() {
  15. echo -e "${YELLOW}[WARNING]${NC} $1"
  16. }
  17. print_error() {
  18. echo -e "${RED}[ERROR]${NC} $1"
  19. }
  20. # Check if running as root
  21. if [ "$EUID" -eq 0 ]; then
  22. print_error "Please do not run this script as root or with sudo"
  23. print_info "The script will prompt for sudo password when needed"
  24. exit 1
  25. fi
  26. # Detect the project directory (where this script is located)
  27. SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
  28. PROJECT_DIR="$SCRIPT_DIR"
  29. print_info "Project directory detected: $PROJECT_DIR"
  30. # Detect current user and group
  31. CURRENT_USER="$USER"
  32. CURRENT_GROUP="$(id -gn)"
  33. print_info "Service will run as user: $CURRENT_USER"
  34. print_info "Service will run as group: $CURRENT_GROUP"
  35. # Check if systemd is available
  36. if ! command -v systemctl &> /dev/null; then
  37. print_error "systemd is not available on this system"
  38. print_error "This script requires a systemd-based Linux distribution"
  39. exit 1
  40. fi
  41. # Check if Node.js is installed
  42. if ! command -v node &> /dev/null; then
  43. print_error "Node.js is not installed"
  44. print_error "Please install Node.js >= 18.0.0 before running this script"
  45. exit 1
  46. fi
  47. # Check Node.js version
  48. NODE_VERSION=$(node --version | cut -d'v' -f2)
  49. NODE_MAJOR_VERSION=$(echo "$NODE_VERSION" | cut -d'.' -f1)
  50. if [ "$NODE_MAJOR_VERSION" -lt 18 ]; then
  51. print_error "Node.js version $NODE_VERSION is not supported"
  52. print_error "Please install Node.js >= 18.0.0"
  53. exit 1
  54. fi
  55. print_info "Node.js version: $NODE_VERSION (OK)"
  56. # Detect node path
  57. NODE_PATH=$(which node)
  58. print_info "Node.js path: $NODE_PATH"
  59. # Check if service file exists
  60. if [ ! -f "$PROJECT_DIR/agent-manager.service" ]; then
  61. print_error "Service file not found: $PROJECT_DIR/agent-manager.service"
  62. exit 1
  63. fi
  64. # Check if .env file exists
  65. if [ ! -f "$PROJECT_DIR/.env" ]; then
  66. print_warning ".env file not found"
  67. if [ -f "$PROJECT_DIR/.env.example" ]; then
  68. print_info "Creating .env from .env.example"
  69. cp "$PROJECT_DIR/.env.example" "$PROJECT_DIR/.env"
  70. print_warning "Please edit .env file to configure your settings"
  71. else
  72. print_error "Neither .env nor .env.example found"
  73. print_error "Please create .env file before installing the service"
  74. exit 1
  75. fi
  76. fi
  77. # Check if commands.json exists
  78. if [ ! -f "$PROJECT_DIR/commands.json" ]; then
  79. print_warning "commands.json file not found"
  80. if [ -f "$PROJECT_DIR/commands.json.example" ]; then
  81. print_info "Creating commands.json from commands.json.example"
  82. cp "$PROJECT_DIR/commands.json.example" "$PROJECT_DIR/commands.json"
  83. print_warning "Please edit commands.json to configure your webhook commands"
  84. else
  85. print_error "Neither commands.json nor commands.json.example found"
  86. print_error "Please create commands.json before installing the service"
  87. exit 1
  88. fi
  89. fi
  90. # Create temporary service file with substitutions
  91. TEMP_SERVICE_FILE=$(mktemp)
  92. print_info "Creating service file with correct paths..."
  93. # Read the template and substitute variables
  94. sed -e "s|WorkingDirectory=.*|WorkingDirectory=$PROJECT_DIR|g" \
  95. -e "s|User=.*|User=$CURRENT_USER|g" \
  96. -e "s|Group=.*|Group=$CURRENT_GROUP|g" \
  97. -e "s|ExecStart=.*|ExecStart=$NODE_PATH src/index.js|g" \
  98. -e "s|ReadWritePaths=.*|ReadWritePaths=$PROJECT_DIR|g" \
  99. "$PROJECT_DIR/agent-manager.service" > "$TEMP_SERVICE_FILE"
  100. print_info "Service file prepared with the following settings:"
  101. echo " - WorkingDirectory: $PROJECT_DIR"
  102. echo " - User: $CURRENT_USER"
  103. echo " - Group: $CURRENT_GROUP"
  104. echo " - ExecStart: $NODE_PATH src/index.js"
  105. echo " - ReadWritePaths: $PROJECT_DIR"
  106. echo ""
  107. # Confirm installation
  108. read -p "Do you want to proceed with the installation? (y/N): " -n 1 -r
  109. echo
  110. if [[ ! $REPLY =~ ^[Yy]$ ]]; then
  111. print_warning "Installation cancelled"
  112. rm "$TEMP_SERVICE_FILE"
  113. exit 0
  114. fi
  115. # Install the service file
  116. print_info "Installing service file..."
  117. sudo cp "$TEMP_SERVICE_FILE" /etc/systemd/system/agent-manager.service
  118. rm "$TEMP_SERVICE_FILE"
  119. # Set correct permissions
  120. sudo chmod 644 /etc/systemd/system/agent-manager.service
  121. # Reload systemd
  122. print_info "Reloading systemd daemon..."
  123. sudo systemctl daemon-reload
  124. # Enable the service
  125. print_info "Enabling agent-manager service..."
  126. sudo systemctl enable agent-manager
  127. # Ask if user wants to start the service now
  128. echo ""
  129. read -p "Do you want to start the service now? (Y/n): " -n 1 -r
  130. echo
  131. if [[ ! $REPLY =~ ^[Nn]$ ]]; then
  132. print_info "Starting agent-manager service..."
  133. sudo systemctl start agent-manager
  134. # Wait a moment for the service to start
  135. sleep 2
  136. # Check service status
  137. if sudo systemctl is-active --quiet agent-manager; then
  138. print_info "✓ Service installed and started successfully!"
  139. echo ""
  140. print_info "Service status:"
  141. sudo systemctl status agent-manager --no-pager -l
  142. else
  143. print_error "Service failed to start. Checking logs..."
  144. echo ""
  145. sudo journalctl -u agent-manager -n 50 --no-pager
  146. exit 1
  147. fi
  148. else
  149. print_info "✓ Service installed successfully (not started)"
  150. print_info "To start the service later, run: sudo systemctl start agent-manager"
  151. fi
  152. echo ""
  153. print_info "Useful commands:"
  154. echo " - Check status: sudo systemctl status agent-manager"
  155. echo " - View logs: journalctl -u agent-manager -f -n100"
  156. echo " - Stop service: sudo systemctl stop agent-manager"
  157. echo " - Restart service: sudo systemctl restart agent-manager"
  158. echo " - Disable service: sudo systemctl disable agent-manager"
  159. echo " - Uninstall: ./uninstall.sh"
  160. echo ""
  161. print_info "Installation complete!"