carlos / platform Public

Clone
git clone https://amadan.net/carlos/platform

Plain git — no account needed to clone.

Download

Download this file

1package plans
2
3import (
4 "context"
5 "encoding/json"
6 "errors"
7 "fmt"
8 "os"
9 "time"
10
11 "github.com/carlosframework/platform/internal/release"
12)
13
14// MarkerSchema is the deployment marker's wire version.
15const MarkerSchema = 1
16
17// Marker says whether plans are on for the deployment (plans 2). The console
18// writes it once at start; boxes read it on every pass and read no limits
19// record while it is absent or disabled, so a deployment without a catalog
20// costs a box one small read per pass and changes nothing.
21type Marker struct {
22 Schema int `json:"schema"`
23 Enabled bool `json:"enabled"`
24 CatalogID string `json:"catalog_id,omitempty"`
25 UpdatedAt time.Time `json:"updated_at"`
26}
27
28// MarkerKey is where the deployment marker lives.
29func MarkerKey() string { return "control/plans.json" }
30
31// GetMarker reads the marker and its etag. Absent is nil, "", nil. An unknown
32// schema is an error, so a reader keeps whatever it last knew.
33func GetMarker(ctx context.Context, s release.Store) (*Marker, string, error) {
34 raw, etag, err := s.Get(ctx, MarkerKey())
35 if err != nil {
36 if errors.Is(err, os.ErrNotExist) {
37 return nil, "", nil
38 }
39 return nil, "", err
40 }
41 var m Marker
42 if err := json.Unmarshal(raw, &m); err != nil {
43 return nil, "", fmt.Errorf("plans: %s: %w", MarkerKey(), err)
44 }
45 if m.Schema != MarkerSchema {
46 return nil, "", fmt.Errorf("plans: %s: schema %d, want %d", MarkerKey(), m.Schema, MarkerSchema)
47 }
48 return &m, etag, nil
49}
50
51// PutMarker CAS-writes the marker: ifMatch "" is create-only, otherwise
52// replace-only. Schema is stamped here.
53func PutMarker(ctx context.Context, s release.Store, m *Marker, ifMatch string) (string, error) {
54 m.Schema = MarkerSchema
55 body, err := json.MarshalIndent(m, "", " ")
56 if err != nil {
57 return "", err
58 }
59 return s.Put(ctx, MarkerKey(), append(body, '\n'), ifMatch)
60}
61