oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package thin
2
3import (
4 "context"
5 "io"
6 "os/exec"
7 "path/filepath"
8 "strings"
9 "sync"
10 "time"
11
12 "amadan.net/oficina/equipo/internal/runner"
13 "amadan.net/oficina/equipo/internal/runner/mac"
14 "amadan.net/oficina/equipo/internal/runner/workspace"
15 "amadan.net/oficina/equipo/internal/wire"
16)
17
18// A worker runs the steps placed on it (Mac runners spec §1.4): checks
19// of a candidate the primary relays as a pack and, on a Mac that can take
20// it (slice F), Engineering from the commits the primary relays. It
21// holds no clone and no forge credential. How a gate is confined is the gate
22// runner's business: Seatbelt through srt on a Mac (slice D); the test
23// build's fake worker runs it in a bare process group.
24
25// GateRunner runs one checks gate and proves its end.
26type GateRunner interface {
27 // Sandbox is what the runner reports in hello (wire.Sandbox*).
28 Sandbox() string
29 // Run runs argv in dir. record persists the attempt's process record
30 // before it runs (so a restart can verify its end); a record that
31 // cannot be kept stops the run.
32 Run(ctx context.Context, attempt, dir string, argv []string, limit time.Duration, record func(id string) error) gateResult
33 // Verify reports whether a recorded attempt has ended for sure.
34 Verify(id string) bool
35}
36
37// placementGuard refuses a dispatch whose needs this runner lacks
38// (placement_refused, nothing run) and marks its capability report stale,
39// so the next heartbeat says what it can do now.
40func placementGuard(tags func() []string, stale func(), inner StepFunc) StepFunc {
41 return func(ctx context.Context, r stepRow, progress func(wire.StepReport)) wire.StepReport {
42 if len(r.Needs) > 0 && !wire.Covers(tags(), r.Needs) {
43 if stale != nil {
44 stale()
45 }
46 return wire.StepReport{TaskID: r.TaskID, StepID: r.StepID, Attempt: r.Attempt, State: wire.StepFailed, Reason: "placement_refused", Termination: true, Findings: []wire.Finding{}}
47 }
48 return inner(ctx, r, progress)
49 }
50}
51
52// Worker executes checks placed on a worker.
53type Worker struct {
54 State *State
55 // Engine supplies the git helpers (its O.Runner.GitPath and state).
56 Engine *Engineer
57 Gate GateRunner
58 // PackWait bounds the wait for the relayed candidate (default 10
59 // minutes); MaxStep bounds the gate.
60 PackWait time.Duration
61 MaxStep time.Duration
62 // StepRoot holds each attempt's tree. On a Mac it is under the
63 // scratch root, outside the state dir the checks profile denies.
64 StepRoot string
65 // Find chooses the gate in a tree (findGate, and on a Mac the
66 // xcodebuild test a project without a gate gets); reason says why
67 // none can run (checks_unavailable).
68 Find func(dir string) (g gate, reason string, ok bool)
69 // Ready waits until the runner may judge (a Mac's probes).
70 Ready func(context.Context) error
71 // Eng is what this worker needs to take Engineering (nil: it takes
72 // none; worker_engineering.go).
73 Eng *WorkerEngineering
74 // mine, in tests, stands in for Engine.Conns.Mine (the person's
75 // connections, read from the bindings file).
76 mine func(person string) []runner.AccountBinding
77
78 // mu is held while an attempt runs, so an update swaps the binary
79 // only between steps; marker is that attempt's EQUIPO_ATTEMPT. An
80 // attempt waiting for Ready does not hold it.
81 mu sync.Mutex
82 marker string
83 // sweepTimer runs the sweep at the next candidate keep's end.
84 sweepMu sync.Mutex
85 sweepTimer *time.Timer
86}
87
88// hold waits until no attempt runs and keeps the next one from starting
89// until the returned release (the Mac updater swaps the binary there).
90// It never waits for Ready: probes that never finish must not keep the
91// runner on an old release.
92func (w *Worker) hold() func() {
93 w.mu.Lock()
94 return w.mu.Unlock
95}
96
97func (w *Worker) root() string { return filepath.Join(w.Engine.O.Runner.StateDir, "worker") }
98
99func (w *Worker) stepsDir() string {
100 if w.StepRoot != "" {
101 return w.StepRoot
102 }
103 return filepath.Join(w.root(), "steps")
104}
105
106func (w *Worker) find(dir string) (gate, string, bool) {
107 if w.Find != nil {
108 return w.Find(dir)
109 }
110 g, ok := findGate(dir)
111 return g, "", ok
112}
113
114// objects is the worker's object store for a repository: relayed packs
115// stay there, so a repository's objects move once.
116func (w *Worker) objects(repo string) string {
117 return filepath.Join(w.root(), "objects", strings.NewReplacer("/", "_", ".", "_").Replace(repo)+".git")
118}
119
120// Exec runs one attempt placed on this worker: Engineering (Mac
121// runners slice F, worker_engineering.go), or checks of a relayed
122// candidate (worker_checks.go).
123func (w *Worker) Exec(ctx context.Context, r stepRow, progress func(wire.StepReport)) wire.StepReport {
124 if r.Specialist == wire.SpecialistEngineering {
125 return w.engineering(ctx, r, progress)
126 }
127 return w.checks(ctx, r, progress)
128}
129
130func (w *Worker) git(ctx context.Context, stdin io.Reader, args ...string) (string, error) {
131 cmd := exec.CommandContext(ctx, w.Engine.O.Runner.GitPath, args...)
132 cmd.Stdin = stdin
133 for k, v := range workspace.GitEnvironment() {
134 cmd.Env = append(cmd.Env, k+"="+v)
135 }
136 cmd.Env = append(cmd.Env, "HOME=/nonexistent", "EQUIPO_ATTEMPT="+w.marker)
137 return ownOutput(cmd)
138}
139
140// ownOutput runs cmd as the runner's own helper (mac.StartOwn), so the
141// sweep at the end of a Mac turn (a probe run, say) spares it, and
142// returns its output, trimmed.
143func ownOutput(cmd *exec.Cmd) (string, error) {
144 var out strings.Builder
145 cmd.Stdout = &out
146 if err := mac.StartOwn(cmd); err != nil {
147 return "", err
148 }
149 err := cmd.Wait()
150 return strings.TrimSpace(out.String()), err
151}
152