@selvajs/cli 4.21.0-beta.9 → 4.21.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/package.json +3 -3
- package/src/checks/runtime.js +4 -1
- package/src/cli.js +13 -6
- package/src/commands/create.js +20 -3
- package/src/commands/migrate.js +7 -6
- package/src/commands/pm2.js +120 -11
- package/src/commands/setup-url.js +33 -0
- package/src/generated/config.js +63 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@selvajs/cli",
|
|
3
|
-
"version": "4.21.0
|
|
3
|
+
"version": "4.21.0",
|
|
4
4
|
"description": "Scaffold and operate a Selva white-label deployment. `npx @selvajs/cli <dir>` to bootstrap, `selva <cmd>` to manage.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "VektorNode",
|
|
@@ -37,7 +37,7 @@
|
|
|
37
37
|
"node": ">=24.0.0"
|
|
38
38
|
},
|
|
39
39
|
"dependencies": {
|
|
40
|
-
"@clack/prompts": "^1.
|
|
40
|
+
"@clack/prompts": "^1.9.0",
|
|
41
41
|
"picocolors": "^1.1.1"
|
|
42
42
|
},
|
|
43
43
|
"devDependencies": {
|
|
@@ -46,6 +46,6 @@
|
|
|
46
46
|
"scripts": {
|
|
47
47
|
"start": "node ./bin/cli.js",
|
|
48
48
|
"selva": "node ./bin/selva.js",
|
|
49
|
-
"test": "node --check ./bin/cli.js && node --check ./bin/selva.js && node --input-type=module -e \"for (const m of ['./src/cli.js','./src/prompts.js','./src/env.js','./src/paths.js','./src/secrets.js','./src/node-range.js','./src/deployment-package.js','./src/commands/create.js','./src/commands/init.js','./src/commands/doctor.js','./src/commands/pm2.js','./src/commands/migrate.js','./src/commands/keys.js','./src/commands/config.js','./src/env-migration.js']) { await import(m); }\" && node --test \"./src/__tests__/*.test.js\""
|
|
49
|
+
"test": "node --check ./bin/cli.js && node --check ./bin/selva.js && node --input-type=module -e \"for (const m of ['./src/cli.js','./src/prompts.js','./src/env.js','./src/paths.js','./src/secrets.js','./src/node-range.js','./src/deployment-package.js','./src/commands/create.js','./src/commands/init.js','./src/commands/doctor.js','./src/commands/pm2.js','./src/commands/migrate.js','./src/commands/keys.js','./src/commands/config.js','./src/commands/setup-url.js','./src/env-migration.js']) { await import(m); }\" && node --test \"./src/__tests__/*.test.js\""
|
|
50
50
|
}
|
|
51
51
|
}
|
package/src/checks/runtime.js
CHANGED
|
@@ -17,7 +17,7 @@ import { join } from 'node:path';
|
|
|
17
17
|
import { homedir } from 'node:os';
|
|
18
18
|
import { fixable, green, red, yellow } from './result.js';
|
|
19
19
|
import { PM2_VERSION } from '../deployment-package.js';
|
|
20
|
-
import {
|
|
20
|
+
import { appName, foreignOwner, nameTakenMessage } from '../commands/pm2.js';
|
|
21
21
|
|
|
22
22
|
/** The real OS. Tests pass a stand-in with the same shape. */
|
|
23
23
|
export const runtimeEnv = {
|
|
@@ -294,6 +294,9 @@ function checkAppProcess(bin, dir, env) {
|
|
|
294
294
|
} catch {
|
|
295
295
|
return yellow(`could not read the pm2 process list — check \`npx pm2 list\` by hand`);
|
|
296
296
|
}
|
|
297
|
+
const APP_NAME = appName(dir);
|
|
298
|
+
const owner = foreignOwner(list, APP_NAME, dir, env.realpath);
|
|
299
|
+
if (owner) return red(nameTakenMessage(APP_NAME, owner, dir));
|
|
297
300
|
const app = list.find((proc) => proc.name === APP_NAME);
|
|
298
301
|
const status = app?.pm2_env?.status;
|
|
299
302
|
if (status === 'online') return green(`${APP_NAME} is online`);
|
package/src/cli.js
CHANGED
|
@@ -16,6 +16,7 @@ const COMMANDS = {
|
|
|
16
16
|
update: () => import('./commands/pm2.js').then((m) => m.runUpdate),
|
|
17
17
|
migrate: () => import('./commands/migrate.js').then((m) => m.runMigrate),
|
|
18
18
|
'setup-proxy': () => import('./commands/proxy.js').then((m) => m.runSetupProxy),
|
|
19
|
+
'setup-url': () => import('./commands/setup-url.js').then((m) => m.runSetupUrl),
|
|
19
20
|
keys: () => import('./commands/keys.js').then((m) => keysDispatch(m)),
|
|
20
21
|
config: () => import('./commands/config.js').then((m) => m.runConfig)
|
|
21
22
|
};
|
|
@@ -37,13 +38,13 @@ const USAGE = {
|
|
|
37
38
|
flags: [['--fix', 'Apply the repairs doctor reports as automatic. Prompts before each.']]
|
|
38
39
|
},
|
|
39
40
|
start: { usage: 'selva start', blurb: 'pm2 start ecosystem.config.cjs' },
|
|
40
|
-
stop: { usage: 'selva stop', blurb:
|
|
41
|
+
stop: { usage: 'selva stop', blurb: "pm2 stop on this install's app" },
|
|
41
42
|
restart: {
|
|
42
43
|
usage: 'selva restart',
|
|
43
44
|
blurb:
|
|
44
|
-
|
|
45
|
+
"pm2 restart --update-env on this install's app. The --update-env is why this\nexists: without it a pm2 restart keeps the old .env in memory."
|
|
45
46
|
},
|
|
46
|
-
logs: { usage: 'selva logs', blurb:
|
|
47
|
+
logs: { usage: 'selva logs', blurb: "pm2 logs for this install's app" },
|
|
47
48
|
update: {
|
|
48
49
|
usage: 'selva update',
|
|
49
50
|
blurb:
|
|
@@ -67,6 +68,11 @@ const USAGE = {
|
|
|
67
68
|
['--acme-email', "Address Let's Encrypt sends expiry notices to."]
|
|
68
69
|
]
|
|
69
70
|
},
|
|
71
|
+
'setup-url': {
|
|
72
|
+
usage: 'selva setup-url',
|
|
73
|
+
blurb:
|
|
74
|
+
'Print the link that opens /setup with its token. The password form needs it\nto create the first admin; it stops working once an admin exists.'
|
|
75
|
+
},
|
|
70
76
|
config: {
|
|
71
77
|
usage: 'selva config <show [--all] | set KEY=VALUE | unset KEY>',
|
|
72
78
|
blurb:
|
|
@@ -149,12 +155,13 @@ function printHelp() {
|
|
|
149
155
|
' init Reconfigure this deployment (prompts again)',
|
|
150
156
|
' doctor [--fix] Validate env, providers, Node engine, and packages',
|
|
151
157
|
' start pm2 start ecosystem.config.cjs',
|
|
152
|
-
' stop pm2 stop
|
|
153
|
-
' restart pm2 restart
|
|
154
|
-
' logs pm2 logs
|
|
158
|
+
' stop pm2 stop',
|
|
159
|
+
' restart pm2 restart --update-env',
|
|
160
|
+
' logs pm2 logs',
|
|
155
161
|
' update npm update @selvajs/cli + @selvajs/selva, then restart',
|
|
156
162
|
' migrate Bring package.json onto the current layout',
|
|
157
163
|
' setup-proxy Install + configure Caddy with TLS for this deployment',
|
|
164
|
+
' setup-url Print the link that creates the first admin',
|
|
158
165
|
' config show|set|unset What .env sets; change one key safely',
|
|
159
166
|
' keys rotate <hmac|at-rest> Rotate a secret; the old one keeps working',
|
|
160
167
|
'',
|
package/src/commands/create.js
CHANGED
|
@@ -9,6 +9,7 @@ import pc from 'picocolors';
|
|
|
9
9
|
import { collectConfig, collectConfigFromEnv } from '../prompts.js';
|
|
10
10
|
import { generateKey } from '../secrets.js';
|
|
11
11
|
import { writeEnvFile } from '../env.js';
|
|
12
|
+
import { setupUrlFor } from './setup-url.js';
|
|
12
13
|
import { isEmptyOrMissing, requiredNodeRange, runtimeTemplatePath } from '../paths.js';
|
|
13
14
|
import {
|
|
14
15
|
buildDeploymentPackageJson,
|
|
@@ -151,15 +152,31 @@ function nextSteps({ rawDir, values, supabase }) {
|
|
|
151
152
|
|
|
152
153
|
lines.push(
|
|
153
154
|
'',
|
|
154
|
-
values
|
|
155
|
-
? `Then visit ${pc.cyan(values.ORIGIN)} and open ${pc.cyan('/setup')} to claim admin.`
|
|
156
|
-
: `Then visit ${pc.cyan('http://localhost:3000')} and open ${pc.cyan('/setup')} to claim admin.`,
|
|
155
|
+
...claimAdminLines(values),
|
|
157
156
|
'',
|
|
158
157
|
pc.dim('`npm run doctor --fix` repairs what it can, including pm2 boot persistence.')
|
|
159
158
|
);
|
|
160
159
|
return lines;
|
|
161
160
|
}
|
|
162
161
|
|
|
162
|
+
/**
|
|
163
|
+
* A password form cannot prove the email typed into it, so it takes the token
|
|
164
|
+
* in this link. Proxy sign-in proves the email, and has no form.
|
|
165
|
+
*/
|
|
166
|
+
function claimAdminLines(values) {
|
|
167
|
+
if (values.SELVA_AUTH_PROVIDER === 'header') {
|
|
168
|
+
return [
|
|
169
|
+
`Then sign in through your proxy as ${pc.cyan(values.BOOTSTRAP_INSTANCE_ADMIN_EMAIL ?? 'the admin')} to claim admin.`
|
|
170
|
+
];
|
|
171
|
+
}
|
|
172
|
+
const { url } = setupUrlFor(values);
|
|
173
|
+
return [
|
|
174
|
+
'Then open the setup link to claim admin:',
|
|
175
|
+
` ${pc.cyan(url)}`,
|
|
176
|
+
pc.dim(' `npx selva setup-url` prints it again. It stops working once an admin exists.')
|
|
177
|
+
];
|
|
178
|
+
}
|
|
179
|
+
|
|
163
180
|
function targetDirLabel(rawDir) {
|
|
164
181
|
return resolve(rawDir);
|
|
165
182
|
}
|
package/src/commands/migrate.js
CHANGED
|
@@ -30,7 +30,7 @@ import {
|
|
|
30
30
|
resolveSelvaPins,
|
|
31
31
|
SELVA_PACKAGES
|
|
32
32
|
} from '../deployment-package.js';
|
|
33
|
-
import {
|
|
33
|
+
import { appName, requireOwnApp, runPm2 } from './pm2.js';
|
|
34
34
|
|
|
35
35
|
// Where migrate parks the old node_modules while npm installs the new one.
|
|
36
36
|
// Left behind only if the process is killed mid-migration; `selva doctor`
|
|
@@ -243,10 +243,11 @@ export async function runMigrate(
|
|
|
243
243
|
// under a running server serves a broken page (same reason `update` stops first).
|
|
244
244
|
// A legacy deployment may predate the local-pm2 layout, so a missing binary
|
|
245
245
|
// isn't fatal here: there's nothing running for us to stop.
|
|
246
|
+
const APP_NAME = requireOwnApp(dir);
|
|
246
247
|
try {
|
|
247
248
|
const stopStatus = pm2(dir, ['stop', APP_NAME], { inherit: false });
|
|
248
249
|
if (stopStatus !== 0) {
|
|
249
|
-
p.log.warn(
|
|
250
|
+
p.log.warn(`pm2 stop did not succeed — ${APP_NAME} may not be running. Continuing.`);
|
|
250
251
|
}
|
|
251
252
|
} catch {
|
|
252
253
|
p.log.warn('No deployment-local pm2 to stop — continuing.');
|
|
@@ -346,14 +347,14 @@ export async function runMigrate(
|
|
|
346
347
|
// the operator's problem to act on, so it gets logged, not swallowed.
|
|
347
348
|
function restartAfterRollback(dir, pm2 = runPm2) {
|
|
348
349
|
try {
|
|
349
|
-
const status = pm2(dir, ['start',
|
|
350
|
+
const status = pm2(dir, ['start', appName(dir), '--update-env'], { inherit: false });
|
|
350
351
|
if (status === 0) {
|
|
351
|
-
p.log.success(`Rolled back and restarted ${
|
|
352
|
+
p.log.success(`Rolled back and restarted ${appName(dir)}.`);
|
|
352
353
|
return;
|
|
353
354
|
}
|
|
354
355
|
p.log.error(
|
|
355
356
|
`Rolled back, but \`pm2 start\` exited ${status}. The app may be down — ` +
|
|
356
|
-
`check \`pm2 logs ${
|
|
357
|
+
`check \`pm2 logs ${appName(dir)}\` and start it with \`selva start\`.`
|
|
357
358
|
);
|
|
358
359
|
} catch (err) {
|
|
359
360
|
p.log.error(
|
|
@@ -506,7 +507,7 @@ export function buildPm2UpgradeNotice(beforeVersion, afterVersion) {
|
|
|
506
507
|
`still the old version. Finish the upgrade (brief restart of managed processes):`,
|
|
507
508
|
``,
|
|
508
509
|
` npx pm2 update`,
|
|
509
|
-
` npx pm2 list #
|
|
510
|
+
` npx pm2 list # the app must be listed and 'online' before the next step`,
|
|
510
511
|
` npx pm2 save`,
|
|
511
512
|
``,
|
|
512
513
|
`Do not run \`pm2 save\` until the list looks right. \`pm2 update\` empties the`,
|
package/src/commands/pm2.js
CHANGED
|
@@ -2,7 +2,8 @@
|
|
|
2
2
|
// the daemon before state changes, and pass --update-env on restart so edits to
|
|
3
3
|
// .env actually take effect.
|
|
4
4
|
|
|
5
|
-
import { existsSync, readFileSync } from 'node:fs';
|
|
5
|
+
import { existsSync, readFileSync, realpathSync } from 'node:fs';
|
|
6
|
+
import { createRequire } from 'node:module';
|
|
6
7
|
import { join } from 'node:path';
|
|
7
8
|
import { spawnSync, execSync } from 'node:child_process';
|
|
8
9
|
import * as p from '@clack/prompts';
|
|
@@ -12,7 +13,77 @@ import { readEnvFile } from '../env.js';
|
|
|
12
13
|
import { needsSupabaseProvider } from '../deployment-package.js';
|
|
13
14
|
import { checkSupabaseMigrations, providerTagFor } from '../checks/migrations.js';
|
|
14
15
|
|
|
15
|
-
|
|
16
|
+
const require = createRequire(import.meta.url);
|
|
17
|
+
const DEFAULT_APP_NAME = 'selva-compute';
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* The pm2 app name this install's ecosystem file declares. pm2 finds an app by name
|
|
21
|
+
* alone, so two installs under one daemon need different ones.
|
|
22
|
+
*/
|
|
23
|
+
export function appName(dir) {
|
|
24
|
+
try {
|
|
25
|
+
const name = require(join(dir, 'ecosystem.config.cjs'))?.apps?.[0]?.name;
|
|
26
|
+
if (typeof name === 'string' && name) return name;
|
|
27
|
+
} catch {
|
|
28
|
+
// No ecosystem file, or one that doesn't load: pm2 reports that itself.
|
|
29
|
+
}
|
|
30
|
+
return DEFAULT_APP_NAME;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* The directory pm2 runs `name` from when that is not `dir`, else null. `list` is
|
|
35
|
+
* `pm2 jlist` parsed.
|
|
36
|
+
*/
|
|
37
|
+
export function foreignOwner(list, name, dir, realpath = realpathSync) {
|
|
38
|
+
const owner = list.find((proc) => proc.name === name)?.pm2_env?.pm_cwd;
|
|
39
|
+
if (!owner) return null;
|
|
40
|
+
const real = (path) => {
|
|
41
|
+
try {
|
|
42
|
+
return realpath(path);
|
|
43
|
+
} catch {
|
|
44
|
+
return path;
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
return real(owner) === real(dir) ? null : owner;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function nameTakenMessage(name, owner, dir) {
|
|
51
|
+
return (
|
|
52
|
+
`pm2's '${name}' runs from ${owner}, not from this install (${dir}). Two installs share ` +
|
|
53
|
+
`one pm2 app name, and pm2 finds an app by name alone: a stop here would take the other ` +
|
|
54
|
+
`install down, and a start would restart it instead of starting this one. Give this ` +
|
|
55
|
+
`install its own \`name\` in ${join(dir, 'ecosystem.config.cjs')}, then run \`npx selva start\`.`
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// pm2's process list, or null when there is no local pm2 or its output is unreadable.
|
|
60
|
+
function pm2List(dir) {
|
|
61
|
+
try {
|
|
62
|
+
const out = spawnSync(pm2Bin(dir), ['jlist'], {
|
|
63
|
+
cwd: dir,
|
|
64
|
+
encoding: 'utf8',
|
|
65
|
+
shell: process.platform === 'win32'
|
|
66
|
+
});
|
|
67
|
+
// A skewed daemon prints its out-of-date banner ahead of the JSON.
|
|
68
|
+
const json = String(out.stdout ?? '')
|
|
69
|
+
.split('\n')
|
|
70
|
+
.find((line) => line.trimStart().startsWith('['));
|
|
71
|
+
return JSON.parse(json ?? '');
|
|
72
|
+
} catch {
|
|
73
|
+
return null;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* This install's pm2 app name. Throws when pm2's app of that name belongs to another
|
|
79
|
+
* install.
|
|
80
|
+
*/
|
|
81
|
+
export function requireOwnApp(dir, list = pm2List(dir)) {
|
|
82
|
+
const name = appName(dir);
|
|
83
|
+
const owner = foreignOwner(list ?? [], name, dir);
|
|
84
|
+
if (owner) throw new Error(nameTakenMessage(name, owner, dir));
|
|
85
|
+
return name;
|
|
86
|
+
}
|
|
16
87
|
|
|
17
88
|
export function pm2Bin(dir) {
|
|
18
89
|
const local = join(dir, 'node_modules', '.bin', process.platform === 'win32' ? 'pm2.cmd' : 'pm2');
|
|
@@ -68,7 +139,7 @@ function ensurePm2InSync(dir) {
|
|
|
68
139
|
throw new Error(
|
|
69
140
|
`The running PM2 daemon (v${daemon}) is NEWER than this deployment's pm2 (v${local}), ` +
|
|
70
141
|
`so a global pm2 owns it. Running \`pm2 update\` would downgrade the daemon and drop ` +
|
|
71
|
-
`its process table, leaving ${
|
|
142
|
+
`its process table, leaving ${appName(dir)} unregistered. Resolve the conflict first: ` +
|
|
72
143
|
`\`which -a pm2\`, \`pm2 -v\`, \`pm2 ping\`.`
|
|
73
144
|
);
|
|
74
145
|
}
|
|
@@ -112,6 +183,7 @@ export async function runStart() {
|
|
|
112
183
|
const dir = resolveDeploymentDir();
|
|
113
184
|
requireDeploymentDir(dir);
|
|
114
185
|
ensurePm2InSync(dir);
|
|
186
|
+
requireOwnApp(dir);
|
|
115
187
|
const exit = runPm2(dir, ['start', 'ecosystem.config.cjs']);
|
|
116
188
|
process.exit(exit);
|
|
117
189
|
}
|
|
@@ -120,7 +192,7 @@ export async function runStop() {
|
|
|
120
192
|
const dir = resolveDeploymentDir();
|
|
121
193
|
requireDeploymentDir(dir);
|
|
122
194
|
ensurePm2InSync(dir);
|
|
123
|
-
const exit = runPm2(dir, ['stop',
|
|
195
|
+
const exit = runPm2(dir, ['stop', requireOwnApp(dir)]);
|
|
124
196
|
process.exit(exit);
|
|
125
197
|
}
|
|
126
198
|
|
|
@@ -130,14 +202,14 @@ export async function runRestart() {
|
|
|
130
202
|
ensurePm2InSync(dir);
|
|
131
203
|
// --update-env is the whole point of this wrapper — without it, edits to
|
|
132
204
|
// .env have no effect on the running process.
|
|
133
|
-
const exit = runPm2(dir, ['restart',
|
|
205
|
+
const exit = runPm2(dir, ['restart', requireOwnApp(dir), '--update-env']);
|
|
134
206
|
process.exit(exit);
|
|
135
207
|
}
|
|
136
208
|
|
|
137
209
|
export async function runLogs(argv) {
|
|
138
210
|
const dir = resolveDeploymentDir();
|
|
139
211
|
requireDeploymentDir(dir);
|
|
140
|
-
const exit = runPm2(dir, ['logs',
|
|
212
|
+
const exit = runPm2(dir, ['logs', appName(dir), ...argv]);
|
|
141
213
|
process.exit(exit);
|
|
142
214
|
}
|
|
143
215
|
|
|
@@ -165,10 +237,27 @@ export async function runUpdate() {
|
|
|
165
237
|
}
|
|
166
238
|
|
|
167
239
|
ensurePm2InSync(dir);
|
|
240
|
+
const APP_NAME = requireOwnApp(dir);
|
|
168
241
|
|
|
169
242
|
const stopStatus = runPm2(dir, ['stop', APP_NAME], { inherit: false });
|
|
170
243
|
if (stopStatus !== 0) {
|
|
171
|
-
p.log.warn(
|
|
244
|
+
p.log.warn(`pm2 stop did not succeed — ${APP_NAME} may not be running. Continuing.`);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
// Nothing may answer the port once the app is stopped. A process this pm2 does not
|
|
248
|
+
// run would keep serving the old build from replaced files, and would answer the
|
|
249
|
+
// health check below as if it were the new version.
|
|
250
|
+
if (await stillAnswering(env)) {
|
|
251
|
+
runPm2(dir, ['start', APP_NAME, '--update-env'], { inherit: false });
|
|
252
|
+
p.outro(
|
|
253
|
+
pc.red(
|
|
254
|
+
`Something still answers on port ${env.PORT || 3000} after \`pm2 stop ${APP_NAME}\`, so nothing was installed.\n` +
|
|
255
|
+
`Either this install's pm2 does not run that process, or it did not exit. Find what holds\n` +
|
|
256
|
+
'the port (`ss -ltnp`), stop it there, run `npx selva start`, then update again.'
|
|
257
|
+
)
|
|
258
|
+
);
|
|
259
|
+
process.exitCode = 1;
|
|
260
|
+
return;
|
|
172
261
|
}
|
|
173
262
|
|
|
174
263
|
const s = p.spinner();
|
|
@@ -294,10 +383,30 @@ function realignPm2Pin(dir, s) {
|
|
|
294
383
|
}
|
|
295
384
|
}
|
|
296
385
|
|
|
297
|
-
|
|
298
|
-
async function waitForHealth(env, timeoutMs = 30_000) {
|
|
386
|
+
function healthUrl(env) {
|
|
299
387
|
const host = !env.HOST || env.HOST === '0.0.0.0' || env.HOST === '::' ? '127.0.0.1' : env.HOST;
|
|
300
|
-
|
|
388
|
+
return `http://${host.includes(':') ? `[${host}]` : host}:${env.PORT || 3000}/api/health`;
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
/**
|
|
392
|
+
* True when the port still answers `seconds` after the stop. pm2 gives the app its kill
|
|
393
|
+
* timeout (10 s in the scaffold) to drain, so only a refused connection counts as stopped.
|
|
394
|
+
*/
|
|
395
|
+
export async function stillAnswering(env, seconds = 15, probe = fetch, pause = 1000) {
|
|
396
|
+
for (let i = 0; i < seconds; i++) {
|
|
397
|
+
try {
|
|
398
|
+
await probe(healthUrl(env), { signal: AbortSignal.timeout(3000) });
|
|
399
|
+
} catch {
|
|
400
|
+
return false;
|
|
401
|
+
}
|
|
402
|
+
await new Promise((r) => setTimeout(r, pause));
|
|
403
|
+
}
|
|
404
|
+
return true;
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
// `stillAnswering` saw the port go silent, so the first answer is the new process.
|
|
408
|
+
async function waitForHealth(env, timeoutMs = 30_000) {
|
|
409
|
+
const url = healthUrl(env);
|
|
301
410
|
const deadline = Date.now() + timeoutMs;
|
|
302
411
|
while (Date.now() < deadline) {
|
|
303
412
|
try {
|
|
@@ -330,7 +439,7 @@ export function degradedReason(body) {
|
|
|
330
439
|
}
|
|
331
440
|
return 'stored compute API keys cannot be decrypted under the current SELVA_AT_REST_KEY';
|
|
332
441
|
}
|
|
333
|
-
return 'see `
|
|
442
|
+
return 'see `npx selva logs`';
|
|
334
443
|
}
|
|
335
444
|
|
|
336
445
|
function readRuntimeVersion(dir) {
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
// `selva setup-url`: the link that claims an instance with no admin yet.
|
|
2
|
+
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
import pc from 'picocolors';
|
|
5
|
+
import { readEnvFile } from '../env.js';
|
|
6
|
+
import { requireDeploymentDir, resolveDeploymentDir } from '../paths.js';
|
|
7
|
+
import { setupUrl } from '../generated/config.js';
|
|
8
|
+
|
|
9
|
+
/** The link for this deployment's `.env`, or an error naming what is missing. */
|
|
10
|
+
export function setupUrlFor(env) {
|
|
11
|
+
const key = env.SELVA_HMAC_KEY?.trim();
|
|
12
|
+
if (!key) return { error: 'SELVA_HMAC_KEY is not set in .env. Run `selva doctor`.' };
|
|
13
|
+
const origin = env.ORIGIN?.trim() || `http://localhost:${env.PORT?.trim() || '3000'}`;
|
|
14
|
+
return { url: setupUrl(origin, key) };
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export async function runSetupUrl() {
|
|
18
|
+
const dir = resolveDeploymentDir();
|
|
19
|
+
requireDeploymentDir(dir);
|
|
20
|
+
const result = setupUrlFor(readEnvFile(join(dir, '.env')));
|
|
21
|
+
if (result.error) {
|
|
22
|
+
console.error(pc.red(result.error));
|
|
23
|
+
process.exit(1);
|
|
24
|
+
}
|
|
25
|
+
console.log(
|
|
26
|
+
[
|
|
27
|
+
result.url,
|
|
28
|
+
'',
|
|
29
|
+
pc.dim('Works only while the instance has no admin. Anyone holding it can claim'),
|
|
30
|
+
pc.dim('the instance until then, so do not paste it into a chat or a ticket.')
|
|
31
|
+
].join('\n')
|
|
32
|
+
);
|
|
33
|
+
}
|
package/src/generated/config.js
CHANGED
|
@@ -480,6 +480,19 @@ function parseDeadline(text) {
|
|
|
480
480
|
Math.floor(Number(`0${fraction}`) * 1e3)
|
|
481
481
|
);
|
|
482
482
|
}
|
|
483
|
+
// ../platform/src/config/setup-token.ts
|
|
484
|
+
/**
|
|
485
|
+
* The secret in the first-run setup link. Derived from `SELVA_HMAC_KEY`, so
|
|
486
|
+
* claiming an instance that has no admin takes read access to its `.env`.
|
|
487
|
+
* Stateless: every instance and the CLI compute the same value.
|
|
488
|
+
*/
|
|
489
|
+
function setupToken(hmacKey) {
|
|
490
|
+
return createHmac('sha256', hmacKey.trim()).update('selva:setup:v1').digest('base64url');
|
|
491
|
+
}
|
|
492
|
+
/** Where the operator claims the instance. `origin` has no trailing slash. */
|
|
493
|
+
function setupUrl(origin, hmacKey) {
|
|
494
|
+
return `${origin.replace(/\/+$/, '')}/setup?token=${setupToken(hmacKey)}`;
|
|
495
|
+
}
|
|
483
496
|
// ../server/src/config/settings.ts
|
|
484
497
|
var flag = (key, label, description, impact) =>
|
|
485
498
|
defineSetting({
|
|
@@ -584,6 +597,34 @@ var COMPUTE_LIMIT_SETTINGS = composeManifest([
|
|
|
584
597
|
description: 'Ask Rhino.Compute to cache solve results server-side.'
|
|
585
598
|
})
|
|
586
599
|
]);
|
|
600
|
+
var retentionDays = (key, label, what, impact) =>
|
|
601
|
+
defineSetting({
|
|
602
|
+
key,
|
|
603
|
+
label,
|
|
604
|
+
type: 'int',
|
|
605
|
+
min: 0,
|
|
606
|
+
default: 0,
|
|
607
|
+
onInvalid: 'fail',
|
|
608
|
+
class: 'setting',
|
|
609
|
+
zero: 'Forever',
|
|
610
|
+
unit: 'days',
|
|
611
|
+
description: `Days ${what} are kept before the daily purge deletes them. 0 keeps them forever.`,
|
|
612
|
+
impact
|
|
613
|
+
});
|
|
614
|
+
var RETENTION_SETTINGS = composeManifest([
|
|
615
|
+
retentionDays(
|
|
616
|
+
'SELVA_AUDIT_RETENTION_DAYS',
|
|
617
|
+
'Keep audit log for',
|
|
618
|
+
'audit events',
|
|
619
|
+
'Older events are gone for good, including who granted instance admin.'
|
|
620
|
+
),
|
|
621
|
+
retentionDays(
|
|
622
|
+
'SELVA_SOLVE_METRICS_RETENTION_DAYS',
|
|
623
|
+
'Keep solve metrics for',
|
|
624
|
+
'solve metrics',
|
|
625
|
+
'Compute usage charts cannot reach back further than this.'
|
|
626
|
+
)
|
|
627
|
+
]);
|
|
587
628
|
var API_TOKEN_RATE_LIMIT_SETTING = tunableInt(
|
|
588
629
|
'API_TOKEN_RATE_LIMIT_MAX',
|
|
589
630
|
600,
|
|
@@ -828,7 +869,8 @@ var ENGINE_SETTINGS = composeManifest([
|
|
|
828
869
|
legacy: { bool: 'exact-true' }
|
|
829
870
|
}),
|
|
830
871
|
COMPUTE_LIMIT_SETTINGS,
|
|
831
|
-
API_TOKEN_RATE_LIMIT_SETTING
|
|
872
|
+
API_TOKEN_RATE_LIMIT_SETTING,
|
|
873
|
+
RETENTION_SETTINGS
|
|
832
874
|
]);
|
|
833
875
|
// ../providers/header-auth/src/config.ts
|
|
834
876
|
var header = (key, fallback, slot) =>
|
|
@@ -1092,6 +1134,15 @@ var APP_SETTINGS = composeManifest([
|
|
|
1092
1134
|
description:
|
|
1093
1135
|
'Deployment directory the admin updater runs in. Defaults to the working directory.'
|
|
1094
1136
|
}),
|
|
1137
|
+
defineSetting({
|
|
1138
|
+
key: 'NPM_CONFIG_REGISTRY',
|
|
1139
|
+
label: 'npm registry',
|
|
1140
|
+
type: 'url',
|
|
1141
|
+
onInvalid: 'fail',
|
|
1142
|
+
class: 'bootstrap',
|
|
1143
|
+
description:
|
|
1144
|
+
'Registry updates are checked against and installed from. Defaults to registry.npmjs.org.'
|
|
1145
|
+
}),
|
|
1095
1146
|
defineSetting({
|
|
1096
1147
|
key: 'SELVA_EVENT_CALLBACK_ORIGIN',
|
|
1097
1148
|
label: 'Solve event callback address',
|
|
@@ -1111,6 +1162,16 @@ var APP_SETTINGS = composeManifest([
|
|
|
1111
1162
|
class: 'setting',
|
|
1112
1163
|
description: 'Browser origins /mcp accepts besides its own.'
|
|
1113
1164
|
}),
|
|
1165
|
+
defineSetting({
|
|
1166
|
+
key: 'MCP_PREVIEW_BROWSER',
|
|
1167
|
+
label: 'MCP preview browser',
|
|
1168
|
+
type: 'string',
|
|
1169
|
+
default: '',
|
|
1170
|
+
onInvalid: 'default',
|
|
1171
|
+
class: 'setting',
|
|
1172
|
+
description:
|
|
1173
|
+
"Chromium or Chrome executable that selva_capture_preview renders with. Empty: Playwright's own Chromium, else an installed Chrome, Edge or distro Chromium."
|
|
1174
|
+
}),
|
|
1114
1175
|
defineSetting({
|
|
1115
1176
|
key: 'LOG_LEVEL',
|
|
1116
1177
|
label: 'Log level',
|
|
@@ -1260,5 +1321,6 @@ export {
|
|
|
1260
1321
|
keyRotationStatus,
|
|
1261
1322
|
legacyValue,
|
|
1262
1323
|
resolveConfig,
|
|
1324
|
+
setupUrl,
|
|
1263
1325
|
takeSnapshot
|
|
1264
1326
|
};
|