carlos / platform Public

seeds: fleet snapshot keys and protocol 4

Fleet file seeds live under apps/<sqid>/<app>/seeds/<host>/<id>/ in the
deployment bucket. ProtocolVersion becomes 4; FleetFileSeedProtocol names
what a fleet file seed needs of every box.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by paul@keymail.dev 7ff03e0829f56d854337ce366905085f82d04ec4 parent 82e237a
4 files changed, +121 −3
  • internal/seeds/floor.go +8 −1
  • internal/seeds/floor_test.go +2 −2
  • internal/seeds/seeds.go +53 −0
  • internal/seeds/seeds_test.go +58 −0
diff --git a/internal/seeds/floor.go b/internal/seeds/floor.go
index b5df3ab77..3106655d2 100644
--- a/internal/seeds/floor.go
+++ b/internal/seeds/floor.go
@@ -27,13 +27,20 @@ import (
// 2 adds fresh demo-claim seeds. The ordinary doors still accept protocol 1.
//
// 3 adds file seeds: a box that places, and rolls back, a region file seed.
-const ProtocolVersion = 3
+//
+// 4 adds fleet file seeds: a box that places, and rolls back, a file seed on
+// a customer fleet (fleet seeded create design §3.4).
+const ProtocolVersion = 4
// FileSeedProtocol is the protocol a region file seed needs of every box in
// its region (fleet seeded create plan D1). A fleet file seed needs a higher
// one, added with the fleet release.
const FileSeedProtocol = 3
+// FleetFileSeedProtocol is what a fleet file seed needs of every box in the
+// fleet (plan D1).
+const FleetFileSeedProtocol = 4
+
// RequiredProtocol is what the console requires every box in the region to
// report before it writes any seed state (spec §3.3).
const RequiredProtocol = 1
diff --git a/internal/seeds/floor_test.go b/internal/seeds/floor_test.go
index fb1d46186..c8de15fc1 100644
--- a/internal/seeds/floor_test.go
+++ b/internal/seeds/floor_test.go
@@ -120,8 +120,8 @@ func TestProtocolMeetsWhatTheConsoleRequires(t *testing.T) {
}
func TestFileSeedProtocol(t *testing.T) {
- if FileSeedProtocol != 3 || ProtocolVersion != 3 {
- t.Fatalf("FileSeedProtocol = %d, ProtocolVersion = %d; want 3 and 3", FileSeedProtocol, ProtocolVersion)
+ if FileSeedProtocol != 3 || FleetFileSeedProtocol != 4 || ProtocolVersion != 4 {
+ t.Fatalf("FileSeedProtocol = %d, FleetFileSeedProtocol = %d, ProtocolVersion = %d; want 3, 4 and 4", FileSeedProtocol, FleetFileSeedProtocol, ProtocolVersion)
}
if ProtocolVersion < FileSeedProtocol {
t.Fatal("a build that places file seeds must report at least FileSeedProtocol")
diff --git a/internal/seeds/seeds.go b/internal/seeds/seeds.go
index 834b4dbaa..3d989583e 100644
--- a/internal/seeds/seeds.go
+++ b/internal/seeds/seeds.go
@@ -138,3 +138,56 @@ func ParseSnapshotKey(key string) (host, id string, ok bool) {
}
return parts[0], parts[1], true
}
+
+// fleetPrefix is apps/<sqid>/<app>/seeds/ for valid parts, else "". It uses
+// controlname directly (release.ValidAccount and ValidApp delegate to it), as
+// release imports a great deal this package should not.
+func fleetPrefix(sqid, app string) string {
+ if !controlname.ValidAccount(sqid) || !controlname.ValidApp(app) {
+ return ""
+ }
+ return "apps/" + sqid + "/" + app + "/seeds/"
+}
+
+// FleetSnapshotsPrefix is one app's fleet seeds: apps/<sqid>/<app>/seeds/
+// (the sweep's listing). Bad parts yield seeds/!invalid/, which names nothing.
+func FleetSnapshotsPrefix(sqid, app string) string {
+ if p := fleetPrefix(sqid, app); p != "" {
+ return p
+ }
+ return HostPrefix("")
+}
+
+// FleetSnapshotPrefix is one fleet file seed's prefix in the deployment
+// bucket: apps/<sqid>/<app>/seeds/<host>/<id>/. Inputs that fail
+// ValidAccount, ValidApp or Check yield seeds/!invalid/invalid/, as
+// SnapshotPrefix does, so no key built here leaves the seed's own prefix.
+func FleetSnapshotPrefix(sqid, app, host, id string) string {
+ p := fleetPrefix(sqid, app)
+ if p == "" || Check(host, id) != nil {
+ return SnapshotPrefix("", "")
+ }
+ return p + host + "/" + id + "/"
+}
+
+// FleetSnapshotKey is one fleet seed's snapshot under FleetSnapshotPrefix.
+func FleetSnapshotKey(sqid, app, host, id string) string {
+ return FleetSnapshotPrefix(sqid, app, host, id) + snapshotName
+}
+
+// ParseFleetSnapshotKey is FleetSnapshotKey's inverse. Anything else does
+// not parse.
+func ParseFleetSnapshotKey(key string) (sqid, app, host, id string, ok bool) {
+ rest, found := strings.CutPrefix(key, "apps/")
+ if !found {
+ return "", "", "", "", false
+ }
+ parts := strings.Split(rest, "/")
+ if len(parts) != 6 || parts[2] != "seeds" || parts[5] != snapshotName {
+ return "", "", "", "", false
+ }
+ if fleetPrefix(parts[0], parts[1]) == "" || Check(parts[3], parts[4]) != nil {
+ return "", "", "", "", false
+ }
+ return parts[0], parts[1], parts[3], parts[4], true
+}
diff --git a/internal/seeds/seeds_test.go b/internal/seeds/seeds_test.go
index 42bf43dab..f7dad6cf6 100644
--- a/internal/seeds/seeds_test.go
+++ b/internal/seeds/seeds_test.go
@@ -197,3 +197,61 @@ func TestKindFileIsNotAKnownImport(t *testing.T) {
t.Fatalf("round trip = %+v, %v", got, err)
}
}
+
+func TestFleetSnapshotKeyShape(t *testing.T) {
+ id, _ := NewID()
+ base := "apps/acct/shop/seeds/canary.shop.example.com/" + id + "/"
+ if got := FleetSnapshotPrefix("acct", "shop", "canary.shop.example.com", id); got != base {
+ t.Fatalf("prefix = %q", got)
+ }
+ if got := FleetSnapshotKey("acct", "shop", "canary.shop.example.com", id); got != base+"snapshot.db" {
+ t.Fatalf("key = %q", got)
+ }
+ if got := FleetSnapshotsPrefix("acct", "shop"); got != "apps/acct/shop/seeds/" {
+ t.Fatalf("seeds prefix = %q", got)
+ }
+}
+
+func TestFleetSnapshotKeyRefusesBadParts(t *testing.T) {
+ id, _ := NewID()
+ for _, c := range [][4]string{
+ {"../x", "shop", "a.example.com", id},
+ {"ABC", "shop", "a.example.com", id},
+ {"acct", "", "a.example.com", id},
+ {"acct", "shop", "../x", id},
+ {"acct", "shop", "a.example.com", "nope"},
+ } {
+ got := FleetSnapshotPrefix(c[0], c[1], c[2], c[3])
+ if got != "seeds/!invalid/invalid/" {
+ t.Errorf("prefix%v = %q, want the invalid prefix", c, got)
+ }
+ if k := FleetSnapshotKey(c[0], c[1], c[2], c[3]); !strings.HasPrefix(k, "seeds/!invalid/") {
+ t.Errorf("key%v = %q", c, k)
+ }
+ }
+ if got := FleetSnapshotsPrefix("ABC", "shop"); got != "seeds/!invalid/" {
+ t.Errorf("seeds prefix = %q", got)
+ }
+}
+
+func TestParseFleetSnapshotKeyRoundTrip(t *testing.T) {
+ id, _ := NewID()
+ key := FleetSnapshotKey("acct", "shop", "a.example.com", id)
+ sqid, app, host, got, ok := ParseFleetSnapshotKey(key)
+ if !ok || sqid != "acct" || app != "shop" || host != "a.example.com" || got != id {
+ t.Fatalf("parse = %q %q %q %q %v", sqid, app, host, got, ok)
+ }
+ for _, bad := range []string{
+ "apps/acct/shop/seeds/a.example.com/" + id + "/other.db",
+ "apps/acct/shop/seeds/a.example.com/" + id + "/x/snapshot.db",
+ "apps/acct/shop/seeds/a.example.com/nope/snapshot.db",
+ "apps/ABC/shop/seeds/a.example.com/" + id + "/snapshot.db",
+ "apps/acct/shop/other/a.example.com/" + id + "/snapshot.db",
+ "seeds/a.example.com/" + id + "/snapshot.db",
+ "",
+ } {
+ if _, _, _, _, ok := ParseFleetSnapshotKey(bad); ok {
+ t.Errorf("parsed %q", bad)
+ }
+ }
+}