From f0a6c3f2ae8695ac1dadceff4aa74ca55c64067c Mon Sep 17 00:00:00 2001 From: Sharang Parnerkar <30073382+mighty840@users.noreply.github.com> Date: Mon, 5 Oct 2026 19:41:51 +0200 Subject: [PATCH] feat(services): build runners INF-CI-* and managed service kind ci-runner ERP items INF-CI-CLD-STD/BUS/ENT (Monat, 149/290/590 EUR) and INF-CI-OPR-STD/BUS/ENT (Jahr, 2900/5900/11900 EUR), priced per runner: a dedicated build machine per customer, never shared (2 vCPU/4 GB, 4 vCPU/8 GB, 8 vCPU/16 GB). Migration 0013 adds the managed service kind ci-runner (SKU ^INF-CI-(CLD|OPR)-(STD|BUS|ENT)$). A runner's url is the Git instance it serves, so the (tenant, kind, url) uniqueness now holds for kind git only (partial unique index); several runners may serve one instance. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01DmXxNULoSNPvjF7buKipsB --- internal/erpsetup/catalog.go | 11 ++++- internal/erpsetup/erpsetup_test.go | 11 +++-- internal/server/services.go | 8 +++- internal/server/services_test.go | 47 +++++++++++++++++++ internal/store/memory_services.go | 8 +++- internal/store/types.go | 14 ++++-- .../0013_managed_services_ci_runner.down.sql | 7 +++ .../0013_managed_services_ci_runner.up.sql | 12 +++++ migrations/migrations_test.go | 7 +++ openapi.yaml | 14 +++--- 10 files changed, 116 insertions(+), 23 deletions(-) create mode 100644 migrations/0013_managed_services_ci_runner.down.sql create mode 100644 migrations/0013_managed_services_ci_runner.up.sql diff --git a/internal/erpsetup/catalog.go b/internal/erpsetup/catalog.go index 8098f43..d727a21 100644 --- a/internal/erpsetup/catalog.go +++ b/internal/erpsetup/catalog.go @@ -18,8 +18,9 @@ const priceList = "Standard Selling" // skus lists every item a sales order may carry. The registry unlocks a // product for the product SKUs; the platform fee (PLT-CORE-*) is billed but -// unlocks nothing on its own, and so is Managed Git (INF-GIT-*) until the -// registry has a `git` product (instances are provisioned by hand, v0). +// unlocks nothing on its own, and so are Managed Git (INF-GIT-*) and its build +// runners (INF-CI-*) until the registry has a `git` product (instances and +// runners are provisioned by hand, v0). Runner prices are per runner. func skus() []sku { return []sku{ {"PLT-CORE-CLD-STD", "BreakPilot Plattform – Starter", "Monat", "Arbeitsbereich mit Anmeldung, Team und Rechnungen: bis 25 Nutzer, bis 3 Produkte, Standard-Support.", 290}, @@ -50,6 +51,12 @@ func skus() []sku { {"INF-GIT-OPR-STD", "Managed Git On-Prem – Standard", "Jahr", "Gitea im eigenen Netz, kein Quellcode verlässt das Haus; Installation, Patches und Überwachung durch BreakPilot; Jahreslizenz.", 4900}, {"INF-GIT-OPR-BUS", "Managed Git On-Prem – Business", "Jahr", "Gitea im eigenen Netz für mittelgroße Teams, schnellerer Patch-Rhythmus; Jahreslizenz.", 9900}, {"INF-GIT-OPR-ENT", "Managed Git On-Prem – Enterprise", "Jahr", "Gitea im eigenen Netz mit Hochverfügbarkeit und Enterprise-SLA; Jahreslizenz.", 19900}, + {"INF-CI-CLD-STD", "Build-Runner Cloud – Standard", "Monat", "Eigene Build-Maschine für Ihr Unternehmen zu Managed Git, betrieben von BreakPilot in Deutschland und nie mit anderen Kunden geteilt (2 vCPU / 4 GB); Preis je Runner.", 149}, + {"INF-CI-CLD-BUS", "Build-Runner Cloud – Business", "Monat", "Eigene Build-Maschine für größere Builds und parallele Jobs, nie geteilt (4 vCPU / 8 GB); Preis je Runner.", 290}, + {"INF-CI-CLD-ENT", "Build-Runner Cloud – Enterprise", "Monat", "Eigene Build-Maschine für umfangreiche Builds, nie geteilt (8 vCPU / 16 GB), Enterprise-SLA; Preis je Runner.", 590}, + {"INF-CI-OPR-STD", "Build-Runner On-Prem – Standard", "Jahr", "Build-Maschine im eigenen Netz, ausschließlich für Ihre Builds, kein Quellcode verlässt das Haus (2 vCPU / 4 GB); Einrichtung, Patches und Überwachung durch BreakPilot; Jahreslizenz je Runner.", 2900}, + {"INF-CI-OPR-BUS", "Build-Runner On-Prem – Business", "Jahr", "Build-Maschine im eigenen Netz für größere Builds (4 vCPU / 8 GB), schnellerer Patch-Rhythmus; Jahreslizenz je Runner.", 5900}, + {"INF-CI-OPR-ENT", "Build-Runner On-Prem – Enterprise", "Jahr", "Build-Maschine im eigenen Netz für umfangreiche Builds (8 vCPU / 16 GB) mit Enterprise-SLA; Jahreslizenz je Runner.", 11900}, } } diff --git a/internal/erpsetup/erpsetup_test.go b/internal/erpsetup/erpsetup_test.go index fe060aa..d8a983d 100644 --- a/internal/erpsetup/erpsetup_test.go +++ b/internal/erpsetup/erpsetup_test.go @@ -136,8 +136,8 @@ func TestRunCreatesThenIsIdempotent(t *testing.T) { if err != nil { t.Fatal(err) } - if len(steps) != 96 || f.posts != 96 { - t.Fatalf("first run: %d steps, %d creates; want 96/96: %+v", len(steps), f.posts, steps) + if len(steps) != 108 || f.posts != 108 { + t.Fatalf("first run: %d steps, %d creates; want 108/108: %+v", len(steps), f.posts, steps) } for _, s := range steps { if s.Result != "created" { @@ -153,8 +153,8 @@ func TestRunCreatesThenIsIdempotent(t *testing.T) { if err != nil { t.Fatal(err) } - if f.posts != 96 { - t.Fatalf("second run created %d more docs", f.posts-96) + if f.posts != 108 { + t.Fatalf("second run created %d more docs", f.posts-108) } for _, s := range steps { if !strings.HasPrefix(s.Result, "exists") { @@ -249,7 +249,7 @@ func TestWebhookDocIsNamed(t *testing.T) { } // Every SKU the registry's catalogue sells exists as an item, with a unit that -// exists too, and the platform fee and Managed Git are there for invoicing. +// exists too, and the platform fee, Managed Git and its build runners are there for invoicing. func TestCatalogueCoversTheRegistrySKUs(t *testing.T) { want := []string{ "CRA-CSA-CLD-STD", "CRA-CSA-CLD-BUS", "CRA-CSA-CLD-ENT", "CRA-CSA-CLD-SCAN", @@ -258,6 +258,7 @@ func TestCatalogueCoversTheRegistrySKUs(t *testing.T) { "DEV-FWK-CLD-STD", "DEV-FWK-CLD-BUS", "DEV-FWK-CLD-ENT", "DEV-FWK-OPR-STD", "DEV-FWK-OPR-BUS", "DEV-FWK-OPR-ENT", "PLT-CORE-CLD-STD", "PLT-CORE-CLD-BUS", "PLT-CORE-CLD-ENT", "INF-GIT-CLD-STD", "INF-GIT-CLD-BUS", "INF-GIT-CLD-ENT", "INF-GIT-OPR-STD", "INF-GIT-OPR-BUS", "INF-GIT-OPR-ENT", + "INF-CI-CLD-STD", "INF-CI-CLD-BUS", "INF-CI-CLD-ENT", "INF-CI-OPR-STD", "INF-CI-OPR-BUS", "INF-CI-OPR-ENT", } units := map[string]bool{} for _, u := range uoms() { diff --git a/internal/server/services.go b/internal/server/services.go index 2d6f929..b86fce0 100644 --- a/internal/server/services.go +++ b/internal/server/services.go @@ -14,7 +14,8 @@ import ( ) // Managed services (0012, portal#77): infrastructure BreakPilot runs for a -// tenant, for now Managed Git (one Gitea instance per customer). Provisioned +// tenant: Managed Git (one Gitea instance per customer) and its build runners +// (kind ci-runner, 0013; one dedicated machine each, url = the Git instance). Provisioned // by hand (v0): staff keep the record through the admin routes (group // "admin"; the portal decides which staff may write), and the portal shows // the tenant's services under "Verwaltete Infrastruktur" (IT_ADMIN, OWNER @@ -22,6 +23,7 @@ import ( var ( gitSKURE = regexp.MustCompile(`^INF-GIT-(CLD|OPR)-(STD|BUS|ENT)$`) + ciRunnerSKURE = regexp.MustCompile(`^INF-CI-(CLD|OPR)-(STD|BUS|ENT)$`) instanceIDRE = regexp.MustCompile(`^[a-z0-9-]*$`) serviceReadRole = map[string]bool{authn.RoleITAdmin: true, authn.RoleOwner: true, "CXO": true} ) @@ -114,13 +116,15 @@ func normalizeManagedService(ms *store.ManagedService) (code, msg string) { ms.Notes = strings.TrimSpace(ms.Notes) switch { case !store.ValidManagedServiceKind(ms.Kind): - return "invalid_kind", "kind must be git" + return "invalid_kind", "kind must be git or ci-runner" case ms.Name == "" || len([]rune(ms.Name)) > 100: return "invalid_name", "name must be 1 to 100 characters" case !validServiceURL(ms.URL): return "invalid_url", "url must be an absolute https:// URL with a host, at most 300 characters" case ms.Kind == store.ManagedServiceKindGit && !gitSKURE.MatchString(ms.SKU): return "invalid_sku", "sku must be INF-GIT-CLD-* or INF-GIT-OPR-* with STD, BUS or ENT" + case ms.Kind == store.ManagedServiceKindCIRunner && !ciRunnerSKURE.MatchString(ms.SKU): + return "invalid_sku", "sku must be INF-CI-CLD-* or INF-CI-OPR-* with STD, BUS or ENT" case !store.ValidManagedServiceStatus(ms.Status): return "invalid_status", "status must be provisioning, active, restricted or halted" case len(ms.InstanceID) > 100 || !instanceIDRE.MatchString(ms.InstanceID): diff --git a/internal/server/services_test.go b/internal/server/services_test.go index 45aad8f..c90d344 100644 --- a/internal/server/services_test.go +++ b/internal/server/services_test.go @@ -208,3 +208,50 @@ func TestManagedServices_tenantDeleteCascades(t *testing.T) { } }) } + +// Build runners (0013): kind ci-runner with an INF-CI-* SKU; its url is the +// Git instance it serves, and several runners may serve one instance. +func TestManagedServices_ciRunner(t *testing.T) { + eachStore(t, func(t *testing.T, h *testHarness) { + admin := "/v1/admin/tenants/" + h.tenant.ID + "/managed-services" + gitURL := "https://git.k-00001.example" + createService(t, h, h.tenant.ID, map[string]any{"kind": "git", "name": "Git", "url": gitURL, "sku": "INF-GIT-CLD-STD"}) + r1 := createService(t, h, h.tenant.ID, map[string]any{ + "kind": " CI-Runner ", "name": "Runner 1", "url": gitURL, "sku": "inf-ci-cld-std", "instance_id": "runner-k-00001-1", + }) + if r1.Kind != store.ManagedServiceKindCIRunner || r1.SKU != "INF-CI-CLD-STD" || r1.Status != "provisioning" { + t.Fatalf("runner: %+v", r1) + } + r2 := createService(t, h, h.tenant.ID, map[string]any{"kind": "ci-runner", "name": "Runner 2", "url": gitURL, "sku": "INF-CI-OPR-ENT"}) + if r2.ID == r1.ID { + t.Fatalf("second runner: %+v", r2) + } + // still one Git instance per url + if resp, raw := h.do("POST", admin, map[string]any{"kind": "git", "name": "x", "url": gitURL, "sku": "INF-GIT-CLD-BUS"}); resp.StatusCode != 409 { + t.Fatalf("duplicate git: %d %s", resp.StatusCode, raw) + } + // the SKU must fit the kind + for _, c := range []struct{ kind, sku string }{ + {"ci-runner", "INF-GIT-CLD-STD"}, + {"ci-runner", "INF-CI-CLD-XXL"}, + {"ci-runner", "INF-CI-SAAS-STD"}, + {"git", "INF-CI-CLD-STD"}, + } { + resp, raw := h.do("POST", admin, map[string]any{"kind": c.kind, "name": "n", "url": "https://git3.k-00001.example", "sku": c.sku}) + if resp.StatusCode != 400 || !strings.Contains(string(raw), `"invalid_sku"`) { + t.Fatalf("%s %s: %d %s", c.kind, c.sku, resp.StatusCode, raw) + } + } + if resp, raw := h.do("PATCH", admin+"/"+r1.ID, map[string]any{"sku": "INF-GIT-CLD-STD"}); resp.StatusCode != 400 || !strings.Contains(string(raw), `"invalid_sku"`) { + t.Fatalf("patch runner to a git sku: %d %s", resp.StatusCode, raw) + } + resp, raw := h.do("PATCH", admin+"/"+r2.ID, map[string]any{"sku": "INF-CI-OPR-BUS", "status": "active"}) + if resp.StatusCode != 200 || decode[store.ManagedService](t, raw).SKU != "INF-CI-OPR-BUS" { + t.Fatalf("patch runner: %d %s", resp.StatusCode, raw) + } + resp, raw = h.do("GET", "/v1/tenants/"+h.tenant.ID+"/managed-services", nil) + if tl := decode[struct{ Items []map[string]any }](t, raw); resp.StatusCode != 200 || len(tl.Items) != 3 || tl.Items[1]["kind"] != "ci-runner" { + t.Fatalf("tenant list: %d %s", resp.StatusCode, raw) + } + }) +} diff --git a/internal/store/memory_services.go b/internal/store/memory_services.go index e1e2117..43568da 100644 --- a/internal/store/memory_services.go +++ b/internal/store/memory_services.go @@ -8,9 +8,13 @@ import ( "github.com/google/uuid" ) -// serviceTaken reports whether another service of the tenant has the same -// kind and url (UNIQUE (tenant_id, kind, url) in 0012). Caller holds the lock. +// serviceTaken reports whether another Git instance of the tenant has the +// same url (unique index managed_services_git_url, 0013; several runners may +// serve one instance). Caller holds the lock. func (m *Memory) serviceTaken(ms ManagedService) bool { + if ms.Kind != ManagedServiceKindGit { + return false + } for _, x := range m.services { if x.TenantID == ms.TenantID && x.ID != ms.ID && x.Kind == ms.Kind && x.URL == ms.URL { return true diff --git a/internal/store/types.go b/internal/store/types.go index dfa2b59..7658c17 100644 --- a/internal/store/types.go +++ b/internal/store/types.go @@ -295,12 +295,13 @@ const ( ) // ManagedService — infrastructure BreakPilot runs for a tenant (0012, -// portal#77), for now Managed Git: one Gitea instance per customer. +// portal#77): Managed Git (one Gitea instance per customer) and, since 0013, +// its build runners (one dedicated machine each; url is the Git instance). // InstanceID and Notes are staff-only; the tenant view leaves them out. type ManagedService struct { ID string `json:"id"` TenantID string `json:"tenant_id"` - Kind string `json:"kind"` // git + Kind string `json:"kind"` // git | ci-runner Name string `json:"name"` URL string `json:"url"` SKU string `json:"sku"` @@ -312,7 +313,8 @@ type ManagedService struct { } const ( - ManagedServiceKindGit = "git" + ManagedServiceKindGit = "git" + ManagedServiceKindCIRunner = "ci-runner" ManagedServiceProvisioning = "provisioning" ManagedServiceActive = "active" @@ -321,8 +323,10 @@ const ( ) // ValidManagedServiceKind and ValidManagedServiceStatus mirror the CHECK -// constraints of 0012. -func ValidManagedServiceKind(k string) bool { return k == ManagedServiceKindGit } +// constraints of 0012 and 0013. +func ValidManagedServiceKind(k string) bool { + return k == ManagedServiceKindGit || k == ManagedServiceKindCIRunner +} func ValidManagedServiceStatus(s string) bool { switch s { diff --git a/migrations/0013_managed_services_ci_runner.down.sql b/migrations/0013_managed_services_ci_runner.down.sql new file mode 100644 index 0000000..6891668 --- /dev/null +++ b/migrations/0013_managed_services_ci_runner.down.sql @@ -0,0 +1,7 @@ +DELETE FROM managed_services WHERE kind = 'ci-runner'; + +DROP INDEX IF EXISTS managed_services_git_url; +ALTER TABLE managed_services ADD CONSTRAINT managed_services_tenant_id_kind_url_key UNIQUE (tenant_id, kind, url); + +ALTER TABLE managed_services DROP CONSTRAINT managed_services_kind_check; +ALTER TABLE managed_services ADD CONSTRAINT managed_services_kind_check CHECK (kind IN ('git')); diff --git a/migrations/0013_managed_services_ci_runner.up.sql b/migrations/0013_managed_services_ci_runner.up.sql new file mode 100644 index 0000000..a2c7475 --- /dev/null +++ b/migrations/0013_managed_services_ci_runner.up.sql @@ -0,0 +1,12 @@ +-- Build runners (INF-CI-{CLD,OPR}-{STD,BUS,ENT}): a dedicated build machine per +-- customer, never shared, registered with the customer's Managed Git +-- instance. Kind `ci-runner`; its url is the Git instance it serves. +-- +-- Runners are priced per runner, so one Git instance may have several: the +-- (tenant, kind, url) uniqueness of 0012 now holds for kind `git` only. +ALTER TABLE managed_services DROP CONSTRAINT managed_services_kind_check; +ALTER TABLE managed_services ADD CONSTRAINT managed_services_kind_check + CHECK (kind IN ('git', 'ci-runner')); + +ALTER TABLE managed_services DROP CONSTRAINT managed_services_tenant_id_kind_url_key; +CREATE UNIQUE INDEX managed_services_git_url ON managed_services (tenant_id, url) WHERE kind = 'git'; diff --git a/migrations/migrations_test.go b/migrations/migrations_test.go index 30d139c..beb369b 100644 --- a/migrations/migrations_test.go +++ b/migrations/migrations_test.go @@ -289,6 +289,13 @@ func TestSeed_canInsertAndQuery(t *testing.T) { if _, err := db.ExecContext(ctx, insMS, tid, "git", "https://git2.k-00001.example", "paused"); err == nil { t.Error("unknown status was stored") } + // 0013: kind ci-runner; several runners may serve one Git instance. + insRunner := `INSERT INTO managed_services (tenant_id, kind, name, url, sku) VALUES ($1, 'ci-runner', 'Runner', $2, 'INF-CI-CLD-STD')` + for i := 0; i < 2; i++ { + if _, err := db.ExecContext(ctx, insRunner, tid, "https://git.k-00001.example"); err != nil { + t.Fatalf("insert runner %d: %v", i+1, err) + } + } // FK cascade — delete tenant, projects/products/keys/audit_log handling. if _, err := db.ExecContext(ctx, `DELETE FROM tenants WHERE id = $1`, tid); err != nil { diff --git a/openapi.yaml b/openapi.yaml index 21d4af5..0068a01 100644 --- a/openapi.yaml +++ b/openapi.yaml @@ -689,7 +689,7 @@ paths: application/json: { schema: { $ref: "#/components/schemas/ManagedServiceAdmin" } } "400": { $ref: "#/components/responses/BadRequest" } "404": { $ref: "#/components/responses/NotFound" } - "409": { description: The tenant has a service of this kind at this url (duplicate). } + "409": { description: The tenant has a Git instance at this url (duplicate; several runners may share one). } /v1/admin/tenants/{id}/managed-services/{sid}: patch: @@ -712,7 +712,7 @@ paths: application/json: { schema: { $ref: "#/components/schemas/ManagedServiceAdmin" } } "400": { $ref: "#/components/responses/BadRequest" } "404": { $ref: "#/components/responses/NotFound" } - "409": { description: The tenant has a service of this kind at this url (duplicate). } + "409": { description: The tenant has a Git instance at this url (duplicate; several runners may share one). } delete: summary: Delete the record of a managed service (staff). parameters: @@ -1530,9 +1530,9 @@ components: properties: id: { type: string, format: uuid } tenant_id: { type: string, format: uuid } - kind: { type: string, enum: [git] } + kind: { type: string, enum: [git, ci-runner] } name: { type: string } - url: { type: string, description: "https:// URL of the instance" } + url: { type: string, description: "https:// URL of the Git instance (for a ci-runner: the instance it serves)" } sku: { type: string, example: INF-GIT-CLD-STD } status: { $ref: "#/components/schemas/ManagedServiceStatus" } created_at: { type: string, format: date-time } @@ -1552,10 +1552,10 @@ components: type: object required: [kind, name, url, sku] properties: - kind: { type: string, enum: [git] } + kind: { type: string, enum: [git, ci-runner] } name: { type: string, minLength: 1, maxLength: 100 } url: { type: string, maxLength: 300, description: "absolute https:// URL with a host" } - sku: { type: string, pattern: "^INF-GIT-(CLD|OPR)-(STD|BUS|ENT)$" } + sku: { type: string, pattern: "^INF-(GIT|CI)-(CLD|OPR)-(STD|BUS|ENT)$", description: "INF-GIT-* for kind git, INF-CI-* for kind ci-runner" } status: { $ref: "#/components/schemas/ManagedServiceStatus" } instance_id: { type: string, maxLength: 100, pattern: "^[a-z0-9-]*$" } notes: { type: string, maxLength: 2000 } @@ -1564,7 +1564,7 @@ components: properties: name: { type: string, minLength: 1, maxLength: 100 } url: { type: string, maxLength: 300 } - sku: { type: string, pattern: "^INF-GIT-(CLD|OPR)-(STD|BUS|ENT)$" } + sku: { type: string, pattern: "^INF-(GIT|CI)-(CLD|OPR)-(STD|BUS|ENT)$", description: "INF-GIT-* for kind git, INF-CI-* for kind ci-runner" } status: { $ref: "#/components/schemas/ManagedServiceStatus" } instance_id: { type: string, maxLength: 100, pattern: "^[a-z0-9-]*$" } notes: { type: string, maxLength: 2000 }