| 1 | package runner |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "errors" |
| 7 | "time" |
| 8 | |
| 9 | "amadan.net/oficina/equipo/internal/runner/containment" |
| 10 | "amadan.net/oficina/equipo/internal/runner/provider" |
| 11 | "amadan.net/oficina/equipo/internal/runnerproto" |
| 12 | ) |
| 13 | |
| 14 | type namespaceAdmissionKey struct{} |
| 15 | type namespaceState struct { |
| 16 | LaunchAcknowledged bool |
| 17 | Owner string |
| 18 | Record containment.Record |
| 19 | Proof *containment.Proof |
| 20 | } |
| 21 | |
| 22 | func (s *Supervisor) namespaceContext(ctx context.Context, r *attemptState) context.Context { |
| 23 | owner := attemptKey(r.Claim.Scope, r.Claim.Handle) |
| 24 | return context.WithValue(ctx, namespaceAdmissionKey{}, func(ctx context.Context, record containment.Record) error { |
| 25 | if err := s.journal.saveState(ctx, "namespace", record.ID, namespaceState{Owner: owner, Record: record}); err != nil { |
| 26 | return err |
| 27 | } |
| 28 | if record.HostPID == 0 { |
| 29 | return nil |
| 30 | } |
| 31 | n := runnerproto.NamespaceLaunch{ID: record.ID, AttemptKey: record.AttemptKey, OwnerKey: owner, BootID: record.BootID, NamespaceID: record.NamespaceID, ManifestHash: record.ManifestHash, PolicyHash: record.PolicyHash, HostPID: record.HostPID, StartTicks: record.StartTicks} |
| 32 | if err := s.namespaceEvent(ctx, r, "namespace.launch", record.AttemptKey, record.ID, n); err != nil { |
| 33 | return err |
| 34 | } |
| 35 | return s.journal.saveState(ctx, "namespace", record.ID, namespaceState{Owner: owner, Record: record, LaunchAcknowledged: true}) |
| 36 | }) |
| 37 | } |
| 38 | |
| 39 | func (s *Supervisor) namespaceEvent(ctx context.Context, r *attemptState, kind, agent, session string, payload any) error { |
| 40 | e := Event{Version: 2, ID: runnerproto.EventHash([]string{kind, agent, session}), TaskID: r.Claim.Assignment.TaskID, AssignmentID: r.Claim.Assignment.ID, Scope: r.Claim.Scope, Handle: r.Claim.Handle, ServerID: "direct", AgentID: agent, SessionID: session, Kind: kind, Payload: marshal(payload), SourceTime: time.Now().UTC(), Isolation: r.Claim.Assignment.Selection.Isolation} |
| 41 | var old []byte |
| 42 | if err := s.journal.db.QueryRowContext(ctx, "SELECT body FROM journal_events WHERE event_key=?", e.DedupKey()).Scan(&old); err == nil { |
| 43 | var saved Event |
| 44 | if json.Unmarshal(old, &saved) != nil { |
| 45 | return ErrJournal |
| 46 | } |
| 47 | e.SourceTime = saved.SourceTime |
| 48 | } |
| 49 | pump := EventPump{Journal: s.journal, Contain: func(context.Context) error { return provider.ErrContainment }} |
| 50 | if err := pump.Append(ctx, e, nil); err != nil { |
| 51 | return err |
| 52 | } |
| 53 | q := r.request(e.ID) |
| 54 | q.RequestID = e.ID |
| 55 | q.Event, _ = runnerproto.Marshal(e) |
| 56 | if err := s.api.Call(ctx, "events", q, nil); err != nil { |
| 57 | return err |
| 58 | } |
| 59 | return s.journal.Ack(ctx, e.ID) |
| 60 | } |
| 61 | |
| 62 | func (s *Supervisor) cleanupDirect(ctx context.Context, r *attemptState) (ContainmentProof, error) { |
| 63 | proof := ContainmentProof{Version: 2, Scope: r.Claim.Scope, Handle: r.Claim.Handle, CheckedAt: time.Now().UTC()} |
| 64 | if r.Phase == "contained" && r.Proof != nil { |
| 65 | return *r.Proof, nil |
| 66 | } |
| 67 | if r.Rotation != nil { |
| 68 | return s.finishRotation(ctx, r) |
| 69 | } |
| 70 | if r.Phase == "verified" && r.Proof != nil { |
| 71 | return s.reportContainment(ctx, r) |
| 72 | } |
| 73 | if s.namespaces == nil { |
| 74 | return proof, provider.ErrContainment |
| 75 | } |
| 76 | s.mu.Lock() |
| 77 | proxy := s.activeProxy |
| 78 | s.mu.Unlock() |
| 79 | if proxy != nil { |
| 80 | if err := proxy.Close(); err != nil { |
| 81 | return proof, err |
| 82 | } |
| 83 | } |
| 84 | var states []namespaceState |
| 85 | if err := s.journal.readStates(ctx, "namespace", &states); err != nil { |
| 86 | return proof, err |
| 87 | } |
| 88 | var failures error |
| 89 | checkWorkspaces := map[string]bool{} |
| 90 | for _, n := range states { |
| 91 | if n.Owner != attemptKey(r.Claim.Scope, r.Claim.Handle) { |
| 92 | continue |
| 93 | } |
| 94 | p, err := s.namespaces.Stop(ctx, n.Record.ID) |
| 95 | if err != nil { |
| 96 | p, err = s.namespaces.Recover(ctx, n.Record) |
| 97 | } |
| 98 | if err != nil || !p.Verified { |
| 99 | failures = errors.Join(failures, provider.ErrContainment, err) |
| 100 | continue |
| 101 | } |
| 102 | if err = s.journal.validateDirectRecord(ctx, p.Record); err != nil { |
| 103 | failures = errors.Join(failures, err) |
| 104 | continue |
| 105 | } |
| 106 | n.Proof = &p |
| 107 | if err = s.journal.saveState(ctx, "namespace", n.Record.ID, n); err != nil { |
| 108 | failures = errors.Join(failures, err) |
| 109 | continue |
| 110 | } |
| 111 | if _, err = s.journal.db.ExecContext(ctx, "UPDATE direct_attempts SET contained=1 WHERE attempt=?", n.Record.AttemptKey); err != nil { |
| 112 | failures = errors.Join(failures, err) |
| 113 | continue |
| 114 | } |
| 115 | if s.workflow != nil { |
| 116 | var raw []byte |
| 117 | var launch provider.Launch |
| 118 | if err = s.journal.db.QueryRowContext(ctx, "SELECT launch FROM direct_attempts WHERE attempt=?", n.Record.AttemptKey).Scan(&raw); err != nil { |
| 119 | failures = errors.Join(failures, err) |
| 120 | continue |
| 121 | } |
| 122 | if json.Unmarshal(raw, &launch) != nil { |
| 123 | failures = errors.Join(failures, ErrJournal) |
| 124 | continue |
| 125 | } |
| 126 | if launch.Policy.IsCheck() { |
| 127 | checkWorkspaces[launch.Workspace] = true |
| 128 | } |
| 129 | } |
| 130 | if n.Record.HostPID != 0 { |
| 131 | exit := runnerproto.NamespaceExit{ID: p.Record.ID, AttemptKey: p.Record.AttemptKey, BootID: p.Record.BootID, NamespaceID: p.Record.NamespaceID, ManifestHash: p.Record.ManifestHash, HostPID: p.Record.HostPID, StartTicks: p.Record.StartTicks, ExitCode: p.Exit.Code, Signal: p.Exit.Signal, ObservedAt: p.Exit.ObservedAt} |
| 132 | if n.LaunchAcknowledged { |
| 133 | proof.Namespaces = append(proof.Namespaces, exit) |
| 134 | } else { |
| 135 | proof.Unstarted = append(proof.Unstarted, exit) |
| 136 | id := runnerproto.EventHash([]string{"namespace.launch", n.Record.AttemptKey, n.Record.ID}) |
| 137 | if err = s.journal.deadLetterLaunch(ctx, id); err != nil { |
| 138 | failures = errors.Join(failures, err) |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | if failures != nil { |
| 144 | return proof, failures |
| 145 | } |
| 146 | for work := range checkWorkspaces { |
| 147 | if err := s.workflow.cfg.Workspaces.RemoveCheck(work); err != nil { |
| 148 | return proof, err |
| 149 | } |
| 150 | } |
| 151 | if s.activeRelease != nil { |
| 152 | if err := s.activeRelease(); err != nil { |
| 153 | return proof, err |
| 154 | } |
| 155 | s.activeRelease = nil |
| 156 | } |
| 157 | if s.workflow != nil && r.WorkspacePath != "" { |
| 158 | if err := s.workflow.cfg.Workspaces.Remove(ctx, r.Claim.Assignment, r.WorkspacePath); err != nil { |
| 159 | return proof, err |
| 160 | } |
| 161 | } |
| 162 | proof.CheckedAt = time.Now().UTC() |
| 163 | r.Proof = &proof |
| 164 | r.Phase = "verified" |
| 165 | if err := s.save(ctx, r); err != nil { |
| 166 | return proof, err |
| 167 | } |
| 168 | return s.reportContainment(ctx, r) |
| 169 | } |
| 170 | |
| 171 | func (j *Journal) deadLetterLaunch(ctx context.Context, id string) error { |
| 172 | tx, err := j.db.BeginTx(ctx, nil) |
| 173 | if err != nil { |
| 174 | return err |
| 175 | } |
| 176 | defer tx.Rollback() |
| 177 | if _, err = tx.ExecContext(ctx, "INSERT OR IGNORE INTO journal_supervisor(kind,state_key,body) SELECT 'unstarted-launch',id,body FROM journal_events WHERE id=?", id); err != nil { |
| 178 | return err |
| 179 | } |
| 180 | if _, err = tx.ExecContext(ctx, "UPDATE journal_events SET acked=1 WHERE id=?", id); err != nil { |
| 181 | return err |
| 182 | } |
| 183 | return tx.Commit() |
| 184 | } |
| 185 | |