Merge branch 'fix/adopt-address'
This commit is contained in:
@@ -166,7 +166,7 @@ router.post('/deployments/:id/proxy-done', wrap(async (req, res) => {
|
||||
}));
|
||||
|
||||
router.post('/deployments/:id/adopt', wrap(async (req, res) => {
|
||||
const job = await jobs.scheduleAdopt({ deploymentId: req.params.id, eventsDir: req.body?.eventsDir, version: req.body?.version, at: req.body?.at }, req.admin);
|
||||
const job = await jobs.scheduleAdopt({ deploymentId: req.params.id, eventsDir: req.body?.eventsDir, version: req.body?.version, address: req.body?.address, at: req.body?.at }, req.admin);
|
||||
await audit(req, 'adopt_scheduled', req.params.id, { job: job.id, at: job.notBefore });
|
||||
res.status(201).json({ job });
|
||||
}));
|
||||
|
||||
@@ -567,7 +567,7 @@ async function linkDeployment(input, admin) {
|
||||
if (!server || !install) throw fail(404, 'NOT_FOUND', 'Server or install not found.');
|
||||
if (install.deployment) throw fail(409, 'ALREADY_LINKED', 'That install is already linked.');
|
||||
const shortName = String(input.shortName || '').trim();
|
||||
if (!SHORT_NAME.test(shortName)) throw fail(400, 'BAD_REQUEST', 'The short name (its folder in /var/www/sites) is a-z, 0-9 and -.');
|
||||
if (!SHORT_NAME.test(shortName)) throw fail(400, 'BAD_REQUEST', 'The short name is just a name (a-z, 0-9 and -, like hope), not a folder: the site lives, or will after adopt, in /var/www/sites/<name>.');
|
||||
const web = parseInt(input.webPort, 10);
|
||||
const api = parseInt(input.apiPort, 10);
|
||||
if (!(web > 0 && api > 0 && web !== api)) throw fail(400, 'BAD_REQUEST', 'Give the site\'s two ports (web and api).');
|
||||
@@ -584,14 +584,20 @@ async function linkDeployment(input, admin) {
|
||||
}
|
||||
|
||||
/** Move a linked 2.1.x install onto the monorepo with `crosscode adopt`, at a time with notice. */
|
||||
async function scheduleAdopt({ deploymentId, eventsDir, version, at }, admin, now = new Date()) {
|
||||
async function scheduleAdopt({ deploymentId, eventsDir, version, address, at }, admin, now = new Date()) {
|
||||
const d = await prisma.deployment.findUnique({ where: { id: deploymentId }, include: { install: true, server: true } });
|
||||
if (!d) throw fail(404, 'NOT_FOUND', 'Not found.');
|
||||
// The church's address (crosscode adopt --address): crosscode can only work it out from an
|
||||
// events.<address> domain, so any other domain (events-demo.crosscode.co.za) needs it given.
|
||||
let church = String(address || '').trim().toLowerCase();
|
||||
if (!church && d.domain.startsWith('events.')) church = d.domain.slice('events.'.length);
|
||||
if (!church) throw fail(400, 'ADDRESS_NEEDED', `${d.domain} doesn't start with "events.", so give the church's address (like hope.co.za).`);
|
||||
if (!DOMAIN.test(church)) throw fail(400, 'BAD_REQUEST', 'The church\'s address is a domain, like hope.co.za.');
|
||||
const when = new Date(at);
|
||||
if (Number.isNaN(when.getTime()) || when - now < SCHEDULE_MIN_HOURS * HOUR) throw fail(400, 'TOO_SOON', `Plan it at least ${SCHEDULE_MIN_HOURS} hours ahead.`);
|
||||
const release = await releaseFor(d.product, version || null);
|
||||
const noticeId = await addNotice(d, { title: 'Planned maintenance', message: `${PRODUCT_NAMES[d.product]} will be moved to its new home on ${sast(when)}. The site will be unavailable for a minute or two.`, endsAt: new Date(when.getTime() + 2 * HOUR) }, now);
|
||||
const job = await makeJob(prisma, { serverId: d.serverId, type: 'adopt', deploymentId: d.id, createdById: admin?.id || null, scheduled: true, notBefore: when, noticeId, args: { church: d.shortName, eventsDir: String(eventsDir || ''), version: `${d.product}@${release.version}` } });
|
||||
const job = await makeJob(prisma, { serverId: d.serverId, type: 'adopt', deploymentId: d.id, createdById: admin?.id || null, scheduled: true, notBefore: when, noticeId, args: { church: d.shortName, eventsDir: String(eventsDir || ''), version: `${d.product}@${release.version}`, address: church } });
|
||||
await emailSiteAdmin(d, `Planned maintenance on ${sast(when)}`, `${PRODUCT_NAMES[d.product]} at ${d.domain} will be moved to its new home on ${sast(when)}. The site will be unavailable for a minute or two; nothing changes for your team.`);
|
||||
return job;
|
||||
}
|
||||
|
||||
@@ -312,3 +312,35 @@ describe('banners reach a site the Hub installed (found in the VM test)', () =>
|
||||
expect((await db.deployment.findUnique({ where: { id: d.id } })).installId).toBe('inst-9');
|
||||
});
|
||||
});
|
||||
|
||||
describe('adopting an existing install', () => {
|
||||
async function linked(domain) {
|
||||
const { server } = await joinServer();
|
||||
await db.release.create({ data: { product: 'events', version: '2.3.0', releasedAt: NOW } });
|
||||
await db.install.create({ data: { id: `inst-${domain}`, customerId: 'cust-1', product: 'events', name: 'Demo', url: `https://${domain}`, secret: 'x', version: '2.1.1' } });
|
||||
return jobs.linkDeployment({ serverId: server.id, installId: `inst-${domain}`, shortName: 'cross-events-demo', webPort: 3100, apiPort: 5100 }, admin);
|
||||
}
|
||||
const at = new Date(NOW.getTime() + 5 * H);
|
||||
|
||||
test('a link takes a name, not a folder', async () => {
|
||||
const { server } = await joinServer();
|
||||
await db.install.create({ data: { id: 'inst-p', customerId: 'cust-1', product: 'events', name: 'P', url: 'https://events.p.test', secret: 'x', version: '2.1.1' } });
|
||||
await expect(jobs.linkDeployment({ serverId: server.id, installId: 'inst-p', shortName: '/var/www/cross-events-demo', webPort: 3100, apiPort: 5100 }, admin))
|
||||
.rejects.toMatchObject({ status: 400, message: expect.stringContaining('not a folder') });
|
||||
});
|
||||
|
||||
test('the old folder and the church address go to crosscode adopt', async () => {
|
||||
const d = await linked('events-demo.crosscode.co.za');
|
||||
const job = await jobs.scheduleAdopt({ deploymentId: d.id, eventsDir: '/var/www/cross-events-demo', address: 'crosscode.co.za', at }, admin, NOW);
|
||||
expect(job.args).toMatchObject({ church: 'cross-events-demo', eventsDir: '/var/www/cross-events-demo', address: 'crosscode.co.za' });
|
||||
expect(agent.jobArgsSchemas.adopt.safeParse(job.args).success).toBe(true);
|
||||
});
|
||||
|
||||
test('a domain without "events." needs the address; events.<address> works it out', async () => {
|
||||
const d = await linked('events-demo.crosscode.co.za');
|
||||
await expect(jobs.scheduleAdopt({ deploymentId: d.id, eventsDir: '/var/www/cross-events-demo', at }, admin, NOW)).rejects.toMatchObject({ status: 400, code: 'ADDRESS_NEEDED' });
|
||||
await db.deployment.update({ where: { id: d.id }, data: { domain: 'events.hope.co.za' } });
|
||||
const job = await jobs.scheduleAdopt({ deploymentId: d.id, eventsDir: '/var/www/hope-events', at }, admin, NOW);
|
||||
expect(job.args.address).toBe('hope.co.za');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -24,7 +24,7 @@ export default function DeploymentPage() {
|
||||
const [msg, setMsg] = useState<Msg>(warn ? { type: "err", text: warn } : null);
|
||||
const [problems, setProblems] = useState<string[]>([]);
|
||||
const [upd, setUpd] = useState({ version: "", at: toInput(new Date(Date.now() + 5 * H)), emergency: false, reason: "", totp: "" });
|
||||
const [adopt, setAdopt] = useState({ eventsDir: "", version: "", at: toInput(new Date(Date.now() + 5 * H)) });
|
||||
const [adopt, setAdopt] = useState({ eventsDir: "", version: "", address: "", at: toInput(new Date(Date.now() + 5 * H)) });
|
||||
const [un, setUn] = useState({ at: toInput(new Date(Date.now() + 7 * 24 * H)), totp: "", confirm: "" });
|
||||
|
||||
const load = useCallback(() => api<Detail>(`/api/admin/deployments/${id}`).then(setD).catch(e => setMsg({ type: "err", text: errorText(e) })), [id]);
|
||||
@@ -85,9 +85,11 @@ export default function DeploymentPage() {
|
||||
)}
|
||||
|
||||
{x.status === "live" && x.install?.version?.startsWith("2.1.") && (
|
||||
<form className="card grid gap-3 md:grid-cols-3" onSubmit={e => { e.preventDefault(); act(`/api/admin/deployments/${x.id}/adopt`, { eventsDir: adopt.eventsDir, version: adopt.version || undefined, at: fromInput(adopt.at) }, "Adopt planned. The site admin was emailed."); }}>
|
||||
<form className="card grid gap-3 md:grid-cols-3" onSubmit={e => { e.preventDefault(); act(`/api/admin/deployments/${x.id}/adopt`, { eventsDir: adopt.eventsDir, version: adopt.version || undefined, address: adopt.address || undefined, at: fromInput(adopt.at) }, "Adopt planned. The site admin was emailed."); }}>
|
||||
<h2 className="font-medium md:col-span-3">Move onto the monorepo (crosscode adopt)</h2>
|
||||
<p className="text-sm text-slate-500 md:col-span-3">Moves the site from its old folder to /var/www/sites/{x.shortName}/events. Same database, uploads, .env and ports; the old folder is kept 14 days for <code>crosscode adopt {x.shortName} --undo</code>.</p>
|
||||
<div><label className="label" htmlFor="a-d">The old install's folder</label><input id="a-d" className="input font-mono" required placeholder="/var/www/hope-events" value={adopt.eventsDir} onChange={e => setAdopt({ ...adopt, eventsDir: e.target.value })} /></div>
|
||||
<div><label className="label" htmlFor="a-a">The church's address</label><input id="a-a" className="input" required={!x.domain.startsWith("events.")} placeholder={x.domain.startsWith("events.") ? x.domain.slice("events.".length) : "hope.co.za"} value={adopt.address} onChange={e => setAdopt({ ...adopt, address: e.target.value })} /></div>
|
||||
<div><label className="label" htmlFor="a-v">Release</label><input id="a-v" className="input" placeholder="latest" value={adopt.version} onChange={e => setAdopt({ ...adopt, version: e.target.value })} /></div>
|
||||
<div><label className="label" htmlFor="a-at">When</label><input id="a-at" type="datetime-local" className="input" value={adopt.at} onChange={e => setAdopt({ ...adopt, at: e.target.value })} /></div>
|
||||
<div className="md:col-span-3"><button className="btn-primary">Plan the move</button></div>
|
||||
|
||||
@@ -111,7 +111,7 @@ export default function InstallsPage() {
|
||||
<p className="text-sm text-slate-500 md:col-span-2">For sites installed by hand or moved with <code>crosscode adopt</code>. Nothing on the server changes; the Hub records the server, folder name and ports (see the server's church.env and frpc.ini).</p>
|
||||
<div><label className="label" htmlFor="l-inst">Install</label><select id="l-inst" className="input" required {...l("installId")}><option value="">Choose…</option>{installs.map(i => <option key={i.id} value={i.id}>{i.customer?.name || i.name} ({i.url})</option>)}</select></div>
|
||||
<div><label className="label" htmlFor="l-srv">Server</label><select id="l-srv" className="input" required {...l("serverId")}><option value="">Choose…</option>{srv.map(s => <option key={s.id} value={s.id}>{s.name}</option>)}</select></div>
|
||||
<div><label className="label" htmlFor="l-short">Short name (its folder in /var/www/sites)</label><input id="l-short" className="input" required {...l("shortName")} /></div>
|
||||
<div><label className="label" htmlFor="l-short">Short name</label><input id="l-short" className="input" required pattern="[a-z][a-z0-9-]{1,30}" placeholder="hope" {...l("shortName")} /><p className="mt-1 text-xs text-slate-500">Just the name, not a path: its folder is /var/www/sites/<name>. A site still in an old folder (/var/www/hope-events) gets that name when you move it onto the monorepo.</p></div>
|
||||
<div className="grid grid-cols-2 gap-2">
|
||||
<div><label className="label" htmlFor="l-web">Web port</label><input id="l-web" className="input" required inputMode="numeric" {...l("webPort")} /></div>
|
||||
<div><label className="label" htmlFor="l-api">API port</label><input id="l-api" className="input" required inputMode="numeric" {...l("apiPort")} /></div>
|
||||
|
||||
Reference in New Issue
Block a user