package activator import ( "context" "errors" "fmt" "github.com/carlosframework/platform/internal/instancectl" "github.com/carlosframework/platform/internal/registry" "github.com/carlosframework/platform/internal/release" ) // identityBackend is the exec backend's identity-pinned spawn and seed // (settings spec ยง4.2, Astra I1 #5). A cold wake captures ONE staged // identity (LiveIdentity), records it, and then seeds and spawns exactly // that file by path, verified against its digest. Neither call execs // live/, so root repointing the link between the record and the exec // cannot make a different build touch the instance's data. type identityBackend interface { // LiveIdentity materializes host's live link if it is missing (the // Materialize hook and its root-stage relay), then resolves it ONCE // into the file it names and that file's digest. Version is "" for a // link outside the version store; the caller decides what that means. LiveIdentity(ctx context.Context, rt registry.Route) (release.StagedIdentity, error) // StartGenExe is StartGen (generation n, perGen choosing the socket) // with the executable named: id.StagedPath, verified against id.Digest // first. A copy that changed is a failed start, never a fallback to the // link. The Gen it returns carries id's build, path, digest and demand, // so a later sidecar binds the same file. StartGenExe(ctx context.Context, rt registry.Route, n uint64, perGen bool, id release.StagedIdentity) (Proc, Gen, error) // SeedExe is Seed with the executable named, verified the same way. SeedExe(ctx context.Context, rt registry.Route, id release.StagedIdentity) error } var _ identityBackend = (*ExecBackend)(nil) // identityPicker is a composed backend that answers per route. type identityPicker interface { identityFor(rt registry.Route) identityBackend } // identityFor is the backend that can pin an executable for rt, or nil // (rt then keeps today's Start and Seed, and nothing is recorded for it). // Production's SplitBackend serves unit rows too, so a composed backend // answers per route, never by a type assertion on itself alone. func identityFor(b Backend, rt registry.Route) identityBackend { if p, ok := b.(identityPicker); ok { return p.identityFor(rt) } ib, _ := b.(identityBackend) return ib } // identityFor is the exec half for an exec row and nil for a unit row: // systemd execs a unit row's live link itself, so nothing here can pin its // file. SplitBackend does not implement identityBackend itself. The exec // half answers by the same rule (identityFor), so a half that is itself // composed answers per route too. func (b *SplitBackend) identityFor(rt registry.Route) identityBackend { if rt.Backing == registry.BackingUnit { return nil } return identityFor(b.pick(rt), rt) } // buildIdentifier names build's staged executable of rt's app on this box, // hashed now: VersionPath(OptDir, RouteAccount(rt), build, rt.App) and its // sha256, refused for an invalid version or an unreadable file. A legacy // sidecar whose live link names a build the declined gate refuses runs its // instance's own build through it instead (Service.startSidecar). type buildIdentifier interface { BuildIdentity(rt registry.Route, build string) (release.StagedIdentity, error) } var ( _ buildIdentifier = (*ExecBackend)(nil) _ buildIdentifier = (*SplitBackend)(nil) ) // errUnitNoBuildIdentity refuses BuildIdentity for a unit row: systemd // execs its live link itself, so no staged file can be named for it. var errUnitNoBuildIdentity = errors.New("activator: a unit-backed row runs its live link, so no staged build can be named for it") // BuildIdentity forwards to the exec half for exec rows. A unit row, or an // exec half that cannot name a build, refuses. func (b *SplitBackend) BuildIdentity(rt registry.Route, build string) (release.StagedIdentity, error) { if rt.Backing == registry.BackingUnit { return release.StagedIdentity{}, errUnitNoBuildIdentity } bi, ok := b.Exec.(buildIdentifier) if !ok { return release.StagedIdentity{}, fmt.Errorf("%s: the exec backend cannot name a staged build", rt.Host) } return bi.BuildIdentity(rt, build) } // stagedExeOf is id as root's control channel names an executable. func stagedExeOf(id release.StagedIdentity) *instancectl.StagedExe { return &instancectl.StagedExe{Path: id.StagedPath, Version: id.Version, Digest: id.Digest} }