Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/caddy.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
22 changes: 21 additions & 1 deletion manager/src/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -231,6 +231,9 @@ export async function runUpdate(plan: UpdatePlan): Promise<UpdateOutcome> {
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);
Expand All @@ -256,6 +259,23 @@ export async function runUpdate(plan: UpdatePlan): Promise<UpdateOutcome> {
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<void> {
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;
Expand Down
60 changes: 60 additions & 0 deletions manager/test/self-update.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[][];

Expand Down
6 changes: 6 additions & 0 deletions manager/test/site.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>;
/** Ghost's database, table by table, as the site's MySQL holds it. */
Expand Down Expand Up @@ -439,6 +441,7 @@ export function scriptSite(
ups: [],
downs: [],
resumes: [],
reloads: [],
running: new Set(),
rows: { posts: 3, users: 1, migrations: 120 },
onUp: () => {},
Expand Down Expand Up @@ -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);
Expand Down
Loading