easy-devops 1.4.0 → 1.4.1

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.
@@ -43,7 +43,8 @@ import {
43
43
  } from '../platform.js';
44
44
  import { run as shellRun } from '../shell.js';
45
45
  import { loadConfig } from '../config.js';
46
- import { dbGet, dbSet } from '../db.js';
46
+ import { dbGet, dbSet, closeDb, reopenDb } from '../db.js';
47
+ import { isPidAlive } from '../dashboardProcess.js';
47
48
  import { inspect as inspectLock, HEARTBEAT_INTERVAL_MS } from '../daemon/lock.js';
48
49
  import { requestStop, clearStopRequest, isStopRequested } from '../daemon/stopRequest.js';
49
50
  import { inspect as inspectOwner } from '../jobs/owner.js';
@@ -53,7 +54,7 @@ import * as systemd from './systemd.js';
53
54
  import * as windowsService from './windowsService.js';
54
55
  import * as windowsPersonal from './windowsPersonal.js';
55
56
  import * as legacy from './legacy.js';
56
- import { enableServerMode, disableServerMode } from './dataDir.js';
57
+ import { enableServerMode, disableServerMode, describeOriginal } from './dataDir.js';
57
58
  import { linkRuntime, inspectRuntime, runtimeNodePath } from './runtime.js';
58
59
 
59
60
  const __dirname = path.dirname(fileURLToPath(import.meta.url));
@@ -64,8 +65,18 @@ const STATE_KEY = 'service-state';
64
65
  /** The two Windows modes. */
65
66
  export const MODES = Object.freeze({ SERVER: 'server', PERSONAL: 'personal' });
66
67
 
67
- /** How long to wait for the daemon's heartbeat after starting a service. */
68
- export const STARTUP_GRACE_MS = 10_000;
68
+ /**
69
+ * How long to wait for the daemon's heartbeat after starting a service.
70
+ *
71
+ * 45 s, not the 10 s it was: the first start of a freshly copied node.exe on
72
+ * Windows Server is routinely slow (Defender scans the new binary before it
73
+ * may run), and 1.4.0 reported "Install failed" for a daemon that came up a
74
+ * few seconds after it stopped waiting.
75
+ */
76
+ export const STARTUP_GRACE_MS = 45_000;
77
+
78
+ /** How often a long wait says it is still waiting. */
79
+ const WAIT_PROGRESS_MS = 5_000;
69
80
 
70
81
  /** How long to wait for a stopping daemon to actually release its lock. */
71
82
  export const STOP_GRACE_MS = 15_000;
