// SPDX-License-Identifier: GPL-3.0-or-later package fleet import ( "context" "errors" "fmt" "io" "sort" "strings" ) const ( // rebootGraceSeconds: a reboot pending for longer than this flags the host (exit 1). rebootGraceSeconds = 2 * 24 * 3600 hostEventLimit = 15 hostEventFetch = 200 ) // hostMetrics are the series `hub host` draws, in order. var hostMetrics = []string{"score", "vuln_findings", "vuln_kev", "updates_apt_pending_count"} // changeKinds are the history event kinds that describe a state change (RS10-T). var changeKinds = map[string]bool{ "kernel_changed": true, "reboot_required_set": true, "reboot_required_cleared": true, "app_version_changed": true, "app_run_finished": true, } // failedRunStatuses are the last-run words that count as a failed run (the job's own "failed" and systemd results). var failedRunStatuses = map[string]bool{ "failed": true, "failure": true, "error": true, "exit-code": true, "timeout": true, "signal": true, "core-dump": true, "watchdog": true, "resources": true, } // degradable reports an error a secondary request may survive (the server answered badly or not at all, but the // credentials and the route are fine). func degradable(err error) bool { var te *TransportError return errors.As(err, &te) } func str(v any) string { s, _ := v.(string); return s } // strList returns the strings of a JSON array; anything else is nil. func strList(v any) []string { list, _ := v.([]any) var out []string for _, it := range list { if s, ok := it.(string); ok && s != "" { out = append(out, s) } } return out } // shortKernel drops a Debian "+debNN-arch" suffix for display: 7.3.4+deb14-amd64 -> 7.3.4. func shortKernel(release string) string { if i := strings.Index(release, "+"); i > 0 { return release[:i] } return release } // bootView renders the "boot" line from telemetry kernel_modules (kernel/module guard) and the reasons that make the host // need attention. An older collector sends no kernel_modules: no line, no attention. // // next boot 7.2.15 ok // next boot 7.3.4 MISSING zfs // next boot unknown, cannot confirm zfs (the next boot kernel could not be determined) // kernel held: zfs supports <=7.2, candidate 7.3 // dpkg interrupted: linux-image-amd64 // // Parts are joined with "; ". Missing modules or an interrupted dpkg run make `hub host` exit 1. func bootView(h map[string]any) (line string, attention []string) { km := obj(h["kernel_modules"]) if len(km) == 0 { return "", nil } next := str(km["next_boot_kernel"]) missing := strList(km["missing_modules"]) interrupted := strList(km["dpkg_interrupted"]) hold := obj(km["kernel_hold"]) var parts []string switch { case next != "" && len(missing) > 0: parts = append(parts, "next boot "+shortKernel(next)+" MISSING "+strings.Join(missing, ", ")) case next != "": parts = append(parts, "next boot "+shortKernel(next)+" ok") case len(missing) > 0: parts = append(parts, "next boot unknown, cannot confirm "+strings.Join(missing, ", ")) } if held, _ := hold["held"].(bool); held { reason := str(hold["reason"]) if reason == "" { reason = "held" } parts = append(parts, "kernel held: "+reason) } if len(interrupted) > 0 { parts = append(parts, "dpkg interrupted: "+strings.Join(interrupted, ", ")) } if len(missing) > 0 { attention = append(attention, "next boot kernel lacks modules: "+strings.Join(missing, ", ")) } if len(interrupted) > 0 { attention = append(attention, "dpkg interrupted: "+strings.Join(interrupted, ", ")) } return strings.Join(parts, "; "), attention } // outcomeText renders reboot_outcome as "deferred (load) at