package host import ( "context" "net/http" "os" "os/exec" "path/filepath" "strings" "github.com/carlosframework/platform/internal/controlsock" "github.com/carlosframework/platform/internal/hostbox" "github.com/carlosframework/platform/internal/hostclaim" execlease "github.com/carlosframework/platform/internal/hostexec" "github.com/carlosframework/platform/internal/instancectl" ) // ClaimRootWithLaunch is the claim-mode root handler with the isolated // launch route beside the existing claim verbs, and the moves held-release // route (Task 7b), which is nil, and refuses, unless CARLOS_MOVES_PHASE1 is // on (WithProductionMovesHeldRelease). type ClaimRootWithLaunch struct { Root ClaimRootHTTP Launch *ClaimLaunchRoot HeldRelease http.Handler } func (h ClaimRootWithLaunch) ServeHTTP(w http.ResponseWriter, r *http.Request) { if r.URL.Path == heldReleasePath { if h.HeldRelease == nil { http.Error(w, "refused", http.StatusServiceUnavailable) return } h.HeldRelease.ServeHTTP(w, r) return } if r.URL.Path != "/claim/launch" { h.Root.ServeHTTP(w, r) return } if h.Launch == nil { http.Error(w, "refused", http.StatusServiceUnavailable) return } h.Launch.ServeHTTP(w, r) } func (h ClaimRootWithLaunch) Start(ctx context.Context) { h.Root.Start(ctx) } func (h ClaimRootWithLaunch) RecoverPending(ctx context.Context) error { return h.Root.RecoverPending(ctx) } // WithProductionClaimLaunch adds the reservation-bearing isolated launch to // the production claim root handler (NewProductionRootClaimStop's). It // reuses that handler's peer authentication, guard and root launch journal, // and the assembly's roster identity and release root; it also puts the // isolated units in the root stop's evidence. func WithProductionClaimLaunch(ctx context.Context, handler http.Handler) (http.Handler, error) { root, ok := handler.(ClaimRootHTTP) if !ok { return nil, ErrClaimStopUnavailable } entry, _ := ctx.Value(claimEntryKey{}).(claimEntry) if entry.Assembly == nil { return nil, &ClaimDependencyError{Contracts: []string{"claim Plan 11 isolated root launch: production claim assembly"}} } a := entry.Assembly root.history = productionCreationHistory wrapped, err := claimLaunchFor(root, a.Identity.Verify, a.ManagedRoots) if err != nil { return nil, err } if a.Local == nil { return nil, ErrClaimLaunchUnavailable } wrapped.Launch.Local = a.Local wrapped.Launch.history = root.history wrapped.Launch.generations = productionRootGenerations(a.Claims, a.ManagedRoots["opt"]) wrapped.Launch.installedProgram = wrapped.Launch.rootInstalledProgram if err := bindProductionClaimUnits(ctx, &wrapped, a); err != nil { return nil, err } return wrapped, nil } func claimLaunchFor(root ClaimRootHTTP, actor func(context.Context) (hostbox.Actor, error), roots map[string]string) (ClaimRootWithLaunch, error) { coordinator := root.Stop.Coordinator if root.Launches == nil || root.Stop.Authenticate == nil || coordinator.Kernel == nil || coordinator.Guard.DataDir == "" || coordinator.Guard.Claims == nil || actor == nil || !claimLaunchAbs(roots["opt"]) || roots["data"] != "/data" { return ClaimRootWithLaunch{}, ErrClaimStopUnavailable } units := instancectl.ClaimUnits{ Systemctl: func(ctx context.Context, args ...string) (string, error) { out, err := exec.CommandContext(ctx, "systemctl", args...).CombinedOutput() return string(out), err }, CgroupRoot: "/sys/fs/cgroup", UnitRoot: "/run/systemd/system", DataRoot: roots["data"], ExeRoot: filepath.Join(controlsock.Dir, "exe"), Room: instancectl.DiskRoom(filepath.Join(roots["data"], "instances")), } launch := &ClaimLaunchRoot{SeedController: verifyFilesSeedDelegation, ValidateExecutable: claimStagedExecutable(&instancectl.Server{OptDir: roots["opt"], HostApp: rootInstallationHostApp(coordinator.Guard, coordinator.Evidence)}), DataRoot: roots["data"], Clock: execlease.SystemClock{}, Authenticate: root.Stop.Authenticate, Guard: coordinator.Guard, Actor: actor, Journal: root.Launches, Units: units, Kernel: linuxClaimLaunchKernel{OptRoot: roots["opt"], CgroupRoot: "/sys/fs/cgroup", ProcRoot: "/proc"}} if regionHAEnabled(claimLaunchEnv) { // Claim Plan 11 "Region HA integration and rollout": the root-owned // authoritative attempt reader for kind relocating (region HA ruling // 48). Off, Attempts stays empty and the kind is refused as before. launch.Attempts = map[string]claimLaunchAttemptReader{hostclaim.KindRelocating: relocationAttemptReader{DataDir: roots["data"]}} } root.Stop.Coordinator.Kernel = claimUnitStopKernel{Inner: coordinator.Kernel, Journal: root.Launches, Units: units} if root.executionRuntime != nil { root.executionRuntime.root = &root } return ClaimRootWithLaunch{Root: root, Launch: launch}, nil } // claimLaunchEnv is the process environment; tests replace it. var claimLaunchEnv = os.Getenv func claimLaunchAbs(p string) bool { return filepath.IsAbs(p) && filepath.Clean(p) == p } func claimStagedExecutable(server *instancectl.Server) func(string, string, string) error { return func(host, path, digest string) error { rel, err := filepath.Rel(filepath.Join(server.OptDir, "versions"), path) if err != nil || !filepath.IsLocal(rel) { return instancectl.ErrStagedExe } version, _, ok := strings.Cut(rel, string(filepath.Separator)) if !ok { return instancectl.ErrStagedExe } return server.ValidateStagedExe(host, instancectl.StagedExe{Path: path, Version: version, Digest: digest}) } }