@@ -164,15 +175,20 @@ export async function suggestWindowsMode({ run = shellRun } = {}) {
164
175
  * @param {number} [opts.timeoutMs]
165
176
  * @returns {Promise<{ alive: boolean, waitedMs: number, lock: Object|null }>}
166
177
  */
167
- export async function waitForDaemon({ dataDir, timeoutMs = STARTUP_GRACE_MS } = {}) {
178
+ export async function waitForDaemon({ dataDir, timeoutMs = STARTUP_GRACE_MS, onProgress = null } = {}) {
168
179
  const started = Date.now();
169
180
  const deadline = started + timeoutMs;
181
+ let nextProgress = started + WAIT_PROGRESS_MS;
170
182
 
171
183
  while (Date.now() < deadline) {
172
184
  const state = inspectLock({ dataDir });
173
185
  if (state.running) {
174
186
  return { alive: true, waitedMs: Date.now() - started, lock: state.lock };
175
187
  }
188
+ if (onProgress && Date.now() >= nextProgress) {
189
+ nextProgress += WAIT_PROGRESS_MS;
190
+ try { onProgress(Date.now() - started, timeoutMs); } catch { /* a progress line must not break the wait */ }
191
+ }
176
192
  await new Promise((r) => setTimeout(r, Math.min(500, HEARTBEAT_INTERVAL_MS)));
177
193
  }
178
194
 
@@ -396,14 +412,68 @@ async function installWindowsServer({ entry, root, log, legacyResult, deps = {}
396
412
  // the service XML's EASY_DEVOPS_DATA_DIR — has to refer to the final
397
413
  // location, and a half-installed service pointing at the old one would see
398
414
  // an empty database.
415
+ //
416
+ // A machine already in Server mode — including one left there by a 1.4.0
417
+ // install that failed at "Registering the service" — skips the migration
418
+ // and resumes from here: enableServerMode() sees the marker and returns
419
+ // without copying, so a ProgramData database that is already the live data
420
+ // is reused rather than refused.
421
+ const migrating = !(deps.isServerMode ?? isServerMode)();
422
+
423
+ // Elevation is checked for real BEFORE anything is copied or migrated. A
424
+ // normal terminal used to get through the write probe, move the data, and
425
+ // then meet a UAC prompt for the unsigned wrapper half way through.
426
+ const elevationProbe = deps.isElevated ?? ws.isElevated;
427
+ const elevated = elevationProbe ? await elevationProbe({}) : null;
428
+ if (elevated === false) {
429
+ return {
430
+ ok: false,
431
+ mechanism: 'winsw',
432
+ mode: MODES.SERVER,
433
+ needsElevation: true,
434
+ reason: (ws.elevationMessage ?? windowsService.elevationMessage)(),
435
+ legacy: legacyResult,
436
+ };
437
+ }
438
+
439
+ if (migrating) {
440
+ // A dashboard started from the menu holds the per-user database open. It
441
+ // would make the backup rename fail, and it would keep writing to a copy
442
+ // nothing reads once the move is done.
443
+ const dashboardPid = dbGet('dashboard-pid');
444
+ if (dashboardPid && (deps.isPidAlive ?? isPidAlive)(dashboardPid)) {
445
+ return {
446
+ ok: false,
447
+ mechanism: 'winsw',
448
+ mode: MODES.SERVER,
449
+ reason: `the dashboard is running (pid ${dashboardPid}) and has the database open, so the data `
450
+ + 'cannot be moved safely. Stop it first (Easy DevOps > Dashboard > Stop), then run '
451
+ + 'ezz service install again. Nothing was changed.',
452
+ legacy: legacyResult,
453
+ };
454
+ }
455
+ }
456
+
399
457
  log('Switching to the machine-wide data directory…');
400
- const switched = (deps.enableServerMode ?? enableServerMode)({ log });
458
+
459
+ // This process's own connection is closed for the move, so Windows can
460
+ // rename the original to its backup name, then reopened on whichever
461
+ // directory getDataDir() now answers — the new one on success, the old one
462
+ // if the switch was refused. Everything after this (loadConfig, record)
463
+ // must read and write the live database, not the abandoned copy.
464
+ (deps.closeDb ?? closeDb)();
465
+ let switched;
466
+ try {
467
+ switched = (deps.enableServerMode ?? enableServerMode)({ log });
468
+ } finally {
469
+ (deps.reopenDb ?? reopenDb)();
470
+ }
401
471
  if (!switched.ok) {
402
472
  return { ok: false, mechanism: 'winsw', mode: MODES.SERVER, reason: switched.reason, legacy: legacyResult };
403
473
  }
404
474
 
405
475
  const dataDir = switched.dataDir;
406
- if (switched.migrated) log(` migrated; backup at ${switched.backup}`);
476
+ if (switched.migrated) log(` migrated; ${describeOriginal(switched)}`);
407
477
 
408
478
  const config = loadConfig();
409
479
 
@@ -414,43 +484,90 @@ async function installWindowsServer({ entry, root, log, legacyResult, deps = {}
414
484
  log,
415
485
  ...(deps.windowsServiceOpts ?? {}),
416
486
  });
487
+
488
+ // Recorded as soon as the service is registered and the grants are in —
489
+ // not only after the heartbeat. Otherwise a slow first start left a
490
+ // registered, granted service with no record, and `status` compared its
491
+ // ACLs against an empty list.
492
+ if (installed.ok || installed.registered) {
493
+ record({
494
+ mode: MODES.SERVER,
495
+ mechanism: 'winsw',
496
+ nodeExe: installed.runtime?.nodeExe ?? null,
497
+ entry: installed.runtime?.entry ?? entry,
498
+ dataDir,
499
+ granted: installed.granted ?? [],
500
+ });
501
+ }
502
+
417
503
  if (!installed.ok) {
504
+ // Server mode is deliberately KEPT after a failed install. The data was
505
+ // copied and verified and the original is still on disk, so nothing is at
506
+ // risk; moving it back and forth would be. Any half-registered service has
507
+ // already been removed by windowsService.install(), and running
508
+ // `ezz service install` again resumes from this point.
509
+ const resume = installed.needsElevation
510
+ ? ''
511
+ : `\n\nServer mode stays enabled (data in ${dataDir}); after fixing the cause, run `
512
+ + 'ezz service install again to finish — it resumes from here.';
418
513
  return {
419
514
  ok: false,
420
515
  mechanism: 'winsw',
421
516
  mode: MODES.SERVER,
422
- reason: installed.reason,
517
+ reason: `${installed.reason}${resume}`,
423
518
  needsElevation: installed.needsElevation ?? false,
519
+ registrationFailed: installed.registrationFailed ?? false,
424
520
  dataDir,
425
521
  legacy: legacyResult,
426
522
  };
427
523
  }
428
524
 
429
- const alive = await waitForDaemon({ dataDir, timeoutMs: deps.startupGraceMs ?? STARTUP_GRACE_MS });
525
+ log('Waiting for the daemon to report in (the first start can take a while)…');
526
+ const alive = await waitForDaemon({
527
+ dataDir,
528
+ timeoutMs: deps.startupGraceMs ?? STARTUP_GRACE_MS,
529
+ onProgress: (waited, limit) => log(` still waiting… ${Math.round(waited / 1000)}s of ${Math.round(limit / 1000)}s`),
530
+ });
430
531
  if (!alive.alive) {
532
+ // Out of time is not the same as failed. If Windows reports the service
533
+ // RUNNING, the daemon is most likely still initialising (a first start of
534
+ // the copied node.exe is slow while Defender scans it): the install is
535
+ // done, and saying "Install failed" would be wrong.
536
+ const scState = ws.queryState ? await ws.queryState({}) : null;
537
+ if (scState === 'RUNNING' || scState === 'START_PENDING') {
538
+ return {
539
+ ok: true,
540
+ stillInitialising: true,
541
+ mechanism: 'winsw',
542
+ mode: MODES.SERVER,
543
+ dataDir,
544
+ migrated: switched.migrated,
545
+ backup: switched.backup,
546
+ originalAt: switched.originalAt ?? null,
547
+ granted: installed.granted,
548
+ aclFailures: installed.aclFailures,
549
+ runtime: installed.runtime,
550
+ daemon: { pid: null, waitedMs: alive.waitedMs },
551
+ detail: 'The service is started and still initialising; check with ezz service status in a minute.',
552
+ legacy: legacyResult,
553
+ };
554
+ }
555
+
431
556
  const logs = ws.readLogs({ lines: 25, which: 'err' });
432
557
  return {
433
558
  ok: false,
434
559
  mechanism: 'winsw',
435
560
  mode: MODES.SERVER,
436
561
  reason: `the service was registered and started, but the daemon did not report in within `
437
- + `${Math.round(alive.waitedMs / 1000)}s — so it is starting and failing.\n\n`
438
- + `Last log lines (${logs.file}):\n${logs.output}`,
562
+ + `${Math.round(alive.waitedMs / 1000)}s and Windows reports it ${scState ? scState.toLowerCase() : 'in an unknown state'}.\n\n`
563
+ + `Last log lines (${logs.file}):\n${logs.output}\n\n`
564
+ + `More: ${windowsService.TROUBLESHOOTING_URL}#logs`,
439
565
  installedButNotRunning: true,
440
566
  dataDir,
441
567
  legacy: legacyResult,
442
568
  };
443
569
  }
444
570
 
445
- record({
446
- mode: MODES.SERVER,
447
- mechanism: 'winsw',
448
- nodeExe: installed.runtime?.nodeExe ?? null,
449
- entry: installed.runtime?.entry ?? entry,
450
- dataDir,
451
- granted: installed.granted ?? [],
452
- });
453
-
454
571
  return {
455
572
  ok: true,
456
573
  mechanism: 'winsw',
@@ -458,6 +575,7 @@ async function installWindowsServer({ entry, root, log, legacyResult, deps = {}
458
575
  dataDir,
459
576
  migrated: switched.migrated,
460
577
  backup: switched.backup,
578
+ originalAt: switched.originalAt ?? null,
461
579
  granted: installed.granted,
462
580
  aclFailures: installed.aclFailures,
463
581
  runtime: installed.runtime,
@@ -471,6 +589,25 @@ async function installWindowsPersonal({ entry, log, legacyResult, deps = {} }) {
471
589
  const dataDir = getDataDir();
472
590
  const wp = deps.windowsPersonal ?? windowsPersonal;
473
591
 
592
+ // Personal mode runs as the logged-in user. On a machine already in Server
593
+ // mode the live data is under ProgramData, written by an elevated install,
594
+ // and a per-user daemon there could not write its own database. So the data
595
+ // has to come back to %APPDATA% first, through the uninstall question that
596
+ // already asks exactly that — never silently from here.
597
+ if ((deps.isServerMode ?? isServerMode)()) {
598
+ return {
599
+ ok: false,
600
+ mechanism: 'startup-supervisor',
601
+ mode: MODES.PERSONAL,
602
+ reason: `this machine is in Server mode: the data is in ${programDataDir()}.\n`
603
+ + 'Personal mode needs it back in %APPDATA% first. In an Administrator PowerShell run\n'
604
+ + ' ezz service uninstall\n'
605
+ + 'and choose to move the data back, then install again and pick Personal mode.\n'
606
+ + 'Or answer Yes to Server mode to finish the Server install instead.',
607
+ legacy: legacyResult,
608
+ };
609
+ }
610
+
474
611
  const installed = wp.install({ dataDir, entry, log, ...(deps.windowsPersonalOpts ?? {}) });
475
612
  if (!installed.ok) {
476
613
  return { ok: false, mechanism: 'startup-supervisor', mode: MODES.PERSONAL, reason: installed.reason, legacy: legacyResult };
@@ -551,12 +688,22 @@ export async function uninstall({
551
688
  result.dataDir = programDataDir();
552
689
  } else {
553
690
  log('Moving the data back to %APPDATA%…');
554
- const moved = (deps.disableServerMode ?? disableServerMode)({ log });
691
+ // Same reason as on install: this process's own handle would stop
692
+ // Windows renaming the ProgramData directory, and every write after the
693
+ // move must reach the database that is now live.
694
+ (deps.closeDb ?? closeDb)();
695
+ let moved;
696
+ try {
697
+ moved = (deps.disableServerMode ?? disableServerMode)({ log });
698
+ } finally {
699
+ (deps.reopenDb ?? reopenDb)();
700
+ }
555
701
  if (!moved.ok) return { ok: false, ...result, reason: moved.reason };
556
702
  result.dataDir = moved.dataDir;
557
703
  result.movedData = moved.moved;
558
704
  result.backup = moved.backup;
559
- if (moved.backup) result.notes.push(`A backup was kept at ${moved.backup}`);
705
+ result.originalAt = moved.originalAt ?? null;
706
+ if (moved.moved) result.notes.push(`Data moved back: ${describeOriginal(moved)}.`);
560
707
  }
561
708
  } else {
562
709
  const removed = (deps.windowsPersonal ?? windowsPersonal).uninstall({ log, ...(deps.windowsPersonalOpts ?? {}) });
@@ -957,12 +1104,37 @@ export async function status({ cache = false, coldWaitMs, deps = {} } = {}) {
957
1104
  };
958
1105
 
959
1106
  if (out.service.crashLooping) {
1107
+ const crashes = out.service.crashRestarts ?? out.service.restarts;
960
1108
  out.warnings.push(
961
- `The service is in a crash loop: ${out.service.restarts} restart(s) recorded. `
1109
+ `The service is in a crash loop: ${crashes} unclean restart(s) in the last 10 minutes. `
962
1110
  + 'It starts and dies rather than staying up. `ezz service logs` shows why.',
963
1111
  );
964
1112
  }
965
1113
 
1114
+ // The record and Windows must agree. When they do not, say which way and
1115
+ // what to run — 1.4.0 reported "Installed: yes" for a service Windows did
1116
+ // not have (sc query: 1060).
1117
+ if (isWindows && mech.mode === MODES.SERVER) {
1118
+ if (out.service.registered === false && (recorded.mechanism === 'winsw' || out.service.filesPresent)) {
1119
+ out.warnings.push(
1120
+ 'Easy DevOps has a Server-mode install on record, but Windows has no EasyDevOps service '
1121
+ + '(sc.exe query: 1060). Run `ezz service install` in an Administrator PowerShell to register it again.',
1122
+ );
1123
+ } else if (out.service.mismatch) {
1124
+ out.warnings.push(`${out.service.mismatch}. Run \`ezz service install\` in an Administrator PowerShell.`);
1125
+ }
1126
+
1127
+ // A daemon holding the lock while Windows says the service is not running
1128
+ // was started some other way (by hand, or a dashboard) — worth naming.
1129
+ if (out.service.installed && out.service.state && !out.service.running && lock.running
1130
+ && out.service.state !== 'unknown') {
1131
+ out.warnings.push(
1132
+ `Windows reports the service ${out.service.state}, but a daemon is running (pid ${lock.lock?.pid}) — `
1133
+ + 'it was not started by the service.',
1134
+ );
1135
+ }
1136
+ }
1137
+
966
1138
  // Installed but the daemon is not reporting in: the most useful single thing
967
1139
  // status can tell a user, and the one a service-manager-only check misses.
968
1140
  if (out.service.installed && out.service.running && !lock.running) {
@@ -1076,6 +1248,13 @@ export async function repair({ log = () => {}, deps = {} } = {}) {
1076
1248
  appRoot: appRoot(),
1077
1249
  config: loadConfig(),
1078
1250
  log,
1251
+ // Repair restores the previous runtime if the daemon does not come up,
1252
+ // and "came up" means its heartbeat — not the service manager's word.
1253
+ waitForStart: () => waitForDaemon({
1254
+ dataDir,
1255
+ timeoutMs: deps.startupGraceMs ?? STARTUP_GRACE_MS,
1256
+ onProgress: (waited, limit) => log(` still waiting… ${Math.round(waited / 1000)}s of ${Math.round(limit / 1000)}s`),
1257
+ }),
1079
1258
  ...(deps.windowsServiceOpts ?? {}),
1080
1259
  });
1081
1260
  if (result.ok) {
@@ -1125,4 +1304,4 @@ export function recordRuntime({ nodeExe }) {
1125
1304
  dbSet(STATE_KEY, { ...existing, nodeExe, runtimeUpdatedAt: new Date().toISOString() });
1126
1305
  }
1127
1306
 
1128
- export { legacy, runtimeNodePath };
1307
+ export { legacy, runtimeNodePath, describeOriginal };
@@ -198,6 +198,31 @@ export function inspectLink(dataDir) {
198
198
  */
199
199
  const COPY_ENTRIES = ['package.json', 'src', 'node_modules', 'dist'];
200
200
 
201
+ /** Path equality as Windows sees it: case-insensitive, separators normalised. */
202
+ function samePath(a, b) {
203
+ return path.resolve(a).toLowerCase() === path.resolve(b).toLowerCase();
204
+ }
205
+
206
+ /**
207
+ * copyFileSync, retried briefly on EBUSY/EPERM.
208
+ *
209
+ * A service that has just reported STOPPED can hold node.exe for a moment
210
+ * longer while Windows tears the process down; a few hundred milliseconds of
211
+ * patience turns that race into a success. Anything still locked after the
212
+ * retries is a real lock, and the error says so.
213
+ */
214
+ function copyFileRetrying(from, to, { tries = 10, delayMs = 300 } = {}) {
215
+ for (let attempt = 1; ; attempt++) {
216
+ try {
217
+ fs.copyFileSync(from, to);
218
+ return;
219
+ } catch (err) {
220
+ if (attempt >= tries || !(err.code === 'EBUSY' || err.code === 'EPERM')) throw err;
221
+ Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, delayMs);
222
+ }
223
+ }
224
+ }
225
+
201
226
  /**
202
227
  * Copies node.exe and the application into the runtime directory.
203
228
  *
@@ -224,10 +249,22 @@ export function copyRuntime({ dataDir, appRoot, nodeExe = process.execPath, log
224
249
  }
225
250
 
226
251
  try {
227
- fs.copyFileSync(fs.realpathSync(nodeExe), destNode);
228
- log(`Copied node.exe → ${destNode}`);
252
+ const source = fs.realpathSync(nodeExe);
253
+ // The updater refreshes the copy FROM the copy's own node.exe; copying a
254
+ // file onto itself is at best pointless and at worst a sharing violation.
255
+ if (fs.existsSync(destNode) && samePath(source, fs.realpathSync(destNode))) {
256
+ log(`node.exe is already the runtime copy (${destNode}); kept`);
257
+ } else {
258
+ copyFileRetrying(source, destNode);
259
+ log(`Copied node.exe → ${destNode}`);
260
+ }
229
261
  } catch (err) {
230
- return { ok: false, reason: `could not copy node.exe: ${err.message}` };
262
+ const locked = err.code === 'EBUSY' || err.code === 'EPERM';
263
+ return {
264
+ ok: false,
265
+ reason: `could not copy node.exe: ${err.message}`
266
+ + (locked ? '. Windows locks a running executable: stop the service first (ezz service stop).' : ''),
267
+ };
231
268
  }
232
269
 
233
270
  for (const entry of COPY_ENTRIES) {