easy-devops 1.3.6 → 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.
Files changed (78) hide show
  1. package/README.md +1 -0
  2. package/dist/ui/assets/index-Bu7af-Kj.css +1 -0
  3. package/dist/ui/assets/index-gndgYe3K.js +532 -0
  4. package/dist/ui/assets/socket-Bv0wCEgi.js +65 -0
  5. package/dist/ui/index.html +2 -2
  6. package/install.bat +14 -7
  7. package/package.json +2 -2
  8. package/src/cli/index.js +8 -0
  9. package/src/cli/managers/domain-manager.js +37 -6
  10. package/src/cli/managers/nginx-manager.js +14 -5
  11. package/src/cli/managers/node-manager.js +19 -13
  12. package/src/cli/managers/ssl-manager.js +89 -26
  13. package/src/cli/menu.js +8 -2
  14. package/src/cli/menus/cleanup.js +4 -2
  15. package/src/cli/menus/dashboard.js +22 -15
  16. package/src/cli/menus/hub.js +402 -0
  17. package/src/cli/menus/runtime.js +12 -0
  18. package/src/cli/menus/service.js +80 -13
  19. package/src/cli/menus/settings.js +46 -12
  20. package/src/cli/menus/uninstall.js +13 -5
  21. package/src/cli/menus/update.js +24 -14
  22. package/src/cli/ui/page.js +123 -0
  23. package/src/core/cert-operations.js +34 -4
  24. package/src/core/cert-reader.js +67 -22
  25. package/src/core/cert-renewal.js +65 -9
  26. package/src/core/cert-suggest.js +120 -0
  27. package/src/core/cleanup.js +221 -1
  28. package/src/core/cloudflare-ips.js +161 -0
  29. package/src/core/daemon/ipc.js +1 -1
  30. package/src/core/daemon/lock.js +296 -9
  31. package/src/core/daemon/run.js +12 -0
  32. package/src/core/daemon/stateFile.js +128 -0
  33. package/src/core/dashboardProcess.js +134 -6
  34. package/src/core/db.js +26 -1
  35. package/src/core/deploy/apps.js +36 -62
  36. package/src/core/deploy/deployKey.js +331 -0
  37. package/src/core/deploy/domainLink.js +280 -23
  38. package/src/core/deploy/engine.js +382 -63
  39. package/src/core/deploy/ezfile.js +496 -29
  40. package/src/core/deploy/githubApi.js +100 -13
  41. package/src/core/deploy/hubCommands.js +158 -18
  42. package/src/core/deploy/status.js +28 -1
  43. package/src/core/domains-db.js +1 -0
  44. package/src/core/hub/agent.js +238 -32
  45. package/src/core/hub/deployProgress.js +411 -0
  46. package/src/core/hubConnect.js +205 -0
  47. package/src/core/hubPermissions.js +48 -10
  48. package/src/core/jobs/bus.js +1 -1
  49. package/src/core/jobs/owner.js +268 -6
  50. package/src/core/nginx-conf-generator.js +124 -19
  51. package/src/core/notifications/claim.js +26 -3
  52. package/src/core/permissions.js +277 -59
  53. package/src/core/platform.js +73 -2
  54. package/src/core/runner.js +96 -40
  55. package/src/core/service/dataDir.js +34 -9
  56. package/src/core/service/index.js +203 -24
  57. package/src/core/service/runtime.js +40 -3
  58. package/src/core/service/windowsService.js +452 -107
  59. package/src/core/service/winsw.js +44 -3
  60. package/src/core/ssl/preflight.js +548 -0
  61. package/src/core/ssl/progress.js +196 -0
  62. package/src/core/ssl-issuer.js +502 -148
  63. package/src/core/validators.js +24 -0
  64. package/src/dashboard/controllers/authController.js +12 -5
  65. package/src/dashboard/controllers/deploymentsController.js +76 -11
  66. package/src/dashboard/controllers/domainsController.js +35 -1
  67. package/src/dashboard/controllers/hubController.js +4 -3
  68. package/src/dashboard/controllers/settingsController.js +73 -68
  69. package/src/dashboard/controllers/sslController.js +135 -15
  70. package/src/dashboard/public/js/app.js +42 -7
  71. package/src/dashboard/routes/deployments.js +2 -0
  72. package/src/dashboard/routes/domains.js +1 -0
  73. package/src/dashboard/server.js +42 -7
  74. package/src/dashboard/socketManager.js +19 -25
  75. package/src/dashboard/views/partials/settings-panel.ejs +29 -10
  76. package/dist/ui/assets/index-BUpPxWmp.css +0 -1
  77. package/dist/ui/assets/index-DD0JjK0Q.js +0 -475
  78. package/dist/ui/assets/socket-4tfdap-e.js +0 -65
