feat: Updates-Tab, Aufgaben-Tab ProxmoxPanel, security_only Tasks, 3x Netzwerk-Charts, Updates-Spalte Übersicht, dist-upgrade, DeleteAgent cascades metrics
This commit is contained in:
@@ -8,79 +8,111 @@ import (
|
||||
type UpdateInfo struct {
|
||||
UpdateAvailable bool `json:"update_available"`
|
||||
PendingCount int `json:"pending_count"`
|
||||
SecurityCount int `json:"security_count"`
|
||||
Updates []PendingUpdateInfo `json:"updates"`
|
||||
}
|
||||
|
||||
type PendingUpdateInfo struct {
|
||||
Package string `json:"package"`
|
||||
Arch string `json:"arch"`
|
||||
CurrentVer string `json:"current_version"`
|
||||
NewVer string `json:"new_version"`
|
||||
Repository string `json:"repository"`
|
||||
Security bool `json:"security"`
|
||||
}
|
||||
|
||||
func CollectUpdates() *UpdateInfo {
|
||||
updates := &UpdateInfo{
|
||||
UpdateAvailable: false,
|
||||
PendingCount: 0,
|
||||
SecurityCount: 0,
|
||||
Updates: []PendingUpdateInfo{},
|
||||
}
|
||||
|
||||
// apt list --upgradable 2>/dev/null
|
||||
|
||||
// apt list --upgradable, LC_ALL=C erzwingt englische Ausgabe
|
||||
cmd := exec.Command("/usr/bin/apt", "list", "--upgradable")
|
||||
cmd.Env = append(cmd.Env, "LC_ALL=C", "LANG=C", "PATH=/usr/bin:/bin")
|
||||
output, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
// Fallback: apt-check falls verfügbar
|
||||
return collectUpdatesAptCheck()
|
||||
}
|
||||
|
||||
|
||||
lines := strings.Split(string(output), "\n")
|
||||
for _, line := range lines {
|
||||
line = strings.TrimSpace(line)
|
||||
if line == "" || strings.HasPrefix(line, "WARNING:") || strings.HasPrefix(line, "Listing...") {
|
||||
continue
|
||||
}
|
||||
|
||||
// Format: package/suite version [upgradable from: old-version]
|
||||
// Beispiel: curl/focal-updates 7.68.0-1ubuntu2.7 amd64 [upgradable from: 7.68.0-1ubuntu2.6]
|
||||
|
||||
|
||||
// Format: package/suite new-version arch [upgradable from: old-version]
|
||||
// Beispiel: curl/focal-security 7.68.0-1ubuntu2.7 amd64 [upgradable from: 7.68.0-1ubuntu2.6]
|
||||
if !strings.Contains(line, "[upgradable from:") {
|
||||
continue
|
||||
}
|
||||
|
||||
// Package-Name extrahieren (bis zum ersten /)
|
||||
|
||||
// Package-Name und Suite trennen
|
||||
parts := strings.SplitN(line, "/", 2)
|
||||
if len(parts) < 2 {
|
||||
continue
|
||||
}
|
||||
packageName := parts[0]
|
||||
|
||||
// Neue Version extrahieren (zwischen / und [upgradable from:)
|
||||
|
||||
remaining := parts[1]
|
||||
beforeUpgradable := strings.Split(remaining, " [upgradable from:")
|
||||
// Suite ist das erste Wort vor dem Leerzeichen
|
||||
suiteParts := strings.SplitN(remaining, " ", 2)
|
||||
suite := suiteParts[0]
|
||||
// Repository: letzter Teil der Suite (z.B. "focal-security/main" → "main")
|
||||
// oder einfach die Suite selbst
|
||||
repository := suite
|
||||
if idx := strings.LastIndex(suite, "/"); idx >= 0 {
|
||||
repository = suite[idx+1:]
|
||||
}
|
||||
|
||||
// Security erkennen: Suite enthält "security"
|
||||
// Beispiel: bookworm-security, trixie-security, buster/updates
|
||||
suiteLower := strings.ToLower(suite)
|
||||
isSecurity := strings.Contains(suiteLower, "security") ||
|
||||
strings.HasSuffix(suiteLower, "/updates")
|
||||
|
||||
// Rest nach Suite parsen: "new-version arch [upgradable from: old-version]"
|
||||
afterSuite := ""
|
||||
if len(suiteParts) > 1 {
|
||||
afterSuite = suiteParts[1]
|
||||
}
|
||||
|
||||
beforeUpgradable := strings.SplitN(afterSuite, " [upgradable from:", 2)
|
||||
if len(beforeUpgradable) < 2 {
|
||||
continue
|
||||
}
|
||||
|
||||
// Neue Version ist im ersten Teil, aber wir müssen die Architektur entfernen
|
||||
versionParts := strings.Fields(beforeUpgradable[0])
|
||||
if len(versionParts) < 1 {
|
||||
continue
|
||||
|
||||
versionAndArch := strings.Fields(strings.TrimSpace(beforeUpgradable[0]))
|
||||
newVersion := ""
|
||||
arch := ""
|
||||
if len(versionAndArch) >= 1 {
|
||||
newVersion = versionAndArch[0]
|
||||
}
|
||||
newVersion := versionParts[0]
|
||||
|
||||
// Alte Version extrahieren (zwischen "from:" und "]")
|
||||
oldVersionPart := beforeUpgradable[1]
|
||||
oldVersion := strings.TrimSuffix(strings.TrimSpace(oldVersionPart), "]")
|
||||
|
||||
if len(versionAndArch) >= 2 {
|
||||
arch = versionAndArch[1]
|
||||
}
|
||||
|
||||
oldVersion := strings.TrimSuffix(strings.TrimSpace(beforeUpgradable[1]), "]")
|
||||
|
||||
updates.Updates = append(updates.Updates, PendingUpdateInfo{
|
||||
Package: packageName,
|
||||
Arch: arch,
|
||||
CurrentVer: oldVersion,
|
||||
NewVer: newVersion,
|
||||
Repository: repository,
|
||||
Security: isSecurity,
|
||||
})
|
||||
if isSecurity {
|
||||
updates.SecurityCount++
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
updates.PendingCount = len(updates.Updates)
|
||||
updates.UpdateAvailable = updates.PendingCount > 0
|
||||
|
||||
|
||||
return updates
|
||||
}
|
||||
|
||||
@@ -89,34 +121,34 @@ func collectUpdatesAptCheck() *UpdateInfo {
|
||||
updates := &UpdateInfo{
|
||||
UpdateAvailable: false,
|
||||
PendingCount: 0,
|
||||
SecurityCount: 0,
|
||||
Updates: []PendingUpdateInfo{},
|
||||
}
|
||||
|
||||
// /usr/lib/update-notifier/apt-check
|
||||
|
||||
cmd := exec.Command("/usr/lib/update-notifier/apt-check")
|
||||
output, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
// Noch ein Fallback: apt-get -s upgrade
|
||||
return collectUpdatesAptGetSimulate()
|
||||
}
|
||||
|
||||
// apt-check gibt "updates;security-updates" auf stderr aus
|
||||
|
||||
result := strings.TrimSpace(string(output))
|
||||
parts := strings.SplitN(result, ";", 2)
|
||||
|
||||
|
||||
if len(parts) >= 1 && parts[0] != "0" {
|
||||
// Packages verfügbar, aber keine Details ohne apt list
|
||||
updates.UpdateAvailable = true
|
||||
updates.PendingCount = 1 // Platzhalter
|
||||
updates.PendingCount = 1
|
||||
updates.Updates = []PendingUpdateInfo{
|
||||
{
|
||||
Package: "system-updates",
|
||||
Arch: "",
|
||||
CurrentVer: "various",
|
||||
NewVer: "available",
|
||||
Repository: "unknown",
|
||||
Security: false,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return updates
|
||||
}
|
||||
|
||||
@@ -125,38 +157,36 @@ func collectUpdatesAptGetSimulate() *UpdateInfo {
|
||||
updates := &UpdateInfo{
|
||||
UpdateAvailable: false,
|
||||
PendingCount: 0,
|
||||
SecurityCount: 0,
|
||||
Updates: []PendingUpdateInfo{},
|
||||
}
|
||||
|
||||
// apt-get -s upgrade (simulate)
|
||||
|
||||
cmd := exec.Command("/usr/bin/apt-get", "-s", "upgrade")
|
||||
output, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
return updates
|
||||
}
|
||||
|
||||
|
||||
lines := strings.Split(string(output), "\n")
|
||||
for _, line := range lines {
|
||||
line = strings.TrimSpace(line)
|
||||
|
||||
// Suche nach "Inst package-name" Zeilen
|
||||
if strings.HasPrefix(line, "Inst ") {
|
||||
fields := strings.Fields(line)
|
||||
if len(fields) >= 3 {
|
||||
packageName := fields[1]
|
||||
|
||||
// Sehr simple Erkennung, bessere Parsing schwierig ohne apt list
|
||||
updates.Updates = append(updates.Updates, PendingUpdateInfo{
|
||||
Package: packageName,
|
||||
Package: fields[1],
|
||||
Arch: "",
|
||||
CurrentVer: "unknown",
|
||||
NewVer: "available",
|
||||
Repository: "unknown",
|
||||
Security: false,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
updates.PendingCount = len(updates.Updates)
|
||||
updates.UpdateAvailable = updates.PendingCount > 0
|
||||
|
||||
|
||||
return updates
|
||||
}
|
||||
}
|
||||
|
||||
Binary file not shown.
+106
-1
@@ -10,6 +10,7 @@ import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -40,6 +41,10 @@ func (h *Handler) HandleCommand(msg Message) {
|
||||
return
|
||||
case "agent_update":
|
||||
response = h.handleAgentUpdate(msg)
|
||||
case "update_check":
|
||||
response = h.handleUpdateCheck(msg)
|
||||
case "update":
|
||||
response = h.handleUpdate(msg)
|
||||
case "pty_start":
|
||||
response = h.handlePTYStart(msg)
|
||||
case "pty_stop":
|
||||
@@ -320,4 +325,104 @@ func (h *Handler) runCommand(command string, timeoutSec int) (string, error) {
|
||||
cmd := exec.CommandContext(ctx, "/bin/sh", "-c", command)
|
||||
output, err := cmd.CombinedOutput()
|
||||
return string(output), err
|
||||
}
|
||||
}
|
||||
// handleUpdateCheck: apt update + Liste verfügbarer Pakete zurückgeben
|
||||
func (h *Handler) handleUpdateCheck(msg Message) Message {
|
||||
var response Message
|
||||
response.Type = "response"
|
||||
response.ID = msg.ID
|
||||
|
||||
log.Println("Update-Check gestartet (apt update)...")
|
||||
|
||||
// apt update (Paketlisten aktualisieren)
|
||||
_, err := h.runCommand("DEBIAN_FRONTEND=noninteractive apt-get update -q 2>&1", 120)
|
||||
if err != nil {
|
||||
log.Printf("apt update Fehler: %v", err)
|
||||
// Fehler ignorieren, apt update gibt oft non-zero bei Warnings zurück
|
||||
}
|
||||
|
||||
// Verfügbare Updates abfragen
|
||||
output, _ := h.runCommand("LC_ALL=C apt list --upgradable 2>/dev/null", 60)
|
||||
|
||||
// Security-Updates separat zählen
|
||||
secOutput, _ := h.runCommand("LC_ALL=C apt list --upgradable 2>/dev/null | grep -c security || true", 30)
|
||||
secCount := 0
|
||||
fmt.Sscanf(strings.TrimSpace(secOutput), "%d", &secCount)
|
||||
|
||||
// Pakete zählen (Zeilen ohne "Listing..." und Leerzeilen)
|
||||
lines := strings.Split(output, "\n")
|
||||
pkgCount := 0
|
||||
for _, l := range lines {
|
||||
l = strings.TrimSpace(l)
|
||||
if l != "" && !strings.HasPrefix(l, "Listing") && !strings.HasPrefix(l, "WARNING") {
|
||||
pkgCount++
|
||||
}
|
||||
}
|
||||
|
||||
response.Status = "ok"
|
||||
response.Data = map[string]interface{}{
|
||||
"update_available": pkgCount > 0,
|
||||
"pending_count": pkgCount,
|
||||
"security_count": secCount,
|
||||
"output": output,
|
||||
}
|
||||
return response
|
||||
}
|
||||
|
||||
// handleUpdate: apt-get upgrade ausführen
|
||||
func (h *Handler) handleUpdate(msg Message) Message {
|
||||
var response Message
|
||||
response.Type = "response"
|
||||
response.ID = msg.ID
|
||||
|
||||
reboot := false
|
||||
if r, ok := msg.Params["reboot"].(bool); ok {
|
||||
reboot = r
|
||||
}
|
||||
securityOnly := false
|
||||
if s, ok := msg.Params["security_only"].(bool); ok {
|
||||
securityOnly = s
|
||||
}
|
||||
|
||||
log.Printf("Update gestartet (reboot=%v, security_only=%v)", reboot, securityOnly)
|
||||
|
||||
// apt update
|
||||
h.runCommand("DEBIAN_FRONTEND=noninteractive apt-get update -q 2>&1", 120)
|
||||
|
||||
// apt-get upgrade oder nur Security-Updates
|
||||
var upgradeCmd string
|
||||
if securityOnly {
|
||||
upgradeCmd = `DEBIAN_FRONTEND=noninteractive apt-get install -y --only-upgrade \
|
||||
$(LC_ALL=C apt list --upgradable 2>/dev/null | grep security | cut -d/ -f1 | tr '\n' ' ') 2>&1`
|
||||
} else {
|
||||
// dist-upgrade statt upgrade: löst auch neue Abhängigkeiten auf (nötig für Proxmox/PVE-Pakete)
|
||||
upgradeCmd = "DEBIAN_FRONTEND=noninteractive apt-get dist-upgrade -y 2>&1"
|
||||
}
|
||||
|
||||
output, err := h.runCommand(upgradeCmd, 900)
|
||||
if err != nil {
|
||||
log.Printf("apt upgrade Fehler: %v", err)
|
||||
response.Status = "error"
|
||||
response.Error = fmt.Sprintf("Update fehlgeschlagen: %v", err)
|
||||
response.Data = map[string]interface{}{"output": output}
|
||||
return response
|
||||
}
|
||||
|
||||
log.Println("Update abgeschlossen")
|
||||
response.Status = "ok"
|
||||
response.Data = map[string]interface{}{
|
||||
"message": "Update erfolgreich",
|
||||
"output": output,
|
||||
"reboot": reboot,
|
||||
}
|
||||
|
||||
if reboot {
|
||||
log.Println("Reboot nach Update in 10 Sekunden...")
|
||||
go func() {
|
||||
time.Sleep(10 * time.Second)
|
||||
exec.Command("/sbin/shutdown", "-r", "now").Run()
|
||||
}()
|
||||
}
|
||||
|
||||
return response
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user