Hub v0.149.0: a box the household switched off is not an outage for 14 days (R-998)
gates / gates (push) Successful in 6m40s

hosts.deliberate_off_at follows the agent's deliberate_off field (set, then cleared by the first
report of a new boot; one info event each way). While held, every silence alarm is held: host and
node staleness, the 05:00 backup deadlines, off-site and restore-test freshness, the OS alarms and
the kernel notice. After 14 days the usual alarms fire once, naming the power-off. Pages show
KIKAPCSOLVA and the time. Consequence tests with controls; each guard red-proofed.
This commit is contained in:
admin committed 2026-10-13 19:47:02 +02:00
1 parent ed59bf3cbd
commit 50e47e29af
24 files changed
+638 -17

No files matched your search

+87
View File
@@ -0,0 +1,87 @@
package store
import (
"database/sql"
"time"
)
// ── The household's deliberate power-off (R-998, documentation/architecture/14-host-power-off.md) ──────
//
// The agent reports `deliberate_off.requested_at` from the moment the household pressed „A gép kikapcsolása"
// until the box goes down; the next boot's reports do not carry it. The hub keeps the time on the host row.
// While it is set and younger than DeliberateOffHold, every silence alarm about that box is held: host and
// controller staleness, the backup deadlines, off-site and restore-test freshness, the OS alarms and the
// kernel notice. After the hold, the alarms fire as usual — a box nobody switched back on in two weeks is
// worth a look. Pinned by monitor/deliberate_off_test.go, api/deliberate_off_test.go and web/deliberate_off_view_test.go.
// DeliberateOffHold is how long a deliberate power-off silences the box's alarms (operator default, 14 days).
const DeliberateOffHold = 14 * 24 * time.Hour
// DeliberateOffHeld reports whether a deliberate-off time still holds the alarms at now.
func DeliberateOffHeld(at *time.Time, now time.Time) bool {
return at != nil && !at.IsZero() && now.Sub(*at) < DeliberateOffHold
}
func parseDeliberateOff(v string) *time.Time {
if v == "" {
return nil
}
t, err := time.Parse(time.RFC3339, v)
if err != nil {
return nil
}
return &t
}
// SetHostDeliberateOff records the report's deliberate-off field. at == nil clears it (a report without the
// field: the box is in a new boot). It returns the previous value, so the caller can emit an event on a change.
func (s *Store) SetHostDeliberateOff(hostID string, at *time.Time) (prev *time.Time, err error) {
var old sql.NullString
if err := s.db.QueryRow(`SELECT deliberate_off_at FROM hosts WHERE host_id = ?`, hostID).Scan(&old); err != nil {
return nil, err
}
prev = parseDeliberateOff(old.String)
var val any
if at != nil {
val = at.UTC().Format(time.RFC3339)
}
if (prev == nil && at == nil) || (prev != nil && at != nil && prev.Equal(*at)) {
return prev, nil
}
_, err = s.db.Exec(`UPDATE hosts SET deliberate_off_at = ?, updated_at = datetime('now') WHERE host_id = ?`, val, hostID)
return prev, err
}
// HostDeliberateOffAt returns the host's deliberate-off time (nil = none).
func (s *Store) HostDeliberateOffAt(hostID string) *time.Time {
var v sql.NullString
if err := s.db.QueryRow(`SELECT deliberate_off_at FROM hosts WHERE host_id = ?`, hostID).Scan(&v); err != nil {
return nil
}
return parseDeliberateOff(v.String)
}
// CustomerDeliberatelyOff reports whether any of the customer's hosts is deliberately off and still within the
// hold at now — the checkers keyed by customer use it. The time is the newest such request.
func (s *Store) CustomerDeliberatelyOff(customerID string, now time.Time) (time.Time, bool) {
rows, err := s.db.Query(`SELECT deliberate_off_at FROM hosts WHERE customer_id = ? AND deliberate_off_at IS NOT NULL`, customerID)
if err != nil {
return time.Time{}, false
}
defer rows.Close()
var newest time.Time
held := false
for rows.Next() {
var v string
if rows.Scan(&v) != nil {
continue
}
if t := parseDeliberateOff(v); DeliberateOffHeld(t, now) {
held = true
if t.After(newest) {
newest = *t
}
}
}
return newest, held
}
+14 -6
View File
@@ -471,6 +471,9 @@ func (s *Store) migrate() error {
// expected key fingerprint, tunnel id) — the hub serves these only in recovery mode; no usable
// secret is hub-held (the blobs need R, which the hub never has).
s.db.Exec(`ALTER TABLE hosts ADD COLUMN recovery_mode_until DATETIME`)
// R-998 (doc 14): the household switched the box off on purpose at this time (RFC3339 UTC, from the agent's
// deliberate_off report field). NULL = not deliberately off. deliberateoff.go reads and writes it.
s.db.Exec(`ALTER TABLE hosts ADD COLUMN deliberate_off_at TEXT`)
s.db.Exec(`ALTER TABLE host_escrow ADD COLUMN identity_blob BLOB`)
s.db.Exec(`ALTER TABLE host_escrow ADD COLUMN directive_json TEXT NOT NULL DEFAULT '{}'`)
@@ -2823,6 +2826,7 @@ type Host struct {
DesiredJSON string
DesiredGeneration int64
RecoveryModeUntil *time.Time // slice 10D: recovery mode active until this time (nil/past = off)
DeliberateOffAt *time.Time // R-998: switched off by the household at this time (nil = not); deliberateoff.go
CreatedAt time.Time
UpdatedAt time.Time
}
@@ -2866,6 +2870,8 @@ type HostStaleRow struct {
HostID string
CustomerID string
LastReportAt time.Time
// DeliberateOffAt (R-998): when the household switched the box off on purpose; nil = not.
DeliberateOffAt *time.Time
}
// GuestID derives the interim guest primary key from host + vmid. The hub owns the
@@ -2890,10 +2896,10 @@ func (s *Store) scanHost(scan func(dest ...any) error) (*Host, error) {
func scanHostRaw(scan func(dest ...any) error) (*Host, error) {
var h Host
var lastReport, recoveryUntil sql.NullString
var lastReport, recoveryUntil, deliberateOff sql.NullString
var createdAt, updatedAt string
err := scan(&h.HostID, &h.CustomerID, &h.APIKey, &h.AgentVersion, &lastReport,
&h.DesiredJSON, &h.DesiredGeneration, &recoveryUntil, &createdAt, &updatedAt)
&h.DesiredJSON, &h.DesiredGeneration, &recoveryUntil, &createdAt, &updatedAt, &deliberateOff)
if err != nil {
return nil, err
}
@@ -2905,13 +2911,14 @@ func scanHostRaw(scan func(dest ...any) error) (*Host, error) {
t := parseSQLiteTime(recoveryUntil.String)
h.RecoveryModeUntil = &t
}
h.DeliberateOffAt = parseDeliberateOff(deliberateOff.String)
h.CreatedAt = parseSQLiteTime(createdAt)
h.UpdatedAt = parseSQLiteTime(updatedAt)
return &h, nil
}
const hostSelectCols = `host_id, customer_id, api_key, agent_version, last_report_at,
desired_json, desired_generation, recovery_mode_until, created_at, updated_at`
desired_json, desired_generation, recovery_mode_until, created_at, updated_at, deliberate_off_at`
// GetHostByAPIKey looks up a host by its per-host hub key. Returns nil (no error)
// if no match — parallels GetCustomerConfigByAPIKey.
@@ -3847,7 +3854,7 @@ func (s *Store) ListGuestsForHost(hostID string) ([]Guest, error) {
// have never reported (NULL last_report_at) are skipped — a freshly-minted host is
// not "down" until it has checked in at least once.
func (s *Store) GetHostStaleness() ([]HostStaleRow, error) {
rows, err := s.db.Query(`SELECT host_id, customer_id, last_report_at FROM hosts WHERE last_report_at IS NOT NULL`)
rows, err := s.db.Query(`SELECT host_id, customer_id, last_report_at, COALESCE(deliberate_off_at, '') FROM hosts WHERE last_report_at IS NOT NULL`)
if err != nil {
return nil, err
}
@@ -3855,11 +3862,12 @@ func (s *Store) GetHostStaleness() ([]HostStaleRow, error) {
var out []HostStaleRow
for rows.Next() {
var r HostStaleRow
var last string
if err := rows.Scan(&r.HostID, &r.CustomerID, &last); err != nil {
var last, off string
if err := rows.Scan(&r.HostID, &r.CustomerID, &last, &off); err != nil {
return nil, err
}
r.LastReportAt = parseSQLiteTime(last)
r.DeliberateOffAt = parseDeliberateOff(off)
out = append(out, r)
}
return out, rows.Err()