@@ -1,20 +1,20 @@
1
1
  /**
2
2
  * core/runner.js
3
3
  *
4
- * The one PM2 wrapper. Everything that talks to PM2 — the CLI `service`
5
- * commands, the dashboard's Processes page, the hub agent's reporting — goes
6
- * through here.
4
+ * The one PM2 wrapper. Everything in Easy DevOps that talks to PM2 — the
5
+ * dashboard's Processes page, the Hub agent's process commands and telemetry,
6
+ * and the deploy engine's start/stop of deployed projects — goes through here.
7
7
  *
8
- * Why PM2 at all: Easy DevOps deliberately does not implement its own daemon.
9
- * PM2 already solves keeping a process alive, restarting it after a crash and
10
- * bringing it back after a reboot, on both platforms this project supports.
11
- * Easy DevOps' job is to manage PM2 on the user's behalf, never to reinvent it.
8
+ * PM2 is what runs the PROJECTS Easy DevOps deploys, and any PM2 app the user
9
+ * already has. Easy DevOps' own dashboard is not a PM2 app: it runs as a
10
+ * native background service (core/service/), so nothing here should assume an
11
+ * `ez-dashboard` app exists.
12
12
  *
13
13
  * Rules this module keeps:
14
14
  * - Every subprocess call goes through core/shell.js.
15
15
  * - Every OS difference comes from core/platform.js.
16
- * - PM2 is NEVER installed silently. install() exists, but the decision to
17
- * call it belongs to the CLI or the dashboard, which must ask first.
16
+ * - PM2 is NEVER installed silently. install() exists, but nothing here
17
+ * calls it: a caller must ask the user first.
18
18
  * - `run` is injectable on every function so tests can assert the exact
19
19
  * command without executing it.
20
20
  */
@@ -31,11 +31,13 @@ const PM2_TIMEOUT_MS = 60_000;
31
31
  const INSTALL_TIMEOUT_MS = 300_000;
32
32
 
33
33
  /**
34
- * Name PM2 knows the dashboard by.
34
+ * The PM2 app name the dashboard used before it became a native service.
35
35
  *
36
- * Dev mode gets its own app so a developer's instance can never be mistaken
37
- * for — or stop — the real one. The suffix follows the data directory, which
38
- * is the thing that actually differs.
36
+ * Nothing starts the dashboard under PM2 any more. The names are kept because
37
+ * an old install still has such an app on some machines, and it has to be
38
+ * recognised (and removable) rather than mistaken for one of the user's. Dev
39
+ * mode had its own name so a developer's instance could never be mistaken for
40
+ * — or stop — the real one.
39
41
  *
40
42
  * @param {boolean} dev
41
43
  * @returns {string}
@@ -44,7 +46,7 @@ export function appName(dev) {
44
46
  return dev ? 'ez-dashboard-dev' : 'ez-dashboard';
45
47
  }
46
48
 
47
- /** Both names, so callers can recognise anything Easy DevOps created. */
49
+ /** Both legacy names, so callers can recognise anything Easy DevOps created. */
48
50
  export const EZ_APP_NAMES = Object.freeze(['ez-dashboard', 'ez-dashboard-dev']);
49
51
 
50
52
  /** @returns {boolean} True when PM2 knows this app name as one of ours. */
@@ -129,30 +131,6 @@ export async function install({ run = defaultRun } = {}) {
129
131
  return { ok: r.success, output: combine(r) };
130
132
  }
131
133
 
