diff --git a/CHANGELOG.md b/CHANGELOG.md index ee6d076..bb60ae9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,25 @@ versions follow [Semantic Versioning](https://semver.org/). ## [Unreleased] +## [2.3.0] - unreleased + +For Accounts 1.0.0 (crosscode-platform `docs/ACCOUNTS-1.0-PLAN.md` §9). Needs agent 1.1.0 on the +church servers for Accounts installs. Contracts 1.5.0 (`backend/vendor/`). + +### Added + +- **New install offers Accounts.** A church's first product can be Accounts (an `install` job with + its free `CCA-` licence). A church already on that server (the same short name) gets the new + product added next to its others: an `add-product` job (`crosscode add`), handled like an install + (waits for the proxy, FastPanel email, ports freed if it fails). Refused for another customer's + short name, a church whose products are on another server, or while its other install hasn't + finished. +- Accounts' env template (`envTemplates/accounts.json`) for releases without one. + +Unchanged (since Hub 2.0): a church gets its Accounts licence with its first product, and Accounts +is never billed. Accounts releases come in once a release source for `accounts@` tags is added +(Releases page). + ## [2.2.0] - unreleased For Cross Events 2.4.0 (crosscode-platform `docs/EVENTS-2.4-PLAN.md` §1): installs move to diff --git a/backend/package-lock.json b/backend/package-lock.json index d61cacc..b535d6b 100644 --- a/backend/package-lock.json +++ b/backend/package-lock.json @@ -10,7 +10,7 @@ "hasInstallScript": true, "dependencies": { "@anthropic-ai/sdk": "^1.3.0", - "@crosscode/contracts": "file:vendor/crosscode-contracts-1.4.0.tgz", + "@crosscode/contracts": "file:vendor/crosscode-contracts-1.5.0.tgz", "@prisma/adapter-pg": "^7.10.1", "@prisma/client": "^7.10.1", "bcryptjs": "^2.4.3", @@ -609,9 +609,9 @@ "license": "MIT" }, "node_modules/@crosscode/contracts": { - "version": "1.4.0", - "resolved": "file:vendor/crosscode-contracts-1.4.0.tgz", - "integrity": "sha512-WJ2LwSBhRmZ7nYTKo4Q1bPmElZew1ZtbO6XZlxopBWtJmpXbv9SSHKoYWS3sVwnXrewPJrAMe2ur8IofE9RsWg==", + "version": "1.5.0", + "resolved": "file:vendor/crosscode-contracts-1.5.0.tgz", + "integrity": "sha512-J3t9tUjPBfyS2UL2wKi49Bc7Ci/Qfy3V6HRj4Z25mNDs4bagi3LqsDwlsxT+Tq9Gxo/GJrIkn73KpDdAGZ2aSw==", "license": "UNLICENSED", "dependencies": { "zod": "^4.6.7" diff --git a/backend/package.json b/backend/package.json index f0a8976..e5185f1 100644 --- a/backend/package.json +++ b/backend/package.json @@ -1,6 +1,6 @@ { "name": "events-hub-backend", - "version": "2.2.0", + "version": "2.3.0", "private": true, "description": "Cross Events Hub — API for managing Cross Events installs, licences and heartbeats", "main": "src/index.js", @@ -20,7 +20,7 @@ }, "dependencies": { "@anthropic-ai/sdk": "^1.3.0", - "@crosscode/contracts": "file:vendor/crosscode-contracts-1.4.0.tgz", + "@crosscode/contracts": "file:vendor/crosscode-contracts-1.5.0.tgz", "@prisma/adapter-pg": "^7.10.1", "@prisma/client": "^7.10.1", "bcryptjs": "^2.4.3", diff --git a/backend/src/utils/envTemplates.js b/backend/src/utils/envTemplates.js index d5239e6..5cf64c3 100644 --- a/backend/src/utils/envTemplates.js +++ b/backend/src/utils/envTemplates.js @@ -10,7 +10,7 @@ const { agent } = require('@crosscode/contracts'); const { getSettings, releaseRepoToken } = require('./settings'); const { PLATFORM_REPO, parseRepoUrl, gitGet } = require('./releaseSync'); -const FALLBACK = { events: require('./envTemplates/events.json') }; +const FALLBACK = { events: require('./envTemplates/events.json'), accounts: require('./envTemplates/accounts.json') }; const cache = new Map(); async function envTemplate(product, version) { diff --git a/backend/src/utils/envTemplates/accounts.json b/backend/src/utils/envTemplates/accounts.json new file mode 100644 index 0000000..5d960e3 --- /dev/null +++ b/backend/src/utils/envTemplates/accounts.json @@ -0,0 +1,24 @@ +{ + "schema": "crosscode.env-template/1", + "product": "accounts", + "files": { + "backend/.env": [ + { "name": "NODE_ENV", "layer": "generated", "required": true, "description": "production" }, + { "name": "PORT", "layer": "install", "from": "{apiPort}", "required": true, "description": "The backend's port (from the Hub's port list)" }, + { "name": "DATABASE_URL", "layer": "generated", "required": true, "description": "Made on the server with a new password; never leaves it" }, + { "name": "ACCOUNTS_SECRET", "layer": "generated", "required": true, "description": "Encrypts stored secrets (signing keys, 2FA secrets, link secrets) and signs cookies: never change it" }, + { "name": "ACCOUNTS_URL", "layer": "install", "from": "https://{domain}", "required": true, "description": "The public address; the OIDC issuer is /api/oidc" }, + { "name": "TRUST_PROXY", "layer": "server", "required": true, "description": "Behind frp: loopback" }, + { "name": "APP_TIMEZONE", "layer": "install", "default": "Africa/Johannesburg" }, + { "name": "HUB_URL", "layer": "defaults", "default": "https://hub.crosscode.co.za" }, + { "name": "HUB_PUBLIC_KEY", "layer": "defaults", "description": "Only for a Hub other than production" }, + { "name": "HUB_LICENCE_KEY", "layer": "install", "description": "A Hub install registers by itself with this (CCA-…)" }, + { "name": "HUB_ADMIN_NAME", "layer": "install" }, + { "name": "HUB_ADMIN_EMAIL", "layer": "install", "description": "Setup only lets this address become the first admin" }, + { "name": "JOBS_ENABLED", "layer": "defaults", "default": "true" } + ], + "frontend/.env.production": [ + { "name": "ACCOUNTS_API_URL", "layer": "install", "from": "http://127.0.0.1:{apiPort}", "required": true, "description": "Where the pages reach the backend on this server" } + ] + } +} diff --git a/backend/src/utils/jobs.js b/backend/src/utils/jobs.js index 2b2d0b5..3c87866 100644 --- a/backend/src/utils/jobs.js +++ b/backend/src/utils/jobs.js @@ -142,6 +142,7 @@ async function afterJob(job, { exportKey }, now) { switch (job.type) { case 'install': + case 'add-product': if (!deployment) break; if (ok) { const version = job.report?.result?.versions?.[deployment.product] || null; @@ -239,7 +240,7 @@ async function tick(now = new Date()) { if (job.noticeId) await endNotice(job.noticeId, now); await emailJoshua(`Missed job: ${job.type} on ${job.server.name}`, `${updated.message}\nNothing ran. A new time needs a new notice to the church.\n\n${hubUrl()}/jobs/${job.id}`); if (job.batchId) await advanceBatch(updated, now); - if (job.type === 'install' && job.deploymentId) { + if ((job.type === 'install' || job.type === 'add-product') && job.deploymentId) { await prisma.deployment.update({ where: { id: job.deploymentId }, data: { status: 'failed' } }); } } @@ -378,10 +379,17 @@ async function preflight(server, product, version, now) { if (parts.length) throw fail(409, 'CONFIG_MISSING', `${parts.join('; ')} (Servers → ${server.name} → Configuration).`); } -/** New install (§7): licence, ports, the install job. */ +/** Products the Hub can install so far (Accounts from Hub 2.3.0, docs/ACCOUNTS-1.0-PLAN.md §9). */ +const INSTALLABLE = new Set(['events', 'accounts']); + +/** + * New install (§7): licence, ports, the install job. A church already on that server (the same + * short name, another product) gets the product added next to its others (an `add-product` job, + * `crosscode add`), e.g. Accounts next to Events. + */ async function newInstall(input, admin, now = new Date()) { const product = input.product || 'events'; - if (product !== 'events') throw fail(400, 'BAD_REQUEST', 'Only Cross Events can be installed so far.'); + if (!INSTALLABLE.has(product)) throw fail(400, 'BAD_REQUEST', `${PRODUCT_NAMES[product] || product} can't be installed from the Hub yet.`); const shortName = String(input.shortName || '').trim(); if (!SHORT_NAME.test(shortName)) throw fail(400, 'BAD_REQUEST', 'The short name is 2–31 characters: a-z, 0-9 and -, starting with a letter.'); const domain = String(input.domain || '').trim().toLowerCase(); @@ -396,7 +404,13 @@ async function newInstall(input, admin, now = new Date()) { if (!server) throw fail(404, 'NOT_FOUND', 'Server not found.'); const release = await releaseFor(product, input.version || null); await preflight(server, product, release.version, now); - if (await prisma.deployment.findFirst({ where: { shortName, status: { notIn: ['removed', 'failed'] } } })) throw fail(409, 'SHORT_NAME_TAKEN', `The short name ${shortName} is already used.`); + // The church's other products: the same short name means the same church folder on one server. + const siblings = await prisma.deployment.findMany({ where: { shortName, status: { notIn: ['removed', 'failed'] } } }); + if (siblings.some(s => s.product === product)) throw fail(409, 'SHORT_NAME_TAKEN', `${shortName} already has ${PRODUCT_NAMES[product]}.`); + if (siblings.some(s => s.customerId !== customer.id)) throw fail(409, 'SHORT_NAME_TAKEN', `The short name ${shortName} is already used by another customer.`); + if (siblings.some(s => s.serverId !== server.id)) throw fail(409, 'OTHER_SERVER', `${shortName}'s other products are on another server; a church's products go on one server.`); + if (siblings.some(s => s.status !== 'live' && s.status !== 'waiting_for_proxy')) throw fail(409, 'BUSY', `Wait until ${shortName}'s other install has finished.`); + const add = siblings.length > 0; // The name is free again after a failed install (its ports were already freed) or an uninstall. // A failed install's record goes; a removed one keeps its history under another name (the // database allows one install per short name and product). Found in the VM test. @@ -417,10 +431,15 @@ async function newInstall(input, admin, now = new Date()) { data: { serverId: server.id, customerId: customer.id, licenceId: licence.id, product, shortName, domain, webPort: ports.web, apiPort: ports.api, timezone: input.timezone || null, adminName: input.adminName || null, adminEmail: adminEmail || null, status: 'installing' }, }); await tx.port.updateMany({ where: { port: { in: [ports.web, ports.api] } }, data: { deploymentId: d.id } }); - await makeJob(tx, { - serverId: server.id, type: 'install', deploymentId: d.id, createdById: admin?.id || null, notBefore: now, - args: { church: shortName, products: [product], versions: { [product]: `${product}@${release.version}` }, domains: { [product]: domain }, ports: { [product]: ports } }, - }); + await makeJob(tx, add + ? { + serverId: server.id, type: 'add-product', deploymentId: d.id, createdById: admin?.id || null, notBefore: now, + args: { church: shortName, product, version: `${product}@${release.version}`, domain, ports }, + } + : { + serverId: server.id, type: 'install', deploymentId: d.id, createdById: admin?.id || null, notBefore: now, + args: { church: shortName, products: [product], versions: { [product]: `${product}@${release.version}` }, domains: { [product]: domain }, ports: { [product]: ports } }, + }); return d; }); return { deployment, warnings }; diff --git a/backend/tests/servers.test.js b/backend/tests/servers.test.js index 09ed6a8..44ef536 100644 --- a/backend/tests/servers.test.js +++ b/backend/tests/servers.test.js @@ -286,6 +286,51 @@ describe('reusing a short name (found in the VM test)', () => { }); }); +describe('Accounts from the Hub (2.3.0)', () => { + const poll = (server) => jobs.poll(server, { agentApiVersion: 1, agentVersion: '1.1.0', mode: 'church', runningJobId: null }, NOW); + beforeEach(async () => { + await db.release.create({ data: { id: 'ra1', product: 'accounts', version: '1.0.0', notes: '- First release', releasedAt: NOW } }); + }); + + test("a church's first product can be Accounts: an install job with its CCA- licence", async () => { + const { server } = await joinServer(); + await servers.setServerConfig(server.id, { TRUST_PROXY: 'loopback' }); + const { deployment } = await jobs.newInstall({ product: 'accounts', customerId: 'cust-1', serverId: server.id, shortName: 'grace', domain: 'accounts.grace.co.za', adminEmail: 'jane@grace.test' }, admin, NOW); + expect(deployment).toMatchObject({ product: 'accounts', status: 'installing' }); + const [job] = (await poll(server)).jobs; + expect(job).toMatchObject({ type: 'install', args: { products: ['accounts'], versions: { accounts: 'accounts@1.0.0' }, domains: { accounts: 'accounts.grace.co.za' } } }); + expect(agent.agentJobSchema.safeParse(job).success).toBe(true); + const config = signing.openConfig(job.config, keys.x.privateKeyPem); + expect(config.install.accounts.HUB_LICENCE_KEY).toMatch(/^CCA-/); + }); + + test("next to the church's Events: an add-product job; finishing it waits for the proxy as an install does", async () => { + const { server } = await joinServer(); + await servers.setServerConfig(server.id, { TRUST_PROXY: 'loopback' }); + const events = await jobs.newInstall({ customerId: 'cust-1', serverId: server.id, shortName: 'hope', domain: 'events.hope.co.za' }, admin, NOW); + await db.deployment.update({ where: { id: events.deployment.id }, data: { status: 'live' } }); + await db.job.updateMany({ where: { deploymentId: events.deployment.id }, data: { status: 'succeeded' } }); + const { deployment } = await jobs.newInstall({ product: 'accounts', customerId: 'cust-1', serverId: server.id, shortName: 'hope', domain: 'accounts.hope.co.za' }, admin, NOW); + const [job] = (await poll(server)).jobs; + expect(job).toMatchObject({ type: 'add-product', args: { church: 'hope', product: 'accounts', version: 'accounts@1.0.0', domain: 'accounts.hope.co.za', ports: { web: 20002, api: 20003 } } }); + expect(agent.agentJobSchema.safeParse(job).success).toBe(true); + expect(signing.openConfig(job.config, keys.x.privateKeyPem).install.accounts).toBeTruthy(); + await jobs.receiveReport(server, signedReport(job, 'succeeded', { result: { versions: { accounts: 'accounts@1.0.0' } } }), NOW); + expect(await db.deployment.findUnique({ where: { id: deployment.id } })).toMatchObject({ status: 'waiting_for_proxy', version: '1.0.0' }); + }); + + test("refused: another server, a product the Hub can't install yet, or while the first is installing", async () => { + const { server } = await joinServer(); + const other = (await joinServer('church', 'church-2')).server; + await servers.setServerConfig(server.id, { TRUST_PROXY: 'loopback' }); + await servers.setServerConfig(other.id, { TRUST_PROXY: 'loopback' }); + await jobs.newInstall({ customerId: 'cust-1', serverId: server.id, shortName: 'hope', domain: 'events.hope.co.za' }, admin, NOW); + await expect(jobs.newInstall({ product: 'accounts', customerId: 'cust-1', serverId: server.id, shortName: 'hope', domain: 'accounts.hope.co.za' }, admin, NOW)).rejects.toMatchObject({ code: 'BUSY' }); + await expect(jobs.newInstall({ product: 'accounts', customerId: 'cust-1', serverId: other.id, shortName: 'hope', domain: 'accounts.hope.co.za' }, admin, NOW)).rejects.toMatchObject({ code: 'OTHER_SERVER' }); + await expect(jobs.newInstall({ product: 'website', customerId: 'cust-1', serverId: server.id, shortName: 'hope', domain: 'www.hope.co.za' }, admin, NOW)).rejects.toMatchObject({ code: 'BAD_REQUEST' }); + }); +}); + describe('settings for servers (found in the VM test)', () => { test('install defaults, the port range and the central IP are saved', async () => { const { setSettings } = require('../src/utils/settings'); diff --git a/backend/vendor/crosscode-contracts-1.4.0.tgz b/backend/vendor/crosscode-contracts-1.4.0.tgz deleted file mode 100644 index 8c98f54..0000000 Binary files a/backend/vendor/crosscode-contracts-1.4.0.tgz and /dev/null differ diff --git a/backend/vendor/crosscode-contracts-1.5.0.tgz b/backend/vendor/crosscode-contracts-1.5.0.tgz new file mode 100644 index 0000000..33524c7 Binary files /dev/null and b/backend/vendor/crosscode-contracts-1.5.0.tgz differ diff --git a/frontend/package-lock.json b/frontend/package-lock.json index 5162362..a3df72c 100644 --- a/frontend/package-lock.json +++ b/frontend/package-lock.json @@ -1,12 +1,12 @@ { "name": "events-hub-frontend", - "version": "2.2.0", + "version": "2.3.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "events-hub-frontend", - "version": "2.2.0", + "version": "2.3.0", "dependencies": { "lucide-react": "^0.536.0", "next": "^15.5.24", diff --git a/frontend/package.json b/frontend/package.json index 49306fa..b4a5dfb 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -1,6 +1,6 @@ { "name": "events-hub-frontend", - "version": "2.2.0", + "version": "2.3.0", "private": true, "scripts": { "dev": "next dev -p 3200", diff --git a/frontend/src/app/(hub)/servers/installs/page.tsx b/frontend/src/app/(hub)/servers/installs/page.tsx index 1f03148..0184d77 100644 --- a/frontend/src/app/(hub)/servers/installs/page.tsx +++ b/frontend/src/app/(hub)/servers/installs/page.tsx @@ -13,7 +13,7 @@ type Msg = { type: "ok" | "err"; text: string } | null; type Customer = { id: string; name: string; domain?: string | null }; type InstallRow = { id: string; name: string; url: string; customer: { id: string; name: string } | null; version: string | null }; -const emptyNew = { customerId: "", serverId: "", shortName: "", domain: "", timezone: "", adminName: "", adminEmail: "", version: "" }; +const emptyNew = { product: "events", customerId: "", serverId: "", shortName: "", domain: "", timezone: "", adminName: "", adminEmail: "", version: "" }; const emptyLink = { installId: "", serverId: "", shortName: "", webPort: "", apiPort: "" }; /** Installs on servers: New install (§7) and linking existing installs (§14). */ @@ -44,7 +44,7 @@ export default function InstallsPage() { setBusy(true); setMsg(null); try { - const r = await api<{ deployment: Deployment; warnings: string[] }>("/api/admin/deployments", { method: "POST", body: { ...form, product: "events", version: form.version || undefined } }); + const r = await api<{ deployment: Deployment; warnings: string[] }>("/api/admin/deployments", { method: "POST", body: { ...form, version: form.version || undefined } }); router.push(`/servers/installs/${r.deployment.id}${r.warnings.length ? `?warn=${encodeURIComponent(r.warnings.join(" "))}` : ""}`); } catch (err) { setMsg({ type: "err", text: errorText(err) }); } setBusy(false); @@ -71,10 +71,22 @@ export default function InstallsPage() { {mode === "new" && (
-

New Cross Events install

+

New install

+
+ + +

+ {form.product === "accounts" + ? "Install Accounts before the Website, the App or Cross Events 3.0. Use the church's short name to add it next to its Events on the same server." + : "A church that already has a product here keeps its short name: the new one is added next to it on the same server."} +

+

Folders, database and tunnels: a-z, 0-9 and -.

-
+

Gets the welcome email and update notices.