fix:fix agent support for alpine

This commit is contained in:
svnmoe
2025-09-29 22:10:15 +08:00
parent 39450ef39a
commit e9e935cb96

View File

@@ -389,8 +389,57 @@ log_success "Komari-agent installed to ${GREEN}$komari_agent_path${NC}"
# Detect init system and configure service
log_step "Configuring system service..."
# Check if running on NixOS
if [ -f /etc/NIXOS ]; then
# Function to detect actual init system
detect_init_system() {
# Check if running on NixOS (special case)
if [ -f /etc/NIXOS ]; then
echo "nixos"
return
fi
# Check for Alpine Linux or other OpenRC-based systems
if [ -f /etc/alpine-release ] || [ -f /sbin/openrc-run ]; then
if command -v rc-service >/dev/null 2>&1; then
echo "openrc"
return
fi
fi
# Check if systemd is actually PID 1 (not just installed)
if [ -d /run/systemd/system ] && command -v systemctl >/dev/null 2>&1; then
# Additional check: verify systemd is actually running
if systemctl --version >/dev/null 2>&1 && [ "$(ps -p 1 -o comm=)" = "systemd" ]; then
echo "systemd"
return
fi
fi
# Check for OpenRC (generic check)
if command -v rc-service >/dev/null 2>&1 && [ -d /etc/init.d ]; then
echo "openrc"
return
fi
# Check for OpenWrt's procd
if command -v uci >/dev/null 2>&1 && [ -f /etc/rc.common ]; then
echo "procd"
return
fi
# Check for macOS launchd
if [ "$os_name" = "darwin" ] && command -v launchctl >/dev/null 2>&1; then
echo "launchd"
return
fi
echo "unknown"
}
init_system=$(detect_init_system)
log_info "Detected init system: ${GREEN}$init_system${NC}"
# Handle each init system
if [ "$init_system" = "nixos" ]; then
log_warning "NixOS detected. System services must be configured declaratively."
log_info "Please add the following to your NixOS configuration:"
echo ""
@@ -409,32 +458,7 @@ if [ -f /etc/NIXOS ]; then
echo ""
log_info "Then run: sudo nixos-rebuild switch"
log_warning "Service not started automatically on NixOS. Please rebuild your configuration."
elif command -v systemctl >/dev/null 2>&1; then
# Systemd service configuration
log_info "Using systemd for service management"
service_file="/etc/systemd/system/${service_name}.service"
cat > "$service_file" << EOF
[Unit]
Description=Komari Agent Service
After=network.target
[Service]
Type=simple
ExecStart=${komari_agent_path} ${komari_args}
WorkingDirectory=${target_dir}
Restart=always
User=root
[Install]
WantedBy=multi-user.target
EOF
# Reload systemd and start service
systemctl daemon-reload
systemctl enable ${service_name}.service
systemctl start ${service_name}.service
log_success "Systemd service configured and started"
elif command -v rc-service >/dev/null 2>&1; then
elif [ "$init_system" = "openrc" ]; then
# OpenRC service configuration
log_info "Using OpenRC for service management"
service_file="/etc/init.d/${service_name}"
@@ -462,7 +486,32 @@ EOF
rc-update add ${service_name} default
rc-service ${service_name} start
log_success "OpenRC service configured and started"
elif command -v uci >/dev/null 2>&1; then
elif [ "$init_system" = "systemd" ]; then
# Systemd service configuration
log_info "Using systemd for service management"
service_file="/etc/systemd/system/${service_name}.service"
cat > "$service_file" << EOF
[Unit]
Description=Komari Agent Service
After=network.target
[Service]
Type=simple
ExecStart=${komari_agent_path} ${komari_args}
WorkingDirectory=${target_dir}
Restart=always
User=root
[Install]
WantedBy=multi-user.target
EOF
# Reload systemd and start service
systemctl daemon-reload
systemctl enable ${service_name}.service
systemctl start ${service_name}.service
log_success "Systemd service configured and started"
elif [ "$init_system" = "procd" ]; then
# procd service configuration (OpenWrt)
log_info "Using procd for service management"
service_file="/etc/init.d/${service_name}"
@@ -502,7 +551,7 @@ EOF
/etc/init.d/${service_name} enable
/etc/init.d/${service_name} start
log_success "procd service configured and started"
elif [ "$os_name" = "darwin" ] && command -v launchctl >/dev/null 2>&1; then
elif [ "$init_system" = "launchd" ]; then
# macOS launchd service configuration
log_info "Using launchd for service management"
@@ -581,7 +630,8 @@ EOF
fi
fi
else
log_error "Unsupported init system (systemd, openrc, procd, or launchd not found)"
log_error "Unsupported or unknown init system detected: $init_system"
log_error "Supported init systems: systemd, openrc, procd, launchd"
exit 1
fi