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:
@@ -743,3 +743,63 @@ describe('one update at a time per install (2.7.1)', () => {
|
||||
expect(await db.job.count({ where: { deploymentId: d.id, type: 'deploy' } })).toBe(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe("an install's .env from the Hub (2.8.0)", () => {
|
||||
const poll = (server, v = '1.3.0') => jobs.poll(server, { agentApiVersion: 1, agentVersion: v, mode: 'church', runningJobId: null }, NOW);
|
||||
async function live({ agentVersion = '1.3.0', product = 'events' } = {}) {
|
||||
const { server } = await joinServer();
|
||||
await db.server.update({ where: { id: server.id }, data: { agentVersion } });
|
||||
const d = await db.deployment.create({ data: { id: 'dep-env', serverId: server.id, customerId: 'cust-1', product, shortName: 'hope', domain: `${product}.hope.test`, webPort: 20000, apiPort: 20001, status: 'live' } });
|
||||
return { server, d };
|
||||
}
|
||||
const envResult = (url) => ({ env: {
|
||||
product: 'events', readAt: NOW.toISOString(),
|
||||
files: [{ file: 'backend/.env', exists: true, variables: [
|
||||
{ name: 'FRONTEND_URL', value: url, secret: false, set: true, layer: 'install', description: null, editable: true },
|
||||
{ name: 'JWT_SECRET', value: null, secret: true, set: true, layer: 'generated', description: null, editable: false },
|
||||
] }],
|
||||
} });
|
||||
|
||||
test('read: an env-read job the agent can verify, and its result shown on the page', async () => {
|
||||
const { server, d } = await live();
|
||||
const job = await jobs.readEnv(d.id, admin, NOW);
|
||||
expect(job).toMatchObject({ type: 'env-read', args: { church: 'hope', product: 'events' } });
|
||||
// Asking twice while it waits doesn't make a second job.
|
||||
expect((await jobs.readEnv(d.id, admin, NOW)).id).toBe(job.id);
|
||||
expect((await jobs.envState(d.id)).pending).toHaveLength(1);
|
||||
const [signed] = (await poll(server)).jobs;
|
||||
expect(agent.agentJobSchema.safeParse(signed).success).toBe(true);
|
||||
expect(signed.config).toBeNull();
|
||||
await jobs.receiveReport(server, signedReport(signed, 'succeeded', { result: envResult('https://events.hope.test') }), NOW);
|
||||
const state = await jobs.envState(d.id);
|
||||
expect(state.pending).toEqual([]);
|
||||
expect(state.env.files[0].variables.map(v => [v.name, v.value])).toEqual([['FRONTEND_URL', 'https://events.hope.test'], ['JWT_SECRET', null]]);
|
||||
});
|
||||
|
||||
test('set: plain values and a restart; afterwards the .env is read again', async () => {
|
||||
const { server, d } = await live();
|
||||
const job = await jobs.setEnv(d.id, { file: 'backend/.env', set: { FRONTEND_URL: 'https://new.hope.test' }, unset: [], restart: true }, admin, NOW);
|
||||
expect(job.args).toEqual({ church: 'hope', product: 'events', file: 'backend/.env', set: { FRONTEND_URL: 'https://new.hope.test' }, unset: [], restart: true });
|
||||
await expect(jobs.setEnv(d.id, { set: { APP_TIMEZONE: 'UTC' } }, admin, NOW)).rejects.toMatchObject({ code: 'ALREADY_PLANNED' });
|
||||
const [signed] = (await poll(server)).jobs;
|
||||
expect(agent.agentJobSchema.safeParse(signed).success).toBe(true);
|
||||
await jobs.receiveReport(server, signedReport(signed, 'succeeded'), NOW);
|
||||
expect(await db.job.findFirst({ where: { deploymentId: d.id, type: 'env-read', status: 'queued' } })).toBeTruthy();
|
||||
expect((await jobs.envState(d.id)).lastSet).toMatchObject({ status: 'succeeded' });
|
||||
});
|
||||
|
||||
test("refused: secrets, values the app can't start with, nothing to change, an old agent, a product without .env support", async () => {
|
||||
let { d } = await live();
|
||||
await expect(jobs.setEnv(d.id, { set: { JWT_SECRET: 'x' } }, admin, NOW)).rejects.toMatchObject({ status: 400, message: expect.stringContaining('secret') });
|
||||
await expect(jobs.setEnv(d.id, { set: { TRUST_PROXY: 'TRUE' } }, admin, NOW)).rejects.toMatchObject({ code: 'BAD_VALUE' });
|
||||
await expect(jobs.setEnv(d.id, { set: {} }, admin, NOW)).rejects.toMatchObject({ status: 400 });
|
||||
await expect(jobs.setEnv(d.id, { file: '../.env', set: { A: '1' } }, admin, NOW)).rejects.toMatchObject({ status: 400 });
|
||||
await seed();
|
||||
({ d } = await live({ agentVersion: '1.2.0' }));
|
||||
await expect(jobs.readEnv(d.id, admin, NOW)).rejects.toMatchObject({ code: 'AGENT_TOO_OLD', message: expect.stringContaining('1.3.0') });
|
||||
await seed();
|
||||
({ d } = await live({ product: 'website' }));
|
||||
await expect(jobs.readEnv(d.id, admin, NOW)).rejects.toMatchObject({ code: 'NOT_SUPPORTED' });
|
||||
expect(await db.job.findMany({})).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user