easy-devops 1.3.1 → 1.3.3
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-BQIUfiNs.js +452 -0
- package/dist/ui/assets/index-D_5mc2NX.css +1 -0
- package/dist/ui/index.html +2 -2
- package/package.json +1 -1
- package/src/core/deploy/apps.js +17 -6
- package/src/core/deploy/engine.js +401 -6
- package/src/core/deploy/ezfile.js +90 -5
- package/src/core/deploy/hubCommands.js +15 -3
- package/src/core/deploy/store.js +29 -3
- package/src/core/hubPermissions.js +1 -0
- package/src/core/probeCache.js +5 -1
- package/src/dashboard/controllers/deploymentsController.js +22 -3
- package/src/dashboard/routes/deployments.js +1 -0
- package/dist/ui/assets/index-CMRzkIKj.css +0 -1
- package/dist/ui/assets/index-DEouL3IK.js +0 -452
|
@@ -52,7 +52,7 @@ import {
|
|
|
52
52
|
} from './ezfile.js';
|
|
53
53
|
import { LAUNCHER_FILE, LAUNCHER_SOURCE, LAUNCHER_COMMAND_ENV } from './launcher.js';
|
|
54
54
|
import {
|
|
55
|
-
acquireLock, appPaths, ensureAppDirs, listReleases, logFile, newReleaseId, pathsIn, pruneReleases, pruneRuns,
|
|
55
|
+
acquireLock, appPaths, ensureAppDirs, isId, listReleases, logFile, newReleaseId, pathsIn, pruneReleases, pruneRuns,
|
|
56
56
|
readRun, readState, releaseDir, validateDeployDir, writeRun, writeState,
|
|
57
57
|
} from './store.js';
|
|
58
58
|
|
|
@@ -241,6 +241,331 @@ export function loadProjectSettings(dir, overrides = {}) {
|
|
|
241
241
|
return { ...resolved, warnings, ezLocation };
|
|
242
242
|
}
|
|
243
243
|
|
|
244
|
+
// ─── Persistent paths ────────────────────────────────────────────────────────
|
|
245
|
+
|
|
246
|
+
/** The link path for one project-relative persistent path. */
|
|
247
|
+
function persistLink(dir, rel) {
|
|
248
|
+
return path.join(dir, ...rel.split('/'));
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/** What is at a path: 'dir', 'file', 'link', 'other', or null when nothing is. */
|
|
252
|
+
function entryKind(target) {
|
|
253
|
+
try {
|
|
254
|
+
const st = fs.lstatSync(target);
|
|
255
|
+
if (st.isSymbolicLink()) return 'link';
|
|
256
|
+
if (st.isDirectory()) return 'dir';
|
|
257
|
+
if (st.isFile()) return 'file';
|
|
258
|
+
return 'other';
|
|
259
|
+
} catch {
|
|
260
|
+
return null;
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/** True when a name looks like a file rather than a folder: its last segment has a dot. */
|
|
265
|
+
function looksLikeFile(rel) {
|
|
266
|
+
return (rel.split('/').pop() ?? '').includes('.');
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* Refuses a link placed under a symbolic link that came from the repository.
|
|
271
|
+
* Following one would put the app's writes outside the release — and outside
|
|
272
|
+
* anything the deployment can clean up.
|
|
273
|
+
*/
|
|
274
|
+
function assertRealParents(dir, rel) {
|
|
275
|
+
let at = dir;
|
|
276
|
+
for (const part of rel.split('/').slice(0, -1)) {
|
|
277
|
+
at = path.join(at, part);
|
|
278
|
+
let st;
|
|
279
|
+
try { st = fs.lstatSync(at); } catch { return; } // nothing below it exists yet
|
|
280
|
+
if (st.isSymbolicLink()) {
|
|
281
|
+
throw new StepError('settings', `"${rel}" is under a symbolic link in the repository, which persistent paths do not allow.`);
|
|
282
|
+
}
|
|
283
|
+
if (!st.isDirectory()) {
|
|
284
|
+
throw new StepError('settings', `"${rel}" is inside "${path.relative(dir, at)}", which is a file in the repository.`);
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/**
|
|
290
|
+
* Creates a directory link, relative where possible.
|
|
291
|
+
*
|
|
292
|
+
* A relative link survives the app's folder being moved. On Windows a
|
|
293
|
+
* non-elevated account cannot create symlinks; a junction needs no privilege
|
|
294
|
+
* but stores an absolute path, which is why moveApp() recreates these links.
|
|
295
|
+
*/
|
|
296
|
+
function createDirLink(targetDir, linkAt) {
|
|
297
|
+
fs.mkdirSync(path.dirname(linkAt), { recursive: true });
|
|
298
|
+
const relative = path.relative(path.dirname(linkAt), targetDir) || '.';
|
|
299
|
+
try {
|
|
300
|
+
fs.symlinkSync(relative, linkAt, 'dir');
|
|
301
|
+
} catch (err) {
|
|
302
|
+
if (process.platform === 'win32' && (err.code === 'EPERM' || err.code === 'EACCES')) {
|
|
303
|
+
fs.symlinkSync(targetDir, linkAt, 'junction');
|
|
304
|
+
return;
|
|
305
|
+
}
|
|
306
|
+
throw err;
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/**
|
|
311
|
+
* Creates a link to one file. Windows without symlink permission gets a hard
|
|
312
|
+
* link instead: it needs no privilege, points at the same bytes, and the
|
|
313
|
+
* inode comparison in verifyPersistentLinks() still notices when a build has
|
|
314
|
+
* replaced it with a copy.
|
|
315
|
+
*/
|
|
316
|
+
function createFileLink(targetFile, linkAt) {
|
|
317
|
+
fs.mkdirSync(path.dirname(linkAt), { recursive: true });
|
|
318
|
+
const relative = path.relative(path.dirname(linkAt), targetFile) || '.';
|
|
319
|
+
try {
|
|
320
|
+
fs.symlinkSync(relative, linkAt, 'file');
|
|
321
|
+
} catch (err) {
|
|
322
|
+
if (process.platform === 'win32' && (err.code === 'EPERM' || err.code === 'EACCES')) {
|
|
323
|
+
fs.linkSync(targetFile, linkAt);
|
|
324
|
+
return;
|
|
325
|
+
}
|
|
326
|
+
throw err;
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/** The same inode on the same device — a hard link, not a copy. */
|
|
331
|
+
function sameHardLink(a, b) {
|
|
332
|
+
try {
|
|
333
|
+
const x = fs.statSync(a);
|
|
334
|
+
const y = fs.statSync(b);
|
|
335
|
+
return x.ino !== 0 && x.ino === y.ino && x.dev === y.dev;
|
|
336
|
+
} catch {
|
|
337
|
+
return false;
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
/** Whether a link resolves to the shared copy. Windows paths compare case-insensitively. */
|
|
342
|
+
function linkPointsAt(linkAt, sharedAt) {
|
|
343
|
+
try {
|
|
344
|
+
const a = fs.realpathSync(linkAt);
|
|
345
|
+
const b = fs.realpathSync(sharedAt);
|
|
346
|
+
return process.platform === 'win32' ? a.toLowerCase() === b.toLowerCase() : a === b;
|
|
347
|
+
} catch {
|
|
348
|
+
return false;
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
function removeAt(target) {
|
|
353
|
+
let st = null;
|
|
354
|
+
try { st = fs.lstatSync(target); } catch { return; }
|
|
355
|
+
if (st) fs.rmSync(target, { recursive: true, force: true });
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
/** Copies the contents of a folder, leaving symlinks out: their targets are not part of this project. */
|
|
359
|
+
function copyInto(from, to, log, rel, { overwrite = false } = {}) {
|
|
360
|
+
for (const entry of fs.readdirSync(from)) {
|
|
361
|
+
const src = path.join(from, entry);
|
|
362
|
+
const st = fs.lstatSync(src);
|
|
363
|
+
if (st.isSymbolicLink()) {
|
|
364
|
+
log.line(` warning: skipped the symbolic link ${rel}/${entry} while seeding persistent data`);
|
|
365
|
+
continue;
|
|
366
|
+
}
|
|
367
|
+
fs.cpSync(src, path.join(to, entry), {
|
|
368
|
+
recursive: true, preserveTimestamps: true, force: overwrite, errorOnExist: !overwrite,
|
|
369
|
+
});
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
/**
|
|
374
|
+
* Gives every persistent path one home outside the releases and links it in.
|
|
375
|
+
*
|
|
376
|
+
* A deployment starts from a fresh checkout, and old releases are deleted —
|
|
377
|
+
* that is what makes a rollback clean, and why runtime data disappears with
|
|
378
|
+
* it. A persistent path lives once, under `<app folder>/shared/`, and every
|
|
379
|
+
* release gets a link to it, so a deploy, a rollback and a prune all see the
|
|
380
|
+
* same data: a folder with its contents, or a single file.
|
|
381
|
+
*
|
|
382
|
+
* The first deployment seeds the shared copy from the repository, so a
|
|
383
|
+
* committed empty uploads/ or a SQLite file is not lost. When the app was
|
|
384
|
+
* already running, the live release is seeded after the repository and wins:
|
|
385
|
+
* its copy is the one holding the data written until now. After that the
|
|
386
|
+
* shared copy wins — editing the path in the repository changes nothing,
|
|
387
|
+
* which is what "persistent" has to mean.
|
|
388
|
+
*
|
|
389
|
+
* A file is supported for the case where the folder is not the unit — one
|
|
390
|
+
* database file beside code that is replaced on every deploy. A folder stays
|
|
391
|
+
* the better choice when the app writes neighbours: SQLite creates `-wal` and
|
|
392
|
+
* `-shm` files next to its database, and those would remain in the release.
|
|
393
|
+
*
|
|
394
|
+
* @param {string|null} [inheritDir] The release that is live now, if any
|
|
395
|
+
*/
|
|
396
|
+
function linkPersistentPaths(appId, dir, paths, log, inheritDir = null) {
|
|
397
|
+
if (!paths || paths.length === 0) return;
|
|
398
|
+
const shared = appPaths(appId).shared;
|
|
399
|
+
fs.mkdirSync(shared, { recursive: true });
|
|
400
|
+
|
|
401
|
+
for (const rel of paths) {
|
|
402
|
+
const linkAt = persistLink(dir, rel);
|
|
403
|
+
const sharedAt = path.join(shared, ...rel.split('/'));
|
|
404
|
+
if (!isInside(linkAt, dir) || !isInside(sharedAt, shared)) {
|
|
405
|
+
throw new StepError('settings', `"${rel}" is not inside the project.`);
|
|
406
|
+
}
|
|
407
|
+
assertRealParents(dir, rel);
|
|
408
|
+
|
|
409
|
+
const inShared = entryKind(sharedAt);
|
|
410
|
+
if (inShared === 'link' || inShared === 'other') {
|
|
411
|
+
throw new StepError('settings', `"${rel}" is not a folder or a file in the app's shared data (${shared}).`);
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
let kind = inShared;
|
|
415
|
+
let seededFrom = null;
|
|
416
|
+
|
|
417
|
+
if (!inShared) {
|
|
418
|
+
const inRelease = entryKind(linkAt);
|
|
419
|
+
if (inRelease === 'link' || inRelease === 'other') {
|
|
420
|
+
throw new StepError('settings', `"${rel}" is a symbolic link in the repository, which persistent paths do not allow.`);
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
let inPrevious = null;
|
|
424
|
+
let previousAt = null;
|
|
425
|
+
if (inheritDir && path.resolve(inheritDir) !== path.resolve(dir)) {
|
|
426
|
+
previousAt = persistLink(inheritDir, rel);
|
|
427
|
+
inPrevious = entryKind(previousAt);
|
|
428
|
+
// A previous release that already had this path persistent points at
|
|
429
|
+
// the shared copy itself; there is nothing of its own to take.
|
|
430
|
+
if (inPrevious === 'link' || inPrevious === 'other') inPrevious = null;
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
const usable = (k) => k === 'dir' || k === 'file';
|
|
434
|
+
kind = usable(inPrevious) ? inPrevious : usable(inRelease) ? inRelease : (looksLikeFile(rel) ? 'file' : 'dir');
|
|
435
|
+
|
|
436
|
+
if (usable(inRelease) && inRelease !== kind) {
|
|
437
|
+
log.line(` warning: "${rel}" is a ${inRelease} in the repository, but the live copy is a ${kind}; the live copy is kept.`);
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
const sources = [];
|
|
441
|
+
if (kind === 'dir') {
|
|
442
|
+
fs.mkdirSync(sharedAt, { recursive: true });
|
|
443
|
+
if (inRelease === 'dir') {
|
|
444
|
+
copyInto(linkAt, sharedAt, log, rel);
|
|
445
|
+
sources.push('the repository');
|
|
446
|
+
}
|
|
447
|
+
if (inPrevious === 'dir') {
|
|
448
|
+
copyInto(previousAt, sharedAt, log, rel, { overwrite: true });
|
|
449
|
+
sources.push('the live release');
|
|
450
|
+
}
|
|
451
|
+
} else {
|
|
452
|
+
fs.mkdirSync(path.dirname(sharedAt), { recursive: true });
|
|
453
|
+
const from = inPrevious === 'file' ? previousAt : inRelease === 'file' ? linkAt : null;
|
|
454
|
+
if (from) {
|
|
455
|
+
fs.copyFileSync(from, sharedAt);
|
|
456
|
+
sources.push(from === linkAt ? 'the repository' : 'the live release');
|
|
457
|
+
} else {
|
|
458
|
+
// Nothing anywhere: start empty, so the app can open and fill it.
|
|
459
|
+
fs.writeFileSync(sharedAt, '');
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
if (sources.length > 0) seededFrom = sources.join(' and ');
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
// Whatever was at this path is replaced by the link: the shared copy is
|
|
466
|
+
// the one that persists.
|
|
467
|
+
removeAt(linkAt);
|
|
468
|
+
if (kind === 'file') createFileLink(sharedAt, linkAt);
|
|
469
|
+
else createDirLink(sharedAt, linkAt);
|
|
470
|
+
log.line(` persist ${rel}${kind === 'file' ? ' (file)' : ''}${seededFrom ? ` (seeded from ${seededFrom})` : ''}`);
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
/**
|
|
475
|
+
* Checks the links again after the build. A build that deletes or recreates a
|
|
476
|
+
* persistent path would put data back inside the release — where the next
|
|
477
|
+
* deployment loses it — so that is a failure, not something to fix silently.
|
|
478
|
+
*/
|
|
479
|
+
function verifyPersistentLinks(appId, dir, paths) {
|
|
480
|
+
if (!paths || paths.length === 0) return;
|
|
481
|
+
const shared = appPaths(appId).shared;
|
|
482
|
+
|
|
483
|
+
for (const rel of paths) {
|
|
484
|
+
const linkAt = persistLink(dir, rel);
|
|
485
|
+
const sharedAt = path.join(shared, ...rel.split('/'));
|
|
486
|
+
const at = entryKind(linkAt);
|
|
487
|
+
|
|
488
|
+
if (entryKind(sharedAt) === null) {
|
|
489
|
+
if (looksLikeFile(rel)) fs.writeFileSync(sharedAt, '');
|
|
490
|
+
else fs.mkdirSync(sharedAt, { recursive: true });
|
|
491
|
+
}
|
|
492
|
+
const isFile = entryKind(sharedAt) === 'file';
|
|
493
|
+
|
|
494
|
+
const linked = (at === 'link' && linkPointsAt(linkAt, sharedAt))
|
|
495
|
+
|| (at === 'file' && isFile && sameHardLink(linkAt, sharedAt));
|
|
496
|
+
if (linked) continue;
|
|
497
|
+
|
|
498
|
+
if (at === null) {
|
|
499
|
+
if (isFile) createFileLink(sharedAt, linkAt);
|
|
500
|
+
else createDirLink(sharedAt, linkAt);
|
|
501
|
+
continue;
|
|
502
|
+
}
|
|
503
|
+
throw new StepError('build', `The build replaced the persistent path "${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.`);
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
/** Every persistent link in every release of an app. */
|
|
508
|
+
function persistentLinks(appId) {
|
|
509
|
+
const out = [];
|
|
510
|
+
for (const id of listReleases(appId)) {
|
|
511
|
+
const dir = releaseDir(appId, id);
|
|
512
|
+
for (const rel of releaseHealth(dir).persist) out.push({ dir, rel });
|
|
513
|
+
}
|
|
514
|
+
return out;
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
/**
|
|
518
|
+
* Removes the links before the app's folder is moved, and recreates them after
|
|
519
|
+
* — against the new shared folder. A relative link would survive on its own;
|
|
520
|
+
* a Windows junction holds an absolute path and would point at a folder that
|
|
521
|
+
* no longer exists. Detaching first also keeps a cross-device copy from
|
|
522
|
+
* reading through the links.
|
|
523
|
+
*
|
|
524
|
+
* A hard link is removed only while it still shares its inode with the shared
|
|
525
|
+
* file: a copy left by a build belongs to the release and stays with it.
|
|
526
|
+
*/
|
|
527
|
+
function detachPersistentLinks(appId) {
|
|
528
|
+
const shared = appPaths(appId).shared;
|
|
529
|
+
for (const { dir, rel } of persistentLinks(appId)) {
|
|
530
|
+
const linkAt = persistLink(dir, rel);
|
|
531
|
+
const sharedAt = path.join(shared, ...rel.split('/'));
|
|
532
|
+
const kind = entryKind(linkAt);
|
|
533
|
+
try {
|
|
534
|
+
if (kind === 'link') fs.rmSync(linkAt, { force: true });
|
|
535
|
+
else if (kind === 'file' && sameHardLink(linkAt, sharedAt)) fs.rmSync(linkAt, { force: true });
|
|
536
|
+
} catch { /* gone already */ }
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
/** @returns {string[]} One message per path that could not be linked again */
|
|
541
|
+
function attachPersistentLinks(appId) {
|
|
542
|
+
const shared = appPaths(appId).shared;
|
|
543
|
+
const failures = [];
|
|
544
|
+
for (const { dir, rel } of persistentLinks(appId)) {
|
|
545
|
+
const linkAt = persistLink(dir, rel);
|
|
546
|
+
const sharedAt = path.join(shared, ...rel.split('/'));
|
|
547
|
+
try {
|
|
548
|
+
const kind = entryKind(linkAt);
|
|
549
|
+
if (kind === 'link') {
|
|
550
|
+
if (linkPointsAt(linkAt, sharedAt)) continue;
|
|
551
|
+
fs.rmSync(linkAt, { force: true }); // a link left pointing at the old folder
|
|
552
|
+
} else if (kind !== null) {
|
|
553
|
+
continue; // real content in the release: not the deployment's to replace
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
if (entryKind(sharedAt) === 'file') {
|
|
557
|
+
createFileLink(sharedAt, linkAt);
|
|
558
|
+
} else {
|
|
559
|
+
fs.mkdirSync(sharedAt, { recursive: true });
|
|
560
|
+
createDirLink(sharedAt, linkAt);
|
|
561
|
+
}
|
|
562
|
+
} catch (err) {
|
|
563
|
+
failures.push(`${rel}: ${err.message}`);
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
return failures;
|
|
567
|
+
}
|
|
568
|
+
|
|
244
569
|
/** The environment a deployed app (and its build) runs with. */
|
|
245
570
|
function appEnvironment(app, settings, releaseId) {
|
|
246
571
|
const env = { ...(app.env ?? {}) };
|
|
@@ -249,6 +574,9 @@ function appEnvironment(app, settings, releaseId) {
|
|
|
249
574
|
if (settings.port && env.PORT === undefined) env.PORT = String(settings.port);
|
|
250
575
|
env.EZ_APP = app.id;
|
|
251
576
|
env.EZ_RELEASE = releaseId;
|
|
577
|
+
// One stable place for data, for an app that would rather build a path than
|
|
578
|
+
// rely on the links: `${EZ_SHARED_DIR}/uploads`.
|
|
579
|
+
if (settings.persist?.length) env.EZ_SHARED_DIR = appPaths(app.id).shared;
|
|
252
580
|
return env;
|
|
253
581
|
}
|
|
254
582
|
|
|
@@ -291,9 +619,10 @@ function releaseHealth(dir) {
|
|
|
291
619
|
strict: Boolean(meta.strict),
|
|
292
620
|
sha: typeof meta.sha === 'string' ? meta.sha : null,
|
|
293
621
|
ref: typeof meta.ref === 'string' ? meta.ref : null,
|
|
622
|
+
persist: Array.isArray(meta.persist) ? meta.persist.filter((p) => typeof p === 'string') : [],
|
|
294
623
|
};
|
|
295
624
|
} catch {
|
|
296
|
-
return { port: null, healthPath: '/', strict: false, sha: null, ref: null };
|
|
625
|
+
return { port: null, healthPath: '/', strict: false, sha: null, ref: null, persist: [] };
|
|
297
626
|
}
|
|
298
627
|
}
|
|
299
628
|
|
|
@@ -541,6 +870,15 @@ async function executeDeployment(app, run, d) {
|
|
|
541
870
|
const value = Array.isArray(settings[field]) ? (settings[field].join(' && ') || '(none)') : (settings[field] ?? '(none)');
|
|
542
871
|
log.line(` ${field.padEnd(10)} ${value} [${sources[field]}]`);
|
|
543
872
|
}
|
|
873
|
+
if (settings.persist.length > 0) {
|
|
874
|
+
log.line(` persist ${settings.persist.join(', ')} [${sources.persist}]`);
|
|
875
|
+
}
|
|
876
|
+
|
|
877
|
+
// Before the hooks: a pre-deploy script that writes to the database must
|
|
878
|
+
// write to the shared copy, not to this disposable release. The live
|
|
879
|
+
// release is where the data written up to now is, so it seeds a path
|
|
880
|
+
// that is being made persistent for the first time.
|
|
881
|
+
linkPersistentPaths(app.id, dir, settings.persist, log, before.current ? releaseDir(app.id, before.current) : null);
|
|
544
882
|
|
|
545
883
|
const env = appEnvironment(app, settings, run.releaseId);
|
|
546
884
|
const strict = sources.healthPath !== 'default';
|
|
@@ -549,6 +887,7 @@ async function executeDeployment(app, run, d) {
|
|
|
549
887
|
await userCommands('pre', 'Pre-deploy hook', settings.pre, env);
|
|
550
888
|
await userCommands('install', 'Install', settings.install, env);
|
|
551
889
|
await userCommands('build', 'Build', settings.build, env);
|
|
890
|
+
verifyPersistentLinks(app.id, dir, settings.persist);
|
|
552
891
|
|
|
553
892
|
// ── 4. switch ───────────────────────────────────────────────────────────
|
|
554
893
|
setStep('start', 'Starting the new release');
|
|
@@ -562,6 +901,7 @@ async function executeDeployment(app, run, d) {
|
|
|
562
901
|
writePm2Config(dir, app, settings, env);
|
|
563
902
|
fs.writeFileSync(path.join(dir, 'ez.release.json'), JSON.stringify({
|
|
564
903
|
sha: run.sha, ref: run.ref, port: settings.port, healthPath: settings.healthPath, strict,
|
|
904
|
+
persist: settings.persist,
|
|
565
905
|
deployedAt: new Date(d.now()).toISOString(),
|
|
566
906
|
}, null, 2));
|
|
567
907
|
|
|
@@ -788,15 +1128,55 @@ export function startRollback(appId, { releaseId = null, actor = null, deps = {}
|
|
|
788
1128
|
return { ok: true, runId, done };
|
|
789
1129
|
}
|
|
790
1130
|
|
|
1131
|
+
/**
|
|
1132
|
+
* Deletes one release folder — the files of an old deployment.
|
|
1133
|
+
*
|
|
1134
|
+
* The live release and the one an automatic rollback would return to are
|
|
1135
|
+
* refused: deleting either turns the next failure into an outage. Run records
|
|
1136
|
+
* are history and stay; only the files go, and the shared data they linked to
|
|
1137
|
+
* is untouched.
|
|
1138
|
+
*
|
|
1139
|
+
* @returns {{ ok: boolean, status: number, error: string|null, deleted?: string }}
|
|
1140
|
+
*/
|
|
1141
|
+
export function removeRelease(appId, releaseId) {
|
|
1142
|
+
const app = getApp(appId);
|
|
1143
|
+
if (!app) return { ok: false, status: 404, error: `No app named "${appId}".` };
|
|
1144
|
+
if (!isId(releaseId)) return { ok: false, status: 400, error: 'Invalid release id.' };
|
|
1145
|
+
if (!listReleases(appId).includes(releaseId)) {
|
|
1146
|
+
return { ok: false, status: 404, error: `Release ${releaseId} is no longer on this server.` };
|
|
1147
|
+
}
|
|
1148
|
+
|
|
1149
|
+
const lock = acquireLock(appId, { runId: null });
|
|
1150
|
+
if (!lock.ok) return { ok: false, status: 409, error: 'A deployment of this app is running. Wait for it to finish.' };
|
|
1151
|
+
try {
|
|
1152
|
+
// Re-read under the lock: a deployment may have finished in between.
|
|
1153
|
+
const state = readState(appId);
|
|
1154
|
+
if (releaseId === state.current) {
|
|
1155
|
+
return { ok: false, status: 409, error: 'That release is live. Roll back to another release before deleting it.' };
|
|
1156
|
+
}
|
|
1157
|
+
if (releaseId === state.previous) {
|
|
1158
|
+
return { ok: false, status: 409, error: 'That release is the one an automatic rollback would return to. Delete it after the next deployment.' };
|
|
1159
|
+
}
|
|
1160
|
+
fs.rmSync(releaseDir(appId, releaseId), { recursive: true, force: true });
|
|
1161
|
+
return { ok: true, status: 200, error: null, deleted: releaseId };
|
|
1162
|
+
} catch (err) {
|
|
1163
|
+
return { ok: false, status: 500, error: `The release could not be deleted: ${err.message}` };
|
|
1164
|
+
} finally {
|
|
1165
|
+
lock.release();
|
|
1166
|
+
}
|
|
1167
|
+
}
|
|
1168
|
+
|
|
791
1169
|
/**
|
|
792
1170
|
* Removes an app: stops its process, forgets it, deletes its files.
|
|
793
1171
|
*
|
|
794
1172
|
* @param {string} appId
|
|
795
1173
|
* @param {Object} [opts]
|
|
796
1174
|
* @param {boolean} [opts.deleteDomain] Also remove the domain linked to it
|
|
797
|
-
* @
|
|
1175
|
+
* @param {boolean} [opts.purgeData] Also delete the persistent paths, not just the app
|
|
1176
|
+
* @returns {Promise<{ ok: boolean, status: number, error: string|null, domainDeleted?: boolean,
|
|
1177
|
+
* warning?: string|null, sharedKept?: boolean, sharedPath?: string }>}
|
|
798
1178
|
*/
|
|
799
|
-
export async function removeApp(appId, { deleteDomain = false, deps = {} } = {}) {
|
|
1179
|
+
export async function removeApp(appId, { deleteDomain = false, purgeData = false, deps = {} } = {}) {
|
|
800
1180
|
const app = getApp(appId);
|
|
801
1181
|
if (!app) return { ok: false, status: 404, error: `No app named "${appId}".` };
|
|
802
1182
|
|
|
@@ -820,8 +1200,13 @@ export async function removeApp(appId, { deleteDomain = false, deps = {} } = {})
|
|
|
820
1200
|
if (!removed.ok) warning = `The app was deleted, but the domain ${app.domain} was not: ${removed.error}`;
|
|
821
1201
|
}
|
|
822
1202
|
|
|
823
|
-
|
|
824
|
-
|
|
1203
|
+
// Resolved before the record goes: afterwards nothing knows this app's folder.
|
|
1204
|
+
const sharedPath = appPaths(appId).shared;
|
|
1205
|
+
const files = deleteApp(appId, { purgeData }) ?? { keptShared: false };
|
|
1206
|
+
return {
|
|
1207
|
+
ok: true, status: 200, error: null, domainDeleted, warning,
|
|
1208
|
+
sharedKept: Boolean(files.keptShared), sharedPath,
|
|
1209
|
+
};
|
|
825
1210
|
}
|
|
826
1211
|
|
|
827
1212
|
/**
|
|
@@ -928,16 +1313,22 @@ export async function moveApp(appId, targetDir, { deps = {} } = {}) {
|
|
|
928
1313
|
// An empty folder left by an earlier attempt would make the rename fail.
|
|
929
1314
|
if (fs.existsSync(to)) fs.rmdirSync(to);
|
|
930
1315
|
|
|
1316
|
+
// Detached first: a junction holds an absolute path into the old folder,
|
|
1317
|
+
// and a cross-device copy must not read through the links.
|
|
1318
|
+
detachPersistentLinks(appId);
|
|
1319
|
+
|
|
931
1320
|
try {
|
|
932
1321
|
if (fs.existsSync(from)) moveDirectory(from, to);
|
|
933
1322
|
else fs.mkdirSync(to, { recursive: true });
|
|
934
1323
|
} catch (err) {
|
|
935
1324
|
// Nothing was recorded, so the app is still where it was. Put it back up.
|
|
1325
|
+
attachPersistentLinks(appId);
|
|
936
1326
|
if (wasRunning) await startCurrent();
|
|
937
1327
|
return { ok: false, status: 500, error: `The folder could not be moved: ${err.message}` };
|
|
938
1328
|
}
|
|
939
1329
|
|
|
940
1330
|
setAppDir(appId, to);
|
|
1331
|
+
const relinkFailures = attachPersistentLinks(appId);
|
|
941
1332
|
|
|
942
1333
|
// The lock file travelled with the folder; it is ours, and the old path it
|
|
943
1334
|
// was taken at no longer exists for release() to clean up.
|
|
@@ -960,6 +1351,10 @@ export async function moveApp(appId, targetDir, { deps = {} } = {}) {
|
|
|
960
1351
|
restarted = await startCurrent();
|
|
961
1352
|
if (!restarted) warning = 'The app was moved, but PM2 could not start it again. Deploy it, or start it from the Processes page.';
|
|
962
1353
|
}
|
|
1354
|
+
if (relinkFailures.length > 0) {
|
|
1355
|
+
const note = `These persistent paths could not be linked again: ${relinkFailures.join('; ')}. Deploy the app to restore them.`;
|
|
1356
|
+
warning = warning ? `${warning} ${note}` : `The app was moved, but ${note}`;
|
|
1357
|
+
}
|
|
963
1358
|
|
|
964
1359
|
return { ok: true, status: 200, error: null, from, to, restarted, warning };
|
|
965
1360
|
} finally {
|
|
@@ -27,8 +27,11 @@
|
|
|
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", "upload"], paths that survive deployments
|
|
30
31
|
* }
|
|
31
32
|
*
|
|
33
|
+
* A persistent path is a folder or a single file.
|
|
34
|
+
*
|
|
32
35
|
* `false` (or an empty string) switches a step off — "this project has no
|
|
33
36
|
* build" is a real answer, and different from "work it out for me".
|
|
34
37
|
*/
|
|
@@ -41,9 +44,12 @@ export const DEFAULT_HEALTH_TIMEOUT_S = 60;
|
|
|
41
44
|
export const MAX_HEALTH_TIMEOUT_S = 600;
|
|
42
45
|
|
|
43
46
|
const COMMAND_FIELDS = Object.freeze(['install', 'build']);
|
|
44
|
-
const KNOWN_KEYS = Object.freeze(['$schema', 'name', 'install', 'build', 'start', 'port', 'health', 'hooks']);
|
|
47
|
+
const KNOWN_KEYS = Object.freeze(['$schema', 'name', 'install', 'build', 'start', 'port', 'health', 'hooks', 'persist']);
|
|
45
48
|
const MAX_COMMAND_LENGTH = 2000;
|
|
46
49
|
|
|
50
|
+
export const MAX_PERSIST_PATHS = 20;
|
|
51
|
+
const MAX_PERSIST_PATH_LENGTH = 200;
|
|
52
|
+
|
|
47
53
|
/** @returns {boolean} */
|
|
48
54
|
function isPlainObject(v) {
|
|
49
55
|
return v !== null && typeof v === 'object' && !Array.isArray(v);
|
|
@@ -94,6 +100,70 @@ export function normalizeHealthPath(value) {
|
|
|
94
100
|
return p;
|
|
95
101
|
}
|
|
96
102
|
|
|
103
|
+
/**
|
|
104
|
+
* A `persist` path must stay inside the project and name a folder or a file.
|
|
105
|
+
*
|
|
106
|
+
* A folder is usually the right unit: SQLite writes `-wal` and `-shm` next to
|
|
107
|
+
* its database, and an uploads folder gains new names every day. A single file
|
|
108
|
+
* is supported for the case where that really is all there is — one database
|
|
109
|
+
* file beside code that is replaced on every deploy.
|
|
110
|
+
*
|
|
111
|
+
* @returns {string|null} What is wrong with it, or null when it is usable
|
|
112
|
+
*/
|
|
113
|
+
function persistPathProblem(p) {
|
|
114
|
+
if (p.length > MAX_PERSIST_PATH_LENGTH) return `is longer than ${MAX_PERSIST_PATH_LENGTH} characters`;
|
|
115
|
+
if (/[\0\r\n]/.test(p)) return 'must be a single line';
|
|
116
|
+
if (p.startsWith('/') || /^[A-Za-z]:/.test(p)) return 'must be relative to the project folder, not an absolute path';
|
|
117
|
+
for (const segment of p.split('/')) {
|
|
118
|
+
if (segment === '' || segment === '.' || segment === '..') return 'must be a relative path without "." or ".."';
|
|
119
|
+
if (segment === '.git') return 'cannot be ".git" — the deployment manages it';
|
|
120
|
+
if (segment === 'node_modules') return 'cannot be "node_modules" — dependencies are installed fresh for each release';
|
|
121
|
+
if (/[<>:"|?*]/.test(segment)) return 'contains characters that are not allowed in a path name';
|
|
122
|
+
}
|
|
123
|
+
return null;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Normalises a `persist` field to a list of project-relative paths.
|
|
128
|
+
*
|
|
129
|
+
* Paths are written with "/" on both platforms; the engine turns them into
|
|
130
|
+
* platform paths. Overlapping paths are refused rather than merged, because
|
|
131
|
+
* two links to the same data would look independent and then diverge.
|
|
132
|
+
*
|
|
133
|
+
* @param {*} value string | string[] | false | null | undefined
|
|
134
|
+
* @returns {{ paths: string[]|null, error: string|null }}
|
|
135
|
+
* `paths: null` means "not specified here"; `[]` means "nothing persistent".
|
|
136
|
+
*/
|
|
137
|
+
export function normalizePersistPaths(value) {
|
|
138
|
+
if (value === undefined || value === null) return { paths: null, error: null };
|
|
139
|
+
if (value === false) return { paths: [], error: null };
|
|
140
|
+
|
|
141
|
+
const list = Array.isArray(value) ? value : [value];
|
|
142
|
+
if (list.length > MAX_PERSIST_PATHS) return { paths: null, error: `has more than ${MAX_PERSIST_PATHS} paths` };
|
|
143
|
+
|
|
144
|
+
const out = [];
|
|
145
|
+
for (const item of list) {
|
|
146
|
+
if (typeof item !== 'string') return { paths: null, error: 'must be project paths (folders or files), as a string or a list of strings' };
|
|
147
|
+
const p = item.trim().replace(/\\/g, '/').replace(/^\.\/+/, '').replace(/\/+$/, '');
|
|
148
|
+
if (!p) continue;
|
|
149
|
+
const problem = persistPathProblem(p);
|
|
150
|
+
if (problem) return { paths: null, error: `"${item.trim()}" ${problem}` };
|
|
151
|
+
if (!out.includes(p)) out.push(p);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// Windows and macOS treat these as the same folder; one path cannot be
|
|
155
|
+
// linked twice there, so the pair is refused everywhere.
|
|
156
|
+
for (const a of out) {
|
|
157
|
+
for (const b of out) {
|
|
158
|
+
if (a !== b && b.toLowerCase().startsWith(`${a.toLowerCase()}/`)) {
|
|
159
|
+
return { paths: null, error: `"${a}" and "${b}" overlap — keep only "${a}"` };
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
return { paths: out, error: null };
|
|
165
|
+
}
|
|
166
|
+
|
|
97
167
|
/**
|
|
98
168
|
* Parses and validates the contents of an ez.json file.
|
|
99
169
|
*
|
|
@@ -181,6 +251,12 @@ export function parseEzFile(text) {
|
|
|
181
251
|
}
|
|
182
252
|
}
|
|
183
253
|
|
|
254
|
+
if (raw.persist !== undefined && raw.persist !== null) {
|
|
255
|
+
const n = normalizePersistPaths(raw.persist);
|
|
256
|
+
if (n.error) errors.push(`ez.json: "persist" ${n.error}`);
|
|
257
|
+
else if (n.paths !== null) value.persist = n.paths;
|
|
258
|
+
}
|
|
259
|
+
|
|
184
260
|
return { ok: errors.length === 0, value: errors.length === 0 ? value : null, errors, warnings };
|
|
185
261
|
}
|
|
186
262
|
|
|
@@ -221,9 +297,10 @@ export function defaultsFromPackageJson(pkg, files = {}) {
|
|
|
221
297
|
* Validates the per-app overrides set in the dashboard or the Hub.
|
|
222
298
|
*
|
|
223
299
|
* 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
|
|
300
|
+
* `healthPath`, `healthTimeout`, `pre`, `post`, `persist`). An absent, null or
|
|
301
|
+
* empty field means "do not override", so a form with untouched inputs
|
|
302
|
+
* overrides nothing. `false` for install/build/pre/post switches that step off;
|
|
303
|
+
* `[]` or `false` for persist means "nothing persistent", overriding ez.json.
|
|
227
304
|
*
|
|
228
305
|
* @param {*} input
|
|
229
306
|
* @returns {{ ok: boolean, value: Object, errors: string[] }}
|
|
@@ -270,6 +347,12 @@ export function normalizeOverrides(input) {
|
|
|
270
347
|
}
|
|
271
348
|
}
|
|
272
349
|
|
|
350
|
+
if (input.persist !== undefined && input.persist !== null && input.persist !== '') {
|
|
351
|
+
const n = normalizePersistPaths(input.persist);
|
|
352
|
+
if (n.error) errors.push(`persist ${n.error}`);
|
|
353
|
+
else if (n.paths !== null) value.persist = n.paths;
|
|
354
|
+
}
|
|
355
|
+
|
|
273
356
|
return { ok: errors.length === 0, value, errors };
|
|
274
357
|
}
|
|
275
358
|
|
|
@@ -288,7 +371,8 @@ export function normalizeOverrides(input) {
|
|
|
288
371
|
* @returns {{
|
|
289
372
|
* ok: boolean, error: string|null,
|
|
290
373
|
* settings: { install: string[], build: string[], start: string|null, port: number|null,
|
|
291
|
-
* healthPath: string, healthTimeout: number, pre: string[], post: string[]
|
|
374
|
+
* healthPath: string, healthTimeout: number, pre: string[], post: string[],
|
|
375
|
+
* persist: string[] },
|
|
292
376
|
* sources: Object<string, 'override'|'ez.json'|'package.json'|'default'>
|
|
293
377
|
* }}
|
|
294
378
|
*/
|
|
@@ -321,6 +405,7 @@ export function resolveSettings({ ez = null, pkg = null, files = {}, overrides =
|
|
|
321
405
|
healthTimeout: pick('healthTimeout', DEFAULT_HEALTH_TIMEOUT_S),
|
|
322
406
|
pre: pick('pre', []),
|
|
323
407
|
post: pick('post', []),
|
|
408
|
+
persist: pick('persist', []),
|
|
324
409
|
};
|
|
325
410
|
|
|
326
411
|
if (!settings.start) {
|