package host import ( "sync/atomic" "github.com/carlosframework/platform/internal/instanceupdates" "github.com/carlosframework/platform/internal/registry" "github.com/carlosframework/platform/internal/release" "github.com/carlosframework/platform/internal/status" ) // buildStatesFailed is set by runAgent (noteBuildStatesInit) when the // build-state store did not start. The refusing BeforeExec hook is then // installed, so the box runs nothing it cannot record. var buildStatesFailed atomic.Bool // noteBuildStatesInit records how newBuildStates ended, for the report. A box // with no replica bucket is (nil, nil): it runs no hibernating instance, so // it has no build state to keep, and it enforces everything it has to; it // reports the protocol. func noteBuildStatesInit(err error) { buildStatesFailed.Store(err != nil) } // boxSettingsMetrics is this binary's settings protocol: it applies pin, // manual and major, and its retention protects the builds they name // (settings spec ยง4.4). It lifts the console's refusal. A box whose // build-state service failed to start reports 0: it refuses every exec and // enforces nothing (Astra I3 #1). func boxSettingsMetrics() int { if buildStatesFailed.Load() { return 0 } return status.UpdateSettingsProtocol } // fillUpdateProbes adds the update-settings report to one status tick's // probes: the protocol this box enforces and root's last decision per host. func fillUpdateProbes(p *status.Probes, reg *registry.Registry, optDir string) { p.UpdateSettings = boxSettingsMetrics() p.Updates = updatesReport(reg, optDir) } // updatesReport is root's last decision per host with settings or a decision, // for the console's list and instance page, each naming the host's tenant. The column is read as root reads // it (ParseUpdateSettingsLenient): a Control name a newer console added must // not blank the report. A column that does not parse even then still reports // root's decision (its reason is unknown). func updatesReport(reg *registry.Registry, optDir string) map[string]status.HostUpdates { routes, err := reg.List() if err != nil { return nil } out := map[string]status.HostUpdates{} for _, rt := range routes { u, perr := release.ParseUpdateSettingsLenient(rt.UpdateSettings) c, _ := release.ReadConsumed(optDir, rt.Host) // The consumed file is keyed by hostname only. One that does not name // this route's tenant (a previous occupant's, or one from before root // named the tenant) is no decision for this route: the entry carries // the route's own column and nothing that came from the file or the // build state's watermark (Astra r2 N1, ruling R22). foreign := c != nil && !c.DecidedFor(rt) if foreign { c = nil } if perr == nil && u.Default() && c == nil { continue } if perr != nil && c == nil { continue } bs, _ := release.ParseBuildStateCopy(rt.BuildState) // The tenant is the route's, by the rollout report's rule // (HostRollout.Account and .App), so the console can attribute the // report without a rollout report (Astra r1, R18). h := status.HostUpdates{Account: release.RouteAccount(rt), App: rt.App} if perr == nil { h.Major = u.Major // the route's own column: its tenant's } if foreign { // Completed and Pending read the watermark, which root takes // from the file and the build state together (Watermark); with // the file not this tenant's, neither is reported until root // decides for this route. out[rt.Host] = h continue } if c != nil { h.Desired, h.Reason, h.Detail, h.Baking = c.Desired, c.Reason, c.Detail, c.Baking // Root writes a raw parse error as the detail of reason unknown. // Members see only a build version. if h.Reason == string(release.ReasonUnknown) && !release.ValidVersion(h.Detail) { h.Detail = "" } h.Completed, h.CompletedFailed = c.Completed, c.CompletedFailed } // The build state joins the watermark only once the copy has been // read; unread, root does no request bookkeeping either. if bs.Known && bs.State != nil { if w := release.Watermark(c, bs.State); w != h.Completed { h.Completed, h.CompletedFailed = w, bs.State.CompletedFailed } } if perr == nil && u.InstallRequest > h.Completed { h.Pending = u.InstallRequest } out[rt.Host] = h } if len(out) == 0 { return nil } return out } // updatesReader is the control socket's reader (plan 3's RegistryReader, // which keeps the channel fallback, plus root's decision and the serving // build). Root's decision is added only when it names the route's tenant, // as in updatesReport (ruling R22): an instance never reads a previous // occupant's decision for its hostname. func updatesReader(reg *registry.Registry, optDir string) func(string) (instanceupdates.Report, error) { base := instanceupdates.RegistryReader(reg) return func(host string) (instanceupdates.Report, error) { rep, err := base(host) if err != nil { return rep, err } rt, err := reg.Get(host) if err != nil || rt == nil { return rep, nil // the base report stands; no decision is added } if c, _ := release.ReadConsumed(optDir, host); c.DecidedFor(*rt) { rep.Desired, rep.Reason = c.Desired, c.Reason } return rep, nil } }