appconfig: SyncUnits reports each declared unit's outcome
UnitOps gains an Outcome hook, called once per declared host per pass after application, with the final state: delivered, refused (with the text the pass printed for the host) or no-route. States are recorded at the existing print sites and emitted at the end; no printed text changes. The tripwire and an error return refuse every routed host not already refused; unreadable declarations and a held lock emit nothing. runAdoptUnderRootRelease runs config sync through adoptConfigSync, which appends each outcome to the named-units record and writes it a second time after SyncUnits; without a usable secrets key there is no second write.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
7 files changed,
+548
−65
internal/appconfig/sync.go+47 −31internal/appconfig/unitsync.go+150 −24internal/appconfig/unitsync_test.go+217 −0internal/host/agent.go+6 −8internal/host/namedunitsfetch.go+29 −0internal/host/namedunitsfetch_test.go+96 −0internal/status/snapshot.go+3 −2
diff --git a/internal/appconfig/sync.go b/internal/appconfig/sync.go| index 95fe2e997..5f6822ca1 100644 |
| --- a/internal/appconfig/sync.go |
| +++ b/internal/appconfig/sync.go |
| @@ -229,7 +229,8 @@ func SyncWithCallbacks(ctx context.Context, reg *registry.Registry, s release.St |
| // (unsupported) abandons the pass. |
| if ops != nil && plan.decls != nil { |
| if err := plan.decls.tripwire(ops); err != nil { |
| - fmt.Printf(" config sync: %v\n", err) |
| + plan.outcomes.abandon(say("config sync", "%v", err)) |
| + plan.outcomes.emit() |
| return nil |
| } |
| } |
| @@ -247,13 +248,13 @@ func SyncWithCallbacks(ctx context.Context, reg *registry.Registry, s release.St |
| // two routes can have two separately corrupt overlay objects, each |
| // wrong for its own reason — so that case is never folded in here; see |
| // the errors.As branch below. |
| - said := map[appKey]bool{} |
| + said := map[appKey]string{} // the line printed, the reason each route's outcome carries |
| for _, rt := range routes { |
| if !(syncable(rt) || Stamped(rt)) { |
| continue |
| } |
| if reason, refused := declaredRefusal[rt.Host]; refused { |
| - fmt.Printf(" %s: delivery blocked: %s\n", rt.Host, reason) |
| + plan.outcomes.refuse(rt.Host, say(rt.Host, "delivery blocked: %s", reason)) |
| continue |
| } |
| if declarationsUnknown && !stampedForDelivery(rt) { |
| @@ -269,7 +270,7 @@ func SyncWithCallbacks(ctx context.Context, reg *registry.Registry, s release.St |
| if env == "" { |
| env = "default" |
| } |
| - fmt.Printf(" %s: config unresolved (environment %q), env file left as-is: %v\n", rt.Host, env, r.err) |
| + plan.outcomes.refuse(rt.Host, say(rt.Host, "config unresolved (environment %q), env file left as-is: %v", env, r.err)) |
| continue |
| } |
| // The instance overlay wins per key over the group's merge, and is |
| @@ -310,15 +311,15 @@ func SyncWithCallbacks(ctx context.Context, reg *registry.Registry, s release.St |
| // the same reason; a line each would bury the cause under |
| // its own repetitions on a box with many routes. |
| ak := appKey{k.acct, k.app} |
| - if !said[ak] { |
| - said[ak] = true |
| + if _, done := said[ak]; !done { |
| // rt.Host anchors the line to a real route (the |
| // first one this pass happened to reach), so an |
| // operator scanning the log has something to grep |
| // for; every other route of this app is silently |
| // covered by the same line. |
| - fmt.Printf(" %s/%s: instance config unresolved (seen at %s), env files left as-is: %v\n", k.acct, k.app, rt.Host, err) |
| + said[ak] = say(k.acct+"/"+k.app, "instance config unresolved (seen at %s), env files left as-is: %v", rt.Host, err) |
| } |
| + plan.outcomes.refuse(rt.Host, said[ak]) |
| } else { |
| // A per-host Get or parse failure, unlike the listing |
| // above, is specific to THIS route: the listing already |
| @@ -327,14 +328,18 @@ func SyncWithCallbacks(ctx context.Context, reg *registry.Registry, s release.St |
| // failure of its own for a different reason. Folding this |
| // into the app-level line would leave that route's stale |
| // credential file invisible, so it is reported every time. |
| - fmt.Printf(" %s: instance config unresolved, env file left as-is: %v\n", rt.Host, err) |
| + plan.outcomes.refuse(rt.Host, say(rt.Host, "instance config unresolved, env file left as-is: %v", err)) |
| } |
| continue |
| } |
| } |
| m := r.m |
| if m == nil && inst == nil { |
| - continue // never configured at all: this file is not ours to touch |
| + // Never configured at all: this file is not ours to touch, and |
| + // nothing is printed. A declared unit is not delivered to, so |
| + // its outcome says why (no line carries this reason). |
| + plan.outcomes.refuse(rt.Host, fmt.Sprintf("no config is set for %s/%s, so there is nothing to deliver", k.acct, k.app)) |
| + continue |
| } |
| // An overlay with no group base behind it is a wholesale replacement, |
| // not an overlay. it is the same state a bound route with no default |
| @@ -359,12 +364,12 @@ func SyncWithCallbacks(ctx context.Context, reg *registry.Registry, s release.St |
| continue |
| } |
| if reason, bad := plan.refused[rt.Host]; bad { |
| - fmt.Printf(" %s: delivery blocked: %s\n", rt.Host, reason) |
| + plan.outcomes.refuse(rt.Host, say(rt.Host, "delivery blocked: %s", reason)) |
| continue |
| } |
| unit := plan.unitOf[rt.Host] |
| if unit == "" { |
| - fmt.Printf(" %s: stamped but planned no unit; skipped\n", rt.Host) |
| + plan.outcomes.refuse(rt.Host, say(rt.Host, "stamped but planned no unit; skipped")) |
| continue |
| } |
| work[unit] = prepareStamped(rt, unit, m, defaultMissing, priv, envDir, ops, plan) |
| @@ -383,12 +388,15 @@ func SyncWithCallbacks(ctx context.Context, reg *registry.Registry, s release.St |
| continue |
| } |
| if err := syncHostClaimed(ctx, selected[rt.Host], rt, m, defaultMissing, priv, envDir, restart, instETag, ack, callbacks); err != nil && hostWrites.Enabled { |
| + plan.outcomes.abandon(err.Error()) |
| + plan.outcomes.emit() |
| return err |
| } |
| } |
| if ops != nil { |
| reapDropIns(plan, ops) |
| applyStamped(envDir, ops, plan, work) |
| + plan.outcomes.emit() |
| } |
| // An empty route list is never authority to delete anything: |
| // registry.Open silently creates an empty database when handed a wrong |
| @@ -668,19 +676,25 @@ type hostRender struct { |
| // refusal and decides what else must happen before the env file lands |
| // (spec §3.2 step 1, codex R2-2). |
| func prepareHost(rt registry.Route, m *Manifest, defaultMissing bool, priv *ecdh.PrivateKey, envDir string) (*hostRender, error) { |
| + hr, _, err := prepareHostSaying(rt, m, defaultMissing, priv, envDir) |
| + return hr, err |
| +} |
| + |
| +// prepareHostSaying is prepareHost with the line it printed: with a nil |
| +// render, refusal is that line's text after the host, the reason the |
| +// stamped path's outcome carries (UnitOps.Outcome). |
| +func prepareHostSaying(rt registry.Route, m *Manifest, defaultMissing bool, priv *ecdh.PrivateKey, envDir string) (hr *hostRender, refusal string, err error) { |
| // The host is joined into a root-written path; a registry row carrying |
| // a separator must never place a file outside envDir. Same trust level |
| // as release's live/<host> symlinks, but this one is one line to pin. |
| if rt.Host == "" || rt.Host == "." || rt.Host == ".." || rt.Host != filepath.Base(rt.Host) { |
| - fmt.Printf(" %q: not a bare hostname, refusing to derive an env file path from it\n", rt.Host) |
| - return nil, nil |
| + return nil, say(fmt.Sprintf("%q", rt.Host), "not a bare hostname, refusing to derive an env file path from it"), nil |
| } |
| path := filepath.Join(envDir, rt.Host+".env") |
| cur, err := os.ReadFile(path) |
| existed := err == nil |
| if err != nil && !os.IsNotExist(err) { |
| - fmt.Printf(" %s: read %s: %v\n", rt.Host, path, err) |
| - return nil, err |
| + return nil, say(rt.Host, "read %s: %v", path, err), err |
| } |
| // The same protection an UNBOUND route gets from Sync's "no manifest |
| // object at all ⇒ leave the file alone" skip, applied to the DEFAULT half |
| @@ -727,23 +741,20 @@ func prepareHost(rt registry.Route, m *Manifest, defaultMissing bool, priv *ecdh |
| // Unparseable content has unknown keys, so nothing here can prove |
| // the render drops none of them. Assume the worst — this guard's |
| // whole job is to be wrong in the safe direction. |
| - fmt.Printf(" %s: %s has no default config bundle, so the merged view is %s ALONE — "+ |
| + return nil, say(rt.Host, "%s has no default config bundle, so the merged view is %s ALONE — "+ |
| "refusing to replace %s, which this pass cannot parse as KEY=value and so cannot prove is safe to overwrite; "+ |
| - "import its config (`carlos env set --app %s KEY=value ...`) or remove the file\n", |
| - rt.Host, rt.App, alone, path, rt.App) |
| - return nil, nil |
| + "import its config (`carlos env set --app %s KEY=value ...`) or remove the file", |
| + rt.App, alone, path, rt.App), nil |
| case len(lost) > 0: |
| - fmt.Printf(" %s: %s has no default config bundle, so the merged view is %s ALONE — "+ |
| + return nil, say(rt.Host, "%s has no default config bundle, so the merged view is %s ALONE — "+ |
| "refusing to drop %d var(s) already in %s (%s); import them (`carlos env set --app %s KEY=value ...`) "+ |
| - "and this route converges on the next pass, or remove the file if it is dead\n", |
| - rt.Host, rt.App, alone, len(lost), path, strings.Join(lost, ", "), rt.App) |
| - return nil, nil |
| + "and this route converges on the next pass, or remove the file if it is dead", |
| + rt.App, alone, len(lost), path, strings.Join(lost, ", "), rt.App), nil |
| } |
| } |
| rendered, err := RenderEnv(m, priv, routeBinding(rt)) |
| if err != nil { |
| - fmt.Printf(" %s: render config: %v\n", rt.Host, err) |
| - return nil, err |
| + return nil, say(rt.Host, "render config: %v", err), err |
| } |
| // Spec §7.3: deliver CARLOS_PURGE=header to unit tenants the same way |
| // the exec backend already does for exec-backed ones (withoutVars / |
| @@ -765,7 +776,7 @@ func prepareHost(rt registry.Route, m *Manifest, defaultMissing bool, priv *ecdh |
| // var itself is the payload, so the one-time fleet-wide restart is the |
| // feature landing, not a side effect of it. |
| rendered = applyAgentVars(rendered) |
| - return &hostRender{path: path, rendered: rendered, cur: cur, existed: existed}, nil |
| + return &hostRender{path: path, rendered: rendered, cur: cur, existed: existed}, "", nil |
| } |
| // commitHost writes rendered to path atomically, the stamped path's write |
| @@ -773,17 +784,22 @@ func prepareHost(rt registry.Route, m *Manifest, defaultMissing bool, priv *ecdh |
| // selected into one. False means the write failed and was printed; the |
| // file on disk is whatever it was. |
| func commitHost(host, path string, rendered []byte) bool { |
| + return commitHostSaying(host, path, rendered) == "" |
| +} |
| + |
| +// commitHostSaying is commitHost with the line it printed on a failure, |
| +// after the host ("" when the file landed): the stamped path's outcome |
| +// reason (UnitOps.Outcome). |
| +func commitHostSaying(host, path string, rendered []byte) string { |
| tmp := path + ".tmp" |
| if err := os.WriteFile(tmp, rendered, 0o600); err != nil { |
| - fmt.Printf(" %s: write %s: %v\n", host, path, err) |
| - return false |
| + return say(host, "write %s: %v", path, err) |
| } |
| if err := os.Rename(tmp, path); err != nil { |
| os.Remove(tmp) // don't leave rendered secrets behind |
| - fmt.Printf(" %s: rename %s: %v\n", host, path, err) |
| - return false |
| + return say(host, "rename %s: %v", path, err) |
| } |
| - return true |
| + return "" |
| } |
| // finishHost is the restart-and-ack half of syncHostClaimed, after the |
diff --git a/internal/appconfig/unitsync.go b/internal/appconfig/unitsync.go| index 7d0ba0b23..f27c30b5c 100644 |
| --- a/internal/appconfig/unitsync.go |
| +++ b/internal/appconfig/unitsync.go |
| @@ -61,6 +61,20 @@ type UnitOps struct { |
| // Restart is `systemctl restart <unit>` — restart, not try-restart: |
| // a marked unit owes a restart even when it is inactive. |
| Restart func(unit string) error |
| + // Outcome, when set, is called exactly once per declared host per pass, |
| + // after planning AND application finish, with the host's FINAL state: |
| + // "delivered" (config current, whether or not anything changed or |
| + // restarted), "refused" with the same reason the pass prints (the last |
| + // refusal wins over an earlier success), or "no-route" (declared, but |
| + // no stamped registry row selects it this pass). An unresolvable |
| + // config is "refused". Implementation: accumulate a map keyed by host |
| + // during the pass, emit once at the end. (Astra plan R1 #3.) |
| + // |
| + // The reason is the line the pass printed for the host after its |
| + // " <host>: " prefix (or the unit's, or "config sync: ", where that |
| + // is the line's prefix). Declared hosts are the declaration snapshot |
| + // the pass loaded; with none loaded, nothing is reported. |
| + Outcome func(host, unit, state, reason string) |
| } |
| // stampedPlan is what the pass decided about the stamped routes on this |
| @@ -84,6 +98,101 @@ type stampedPlan struct { |
| // still loads that host's env file (census R3-E1): such a file is |
| // neither rewritten by another row nor reaped while the drop-in stands. |
| referenced map[string]string |
| + // outcomes accumulates each declared host's state for UnitOps.Outcome; |
| + // nil when there is no hook or no declaration snapshot. |
| + outcomes *unitOutcomes |
| +} |
| + |
| +// The states UnitOps.Outcome reports. |
| +const ( |
| + OutcomeDelivered = "delivered" |
| + OutcomeRefused = "refused" |
| + OutcomeNoRoute = "no-route" |
| +) |
| + |
| +// unitOutcomes is one pass's per-declared-host state (UnitOps.Outcome), |
| +// recorded at the pass's own print sites and emitted once at the end. A |
| +// refusal always replaces what was recorded before it; a delivery never |
| +// replaces a refusal. A declared host with nothing recorded is no-route. |
| +// Every method is safe on nil. |
| +type unitOutcomes struct { |
| + decls *declarations |
| + emitTo func(host, unit, state, reason string) |
| + final map[string]unitOutcome |
| + routed map[string]bool // hosts a registry row this pass selects |
| + emitted bool |
| +} |
| + |
| +type unitOutcome struct{ state, reason string } |
| + |
| +func newUnitOutcomes(ops *UnitOps, decls *declarations, routes []registry.Route) *unitOutcomes { |
| + if ops == nil || ops.Outcome == nil || decls == nil { |
| + return nil |
| + } |
| + o := &unitOutcomes{decls: decls, emitTo: ops.Outcome, final: map[string]unitOutcome{}, routed: map[string]bool{}} |
| + for _, rt := range routes { |
| + if syncable(rt) || Stamped(rt) { |
| + o.routed[rt.Host] = true |
| + } |
| + } |
| + return o |
| +} |
| + |
| +func (o *unitOutcomes) refuse(host, reason string) { |
| + if o == nil { |
| + return |
| + } |
| + o.final[host] = unitOutcome{OutcomeRefused, reason} |
| +} |
| + |
| +func (o *unitOutcomes) deliver(host string) { |
| + if o == nil || o.final[host].state == OutcomeRefused { |
| + return |
| + } |
| + o.final[host] = unitOutcome{state: OutcomeDelivered} |
| +} |
| + |
| +// abandon is a pass that stops before its end (the tripwire, an error): |
| +// every declared host a row selects and nothing refused yet is refused |
| +// with reason, since whatever was prepared for it was never applied. |
| +func (o *unitOutcomes) abandon(reason string) { |
| + if o == nil { |
| + return |
| + } |
| + for host := range o.decls.byHost { |
| + if o.routed[host] && o.final[host].state != OutcomeRefused { |
| + o.refuse(host, reason) |
| + } |
| + } |
| +} |
| + |
| +// emit calls the hook once per declared host, in host order, and only |
| +// the first time it is called in a pass. |
| +func (o *unitOutcomes) emit() { |
| + if o == nil || o.emitted { |
| + return |
| + } |
| + o.emitted = true |
| + hosts := make([]string, 0, len(o.decls.byHost)) |
| + for host := range o.decls.byHost { |
| + hosts = append(hosts, host) |
| + } |
| + sort.Strings(hosts) |
| + for _, host := range hosts { |
| + got, ok := o.final[host] |
| + if !ok { |
| + got = unitOutcome{state: OutcomeNoRoute} |
| + } |
| + o.emitTo(host, o.decls.byHost[host].Unit, got.state, got.reason) |
| + } |
| +} |
| + |
| +// say prints one line of the pass, " <prefix>: <msg>", and returns msg: |
| +// the reason an outcome carries is the text the pass printed. |
| +func say(prefix, format string, args ...any) string { |
| + msg := fmt.Sprintf(format, args...) |
| + fmt.Printf(" %s: %s\n", prefix, msg) |
| + return msg |
| } |
| // declaredHostRefusal is invariant A at the dispatch (round 3 P1-1, census |
| @@ -127,6 +236,7 @@ func planStamped(routes []registry.Route, envDir string, ops *UnitOps) stampedPl |
| p := stampedPlan{unitOf: map[string]string{}, refused: map[string]string{}, probe: map[string]UnitState{}} |
| decls, declErr := loadDeclarations(ops) |
| p.decls = decls |
| + p.outcomes = newUnitOutcomes(ops, decls, routes) |
| if decls != nil { |
| p.referenced = referencedEnvHosts(ops, envDir) |
| } |
| @@ -261,11 +371,12 @@ func prepareStamped(rt registry.Route, unit string, m *Manifest, defaultMissing |
| return w |
| } |
| if _, err := plan.decls.DeclaredBinding(rt); err != nil || rt.Unit != unit { |
| - fmt.Printf(" %s: delivery blocked: not declared: %v\n", rt.Host, err) |
| + plan.outcomes.refuse(rt.Host, say(rt.Host, "delivery blocked: not declared: %v", err)) |
| return w |
| } |
| - hr, _ := prepareHost(rt, m, defaultMissing, priv, envDir) |
| + hr, refusal, _ := prepareHostSaying(rt, m, defaultMissing, priv, envDir) |
| if hr == nil { |
| + plan.outcomes.refuse(rt.Host, refusal) |
| return w |
| } |
| path, rendered, cur, existed := hr.path, hr.rendered, hr.cur, hr.existed |
| @@ -273,7 +384,7 @@ func prepareStamped(rt registry.Route, unit string, m *Manifest, defaultMissing |
| // (review round 1 P1-2): refused for a stamped route at render, by |
| // name, before either layer is compared. |
| if name := carriageReturnVar(rendered); name != "" { |
| - fmt.Printf(" %s: delivery blocked: the value of %s contains a carriage return, which systemd reads as a line break; set it without one and the route converges on the next pass\n", rt.Host, name) |
| + plan.outcomes.refuse(rt.Host, say(rt.Host, "delivery blocked: the value of %s contains a carriage return, which systemd reads as a line break; set it without one and the route converges on the next pass", name)) |
| return w |
| } |
| // One owner per variable (§4.1): a name stated by both the base file |
| @@ -282,26 +393,26 @@ func prepareStamped(rt registry.Route, unit string, m *Manifest, defaultMissing |
| base := baseEnvPath(ops.BaseEnvDir, unit) |
| baseNames, err := envFileNames(ops, base) |
| if err != nil { |
| - fmt.Printf(" %s: delivery blocked: %s: %v; the base file must be root's, plain NAME=value lines, and readable without following a link\n", rt.Host, base, err) |
| + plan.outcomes.refuse(rt.Host, say(rt.Host, "delivery blocked: %s: %v; the base file must be root's, plain NAME=value lines, and readable without following a link", base, err)) |
| return w |
| } |
| if both := overlappingNames(baseNames, rendered); len(both) > 0 { |
| - fmt.Printf(" %s: delivery blocked: %s and the route's config both set %s; one layer owns each variable. Remove the name from one of them and the route converges on the next pass\n", |
| - rt.Host, base, strings.Join(both, ", ")) |
| + plan.outcomes.refuse(rt.Host, say(rt.Host, "delivery blocked: %s and the route's config both set %s; one layer owns each variable. Remove the name from one of them and the route converges on the next pass", |
| + base, strings.Join(both, ", "))) |
| return w |
| } |
| if err := ensureDropInDir(ops, unit); err != nil { |
| - fmt.Printf(" %s: delivery blocked: drop-in directory for %s: %v\n", rt.Host, unit, err) |
| + plan.outcomes.refuse(rt.Host, say(rt.Host, "delivery blocked: drop-in directory for %s: %v", unit, err)) |
| return w |
| } |
| curDrop, dropExists, marked, err := readDropIn(ops, unit) |
| if err != nil { |
| - fmt.Printf(" %s: delivery blocked: read drop-in for %s: %v\n", rt.Host, unit, err) |
| + plan.outcomes.refuse(rt.Host, say(rt.Host, "delivery blocked: read drop-in for %s: %v", unit, err)) |
| return w |
| } |
| if dropExists && !marked { |
| - fmt.Printf(" %s: %s holds a %s that is %v; delivery is blocked until it is moved\n", |
| - rt.Host, dropInDir(ops.DropInRoot, unit), DropInFile, errNotOurs) |
| + plan.outcomes.refuse(rt.Host, say(rt.Host, "%s holds a %s that is %v; delivery is blocked until it is moved", |
| + dropInDir(ops.DropInRoot, unit), DropInFile, errNotOurs)) |
| return w |
| } |
| wantDrop := renderDropIn(envDir, ops.BaseEnvDir, unit, rt.Host) |
| @@ -309,18 +420,22 @@ func prepareStamped(rt registry.Route, unit string, m *Manifest, defaultMissing |
| dropChanged := !(dropExists && bytes.Equal(curDrop, wantDrop)) |
| if !envChanged && !dropChanged { |
| w.ok = true |
| + plan.outcomes.deliver(rt.Host) |
| return w |
| } |
| if err := writePending(ops.DropInRoot, unit); err != nil { |
| - fmt.Printf(" %s: record pending restart for %s: %v (nothing written)\n", rt.Host, unit, err) |
| + plan.outcomes.refuse(rt.Host, say(rt.Host, "record pending restart for %s: %v (nothing written)", unit, err)) |
| return w |
| } |
| - if envChanged && !commitHost(rt.Host, path, rendered) { |
| - return w |
| + if envChanged { |
| + if failed := commitHostSaying(rt.Host, path, rendered); failed != "" { |
| + plan.outcomes.refuse(rt.Host, failed) |
| + return w |
| + } |
| } |
| if dropChanged { |
| if err := writeDropIn(ops.DropInRoot, unit, wantDrop); err != nil { |
| - fmt.Printf(" %s: write drop-in for %s: %v\n", rt.Host, unit, err) |
| + plan.outcomes.refuse(rt.Host, say(rt.Host, "write drop-in for %s: %v", unit, err)) |
| return w |
| } |
| fmt.Printf(" %s: drop-in written for %s\n", rt.Host, unit) |
| @@ -329,6 +444,7 @@ func prepareStamped(rt registry.Route, unit string, m *Manifest, defaultMissing |
| fmt.Printf(" %s: config updated for %s\n", rt.Host, unit) |
| } |
| w.ok = true |
| + plan.outcomes.deliver(rt.Host) |
| return w |
| } |
| @@ -401,7 +517,10 @@ func applyStamped(envDir string, ops *UnitOps, plan stampedPlan, work map[string |
| if len(pending) > 0 { |
| if err := ops.Reload(); err != nil { |
| for _, unit := range pending { |
| - fmt.Printf(" %s: daemon-reload failed (%v); reload and restart still owed\n", unit, err) |
| + msg := say(unit, "daemon-reload failed (%v); reload and restart still owed", err) |
| + if w, prepared := work[unit]; prepared && w.ok { |
| + plan.outcomes.refuse(w.host, msg) |
| + } |
| } |
| return |
| } |
| @@ -412,7 +531,10 @@ func applyStamped(envDir string, ops *UnitOps, plan stampedPlan, work map[string |
| w, prepared := work[unit] |
| _, exists, isMarked, err := readDropIn(ops, unit) |
| if err != nil { |
| - fmt.Printf(" %s: read drop-in: %v; restart still owed\n", unit, err) |
| + msg := say(unit, "read drop-in: %v; restart still owed", err) |
| + if prepared && w.ok { |
| + plan.outcomes.refuse(w.host, msg) |
| + } |
| continue |
| } |
| switch { |
| @@ -435,17 +557,17 @@ func applyStamped(envDir string, ops *UnitOps, plan stampedPlan, work map[string |
| } |
| states, err := ops.Probe([]string{unit}) |
| if err != nil { |
| - fmt.Printf(" %s: post-reload probe failed (%v); restart still owed\n", unit, err) |
| + plan.outcomes.refuse(w.host, say(unit, "post-reload probe failed (%v); restart still owed", err)) |
| continue |
| } |
| if st, ok := states[unit]; !ok || st.LoadState != "loaded" { |
| - fmt.Printf(" %s: %s is not loaded after reload (%s); restart still owed\n", w.host, unit, st.LoadState) |
| + plan.outcomes.refuse(w.host, say(w.host, "%s is not loaded after reload (%s); restart still owed", unit, st.LoadState)) |
| continue |
| } |
| base, want := baseEnvPath(ops.BaseEnvDir, unit), filepath.Join(envDir, w.host+".env") |
| if !envFilesMatch(states[unit].EnvFiles, base, want) { |
| - fmt.Printf(" %s: delivery blocked: %s reads %v after reload, want exactly [%s %s]; a later drop-in owns the EnvironmentFile list\n", |
| - w.host, unit, states[unit].EnvFiles, base, want) |
| + plan.outcomes.refuse(w.host, say(w.host, "delivery blocked: %s reads %v after reload, want exactly [%s %s]; a later drop-in owns the EnvironmentFile list", |
| + unit, states[unit].EnvFiles, base, want)) |
| continue |
| } |
| // The decider, again, before the restart (invariant A). |
| @@ -453,18 +575,22 @@ func applyStamped(envDir string, ops *UnitOps, plan stampedPlan, work map[string |
| continue |
| } |
| if _, err := plan.decls.DeclaredBinding(w.rt); err != nil { |
| - fmt.Printf(" %s: restart of %s refused: not declared: %v\n", w.host, unit, err) |
| + plan.outcomes.refuse(w.host, say(w.host, "restart of %s refused: not declared: %v", unit, err)) |
| continue |
| } |
| if err := ops.Restart(unit); err != nil { |
| - fmt.Printf(" %s: config updated but restart of %s failed: %v; restart still owed\n", w.host, unit, err) |
| + plan.outcomes.refuse(w.host, say(w.host, "config updated but restart of %s failed: %v; restart still owed", unit, err)) |
| continue |
| } |
| + // A marker that cannot be cleared after the restart costs one more |
| + // restart next pass; the unit runs this config now, so the host |
| + // stays delivered. |
| if err := removePending(ops.DropInRoot, unit); err != nil { |
| fmt.Printf(" %s: clear pending marker for %s: %v\n", w.host, unit, err) |
| continue |
| } |
| fmt.Printf(" %s: config updated, %s restarted\n", w.host, unit) |
| + plan.outcomes.deliver(w.host) |
| } |
| // Drift (step 7): an unmarked, delivered unit whose pre-mutation probe |
| // shows anything but carlos's file is running what the operator |
| @@ -481,8 +607,8 @@ func applyStamped(envDir string, ops *UnitOps, plan stampedPlan, work map[string |
| } |
| base, want := baseEnvPath(ops.BaseEnvDir, unit), filepath.Join(envDir, w.host+".env") |
| if st, ok := plan.probe[unit]; ok && !envFilesMatch(st.EnvFiles, base, want) { |
| - fmt.Printf(" %s: delivery blocked: %s reads %v, want exactly [%s %s]; a later drop-in owns the EnvironmentFile list\n", |
| - w.host, unit, st.EnvFiles, base, want) |
| + plan.outcomes.refuse(w.host, say(w.host, "delivery blocked: %s reads %v, want exactly [%s %s]; a later drop-in owns the EnvironmentFile list", |
| + unit, st.EnvFiles, base, want)) |
| } |
| } |
| } |
diff --git a/internal/appconfig/unitsync_test.go b/internal/appconfig/unitsync_test.go| index 31852d045..78b7ac4a7 100644 |
| --- a/internal/appconfig/unitsync_test.go |
| +++ b/internal/appconfig/unitsync_test.go |
| @@ -1626,3 +1626,220 @@ func TestSyncUnitsRefusesAnOrdinaryRowWhoseHostIsDeclared(t *testing.T) { |
| t.Fatalf("the ordinary writer wrote a declared host: restarts=%d", hostRestarts) |
| } |
| } |
| + |
| +// outcomeCall is one call of UnitOps.Outcome. |
| +type outcomeCall struct{ host, unit, state, reason string } |
| + |
| +// syncWithOutcomes runs one pass with an Outcome hook and returns what it |
| +// printed and every call the hook received, in order. |
| +func syncWithOutcomes(t *testing.T, ctx context.Context, reg *registry.Registry, s release.Store, priv *ecdh.PrivateKey, envDir string, f *fakeUnits) (string, []outcomeCall) { |
| + t.Helper() |
| + var calls []outcomeCall |
| + ops := f.ops() |
| + ops.Outcome = func(host, unit, state, reason string) { |
| + calls = append(calls, outcomeCall{host, unit, state, reason}) |
| + } |
| + out := captureStdout(t, func() { |
| + if err := SyncUnits(ctx, reg, s, priv, envDir, noHostRestart(t), nil, ops); err != nil { |
| + t.Fatalf("SyncUnits: %v", err) |
| + } |
| + }) |
| + return out, calls |
| +} |
| + |
| +// outcomeFor is the one call for host, failing if there is not exactly one. |
| +func outcomeFor(t *testing.T, calls []outcomeCall, host string) outcomeCall { |
| + t.Helper() |
| + var got []outcomeCall |
| + for _, c := range calls { |
| + if c.host == host { |
| + got = append(got, c) |
| + } |
| + } |
| + if len(got) != 1 { |
| + t.Fatalf("%s: %d Outcome calls, want exactly one: %+v", host, len(got), calls) |
| + } |
| + return got[0] |
| +} |
| + |
| +// printedReason: a refusal's reason is the text the pass printed for the |
| +// host, the line after its " <host>: " prefix. |
| +func printedReason(t *testing.T, out string, c outcomeCall) { |
| + t.Helper() |
| + if c.reason == "" || !strings.Contains(out, " "+c.host+": "+c.reason+"\n") { |
| + t.Fatalf("%s: reason %q is not a line the pass printed:\n%s", c.host, c.reason, out) |
| + } |
| +} |
| + |
| +func TestSyncUnitsReportsOneOutcomePerDeclaredHost(t *testing.T) { |
| + ctx, s, reg, envDir, priv := unitKit(t) |
| + f := newFakeUnits(t) |
| + f.loaded("relay.service", envDir, "relay.example.com") |
| + f.states["console.service"] = UnitState{ID: "console.service", LoadState: "loaded", |
| + EnvFiles: []string{"/etc/console/hand.env", filepath.Join(envDir, "console.example.com.env")}} |
| + stampDeclared(t, f, reg, "relay.example.com", "relay.service", nil) |
| + stampDeclared(t, f, reg, "console.example.com", "console.service", nil) |
| + |
| + out, calls := syncWithOutcomes(t, ctx, reg, s, priv, envDir, f) |
| + if len(calls) != 2 { |
| + t.Fatalf("Outcome calls = %+v, want exactly two", calls) |
| + } |
| + if c := outcomeFor(t, calls, "relay.example.com"); c.unit != "relay.service" || c.state != "delivered" || c.reason != "" { |
| + t.Fatalf("relay: %+v, want delivered on relay.service with no reason", c) |
| + } |
| + c := outcomeFor(t, calls, "console.example.com") |
| + if c.unit != "console.service" || c.state != "refused" || !strings.Contains(c.reason, "/etc/console/hand.env") { |
| + t.Fatalf("console: %+v, want refused on console.service naming the unmanaged file", c) |
| + } |
| + printedReason(t, out, c) |
| +} |
| + |
| +func TestSyncUnitsOutcomeUnchangedIsDelivered(t *testing.T) { |
| + ctx, s, reg, envDir, priv := unitKit(t) |
| + f := newFakeUnits(t) |
| + f.loaded("relay.service", envDir, "relay.example.com") |
| + stampDeclared(t, f, reg, "relay.example.com", "relay.service", nil) |
| + syncOnce(t, ctx, reg, s, priv, envDir, f) |
| + f.restarted, f.reloads = nil, 0 |
| + |
| + _, calls := syncWithOutcomes(t, ctx, reg, s, priv, envDir, f) |
| + if f.reloads != 0 || len(f.restarted) != 0 { |
| + t.Fatalf("second pass changed something: reloads=%d restarted=%v", f.reloads, f.restarted) |
| + } |
| + if len(calls) != 1 || calls[0] != (outcomeCall{"relay.example.com", "relay.service", "delivered", ""}) { |
| + t.Fatalf("Outcome calls = %+v, want one delivered", calls) |
| + } |
| +} |
| + |
| +func TestSyncUnitsOutcomeLaterRefusalWins(t *testing.T) { |
| + ctx, s, reg, envDir, priv := unitKit(t) |
| + f := newFakeUnits(t) |
| + f.loaded("relay.service", envDir, "relay.example.com") |
| + stampDeclared(t, f, reg, "relay.example.com", "relay.service", nil) |
| + syncOnce(t, ctx, reg, s, priv, envDir, f) |
| + // The write succeeds (a config change), then the post-reload check |
| + // finds a legacy file ahead of carlos's two. |
| + f.states["relay.service"] = UnitState{ID: "relay.service", LoadState: "loaded", |
| + EnvFiles: []string{"/data/relay.example.com.config.env", baseEnvPath(f.base, "relay.service"), filepath.Join(envDir, "relay.example.com.env")}} |
| + if err := SetEnv(ctx, s, "bab", "relay", map[string]string{"LOG_LEVEL": "info"}); err != nil { |
| + t.Fatal(err) |
| + } |
| + out, calls := syncWithOutcomes(t, ctx, reg, s, priv, envDir, f) |
| + if !strings.Contains(out, "relay.example.com: config updated for relay.service") { |
| + t.Fatalf("the write did not succeed first:\n%s", out) |
| + } |
| + if len(calls) != 1 { |
| + t.Fatalf("Outcome calls = %+v, want exactly one", calls) |
| + } |
| + c := calls[0] |
| + if c.state != "refused" || !strings.Contains(c.reason, "/data/relay.example.com.config.env") { |
| + t.Fatalf("%+v, want refused naming the list the unit reads", c) |
| + } |
| + printedReason(t, out, c) |
| +} |
| + |
| +func TestSyncUnitsOutcomeNoRouteAndUnresolved(t *testing.T) { |
| + ctx, s, reg, envDir, priv := unitKit(t) |
| + f := newFakeUnits(t) |
| + f.loaded("bound.service", envDir, "bound.example.com") |
| + stampDeclared(t, f, reg, "bound.example.com", "bound.service", func(r *registry.Route) { r.Environment = "missing" }) |
| + // Declared, with no registry row at all. |
| + f.declared = append(f.declared, Declaration{Host: "gone.example.com", Account: "bab", App: "relay", Unit: "gone.service"}) |
| + // Declared, with a row no stamped selection reaches (not ready). |
| + f.declared = append(f.declared, Declaration{Host: "wait.example.com", Account: "bab", App: "relay", Unit: "wait.service"}) |
| + f.declare(t, f.declared...) |
| + upsertStamped(t, reg, "wait.example.com", "wait.service", func(r *registry.Route) { r.Status = "provisioning" }) |
| + |
| + out, calls := syncWithOutcomes(t, ctx, reg, s, priv, envDir, f) |
| + if len(calls) != 3 { |
| + t.Fatalf("Outcome calls = %+v, want one per declared host", calls) |
| + } |
| + for _, host := range []string{"gone.example.com", "wait.example.com"} { |
| + if c := outcomeFor(t, calls, host); c.state != "no-route" || c.reason != "" || c.unit != strings.TrimSuffix(host, ".example.com")+".service" { |
| + t.Fatalf("%s: %+v, want no-route on its declared unit", host, c) |
| + } |
| + } |
| + c := outcomeFor(t, calls, "bound.example.com") |
| + if c.state != "refused" || !strings.Contains(c.reason, "config unresolved") { |
| + t.Fatalf("bound: %+v, want refused as unresolved", c) |
| + } |
| + printedReason(t, out, c) |
| +} |
| + |
| +// A stamped, declared route whose app has no config at all writes nothing |
| +// (the file is not ours to touch); it is refused, not delivered. |
| +func TestSyncUnitsOutcomeNeverConfiguredIsRefused(t *testing.T) { |
| + ctx, s, reg, envDir, priv := unitKit(t) |
| + f := newFakeUnits(t) |
| + f.loaded("quiet.service", envDir, "quiet.example.com") |
| + upsertStamped(t, reg, "quiet.example.com", "quiet.service", func(r *registry.Route) { r.App = "quiet" }) |
| + f.declare(t, Declaration{Host: "quiet.example.com", Account: "bab", App: "quiet", Unit: "quiet.service"}) |
| + |
| + _, calls := syncWithOutcomes(t, ctx, reg, s, priv, envDir, f) |
| + if len(calls) != 1 || calls[0].state != "refused" || !strings.Contains(calls[0].reason, "bab/quiet") { |
| + t.Fatalf("Outcome calls = %+v, want one refused naming bab/quiet", calls) |
| + } |
| + if _, err := os.Stat(filepath.Join(envDir, "quiet.example.com.env")); !os.IsNotExist(err) { |
| + t.Fatal("a never-configured route must get no env file") |
| + } |
| +} |
| + |
| +// The tripwire abandons the pass before any mutation: every declared host |
| +// is refused with the reason printed. |
| +func TestSyncUnitsOutcomeTripwireRefusesEveryDeclaredHost(t *testing.T) { |
| + ctx, s, reg, envDir, priv := unitKit(t) |
| + f := newFakeUnits(t) |
| + f.loaded("relay.service", envDir, "relay.example.com") |
| + stampDeclared(t, f, reg, "relay.example.com", "relay.service", nil) |
| + f.onProbe = func(call int) { |
| + if call == 1 { |
| + f.declare(t, Declaration{Host: "relay.example.com", Account: "bab", App: "relay", Unit: "other.service"}) |
| + } |
| + } |
| + out, calls := syncWithOutcomes(t, ctx, reg, s, priv, envDir, f) |
| + if len(calls) != 1 || calls[0].state != "refused" || !strings.Contains(calls[0].reason, "changed during this pass") { |
| + t.Fatalf("Outcome calls = %+v, want one refused with the tripwire's reason", calls) |
| + } |
| + if !strings.Contains(out, " config sync: "+calls[0].reason+"\n") { |
| + t.Fatalf("reason %q is not the printed tripwire line:\n%s", calls[0].reason, out) |
| + } |
| +} |
| + |
| +// With the declaration file unreadable no host is known to be declared, |
| +// so nothing is reported; with no lock, nothing was planned. |
| +func TestSyncUnitsOutcomeNothingWithoutDeclarationsOrLock(t *testing.T) { |
| + ctx, s, reg, envDir, priv := unitKit(t) |
| + f := newFakeUnits(t) |
| + f.loaded("relay.service", envDir, "relay.example.com") |
| + stampDeclared(t, f, reg, "relay.example.com", "relay.service", nil) |
| + f.declRaw(t, "not json") |
| + if _, calls := syncWithOutcomes(t, ctx, reg, s, priv, envDir, f); len(calls) != 0 { |
| + t.Fatalf("unreadable declarations: Outcome calls = %+v, want none", calls) |
| + } |
| + f.declare(t, f.declared...) |
| + f.lockErr = errors.New("held elsewhere") |
| + if _, calls := syncWithOutcomes(t, ctx, reg, s, priv, envDir, f); len(calls) != 0 { |
| + t.Fatalf("no lock: Outcome calls = %+v, want none", calls) |
| + } |
| +} |
| + |
| +func TestSyncUnitsOutcomeNilIsSafe(t *testing.T) { |
| + ctx, s, reg, envDir, priv := unitKit(t) |
| + f := newFakeUnits(t) |
| + f.loaded("relay.service", envDir, "relay.example.com") |
| + f.states["console.service"] = UnitState{ID: "console.service", LoadState: "loaded", EnvFiles: []string{"/etc/console/hand.env"}} |
| + stampDeclared(t, f, reg, "relay.example.com", "relay.service", nil) |
| + stampDeclared(t, f, reg, "console.example.com", "console.service", nil) |
| + f.declared = append(f.declared, Declaration{Host: "gone.example.com", Account: "bab", App: "relay", Unit: "gone.service"}) |
| + f.declare(t, f.declared...) |
| + if f.ops().Outcome != nil { |
| + t.Fatal("the fake's ops must leave Outcome nil for this test") |
| + } |
| + syncOnce(t, ctx, reg, s, priv, envDir, f) |
| + if len(f.restarted) != 1 || f.restarted[0] != "relay.service" || hasPending(f.root, "relay.service") { |
| + t.Fatalf("restarted=%v pending=%v, want relay.service delivered as before", f.restarted, hasPending(f.root, "relay.service")) |
| + } |
| + if _, err := os.Stat(filepath.Join(envDir, "console.example.com.env")); !os.IsNotExist(err) { |
| + t.Fatal("the refused route must get no env file") |
| + } |
| +} |
diff --git a/internal/host/agent.go b/internal/host/agent.go| index e92c1903c..78822e5a0 100644 |
| --- a/internal/host/agent.go |
| +++ b/internal/host/agent.go |
| @@ -6184,14 +6184,12 @@ func runAdoptUnderRootRelease(args []string, buildClaims ClaimFactory, ensureUni |
| // runs before release adoption so a newly adopted route's very first |
| // start already sees its env file, and a pass carrying both a release |
| // and a config change never starts the new binary against stale config. |
| - if raw := os.Getenv("CARLOS_SECRETS_KEY"); raw != "" { |
| - secretsPriv, err := appconfig.ParsePrivateKey(raw, "CARLOS_SECRETS_KEY") |
| - if err != nil { |
| - log.Printf("adopt: config sync disabled: %v", err) |
| - } else if err := appconfig.SyncUnits(ctx, reg, store, secretsPriv, appconfig.DefaultEnvDir, restart, configAckRootFunc(ctx, store), productionUnitOps(appconfig.DefaultEnvDir), claims.Writes); err != nil { |
| - log.Printf("adopt: config sync: %v", err) |
| - } |
| - } |
| + // The named-units record is written again after it, with each |
| + // declared unit's outcome; a pass that skips config sync for want of |
| + // a usable key leaves the step's record as it is. |
| + adoptConfigSync(os.Getenv("CARLOS_SECRETS_KEY"), namedUnits, productionUnitOps(appconfig.DefaultEnvDir), func(secretsPriv *ecdh.PrivateKey, ops *appconfig.UnitOps) error { |
| + return appconfig.SyncUnits(ctx, reg, store, secretsPriv, appconfig.DefaultEnvDir, restart, configAckRootFunc(ctx, store), ops, claims.Writes) |
| + }, writeNamedUnitsStatus) |
| if *configOnly { |
| // Everything below is release adoption and its reporting — the half |
| // carlos-config-sync.service must never reach, however often a |
diff --git a/internal/host/namedunitsfetch.go b/internal/host/namedunitsfetch.go| index d6fa41782..1009500b0 100644 |
| --- a/internal/host/namedunitsfetch.go |
| +++ b/internal/host/namedunitsfetch.go |
| @@ -11,12 +11,14 @@ package host |
| import ( |
| "bytes" |
| "context" |
| + "crypto/ecdh" |
| "crypto/sha256" |
| "encoding/hex" |
| "encoding/json" |
| "errors" |
| "fmt" |
| "io" |
| + "log" |
| "os" |
| "slices" |
| "time" |
| @@ -302,6 +304,33 @@ func writeNamedUnitsStatus(st status.NamedUnitsStatus) error { |
| return s.Write("status.json", raw) |
| } |
| +// adoptConfigSync is runAdoptUnderRootRelease's config sync (inert until |
| +// the box holds CARLOS_SECRETS_KEY) and the record's second write: ops |
| +// gets an Outcome that appends each declared unit's state to st.Units, |
| +// and once sync returns st is written again, the fetch outcome with the |
| +// units beside it. Without a usable key nothing syncs and nothing is |
| +// written; the step's own record stands. Never fatal. |
| +func adoptConfigSync(secretsKey string, st status.NamedUnitsStatus, ops *appconfig.UnitOps, sync func(priv *ecdh.PrivateKey, ops *appconfig.UnitOps) error, write func(status.NamedUnitsStatus) error) { |
| + if secretsKey == "" { |
| + return |
| + } |
| + priv, err := appconfig.ParsePrivateKey(secretsKey, "CARLOS_SECRETS_KEY") |
| + if err != nil { |
| + log.Printf("adopt: config sync disabled: %v", err) |
| + return |
| + } |
| + st.Units = nil |
| + ops.Outcome = func(host, unit, state, reason string) { |
| + st.Units = append(st.Units, status.NamedUnitDelivery{Host: host, Unit: unit, State: state, Reason: reason}) |
| + } |
| + if err := sync(priv, ops); err != nil { |
| + log.Printf("adopt: config sync: %v", err) |
| + } |
| + if err := write(st); err != nil { |
| + log.Printf("adopt: named-units status: %v", err) |
| + } |
| +} |
| + |
| // namedUnitsLogLine is the step's one journal line per pass. |
| func namedUnitsLogLine(st status.NamedUnitsStatus) string { |
| switch st.Outcome { |
diff --git a/internal/host/namedunitsfetch_test.go b/internal/host/namedunitsfetch_test.go| index d6d4f59e8..e2128f9b3 100644 |
| --- a/internal/host/namedunitsfetch_test.go |
| +++ b/internal/host/namedunitsfetch_test.go |
| @@ -2,6 +2,7 @@ package host |
| import ( |
| "context" |
| + "crypto/ecdh" |
| "crypto/sha256" |
| "encoding/hex" |
| "errors" |
| @@ -553,3 +554,98 @@ func TestNamedUnitsSkipReason(t *testing.T) { |
| }) |
| } |
| } |
| + |
| +// TestAdoptWritesNamedUnitsStatusAfterSync: the record is written once |
| +// for the fetch step (before the release key parse) and, on a pass that |
| +// runs config sync, once more with each declared unit's outcome. Without |
| +// a usable secrets key config sync is skipped and so is the second write; |
| +// a -restart-only pass returns before either. |
| +func TestAdoptWritesNamedUnitsStatusAfterSync(t *testing.T) { |
| + step := func(context.Context) status.NamedUnitsStatus { |
| + return status.NamedUnitsStatus{Parameter: "/carlos/boxes/" + fetchIID + "/named-units", InstanceID: fetchIID, Region: fetchRegion, |
| + Version: 3, Outcome: namedUnitsUnchanged, At: time.Unix(1700000000, 0).UTC()} |
| + } |
| + key, _, err := appconfig.GenerateKey() |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + // What runAdoptUnderRootRelease does from the step on, with the step, |
| + // the writer and SyncUnits faked: the step's record written, then |
| + // adoptConfigSync. |
| + pass := func(secretsKey string) (writes []status.NamedUnitsStatus, synced int) { |
| + write := func(st status.NamedUnitsStatus) error { writes = append(writes, st); return nil } |
| + st := step(context.Background()) |
| + if err := write(st); err != nil { |
| + t.Fatal(err) |
| + } |
| + adoptConfigSync(secretsKey, st, &appconfig.UnitOps{}, func(priv *ecdh.PrivateKey, ops *appconfig.UnitOps) error { |
| + synced++ |
| + if priv == nil || ops.Outcome == nil { |
| + t.Fatalf("sync got priv=%v Outcome nil=%v", priv, ops.Outcome == nil) |
| + } |
| + ops.Outcome("a.example.com", "shop.service", "delivered", "") |
| + ops.Outcome("b.example.com", "shop-b.service", "refused", "delivery blocked: why") |
| + return errors.New("a later route failed") |
| + }, write) |
| + return writes, synced |
| + } |
| + |
| + writes, synced := pass(key) |
| + if synced != 1 || len(writes) != 2 { |
| + t.Fatalf("with a key: synced=%d writes=%d, want 1 and 2", synced, len(writes)) |
| + } |
| + if len(writes[0].Units) != 0 { |
| + t.Fatalf("the fetch record carries units: %+v", writes[0].Units) |
| + } |
| + want := writes[0] |
| + want.Units = []status.NamedUnitDelivery{ |
| + {Host: "a.example.com", Unit: "shop.service", State: "delivered"}, |
| + {Host: "b.example.com", Unit: "shop-b.service", State: "refused", Reason: "delivery blocked: why"}, |
| + } |
| + if !reflect.DeepEqual(writes[1], want) { |
| + t.Fatalf("second write = %+v, want the fetch record plus units %+v", writes[1], want) |
| + } |
| + for _, k := range []string{"", "not a key"} { |
| + if writes, synced := pass(k); synced != 0 || len(writes) != 1 { |
| + t.Fatalf("key %q: synced=%d writes=%d, want 0 and 1", k, synced, len(writes)) |
| + } |
| + } |
| + |
| + // The real pass calls exactly that, in that order, after the |
| + // -restart-only return and before the -config-only one. |
| + raw, err := os.ReadFile("agent.go") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + src := string(raw) |
| + start := strings.Index(src, "\nfunc runAdoptUnderRootRelease(") |
| + if start < 0 { |
| + t.Fatal("runAdoptUnderRootRelease is no longer in agent.go, move this test with it") |
| + } |
| + body := src[start+1:] |
| + if end := strings.Index(body, "\n}\n"); end >= 0 { |
| + body = body[:end] |
| + } |
| + at := func(s string) int { |
| + t.Helper() |
| + i := strings.Index(body, s) |
| + if i < 0 { |
| + t.Fatalf("runAdoptUnderRootRelease no longer contains %q", s) |
| + } |
| + return i |
| + } |
| + for _, s := range []string{"adoptNamedUnitsStep(", "writeNamedUnitsStatus(", "adoptConfigSync(", "appconfig.SyncUnits("} { |
| + if n := strings.Count(body, s); n != 1 { |
| + t.Fatalf("%s appears %d times in runAdoptUnderRootRelease, want once", s, n) |
| + } |
| + } |
| + restart := at("if *restartOnly {") |
| + restartEnd := restart + strings.Index(body[restart:], "\n\t}\n") |
| + step0, write0 := at("adoptNamedUnitsStep("), at("writeNamedUnitsStatus(") |
| + pub, cfg := at("release.ParsePublicKey("), at("adoptConfigSync(") |
| + sync, configOnly := at("appconfig.SyncUnits("), at("if *configOnly {\n\t\t// Everything below") |
| + if !(restartEnd < step0 && step0 < write0 && write0 < pub && pub < cfg && cfg < sync && sync < configOnly) { |
| + t.Fatalf("order: restart-only end %d, step %d, write %d, ParsePublicKey %d, adoptConfigSync %d, SyncUnits %d, config-only return %d", |
| + restartEnd, step0, write0, pub, cfg, sync, configOnly) |
| + } |
| +} |
diff --git a/internal/status/snapshot.go b/internal/status/snapshot.go| index 0b04b3e8a..a364bfb6d 100644 |
| --- a/internal/status/snapshot.go |
| +++ b/internal/status/snapshot.go |
| @@ -419,11 +419,12 @@ type NamedUnitsStatus struct { |
| } |
| // NamedUnitDelivery is one declared unit's config delivery on the last |
| -// pass: State "delivered", or "refused" with the rail's Reason. |
| +// pass: State "delivered", "refused" with the Reason the pass printed, or |
| +// "no-route" when no registry row selects the declared host. |
| type NamedUnitDelivery struct { |
| Host string `json:"host"` |
| Unit string `json:"unit"` |
| - State string `json:"state"` // "delivered" | "refused" |
| + State string `json:"state"` // "delivered" | "refused" | "no-route" (appconfig.Outcome*) |
| Reason string `json:"reason,omitempty"` |
| } |