oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1package equipo
2
3import (
4 "bytes"
5 "context"
6 "crypto/sha256"
7 "encoding/hex"
8 "errors"
9
10 "amadan.net/oficina/equipo/internal/equipo/team"
11 "gorm.io/gorm"
12)
13
14const MaxBlobBytes = 1 << 20
15const MaxAttemptBlobBytes = 128 << 20
16
17type artifactBlob struct {
18 TeamID string `gorm:"primaryKey"`
19 JobID int64 `gorm:"primaryKey;autoIncrement:false"`
20 Digest string `gorm:"primaryKey"`
21 Bytes []byte
22}
23
24func (artifactBlob) TableName() string { return "artifact_blobs" }
25
26type artifactBlobUpload struct {
27 AttemptID int64 `gorm:"primaryKey;autoIncrement:false"`
28 Digest string `gorm:"primaryKey"`
29 Size int64
30}
31
32func (artifactBlobUpload) TableName() string { return "artifact_blob_uploads" }
33func blobDigest(b []byte) string { sum := sha256.Sum256(b); return hex.EncodeToString(sum[:]) }
34func (s *Store) PutBlob(ctx context.Context, p RunnerPrincipal, scope team.Scope, h team.Handle, digest string, data []byte) error {
35 if len(data) == 0 || len(data) > MaxBlobBytes || blobDigest(data) != digest {
36 return ErrInvalid
37 }
38 if s.execution == nil {
39 return ErrUnavailable
40 }
41 return s.execution.atomic(ctx, func(ctx context.Context, tx *gorm.DB) error {
42 var a runnerAssignment
43 if err := tx.First(&a, "team_id=? AND job_id=? AND attempt_id=? AND fence=? AND runner_id=?", scope.Account, scope.Job, h.Attempt, h.Fence, h.Runner).Error; err != nil {
44 return storeError(err)
45 }
46 if _, err := s.execution.assignmentCurrent(ctx, tx, p, a); err != nil {
47 return err
48 }
49 if a.State != "claimed" || a.Kind != "step" {
50 return ErrForbidden
51 }
52 if err := s.execution.Core.ValidateHandle(ctx, scope, h); err != nil {
53 return err
54 }
55 var old artifactBlob
56 err := tx.First(&old, "team_id=? AND job_id=? AND digest=?", scope.Account, scope.Job, digest).Error
57 if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
58 return storeError(err)
59 }
60 exists := err == nil
61 if exists && (!bytes.Equal(old.Bytes, data) || blobDigest(old.Bytes) != digest) {
62 return ErrConflict
63 }
64 var upload artifactBlobUpload
65 err = tx.First(&upload, "attempt_id=? AND digest=?", h.Attempt, digest).Error
66 if err == nil {
67 if !exists || upload.Size != int64(len(data)) {
68 return ErrConflict
69 }
70 return nil
71 }
72 if !errors.Is(err, gorm.ErrRecordNotFound) {
73 return storeError(err)
74 }
75 var total int64
76 if err := tx.Model(&artifactBlobUpload{}).Where("attempt_id=?", h.Attempt).Select("COALESCE(SUM(size),0)").Scan(&total).Error; err != nil {
77 return storeError(err)
78 }
79 if total < 0 || total > MaxAttemptBlobBytes-int64(len(data)) {
80 return ErrInvalid
81 }
82 if !exists {
83 if err := tx.Create(&artifactBlob{TeamID: string(scope.Account), JobID: int64(scope.Job), Digest: digest, Bytes: append([]byte(nil), data...)}).Error; err != nil {
84 return storeError(err)
85 }
86 }
87 if err := tx.Create(&artifactBlobUpload{AttemptID: int64(h.Attempt), Digest: digest, Size: int64(len(data))}).Error; err != nil {
88 return storeError(err)
89 }
90 return appendAudit(tx, Actor{Address: p.Person}, "artifact.blob", digest)
91 })
92}
93func (s *Store) Blob(ctx context.Context, p RunnerPrincipal, scope team.Scope, digest string) ([]byte, error) {
94 if s.execution == nil {
95 return nil, ErrUnavailable
96 }
97 var result []byte
98 err := s.execution.atomic(ctx, func(ctx context.Context, tx *gorm.DB) error {
99 if _, err := runnerPrincipalAt(tx, p, s.runnerClock.Now().UTC()); err != nil {
100 return err
101 }
102 var a runnerAssignment
103 // Only a live claimed step can read; ordering is stable across query plans.
104 err := tx.Where("team_id=? AND job_id=? AND runner_id=? AND person=? AND state=? AND kind=?", scope.Account, scope.Job, p.RunnerID, p.Person, "claimed", "step").Order("attempt_id DESC, fence DESC, revision DESC, id DESC").First(&a).Error
105 if errors.Is(err, gorm.ErrRecordNotFound) {
106 return ErrForbidden
107 }
108 if err != nil {
109 return storeError(err)
110 }
111 if _, err := s.execution.assignmentCurrent(ctx, tx, p, a); err != nil {
112 return err
113 }
114 h := team.Handle{Attempt: team.AttemptID(a.AttemptID), Fence: a.Fence, Runner: a.RunnerID}
115 if err := s.execution.Core.ValidateHandle(ctx, scope, h); err != nil {
116 return err
117 }
118 var row artifactBlob
119 if err := tx.First(&row, "team_id=? AND job_id=? AND digest=?", scope.Account, scope.Job, digest).Error; err != nil {
120 return storeError(err)
121 }
122 if len(row.Bytes) == 0 || len(row.Bytes) > MaxBlobBytes || blobDigest(row.Bytes) != digest {
123 return ErrConflict
124 }
125 result = append([]byte(nil), row.Bytes...)
126 return nil
127 })
128 return result, err
129}
130