| 1 | package runner |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "path/filepath" |
| 7 | "time" |
| 8 | |
| 9 | "amadan.net/oficina/equipo/internal/equipo/team" |
| 10 | "amadan.net/oficina/equipo/internal/runner/checks" |
| 11 | "amadan.net/oficina/equipo/internal/runner/forge" |
| 12 | "amadan.net/oficina/equipo/internal/runner/provider" |
| 13 | "amadan.net/oficina/equipo/internal/runner/workspace" |
| 14 | "amadan.net/oficina/equipo/internal/runnerproto" |
| 15 | ) |
| 16 | |
| 17 | func (r *Runtime) active() (RunClaim, error) { |
| 18 | r.mu.Lock() |
| 19 | defer r.mu.Unlock() |
| 20 | if r.current == nil { |
| 21 | return RunClaim{}, provider.ErrIdentity |
| 22 | } |
| 23 | return *r.current, nil |
| 24 | } |
| 25 | func claimRequest(c RunClaim, op string) runnerproto.RunnerRequest { |
| 26 | return runnerproto.RunnerRequest{Version: 2, AssignmentID: c.Assignment.ID, Scope: c.Scope, Handle: c.Handle, RequestID: attemptKey(c.Scope, c.Handle) + "/" + op} |
| 27 | } |
| 28 | func (r *Runtime) artifacts(ctx context.Context, c RunClaim) ([]team.Artifact, error) { |
| 29 | var list []team.Artifact |
| 30 | if err := r.api.Call(ctx, "evidence", claimRequest(c, "evidence"), &list); err != nil { |
| 31 | return nil, err |
| 32 | } |
| 33 | for i, item := range list { |
| 34 | q := claimRequest(c, "evidence") |
| 35 | q.ArtifactID = item.ID |
| 36 | var full team.Artifact |
| 37 | if err := r.api.Call(ctx, "evidence", q, &full); err != nil { |
| 38 | return nil, err |
| 39 | } |
| 40 | if full.ID != item.ID || full.Hash != item.Hash || team.ContentDigest(full.Payload) != item.Hash { |
| 41 | return nil, provider.ErrIdentity |
| 42 | } |
| 43 | list[i] = full |
| 44 | } |
| 45 | return list, nil |
| 46 | } |
| 47 | func (r *Runtime) wire(ctx context.Context) error { |
| 48 | w, err := NewWitness(r.journal) |
| 49 | if err != nil { |
| 50 | return err |
| 51 | } |
| 52 | bs, err := loadDirectBindings(r.cfg.AccountBindingsPath) |
| 53 | if err != nil { |
| 54 | return err |
| 55 | } |
| 56 | var manager *workspace.Manager |
| 57 | executor := func(ctx context.Context, c RunClaim, d checks.Definition, repro checks.Reproduction, deadline time.Time) (*checks.Executor, error) { |
| 58 | policy, err := r.policy(ctx, selection(c.Assignment)) |
| 59 | if err != nil { |
| 60 | return nil, err |
| 61 | } |
| 62 | baseRepo := filepath.Join(r.cfg.StateDir, "private", "project-"+runnerproto.EventHash(d.Repository)) |
| 63 | candidate := func(ctx context.Context, hash string) (team.GitCandidate, error) { |
| 64 | arts, err := r.artifacts(ctx, c) |
| 65 | if err != nil { |
| 66 | return team.GitCandidate{}, err |
| 67 | } |
| 68 | for _, art := range arts { |
| 69 | if art.Role == team.RoleCandidate { |
| 70 | var v team.GitCandidate |
| 71 | if json.Unmarshal(art.Payload, &v) == nil && checks.CandidateHash(v) == hash { |
| 72 | return v, nil |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | return team.GitCandidate{}, provider.ErrIdentity |
| 77 | } |
| 78 | recipeHash := "" |
| 79 | if d.Render != nil { |
| 80 | recipeHash = checks.Digest(marshal(d.Render)) |
| 81 | } |
| 82 | return checks.New(checks.Config{RenderRecipeHash: recipeHash, ScratchRoot: r.cfg.WorkspaceRoot, Scope: c.Scope, Handle: c.Handle, Deadline: deadline, Policy: policy, DefinitionHash: checks.Digest(marshal(d)), ReproductionHash: checks.Digest(marshal(repro)), Reproduction: func(_ context.Context, hash string) (checks.Reproduction, error) { |
| 83 | if hash != checks.Digest(marshal(repro)) { |
| 84 | return checks.Reproduction{}, provider.ErrIdentity |
| 85 | } |
| 86 | return repro, nil |
| 87 | }, ResolveCandidate: candidate, Isolation: func(ctx context.Context) (team.IsolationSnapshot, error) { |
| 88 | // Refresh the authenticated assignment, including runner-wide taint. |
| 89 | var selected runnerproto.Selection |
| 90 | if err := r.api.Call(ctx, "validate", claimRequest(c, "isolation"), &selected); err != nil { |
| 91 | return team.IsolationSnapshot{}, err |
| 92 | } |
| 93 | return selected.Isolation, nil |
| 94 | }, Export: func(ctx context.Context, commit, dest string) error { |
| 95 | if commit == d.Base { |
| 96 | return checks.ExportBase(ctx, baseRepo, commit, dest) |
| 97 | } |
| 98 | arts, err := r.artifacts(ctx, c) |
| 99 | if err != nil { |
| 100 | return err |
| 101 | } |
| 102 | for _, art := range arts { |
| 103 | if art.Role == team.RoleCandidate { |
| 104 | var v team.GitCandidate |
| 105 | if json.Unmarshal(art.Payload, &v) == nil && v.Commit == commit { |
| 106 | return manager.ExportCandidate(ctx, v, dest) |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | return provider.ErrIdentity |
| 111 | }}, r.sandbox) |
| 112 | } |
| 113 | credentials := forge.FileCredentials{Path: r.cfg.AmadanCredentialPath} |
| 114 | manager, err = workspace.New(workspace.Config{Protected: r.cfg.securityPaths(bs), PrivateRoot: filepath.Join(r.cfg.StateDir, "private"), WorkspaceRoot: r.cfg.WorkspaceRoot, GitPath: r.cfg.GitPath, Forge: credentials, Policy: r.policy, Definitions: func(ctx context.Context, target team.TargetBinding, repo string) (runnerproto.PreparedBug, error) { |
| 115 | c, err := r.active() |
| 116 | if err != nil { |
| 117 | return runnerproto.PreparedBug{}, err |
| 118 | } |
| 119 | d, err := checks.LoadBaseDefinition(ctx, repo, target.Repository, target.BaseRevision, r.cfg.ScratchRoot) |
| 120 | if err != nil { |
| 121 | return runnerproto.PreparedBug{}, err |
| 122 | } |
| 123 | e, err := executor(ctx, c, d, checks.Reproduction{}, c.AttemptDeadline) |
| 124 | if err != nil { |
| 125 | return runnerproto.PreparedBug{}, err |
| 126 | } |
| 127 | d.Baseline, err = e.CaptureBaseline(ctx, d) |
| 128 | if err != nil { |
| 129 | return runnerproto.PreparedBug{}, err |
| 130 | } |
| 131 | raw := marshal(d) |
| 132 | return runnerproto.PreparedBug{CheckDefinition: raw, CheckDefinitionHash: checks.Digest(raw), BaselineTestSetHash: d.Baseline.Hash}, nil |
| 133 | }}) |
| 134 | if err != nil { |
| 135 | return err |
| 136 | } |
| 137 | publisherFor := func(ctx context.Context, auth runnerproto.PublicationAuthorization) (*forge.Publisher, error) { |
| 138 | return forge.New(forge.Config{Root: filepath.Join(r.cfg.StateDir, "publish"), GitPath: r.cfg.GitPath, Runner: r.api.bearer.RunnerID, Incarnation: auth.Ticket.Incarnation, Clock: time.Now, Authorize: w.PublicationAuthorization, Revalidate: func(ctx context.Context, a forge.Authorization) error { |
| 139 | c, err := r.active() |
| 140 | if err != nil { |
| 141 | return err |
| 142 | } |
| 143 | if c.Scope != a.Ticket.Scope || c.Handle != a.Ticket.Handle { |
| 144 | return provider.ErrIdentity |
| 145 | } |
| 146 | return r.api.Call(ctx, "validate", claimRequest(c, "publish-validate"), nil) |
| 147 | }, Export: manager.ExportCandidate, Credential: credentials.Credential, Description: forge.NewDescriptionTransport(nil, forge.NewCommandDescriptionWriter(r.cfg.AmadanPath, r.cfg.ScratchRoot))}) |
| 148 | } |
| 149 | workflow, err := NewPlanWorkflow(BugWorkflowConfig{UploadBlob: func(ctx context.Context, c RunClaim, hash string, raw []byte) error { |
| 150 | token, err := r.api.token(c.Assignment.ID, false) |
| 151 | if err != nil { |
| 152 | return err |
| 153 | } |
| 154 | return retryAuthority(ctx, func() error { |
| 155 | return r.api.post(ctx, "blobs", runnerproto.BlobRequest{Version: 2, Scope: c.Scope, Handle: c.Handle, Digest: hash, Bytes: raw}, token, nil) |
| 156 | }) |
| 157 | }, Workspaces: manager, Launchers: r.launchers, Namespaces: r.namespaces, Launch: r.directLaunch, Witness: w, Policy: r.policy, Executor: executor, PublisherFor: publisherFor, StateDir: r.cfg.StateDir, Executable: r.cfg.Executable, InstanceID: r.cfg.InstanceID, Copy: r.cfg.Copy}) |
| 158 | if err != nil { |
| 159 | return err |
| 160 | } |
| 161 | supervisor, err := NewSupervisor(SupervisorConfig{Journal: r.journal, Namespaces: r.namespaces, API: r, Workflow: workflow, CredentialsPath: filepath.Join(r.cfg.StateDir, "credentials.json"), InstallCredentials: r.api.InstallCredentials}) |
| 162 | if err != nil { |
| 163 | return err |
| 164 | } |
| 165 | r.service = &Service{Supervisor: supervisor, Source: r, Report: r.cfg.Report, BeforeRun: func(c RunClaim) { r.mu.Lock(); r.current = &c; r.mu.Unlock() }} |
| 166 | r.service.Coordinator = r.coordinatorService() |
| 167 | return nil |
| 168 | } |
| 169 | |