132
- /**
133
- * Installs the pm2-logrotate module, so PM2's logs cannot grow without bound.
134
- *
135
- * A PM2 module, not an npm dependency of this project: it is installed into
136
- * the user's PM2, exactly like PM2 itself.
137
- *
138
- * @param {{ run?: Function, pm2Path?: string }} [deps]
139
- * @returns {Promise<{ ok: boolean, output: string }>}
140
- */
141
- export async function installLogrotate({ run = defaultRun, pm2Path = 'pm2' } = {}) {
142
- const r = await run(`${quoteExe(pm2Path)} install pm2-logrotate`, { timeout: INSTALL_TIMEOUT_MS });
143
- return { ok: r.success, output: combine(r) };
144
- }
145
-
146
- /**
147
- * True when pm2-logrotate is present.
148
- * @param {{ run?: Function, pm2Path?: string }} [deps]
149
- * @returns {Promise<boolean>}
150
- */
151
- export async function hasLogrotate({ run = defaultRun, pm2Path = 'pm2' } = {}) {
152
- const r = await run(`${quoteExe(pm2Path)} ls`, { timeout: PM2_TIMEOUT_MS });
153
- return /pm2-logrotate/.test(combine(r));
154
- }
155
-
156
134
  /**
157
135
  * Lists PM2 apps.
158
136
  *
@@ -185,6 +163,63 @@ export async function list({ run = defaultRun, pm2Path = 'pm2' } = {}) {
185
163
  return { ok: true, apps: parsed.map(normalizeApp), error: null };
186
164
  }
187
165
 
166
+ /**
167
+ * One app's PM2 status, keyed by name — or null when PM2 does not know it.
168
+ *
169
+ * This is the read the deploy engine uses while it watches a new release for a
170
+ * boot crash: the restart count from `jlist` is what tells "it answered once"
171
+ * apart from "it is crash-looping". It NEVER starts the PM2 daemon. A status
172
+ * read on a machine that does not use PM2 must not create one, so the pid file
173
+ * is checked first and the answer is `daemonRunning: false` instead of a
174
+ * subprocess. `isRunning` is injectable so tests can prove that.
175
+ *
176
+ * @param {string} name PM2 app name
177
+ * @param {{ run?: Function, pm2Path?: string, isRunning?: () => boolean }} [deps]
178
+ * @returns {Promise<{ ok: boolean, app: Object|null, daemonRunning: boolean, error: string|null }>}
179
+ */
180
+ export async function status(name, { run = defaultRun, pm2Path = 'pm2', isRunning = isDaemonRunning } = {}) {
181
+ const all = await statusAll(name, { run, pm2Path, isRunning });
182
+ if (!all.ok) {
183
+ return { ok: false, app: null, daemonRunning: all.daemonRunning, error: all.error };
184
+ }
185
+ return {
186
+ ok: true,
187
+ app: all.apps[0] ?? null,
188
+ apps: all.apps,
189
+ daemonRunning: true,
190
+ error: null,
191
+ };
192
+ }
193
+
194
+ /**
195
+ * Every PM2 entry with one name — a cluster app has one per worker.
196
+ *
197
+ * The same daemon rule as status(): this must never be what starts PM2. A
198
+ * deploy's boot-crash check uses it so one crashing worker among several is
199
+ * not hidden behind the first entry PM2 happens to list.
200
+ *
201
+ * @param {string} name PM2 app name
202
+ * @param {{ run?: Function, pm2Path?: string, isRunning?: () => boolean }} [deps]
203
+ * @returns {Promise<{ ok: boolean, apps: Array<Object>, daemonRunning: boolean, error: string|null }>}
204
+ */
205
+ export async function statusAll(name, { run = defaultRun, pm2Path = 'pm2', isRunning = isDaemonRunning } = {}) {
206
+ if (!isRunning()) {
207
+ return { ok: false, apps: [], daemonRunning: false, error: 'PM2 is not running.' };
208
+ }
209
+
210
+ const listed = await list({ run, pm2Path });
211
+ if (!listed.ok) {
212
+ return { ok: false, apps: [], daemonRunning: true, error: listed.error };
213
+ }
214
+
215
+ return {
216
+ ok: true,
217
+ apps: listed.apps.filter((a) => a.name === name),
218
+ daemonRunning: true,
219
+ error: null,
220
+ };
221
+ }
222
+
188
223
  /**
189
224
  * Reduces a `pm2 jlist` entry to the fields the UI needs.
190
225
  *
@@ -209,6 +244,8 @@ export function normalizeApp(app) {
209
244
  uptimeMs: startedAt && env.status === 'online' ? Math.max(0, Date.now() - startedAt) : null,
210
245
  restarts: typeof env.restart_time === 'number' ? env.restart_time : 0,
211
246
  unstableRestarts: typeof env.unstable_restarts === 'number' ? env.unstable_restarts : 0,
247
+ /** PM2 reports "cluster_mode" or "fork_mode"; anything else is fork. */
248
+ execMode: String(env.exec_mode ?? '').toLowerCase().includes('cluster') ? 'cluster' : 'fork',
212
249
  execPath: env.pm_exec_path ?? null,
