@selvajs/cli 4.21.0-beta.14 → 4.21.0-beta.16
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 +1 -1
- package/src/checks/runtime.js +4 -1
- package/src/cli.js +6 -6
- package/src/commands/migrate.js +7 -6
- package/src/commands/pm2.js +120 -11
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@selvajs/cli",
|
|
3
|
-
"version": "4.21.0-beta.
|
|
3
|
+
"version": "4.21.0-beta.16",
|
|
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",
|
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
|
@@ -38,13 +38,13 @@ const USAGE = {
|
|
|
38
38
|
flags: [['--fix', 'Apply the repairs doctor reports as automatic. Prompts before each.']]
|
|
39
39
|
},
|
|
40
40
|
start: { usage: 'selva start', blurb: 'pm2 start ecosystem.config.cjs' },
|
|
41
|
-
stop: { usage: 'selva stop', blurb:
|
|
41
|
+
stop: { usage: 'selva stop', blurb: "pm2 stop on this install's app" },
|
|
42
42
|
restart: {
|
|
43
43
|
usage: 'selva restart',
|
|
44
44
|
blurb:
|
|
45
|
-
|
|
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."
|
|
46
46
|
},
|
|
47
|
-
logs: { usage: 'selva logs', blurb:
|
|
47
|
+
logs: { usage: 'selva logs', blurb: "pm2 logs for this install's app" },
|
|
48
48
|
update: {
|
|
49
49
|
usage: 'selva update',
|
|
50
50
|
blurb:
|
|
@@ -155,9 +155,9 @@ function printHelp() {
|
|
|
155
155
|
' init Reconfigure this deployment (prompts again)',
|
|
156
156
|
' doctor [--fix] Validate env, providers, Node engine, and packages',
|
|
157
157
|
' start pm2 start ecosystem.config.cjs',
|
|
158
|
-
' stop pm2 stop
|
|
159
|
-
' restart pm2 restart
|
|
160
|
-
' logs pm2 logs
|
|
158
|
+
' stop pm2 stop',
|
|
159
|
+
' restart pm2 restart --update-env',
|
|
160
|
+
' logs pm2 logs',
|
|
161
161
|
' update npm update @selvajs/cli + @selvajs/selva, then restart',
|
|
162
162
|
' migrate Bring package.json onto the current layout',
|
|
163
163
|
' setup-proxy Install + configure Caddy with TLS for this deployment',
|
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) {
|