| 1 | package seeds |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "errors" |
| 7 | "fmt" |
| 8 | "os" |
| 9 | "strings" |
| 10 | |
| 11 | "github.com/carlosframework/platform/internal/release" |
| 12 | ) |
| 13 | |
| 14 | // ProtocolVersion is the seeds protocol this build speaks. A box that |
| 15 | // reports 0 (Plans 3 to 7, and any older build) can never satisfy the |
| 16 | // console's RequiredProtocol, so every seed door stays shut for it. |
| 17 | // |
| 18 | // 1: this build places, swaps and rolls back a seed, refuses moves of an |
| 19 | // unplaced or uncommitted one, and its console sweeps and assigns. |
| 20 | // |
| 21 | // It stayed 0 through Plan 7, although that build placed, swapped and |
| 22 | // rolled back a seed (Plans 4 and 7): spec §5.8's move refusals, the |
| 23 | // seeds sweep and the legacy-routes assign door had to exist before any |
| 24 | // door opens (preflight M9). Plan 8 built them, and its Task 6 raised it |
| 25 | // to 1. Raising it opens nothing alone: a door opens only once an |
| 26 | // operator publishes the seeds floor (spec §10 step 2). |
| 27 | const ProtocolVersion = 1 |
| 28 | |
| 29 | // RequiredProtocol is what the console requires every box in the region to |
| 30 | // report before it writes any seed state (spec §3.3). |
| 31 | const RequiredProtocol = 1 |
| 32 | |
| 33 | // FloorKey is the operator-published seeds floor (spec §3.3). |
| 34 | const FloorKey = "control/seeds-protocol.json" |
| 35 | |
| 36 | // The box's local copy of the floor and its verdict on carlos.prev, beside |
| 37 | // the moves floor's files under the same floor.lock |
| 38 | // (internal/host/movesfloor.go). The two system units read the second and |
| 39 | // third by fixed path. |
| 40 | const ( |
| 41 | FloorFile = ".moves/seeds-floor.json" // under the box's DataDir |
| 42 | FloorMinReleaseFile = ".moves/seeds-floor.min-release" // under the box's DataDir |
| 43 | PrevAllowedFile = "carlos.prev.seeds-allowed" // beside carlos.prev in the opt dir |
| 44 | ) |
| 45 | |
| 46 | // Floor is the seeds floor: the protocol every box must speak, and the |
| 47 | // first release that speaks it. |
| 48 | type Floor struct { |
| 49 | Min int `json:"min"` |
| 50 | MinRelease string `json:"min_release"` |
| 51 | } |
| 52 | |
| 53 | // ReadFloor reads the published floor. Absent is (nil, nil); present but |
| 54 | // unreadable is an error, never a silent "no floor". |
| 55 | func ReadFloor(ctx context.Context, s release.Store) (*Floor, error) { |
| 56 | f, _, err := readFloorETag(ctx, s) |
| 57 | return f, err |
| 58 | } |
| 59 | |
| 60 | func readFloorETag(ctx context.Context, s release.Store) (*Floor, string, error) { |
| 61 | body, etag, err := s.Get(ctx, FloorKey) |
| 62 | if errors.Is(err, os.ErrNotExist) { |
| 63 | return nil, "", nil |
| 64 | } |
| 65 | if err != nil { |
| 66 | return nil, "", fmt.Errorf("seeds: read floor: %w", err) |
| 67 | } |
| 68 | var f Floor |
| 69 | if err := json.Unmarshal(body, &f); err != nil { |
| 70 | return nil, "", fmt.Errorf("seeds: parse floor: %w", err) |
| 71 | } |
| 72 | return &f, etag, nil |
| 73 | } |
| 74 | |
| 75 | // ReleaseAtOrAbove reports whether version is at or above minRelease, both |
| 76 | // vMAJOR.MINOR.PATCH. It fails closed: a prerelease or a malformed value is |
| 77 | // false with an error. |
| 78 | func ReleaseAtOrAbove(version, minRelease string) (bool, error) { |
| 79 | min, ok := release.ParseSemVer(strings.TrimPrefix(minRelease, "v")) |
| 80 | if !ok { |
| 81 | return false, fmt.Errorf("seeds: malformed minimum release %q", minRelease) |
| 82 | } |
| 83 | v, ok := release.ParseSemVer(strings.TrimPrefix(version, "v")) |
| 84 | if !ok { |
| 85 | return false, fmt.Errorf("seeds: unparseable release %q", version) |
| 86 | } |
| 87 | return v.Compare(min) >= 0, nil |
| 88 | } |
| 89 | |
| 90 | // FloorLowerError is PublishFloor refusing to lower the floor. |
| 91 | type FloorLowerError struct{ Current string } |
| 92 | |
| 93 | func (e *FloorLowerError) Error() string { |
| 94 | return "seeds: the published floor is already " + e.Current |
| 95 | } |
| 96 | |
| 97 | // publishAttempts bounds the CAS retry. Each lost race means another |
| 98 | // publisher moved the floor, so a few rounds are plenty. |
| 99 | const publishAttempts = 5 |
| 100 | |
| 101 | // PublishFloor writes the floor by CAS. A floor only rises: a lower |
| 102 | // min_release or a lower min is a *FloorLowerError; the same one is a no-op |
| 103 | // success. A lost CAS re-reads and decides again against the new floor, so a |
| 104 | // lower publish that raced a higher one is refused, never written over it. |
| 105 | func PublishFloor(ctx context.Context, s release.Store, f Floor) error { |
| 106 | if ok, err := ReleaseAtOrAbove(f.MinRelease, f.MinRelease); !ok || err != nil { |
| 107 | return fmt.Errorf("seeds: %q is not a release", f.MinRelease) |
| 108 | } |
| 109 | body, err := json.Marshal(f) |
| 110 | if err != nil { |
| 111 | return err |
| 112 | } |
| 113 | for range publishAttempts { |
| 114 | cur, etag, err := readFloorETag(ctx, s) |
| 115 | if err != nil { |
| 116 | return err |
| 117 | } |
| 118 | if cur != nil { |
| 119 | if ok, err := ReleaseAtOrAbove(f.MinRelease, cur.MinRelease); err != nil { |
| 120 | return fmt.Errorf("seeds: published floor is unusable: %w", err) |
| 121 | } else if !ok || f.Min < cur.Min { |
| 122 | return &FloorLowerError{Current: cur.MinRelease} |
| 123 | } |
| 124 | if *cur == f { |
| 125 | return nil |
| 126 | } |
| 127 | } |
| 128 | _, err = s.Put(ctx, FloorKey, body, etag) |
| 129 | if !errors.Is(err, release.ErrCASConflict) { |
| 130 | return err |
| 131 | } |
| 132 | } |
| 133 | return fmt.Errorf("seeds: publish floor: %w", release.ErrCASConflict) |
| 134 | } |
| 135 | |