hub v0.160.0: a live re-enroll mails the operator (R-1156); further refused backups counted, alarm once a day (R-1157); the reinstall's off-site re-issue says what it still does (R-1158)

This commit is contained in:
admin committed 2026-10-19 10:34:41 +02:00
1 parent d5e3c8b0d9
commit 503c93fada
10 files changed
+251 -28

No files matched your search

+62 -5
View File
@@ -4,8 +4,10 @@ import (
"encoding/json"
"log"
"sort"
"strconv"
"strings"
"sync"
"time"
"gitea.dooplex.hu/admin/felhom-hub/internal/store"
)
@@ -40,6 +42,12 @@ import (
// stands): each fires on its own edge, so one report flip is at most one event per state — never
// two events for one state, and never a "grants restored" line when only the refusal window ended.
// Pinned by host_capability_test.go TestR1150_*.
//
// R-1157 (hub v0.160.0) — ONE EPISODE, COUNTED. The agent holds the cap degraded for 24 h after its NEWEST refusal,
// so a guest refused every day reads as one long degraded state. The agent's reason names the newest refusal's
// time, so a CHANGED reason while degraded is a further refusal: it adds to the episode's count, and the alarm
// repeats (whole_guest_backup_refused again, the count and the first refusal in its text and details) at most
// once per refusalRepeatEvery. The cleared record carries the episode's total. Pinned by TestR1157_*.
type HostCapabilityChecker struct {
store *store.Store
logger *log.Logger
@@ -48,9 +56,21 @@ type HostCapabilityChecker struct {
mu sync.Mutex
states map[string]string // hostID → "ok" | "degraded" (the grants)
refusals map[string]string // hostID → "ok" | "degraded" (the backup cadence cap, R-1150)
episodes map[string]*refusalEpisode
customerOf map[string]string // hostID → customerID
now func() time.Time
}
// refusalEpisode is one degraded stretch of the cadence cap on a host (R-1157).
type refusalEpisode struct {
firstReason, lastReason string
count int
lastAlarm time.Time
}
// refusalRepeatEvery bounds the repeat of whole_guest_backup_refused inside one episode (R-1157).
const refusalRepeatEvery = 24 * time.Hour
// backupCadenceCapName is the agent's R-973 cadence-cap status name. It must equal felhom-agent's
// backupCapStatusName (cmd/felhom-agent/main.go) — the agent's status name stays (R-1150).
const backupCadenceCapName = "localapi:backup-cadence-cap"
@@ -72,7 +92,9 @@ func NewHostCapabilityChecker(s *store.Store, onEvent EventNotifyFunc, logger *l
onEvent: onEvent,
states: make(map[string]string),
refusals: make(map[string]string),
episodes: make(map[string]*refusalEpisode),
customerOf: make(map[string]string),
now: time.Now,
}
rows, err := s.GetHostCapabilities()
if err != nil {
@@ -123,6 +145,7 @@ func (cc *HostCapabilityChecker) Check() {
if cc.store.IsCustomerBlocked(row.CustomerID) {
delete(cc.states, row.HostID)
delete(cc.refusals, row.HostID)
delete(cc.episodes, row.HostID)
continue
}
cc.customerOf[row.HostID] = row.CustomerID
@@ -132,8 +155,26 @@ func (cc *HostCapabilityChecker) Check() {
cc.emitTransition(row.HostID, row.CustomerID, old, newState, names, features)
}
refState, reason := backupRefusalState(row.Capabilities)
if old, changed := step(cc.refusals, row.HostID, refState); changed {
cc.emitRefusal(row.HostID, row.CustomerID, old, refState, reason)
old, changed := step(cc.refusals, row.HostID, refState)
ep := cc.episodes[row.HostID]
switch {
case changed && refState == "degraded":
ep = &refusalEpisode{firstReason: reason, lastReason: reason, count: 1, lastAlarm: cc.now()}
cc.episodes[row.HostID] = ep
cc.emitRefusal(row.HostID, row.CustomerID, old, refState, reason, ep)
case changed:
cc.emitRefusal(row.HostID, row.CustomerID, old, refState, reason, ep)
delete(cc.episodes, row.HostID)
case refState == "degraded" && ep != nil && reason != ep.lastReason:
// A further refusal inside the episode (R-1157): counted always, alarmed at most once a day.
ep.count++
ep.lastReason = reason
if cc.now().Sub(ep.lastAlarm) >= refusalRepeatEvery {
ep.lastAlarm = cc.now()
cc.emitRefusal(row.HostID, row.CustomerID, refState, refState, reason, ep)
} else {
cc.logger.Printf("[INFO] Host capability: %s refused another whole-guest backup (%d in this episode); the alarm repeats at most once a day", row.HostID, ep.count)
}
}
}
@@ -144,6 +185,11 @@ func (cc *HostCapabilityChecker) Check() {
}
}
}
for id := range cc.episodes {
if !seen[id] {
delete(cc.episodes, id)
}
}
}
// step records newState for hostID in m and reports whether an event is due, with the state it left
@@ -251,9 +297,15 @@ func backupRefusalState(caps []store.CapabilityStatus) (state, reason string) {
// emitRefusal raises the operator's alarm for a refused whole-guest backup (R-1150) — the agent turned
// down a backup the guest asked for beyond its schedule — and records its end.
func (cc *HostCapabilityChecker) emitRefusal(hostID, customerID, oldState, newState, reason string) {
func (cc *HostCapabilityChecker) emitRefusal(hostID, customerID, oldState, newState, reason string, ep *refusalEpisode) {
var eventType, severity, message string
switch {
case newState == "degraded" && oldState == "degraded" && ep != nil:
eventType = EventWholeGuestBackupRefused
severity = "warning"
message = "Host " + hostID + ": whole-guest backups are still being refused — " + strconv.Itoa(ep.count) +
" refusals since the first one in this episode. The guest keeps asking for more whole-guest backups than " +
"its schedule allows. Its controller may be misbehaving or compromised: check the guest."
case newState == "degraded":
eventType = EventWholeGuestBackupRefused
severity = "warning"
@@ -266,11 +318,16 @@ func (cc *HostCapabilityChecker) emitRefusal(hostID, customerID, oldState, newSt
default:
return
}
details, _ := json.Marshal(map[string]any{
d := map[string]any{
"host_id": hostID,
"capability": backupCadenceCapName,
"agent_reason": reason,
})
}
if ep != nil {
d["refusals_in_episode"] = ep.count
d["first_refusal"] = ep.firstReason
}
details, _ := json.Marshal(d)
cc.logger.Printf("[INFO] Host capability: %s backup refusal %s → %s (%s)", hostID, oldState, newState, eventType)
if _, err := cc.store.SaveEvent(customerID, eventType, severity, message, string(details), "hub"); err != nil {
cc.logger.Printf("[WARN] Failed to save backup refusal event for %s: %v", hostID, err)