213
250
  cwd: env.pm_cwd ?? null,
214
251
  user: env.username ?? null,
@@ -279,6 +316,23 @@ export async function reload(name, { run = defaultRun, pm2Path = 'pm2' } = {}) {
279
316
  return simple('reload', name, run, pm2Path);
280
317
  }
281
318
 
319
+ /**
320
+ * Zero-downtime reload of the app(s) in a config file.
321
+ *
322
+ * `--update-env` matters for a deployment: the new release's environment and
323
+ * working directory come from the file, and PM2 otherwise keeps the ones the
324
+ * process was first started with. Only valid for an app already running in
325
+ * cluster mode; a fork app cannot overlap its old and new process.
326
+ *
327
+ * @param {string} file Absolute path to the PM2 config file
328
+ * @param {{ run?: Function, pm2Path?: string }} [deps]
329
+ * @returns {Promise<{ ok: boolean, output: string }>}
330
+ */
331
+ export async function reloadConfig(file, { run = defaultRun, pm2Path = 'pm2' } = {}) {
332
+ const r = await run(`${quoteExe(pm2Path)} reload "${file}" --update-env`, { timeout: PM2_TIMEOUT_MS });
333
+ return { ok: r.success, output: combine(r) };
334
+ }
335
+
282
336
  /** Removes the app from PM2 entirely. @returns {Promise<{ ok: boolean, output: string }>} */
