Hub 2.4.0: demo sites, and demo data loaded from the Hub
Mark a Cross Events install as a demo site, then load a demo set (small, events, ministry, multicampus) with a seed-demo job (agent 1.2.0+, crosscode demo: backup, demo data, restart). Typing the short name for both. Deployment.isDemo (migration), contracts 1.7.0 vendored. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Generated
+6
-6
@@ -1,16 +1,16 @@
|
||||
{
|
||||
"name": "events-hub-backend",
|
||||
"version": "2.3.3",
|
||||
"version": "2.4.0",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "events-hub-backend",
|
||||
"version": "2.3.3",
|
||||
"version": "2.4.0",
|
||||
"hasInstallScript": true,
|
||||
"dependencies": {
|
||||
"@anthropic-ai/sdk": "^1.3.0",
|
||||
"@crosscode/contracts": "file:vendor/crosscode-contracts-1.6.0.tgz",
|
||||
"@crosscode/contracts": "file:vendor/crosscode-contracts-1.7.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.6.0",
|
||||
"resolved": "file:vendor/crosscode-contracts-1.6.0.tgz",
|
||||
"integrity": "sha512-uAgoeo0eVUrQtklucztwXxRaawf33ITGqvob7i1C4BcgEkm4hIKbOHWp1qZxW3xT0vkZPfjoeLKtCHp8j1/rSg==",
|
||||
"version": "1.7.0",
|
||||
"resolved": "file:vendor/crosscode-contracts-1.7.0.tgz",
|
||||
"integrity": "sha512-s/c7s6nMhsBnnePV8PN4HBvx8Hg4bSA/OpHtJF5musTPixpNjP4XG8xGAwYtHtyRjGrT5zdgpRppattbqhEJeg==",
|
||||
"license": "UNLICENSED",
|
||||
"dependencies": {
|
||||
"zod": "^4.6.7"
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "events-hub-backend",
|
||||
"version": "2.3.3",
|
||||
"version": "2.4.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.6.0.tgz",
|
||||
"@crosscode/contracts": "file:vendor/crosscode-contracts-1.7.0.tgz",
|
||||
"@prisma/adapter-pg": "^7.10.1",
|
||||
"@prisma/client": "^7.10.1",
|
||||
"bcryptjs": "^2.4.3",
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
-- Installs marked as demo sites: only these can have their data replaced with demo data.
|
||||
ALTER TABLE "Deployment" ADD COLUMN "isDemo" BOOLEAN NOT NULL DEFAULT false;
|
||||
@@ -574,6 +574,8 @@ model Deployment {
|
||||
// The church's site admin, for the welcome and update emails.
|
||||
adminName String?
|
||||
adminEmail String?
|
||||
// A demo site (2.4.0): only these can have their data replaced with demo data (seed-demo jobs).
|
||||
isDemo Boolean @default(false)
|
||||
status DeploymentStatus @default(installing)
|
||||
// The release it runs (from reports and heartbeats).
|
||||
version String?
|
||||
|
||||
@@ -187,6 +187,19 @@ router.post('/deployments/:id/adopt', wrap(async (req, res) => {
|
||||
res.status(201).json({ job });
|
||||
}));
|
||||
|
||||
// Demo sites (2.4.0): mark one, then replace its data with a demo set.
|
||||
router.post('/deployments/:id/demo', wrap(async (req, res) => {
|
||||
const d = await jobs.setDemo(req.params.id, { isDemo: req.body?.isDemo === true, confirm: req.body?.confirm });
|
||||
await audit(req, d.isDemo ? 'install_marked_demo' : 'install_unmarked_demo', d.id, {});
|
||||
res.json({ isDemo: d.isDemo });
|
||||
}));
|
||||
|
||||
router.post('/deployments/:id/seed-demo', wrap(async (req, res) => {
|
||||
const job = await jobs.seedDemo({ deploymentId: req.params.id, profile: req.body?.profile, confirm: req.body?.confirm }, req.admin);
|
||||
await audit(req, 'demo_seed_queued', req.params.id, { job: job.id, profile: job.args.profile });
|
||||
res.status(201).json({ job });
|
||||
}));
|
||||
|
||||
router.post('/deployments/:id/uninstall', wrap(async (req, res) => {
|
||||
const out = await jobs.scheduleUninstall({ deploymentId: req.params.id, at: req.body?.at, totp: req.body?.totp, confirm: req.body?.confirm }, req.admin);
|
||||
await audit(req, 'uninstall_scheduled', req.params.id, { at: out.uninstallJob.notBefore });
|
||||
|
||||
@@ -168,6 +168,10 @@ async function afterJob(job, { exportKey }, now) {
|
||||
await emailJoshua(`${job.type === 'adopt' ? 'Adopt' : 'Update'} ${ok ? 'done' : job.status.replace('_', ' ')}: ${deployment.shortName}`, jobReportText(job));
|
||||
break;
|
||||
}
|
||||
case 'seed-demo':
|
||||
if (!deployment) break;
|
||||
await emailJoshua(`Demo data ${ok ? `(${job.args.profile}) loaded` : job.status.replace('_', ' ')}: ${deployment.shortName}`, jobReportText(job));
|
||||
break;
|
||||
case 'export':
|
||||
if (!deployment) break;
|
||||
if (ok && exportKey) {
|
||||
@@ -677,8 +681,39 @@ async function runNow({ serverId, type, args, deploymentId = null }, admin, now
|
||||
return makeJob(prisma, { serverId, type, args, deploymentId, createdById: admin?.id || null, notBefore: now });
|
||||
}
|
||||
|
||||
/** Mark or unmark an install as a demo site. Marking it asks for its short name, since a demo site's data can be replaced. */
|
||||
async function setDemo(deploymentId, { isDemo, confirm }) {
|
||||
const d = await prisma.deployment.findUnique({ where: { id: deploymentId } });
|
||||
if (!d) throw fail(404, 'NOT_FOUND', 'Not found.');
|
||||
if (d.product !== 'events') throw fail(400, 'NOT_EVENTS', 'Only Cross Events installs have demo data.');
|
||||
if (isDemo && confirm !== d.shortName) throw fail(400, 'CONFIRM', `Type ${d.shortName} to mark it as a demo site.`);
|
||||
return prisma.deployment.update({ where: { id: d.id }, data: { isDemo: !!isDemo } });
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace a demo site's data with a demo set (`crosscode demo`, a backup is taken first). Only for
|
||||
* installs marked as demo sites, typing the short name, on an agent that knows the job (1.2.0+).
|
||||
*/
|
||||
async function seedDemo({ deploymentId, profile, confirm }, admin, now = new Date()) {
|
||||
const d = await prisma.deployment.findUnique({ where: { id: deploymentId }, include: { server: true } });
|
||||
if (!d) throw fail(404, 'NOT_FOUND', 'Not found.');
|
||||
if (!d.isDemo) throw fail(409, 'NOT_DEMO', `${d.shortName} isn't marked as a demo site.`);
|
||||
if (d.product !== 'events') throw fail(400, 'NOT_EVENTS', 'Only Cross Events installs have demo data.');
|
||||
if (!['live', 'waiting_for_proxy'].includes(d.status)) throw fail(409, 'NOT_LIVE', `It's ${d.status.replace(/_/g, ' ')}; demo data goes on an installed site.`);
|
||||
if (confirm !== d.shortName) throw fail(400, 'CONFIRM', `Type ${d.shortName} to replace its data.`);
|
||||
if (!agent.DEMO_PROFILES.includes(profile)) throw fail(400, 'BAD_REQUEST', `Choose one of: ${agent.DEMO_PROFILES.join(', ')}.`);
|
||||
if (d.server.removedAt) throw fail(409, 'SERVER_REMOVED', 'That server was removed.');
|
||||
if (compareVersions(d.server.agentVersion || '0.0.0', agent.SEED_DEMO_MIN_AGENT) < 0) {
|
||||
throw fail(409, 'AGENT_TOO_OLD', `The agent on ${d.server.name} is ${d.server.agentVersion || 'an unknown version'}; demo data needs ${agent.SEED_DEMO_MIN_AGENT} or later (update the agent first).`);
|
||||
}
|
||||
const running = await prisma.job.findFirst({ where: { serverId: d.serverId, status: 'delivered' }, orderBy: { deliveredAt: 'desc' } });
|
||||
if (!agentOnline(d.server, now, running)) throw fail(409, 'AGENT_OFFLINE', `The agent on ${d.server.name} isn't online.`);
|
||||
const adminEmail = d.adminEmail && /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(d.adminEmail) ? d.adminEmail.toLowerCase() : undefined;
|
||||
return makeJob(prisma, { serverId: d.serverId, type: 'seed-demo', deploymentId: d.id, createdById: admin?.id || null, notBefore: now, args: { church: d.shortName, profile, ...(adminEmail ? { adminEmail } : {}) } });
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
WINDOW_MIN, SCHEDULE_MIN_HOURS, EMERGENCY_MIN_MINUTES,
|
||||
WINDOW_MIN, SCHEDULE_MIN_HOURS, EMERGENCY_MIN_MINUTES, setDemo, seedDemo,
|
||||
makeJob, signedJob, jobConfig, poll, receiveReport, finish, tick, advanceBatch,
|
||||
newInstall, proxyDone, scheduleUpdate, cancelJob, scheduleUninstall, neverInstalled, removeFailedInstall, receiveExport, linkDeployment, scheduleAdopt, queueCentralCheck, runNow,
|
||||
fastPanelText, jobReportText, scheduleWarnings, requireTotp, agentOnline, siteAdminEmail,
|
||||
|
||||
@@ -455,3 +455,41 @@ describe('adopting an existing install', () => {
|
||||
expect(job.args.address).toBe('hope.co.za');
|
||||
});
|
||||
});
|
||||
|
||||
describe('demo sites (2.4.0)', () => {
|
||||
async function liveDemo({ agentVersion = '1.2.0', isDemo = true, adminEmail = 'Jane@Hope.test' } = {}) {
|
||||
const { server } = await joinServer();
|
||||
await db.server.update({ where: { id: server.id }, data: { agentVersion } });
|
||||
const d = await db.deployment.create({ data: { id: 'dep-demo', serverId: server.id, customerId: 'cust-1', product: 'events', shortName: 'demo2', domain: 'events.demo2.test', webPort: 20000, apiPort: 20001, status: 'live', isDemo, adminEmail } });
|
||||
return { server, d };
|
||||
}
|
||||
|
||||
test('marking a site as demo asks for its short name; unmarking doesn\'t', async () => {
|
||||
const { d } = await liveDemo({ isDemo: false });
|
||||
await expect(jobs.setDemo(d.id, { isDemo: true, confirm: 'nope' })).rejects.toMatchObject({ code: 'CONFIRM' });
|
||||
expect(await jobs.setDemo(d.id, { isDemo: true, confirm: 'demo2' })).toMatchObject({ isDemo: true });
|
||||
expect(await jobs.setDemo(d.id, { isDemo: false })).toMatchObject({ isDemo: false });
|
||||
});
|
||||
|
||||
test('a seed-demo job for a demo site: the profile, and its admin kept', async () => {
|
||||
const { server, d } = await liveDemo();
|
||||
const job = await jobs.seedDemo({ deploymentId: d.id, profile: 'ministry', confirm: 'demo2' }, admin, NOW);
|
||||
expect(job).toMatchObject({ type: 'seed-demo', deploymentId: d.id, args: { church: 'demo2', profile: 'ministry', adminEmail: 'jane@hope.test' } });
|
||||
const polled = await jobs.poll(server, { agentApiVersion: 1, agentVersion: '1.2.0', mode: 'church', runningJobId: null }, NOW);
|
||||
expect(agent.agentJobSchema.safeParse(polled.jobs[0]).success).toBe(true);
|
||||
expect(polled.jobs[0].config).toBeNull();
|
||||
});
|
||||
|
||||
test('refused: not a demo site, wrong short name, unknown profile, an agent before 1.2.0', async () => {
|
||||
let { d } = await liveDemo({ isDemo: false });
|
||||
await expect(jobs.seedDemo({ deploymentId: d.id, profile: 'small', confirm: 'demo2' }, admin, NOW)).rejects.toMatchObject({ code: 'NOT_DEMO' });
|
||||
await db.deployment.update({ where: { id: d.id }, data: { isDemo: true } });
|
||||
await expect(jobs.seedDemo({ deploymentId: d.id, profile: 'small', confirm: 'demo' }, admin, NOW)).rejects.toMatchObject({ code: 'CONFIRM' });
|
||||
await expect(jobs.seedDemo({ deploymentId: d.id, profile: 'everything', confirm: 'demo2' }, admin, NOW)).rejects.toMatchObject({ code: 'BAD_REQUEST' });
|
||||
|
||||
await seed();
|
||||
({ d } = await liveDemo({ agentVersion: '1.1.1' }));
|
||||
await expect(jobs.seedDemo({ deploymentId: d.id, profile: 'small', confirm: 'demo2' }, admin, NOW)).rejects.toMatchObject({ code: 'AGENT_TOO_OLD', message: expect.stringContaining('1.2.0') });
|
||||
expect(await db.job.findMany({})).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
BIN
Binary file not shown.
BIN
Binary file not shown.
Reference in New Issue
Block a user