@basaltkit/deploy 0.2.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -0
- package/dist/backup.d.ts +5 -3
- package/dist/backup.js +32 -12
- package/dist/config.d.ts +6 -0
- package/dist/config.js +3 -0
- package/dist/pipeline.js +21 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -148,6 +148,10 @@ checks C2 and C1 on each vhost on every deploy and never writes them.
|
|
|
148
148
|
- The dump before migrating — when something is migrated and there is a
|
|
149
149
|
Postgres to dump (dedicated, or a `postgres://` URL in the `.env`);
|
|
150
150
|
`backup.beforeMigrate` forces it either way. `plan` says why when it is skipped.
|
|
151
|
+
With external infra in a container, `backup: { container: 'myapp-postgres' }`
|
|
152
|
+
runs `pg_dump` inside it (`docker exec`), as the user and database of the
|
|
153
|
+
URL at `backup.urlKey` — the server's own pg_dump, none needed on the host.
|
|
154
|
+
The preflight checks the container is running.
|
|
151
155
|
|
|
152
156
|
## What it refuses to do
|
|
153
157
|
|
package/dist/backup.d.ts
CHANGED
|
@@ -6,8 +6,10 @@ import type { InfraPlan } from './infra.js';
|
|
|
6
6
|
* schema-per-tenant every company's schema is in the same database, so one
|
|
7
7
|
* dump covers them all.
|
|
8
8
|
*
|
|
9
|
-
* With dedicated infra the dump runs INSIDE the
|
|
10
|
-
* as the server, by construction
|
|
9
|
+
* With dedicated infra, or `backup.container`, the dump runs INSIDE the
|
|
10
|
+
* container: same pg_dump major as the server, by construction. The password,
|
|
11
|
+
* when there is one, travels in the environment (`docker exec -e PGPASSWORD`,
|
|
12
|
+
* no value), never on a command line.
|
|
11
13
|
*/
|
|
12
14
|
export declare function backupDatabase(ctx: DeployContext, values: Map<string, string>, infra: InfraPlan, sha: string): Promise<string>;
|
|
13
|
-
export declare function restoreCommand(ctx: DeployContext, infra: InfraPlan, file: string): string;
|
|
15
|
+
export declare function restoreCommand(ctx: DeployContext, infra: InfraPlan, file: string, values?: Map<string, string>): string;
|
package/dist/backup.js
CHANGED
|
@@ -6,8 +6,10 @@ import { DeployError } from './context.js';
|
|
|
6
6
|
* schema-per-tenant every company's schema is in the same database, so one
|
|
7
7
|
* dump covers them all.
|
|
8
8
|
*
|
|
9
|
-
* With dedicated infra the dump runs INSIDE the
|
|
10
|
-
* as the server, by construction
|
|
9
|
+
* With dedicated infra, or `backup.container`, the dump runs INSIDE the
|
|
10
|
+
* container: same pg_dump major as the server, by construction. The password,
|
|
11
|
+
* when there is one, travels in the environment (`docker exec -e PGPASSWORD`,
|
|
12
|
+
* no value), never on a command line.
|
|
11
13
|
*/
|
|
12
14
|
export async function backupDatabase(ctx, values, infra, sha) {
|
|
13
15
|
const { host, config } = ctx;
|
|
@@ -21,16 +23,19 @@ export async function backupDatabase(ctx, values, infra, sha) {
|
|
|
21
23
|
result = await host.exec('docker', ['exec', pg.container, 'pg_dump', '-U', pg.user ?? config.app, '-d', pg.database ?? config.app, '--format=custom', '--no-owner', '--no-acl'], { stdoutFile: file });
|
|
22
24
|
}
|
|
23
25
|
else {
|
|
24
|
-
const
|
|
25
|
-
const raw = values.get(key);
|
|
26
|
-
if (!raw)
|
|
27
|
-
throw new DeployError(`backup: ${key} is not set in the .env`);
|
|
28
|
-
const url = new URL(raw);
|
|
26
|
+
const url = databaseUrl(ctx, values);
|
|
29
27
|
const password = decodeURIComponent(url.password);
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
28
|
+
const container = config.backup?.container;
|
|
29
|
+
if (container) {
|
|
30
|
+
const args = ['exec', '-e', 'PGPASSWORD', container, 'pg_dump', '-U', decodeURIComponent(url.username), '-d', databaseName(url), '--format=custom', '--no-owner', '--no-acl'];
|
|
31
|
+
result = await host.exec('docker', args, { stdoutFile: file, env: { PGPASSWORD: password } });
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
url.password = '';
|
|
35
|
+
// Prisma's `?schema=` is not a libpq parameter; pg_dump refuses it.
|
|
36
|
+
url.search = '';
|
|
37
|
+
result = await host.exec('pg_dump', ['--dbname', url.toString(), '--format=custom', '--no-owner', '--no-acl'], { stdoutFile: file, env: { PGPASSWORD: password } });
|
|
38
|
+
}
|
|
34
39
|
}
|
|
35
40
|
const size = (await host.stat(file))?.size ?? 0;
|
|
36
41
|
if (result.code !== 0 || size === 0) {
|
|
@@ -43,9 +48,24 @@ export async function backupDatabase(ctx, values, infra, sha) {
|
|
|
43
48
|
await host.remove(join(dir, old));
|
|
44
49
|
return file;
|
|
45
50
|
}
|
|
46
|
-
export function restoreCommand(ctx, infra, file) {
|
|
51
|
+
export function restoreCommand(ctx, infra, file, values = new Map()) {
|
|
47
52
|
const pg = infra.services.find((s) => s.service === 'postgres');
|
|
48
53
|
if (pg)
|
|
49
54
|
return `docker exec -i ${pg.container} pg_restore --clean --if-exists --no-owner -U ${pg.user ?? ctx.config.app} -d ${pg.database ?? ctx.config.app} < ${file}`;
|
|
55
|
+
const container = ctx.config.backup?.container;
|
|
56
|
+
if (container) {
|
|
57
|
+
const url = databaseUrl(ctx, values);
|
|
58
|
+
return `docker exec -i ${container} pg_restore --clean --if-exists --no-owner -U ${decodeURIComponent(url.username)} -d ${databaseName(url)} < ${file}`;
|
|
59
|
+
}
|
|
50
60
|
return `pg_restore --clean --if-exists --no-owner --dbname "<the database URL, without ?schema=>" ${file}`;
|
|
51
61
|
}
|
|
62
|
+
function databaseUrl(ctx, values) {
|
|
63
|
+
const key = ctx.config.backup?.urlKey ?? 'DATABASE_URL';
|
|
64
|
+
const raw = values.get(key);
|
|
65
|
+
if (!raw)
|
|
66
|
+
throw new DeployError(`backup: ${key} is not set in the .env`);
|
|
67
|
+
return new URL(raw);
|
|
68
|
+
}
|
|
69
|
+
function databaseName(url) {
|
|
70
|
+
return decodeURIComponent(url.pathname.replace(/^\//, ''));
|
|
71
|
+
}
|
package/dist/config.d.ts
CHANGED
|
@@ -150,6 +150,12 @@ export interface BackupConfig {
|
|
|
150
150
|
dir?: string;
|
|
151
151
|
/** With external infra: the `.env` key holding the database URL. Default `DATABASE_URL`. */
|
|
152
152
|
urlKey?: string;
|
|
153
|
+
/**
|
|
154
|
+
* With external infra: the Postgres container to dump INSIDE (`docker exec`)
|
|
155
|
+
* — the server's own pg_dump major, and no pg_dump needed on the host. The
|
|
156
|
+
* user and database come from the URL at `urlKey`.
|
|
157
|
+
*/
|
|
158
|
+
container?: string;
|
|
153
159
|
}
|
|
154
160
|
export type RuntimeConfig = Pm2Runtime | ComposeRuntime;
|
|
155
161
|
/** A Docker Compose project, its images built per commit (RFC 0006 §13.2). */
|
package/dist/config.js
CHANGED
|
@@ -166,6 +166,9 @@ export function validateConfig(config) {
|
|
|
166
166
|
if (config.backup?.keep !== undefined && (!Number.isInteger(config.backup.keep) || config.backup.keep < 1)) {
|
|
167
167
|
problems.push('backup.keep must be a whole number ≥ 1');
|
|
168
168
|
}
|
|
169
|
+
if (config.backup?.container !== undefined && !/^[a-zA-Z0-9][a-zA-Z0-9_.-]*$/.test(config.backup.container)) {
|
|
170
|
+
problems.push(`backup.container "${config.backup.container}" is not a container name`);
|
|
171
|
+
}
|
|
169
172
|
if (problems.length > 0)
|
|
170
173
|
throw new DeployConfigError(problems);
|
|
171
174
|
return config;
|
package/dist/pipeline.js
CHANGED
|
@@ -80,8 +80,24 @@ export async function preflight(ctx, options = {}) {
|
|
|
80
80
|
findings.push(...await apachePreflight(ctx, port));
|
|
81
81
|
if (!options.skip?.includes('tls'))
|
|
82
82
|
findings.push(...await tlsFindings(ctx));
|
|
83
|
+
findings.push(...await backupFindings(ctx, values, options));
|
|
83
84
|
return { findings, port, values };
|
|
84
85
|
}
|
|
86
|
+
/** `backup.container` running — found now, not after the build, minutes later. */
|
|
87
|
+
async function backupFindings(ctx, values, options) {
|
|
88
|
+
const container = ctx.config.backup?.container;
|
|
89
|
+
if (!container || !backupDecision(ctx.config, values, options.skip ?? []).run)
|
|
90
|
+
return [];
|
|
91
|
+
const running = await ctx.host.exec('docker', ['inspect', '--format', '{{.State.Running}}', container]);
|
|
92
|
+
if (running.stdout.trim() === 'true')
|
|
93
|
+
return [];
|
|
94
|
+
return [{
|
|
95
|
+
id: 'backup',
|
|
96
|
+
level: 'error',
|
|
97
|
+
message: running.code === 0 ? `backup.container ${container} is not running` : `backup.container ${container} does not exist (or docker is not available to this user)`,
|
|
98
|
+
fix: `docker start ${container}, or deploy with --skip=backup`,
|
|
99
|
+
}];
|
|
100
|
+
}
|
|
85
101
|
const LOCKFILES = ['pnpm-lock.yaml', 'package-lock.json', 'yarn.lock', 'bun.lock'];
|
|
86
102
|
async function gitFindings(ctx, options) {
|
|
87
103
|
const findings = [];
|
|
@@ -284,8 +300,10 @@ async function publicHealthy(ctx) {
|
|
|
284
300
|
return undefined;
|
|
285
301
|
const url = `https://${config.domain}${config.proxy.apiPrefix.replace(/\/$/, '')}${config.health?.path ?? '/health'}`;
|
|
286
302
|
const answer = await ctx.host.httpGet(url, { timeoutMs: 10_000 });
|
|
287
|
-
if ('status' in answer && answer.status >= 200 && answer.status < 400)
|
|
303
|
+
if ('status' in answer && answer.status >= 200 && answer.status < 400) {
|
|
304
|
+
ctx.host.out.ok(url);
|
|
288
305
|
return undefined;
|
|
306
|
+
}
|
|
289
307
|
return {
|
|
290
308
|
id: 'health',
|
|
291
309
|
level: 'error',
|
|
@@ -336,7 +354,7 @@ export async function plan(ctx, options = {}) {
|
|
|
336
354
|
...(config.hooks?.infra ?? []).map((h) => `infra hook: ${h.join(' ')}`),
|
|
337
355
|
`validate: ${config.env.schema ?? '(no schema configured)'} against the new .env`,
|
|
338
356
|
((decision) => decision.run
|
|
339
|
-
? `backup: pg_dump → ${config.backup?.dir ?? 'backups/predeploy'} (keep ${config.backup?.keep ?? 10})`
|
|
357
|
+
? `backup: pg_dump${config.backup?.container ? ` inside ${config.backup.container}` : ''} → ${config.backup?.dir ?? 'backups/predeploy'} (keep ${config.backup?.keep ?? 10})`
|
|
340
358
|
: `backup: skipped (${decision.why})`)(backupDecision(config, checked.values, options.skip ?? [])),
|
|
341
359
|
`migrate: ${[config.migrate?.command?.join(' ') ?? '', migratesCentral(config) ? 'prisma migrate deploy' : '', config.migrate?.tenants ? 'tenant:migrate' : ''].filter(Boolean).join(' + ') || 'nothing configured'}`,
|
|
342
360
|
`release: ${describeRelease(ctx, checked.port)}`,
|
|
@@ -427,7 +445,7 @@ export async function run(ctx, options = {}) {
|
|
|
427
445
|
if (destructive.length > 0)
|
|
428
446
|
host.out.error(`not rolled back automatically: ${destructive.join(', ')} ${destructive.length === 1 ? 'is' : 'are'} marked destructive`);
|
|
429
447
|
if (record.backup)
|
|
430
|
-
host.out.info(`database before this release: ${restoreCommand(ctx, infra, record.backup)}`);
|
|
448
|
+
host.out.info(`database before this release: ${restoreCommand(ctx, infra, record.backup, values)}`);
|
|
431
449
|
record.note = why;
|
|
432
450
|
await recordRelease(ctx, record);
|
|
433
451
|
return 1;
|