Hub 2.8.0: an install's .env on its page, changed with a restart
- Settings (.env) panel on an Events or Accounts install: reads the .env through an env-read job (addresses first; secrets only as set or not), and changes plain values with an env-set job (restart, or rebuild for the frontend's file; the server puts the file back if the site doesn't start). After a change the file is read again. One change per install at a time. - Contracts 1.12.0 vendored (env jobs need agent 1.3.0). - env-read / env-set in the Jobs type filter. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -187,6 +187,13 @@ async function afterJob(job, { exportKey }, now) {
|
||||
await emailJoshua(`Export failed: ${deployment.shortName}`, jobReportText(job));
|
||||
}
|
||||
break;
|
||||
case 'env-set':
|
||||
// 2.8.0: read the file again, so the install's page shows what's there now (also after a put-back).
|
||||
if (deployment && ['succeeded', 'rolled_back'].includes(job.status)) {
|
||||
await makeJob(prisma, { serverId: deployment.serverId, type: 'env-read', args: { church: deployment.shortName, product: deployment.product }, deploymentId: deployment.id, createdById: job.createdById, notBefore: now });
|
||||
}
|
||||
if (deployment && !ok) await emailJoshua(`.env change ${job.status.replace('_', ' ')}: ${deployment.shortName}`, jobReportText(job));
|
||||
break;
|
||||
case 'uninstall':
|
||||
if (!deployment) break;
|
||||
if (ok) {
|
||||
@@ -985,7 +992,71 @@ async function seedDemo({ deploymentId, profile, confirm }, admin, now = new Dat
|
||||
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 } : {}) } });
|
||||
}
|
||||
|
||||
// ─── An install's .env (2.8.0, contracts 1.12.0, agent 1.3.0) ─────────────────
|
||||
|
||||
const ENV_PRODUCTS = ['events', 'accounts'];
|
||||
const ENV_OPEN = ['queued', 'delivered'];
|
||||
|
||||
/** The install, checked for reading or changing its .env: installed, a product the script knows, an agent that can. */
|
||||
async function envDeployment(deploymentId, now) {
|
||||
const d = await prisma.deployment.findUnique({ where: { id: deploymentId }, include: { server: true } });
|
||||
if (!d) throw fail(404, 'NOT_FOUND', 'Not found.');
|
||||
if (!ENV_PRODUCTS.includes(d.product)) throw fail(400, 'NOT_SUPPORTED', `${PRODUCT_NAMES[d.product] || d.product} installs don't have .env settings here yet.`);
|
||||
if (!['live', 'waiting_for_proxy'].includes(d.status)) throw fail(409, 'NOT_LIVE', `It's ${d.status.replace(/_/g, ' ')}; .env settings are for an installed site.`);
|
||||
if (d.server.removedAt) throw fail(409, 'SERVER_REMOVED', 'That server was removed.');
|
||||
if (compareVersions(d.server.agentVersion || '0.0.0', agent.ENV_JOBS_MIN_AGENT) < 0) {
|
||||
throw fail(409, 'AGENT_TOO_OLD', `The agent on ${d.server.name} is ${d.server.agentVersion || 'an unknown version'}; .env settings need ${agent.ENV_JOBS_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.`);
|
||||
return d;
|
||||
}
|
||||
|
||||
/** What the install's page shows: the last read of its .env (secrets only as set or not) and any env job not finished. */
|
||||
async function envState(deploymentId) {
|
||||
const d = await prisma.deployment.findUnique({ where: { id: deploymentId } });
|
||||
if (!d) throw fail(404, 'NOT_FOUND', 'Not found.');
|
||||
const lastRead = await prisma.job.findFirst({ where: { deploymentId, type: 'env-read', status: 'succeeded' }, orderBy: { finishedAt: 'desc' } });
|
||||
const parsed = lastRead ? agent.envReadResultSchema.safeParse(lastRead.report?.result?.env) : null;
|
||||
const pending = await prisma.job.findMany({ where: { deploymentId, type: { in: ['env-read', 'env-set'] }, status: { in: ENV_OPEN } }, orderBy: { createdAt: 'asc' } });
|
||||
const lastSet = await prisma.job.findFirst({ where: { deploymentId, type: 'env-set', status: { notIn: ENV_OPEN } }, orderBy: { createdAt: 'desc' } });
|
||||
return {
|
||||
supported: ENV_PRODUCTS.includes(d.product),
|
||||
minAgent: agent.ENV_JOBS_MIN_AGENT,
|
||||
env: parsed?.success ? parsed.data : null,
|
||||
readJobId: lastRead?.id || null,
|
||||
pending: pending.map(j => ({ id: j.id, type: j.type, status: j.status, createdAt: j.createdAt })),
|
||||
lastSet: lastSet ? { id: lastSet.id, status: lastSet.status, message: lastSet.message, finishedAt: lastSet.finishedAt, args: lastSet.args } : null,
|
||||
};
|
||||
}
|
||||
|
||||
/** Ask the agent for the install's .env values (an env-read job, at its next poll). */
|
||||
async function readEnv(deploymentId, admin, now = new Date()) {
|
||||
const d = await envDeployment(deploymentId, now);
|
||||
const open = await prisma.job.findFirst({ where: { deploymentId: d.id, type: 'env-read', status: { in: ENV_OPEN } } });
|
||||
if (open) return open;
|
||||
return makeJob(prisma, { serverId: d.serverId, type: 'env-read', args: { church: d.shortName, product: d.product }, deploymentId: d.id, createdById: admin?.id || null, notBefore: now });
|
||||
}
|
||||
|
||||
/**
|
||||
* Change values in one of the install's .env files, then (with `restart`, or always for the
|
||||
* frontend's file, which needs a rebuild) reload it. Never secrets (the contract refuses them, and
|
||||
* so does the server); values the app can't start with are refused here first. If the site doesn't
|
||||
* come back, the server puts the file back.
|
||||
*/
|
||||
async function setEnv(deploymentId, { file, set, unset, restart }, admin, now = new Date()) {
|
||||
const d = await envDeployment(deploymentId, now);
|
||||
const values = set && typeof set === 'object' && !Array.isArray(set) ? Object.fromEntries(Object.entries(set).map(([k, v]) => [k, String(v ?? '')])) : {};
|
||||
const bad = agent.invalidValues(values);
|
||||
if (Object.keys(bad).length) throw fail(400, 'BAD_VALUE', Object.values(bad).join(' '));
|
||||
const args = { church: d.shortName, product: d.product, file: String(file || 'backend/.env'), set: values, unset: Array.isArray(unset) ? unset.map(String) : [], restart: restart !== false };
|
||||
const open = await prisma.job.findFirst({ where: { deploymentId: d.id, type: 'env-set', status: { in: ENV_OPEN } } });
|
||||
if (open) throw fail(409, 'ALREADY_PLANNED', 'A change to this install\'s .env is already waiting for the agent. Wait for it to finish first.');
|
||||
return makeJob(prisma, { serverId: d.serverId, type: 'env-set', args, deploymentId: d.id, createdById: admin?.id || null, notBefore: now });
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
readEnv, setEnv, envState,
|
||||
WINDOW_MIN, SCHEDULE_MIN_HOURS, EMERGENCY_MIN_MINUTES, setDemo, seedDemo,
|
||||
makeJob, signedJob, jobConfig, poll, receiveReport, finish, tick, advanceBatch,
|
||||
JOB_STATUSES, linkFromStatus, listWhere, lookForUnlinked, newInstall, proxyDone, scheduleUpdate, cancelJob, scheduleUninstall, neverInstalled, removeFailedInstall, receiveExport, linkDeployment, scheduleAdopt, queueCentralCheck, runNow,
|
||||
|
||||
Reference in New Issue
Block a user