@basaltkit/deploy 0.4.1 → 0.5.0
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 +276 -23
- package/dist/answers.d.ts +70 -0
- package/dist/answers.js +196 -0
- package/dist/apache.js +11 -2
- package/dist/audit.d.ts +20 -0
- package/dist/audit.js +26 -0
- package/dist/backup.d.ts +17 -1
- package/dist/backup.js +49 -1
- package/dist/bin.js +9 -1
- package/dist/bootstrap.d.ts +68 -18
- package/dist/bootstrap.js +177 -45
- package/dist/caddy.js +2 -1
- package/dist/check.js +18 -5
- package/dist/checks.js +1 -1
- package/dist/cli.d.ts +22 -1
- package/dist/cli.js +498 -57
- package/dist/compose.d.ts +1 -1
- package/dist/compose.js +6 -1
- package/dist/config.d.ts +25 -2
- package/dist/config.js +65 -3
- package/dist/context.d.ts +43 -13
- package/dist/context.js +50 -7
- package/dist/detach.d.ts +23 -0
- package/dist/detach.js +105 -0
- package/dist/env-step.d.ts +16 -1
- package/dist/env-step.js +58 -5
- package/dist/events.d.ts +168 -0
- package/dist/events.js +477 -0
- package/dist/host.d.ts +171 -7
- package/dist/host.js +302 -41
- package/dist/index.d.ts +10 -5
- package/dist/index.js +7 -3
- package/dist/init.d.ts +2 -0
- package/dist/init.js +2 -1
- package/dist/install-plan.d.ts +40 -0
- package/dist/install-plan.js +433 -0
- package/dist/json-host.d.ts +32 -0
- package/dist/json-host.js +106 -0
- package/dist/lock.d.ts +45 -0
- package/dist/lock.js +128 -0
- package/dist/pipeline.d.ts +51 -3
- package/dist/pipeline.js +339 -173
- package/dist/protocol.d.ts +364 -0
- package/dist/protocol.js +134 -0
- package/dist/runs.d.ts +75 -0
- package/dist/runs.js +287 -0
- package/dist/state.d.ts +26 -13
- package/dist/state.js +68 -4
- package/dist/trust.d.ts +63 -0
- package/dist/trust.js +159 -0
- package/dist/version.d.ts +2 -0
- package/dist/version.js +10 -0
- package/package.json +5 -1
package/dist/index.js
CHANGED
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
export { defineDeploy, secret, ask, pm2, compose, apache, caddy, none, external, bucketName, dockerInfra, validateConfig, applyOverrides, parseOverrides, processName, DeployConfigError, MINIO_IMAGE, } from './config.js';
|
|
2
|
-
export { main, loadConfig, createContext, USAGE } from './cli.js';
|
|
2
|
+
export { main, loadConfig, createContext, processIO, USAGE } from './cli.js';
|
|
3
3
|
export { nodeHost, consoleOutput } from './host.js';
|
|
4
|
-
export { DeployError, INSTALL_COMMAND, RUN_COMMAND } from './context.js';
|
|
4
|
+
export { DeployAbortedError, DeployError, INSTALL_COMMAND, RUN_COMMAND, rootInstallCommand } from './context.js';
|
|
5
5
|
export { parseEnv, applyEnvChanges, formatValue, envValues, UnwritableValueError } from './dotenv.js';
|
|
6
6
|
export { renderVhost, proxiedPorts } from './apache.js';
|
|
7
7
|
export { renderCompose } from './infra.js';
|
|
8
8
|
export { coercionFindings } from './env-check.js';
|
|
9
9
|
export { covers } from './checks.js';
|
|
10
|
-
export { plan, run, envCommand, rollback, status,
|
|
10
|
+
export { plan, run, envCommand, rollback, status, preflight, refuseRoot, SKIPPABLE } from './pipeline.js';
|
|
11
|
+
export { install, buildInstallPlan, parseInstallPlan, readInstallPlan } from './install-plan.js';
|
|
12
|
+
export { DeployLockedError } from './lock.js';
|
|
13
|
+
export { EXIT_ABORTED, EXIT_LOCKED } from './cli.js';
|
|
14
|
+
export { parseEvent, parseAnswerLine, PROTOCOL_VERSION, STEP_IDS, ERROR_CODES, EVENT_TYPES, RUN_ID, } from './protocol.js';
|
package/dist/init.d.ts
CHANGED
|
@@ -61,6 +61,8 @@ export interface InitAnswers {
|
|
|
61
61
|
branch: string;
|
|
62
62
|
proxy: 'apache' | 'caddy' | 'none';
|
|
63
63
|
}
|
|
64
|
+
/** Key names whose values are secrets (what `init` asks as `ask.secret`, what JSON mode redacts). */
|
|
65
|
+
export declare const SENSITIVE: RegExp;
|
|
64
66
|
export declare function detectProject(host: Host, dir: string, options?: {
|
|
65
67
|
server?: string | undefined;
|
|
66
68
|
}): Promise<ProjectFacts>;
|
package/dist/init.js
CHANGED
|
@@ -3,7 +3,8 @@ import { APP_NAME } from './config.js';
|
|
|
3
3
|
const COMPOSE_FILES = ['docker/compose.prod.yml', 'docker/compose.prod.yaml', 'compose.prod.yml', 'compose.prod.yaml', 'docker-compose.prod.yml'];
|
|
4
4
|
const PRISMA_CENTRAL = 'prisma.config.ts';
|
|
5
5
|
const PRISMA_TENANTS = 'prisma/tenants/prisma.config.ts';
|
|
6
|
-
|
|
6
|
+
/** Key names whose values are secrets (what `init` asks as `ask.secret`, what JSON mode redacts). */
|
|
7
|
+
export const SENSITIVE = /SECRET|TOKEN|PASSWORD|PASS\b|_KEY\b|^KEY_|CREDENTIAL|_URL$|_DSN$/;
|
|
7
8
|
/**
|
|
8
9
|
* A `secret()` the app can mint for itself (signing, encryption): generated on
|
|
9
10
|
* the server. One a provider issues (S3, an API, SMTP) is asked instead — a
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { type DeployContext } from './context.js';
|
|
2
|
+
import type { Host } from './host.js';
|
|
3
|
+
import type { InstallPlan } from './protocol.js';
|
|
4
|
+
/**
|
|
5
|
+
* `install` in two halves (RFC 0006 §11.4), so that root never runs code the
|
|
6
|
+
* app user can change:
|
|
7
|
+
*
|
|
8
|
+
* 1. as the app user — `install-plan`: loads deploy.config.ts and writes
|
|
9
|
+
* what install needs as plain JSON (no function, no path it can choose
|
|
10
|
+
* freely);
|
|
11
|
+
* 2. as root, with a copy of this tool root owns — `install --plan=<file>`:
|
|
12
|
+
* reads that JSON without following a link, validates every field again,
|
|
13
|
+
* and only then writes: the state directory, the proxy site, the env
|
|
14
|
+
* files' ownership.
|
|
15
|
+
*
|
|
16
|
+
* The plan is untrusted input to root. Everything root writes comes from a
|
|
17
|
+
* field matched against a fixed pattern here; anything that would land raw in
|
|
18
|
+
* a config file (Caddy `extra`, a non-default config root) is shown and needs
|
|
19
|
+
* a "yes" at the terminal.
|
|
20
|
+
*/
|
|
21
|
+
export declare const PLAN_KIND: InstallPlan['kind'];
|
|
22
|
+
export type { InstallPlan, PlanProxy } from './protocol.js';
|
|
23
|
+
/** What install needs, from the loaded config. Fails, as the app user, on anything install would refuse. */
|
|
24
|
+
export declare function buildInstallPlan(ctx: DeployContext, tool: string): Promise<InstallPlan>;
|
|
25
|
+
/** Parses and validates a plan; every problem is listed. The text is data, never evaluated. */
|
|
26
|
+
export declare function parseInstallPlan(text: string, source: string): InstallPlan;
|
|
27
|
+
/** Reads a plan as root: no symlink followed, a regular file, 64 KiB at most. */
|
|
28
|
+
export declare function readInstallPlan(host: Host, path: string): Promise<InstallPlan>;
|
|
29
|
+
export interface InstallOptions {
|
|
30
|
+
/** Where the state directories live. @default '/var/lib/basalt' */
|
|
31
|
+
stateRoot?: string;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* `sudo … basalt-deploy install --plan=<file>` — once per server, by an
|
|
35
|
+
* administrator, with a copy of this tool root owns (never the checkout's
|
|
36
|
+
* node_modules). Everything that needs root and nothing else.
|
|
37
|
+
*/
|
|
38
|
+
export declare function install(host: Host, plan: InstallPlan, user: string | undefined, options?: InstallOptions): Promise<number>;
|
|
39
|
+
/** The host names a proxy config file serves: Apache ServerName/ServerAlias, Caddy site addresses. */
|
|
40
|
+
export declare function servedNames(text: string, kind: 'apache' | 'caddy'): string[];
|
|
@@ -0,0 +1,433 @@
|
|
|
1
|
+
import { basename, dirname, join, normalize } from 'node:path';
|
|
2
|
+
import { apacheInstall, vhostPaths } from './apache.js';
|
|
3
|
+
import { caddyInstall, caddyPaths } from './caddy.js';
|
|
4
|
+
import { APP_NAME, DOMAIN, vhostName } from './config.js';
|
|
5
|
+
import { DeployError, RUN_COMMAND } from './context.js';
|
|
6
|
+
import { envValues } from './dotenv.js';
|
|
7
|
+
import { envFile, envMirrors, readEnv } from './env-step.js';
|
|
8
|
+
import { acquireLock } from './lock.js';
|
|
9
|
+
import { covers } from './checks.js';
|
|
10
|
+
import { API_PREFIX, ASK_PATH, assertRootOwnedDir, caddyExtraProblem, CERT_NAME, envLocationProblem, isConfigRoot, isWebRoot, USER_NAME, vhostFileProblem } from './trust.js';
|
|
11
|
+
/**
|
|
12
|
+
* `install` in two halves (RFC 0006 §11.4), so that root never runs code the
|
|
13
|
+
* app user can change:
|
|
14
|
+
*
|
|
15
|
+
* 1. as the app user — `install-plan`: loads deploy.config.ts and writes
|
|
16
|
+
* what install needs as plain JSON (no function, no path it can choose
|
|
17
|
+
* freely);
|
|
18
|
+
* 2. as root, with a copy of this tool root owns — `install --plan=<file>`:
|
|
19
|
+
* reads that JSON without following a link, validates every field again,
|
|
20
|
+
* and only then writes: the state directory, the proxy site, the env
|
|
21
|
+
* files' ownership.
|
|
22
|
+
*
|
|
23
|
+
* The plan is untrusted input to root. Everything root writes comes from a
|
|
24
|
+
* field matched against a fixed pattern here; anything that would land raw in
|
|
25
|
+
* a config file (Caddy `extra`, a non-default config root) is shown and needs
|
|
26
|
+
* a "yes" at the terminal.
|
|
27
|
+
*/
|
|
28
|
+
export const PLAN_KIND = 'basalt-deploy/install-plan';
|
|
29
|
+
const MAX_PLAN_BYTES = 64 * 1024;
|
|
30
|
+
// ── the app user's half ────────────────────────────────────────────────────
|
|
31
|
+
/** What install needs, from the loaded config. Fails, as the app user, on anything install would refuse. */
|
|
32
|
+
export async function buildInstallPlan(ctx, tool) {
|
|
33
|
+
const { config } = ctx;
|
|
34
|
+
const port = config.runtime.kind === 'compose' ? config.runtime.upstream.port : Number(envValues(await readEnv(ctx)).get('PORT'));
|
|
35
|
+
if (!Number.isInteger(port) || port <= 0)
|
|
36
|
+
throw new DeployError(`no PORT in the .env yet: run \`${RUN_COMMAND.replace(/ run$/, ' env')}\` first`);
|
|
37
|
+
const plan = {
|
|
38
|
+
kind: PLAN_KIND,
|
|
39
|
+
version: 1,
|
|
40
|
+
app: config.app,
|
|
41
|
+
domain: config.domain,
|
|
42
|
+
tenants: config.tenants ?? 'none',
|
|
43
|
+
dir: ctx.dir,
|
|
44
|
+
port,
|
|
45
|
+
proxy: planProxy(config),
|
|
46
|
+
envFiles: [envFile(ctx), ...envMirrors(ctx)].filter((f) => !f.startsWith(`${ctx.dir}/`)),
|
|
47
|
+
docker: config.infra?.kind === 'docker' || config.runtime.kind === 'compose',
|
|
48
|
+
...(config.bootstrap ? { bootstrap: { ...config.bootstrap } } : {}),
|
|
49
|
+
tool,
|
|
50
|
+
};
|
|
51
|
+
// Root will run exactly these checks: refuse now, where the fix is.
|
|
52
|
+
parseInstallPlan(JSON.stringify(plan), 'the plan');
|
|
53
|
+
return plan;
|
|
54
|
+
}
|
|
55
|
+
function planProxy(config) {
|
|
56
|
+
const proxy = config.proxy;
|
|
57
|
+
if (proxy?.kind === 'apache') {
|
|
58
|
+
return {
|
|
59
|
+
kind: 'apache', managed: proxy.managed !== false, vhost: vhostName(config), webRoot: proxy.webRoot, apiPrefix: proxy.apiPrefix,
|
|
60
|
+
...(proxy.certName === undefined ? {} : { certName: proxy.certName }),
|
|
61
|
+
...(proxy.root === undefined ? {} : { root: proxy.root }),
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
if (proxy?.kind === 'caddy') {
|
|
65
|
+
return {
|
|
66
|
+
kind: 'caddy', managed: proxy.managed !== false, webRoot: proxy.webRoot, apiPrefix: proxy.apiPrefix,
|
|
67
|
+
...(proxy.onDemandTls ? { onDemandTls: { ask: proxy.onDemandTls.ask } } : {}),
|
|
68
|
+
...(proxy.extra === undefined ? {} : { extra: proxy.extra }),
|
|
69
|
+
...(proxy.root === undefined ? {} : { root: proxy.root }),
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
return { kind: 'none' };
|
|
73
|
+
}
|
|
74
|
+
// ── validation: the same on both sides ─────────────────────────────────────
|
|
75
|
+
const VERSION = /^\d+\.\d+\.\d+(-[0-9A-Za-z.-]+)?$/;
|
|
76
|
+
/** Parses and validates a plan; every problem is listed. The text is data, never evaluated. */
|
|
77
|
+
export function parseInstallPlan(text, source) {
|
|
78
|
+
let value;
|
|
79
|
+
try {
|
|
80
|
+
value = JSON.parse(text);
|
|
81
|
+
}
|
|
82
|
+
catch {
|
|
83
|
+
throw new DeployError(`${source} is not JSON — write it with install-plan`);
|
|
84
|
+
}
|
|
85
|
+
const problems = [];
|
|
86
|
+
const plan = (typeof value === 'object' && value !== null && !Array.isArray(value) ? value : {});
|
|
87
|
+
const known = (record, keys, where) => {
|
|
88
|
+
for (const key of Object.keys(record))
|
|
89
|
+
if (!keys.includes(key))
|
|
90
|
+
problems.push(`${where}: unknown key "${key}"`);
|
|
91
|
+
};
|
|
92
|
+
known(plan, ['kind', 'version', 'app', 'domain', 'tenants', 'dir', 'port', 'proxy', 'envFiles', 'docker', 'bootstrap', 'tool'], 'plan');
|
|
93
|
+
if (plan['kind'] !== PLAN_KIND || plan['version'] !== 1)
|
|
94
|
+
problems.push(`not a version-1 ${PLAN_KIND}`);
|
|
95
|
+
const app = typeof plan['app'] === 'string' && APP_NAME.test(plan['app']) ? plan['app'] : undefined;
|
|
96
|
+
if (!app)
|
|
97
|
+
problems.push('app is not an app name');
|
|
98
|
+
if (typeof plan['domain'] !== 'string' || !DOMAIN.test(plan['domain']))
|
|
99
|
+
problems.push('domain is not a lowercase host name');
|
|
100
|
+
if (plan['tenants'] !== 'subdomain' && plan['tenants'] !== 'none')
|
|
101
|
+
problems.push('tenants is subdomain or none');
|
|
102
|
+
const dir = plan['dir'];
|
|
103
|
+
if (typeof dir !== 'string' || !dir.startsWith('/') || dir === '/' || normalize(dir) !== dir || dir.endsWith('/') || /[\0\n\r"'\\$`\s]/.test(dir)) {
|
|
104
|
+
problems.push('dir must be an absolute, normalized path without quotes, spaces or shell characters (not /)');
|
|
105
|
+
}
|
|
106
|
+
const port = plan['port'];
|
|
107
|
+
if (typeof port !== 'number' || !Number.isInteger(port) || port < 1 || port > 65535)
|
|
108
|
+
problems.push('port must be a port number');
|
|
109
|
+
if (typeof plan['docker'] !== 'boolean')
|
|
110
|
+
problems.push('docker must be true or false');
|
|
111
|
+
if (typeof plan['tool'] !== 'string' || !VERSION.test(plan['tool']))
|
|
112
|
+
problems.push('tool must be a version');
|
|
113
|
+
const files = plan['envFiles'];
|
|
114
|
+
if (!Array.isArray(files) || files.length > 8)
|
|
115
|
+
problems.push('envFiles must be a list (at most 8)');
|
|
116
|
+
else {
|
|
117
|
+
for (const file of files) {
|
|
118
|
+
const problem = typeof file === 'string' && file.startsWith('/') && app ? envLocationProblem(app, file) : `"${String(file)}" must be an absolute path in /etc/${app ?? '<app>'}/`;
|
|
119
|
+
if (problem)
|
|
120
|
+
problems.push(`envFiles: ${problem}`);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
const bootstrap = plan['bootstrap'];
|
|
124
|
+
if (bootstrap !== undefined) {
|
|
125
|
+
if (typeof bootstrap !== 'object' || bootstrap === null || Array.isArray(bootstrap))
|
|
126
|
+
problems.push('bootstrap must be an object');
|
|
127
|
+
else {
|
|
128
|
+
const b = bootstrap;
|
|
129
|
+
known(b, ['swapGb', 'firewall'], 'bootstrap');
|
|
130
|
+
if (b['swapGb'] !== undefined && (typeof b['swapGb'] !== 'number' || !Number.isInteger(b['swapGb']) || b['swapGb'] < 0 || b['swapGb'] > 64))
|
|
131
|
+
problems.push('bootstrap.swapGb must be a whole number from 0 to 64');
|
|
132
|
+
if (b['firewall'] !== undefined && typeof b['firewall'] !== 'boolean')
|
|
133
|
+
problems.push('bootstrap.firewall must be true or false');
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
const proxy = (typeof plan['proxy'] === 'object' && plan['proxy'] !== null ? plan['proxy'] : {});
|
|
137
|
+
const kind = proxy['kind'];
|
|
138
|
+
if (kind === 'none')
|
|
139
|
+
known(proxy, ['kind'], 'proxy');
|
|
140
|
+
else if (kind === 'apache' || kind === 'caddy') {
|
|
141
|
+
known(proxy, kind === 'apache'
|
|
142
|
+
? ['kind', 'managed', 'vhost', 'webRoot', 'apiPrefix', 'certName', 'root']
|
|
143
|
+
: ['kind', 'managed', 'webRoot', 'apiPrefix', 'onDemandTls', 'extra', 'root'], 'proxy');
|
|
144
|
+
if (typeof proxy['managed'] !== 'boolean')
|
|
145
|
+
problems.push('proxy.managed must be true or false');
|
|
146
|
+
// Unmanaged, root writes no site: the two fields go nowhere, but they stay strings.
|
|
147
|
+
const managed = proxy['managed'] !== false;
|
|
148
|
+
if (managed ? !isWebRoot(proxy['webRoot']) : typeof proxy['webRoot'] !== 'string')
|
|
149
|
+
problems.push('proxy.webRoot must be a directory inside the checkout (no dot directories, not the checkout itself)');
|
|
150
|
+
if (managed ? typeof proxy['apiPrefix'] !== 'string' || !API_PREFIX.test(proxy['apiPrefix']) : typeof proxy['apiPrefix'] !== 'string')
|
|
151
|
+
problems.push('proxy.apiPrefix must be a URL path such as /api');
|
|
152
|
+
if (proxy['root'] !== undefined && !isConfigRoot(proxy['root']))
|
|
153
|
+
problems.push('proxy.root must be an absolute directory');
|
|
154
|
+
if (kind === 'apache') {
|
|
155
|
+
const vhost = vhostFileProblem(app ?? '', proxy['vhost']);
|
|
156
|
+
if (vhost)
|
|
157
|
+
problems.push(`proxy.vhost ${vhost}`);
|
|
158
|
+
if (proxy['certName'] !== undefined && (typeof proxy['certName'] !== 'string' || !CERT_NAME.test(proxy['certName'])))
|
|
159
|
+
problems.push('proxy.certName is not a certificate name');
|
|
160
|
+
}
|
|
161
|
+
else {
|
|
162
|
+
const ask = proxy['onDemandTls'];
|
|
163
|
+
if (ask !== undefined && (typeof ask !== 'object' || ask === null || typeof ask.ask !== 'string' || !ASK_PATH.test(ask.ask) || Object.keys(ask).length !== 1)) {
|
|
164
|
+
problems.push('proxy.onDemandTls must be { ask: "/path" }');
|
|
165
|
+
}
|
|
166
|
+
const extra = proxy['extra'] === undefined ? undefined : caddyExtraProblem(proxy['extra']);
|
|
167
|
+
if (extra)
|
|
168
|
+
problems.push(`proxy.extra ${extra}`);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
else
|
|
172
|
+
problems.push('proxy.kind is apache, caddy or none');
|
|
173
|
+
if (problems.length > 0)
|
|
174
|
+
throw new DeployError(`${source} is refused:\n${problems.map((p) => ` - ${p}`).join('\n')}`);
|
|
175
|
+
return value;
|
|
176
|
+
}
|
|
177
|
+
/** Reads a plan as root: no symlink followed, a regular file, 64 KiB at most. */
|
|
178
|
+
export async function readInstallPlan(host, path) {
|
|
179
|
+
let text;
|
|
180
|
+
try {
|
|
181
|
+
text = await host.readPlainFile(path, MAX_PLAN_BYTES);
|
|
182
|
+
}
|
|
183
|
+
catch (error) {
|
|
184
|
+
throw new DeployError(`--plan=${path}: ${error.message}`);
|
|
185
|
+
}
|
|
186
|
+
return parseInstallPlan(text, path);
|
|
187
|
+
}
|
|
188
|
+
async function account(host, user) {
|
|
189
|
+
const uid = await host.exec('id', ['-u', user]);
|
|
190
|
+
const gid = await host.exec('id', ['-g', user]);
|
|
191
|
+
const u = Number(uid.stdout.trim());
|
|
192
|
+
const g = Number(gid.stdout.trim());
|
|
193
|
+
if (uid.code !== 0 || gid.code !== 0 || !Number.isInteger(u) || !Number.isInteger(g))
|
|
194
|
+
throw new DeployError(`no user ${user} on this server`);
|
|
195
|
+
if (u === 0)
|
|
196
|
+
throw new DeployError(`${user} is uid 0: install needs the unprivileged user the deploys run as`);
|
|
197
|
+
return { name: user, uid: u, gid: g };
|
|
198
|
+
}
|
|
199
|
+
/** The DeployContext the proxy writers read: built from the plan's fields only. */
|
|
200
|
+
function contextOf(host, plan, stateDir) {
|
|
201
|
+
const proxy = plan.proxy.kind === 'apache'
|
|
202
|
+
? { ...plan.proxy }
|
|
203
|
+
: plan.proxy.kind === 'caddy'
|
|
204
|
+
? { ...plan.proxy }
|
|
205
|
+
: { kind: 'none' };
|
|
206
|
+
const config = {
|
|
207
|
+
app: plan.app, domain: plan.domain, tenants: plan.tenants, env: {},
|
|
208
|
+
runtime: { kind: 'pm2', script: 'start', port: plan.port, avoid: [] },
|
|
209
|
+
proxy,
|
|
210
|
+
};
|
|
211
|
+
return { config, dir: plan.dir, host, pm: 'pnpm', envPath: '', stateDir };
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* `sudo … basalt-deploy install --plan=<file>` — once per server, by an
|
|
215
|
+
* administrator, with a copy of this tool root owns (never the checkout's
|
|
216
|
+
* node_modules). Everything that needs root and nothing else.
|
|
217
|
+
*/
|
|
218
|
+
export async function install(host, plan, user, options = {}) {
|
|
219
|
+
if (host.uid() !== 0)
|
|
220
|
+
throw new DeployError('install needs root: sudo, with a copy of @basaltkit/deploy root owns (see install-plan)');
|
|
221
|
+
if (!user || user === 'root')
|
|
222
|
+
throw new DeployError('install needs the unprivileged user the deploys run as: --user=<name> (never root)');
|
|
223
|
+
if (!USER_NAME.test(user))
|
|
224
|
+
throw new DeployError(`--user=${user} is not a login name`);
|
|
225
|
+
// Validated again: a plan built in code may not have come through readInstallPlan.
|
|
226
|
+
parseInstallPlan(JSON.stringify(plan), 'the plan');
|
|
227
|
+
const owner = await account(host, user);
|
|
228
|
+
// The checkout is the user's own directory — the web root is served from it.
|
|
229
|
+
const checkout = await host.stat(plan.dir);
|
|
230
|
+
if (checkout?.kind !== 'dir')
|
|
231
|
+
throw new DeployError(`${plan.dir} is not a directory (a symbolic link is refused)`);
|
|
232
|
+
if (checkout.uid !== owner.uid)
|
|
233
|
+
throw new DeployError(`${plan.dir} is not ${user}'s (uid ${checkout.uid}): install serves ${user}'s checkout only`);
|
|
234
|
+
const stateRoot = options.stateRoot ?? '/var/lib/basalt';
|
|
235
|
+
await host.mkdir(stateRoot, 0o755);
|
|
236
|
+
await assertRootOwnedDir(host, stateRoot, 'state directory');
|
|
237
|
+
const stateDir = join(stateRoot, plan.app);
|
|
238
|
+
const backups = join(stateRoot, `${plan.app}.install-backups`);
|
|
239
|
+
host.out.step(`state directory ${stateDir}`);
|
|
240
|
+
await refuseForeign(host, stateDir, owner, 'the state directory');
|
|
241
|
+
await host.mkdir(stateDir, 0o700);
|
|
242
|
+
await secure(host, stateDir, { uid: owner.uid, gid: owner.gid, mode: 0o700, kind: 'dir' });
|
|
243
|
+
// Root's own: the copies of the proxy files it replaces. Not in stateDir, which the user owns.
|
|
244
|
+
await host.mkdir(backups, 0o700);
|
|
245
|
+
await secure(host, backups, { uid: 0, gid: 0, mode: 0o700, kind: 'dir' });
|
|
246
|
+
const release = await acquireLock(host, stateDir, 'install', host.env['SUDO_USER'] ?? 'root');
|
|
247
|
+
const off = host.onSignal((signal) => host.out.warn(`${signal}: install cannot stop safely mid-way — it finishes, then exits (a second ${signal} stops it at once)`));
|
|
248
|
+
try {
|
|
249
|
+
host.out.ok(`${stateDir} (0700, owned by ${user}); the release history moves there on the next deploy`);
|
|
250
|
+
const overrides = `/etc/basalt/${plan.app}.json`;
|
|
251
|
+
await host.mkdir('/etc/basalt', 0o755);
|
|
252
|
+
await assertRootOwnedDir(host, '/etc/basalt', 'server overrides');
|
|
253
|
+
if (!(await host.exists(overrides))) {
|
|
254
|
+
await host.writeFile(overrides, '{}\n', 0o644);
|
|
255
|
+
host.out.ok(`${overrides} created (server-local overrides, no secrets)`);
|
|
256
|
+
}
|
|
257
|
+
const ctx = contextOf(host, plan, stateDir);
|
|
258
|
+
summary(host, plan, user, stateDir, ctx);
|
|
259
|
+
await envOwnership(host, plan, owner);
|
|
260
|
+
if (plan.proxy.kind === 'apache' && plan.proxy.managed) {
|
|
261
|
+
const root = plan.proxy.root ?? '/etc/apache2';
|
|
262
|
+
await confirmRoot(host, root, '/etc/apache2');
|
|
263
|
+
for (const dir of [join(root, 'sites-available'), join(root, 'sites-enabled')])
|
|
264
|
+
await assertRootOwnedDir(host, dir, 'Apache');
|
|
265
|
+
await refuseTakenNames(host, plan, [join(root, 'sites-available'), join(root, 'sites-enabled')], vhostPaths(ctx).file, 'apache');
|
|
266
|
+
host.out.step(`Apache vhost ${vhostPaths(ctx).file}`);
|
|
267
|
+
await apacheInstall(ctx, plan.port, backups);
|
|
268
|
+
}
|
|
269
|
+
if (plan.proxy.kind === 'caddy' && plan.proxy.managed) {
|
|
270
|
+
const root = plan.proxy.root ?? '/etc/caddy';
|
|
271
|
+
await confirmRoot(host, root, '/etc/caddy');
|
|
272
|
+
await assertRootOwnedDir(host, root, 'Caddy');
|
|
273
|
+
await refuseTakenNames(host, plan, [caddyPaths(ctx).dir], `${plan.app}.caddy`, 'caddy', caddyPaths(ctx).main);
|
|
274
|
+
if (plan.proxy.extra !== undefined)
|
|
275
|
+
await confirmExtra(host, plan.proxy.extra, caddyPaths(ctx).site);
|
|
276
|
+
host.out.step(`Caddy site ${caddyPaths(ctx).site}`);
|
|
277
|
+
await host.mkdir(caddyPaths(ctx).dir, 0o755);
|
|
278
|
+
await assertRootOwnedDir(host, caddyPaths(ctx).dir, 'Caddy');
|
|
279
|
+
await caddyInstall(ctx, plan.port, backups);
|
|
280
|
+
}
|
|
281
|
+
if (plan.docker) {
|
|
282
|
+
const groups = (await host.exec('id', ['-nG', user])).stdout.split(/\s+/);
|
|
283
|
+
if (groups.includes('docker')) {
|
|
284
|
+
host.out.warn(`${user} is in the docker group: that is root on this machine (a container can mount /). Plan the move to rootless Docker for ${user} (dockerd-rootless-setuptool.sh install); this command will not do it for a server with data in rootful volumes.`);
|
|
285
|
+
}
|
|
286
|
+
else {
|
|
287
|
+
host.out.info(`Docker for ${user}: rootless — dockerd-rootless-setuptool.sh install (as ${user}), then loginctl enable-linger ${user}`);
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
host.out.ok(`installed — from now on, as ${user} (not root), in ${plan.dir}: ${RUN_COMMAND}`);
|
|
291
|
+
return 0;
|
|
292
|
+
}
|
|
293
|
+
finally {
|
|
294
|
+
off();
|
|
295
|
+
await release();
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
/** What root is about to do, from the plan — said before anything is written. */
|
|
299
|
+
function summary(host, plan, user, stateDir, ctx) {
|
|
300
|
+
const names = [plan.domain, ...(plan.tenants === 'subdomain' ? [`*.${plan.domain}`] : [])].join(', ');
|
|
301
|
+
const site = plan.proxy.kind === 'apache' && plan.proxy.managed
|
|
302
|
+
? `Apache ${vhostPaths(ctx).available} for ${names} → 127.0.0.1:${plan.port}, ${join(plan.dir, plan.proxy.webRoot)} served, certificate ${plan.proxy.certName ?? plan.domain}`
|
|
303
|
+
: plan.proxy.kind === 'caddy' && plan.proxy.managed
|
|
304
|
+
? `Caddy ${caddyPaths(ctx).site} for ${names} → 127.0.0.1:${plan.port}, ${join(plan.dir, plan.proxy.webRoot)} served`
|
|
305
|
+
: 'no proxy site';
|
|
306
|
+
host.out.info(`install for "${plan.app}" (plan by @basaltkit/deploy ${plan.tool}), for ${user}, checkout ${plan.dir}:`);
|
|
307
|
+
host.out.info(` state ${stateDir} · ${site}${plan.envFiles.length ? ` · env ${plan.envFiles.join(', ')}` : ''}`);
|
|
308
|
+
}
|
|
309
|
+
/** The host names a proxy config file serves: Apache ServerName/ServerAlias, Caddy site addresses. */
|
|
310
|
+
export function servedNames(text, kind) {
|
|
311
|
+
const clean = (name) => name.toLowerCase().replace(/^[a-z]+:\/\//, '').replace(/:\d+$/, '').replace(/\.$/, '');
|
|
312
|
+
if (kind === 'apache') {
|
|
313
|
+
return [...text.matchAll(/^\s*Server(?:Name|Alias)\s+(.+)$/gim)].flatMap((m) => (m[1] ?? '').trim().split(/\s+/)).map(clean).filter(Boolean);
|
|
314
|
+
}
|
|
315
|
+
const names = [];
|
|
316
|
+
let depth = 0;
|
|
317
|
+
for (const raw of text.split('\n')) {
|
|
318
|
+
const line = raw.replace(/(^|\s)#.*$/, '').trim();
|
|
319
|
+
if (depth === 0 && line.endsWith('{') && line !== '{') {
|
|
320
|
+
for (const address of line.slice(0, -1).split(/[\s,]+/)) {
|
|
321
|
+
if (address && !address.startsWith('(') && !address.startsWith(':'))
|
|
322
|
+
names.push(clean(address));
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
depth += (line.match(/{/g) ?? []).length - (line.match(/}/g) ?? []).length;
|
|
326
|
+
if (depth < 0)
|
|
327
|
+
depth = 0;
|
|
328
|
+
}
|
|
329
|
+
return names.filter(Boolean);
|
|
330
|
+
}
|
|
331
|
+
const overlap = (a, b) => a === b || (a.startsWith('*.') && covers(a, b)) || (b.startsWith('*.') && covers(b, a));
|
|
332
|
+
/**
|
|
333
|
+
* The plan's names are not another site's: a new file whose ServerName (or
|
|
334
|
+
* Caddy address) is a co-hosted app's domain — or a more specific name under
|
|
335
|
+
* its wildcard — would take that app's traffic and certificate.
|
|
336
|
+
*/
|
|
337
|
+
async function refuseTakenNames(host, plan, dirs, own, kind, main) {
|
|
338
|
+
const ours = [plan.domain, ...(plan.tenants === 'subdomain' ? [`*.${plan.domain}`] : [])];
|
|
339
|
+
const files = [...(main ? [main] : []), ...(await Promise.all(dirs.map(async (dir) => (await host.list(dir)).filter((name) => name !== own).map((name) => join(dir, name))))).flat()];
|
|
340
|
+
for (const file of files) {
|
|
341
|
+
const text = await host.readFile(file);
|
|
342
|
+
if (text === undefined)
|
|
343
|
+
continue;
|
|
344
|
+
const taken = servedNames(text, kind).filter((name) => ours.some((mine) => overlap(mine, name)));
|
|
345
|
+
if (taken.length > 0)
|
|
346
|
+
throw new DeployError(`refused: ${file} already serves ${[...new Set(taken)].join(', ')} — "${plan.app}" may not take another site's names (${ours.join(', ')})`);
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
async function secure(host, path, owner) {
|
|
350
|
+
try {
|
|
351
|
+
await host.secureOwner(path, owner);
|
|
352
|
+
}
|
|
353
|
+
catch (error) {
|
|
354
|
+
throw new DeployError(`refused: ${error.message}`);
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
/**
|
|
358
|
+
* An existing path install would hand to the app user: refused when another
|
|
359
|
+
* account owns it — another app's state, its dumps, its secrets.
|
|
360
|
+
*/
|
|
361
|
+
async function refuseForeign(host, path, owner, what) {
|
|
362
|
+
const stat = await host.stat(path);
|
|
363
|
+
if (!stat || stat.uid === undefined || stat.uid === 0 || stat.uid === owner.uid)
|
|
364
|
+
return;
|
|
365
|
+
throw new DeployError(`refused: ${what} ${path} belongs to uid ${stat.uid}, not ${owner.name} — another app's? Nothing is handed over from one account to another; name this app something else`);
|
|
366
|
+
}
|
|
367
|
+
/**
|
|
368
|
+
* The env files in `/etc/<app>/` (deploys run as the app user and rewrite
|
|
369
|
+
* them): the directory and each existing file become the user's, 0700/0600 —
|
|
370
|
+
* through O_NOFOLLOW descriptors, so a link planted there is refused, never
|
|
371
|
+
* followed (`ln -sf /etc/sudoers /etc/<app>/api.env` changes nothing).
|
|
372
|
+
*
|
|
373
|
+
* The app name chooses the directory, so it may name one that is not the
|
|
374
|
+
* app's: install creates `/etc/<app>` or keeps it the user's; it never takes
|
|
375
|
+
* one a Debian package ships, one another user owns, or one with
|
|
376
|
+
* subdirectories, and it asks before taking one root made by hand.
|
|
377
|
+
*/
|
|
378
|
+
async function envOwnership(host, plan, owner) {
|
|
379
|
+
if (plan.envFiles.length === 0)
|
|
380
|
+
return;
|
|
381
|
+
host.out.step('env files outside the checkout');
|
|
382
|
+
const dir = `/etc/${plan.app}`;
|
|
383
|
+
await assertRootOwnedDir(host, '/etc', 'env files');
|
|
384
|
+
for (const file of plan.envFiles)
|
|
385
|
+
if (dirname(file) !== dir)
|
|
386
|
+
throw new DeployError(`refused: ${file} is not directly in ${dir}`);
|
|
387
|
+
const existing = await host.stat(dir);
|
|
388
|
+
if (existing) {
|
|
389
|
+
if ((await host.exec('dpkg-query', ['-S', dir])).code === 0)
|
|
390
|
+
throw new DeployError(`refused: ${dir} belongs to an installed package — it is not the app's to own; keep the env inside the checkout, or name the app something else`);
|
|
391
|
+
await refuseForeign(host, dir, owner, 'the env directory');
|
|
392
|
+
if (existing.kind === 'dir' && existing.uid === 0) {
|
|
393
|
+
const entries = await host.list(dir);
|
|
394
|
+
const kinds = await Promise.all(entries.map(async (name) => ({ name, stat: await host.stat(join(dir, name)) })));
|
|
395
|
+
const odd = kinds.filter((e) => e.stat?.kind !== 'file').map((e) => e.name);
|
|
396
|
+
if (odd.length > 0)
|
|
397
|
+
throw new DeployError(`refused: ${dir} holds ${odd.join(', ')} (not plain files) — an app's env directory holds env files only`);
|
|
398
|
+
host.out.warn(`${dir} exists and is root's${entries.length ? ` (${entries.join(', ')})` : ''}: install would give it and the env files to ${owner.name}`);
|
|
399
|
+
if (!(await host.confirm(`Give ${dir} to ${owner.name}?`)))
|
|
400
|
+
throw new DeployError(`${dir} not handed over (not confirmed) — nothing else was changed there (a terminal must answer)`);
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
else {
|
|
404
|
+
await host.mkdir(dir, 0o700);
|
|
405
|
+
}
|
|
406
|
+
await secure(host, dir, { uid: owner.uid, gid: owner.gid, mode: 0o700, kind: 'dir' });
|
|
407
|
+
for (const file of plan.envFiles) {
|
|
408
|
+
if (!(await host.stat(file))) {
|
|
409
|
+
host.out.info(`${file} does not exist yet — the first env or run creates it`);
|
|
410
|
+
continue;
|
|
411
|
+
}
|
|
412
|
+
await refuseForeign(host, file, owner, 'the env file');
|
|
413
|
+
await secure(host, file, { uid: owner.uid, gid: owner.gid, mode: 0o600, kind: 'file' });
|
|
414
|
+
}
|
|
415
|
+
host.out.ok(`${dir} and its env files (${plan.envFiles.map((f) => basename(f)).join(', ')}) owned by ${owner.name}, 0700/0600`);
|
|
416
|
+
}
|
|
417
|
+
/** A config root other than the package's default: shown, and written only after a "yes". */
|
|
418
|
+
async function confirmRoot(host, root, standard) {
|
|
419
|
+
if (root === standard)
|
|
420
|
+
return;
|
|
421
|
+
host.out.warn(`the plan asks to write proxy configuration under ${root}, not ${standard}`);
|
|
422
|
+
if (!(await host.confirm(`Write under ${root}?`)))
|
|
423
|
+
throw new DeployError(`proxy root ${root} not confirmed — nothing written (a terminal must answer)`);
|
|
424
|
+
}
|
|
425
|
+
/** Caddy `extra` is raw configuration root installs: shown in full, and written only after a "yes". */
|
|
426
|
+
async function confirmExtra(host, extra, site) {
|
|
427
|
+
host.out.info(`proxy.extra — raw Caddyfile appended to ${site}, as written by the app user (tabs shown as →):`);
|
|
428
|
+
// parseInstallPlan refused every character a terminal would hide text with.
|
|
429
|
+
for (const line of extra.trim().split('\n'))
|
|
430
|
+
host.out.info(` + ${line.replace(/\t/g, '→ ')}`);
|
|
431
|
+
if (!(await host.confirm('Install this raw configuration as root?')))
|
|
432
|
+
throw new DeployError('proxy.extra not confirmed — nothing written (a terminal must answer)');
|
|
433
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { type AnswerChannel } from './answers.js';
|
|
2
|
+
import type { DeployConfig } from './config.js';
|
|
3
|
+
import { type EventOutput, type EventWriter, type Redactor } from './events.js';
|
|
4
|
+
import { type Host } from './host.js';
|
|
5
|
+
/**
|
|
6
|
+
* The machine protocol's host (RFC 0008 §5.1): the real host, with
|
|
7
|
+
*
|
|
8
|
+
* - `out` turned into events;
|
|
9
|
+
* - `prompt` answered from `--answers=stdin` (never from a terminal);
|
|
10
|
+
* - `interactive()` false — stdin is the protocol, not a person;
|
|
11
|
+
* - streamed children piped and wrapped in `log` events, never passed
|
|
12
|
+
* through; an interactive child refused (it would read the answers);
|
|
13
|
+
* - the `.env` and every child's environment learnt by the redactor before
|
|
14
|
+
* anything can print their secrets.
|
|
15
|
+
*
|
|
16
|
+
* Files, the lock, the clock, TLS and HTTP are the real host's, unchanged.
|
|
17
|
+
*/
|
|
18
|
+
export interface JsonSession {
|
|
19
|
+
host: Host;
|
|
20
|
+
output: EventOutput;
|
|
21
|
+
/** The config is known: its `.env` files and which keys are secrets. */
|
|
22
|
+
watchEnv(config: DeployConfig, dir: string): void;
|
|
23
|
+
/** Learns the secrets among key/value pairs (`config` says which keys it declares secret); warns once for each one too short. */
|
|
24
|
+
learn(pairs: Iterable<[string, string]>, config?: DeployConfig): void;
|
|
25
|
+
/** Learns a secret answer (called by the answer channel the moment it parses one). */
|
|
26
|
+
secret(key: string, value: string): void;
|
|
27
|
+
/** Where the `.env` answers come from (`--answers=stdin`), once the config says what it asks. */
|
|
28
|
+
useAnswers(channel: AnswerChannel): void;
|
|
29
|
+
}
|
|
30
|
+
export declare function jsonHost(base: Host, writer: EventWriter, redactor: Redactor, options?: {
|
|
31
|
+
answerTimeoutMs?: number;
|
|
32
|
+
}): JsonSession;
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { basename, resolve } from 'node:path';
|
|
2
|
+
import { ANSWER_TIMEOUT_SECONDS } from './answers.js';
|
|
3
|
+
import { DeployError } from './context.js';
|
|
4
|
+
import { childSink, eventOutput, learnEnvText, learnSecrets, urlPasswords } from './events.js';
|
|
5
|
+
import { secondSignalMessage } from './host.js';
|
|
6
|
+
export function jsonHost(base, writer, redactor, options = {}) {
|
|
7
|
+
const output = eventOutput(writer);
|
|
8
|
+
const envFiles = new Set();
|
|
9
|
+
const attempts = new Map();
|
|
10
|
+
let config;
|
|
11
|
+
const timeout = options.answerTimeoutMs ?? ANSWER_TIMEOUT_SECONDS * 1000;
|
|
12
|
+
let channel;
|
|
13
|
+
/** A secret too short to redact is said once, by key (RFC 0008 §6.2). */
|
|
14
|
+
const warnShort = (keys) => {
|
|
15
|
+
for (const key of keys) {
|
|
16
|
+
output.finding({ id: 'secret-short', level: 'warning', message: `${key} is shorter than 6 characters: --json output cannot redact it`, fix: 'use a longer value (a generated one is 32+ characters)' });
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
const learnEnv = (text) => { if (text)
|
|
20
|
+
warnShort(learnEnvText(redactor, text, config)); };
|
|
21
|
+
const overrides = {
|
|
22
|
+
out: output,
|
|
23
|
+
interactive: () => false,
|
|
24
|
+
async readFile(path) {
|
|
25
|
+
const text = await base.readFile(path);
|
|
26
|
+
if (envFiles.has(path))
|
|
27
|
+
learnEnv(text);
|
|
28
|
+
return text;
|
|
29
|
+
},
|
|
30
|
+
async writeFile(path, content, mode) {
|
|
31
|
+
if (envFiles.has(path))
|
|
32
|
+
learnEnv(content);
|
|
33
|
+
return base.writeFile(path, content, mode);
|
|
34
|
+
},
|
|
35
|
+
async exec(command, args, execOptions = {}) {
|
|
36
|
+
if (execOptions.interactive) {
|
|
37
|
+
throw new DeployError(`${command} would read the terminal, and stdin is the protocol under --json`, [], 'unsupported');
|
|
38
|
+
}
|
|
39
|
+
if (execOptions.env)
|
|
40
|
+
warnShort(learnSecrets(redactor, Object.entries(execOptions.env), config));
|
|
41
|
+
if (!execOptions.stream)
|
|
42
|
+
return base.exec(command, args, execOptions);
|
|
43
|
+
const sink = childSink(writer, basename(command), output.current);
|
|
44
|
+
try {
|
|
45
|
+
return await base.exec(command, args, { ...execOptions, onOutput: (text, stream) => sink.push(text, stream) });
|
|
46
|
+
}
|
|
47
|
+
finally {
|
|
48
|
+
sink.end();
|
|
49
|
+
}
|
|
50
|
+
},
|
|
51
|
+
async prompt(question, { secret }, meta) {
|
|
52
|
+
const answers = channel;
|
|
53
|
+
if (!answers || !meta)
|
|
54
|
+
return undefined;
|
|
55
|
+
const attempt = (attempts.get(meta.id) ?? 0) + 1;
|
|
56
|
+
attempts.set(meta.id, attempt);
|
|
57
|
+
if (attempt > 1) {
|
|
58
|
+
if (!answers.live) {
|
|
59
|
+
throw new DeployError(`${meta.key}: the answer was refused (the warning above says why) — nothing was written`, [], 'answer-invalid', 'answers sent up front get no second chance: correct it and run again');
|
|
60
|
+
}
|
|
61
|
+
answers.expectAgain(meta.id);
|
|
62
|
+
}
|
|
63
|
+
writer.emit({
|
|
64
|
+
type: 'prompt', id: meta.id, kind: secret ? 'secret' : 'text', message: question, key: meta.key,
|
|
65
|
+
optional: meta.optional, attempt,
|
|
66
|
+
...(!secret && meta.default !== undefined ? { default: meta.default } : {}),
|
|
67
|
+
});
|
|
68
|
+
return (await answers.take(meta.id, timeout))?.trim();
|
|
69
|
+
},
|
|
70
|
+
// The second signal's line is an event like any other, never text on stderr.
|
|
71
|
+
onSignal(listener) {
|
|
72
|
+
return base.onSignal(listener, { second: (signal) => output.error(secondSignalMessage(signal)) });
|
|
73
|
+
},
|
|
74
|
+
async confirm(question) {
|
|
75
|
+
// No app-user command confirms; a confirmation is never assumed (RFC 0008 §5.1).
|
|
76
|
+
writer.emit({ type: 'prompt', id: 'confirm', kind: 'confirm', message: question, optional: true, attempt: 1 });
|
|
77
|
+
return false;
|
|
78
|
+
},
|
|
79
|
+
};
|
|
80
|
+
const host = new Proxy(base, {
|
|
81
|
+
get(target, property) {
|
|
82
|
+
if (Object.hasOwn(overrides, property))
|
|
83
|
+
return overrides[property];
|
|
84
|
+
// The real host's own members, called on the real host (`this` is never the proxy).
|
|
85
|
+
const value = Reflect.get(target, property, target);
|
|
86
|
+
return typeof value === 'function' ? value.bind(target) : value;
|
|
87
|
+
},
|
|
88
|
+
});
|
|
89
|
+
return {
|
|
90
|
+
host,
|
|
91
|
+
output,
|
|
92
|
+
useAnswers(next) { channel = next; },
|
|
93
|
+
learn(pairs, given) { warnShort(learnSecrets(redactor, pairs, given ?? config)); },
|
|
94
|
+
watchEnv(next, dir) {
|
|
95
|
+
config = next;
|
|
96
|
+
envFiles.add(resolve(dir, next.env.file ?? '.env'));
|
|
97
|
+
for (const mirror of next.env.mirror ?? [])
|
|
98
|
+
envFiles.add(resolve(dir, mirror));
|
|
99
|
+
},
|
|
100
|
+
secret(key, value) {
|
|
101
|
+
const short = [value, ...urlPasswords(value)].some((form) => redactor.add(key, form) === 'short');
|
|
102
|
+
if (short)
|
|
103
|
+
warnShort([key]);
|
|
104
|
+
},
|
|
105
|
+
};
|
|
106
|
+
}
|