283
337
  export async function del(name, { run = defaultRun, pm2Path = 'pm2' } = {}) {
284
338
  return simple('delete', name, run, pm2Path);
@@ -304,7 +358,8 @@ export async function logs(name, { lines = 200, run = defaultRun, pm2Path = 'pm2
304
358
 
305
359
  /**
306
360
  * Saves the current process list, so `pm2 resurrect` can restore it.
307
- * This is what makes a reboot bring the dashboard back.
361
+ * This is what makes DEPLOYED APPS come back after a reboot; the dashboard
362
+ * itself is a native service and does not depend on it.
308
363
  * @returns {Promise<{ ok: boolean, output: string }>}
309
364
  */
310
365
  export async function save({ run = defaultRun, pm2Path = 'pm2' } = {}) {
@@ -316,7 +371,8 @@ export async function save({ run = defaultRun, pm2Path = 'pm2' } = {}) {
316
371
  * Asks PM2 for its init-system startup hook.
317
372
  *
318
373
  * Linux and macOS only. PM2 has no Windows support here and exits non-zero,
319
- * which is why Windows gets its own two mechanisms — see core/service.js.
374
+ * which is why the old Windows setup used its own mechanisms — see
375
+ * core/service/legacy.js.
320
376
  *
321
377
  * PM2 does not install the hook itself when it needs root: it prints a command
322
378
  * for the user to run under sudo. That command is returned verbatim rather
@@ -146,18 +146,23 @@ export function backupPathFor(dir, now = new Date()) {
146
146
  * @param {string} opts.to
147
147
  * @param {boolean} [opts.keepBackup] Leave the backup in place (default true)
148
148
  * @param {(line: string) => void} [opts.log]
149
- * @returns {{ ok: true, moved: boolean, backup: string|null, reason?: string }
149
+ * `originalAt` always says where the original data is afterwards: the backup
150
+ * path when the rename worked, or `from` itself when it could not be renamed
151
+ * (Windows refuses to rename a directory while a file in it is open). It is
152
+ * null only when there was no original, or it was removed on request.
153
+ *
154
+ * @returns {{ ok: true, moved: boolean, backup: string|null, originalAt: string|null, reason?: string }
150
155
  * | { ok: false, reason: string }}
151
156
  */
152
157
  export function moveDataDir({ from, to, keepBackup = true, log = () => {} }) {
153
158
  if (!fs.existsSync(from)) {
154
159
  // Nothing to move is a success: a fresh install has no per-user data, and
155
160
  // the service simply starts with an empty ProgramData directory.
156
- return { ok: true, moved: false, backup: null, reason: `${from} does not exist` };
161
+ return { ok: true, moved: false, backup: null, originalAt: null, reason: `${from} does not exist` };
157
162
  }
158
163
 
159
164
  if (path.resolve(from).toLowerCase() === path.resolve(to).toLowerCase()) {
160
- return { ok: true, moved: false, backup: null, reason: 'source and destination are the same' };
165
+ return { ok: true, moved: false, backup: null, originalAt: from, reason: 'source and destination are the same' };
161
166
  }
162
167
 
163
168
  const busy = assertNotInUse(from);
@@ -203,10 +208,12 @@ export function moveDataDir({ from, to, keepBackup = true, log = () => {} }) {
203
208
  // The copy is good and verified, so the data is safe in two places. Not
204
209
  // being able to rename the original is worth reporting but not worth
205
210
  // undoing a verified migration over.
211
+ log(`Original left in place at ${from} (it could not be renamed: ${err.code ?? err.message})`);
206
212
  return {
207
213
  ok: true,
208
214
  moved: true,
209
215
  backup: null,
216
+ originalAt: from,
210
217
  reason: `the data was copied and verified, but the original could not be renamed: ${err.message}. `
211
218
  + `It is still at ${from} — remove it by hand once you are satisfied.`,
212
219
  };
@@ -216,13 +223,30 @@ export function moveDataDir({ from, to, keepBackup = true, log = () => {} }) {
216
223
  try {
217
224
  fs.rmSync(backup, { recursive: true, force: true });
218
225
  log('Backup removed as requested.');
219
- return { ok: true, moved: true, backup: null };
226
+ return { ok: true, moved: true, backup: null, originalAt: null };
220
227
  } catch (err) {
221
- return { ok: true, moved: true, backup, reason: `the backup could not be removed: ${err.message}` };
228
+ return { ok: true, moved: true, backup, originalAt: backup, reason: `the backup could not be removed: ${err.message}` };
222
229
  }
223
230
  }
224
231
 
225
- return { ok: true, moved: true, backup };
232
+ return { ok: true, moved: true, backup, originalAt: backup };
233
+ }
234
+
235
+ /**
236
+ * One line saying where the user's original data is after a migration.
237
+ *
238
+ * Exists because 1.4.0 printed `backup at null`: the rename to a backup name
239
+ * fails on Windows while the database inside is open, the original then stays
240
+ * where it was, and `backup` is null for exactly that reason. The data was
241
+ * never lost — the message just did not say where it was.
242
+ *
243
+ * @param {{ backup?: string|null, originalAt?: string|null }} result
244
+ * @returns {string}
245
+ */
246
+ export function describeOriginal(result) {
247
+ if (result?.backup) return `the original is kept as a backup at ${result.backup}`;
248
+ if (result?.originalAt) return `the original was left in place at ${result.originalAt}; remove it by hand once you are satisfied`;
249
+ return 'there was no earlier data to keep';
226
250
  }
227
251
 
228
252
  /**
@@ -235,7 +259,8 @@ export function moveDataDir({ from, to, keepBackup = true, log = () => {} }) {
235
259
  *
236
260
  * @param {Object} [opts]
237
261
  * @param {(line: string) => void} [opts.log]
238
- * @returns {{ ok: boolean, dataDir: string, migrated: boolean, backup: string|null, reason?: string }}
262
+ * @returns {{ ok: boolean, dataDir: string, migrated: boolean, backup: string|null, originalAt?: string|null,
263
+ * reason?: string }}
239
264
  */
240
265
  export function enableServerMode({ log = () => {} } = {}) {
241
266
  if (!isWindows) {
@@ -289,7 +314,7 @@ export function enableServerMode({ log = () => {} } = {}) {
289
314
  resetDataDirCache();
290
315
  log(`Server mode enabled. Data directory: ${target}`);
291
316
 
292
- return { ok: true, dataDir: target, migrated: moved.moved, backup: moved.backup, reason: moved.reason };
317
+ return { ok: true, dataDir: target, migrated: moved.moved, backup: moved.backup, originalAt: moved.originalAt, reason: moved.reason };
293
318
  }
294
319
 
295
320
  /**
@@ -341,5 +366,5 @@ export function disableServerMode({ log = () => {} } = {}) {
341
366
  }
342
367
 
343
368
  log(`Server mode disabled. Data directory: ${target}`);
344
- return { ok: true, dataDir: target, moved: moved.moved, backup: moved.backup, reason: moved.reason };
369
+ return { ok: true, dataDir: target, moved: moved.moved, backup: moved.backup, originalAt: moved.originalAt, reason: moved.reason };
345
370
  }
@@ -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 };