From b8a41c450d345e56368025f8131d60418232a25c Mon Sep 17 00:00:00 2001 From: Austin Burdine Date: Mon, 2 Nov 2026 15:54:58 -0500 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fixed=20an=20update=20leaving=20?= =?UTF-8?q?Caddy=20on=20its=20old=20configuration?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Compose recreates a container only when its service definition changes. A release that changed only a file Caddy mounts, such as caddy/Caddyfile or a snippet, was written, `up --wait` left Caddy running, and the update was recorded as installed while Caddy kept serving the configuration it had. The shared Caddy's self-update (S13) reloads Caddy explicitly for the same reason; a site with its own Caddy has had this gap since before it. runUpdate, which self-update, `update` and the supervisor share, now runs `caddy reload` once the services are healthy, on a production layout. A configuration Caddy refuses fails the update after startup, so recovery stops the services and puts the files back as for any failed start. Stopped, Caddy reads the restored files when it starts again, so the rollback needs no reload of its own. - manager/src/update.ts: reloadCaddy after up, before verification. - manager/test/site.ts: the scripted site answers `caddy reload`. - manager/test/self-update.test.ts: a production site's update reloads Caddy after up; a refused reload rolls the files back. - docs/caddy.md: an update reloads Caddy, loading unreloaded edits. --- docs/caddy.md | 3 ++ manager/src/update.ts | 22 +++++++++++- manager/test/self-update.test.ts | 60 ++++++++++++++++++++++++++++++++ manager/test/site.ts | 6 ++++ 4 files changed, 90 insertions(+), 1 deletion(-) 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);