diff --git a/docs/caddy.md b/docs/caddy.md index a838e602..dd755ce8 100644 --- a/docs/caddy.md +++ b/docs/caddy.md @@ -47,6 +47,9 @@ docker compose exec caddy caddy reload --config /etc/caddy/Caddyfile A reload loads the new configuration whole or not at all: if it does not load, Caddy says why and keeps serving the previous one. Production uses an explicit reload; Caddy documents `--watch` as a local development feature. +An update (`self-update`, `update`) reloads Caddy once the services are up, +so it loads what the files say then, edits not yet reloaded included; a +configuration Caddy refuses fails the update. The changes people make most often, inside the site's block: diff --git a/manager/src/update.ts b/manager/src/update.ts index e45ecbed..be8ed692 100644 --- a/manager/src/update.ts +++ b/manager/src/update.ts @@ -6,7 +6,7 @@ // (docs/architecture.md#recovery). import { relative } from 'node:path'; import { takeBackup } from './backup.ts'; -import { upAndWait } from './compose.ts'; +import { compose, composeError, upAndWait } from './compose.ts'; import { CliError, describeError } from './errors.ts'; import type { Io } from './io.ts'; import type { Metadata } from './meta.ts'; @@ -231,6 +231,9 @@ export async function runUpdate(plan: UpdatePlan): Promise { except, ); ok(io, 'services', 'healthy, by their own health checks'); + if (site.layout === 'production') { + await reloadCaddy(io, dir); + } plan.onStage?.('verifying'); await verifySite(io, dir); @@ -256,6 +259,23 @@ export async function runUpdate(plan: UpdatePlan): Promise { return { state: 'done', backup }; } +/** + * `up` recreates Caddy only when its service changed; a Caddyfile or snippet + * it mounts is loaded only by a reload. One Caddy refuses is a failed update. + */ +async function reloadCaddy(io: Io, dir: string): Promise { + const reload = await io.busy( + 'Loading Caddy’s configuration', + () => compose(io, { dir })`exec -T caddy caddy reload --config /etc/caddy/Caddyfile`, + ); + if (reload.exitCode !== 0) { + throw new CliError( + `Caddy did not load its configuration, and keeps serving the one it had: ${composeError(reload)}`, + ); + } + ok(io, 'caddy', 'its configuration loaded'); +} + /** Where a failed change to a running site stands, for recover(). */ export interface Failure { readonly io: Io; diff --git a/manager/test/self-update.test.ts b/manager/test/self-update.test.ts index 0ac6e8f0..f6b08b05 100644 --- a/manager/test/self-update.test.ts +++ b/manager/test/self-update.test.ts @@ -666,6 +666,66 @@ describe('the writers through an update', () => { }); }); +describe('a production site, with its own Caddy', () => { + beforeEach(async () => { + // Installed from the first release again, as production. + for (const entry of readdirSync(h.dir)) { + rmSync(join(h.dir, entry), { recursive: true, force: true }); + } + release('v0.1.0-beta.1'); + managerId = FIRST; + const installed = await h.run('install', '--domain', 'example.com', '--no-start'); + assert.equal(installed.code, 0, installed.stderr); + writeSiteData(h.dir); + site.running.add('ghost').add('db').add('caddy'); + compose.length = 0; + + // A release that changes only a file Caddy mounts: `up` leaves Caddy as it is. + release('v0.1.0-beta.2'); + managerId = SECOND; + appendFileSync(join(stack, 'caddy', 'Caddyfile'), '# changed in beta.2\n'); + }); + + const reloads = () => compose.filter((args) => args[0] === 'exec' && args.includes('reload')); + + test('Caddy loads the release’s configuration once the services are up', async () => { + const result = await update(); + assert.equal(result.code, 0, result.stderr); + assert.equal( + readSite('caddy/Caddyfile'), + readFileSync(join(stack, 'caddy', 'Caddyfile'), 'utf8'), + ); + assert.deepEqual(reloads(), [ + ['exec', '-T', 'caddy', 'caddy', 'reload', '--config', '/etc/caddy/Caddyfile'], + ]); + const commands = compose.map((args) => args[0]); + assert.ok(commands.lastIndexOf('up') < commands.lastIndexOf('exec')); + assert.match(result.stdout, /caddy +its configuration loaded/); + assert.equal(metadata().stack.version, 'v0.1.0-beta.2'); + }); + + test('a configuration Caddy refuses fails the update, which puts the files back', async () => { + const before = siteOnly(); + site.reloads = [ + failed(1, 'Error: adapting config using caddyfile: unrecognized directive'), + ]; + const result = await update(); + assert.equal(result.code, 1); + assert.match( + result.stderr, + /Caddy did not load its configuration, and keeps serving the one it had: Error: adapting config using caddyfile: unrecognized directive/, + ); + assert.match(result.stderr, /The files are v0\.1\.0-beta\.1's again\./); + assert.match(result.stderr, /the update failed, and the site needs the operator/); + assert.deepEqual(siteOnly(), before); + assert.equal(metadata().stack.version, 'v0.1.0-beta.1'); + assert.equal(pinnedImage(), FIRST); + // Stopped, Caddy among them: started again, it reads the files as they were. + assert.deepEqual([...site.running], []); + assert.equal(reloads().length, 1); + }); +}); + describe('a checkout', () => { let gitCalls: string[][]; diff --git a/manager/test/site.ts b/manager/test/site.ts index 62665e9b..7fee4024 100644 --- a/manager/test/site.ts +++ b/manager/test/site.ts @@ -401,6 +401,8 @@ export interface ScriptedSite { downs: ProgramResult[]; /** What each `up --no-recreate`, starting stopped services again, answers; then success. */ resumes: ProgramResult[]; + /** What each `caddy reload` answers, in order; then success. */ + reloads: ProgramResult[]; /** The services running: what `up` started, until `down` or `stop`; none at first. */ readonly running: Set; /** Ghost's database, table by table, as the site's MySQL holds it. */ @@ -439,6 +441,7 @@ export function scriptSite( ups: [], downs: [], resumes: [], + reloads: [], running: new Set(), rows: { posts: 3, users: 1, migrations: 120 }, onUp: () => {}, @@ -530,6 +533,9 @@ export function scriptSite( .join('\n') + '\n', ); case 'exec': + if (args.includes('reload')) { + return site.reloads.shift() ?? ok(''); + } if ((args[args.indexOf('-c') + 1] ?? '').includes('mysqldump')) { dumped = { ...site.rows }; return ok(SITE_DUMP);