package runner import ( "context" "encoding/json" "path/filepath" "time" "amadan.net/oficina/equipo/internal/equipo/team" "amadan.net/oficina/equipo/internal/runner/checks" "amadan.net/oficina/equipo/internal/runner/forge" "amadan.net/oficina/equipo/internal/runner/provider" "amadan.net/oficina/equipo/internal/runner/workspace" "amadan.net/oficina/equipo/internal/runnerproto" ) func (r *Runtime) active() (RunClaim, error) { r.mu.Lock() defer r.mu.Unlock() if r.current == nil { return RunClaim{}, provider.ErrIdentity } return *r.current, nil } func claimRequest(c RunClaim, op string) runnerproto.RunnerRequest { return runnerproto.RunnerRequest{Version: 2, AssignmentID: c.Assignment.ID, Scope: c.Scope, Handle: c.Handle, RequestID: attemptKey(c.Scope, c.Handle) + "/" + op} } func (r *Runtime) artifacts(ctx context.Context, c RunClaim) ([]team.Artifact, error) { var list []team.Artifact if err := r.api.Call(ctx, "evidence", claimRequest(c, "evidence"), &list); err != nil { return nil, err } for i, item := range list { q := claimRequest(c, "evidence") q.ArtifactID = item.ID var full team.Artifact if err := r.api.Call(ctx, "evidence", q, &full); err != nil { return nil, err } if full.ID != item.ID || full.Hash != item.Hash || team.ContentDigest(full.Payload) != item.Hash { return nil, provider.ErrIdentity } list[i] = full } return list, nil } func (r *Runtime) wire(ctx context.Context) error { w, err := NewWitness(r.journal) if err != nil { return err } bs, err := loadDirectBindings(r.cfg.AccountBindingsPath) if err != nil { return err } var manager *workspace.Manager executor := func(ctx context.Context, c RunClaim, d checks.Definition, repro checks.Reproduction, deadline time.Time) (*checks.Executor, error) { policy, err := r.policy(ctx, selection(c.Assignment)) if err != nil { return nil, err } baseRepo := filepath.Join(r.cfg.StateDir, "private", "project-"+runnerproto.EventHash(d.Repository)) candidate := func(ctx context.Context, hash string) (team.GitCandidate, error) { arts, err := r.artifacts(ctx, c) if err != nil { return team.GitCandidate{}, err } for _, art := range arts { if art.Role == team.RoleCandidate { var v team.GitCandidate if json.Unmarshal(art.Payload, &v) == nil && checks.CandidateHash(v) == hash { return v, nil } } } return team.GitCandidate{}, provider.ErrIdentity } recipeHash := "" if d.Render != nil { recipeHash = checks.Digest(marshal(d.Render)) } 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) { if hash != checks.Digest(marshal(repro)) { return checks.Reproduction{}, provider.ErrIdentity } return repro, nil }, ResolveCandidate: candidate, Isolation: func(ctx context.Context) (team.IsolationSnapshot, error) { // Refresh the authenticated assignment, including runner-wide taint. var selected runnerproto.Selection if err := r.api.Call(ctx, "validate", claimRequest(c, "isolation"), &selected); err != nil { return team.IsolationSnapshot{}, err } return selected.Isolation, nil }, Export: func(ctx context.Context, commit, dest string) error { if commit == d.Base { return checks.ExportBase(ctx, baseRepo, commit, dest) } arts, err := r.artifacts(ctx, c) if err != nil { return err } for _, art := range arts { if art.Role == team.RoleCandidate { var v team.GitCandidate if json.Unmarshal(art.Payload, &v) == nil && v.Commit == commit { return manager.ExportCandidate(ctx, v, dest) } } } return provider.ErrIdentity }}, r.sandbox) } credentials := forge.FileCredentials{Path: r.cfg.AmadanCredentialPath} 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) { c, err := r.active() if err != nil { return runnerproto.PreparedBug{}, err } d, err := checks.LoadBaseDefinition(ctx, repo, target.Repository, target.BaseRevision, r.cfg.ScratchRoot) if err != nil { return runnerproto.PreparedBug{}, err } e, err := executor(ctx, c, d, checks.Reproduction{}, c.AttemptDeadline) if err != nil { return runnerproto.PreparedBug{}, err } d.Baseline, err = e.CaptureBaseline(ctx, d) if err != nil { return runnerproto.PreparedBug{}, err } raw := marshal(d) return runnerproto.PreparedBug{CheckDefinition: raw, CheckDefinitionHash: checks.Digest(raw), BaselineTestSetHash: d.Baseline.Hash}, nil }}) if err != nil { return err } publisherFor := func(ctx context.Context, auth runnerproto.PublicationAuthorization) (*forge.Publisher, error) { 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 { c, err := r.active() if err != nil { return err } if c.Scope != a.Ticket.Scope || c.Handle != a.Ticket.Handle { return provider.ErrIdentity } return r.api.Call(ctx, "validate", claimRequest(c, "publish-validate"), nil) }, Export: manager.ExportCandidate, Credential: credentials.Credential, Description: forge.NewDescriptionTransport(nil, forge.NewCommandDescriptionWriter(r.cfg.AmadanPath, r.cfg.ScratchRoot))}) } workflow, err := NewPlanWorkflow(BugWorkflowConfig{UploadBlob: func(ctx context.Context, c RunClaim, hash string, raw []byte) error { token, err := r.api.token(c.Assignment.ID, false) if err != nil { return err } return retryAuthority(ctx, func() error { return r.api.post(ctx, "blobs", runnerproto.BlobRequest{Version: 2, Scope: c.Scope, Handle: c.Handle, Digest: hash, Bytes: raw}, token, nil) }) }, 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}) if err != nil { return err } 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}) if err != nil { return err } r.service = &Service{Supervisor: supervisor, Source: r, Report: r.cfg.Report, BeforeRun: func(c RunClaim) { r.mu.Lock(); r.current = &c; r.mu.Unlock() }} r.service.Coordinator = r.coordinatorService() return nil }