- Added systemd services for system-level persistence (gateway + monitor) - Enhanced agent-monitor.js with auto-healing and Telegram notifications - Created deploy.sh for one-click deployment and management - Updated CORE_INDEX.md with complete architecture documentation - Updated MEMORY.md with implementation details and usage guide - All memory files now tracked in git for version control and rollback Features implemented: ✓ System-Level: Services auto-start on boot, survive logout/reboot ✓ Auto-Healing: Crash detection, auto-restart with rate limiting ✓ Multi-Layer Memory: Core (CORE_INDEX.md) + Long-term (MEMORY.md) + Daily (memory/) ✓ Git Rollback: ./deploy.sh rollback / rollback-to <commit> ✓ Telegram Notifications: Alerts on stop/error/restart eventsmaster
parent
5707edd78a
commit
820530d1ec
7 changed files with 719 additions and 48 deletions
@ -0,0 +1,290 @@ |
||||
#!/bin/bash |
||||
|
||||
############################################################################### |
||||
# OpenClaw System Deployment & Management Script |
||||
# |
||||
# Features: |
||||
# - One-click deployment of OpenClaw with systemd services |
||||
# - Auto-healing configuration |
||||
# - Health monitoring |
||||
# - Rollback support via git |
||||
# - Telegram notifications |
||||
# |
||||
# Usage: |
||||
# ./deploy.sh install - Install and start all services |
||||
# ./deploy.sh start - Start all services |
||||
# ./deploy.sh stop - Stop all services |
||||
# ./deploy.sh restart - Restart all services |
||||
# ./deploy.sh status - Show service status |
||||
# ./deploy.sh logs - Show recent logs |
||||
# ./deploy.sh rollback - Rollback to previous git commit |
||||
# ./deploy.sh backup - Create backup of current state |
||||
############################################################################### |
||||
|
||||
set -e |
||||
|
||||
WORKSPACE="/root/.openclaw/workspace" |
||||
LOG_DIR="/root/.openclaw/workspace/logs/system" |
||||
TIMESTAMP=$(date +%Y%m%d-%H%M%S) |
||||
|
||||
# Colors for output |
||||
RED='\033[0;31m' |
||||
GREEN='\033[0;32m' |
||||
YELLOW='\033[1;33m' |
||||
BLUE='\033[0;34m' |
||||
NC='\033[0m' # No Color |
||||
|
||||
log_info() { |
||||
echo -e "${BLUE}[INFO]${NC} $1" |
||||
} |
||||
|
||||
log_success() { |
||||
echo -e "${GREEN}[SUCCESS]${NC} $1" |
||||
} |
||||
|
||||
log_warning() { |
||||
echo -e "${YELLOW}[WARNING]${NC} $1" |
||||
} |
||||
|
||||
log_error() { |
||||
echo -e "${RED}[ERROR]${NC} $1" |
||||
} |
||||
|
||||
ensure_log_dir() { |
||||
mkdir -p "$LOG_DIR" |
||||
} |
||||
|
||||
install_services() { |
||||
log_info "Installing OpenClaw systemd services..." |
||||
|
||||
# Copy service files |
||||
cp "$WORKSPACE/systemd/openclaw-gateway.service" /etc/systemd/system/ |
||||
cp "$WORKSPACE/systemd/openclaw-agent-monitor.service" /etc/systemd/system/ |
||||
|
||||
# Reload systemd |
||||
systemctl daemon-reload |
||||
|
||||
# Enable services |
||||
systemctl enable openclaw-gateway |
||||
systemctl enable openclaw-agent-monitor |
||||
|
||||
# Start services |
||||
systemctl start openclaw-gateway |
||||
systemctl start openclaw-agent-monitor |
||||
|
||||
log_success "OpenClaw services installed and started!" |
||||
log_info "Gateway: http://localhost:18789" |
||||
log_info "Logs: journalctl -u openclaw-gateway -f" |
||||
} |
||||
|
||||
start_services() { |
||||
log_info "Starting OpenClaw services..." |
||||
systemctl start openclaw-gateway |
||||
systemctl start openclaw-agent-monitor |
||||
log_success "Services started!" |
||||
} |
||||
|
||||
stop_services() { |
||||
log_info "Stopping OpenClaw services..." |
||||
systemctl stop openclaw-gateway |
||||
systemctl stop openclaw-agent-monitor |
||||
log_success "Services stopped!" |
||||
} |
||||
|
||||
restart_services() { |
||||
log_info "Restarting OpenClaw services..." |
||||
systemctl restart openclaw-gateway |
||||
systemctl restart openclaw-agent-monitor |
||||
log_success "Services restarted!" |
||||
} |
||||
|
||||
show_status() { |
||||
echo "" |
||||
log_info "=== OpenClaw Gateway Status ===" |
||||
systemctl status openclaw-gateway --no-pager -l |
||||
echo "" |
||||
log_info "=== Agent Monitor Status ===" |
||||
systemctl status openclaw-agent-monitor --no-pager -l |
||||
echo "" |
||||
log_info "=== Recent Logs ===" |
||||
journalctl -u openclaw-gateway -u openclaw-agent-monitor --no-pager -n 20 |
||||
} |
||||
|
||||
show_logs() { |
||||
log_info "Showing recent logs (last 50 lines)..." |
||||
journalctl -u openclaw-gateway -u openclaw-agent-monitor --no-pager -n 50 |
||||
} |
||||
|
||||
rollback() { |
||||
log_warning "This will rollback the workspace to the previous git commit!" |
||||
read -p "Are you sure? (y/N): " confirm |
||||
|
||||
if [[ $confirm =~ ^[Yy]$ ]]; then |
||||
cd "$WORKSPACE" |
||||
|
||||
# Create backup before rollback |
||||
backup |
||||
|
||||
# Show current commit |
||||
log_info "Current commit:" |
||||
git log -1 --oneline |
||||
|
||||
# Rollback |
||||
git reset --hard HEAD~1 |
||||
|
||||
log_success "Rolled back to previous commit!" |
||||
log_info "Restarting services to apply changes..." |
||||
restart_services |
||||
else |
||||
log_info "Rollback cancelled." |
||||
fi |
||||
} |
||||
|
||||
rollback_to() { |
||||
if [ -z "$1" ]; then |
||||
log_error "Please specify a commit hash or tag" |
||||
exit 1 |
||||
fi |
||||
|
||||
log_warning "This will rollback the workspace to commit: $1" |
||||
read -p "Are you sure? (y/N): " confirm |
||||
|
||||
if [[ $confirm =~ ^[Yy]$ ]]; then |
||||
cd "$WORKSPACE" |
||||
backup |
||||
git reset --hard "$1" |
||||
log_success "Rolled back to commit: $1" |
||||
restart_services |
||||
else |
||||
log_info "Rollback cancelled." |
||||
fi |
||||
} |
||||
|
||||
backup() { |
||||
local backup_dir="/root/.openclaw/backups" |
||||
mkdir -p "$backup_dir" |
||||
|
||||
log_info "Creating backup..." |
||||
|
||||
# Backup workspace |
||||
tar -czf "$backup_dir/workspace-$TIMESTAMP.tar.gz" \ |
||||
--exclude='.git' \ |
||||
--exclude='logs' \ |
||||
-C /root/.openclaw workspace |
||||
|
||||
# Backup config |
||||
cp /root/.openclaw/openclaw.json "$backup_dir/openclaw-config-$TIMESTAMP.json" 2>/dev/null || true |
||||
|
||||
log_success "Backup created: $backup_dir/workspace-$TIMESTAMP.tar.gz" |
||||
} |
||||
|
||||
health_check() { |
||||
log_info "Running health check..." |
||||
|
||||
local issues=0 |
||||
|
||||
# Check gateway |
||||
if systemctl is-active --quiet openclaw-gateway; then |
||||
log_success "✓ Gateway is running" |
||||
else |
||||
log_error "✗ Gateway is not running" |
||||
((issues++)) |
||||
fi |
||||
|
||||
# Check monitor |
||||
if systemctl is-active --quiet openclaw-agent-monitor; then |
||||
log_success "✓ Agent Monitor is running" |
||||
else |
||||
log_error "✗ Agent Monitor is not running" |
||||
((issues++)) |
||||
fi |
||||
|
||||
# Check disk space |
||||
local disk_usage=$(df -h /root | tail -1 | awk '{print $5}' | sed 's/%//') |
||||
if [ "$disk_usage" -lt 80 ]; then |
||||
log_success "✓ Disk usage: ${disk_usage}%" |
||||
else |
||||
log_warning "⚠ Disk usage: ${disk_usage}%" |
||||
((issues++)) |
||||
fi |
||||
|
||||
# Check memory |
||||
local mem_usage=$(free | grep Mem | awk '{printf("%.0f", $3/$2 * 100.0)}') |
||||
if [ "$mem_usage" -lt 80 ]; then |
||||
log_success "✓ Memory usage: ${mem_usage}%" |
||||
else |
||||
log_warning "⚠ Memory usage: ${mem_usage}%" |
||||
((issues++)) |
||||
fi |
||||
|
||||
echo "" |
||||
if [ $issues -eq 0 ]; then |
||||
log_success "All health checks passed!" |
||||
return 0 |
||||
else |
||||
log_error "$issues health check(s) failed!" |
||||
return 1 |
||||
fi |
||||
} |
||||
|
||||
show_help() { |
||||
echo "OpenClaw System Management Script" |
||||
echo "" |
||||
echo "Usage: $0 <command>" |
||||
echo "" |
||||
echo "Commands:" |
||||
echo " install - Install and start all systemd services" |
||||
echo " start - Start all services" |
||||
echo " stop - Stop all services" |
||||
echo " restart - Restart all services" |
||||
echo " status - Show service status" |
||||
echo " logs - Show recent logs" |
||||
echo " health - Run health check" |
||||
echo " backup - Create backup of current state" |
||||
echo " rollback - Rollback to previous git commit" |
||||
echo " rollback-to <commit> - Rollback to specific commit" |
||||
echo " help - Show this help message" |
||||
echo "" |
||||
} |
||||
|
||||
# Main |
||||
case "${1:-help}" in |
||||
install) |
||||
install_services |
||||
;; |
||||
start) |
||||
start_services |
||||
;; |
||||
stop) |
||||
stop_services |
||||
;; |
||||
restart) |
||||
restart_services |
||||
;; |
||||
status) |
||||
show_status |
||||
;; |
||||
logs) |
||||
show_logs |
||||
;; |
||||
health) |
||||
health_check |
||||
;; |
||||
backup) |
||||
backup |
||||
;; |
||||
rollback) |
||||
rollback |
||||
;; |
||||
rollback-to) |
||||
rollback_to "$2" |
||||
;; |
||||
help|--help|-h) |
||||
show_help |
||||
;; |
||||
*) |
||||
log_error "Unknown command: $1" |
||||
show_help |
||||
exit 1 |
||||
;; |
||||
esac |
||||
@ -0,0 +1,4 @@ |
||||
[2026-02-20T14:25:25.027Z] [INFO] Agent Health Monitor initialized |
||||
[2026-02-20T14:25:25.035Z] [INFO] Agent Health Monitor starting... |
||||
[2026-02-20T14:25:25.036Z] [INFO] Starting OpenClaw Gateway monitoring... |
||||
[2026-02-20T14:25:25.038Z] [INFO] Monitor is now active. Press Ctrl+C to stop. |
||||
@ -0,0 +1,38 @@ |
||||
[Unit] |
||||
Description=OpenClaw Agent Health Monitor |
||||
Documentation=https://docs.openclaw.ai |
||||
After=network.target openclaw-gateway.service |
||||
Wants=network-online.target |
||||
|
||||
[Service] |
||||
Type=simple |
||||
User=root |
||||
WorkingDirectory=/root/.openclaw/workspace |
||||
Environment=NODE_ENV=production |
||||
|
||||
# Monitor process |
||||
ExecStart=/usr/bin/node /root/.openclaw/workspace/agent-monitor.js |
||||
|
||||
# Auto-healing configuration |
||||
Restart=always |
||||
RestartSec=5 |
||||
StartLimitInterval=300 |
||||
StartLimitBurst=10 |
||||
|
||||
# Resource limits |
||||
MemoryLimit=512M |
||||
CPUQuota=20% |
||||
|
||||
# Logging |
||||
StandardOutput=journal |
||||
StandardError=journal |
||||
SyslogIdentifier=openclaw-monitor |
||||
|
||||
# Security |
||||
NoNewPrivileges=true |
||||
ProtectSystem=strict |
||||
ProtectHome=read-only |
||||
ReadWritePaths=/root/.openclaw/workspace/logs |
||||
|
||||
[Install] |
||||
WantedBy=multi-user.target |
||||
@ -0,0 +1,42 @@ |
||||
[Unit] |
||||
Description=OpenClaw Gateway Service |
||||
Documentation=https://docs.openclaw.ai |
||||
After=network.target |
||||
Wants=network-online.target |
||||
|
||||
[Service] |
||||
Type=simple |
||||
User=root |
||||
WorkingDirectory=/root/.openclaw |
||||
Environment=NODE_ENV=production |
||||
|
||||
# Main gateway process |
||||
ExecStart=/usr/bin/node /www/server/nodejs/v24.13.1/bin/openclaw gateway start |
||||
ExecReload=/bin/kill -HUP $MAINPID |
||||
|
||||
# Auto-healing configuration |
||||
Restart=always |
||||
RestartSec=10 |
||||
StartLimitInterval=300 |
||||
StartLimitBurst=5 |
||||
|
||||
# Resource limits to prevent OOM |
||||
MemoryLimit=2G |
||||
CPUQuota=80% |
||||
|
||||
# Logging |
||||
StandardOutput=journal |
||||
StandardError=journal |
||||
SyslogIdentifier=openclaw-gateway |
||||
|
||||
# Security hardening |
||||
NoNewPrivileges=true |
||||
ProtectSystem=strict |
||||
ProtectHome=read-only |
||||
ReadWritePaths=/root/.openclaw |
||||
|
||||
# Watchdog for health monitoring |
||||
WatchdogSec=30 |
||||
|
||||
[Install] |
||||
WantedBy=multi-user.target |
||||
Loading…
Reference in new issue