easy-devops 1.3.0 → 1.3.2
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/dist/ui/assets/index-CMRzkIKj.css +1 -0
- package/dist/ui/assets/index-GEcisbty.js +453 -0
- package/dist/ui/index.html +2 -2
- package/package.json +1 -1
- package/src/cli/menus/update.js +19 -0
- package/src/core/deploy/apps.js +17 -6
- package/src/core/deploy/engine.js +257 -5
- package/src/core/deploy/ezfile.js +87 -5
- package/src/core/deploy/hubCommands.js +6 -2
- package/src/core/deploy/store.js +29 -3
- package/src/core/probeCache.js +5 -1
- package/src/dashboard/controllers/deploymentsController.js +10 -2
- package/src/dashboard/controllers/domainsController.js +43 -11
- package/src/dashboard/controllers/sslController.js +117 -0
- package/src/dashboard/routes/ssl.js +1 -0
- package/dist/ui/assets/index-B6zdwEnG.css +0 -1
- package/dist/ui/assets/index-Cy0oNmul.js +0 -442
package/dist/ui/index.html
CHANGED
|
@@ -62,8 +62,8 @@
|
|
|
62
62
|
}
|
|
63
63
|
})();
|
|
64
64
|
</script>
|
|
65
|
-
<script type="module" crossorigin src="./assets/index-
|
|
66
|
-
<link rel="stylesheet" crossorigin href="./assets/index-
|
|
65
|
+
<script type="module" crossorigin src="./assets/index-GEcisbty.js"></script>
|
|
66
|
+
<link rel="stylesheet" crossorigin href="./assets/index-CMRzkIKj.css">
|
|
67
67
|
</head>
|
|
68
68
|
<body>
|
|
69
69
|
<div id="root"></div>
|
package/package.json
CHANGED
package/src/cli/menus/update.js
CHANGED
|
@@ -23,6 +23,25 @@ import ora from 'ora';
|
|
|
23
23
|
import { dbGet, dbSet } from '../../core/db.js';
|
|
24
24
|
import { loadConfig } from '../../core/config.js';
|
|
25
25
|
import { getDashboardStatus, startDashboard, stopDashboard } from './dashboard.js';
|
|
26
|
+
import { run } from '../../core/shell.js';
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
// ─── Version helpers ──────────────────────────────────────────────────────────
|
|
30
|
+
|
|
31
|
+
async function fetchLatestVersion() {
|
|
32
|
+
const result = await run('npm view easy-devops version', { timeout: 20000 });
|
|
33
|
+
if (result.success && result.stdout.trim()) return result.stdout.trim();
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function isNewer(latest, current) {
|
|
38
|
+
const parse = v => v.replace(/^v/, '').split('.').map(Number);
|
|
39
|
+
const [la, lb, lc] = parse(latest);
|
|
40
|
+
const [ca, cb, cc] = parse(current);
|
|
41
|
+
if (la !== ca) return la > ca;
|
|
42
|
+
if (lb !== cb) return lb > cb;
|
|
43
|
+
return lc > cc;
|
|
44
|
+
}
|
|
26
45
|
|
|
27
46
|
// ─── Recover interrupted update ───────────────────────────────────────────────
|
|
28
47
|
// If a previous update crashed after stopping the dashboard but before restarting
|
package/src/core/deploy/apps.js
CHANGED
|
@@ -332,15 +332,20 @@ function patchApp(id, patch) {
|
|
|
332
332
|
/**
|
|
333
333
|
* Removes an app from the registry and deletes its files.
|
|
334
334
|
* Stopping its PM2 process is the caller's job (core/deploy/engine.js).
|
|
335
|
-
*
|
|
335
|
+
*
|
|
336
|
+
* Persistent data is kept unless `purgeData` says otherwise: a delete removes
|
|
337
|
+
* a deployment, not the database and uploads it was serving.
|
|
338
|
+
*
|
|
339
|
+
* @returns {{ keptShared: boolean, sharedPath: string } | null} null when there was no such app
|
|
336
340
|
*/
|
|
337
|
-
export function deleteApp(id) {
|
|
341
|
+
export function deleteApp(id, { purgeData = false } = {}) {
|
|
338
342
|
const apps = listApps();
|
|
339
|
-
if (!apps.some((a) => a.id === id)) return
|
|
343
|
+
if (!apps.some((a) => a.id === id)) return null;
|
|
340
344
|
// Files first: once the record is gone, nothing remembers which folder was its.
|
|
341
|
-
|
|
345
|
+
let files = { keptShared: false, sharedPath: appPaths(id).shared };
|
|
346
|
+
try { files = removeAppFiles(id, { keepShared: !purgeData }); } catch { /* files in use: the registry entry is removed regardless */ }
|
|
342
347
|
saveApps(apps.filter((a) => a.id !== id));
|
|
343
|
-
return
|
|
348
|
+
return files;
|
|
344
349
|
}
|
|
345
350
|
|
|
346
351
|
// ─── The deploy-hook token ────────────────────────────────────────────────────
|
|
@@ -454,6 +459,9 @@ export function deployKeyPath(id) {
|
|
|
454
459
|
*/
|
|
455
460
|
export function dashboardApp(app) {
|
|
456
461
|
if (!app) return null;
|
|
462
|
+
const paths = appPaths(app.id);
|
|
463
|
+
let hasSharedData = false;
|
|
464
|
+
try { hasSharedData = fs.readdirSync(paths.shared).length > 0; } catch { /* nothing persistent */ }
|
|
457
465
|
return {
|
|
458
466
|
id: app.id,
|
|
459
467
|
pm2Name: pm2Name(app.id),
|
|
@@ -467,7 +475,10 @@ export function dashboardApp(app) {
|
|
|
467
475
|
keepReleases: app.keepReleases ?? DEFAULT_KEEP_RELEASES,
|
|
468
476
|
autoRollback: app.autoRollback !== false,
|
|
469
477
|
notifyChannelIds: app.notifyChannelIds ?? [],
|
|
470
|
-
dir:
|
|
478
|
+
dir: paths.root,
|
|
479
|
+
/** Where the folders listed in `settings.persist` live. */
|
|
480
|
+
sharedDir: paths.shared,
|
|
481
|
+
hasSharedData,
|
|
471
482
|
domain: app.domain ?? null,
|
|
472
483
|
hook: {
|
|
473
484
|
hasToken: Boolean(app.hook?.tokenHash),
|
|
@@ -241,6 +241,226 @@ export function loadProjectSettings(dir, overrides = {}) {
|
|
|
241
241
|
return { ...resolved, warnings, ezLocation };
|
|
242
242
|
}
|
|
243
243
|
|
|
244
|
+
// ─── Persistent folders ──────────────────────────────────────────────────────
|
|
245
|
+
|
|
246
|
+
/** The link path for one project-relative persistent folder. */
|
|
247
|
+
function persistLink(dir, rel) {
|
|
248
|
+
return path.join(dir, ...rel.split('/'));
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* Refuses a link placed under a symbolic link that came from the repository.
|
|
253
|
+
* Following one would put the app's writes outside the release — and outside
|
|
254
|
+
* anything the deployment can clean up.
|
|
255
|
+
*/
|
|
256
|
+
function assertRealParents(dir, rel) {
|
|
257
|
+
let at = dir;
|
|
258
|
+
for (const part of rel.split('/').slice(0, -1)) {
|
|
259
|
+
at = path.join(at, part);
|
|
260
|
+
let st;
|
|
261
|
+
try { st = fs.lstatSync(at); } catch { return; } // nothing below it exists yet
|
|
262
|
+
if (st.isSymbolicLink()) {
|
|
263
|
+
throw new StepError('settings', `"${rel}" is under a symbolic link in the repository, which persistent folders do not allow.`);
|
|
264
|
+
}
|
|
265
|
+
if (!st.isDirectory()) {
|
|
266
|
+
throw new StepError('settings', `"${rel}" is inside "${path.relative(dir, at)}", which is a file in the repository.`);
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* Creates a directory link, relative where possible.
|
|
273
|
+
*
|
|
274
|
+
* A relative link survives the app's folder being moved. On Windows a
|
|
275
|
+
* non-elevated account cannot create symlinks; a junction needs no privilege
|
|
276
|
+
* but stores an absolute path, which is why moveApp() recreates these links.
|
|
277
|
+
*/
|
|
278
|
+
function createDirLink(targetDir, linkAt) {
|
|
279
|
+
fs.mkdirSync(path.dirname(linkAt), { recursive: true });
|
|
280
|
+
const relative = path.relative(path.dirname(linkAt), targetDir) || '.';
|
|
281
|
+
try {
|
|
282
|
+
fs.symlinkSync(relative, linkAt, 'dir');
|
|
283
|
+
} catch (err) {
|
|
284
|
+
if (process.platform === 'win32' && (err.code === 'EPERM' || err.code === 'EACCES')) {
|
|
285
|
+
fs.symlinkSync(targetDir, linkAt, 'junction');
|
|
286
|
+
return;
|
|
287
|
+
}
|
|
288
|
+
throw err;
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
function removeAt(target) {
|
|
293
|
+
let st = null;
|
|
294
|
+
try { st = fs.lstatSync(target); } catch { return; }
|
|
295
|
+
if (st) fs.rmSync(target, { recursive: true, force: true });
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/** Copies the contents of a folder, leaving symlinks out: their targets are not part of this project. */
|
|
299
|
+
function copyInto(from, to, log, rel, { overwrite = false } = {}) {
|
|
300
|
+
for (const entry of fs.readdirSync(from)) {
|
|
301
|
+
const src = path.join(from, entry);
|
|
302
|
+
const st = fs.lstatSync(src);
|
|
303
|
+
if (st.isSymbolicLink()) {
|
|
304
|
+
log.line(` warning: skipped the symbolic link ${rel}/${entry} while seeding persistent data`);
|
|
305
|
+
continue;
|
|
306
|
+
}
|
|
307
|
+
fs.cpSync(src, path.join(to, entry), {
|
|
308
|
+
recursive: true, preserveTimestamps: true, force: overwrite, errorOnExist: !overwrite,
|
|
309
|
+
});
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
/**
|
|
314
|
+
* Gives every persistent folder one home outside the releases and links it in.
|
|
315
|
+
*
|
|
316
|
+
* A deployment starts from a fresh checkout, and old releases are deleted —
|
|
317
|
+
* that is what makes a rollback clean, and why runtime data disappears with
|
|
318
|
+
* it. A persistent folder lives once, under `<app folder>/shared/`, and every
|
|
319
|
+
* release gets a link to it, so a deploy, a rollback and a prune all see the
|
|
320
|
+
* same files.
|
|
321
|
+
*
|
|
322
|
+
* The first deployment seeds the shared copy from the repository, so a
|
|
323
|
+
* committed empty uploads/ or a SQLite file is not lost. When the app was
|
|
324
|
+
* already running, the live release is seeded after the repository and wins:
|
|
325
|
+
* its folder is the one holding the data written until now. After that the
|
|
326
|
+
* shared copy wins — editing the folder in the repository changes nothing,
|
|
327
|
+
* which is what "persistent" has to mean.
|
|
328
|
+
*
|
|
329
|
+
* @param {string|null} [inheritDir] The release that is live now, if any
|
|
330
|
+
*/
|
|
331
|
+
function linkPersistentDirs(appId, dir, paths, log, inheritDir = null) {
|
|
332
|
+
if (!paths || paths.length === 0) return;
|
|
333
|
+
const shared = appPaths(appId).shared;
|
|
334
|
+
fs.mkdirSync(shared, { recursive: true });
|
|
335
|
+
|
|
336
|
+
for (const rel of paths) {
|
|
337
|
+
const linkAt = persistLink(dir, rel);
|
|
338
|
+
const sharedAt = path.join(shared, ...rel.split('/'));
|
|
339
|
+
if (!isInside(linkAt, dir) || !isInside(sharedAt, shared)) {
|
|
340
|
+
throw new StepError('settings', `"${rel}" is not inside the project.`);
|
|
341
|
+
}
|
|
342
|
+
assertRealParents(dir, rel);
|
|
343
|
+
|
|
344
|
+
let inShared = null;
|
|
345
|
+
try { inShared = fs.lstatSync(sharedAt); } catch { /* first deployment for this folder */ }
|
|
346
|
+
if (inShared && !inShared.isDirectory()) {
|
|
347
|
+
throw new StepError('settings', `"${rel}" is not a folder in the app's shared data (${shared}).`);
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
let seededFrom = null;
|
|
351
|
+
if (!inShared) {
|
|
352
|
+
const sources = [];
|
|
353
|
+
|
|
354
|
+
let inRelease = null;
|
|
355
|
+
try { inRelease = fs.lstatSync(linkAt); } catch { /* the repository has nothing there */ }
|
|
356
|
+
if (inRelease && (!inRelease.isDirectory() || inRelease.isSymbolicLink())) {
|
|
357
|
+
throw new StepError('settings', `"${rel}" is a file in the repository. Persist the folder that contains it instead.`);
|
|
358
|
+
}
|
|
359
|
+
fs.mkdirSync(sharedAt, { recursive: true });
|
|
360
|
+
if (inRelease) {
|
|
361
|
+
copyInto(linkAt, sharedAt, log, rel);
|
|
362
|
+
sources.push('the repository');
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
if (inheritDir && path.resolve(inheritDir) !== path.resolve(dir)) {
|
|
366
|
+
const previousAt = persistLink(inheritDir, rel);
|
|
367
|
+
let previous = null;
|
|
368
|
+
try { previous = fs.lstatSync(previousAt); } catch { /* that release has nothing there */ }
|
|
369
|
+
if (previous && previous.isDirectory() && !previous.isSymbolicLink()) {
|
|
370
|
+
copyInto(previousAt, sharedAt, log, rel, { overwrite: true });
|
|
371
|
+
sources.push('the live release');
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
if (sources.length > 0) seededFrom = sources.join(' and ');
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
// Whatever was at this path is replaced by the link: the shared copy is
|
|
378
|
+
// the one that persists.
|
|
379
|
+
removeAt(linkAt);
|
|
380
|
+
createDirLink(sharedAt, linkAt);
|
|
381
|
+
log.line(` persist ${rel}${seededFrom ? ` (seeded from ${seededFrom})` : ''}`);
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
/**
|
|
386
|
+
* Checks the links again after the build. A build that deletes or recreates a
|
|
387
|
+
* persistent folder would put data back inside the release — where the next
|
|
388
|
+
* deployment loses it — so that is a failure, not something to fix silently.
|
|
389
|
+
*/
|
|
390
|
+
function verifyPersistentLinks(appId, dir, paths) {
|
|
391
|
+
if (!paths || paths.length === 0) return;
|
|
392
|
+
const shared = appPaths(appId).shared;
|
|
393
|
+
|
|
394
|
+
for (const rel of paths) {
|
|
395
|
+
const linkAt = persistLink(dir, rel);
|
|
396
|
+
const sharedAt = path.join(shared, ...rel.split('/'));
|
|
397
|
+
let st = null;
|
|
398
|
+
try { st = fs.lstatSync(linkAt); } catch { /* the build removed it */ }
|
|
399
|
+
|
|
400
|
+
let pointsAtShared = false;
|
|
401
|
+
if (st && st.isSymbolicLink()) {
|
|
402
|
+
try {
|
|
403
|
+
const a = fs.realpathSync(linkAt);
|
|
404
|
+
const b = fs.realpathSync(sharedAt);
|
|
405
|
+
pointsAtShared = process.platform === 'win32' ? a.toLowerCase() === b.toLowerCase() : a === b;
|
|
406
|
+
} catch { pointsAtShared = false; }
|
|
407
|
+
}
|
|
408
|
+
if (pointsAtShared) continue;
|
|
409
|
+
|
|
410
|
+
if (!st) {
|
|
411
|
+
fs.mkdirSync(sharedAt, { recursive: true });
|
|
412
|
+
createDirLink(sharedAt, linkAt);
|
|
413
|
+
continue;
|
|
414
|
+
}
|
|
415
|
+
throw new StepError('build', `The build replaced the persistent folder "${rel}" with a real file or folder, so anything written there would be lost on the next deployment. Do not delete or recreate "${rel}" in the build.`);
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
/** Every persistent link in every release of an app. */
|
|
420
|
+
function persistentLinks(appId) {
|
|
421
|
+
const out = [];
|
|
422
|
+
for (const id of listReleases(appId)) {
|
|
423
|
+
const dir = releaseDir(appId, id);
|
|
424
|
+
for (const rel of releaseHealth(dir).persist) out.push({ dir, rel });
|
|
425
|
+
}
|
|
426
|
+
return out;
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
/**
|
|
430
|
+
* Removes the links before the app's folder is moved, and recreates them after
|
|
431
|
+
* — against the new shared folder. A relative link would survive on its own;
|
|
432
|
+
* a Windows junction holds an absolute path and would point at a folder that
|
|
433
|
+
* no longer exists. Detaching first also keeps a cross-device copy from
|
|
434
|
+
* reading through the links.
|
|
435
|
+
*/
|
|
436
|
+
function detachPersistentLinks(appId) {
|
|
437
|
+
for (const { dir, rel } of persistentLinks(appId)) {
|
|
438
|
+
const linkAt = persistLink(dir, rel);
|
|
439
|
+
try {
|
|
440
|
+
if (fs.lstatSync(linkAt).isSymbolicLink()) fs.rmSync(linkAt, { force: true });
|
|
441
|
+
} catch { /* gone already */ }
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
/** @returns {string[]} One message per folder that could not be linked again */
|
|
446
|
+
function attachPersistentLinks(appId) {
|
|
447
|
+
const shared = appPaths(appId).shared;
|
|
448
|
+
const failures = [];
|
|
449
|
+
for (const { dir, rel } of persistentLinks(appId)) {
|
|
450
|
+
const linkAt = persistLink(dir, rel);
|
|
451
|
+
try {
|
|
452
|
+
if (!fs.existsSync(linkAt)) {
|
|
453
|
+
const sharedAt = path.join(shared, ...rel.split('/'));
|
|
454
|
+
fs.mkdirSync(sharedAt, { recursive: true });
|
|
455
|
+
createDirLink(sharedAt, linkAt);
|
|
456
|
+
}
|
|
457
|
+
} catch (err) {
|
|
458
|
+
failures.push(`${rel}: ${err.message}`);
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
return failures;
|
|
462
|
+
}
|
|
463
|
+
|
|
244
464
|
/** The environment a deployed app (and its build) runs with. */
|
|
245
465
|
function appEnvironment(app, settings, releaseId) {
|
|
246
466
|
const env = { ...(app.env ?? {}) };
|
|
@@ -249,6 +469,9 @@ function appEnvironment(app, settings, releaseId) {
|
|
|
249
469
|
if (settings.port && env.PORT === undefined) env.PORT = String(settings.port);
|
|
250
470
|
env.EZ_APP = app.id;
|
|
251
471
|
env.EZ_RELEASE = releaseId;
|
|
472
|
+
// One stable place for data, for an app that would rather build a path than
|
|
473
|
+
// rely on the folder links: `${EZ_SHARED_DIR}/uploads`.
|
|
474
|
+
if (settings.persist?.length) env.EZ_SHARED_DIR = appPaths(app.id).shared;
|
|
252
475
|
return env;
|
|
253
476
|
}
|
|
254
477
|
|
|
@@ -291,9 +514,10 @@ function releaseHealth(dir) {
|
|
|
291
514
|
strict: Boolean(meta.strict),
|
|
292
515
|
sha: typeof meta.sha === 'string' ? meta.sha : null,
|
|
293
516
|
ref: typeof meta.ref === 'string' ? meta.ref : null,
|
|
517
|
+
persist: Array.isArray(meta.persist) ? meta.persist.filter((p) => typeof p === 'string') : [],
|
|
294
518
|
};
|
|
295
519
|
} catch {
|
|
296
|
-
return { port: null, healthPath: '/', strict: false, sha: null, ref: null };
|
|
520
|
+
return { port: null, healthPath: '/', strict: false, sha: null, ref: null, persist: [] };
|
|
297
521
|
}
|
|
298
522
|
}
|
|
299
523
|
|
|
@@ -541,6 +765,15 @@ async function executeDeployment(app, run, d) {
|
|
|
541
765
|
const value = Array.isArray(settings[field]) ? (settings[field].join(' && ') || '(none)') : (settings[field] ?? '(none)');
|
|
542
766
|
log.line(` ${field.padEnd(10)} ${value} [${sources[field]}]`);
|
|
543
767
|
}
|
|
768
|
+
if (settings.persist.length > 0) {
|
|
769
|
+
log.line(` persist ${settings.persist.join(', ')} [${sources.persist}]`);
|
|
770
|
+
}
|
|
771
|
+
|
|
772
|
+
// Before the hooks: a pre-deploy script that writes to the database must
|
|
773
|
+
// write to the shared copy, not to this disposable release. The live
|
|
774
|
+
// release is where the data written up to now is, so it seeds a folder
|
|
775
|
+
// that is being made persistent for the first time.
|
|
776
|
+
linkPersistentDirs(app.id, dir, settings.persist, log, before.current ? releaseDir(app.id, before.current) : null);
|
|
544
777
|
|
|
545
778
|
const env = appEnvironment(app, settings, run.releaseId);
|
|
546
779
|
const strict = sources.healthPath !== 'default';
|
|
@@ -549,6 +782,7 @@ async function executeDeployment(app, run, d) {
|
|
|
549
782
|
await userCommands('pre', 'Pre-deploy hook', settings.pre, env);
|
|
550
783
|
await userCommands('install', 'Install', settings.install, env);
|
|
551
784
|
await userCommands('build', 'Build', settings.build, env);
|
|
785
|
+
verifyPersistentLinks(app.id, dir, settings.persist);
|
|
552
786
|
|
|
553
787
|
// ── 4. switch ───────────────────────────────────────────────────────────
|
|
554
788
|
setStep('start', 'Starting the new release');
|
|
@@ -562,6 +796,7 @@ async function executeDeployment(app, run, d) {
|
|
|
562
796
|
writePm2Config(dir, app, settings, env);
|
|
563
797
|
fs.writeFileSync(path.join(dir, 'ez.release.json'), JSON.stringify({
|
|
564
798
|
sha: run.sha, ref: run.ref, port: settings.port, healthPath: settings.healthPath, strict,
|
|
799
|
+
persist: settings.persist,
|
|
565
800
|
deployedAt: new Date(d.now()).toISOString(),
|
|
566
801
|
}, null, 2));
|
|
567
802
|
|
|
@@ -794,9 +1029,11 @@ export function startRollback(appId, { releaseId = null, actor = null, deps = {}
|
|
|
794
1029
|
* @param {string} appId
|
|
795
1030
|
* @param {Object} [opts]
|
|
796
1031
|
* @param {boolean} [opts.deleteDomain] Also remove the domain linked to it
|
|
797
|
-
* @
|
|
1032
|
+
* @param {boolean} [opts.purgeData] Also delete the persistent folders, not just the app
|
|
1033
|
+
* @returns {Promise<{ ok: boolean, status: number, error: string|null, domainDeleted?: boolean,
|
|
1034
|
+
* warning?: string|null, sharedKept?: boolean, sharedPath?: string }>}
|
|
798
1035
|
*/
|
|
799
|
-
export async function removeApp(appId, { deleteDomain = false, deps = {} } = {}) {
|
|
1036
|
+
export async function removeApp(appId, { deleteDomain = false, purgeData = false, deps = {} } = {}) {
|
|
800
1037
|
const app = getApp(appId);
|
|
801
1038
|
if (!app) return { ok: false, status: 404, error: `No app named "${appId}".` };
|
|
802
1039
|
|
|
@@ -820,8 +1057,13 @@ export async function removeApp(appId, { deleteDomain = false, deps = {} } = {})
|
|
|
820
1057
|
if (!removed.ok) warning = `The app was deleted, but the domain ${app.domain} was not: ${removed.error}`;
|
|
821
1058
|
}
|
|
822
1059
|
|
|
823
|
-
|
|
824
|
-
|
|
1060
|
+
// Resolved before the record goes: afterwards nothing knows this app's folder.
|
|
1061
|
+
const sharedPath = appPaths(appId).shared;
|
|
1062
|
+
const files = deleteApp(appId, { purgeData }) ?? { keptShared: false };
|
|
1063
|
+
return {
|
|
1064
|
+
ok: true, status: 200, error: null, domainDeleted, warning,
|
|
1065
|
+
sharedKept: Boolean(files.keptShared), sharedPath,
|
|
1066
|
+
};
|
|
825
1067
|
}
|
|
826
1068
|
|
|
827
1069
|
/**
|
|
@@ -928,16 +1170,22 @@ export async function moveApp(appId, targetDir, { deps = {} } = {}) {
|
|
|
928
1170
|
// An empty folder left by an earlier attempt would make the rename fail.
|
|
929
1171
|
if (fs.existsSync(to)) fs.rmdirSync(to);
|
|
930
1172
|
|
|
1173
|
+
// Detached first: a junction holds an absolute path into the old folder,
|
|
1174
|
+
// and a cross-device copy must not read through the links.
|
|
1175
|
+
detachPersistentLinks(appId);
|
|
1176
|
+
|
|
931
1177
|
try {
|
|
932
1178
|
if (fs.existsSync(from)) moveDirectory(from, to);
|
|
933
1179
|
else fs.mkdirSync(to, { recursive: true });
|
|
934
1180
|
} catch (err) {
|
|
935
1181
|
// Nothing was recorded, so the app is still where it was. Put it back up.
|
|
1182
|
+
attachPersistentLinks(appId);
|
|
936
1183
|
if (wasRunning) await startCurrent();
|
|
937
1184
|
return { ok: false, status: 500, error: `The folder could not be moved: ${err.message}` };
|
|
938
1185
|
}
|
|
939
1186
|
|
|
940
1187
|
setAppDir(appId, to);
|
|
1188
|
+
const relinkFailures = attachPersistentLinks(appId);
|
|
941
1189
|
|
|
942
1190
|
// The lock file travelled with the folder; it is ours, and the old path it
|
|
943
1191
|
// was taken at no longer exists for release() to clean up.
|
|
@@ -960,6 +1208,10 @@ export async function moveApp(appId, targetDir, { deps = {} } = {}) {
|
|
|
960
1208
|
restarted = await startCurrent();
|
|
961
1209
|
if (!restarted) warning = 'The app was moved, but PM2 could not start it again. Deploy it, or start it from the Processes page.';
|
|
962
1210
|
}
|
|
1211
|
+
if (relinkFailures.length > 0) {
|
|
1212
|
+
const note = `These persistent folders could not be linked again: ${relinkFailures.join('; ')}. Deploy the app to restore them.`;
|
|
1213
|
+
warning = warning ? `${warning} ${note}` : `The app was moved, but ${note}`;
|
|
1214
|
+
}
|
|
963
1215
|
|
|
964
1216
|
return { ok: true, status: 200, error: null, from, to, restarted, warning };
|
|
965
1217
|
} finally {
|
|
@@ -27,6 +27,7 @@
|
|
|
27
27
|
* "port": 3000, the port the app listens on
|
|
28
28
|
* "health": "/healthz", string, or { "path": "/healthz", "timeout": 60 }
|
|
29
29
|
* "hooks": { "pre": "...", "post": "..." } string | string[]
|
|
30
|
+
* "persist": ["data", "uploads"], folders that survive deployments
|
|
30
31
|
* }
|
|
31
32
|
*
|
|
32
33
|
* `false` (or an empty string) switches a step off — "this project has no
|
|
@@ -41,9 +42,12 @@ export const DEFAULT_HEALTH_TIMEOUT_S = 60;
|
|
|
41
42
|
export const MAX_HEALTH_TIMEOUT_S = 600;
|
|
42
43
|
|
|
43
44
|
const COMMAND_FIELDS = Object.freeze(['install', 'build']);
|
|
44
|
-
const KNOWN_KEYS = Object.freeze(['$schema', 'name', 'install', 'build', 'start', 'port', 'health', 'hooks']);
|
|
45
|
+
const KNOWN_KEYS = Object.freeze(['$schema', 'name', 'install', 'build', 'start', 'port', 'health', 'hooks', 'persist']);
|
|
45
46
|
const MAX_COMMAND_LENGTH = 2000;
|
|
46
47
|
|
|
48
|
+
export const MAX_PERSIST_PATHS = 20;
|
|
49
|
+
const MAX_PERSIST_PATH_LENGTH = 200;
|
|
50
|
+
|
|
47
51
|
/** @returns {boolean} */
|
|
48
52
|
function isPlainObject(v) {
|
|
49
53
|
return v !== null && typeof v === 'object' && !Array.isArray(v);
|
|
@@ -94,6 +98,69 @@ export function normalizeHealthPath(value) {
|
|
|
94
98
|
return p;
|
|
95
99
|
}
|
|
96
100
|
|
|
101
|
+
/**
|
|
102
|
+
* A `persist` path must stay inside the project and name a whole folder.
|
|
103
|
+
*
|
|
104
|
+
* Directories only, deliberately: a file is the wrong unit. SQLite writes
|
|
105
|
+
* `-wal` and `-shm` next to its database, and an uploads folder gains new
|
|
106
|
+
* names every day — a persisted file would take neither with it.
|
|
107
|
+
*
|
|
108
|
+
* @returns {string|null} What is wrong with it, or null when it is usable
|
|
109
|
+
*/
|
|
110
|
+
function persistPathProblem(p) {
|
|
111
|
+
if (p.length > MAX_PERSIST_PATH_LENGTH) return `is longer than ${MAX_PERSIST_PATH_LENGTH} characters`;
|
|
112
|
+
if (/[\0\r\n]/.test(p)) return 'must be a single line';
|
|
113
|
+
if (p.startsWith('/') || /^[A-Za-z]:/.test(p)) return 'must be relative to the project folder, not an absolute path';
|
|
114
|
+
for (const segment of p.split('/')) {
|
|
115
|
+
if (segment === '' || segment === '.' || segment === '..') return 'must be a relative path without "." or ".."';
|
|
116
|
+
if (segment === '.git') return 'cannot be ".git" — the deployment manages it';
|
|
117
|
+
if (segment === 'node_modules') return 'cannot be "node_modules" — dependencies are installed fresh for each release';
|
|
118
|
+
if (/[<>:"|?*]/.test(segment)) return 'contains characters that are not allowed in a folder name';
|
|
119
|
+
}
|
|
120
|
+
return null;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Normalises a `persist` field to a list of project-relative folders.
|
|
125
|
+
*
|
|
126
|
+
* Paths are written with "/" on both platforms; the engine turns them into
|
|
127
|
+
* platform paths. Overlapping folders are refused rather than merged, because
|
|
128
|
+
* two links to the same data would look independent and then diverge.
|
|
129
|
+
*
|
|
130
|
+
* @param {*} value string | string[] | false | null | undefined
|
|
131
|
+
* @returns {{ paths: string[]|null, error: string|null }}
|
|
132
|
+
* `paths: null` means "not specified here"; `[]` means "nothing persistent".
|
|
133
|
+
*/
|
|
134
|
+
export function normalizePersistPaths(value) {
|
|
135
|
+
if (value === undefined || value === null) return { paths: null, error: null };
|
|
136
|
+
if (value === false) return { paths: [], error: null };
|
|
137
|
+
|
|
138
|
+
const list = Array.isArray(value) ? value : [value];
|
|
139
|
+
if (list.length > MAX_PERSIST_PATHS) return { paths: null, error: `has more than ${MAX_PERSIST_PATHS} folders` };
|
|
140
|
+
|
|
141
|
+
const out = [];
|
|
142
|
+
for (const item of list) {
|
|
143
|
+
if (typeof item !== 'string') return { paths: null, error: 'must be folder paths, as a string or a list of strings' };
|
|
144
|
+
const p = item.trim().replace(/\\/g, '/').replace(/^\.\/+/, '').replace(/\/+$/, '');
|
|
145
|
+
if (!p) continue;
|
|
146
|
+
const problem = persistPathProblem(p);
|
|
147
|
+
if (problem) return { paths: null, error: `"${item.trim()}" ${problem}` };
|
|
148
|
+
if (!out.includes(p)) out.push(p);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// Windows and macOS treat these as the same folder; one path cannot be
|
|
152
|
+
// linked twice there, so the pair is refused everywhere.
|
|
153
|
+
for (const a of out) {
|
|
154
|
+
for (const b of out) {
|
|
155
|
+
if (a !== b && b.toLowerCase().startsWith(`${a.toLowerCase()}/`)) {
|
|
156
|
+
return { paths: null, error: `"${a}" and "${b}" overlap — keep only "${a}"` };
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
return { paths: out, error: null };
|
|
162
|
+
}
|
|
163
|
+
|
|
97
164
|
/**
|
|
98
165
|
* Parses and validates the contents of an ez.json file.
|
|
99
166
|
*
|
|
@@ -181,6 +248,12 @@ export function parseEzFile(text) {
|
|
|
181
248
|
}
|
|
182
249
|
}
|
|
183
250
|
|
|
251
|
+
if (raw.persist !== undefined && raw.persist !== null) {
|
|
252
|
+
const n = normalizePersistPaths(raw.persist);
|
|
253
|
+
if (n.error) errors.push(`ez.json: "persist" ${n.error}`);
|
|
254
|
+
else if (n.paths !== null) value.persist = n.paths;
|
|
255
|
+
}
|
|
256
|
+
|
|
184
257
|
return { ok: errors.length === 0, value: errors.length === 0 ? value : null, errors, warnings };
|
|
185
258
|
}
|
|
186
259
|
|
|
@@ -221,9 +294,10 @@ export function defaultsFromPackageJson(pkg, files = {}) {
|
|
|
221
294
|
* Validates the per-app overrides set in the dashboard or the Hub.
|
|
222
295
|
*
|
|
223
296
|
* Same shape as a parsed ez.json (`install`, `build`, `start`, `port`,
|
|
224
|
-
* `healthPath`, `healthTimeout`, `pre`, `post`). An absent, null or
|
|
225
|
-
* field means "do not override", so a form with untouched inputs
|
|
226
|
-
* nothing. `false` for install/build/pre/post switches that step off
|
|
297
|
+
* `healthPath`, `healthTimeout`, `pre`, `post`, `persist`). An absent, null or
|
|
298
|
+
* empty field means "do not override", so a form with untouched inputs
|
|
299
|
+
* overrides nothing. `false` for install/build/pre/post switches that step off;
|
|
300
|
+
* `[]` or `false` for persist means "nothing persistent", overriding ez.json.
|
|
227
301
|
*
|
|
228
302
|
* @param {*} input
|
|
229
303
|
* @returns {{ ok: boolean, value: Object, errors: string[] }}
|
|
@@ -270,6 +344,12 @@ export function normalizeOverrides(input) {
|
|
|
270
344
|
}
|
|
271
345
|
}
|
|
272
346
|
|
|
347
|
+
if (input.persist !== undefined && input.persist !== null && input.persist !== '') {
|
|
348
|
+
const n = normalizePersistPaths(input.persist);
|
|
349
|
+
if (n.error) errors.push(`persist ${n.error}`);
|
|
350
|
+
else if (n.paths !== null) value.persist = n.paths;
|
|
351
|
+
}
|
|
352
|
+
|
|
273
353
|
return { ok: errors.length === 0, value, errors };
|
|
274
354
|
}
|
|
275
355
|
|
|
@@ -288,7 +368,8 @@ export function normalizeOverrides(input) {
|
|
|
288
368
|
* @returns {{
|
|
289
369
|
* ok: boolean, error: string|null,
|
|
290
370
|
* settings: { install: string[], build: string[], start: string|null, port: number|null,
|
|
291
|
-
* healthPath: string, healthTimeout: number, pre: string[], post: string[]
|
|
371
|
+
* healthPath: string, healthTimeout: number, pre: string[], post: string[],
|
|
372
|
+
* persist: string[] },
|
|
292
373
|
* sources: Object<string, 'override'|'ez.json'|'package.json'|'default'>
|
|
293
374
|
* }}
|
|
294
375
|
*/
|
|
@@ -321,6 +402,7 @@ export function resolveSettings({ ez = null, pkg = null, files = {}, overrides =
|
|
|
321
402
|
healthTimeout: pick('healthTimeout', DEFAULT_HEALTH_TIMEOUT_S),
|
|
322
403
|
pre: pick('pre', []),
|
|
323
404
|
post: pick('post', []),
|
|
405
|
+
persist: pick('persist', []),
|
|
324
406
|
};
|
|
325
407
|
|
|
326
408
|
if (!settings.start) {
|
|
@@ -152,9 +152,13 @@ export async function handleHubDeployCommand(payload, { onFinished = null } = {}
|
|
|
152
152
|
if (deleteDomain && getHubPermissions()['domains.delete'] !== true) {
|
|
153
153
|
throw refuse("Deleting the linked domain needs 'domains.delete', which this server has switched off. Delete the app without its domain, or enable it in Settings → Hub.");
|
|
154
154
|
}
|
|
155
|
-
|
|
155
|
+
// Persistent data is kept unless asked for, exactly as on the dashboard.
|
|
156
|
+
const result = await removeApp(id, { deleteDomain, purgeData: payload.purgeData === true });
|
|
156
157
|
if (!result.ok) throw refuse(result.error);
|
|
157
|
-
return {
|
|
158
|
+
return {
|
|
159
|
+
deleted: id, domainDeleted: result.domainDeleted, warning: result.warning ?? null,
|
|
160
|
+
sharedKept: result.sharedKept, sharedPath: result.sharedPath,
|
|
161
|
+
};
|
|
158
162
|
}
|
|
159
163
|
|
|
160
164
|
case 'linkDomain': {
|
package/src/core/deploy/store.js
CHANGED
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
* releases/<releaseId>/ one checkout per deployment, built in place
|
|
8
8
|
* runs/<runId>.json the record of one deployment attempt
|
|
9
9
|
* logs/<runId>.log its output
|
|
10
|
+
* shared/ data that outlives a release, linked into each one
|
|
10
11
|
* state.json which release is live, and the one before it
|
|
11
12
|
* deploy.lock held while a deployment is running
|
|
12
13
|
* id_deploy the app's private deploy key, when it has one
|
|
@@ -127,6 +128,7 @@ export function pathsIn(root) {
|
|
|
127
128
|
releases: path.join(root, 'releases'),
|
|
128
129
|
runs: path.join(root, 'runs'),
|
|
129
130
|
logs: path.join(root, 'logs'),
|
|
131
|
+
shared: path.join(root, 'shared'),
|
|
130
132
|
state: path.join(root, 'state.json'),
|
|
131
133
|
lock: path.join(root, 'deploy.lock'),
|
|
132
134
|
deployKey: path.join(root, 'id_deploy'),
|
|
@@ -392,7 +394,31 @@ export function lockHolder(appId, dataDir) {
|
|
|
392
394
|
return { pid: held.pid, runId: held.runId ?? null, at: held.at ?? null };
|
|
393
395
|
}
|
|
394
396
|
|
|
395
|
-
/**
|
|
396
|
-
|
|
397
|
-
|
|
397
|
+
/**
|
|
398
|
+
* Removes an app's folder — or everything in it except `shared/`.
|
|
399
|
+
*
|
|
400
|
+
* Persistent data is the one thing a delete does not destroy silently: the
|
|
401
|
+
* default keeps it, and removing it is an explicit choice. A `shared/` that
|
|
402
|
+
* holds nothing is removed with the rest, so an app that never used it leaves
|
|
403
|
+
* no trace.
|
|
404
|
+
*
|
|
405
|
+
* @returns {{ keptShared: boolean, sharedPath: string }}
|
|
406
|
+
*/
|
|
407
|
+
export function removeAppFiles(appId, { keepShared = false, dataDir } = {}) {
|
|
408
|
+
const p = appPaths(appId, dataDir);
|
|
409
|
+
|
|
410
|
+
let hasShared = false;
|
|
411
|
+
if (keepShared) {
|
|
412
|
+
try { hasShared = fs.readdirSync(p.shared).length > 0; } catch { /* no shared folder */ }
|
|
413
|
+
}
|
|
414
|
+
if (!hasShared) {
|
|
415
|
+
fs.rmSync(p.root, { recursive: true, force: true });
|
|
416
|
+
return { keptShared: false, sharedPath: p.shared };
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
for (const entry of fs.readdirSync(p.root)) {
|
|
420
|
+
if (entry === 'shared') continue;
|
|
421
|
+
fs.rmSync(path.join(p.root, entry), { recursive: true, force: true });
|
|
422
|
+
}
|
|
423
|
+
return { keptShared: true, sharedPath: p.shared };
|
|
398
424
|
}
|
package/src/core/probeCache.js
CHANGED
|
@@ -130,7 +130,11 @@ export async function probe(key, producer, {
|
|
|
130
130
|
|
|
131
131
|
const settled = entries.get(key);
|
|
132
132
|
|
|
133
|
-
|
|
133
|
+
// `settled` can be gone: invalidate() deletes the entry while the producer
|
|
134
|
+
// is in flight, and this read was already waiting on that producer. The
|
|
135
|
+
// promise still resolves, but its answer no longer belongs to any cache
|
|
136
|
+
// entry — treat it as "no answer yet" instead of dereferencing undefined.
|
|
137
|
+
if (raced === 'done' && settled && settled.at !== null) {
|
|
134
138
|
return {
|
|
135
139
|
value: settled.value,
|
|
136
140
|
ageMs: Date.now() - settled.at,
|