refactor(archied): move the update relay to the process that owns updates
The dashboard's Run read the watchdog's phase-2 report off local disk and put it on the activity stream. That is a host-local file read in the process that is moving off the host: the watchdog restarts archied and leaves its verdict beside archied, so archied is what can read it. The daemon now polls and relays it, publishing the same event through the same bus, so it persists with every other event and reaches whichever dashboard is watching. The event builder moves to releaseupdate, which owns Report and its verification; the dashboard keeps only what it needs to tell an installer where to write. The startup poll stays: the watchdog writes its result only once the process answers /healthz, so a single read at boot can always miss the report describing that very boot. persistAndBroadcastEvents takes the daemon context and detaches it for the insert, which is what it always did with context.Background() -- the last events of a run are the ones an operator reads afterwards.
This commit is contained in:
1 parent
54862f9fe6
commit
29b074ef77
9 files changed
+266
-197
No files matched your search
@@ -213,9 +213,8 @@ type Server struct {
|
||||
// When Cfg is present, Cfg.Get().Web.TrustForwardedHeaders is also checked.
|
||||
TrustForwardedHeaders bool
|
||||
|
||||
mu sync.Mutex
|
||||
conns map[chan events.Event]struct{}
|
||||
updateReportMu sync.Mutex
|
||||
mu sync.Mutex
|
||||
conns map[chan events.Event]struct{}
|
||||
}
|
||||
|
||||
func (s *Server) trustForwardedHeaders() bool {
|
||||
@@ -390,8 +389,6 @@ func trimLeadingSlash(p string) string {
|
||||
|
||||
// Run serves until ctx ends.
|
||||
func (s *Server) Run(ctx context.Context, listen string) error {
|
||||
s.reportPendingUpdate(ctx)
|
||||
go s.watchPendingUpdate(ctx)
|
||||
srv := &http.Server{Addr: listen, Handler: s.Handler(), ReadHeaderTimeout: 5 * time.Second}
|
||||
go func() {
|
||||
<-ctx.Done()
|
||||
@@ -406,22 +403,6 @@ func (s *Server) Run(ctx context.Context, listen string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// watchPendingUpdate closes the watchdog/startup race: the watchdog writes
|
||||
// the health result only after this process answers /healthz, so a startup
|
||||
// read alone can always miss a report for the very boot it describes.
|
||||
func (s *Server) watchPendingUpdate(ctx context.Context) {
|
||||
ticker := time.NewTicker(2 * time.Second)
|
||||
defer ticker.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-ticker.C:
|
||||
s.reportPendingUpdate(ctx)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func writeJSON(w http.ResponseWriter, v any) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_ = json.NewEncoder(w).Encode(v)
|
||||
|
||||
@@ -1,119 +0,0 @@
|
||||
package webui
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/events"
|
||||
"github.com/samcharles93/archie-core/internal/releaseupdate"
|
||||
)
|
||||
|
||||
// reportPendingUpdate checks for a phase-2 outcome left by the update
|
||||
// watchdog for a dashboard-initiated install and relays it onto the
|
||||
// activity stream, mirroring internal/channels/telegram/update.go's
|
||||
// reportPendingUpdate for the Telegram channel. Called once on every daemon
|
||||
// boot (Server.Run), so it fires whether this is the restart the update
|
||||
// itself triggered or a later, unrelated restart.
|
||||
//
|
||||
// A report is cleared as soon as it's handled -- relayed, or found
|
||||
// unreadable -- so a later boot never retries it and two boots never race
|
||||
// to relay the same outcome.
|
||||
func (s *Server) reportPendingUpdate(ctx context.Context) {
|
||||
s.updateReportMu.Lock()
|
||||
defer s.updateReportMu.Unlock()
|
||||
if s.UpdateReportPath == "" {
|
||||
return
|
||||
}
|
||||
report, found, err := releaseupdate.ReadPendingReport(s.UpdateReportPath)
|
||||
if err != nil {
|
||||
s.logf("read pending update report failed; discarding it", "err", err)
|
||||
if clearErr := releaseupdate.ClearPendingReport(s.UpdateReportPath); clearErr != nil {
|
||||
s.logf("clear unreadable update report failed", "err", clearErr)
|
||||
}
|
||||
return
|
||||
}
|
||||
if !found {
|
||||
return
|
||||
}
|
||||
s.emit(ctx, updateReportEvent(report, s.runningVersions()))
|
||||
if err := releaseupdate.ClearPendingReport(s.UpdateReportPath); err != nil {
|
||||
s.logf("clear pending update report failed", "err", err)
|
||||
}
|
||||
}
|
||||
|
||||
// runningVersions returns the versions components report for themselves, or
|
||||
// nil when the composition root wired none -- see the identical seam on
|
||||
// internal/channels/telegram.Gateway. Nil is not a no-op: with nothing to
|
||||
// check against, every claim comes back Unverified rather than Confirmed.
|
||||
func (s *Server) runningVersions() map[string]string {
|
||||
if s.RunningVersions == nil {
|
||||
return nil
|
||||
}
|
||||
return s.RunningVersions()
|
||||
}
|
||||
|
||||
// updateReportEvent turns a phase-2 Report, plus its verification against
|
||||
// the versions actually running, into the activity-stream event the
|
||||
// dashboard renders. Unlike Telegram's formatPendingReport, this ships the
|
||||
// structured Verification rather than pre-rendered prose, so the frontend
|
||||
// controls presentation -- the two channels' operators read the same facts
|
||||
// through different UIs, not necessarily the same sentence.
|
||||
func updateReportEvent(report releaseupdate.Report, running map[string]string) events.Event {
|
||||
verification := report.Verify(running)
|
||||
drift := make([]map[string]any, 0, len(verification.Drift))
|
||||
for _, d := range verification.Drift {
|
||||
drift = append(drift, map[string]any{
|
||||
"id": d.ID,
|
||||
"claimed": d.Claimed,
|
||||
"running": d.Running,
|
||||
"running_is_previous": d.RunningIsPrevious,
|
||||
})
|
||||
}
|
||||
return events.Event{
|
||||
Kind: events.KindUpdateReport,
|
||||
Detail: summarizeUpdateReport(report, verification),
|
||||
Data: map[string]any{
|
||||
"health_check": report.HealthCheck,
|
||||
"rolled_back": report.RolledBack,
|
||||
"previous": report.Previous,
|
||||
"installed": report.Installed,
|
||||
"confirmed": verification.Confirmed,
|
||||
"drift": drift,
|
||||
"unverified": verification.Unverified,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// summarizeUpdateReport is the one-line summary shown in the dashboard's
|
||||
// activity feed, which renders event.Detail generically for every event
|
||||
// kind (see ui/src/dashboard/dashboard.js activityRow). It condenses the
|
||||
// same facts internal/channels/telegram/update.go's formatPendingReport
|
||||
// spells out at length -- not a byte-identical wording, since the two
|
||||
// surfaces render differently, but the same four outcomes: confirmed,
|
||||
// drifted, unverified, or rolled back.
|
||||
func summarizeUpdateReport(report releaseupdate.Report, verification releaseupdate.Verification) string {
|
||||
if report.RolledBack {
|
||||
return "Update failed its health check and was rolled back to the previous version."
|
||||
}
|
||||
if report.HealthCheck != "passed" {
|
||||
return "Update failed its health check and could NOT be rolled back automatically -- manual intervention required."
|
||||
}
|
||||
if len(verification.Drift) > 0 {
|
||||
ids := make([]string, 0, len(verification.Drift))
|
||||
for _, d := range verification.Drift {
|
||||
ids = append(ids, fmt.Sprintf("%s reports %s, not the installed %s", d.ID, d.Running, d.Claimed))
|
||||
}
|
||||
sort.Strings(ids)
|
||||
return "Update did NOT take effect: " + strings.Join(ids, "; ") + "."
|
||||
}
|
||||
if len(verification.Confirmed) == 0 {
|
||||
return "Update installed and health check passed, but the running version could not be verified -- please check manually before assuming the update worked."
|
||||
}
|
||||
text := "Update complete. Health check passed -- confirmed running on the new version (" + strings.Join(verification.Confirmed, ", ") + ")."
|
||||
if len(verification.Unverified) > 0 {
|
||||
text += " Not independently checked: " + strings.Join(verification.Unverified, ", ") + "."
|
||||
}
|
||||
return text
|
||||
}
|
||||
@@ -6,131 +6,13 @@ import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/events"
|
||||
"github.com/samcharles93/archie-core/internal/gateway"
|
||||
"github.com/samcharles93/archie-core/internal/releaseupdate"
|
||||
)
|
||||
|
||||
// TestReportPendingUpdateEmitsAndClearsReport is the webui counterpart of
|
||||
// telegram's TestReportPendingUpdateSendsAndClearsReport: a dashboard-
|
||||
// initiated install must produce a phase-2 report that reaches the
|
||||
// dashboard, and must never be relayed twice (archie-core-nln7).
|
||||
func TestReportPendingUpdateEmitsAndClearsReport(t *testing.T) {
|
||||
s := newTestServer(t)
|
||||
path := filepath.Join(t.TempDir(), "update-report.json")
|
||||
report := releaseupdate.Report{
|
||||
Channel: "webui",
|
||||
Previous: map[string]string{releaseupdate.ComponentDaemon: "1.0.0"},
|
||||
Installed: map[string]string{releaseupdate.ComponentDaemon: "1.1.0"},
|
||||
HealthCheck: "passed",
|
||||
}
|
||||
if err := releaseupdate.WritePendingReport(path, report); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
s.UpdateReportPath = path
|
||||
s.RunningVersions = func() map[string]string {
|
||||
return map[string]string{releaseupdate.ComponentDaemon: "1.1.0"}
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
s.reportPendingUpdate(ctx)
|
||||
|
||||
evs, err := s.Store.EventsSince(ctx, 0, 10)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(evs) != 1 {
|
||||
t.Fatalf("events = %#v, want exactly one", evs)
|
||||
}
|
||||
if evs[0].Kind != events.KindUpdateReport {
|
||||
t.Errorf("kind = %q, want %q", evs[0].Kind, events.KindUpdateReport)
|
||||
}
|
||||
confirmed, _ := evs[0].Data["confirmed"].([]any)
|
||||
if len(confirmed) != 1 || confirmed[0] != releaseupdate.ComponentDaemon {
|
||||
t.Errorf("confirmed = %#v, want [%q]", evs[0].Data["confirmed"], releaseupdate.ComponentDaemon)
|
||||
}
|
||||
if !strings.Contains(evs[0].Detail, "confirmed running") {
|
||||
t.Errorf("Detail = %q, want it to say confirmed running", evs[0].Detail)
|
||||
}
|
||||
|
||||
if _, found, err := releaseupdate.ReadPendingReport(path); err != nil || found {
|
||||
t.Errorf("report still present after reportPendingUpdate: found=%v err=%v", found, err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestReportPendingUpdateClearsUnreadableReport mirrors the telegram
|
||||
// behaviour: a corrupt report must not jam every future boot.
|
||||
func TestReportPendingUpdateClearsUnreadableReport(t *testing.T) {
|
||||
s := newTestServer(t)
|
||||
path := filepath.Join(t.TempDir(), "update-report.json")
|
||||
if err := os.WriteFile(path, []byte("{not valid json"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
s.UpdateReportPath = path
|
||||
|
||||
s.reportPendingUpdate(context.Background())
|
||||
|
||||
if _, err := os.Stat(path); !os.IsNotExist(err) {
|
||||
t.Fatalf("corrupt report file still exists after reportPendingUpdate: err = %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReportPendingUpdateNoFileIsNoop(t *testing.T) {
|
||||
s := newTestServer(t)
|
||||
s.UpdateReportPath = filepath.Join(t.TempDir(), "does-not-exist.json")
|
||||
|
||||
s.reportPendingUpdate(context.Background())
|
||||
|
||||
evs, err := s.Store.EventsSince(context.Background(), 0, 10)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(evs) != 0 {
|
||||
t.Fatalf("events = %#v, want none", evs)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReportPendingUpdateDisabledWhenPathEmpty(t *testing.T) {
|
||||
s := newTestServer(t)
|
||||
|
||||
s.reportPendingUpdate(context.Background())
|
||||
|
||||
evs, err := s.Store.EventsSince(context.Background(), 0, 10)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(evs) != 0 {
|
||||
t.Fatalf("events = %#v, want none", evs)
|
||||
}
|
||||
}
|
||||
|
||||
// TestSummarizeUpdateReportDistinguishesDriftFromConfirmed is the false-
|
||||
// success regression the whole verification mechanism (c25799e,
|
||||
// archie-core-3jag's sibling) exists to catch: a passed health probe alone
|
||||
// must never be worded as a confirmed version.
|
||||
func TestSummarizeUpdateReportDistinguishesDriftFromConfirmed(t *testing.T) {
|
||||
report := releaseupdate.Report{
|
||||
HealthCheck: "passed",
|
||||
Previous: map[string]string{releaseupdate.ComponentDaemon: "1.0.0"},
|
||||
Installed: map[string]string{releaseupdate.ComponentDaemon: "1.1.0"},
|
||||
}
|
||||
running := map[string]string{releaseupdate.ComponentDaemon: "1.0.0"}
|
||||
verification := report.Verify(running)
|
||||
|
||||
got := summarizeUpdateReport(report, verification)
|
||||
if !strings.Contains(got, "did NOT take effect") {
|
||||
t.Errorf("summary = %q, want it to flag drift rather than claim success", got)
|
||||
}
|
||||
if strings.Contains(got, "confirmed running") {
|
||||
t.Errorf("summary = %q, must not claim confirmed running when drift was detected", got)
|
||||
}
|
||||
}
|
||||
|
||||
// TestHandleChatUpdateInstallSetsReportPath is the regression proof: before
|
||||
// this fix, the webui install handler passed
|
||||
// releaseupdate.InstallMeta{Channel: "webui"} with no ReportPath, so the
|
||||
|
||||
Reference in new issue
Block a user