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
+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 };