package thin import ( "context" "io" "os/exec" "path/filepath" "strings" "sync" "time" "amadan.net/oficina/equipo/internal/runner" "amadan.net/oficina/equipo/internal/runner/mac" "amadan.net/oficina/equipo/internal/runner/workspace" "amadan.net/oficina/equipo/internal/wire" ) // A worker runs the steps placed on it (Mac runners spec ยง1.4): checks // of a candidate the primary relays as a pack and, on a Mac that can take // it (slice F), Engineering from the commits the primary relays. It // holds no clone and no forge credential. How a gate is confined is the gate // runner's business: Seatbelt through srt on a Mac (slice D); the test // build's fake worker runs it in a bare process group. // GateRunner runs one checks gate and proves its end. type GateRunner interface { // Sandbox is what the runner reports in hello (wire.Sandbox*). Sandbox() string // Run runs argv in dir. record persists the attempt's process record // before it runs (so a restart can verify its end); a record that // cannot be kept stops the run. Run(ctx context.Context, attempt, dir string, argv []string, limit time.Duration, record func(id string) error) gateResult // Verify reports whether a recorded attempt has ended for sure. Verify(id string) bool } // placementGuard refuses a dispatch whose needs this runner lacks // (placement_refused, nothing run) and marks its capability report stale, // so the next heartbeat says what it can do now. func placementGuard(tags func() []string, stale func(), inner StepFunc) StepFunc { return func(ctx context.Context, r stepRow, progress func(wire.StepReport)) wire.StepReport { if len(r.Needs) > 0 && !wire.Covers(tags(), r.Needs) { if stale != nil { stale() } return wire.StepReport{TaskID: r.TaskID, StepID: r.StepID, Attempt: r.Attempt, State: wire.StepFailed, Reason: "placement_refused", Termination: true, Findings: []wire.Finding{}} } return inner(ctx, r, progress) } } // Worker executes checks placed on a worker. type Worker struct { State *State // Engine supplies the git helpers (its O.Runner.GitPath and state). Engine *Engineer Gate GateRunner // PackWait bounds the wait for the relayed candidate (default 10 // minutes); MaxStep bounds the gate. PackWait time.Duration MaxStep time.Duration // StepRoot holds each attempt's tree. On a Mac it is under the // scratch root, outside the state dir the checks profile denies. StepRoot string // Find chooses the gate in a tree (findGate, and on a Mac the // xcodebuild test a project without a gate gets); reason says why // none can run (checks_unavailable). Find func(dir string) (g gate, reason string, ok bool) // Ready waits until the runner may judge (a Mac's probes). Ready func(context.Context) error // Eng is what this worker needs to take Engineering (nil: it takes // none; worker_engineering.go). Eng *WorkerEngineering // mine, in tests, stands in for Engine.Conns.Mine (the person's // connections, read from the bindings file). mine func(person string) []runner.AccountBinding // mu is held while an attempt runs, so an update swaps the binary // only between steps; marker is that attempt's EQUIPO_ATTEMPT. An // attempt waiting for Ready does not hold it. mu sync.Mutex marker string // sweepTimer runs the sweep at the next candidate keep's end. sweepMu sync.Mutex sweepTimer *time.Timer } // hold waits until no attempt runs and keeps the next one from starting // until the returned release (the Mac updater swaps the binary there). // It never waits for Ready: probes that never finish must not keep the // runner on an old release. func (w *Worker) hold() func() { w.mu.Lock() return w.mu.Unlock } func (w *Worker) root() string { return filepath.Join(w.Engine.O.Runner.StateDir, "worker") } func (w *Worker) stepsDir() string { if w.StepRoot != "" { return w.StepRoot } return filepath.Join(w.root(), "steps") } func (w *Worker) find(dir string) (gate, string, bool) { if w.Find != nil { return w.Find(dir) } g, ok := findGate(dir) return g, "", ok } // objects is the worker's object store for a repository: relayed packs // stay there, so a repository's objects move once. func (w *Worker) objects(repo string) string { return filepath.Join(w.root(), "objects", strings.NewReplacer("/", "_", ".", "_").Replace(repo)+".git") } // Exec runs one attempt placed on this worker: Engineering (Mac // runners slice F, worker_engineering.go), or checks of a relayed // candidate (worker_checks.go). func (w *Worker) Exec(ctx context.Context, r stepRow, progress func(wire.StepReport)) wire.StepReport { if r.Specialist == wire.SpecialistEngineering { return w.engineering(ctx, r, progress) } return w.checks(ctx, r, progress) } func (w *Worker) git(ctx context.Context, stdin io.Reader, args ...string) (string, error) { cmd := exec.CommandContext(ctx, w.Engine.O.Runner.GitPath, args...) cmd.Stdin = stdin for k, v := range workspace.GitEnvironment() { cmd.Env = append(cmd.Env, k+"="+v) } cmd.Env = append(cmd.Env, "HOME=/nonexistent", "EQUIPO_ATTEMPT="+w.marker) return ownOutput(cmd) } // ownOutput runs cmd as the runner's own helper (mac.StartOwn), so the // sweep at the end of a Mac turn (a probe run, say) spares it, and // returns its output, trimmed. func ownOutput(cmd *exec.Cmd) (string, error) { var out strings.Builder cmd.Stdout = &out if err := mac.StartOwn(cmd); err != nil { return "", err } err := cmd.Wait() return strings.TrimSpace(out.String()), err }