Hub 2.3.0: install Accounts (first product, or added next to a church's others)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-18 14:34:18 +02:00
co-authored by Claude Opus 5.5
parent be3945b8be
commit 78857bc0e4
12 changed files with 141 additions and 22 deletions
+4 -4
View File
@@ -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"
+2 -2
View File
@@ -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",
+1 -1
View File
@@ -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) {
@@ -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 <this>/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" }
]
}
}
+27 -8
View File
@@ -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 };
+45
View File
@@ -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');
Binary file not shown.
Binary file not shown.