- winsvc package mit Build-Tag-basierter plattformspezifischer Implementierung: - service_windows.go: svc.Handler + Install/Uninstall/Start/Stop via windows/svc/mgr - service_stub.go: Stub-Implementierungen für nicht-Windows Builds - main.go refaktoriert: - os.Executable() für absoluten Config-Pfad (Service-Modus: CWD = C:\Windows\System32) - Kommandozeilen-Args: install / uninstall / start / stop - winsvc.IsWindowsService() Erkennung → Service-Modus oder Konsolen-Modus - Agent-Loop als makeRunFn() extrahiert (wiederverwendbar für beide Modi) - install-service.ps1: Convenience-Skript zum Bauen, Installieren und Starten Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
128 lines
4.7 KiB
PowerShell
128 lines
4.7 KiB
PowerShell
#Requires -RunAsAdministrator
|
|
<#
|
|
.SYNOPSIS
|
|
Installiert den NexusRMM Agent als Windows Service
|
|
.DESCRIPTION
|
|
Baut den Agent, kopiert ihn ins Zielverzeichnis und registriert ihn als Windows Service.
|
|
Muss als Administrator ausgefuehrt werden.
|
|
.PARAMETER ServerAddress
|
|
gRPC-Adresse des NexusRMM Servers (Standard: localhost:5001)
|
|
.PARAMETER InstallDir
|
|
Installationsverzeichnis (Standard: C:\Program Files\NexusRMM)
|
|
#>
|
|
param(
|
|
[string]$ServerAddress = "localhost:5001",
|
|
[string]$InstallDir = "C:\Program Files\NexusRMM"
|
|
)
|
|
|
|
$ErrorActionPreference = "Continue"
|
|
$Root = $PSScriptRoot
|
|
|
|
function Write-Step { param($msg) Write-Host "" ; Write-Host "==> $msg" -ForegroundColor Cyan }
|
|
function Write-OK { param($msg) Write-Host " [OK] $msg" -ForegroundColor Green }
|
|
function Write-Fail { param($msg) Write-Host " [FEHLER] $msg" -ForegroundColor Red }
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# 1. Agent bauen
|
|
# ---------------------------------------------------------------------------
|
|
Write-Step "Baue NexusRMM Agent..."
|
|
|
|
Set-Location "$Root\Agent"
|
|
$env:GOOS = "windows"
|
|
$env:GOARCH = "amd64"
|
|
go build -ldflags "-s -w -X main.version=1.0.0" -o nexus-agent.exe ./cmd/agent
|
|
if ($LASTEXITCODE -ne 0) {
|
|
Write-Fail "go build fehlgeschlagen."
|
|
exit 1
|
|
}
|
|
Write-OK "nexus-agent.exe erstellt."
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# 2. Installationsverzeichnis anlegen
|
|
# ---------------------------------------------------------------------------
|
|
Write-Step "Lege Installationsverzeichnis an: $InstallDir"
|
|
|
|
if (-not (Test-Path $InstallDir)) {
|
|
New-Item -ItemType Directory -Path $InstallDir -Force | Out-Null
|
|
}
|
|
Write-OK "Verzeichnis bereit."
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# 3. Dateien kopieren
|
|
# ---------------------------------------------------------------------------
|
|
Write-Step "Kopiere Dateien..."
|
|
|
|
Copy-Item "$Root\Agent\nexus-agent.exe" "$InstallDir\nexus-agent.exe" -Force
|
|
|
|
# Konfigurationsdatei anlegen falls nicht vorhanden
|
|
$configPath = "$InstallDir\nexus-agent.yaml"
|
|
if (-not (Test-Path $configPath)) {
|
|
@"
|
|
serverAddress: $ServerAddress
|
|
agentId: ""
|
|
heartbeatInterval: 30
|
|
meshEnabled: false
|
|
meshCentralUrl: ""
|
|
"@ | Out-File -FilePath $configPath -Encoding utf8
|
|
Write-OK "nexus-agent.yaml erstellt (serverAddress: $ServerAddress)."
|
|
} else {
|
|
Write-OK "nexus-agent.yaml bereits vorhanden, wird nicht ueberschrieben."
|
|
}
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# 4. Bestehenden Service stoppen/deinstallieren falls vorhanden
|
|
# ---------------------------------------------------------------------------
|
|
$exePath = "$InstallDir\nexus-agent.exe"
|
|
|
|
$svcCheck = Get-Service -Name "NexusRMMAgent" -ErrorAction SilentlyContinue
|
|
if ($svcCheck) {
|
|
Write-Step "Entferne vorhandenen Service..."
|
|
& $exePath stop 2>$null
|
|
Start-Sleep -Seconds 2
|
|
& $exePath uninstall
|
|
if ($LASTEXITCODE -ne 0) {
|
|
Write-Fail "Service-Deinstallation fehlgeschlagen."
|
|
exit 1
|
|
}
|
|
Write-OK "Alter Service entfernt."
|
|
}
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# 5. Service installieren und starten
|
|
# ---------------------------------------------------------------------------
|
|
Write-Step "Installiere Service..."
|
|
|
|
& $exePath install
|
|
if ($LASTEXITCODE -ne 0) {
|
|
Write-Fail "Service-Installation fehlgeschlagen."
|
|
exit 1
|
|
}
|
|
Write-OK "Service installiert."
|
|
|
|
Write-Step "Starte Service..."
|
|
& $exePath start
|
|
if ($LASTEXITCODE -ne 0) {
|
|
Write-Fail "Service konnte nicht gestartet werden."
|
|
exit 1
|
|
}
|
|
Write-OK "Service gestartet."
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Zusammenfassung
|
|
# ---------------------------------------------------------------------------
|
|
Write-Host ""
|
|
Write-Host "+--------------------------------------------------+" -ForegroundColor Green
|
|
Write-Host "| NexusRMM Agent als Service installiert! |" -ForegroundColor Green
|
|
Write-Host "+--------------------------------------------------+" -ForegroundColor Green
|
|
Write-Host "| Installiert in: $InstallDir" -ForegroundColor Green
|
|
Write-Host "| Server: $ServerAddress" -ForegroundColor Green
|
|
Write-Host "| Service-Name: NexusRMMAgent" -ForegroundColor Green
|
|
Write-Host "+--------------------------------------------------+" -ForegroundColor Green
|
|
Write-Host ""
|
|
Write-Host "Verwalten:" -ForegroundColor Yellow
|
|
Write-Host " Start: & '$exePath' start" -ForegroundColor Yellow
|
|
Write-Host " Stop: & '$exePath' stop" -ForegroundColor Yellow
|
|
Write-Host " Entfernen: & '$exePath' uninstall" -ForegroundColor Yellow
|
|
Write-Host " Oder via: services.msc -> NexusRMMAgent" -ForegroundColor Yellow
|
|
Write-Host ""
|