easy-devops 1.2.1 → 1.3.0

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 (126) hide show
  1. package/README.md +97 -6
  2. package/dist/ui/assets/index-B6zdwEnG.css +1 -0
  3. package/dist/ui/assets/index-Cy0oNmul.js +442 -0
  4. package/dist/ui/assets/socket-DwLPrgsr.js +1 -0
  5. package/dist/ui/assets/socket-K5ttLD2q.js +65 -0
  6. package/dist/ui/brand/apple-touch-icon.png +0 -0
  7. package/dist/ui/brand/favicon-16.png +0 -0
  8. package/dist/ui/brand/favicon-32.png +0 -0
  9. package/dist/ui/brand/favicon-light-16.png +0 -0
  10. package/dist/ui/brand/favicon-light-32.png +0 -0
  11. package/dist/ui/brand/favicon.ico +0 -0
  12. package/dist/ui/brand/icon-192.png +0 -0
  13. package/dist/ui/brand/icon-48.png +0 -0
  14. package/dist/ui/brand/icon-512.png +0 -0
  15. package/dist/ui/brand/icon-64-light.png +0 -0
  16. package/dist/ui/brand/icon-64.png +0 -0
  17. package/dist/ui/brand/logo-dark.png +0 -0
  18. package/dist/ui/brand/logo-light.png +0 -0
  19. package/dist/ui/brand/mark-dark.png +0 -0
  20. package/dist/ui/brand/mark-light.png +0 -0
  21. package/dist/ui/index.html +71 -0
  22. package/install.ps1 +188 -1
  23. package/install.sh +254 -29
  24. package/package.json +15 -4
  25. package/src/cli/dash.js +110 -0
  26. package/src/cli/index.js +237 -106
  27. package/src/cli/managers/domain-manager.js +60 -40
  28. package/src/cli/managers/ssl-manager.js +47 -330
  29. package/src/cli/menu.js +156 -0
  30. package/src/cli/menus/cleanup.js +205 -0
  31. package/src/cli/menus/dashboard.js +92 -1
  32. package/src/cli/menus/runtime.js +236 -0
  33. package/src/cli/menus/service.js +764 -0
  34. package/src/cli/menus/settings.js +176 -8
  35. package/src/cli/menus/uninstall.js +167 -0
  36. package/src/cli/menus/update.js +221 -65
  37. package/src/core/acme-challenge.js +273 -0
  38. package/src/core/cert-operations.js +189 -0
  39. package/src/{dashboard/lib → core}/cert-reader.js +2 -2
  40. package/src/core/cert-renewal.js +494 -0
  41. package/src/core/cleanup.js +600 -0
  42. package/src/core/config.js +169 -2
  43. package/src/core/daemon/ipc.js +292 -0
  44. package/src/core/daemon/lock.js +294 -0
  45. package/src/core/daemon/run.js +241 -0
  46. package/src/core/daemon/stopRequest.js +162 -0
  47. package/src/core/db.js +37 -8
  48. package/src/core/deploy/apps.js +491 -0
  49. package/src/core/deploy/domainLink.js +200 -0
  50. package/src/core/deploy/engine.js +973 -0
  51. package/src/core/deploy/ezfile.js +361 -0
  52. package/src/core/deploy/github.js +239 -0
  53. package/src/core/deploy/githubApi.js +261 -0
  54. package/src/core/deploy/hookServer.js +325 -0
  55. package/src/core/deploy/hubCommands.js +267 -0
  56. package/src/core/deploy/launcher.js +57 -0
  57. package/src/core/deploy/status.js +91 -0
  58. package/src/core/deploy/store.js +398 -0
  59. package/src/core/domainNotifier.js +94 -28
  60. package/src/core/domains-db.js +234 -0
  61. package/src/core/events.js +94 -2
  62. package/src/core/hub/agent.js +842 -0
  63. package/src/core/hubLink.js +306 -0
  64. package/src/core/hubPermissions.js +430 -0
  65. package/src/core/installSource.js +162 -0
  66. package/src/core/jobs/bus.js +120 -0
  67. package/src/core/jobs/index.js +371 -0
  68. package/src/core/jobs/monitors.js +368 -0
  69. package/src/core/jobs/owner.js +217 -0
  70. package/src/core/jobs/revisionWatcher.js +106 -0
  71. package/src/core/nginx-apply.js +213 -0
  72. package/src/core/nginx-conf-generator.js +253 -17
  73. package/src/{dashboard/lib → core}/nginx-service.js +18 -9
  74. package/src/core/notifications/claim.js +214 -0
  75. package/src/core/notifications/dispatch.js +154 -0
  76. package/src/core/notifier.js +31 -1
  77. package/src/core/passwords.js +172 -0
  78. package/src/core/permissions.js +8 -4
  79. package/src/core/platform.js +709 -2
  80. package/src/core/probeCache.js +258 -0
  81. package/src/core/purge.js +372 -0
  82. package/src/core/runner.js +488 -0
  83. package/src/core/service/acl.js +226 -0
  84. package/src/core/service/dataDir.js +345 -0
  85. package/src/core/service/index.js +1128 -0
  86. package/src/core/service/legacy.js +218 -0
  87. package/src/core/service/runtime.js +446 -0
  88. package/src/core/service/runtimeUpdate.js +800 -0
  89. package/src/core/service/systemd.js +458 -0
  90. package/src/core/service/windowsPersonal.js +530 -0
  91. package/src/core/service/windowsService.js +576 -0
  92. package/src/core/service/winsw.js +260 -0
  93. package/src/core/setupCode.js +331 -0
  94. package/src/core/shell.js +70 -7
  95. package/src/core/ssl-issuer.js +627 -0
  96. package/src/core/updater.js +295 -0
  97. package/src/core/validators.js +99 -0
  98. package/src/dashboard/controllers/authController.js +302 -15
  99. package/src/dashboard/controllers/deploymentsController.js +479 -0
  100. package/src/dashboard/controllers/domainsController.js +39 -52
  101. package/src/dashboard/controllers/hubController.js +135 -0
  102. package/src/dashboard/controllers/nginxController.js +21 -3
  103. package/src/dashboard/controllers/processesController.js +368 -0
  104. package/src/dashboard/controllers/settingsController.js +180 -12
  105. package/src/dashboard/controllers/sslController.js +171 -10
  106. package/src/dashboard/lib/daemonBridge.js +163 -0
  107. package/src/dashboard/lib/localRequest.js +235 -0
  108. package/src/dashboard/lib/loginRateLimit.js +119 -0
  109. package/src/dashboard/lib/requireAuth.js +80 -0
  110. package/src/dashboard/public/js/app.js +489 -19
  111. package/src/dashboard/routes/auth.js +10 -10
  112. package/src/dashboard/routes/deployments.js +46 -0
  113. package/src/dashboard/routes/domains.js +9 -0
  114. package/src/dashboard/routes/hub.js +22 -0
  115. package/src/dashboard/routes/nginx.js +9 -0
  116. package/src/dashboard/routes/processes.js +29 -0
  117. package/src/dashboard/routes/settings.js +9 -0
  118. package/src/dashboard/routes/ssl.js +9 -0
  119. package/src/dashboard/server.js +288 -21
  120. package/src/dashboard/socketManager.js +171 -207
  121. package/src/dashboard/views/index.ejs +1 -0
  122. package/src/dashboard/views/partials/login.ejs +68 -7
  123. package/src/dashboard/views/partials/processes-panel.ejs +261 -0
  124. package/src/dashboard/views/partials/settings-panel.ejs +287 -0
  125. package/src/dashboard/views/partials/sidebar.ejs +2 -0
  126. package/src/dashboard/lib/domains-db.js +0 -51
@@ -0,0 +1,295 @@
1
+ /**
2
+ * core/updater.js
3
+ *
4
+ * Updating Easy DevOps itself.
5
+ *
6
+ * ── Why this is not a dashboard feature, and not a Hub command ─────────────
7
+ *
8
+ * The process that would perform the update is the process being replaced.
9
+ * `npm install -g` overwrites the package directory the dashboard is running
10
+ * from, and the background service has to be stopped so the files it holds
11
+ * open can be replaced — on Windows an open handle makes the install fail
12
+ * outright. A request that arrives over HTTP or over the Hub socket cannot
13
+ * survive its own server being stopped, so it could report neither success nor
14
+ * failure: the connection would simply end mid-update, and whatever happened
15
+ * next would be invisible.
16
+ *
17
+ * Doing it from a terminal means someone is watching, the output is visible,
18
+ * and a failure can be rolled back while they are still there. That is not a
19
+ * limitation to be engineered around; it is the correct shape for an operation
20
+ * that replaces the running code. A later design could hand the work to a
21
+ * short-lived detached helper that reports back afterwards — until then, the
22
+ * CLI is the honest answer and the output says so.
23
+ *
24
+ * ── Why the install source is detected first ───────────────────────────────
25
+ *
26
+ * On a git clone, `npm install -g easy-devops` installs a SECOND copy from the
27
+ * registry and leaves the service running the clone. The update then reports
28
+ * success and changes nothing that runs. So the source is established before
29
+ * anything else, and a clone is told to `git pull` instead — which this tool
30
+ * deliberately does not run on the user's behalf: a pull can conflict, and the
31
+ * working tree may hold work in progress. See core/installSource.js.
32
+ */
33
+
34
+ import path from 'path';
35
+ import { createRequire } from 'module';
36
+
37
+ import { run } from './shell.js';
38
+ import { getDataDir, isWindows } from './platform.js';
39
+ import { closeDb, initDb } from './db.js';
40
+ import { detectInstallSource, describeUnmanaged } from './installSource.js';
41
+ import { inspect as inspectLock } from './daemon/lock.js';
42
+ import { loadConfig } from './config.js';
43
+ import { copyRuntime, runtimeDir } from './service/runtime.js';
44
+ import { MODES } from './service/index.js';
45
+
46
+ const require = createRequire(import.meta.url);
47
+
48
+ /** The npm package name, in one place. */
49
+ export const PACKAGE = 'easy-devops';
50
+
51
+ /** How long to wait for the daemon to report in after the restart. */
52
+ export const VERIFY_MS = 10_000;
53
+
54
+ /** This package's version, read fresh so a post-update read is accurate. */
55
+ export function currentVersion() {
56
+ return String(require('../../package.json').version);
57
+ }
58
+
59
+ /** -1 / 0 / 1 by semver-ish triple. */
60
+ export function compareVersions(a, b) {
61
+ const parse = (v) => String(v ?? '').replace(/^v/, '').split('.').map((n) => Number(n) || 0);
62
+ const pa = parse(a);
63
+ const pb = parse(b);
64
+ for (let i = 0; i < 3; i += 1) {
65
+ if ((pa[i] ?? 0) !== (pb[i] ?? 0)) return (pa[i] ?? 0) > (pb[i] ?? 0) ? 1 : -1;
66
+ }
67
+ return 0;
68
+ }
69
+
70
+ /**
71
+ * What the registry has.
72
+ *
73
+ * @param {Object} [opts]
74
+ * @returns {Promise<{ reachable: boolean, current: string, latest: string|null, updateAvailable: boolean }>}
75
+ */
76
+ export async function checkForUpdate({ deps = {} } = {}) {
77
+ const exec = deps.run ?? run;
78
+ const current = deps.currentVersion ? deps.currentVersion() : currentVersion();
79
+
80
+ const result = await exec(`npm view ${PACKAGE} version`, { timeout: 30_000 });
81
+ const latest = result.success ? result.stdout.trim().split('\n').pop()?.trim() : null;
82
+
83
+ if (!latest || !/^\d+\.\d+\.\d+/.test(latest)) {
84
+ return { reachable: false, current, latest: null, updateAvailable: false };
85
+ }
86
+
87
+ return {
88
+ reachable: true,
89
+ current,
90
+ latest,
91
+ updateAvailable: compareVersions(latest, current) > 0,
92
+ };
93
+ }
94
+
95
+ /**
96
+ * Updates the package, restarts the service, verifies it, and rolls back if it
97
+ * did not come up.
98
+ *
99
+ * The dashboard is NOT handled here: a detached dashboard is a CLI concern
100
+ * (cli/menus/dashboard.js owns starting and stopping it) and core must not
101
+ * import from cli/. The caller stops and restarts it around this call.
102
+ *
103
+ * @param {Object} opts
104
+ * @param {string} opts.version Target version
105
+ * @param {(line: string) => void} [opts.log]
106
+ * @returns {Promise<Object>}
107
+ */
108
+ export async function performUpdate({
109
+ version,
110
+ dataDir = getDataDir(),
111
+ log = () => {},
112
+ deps = {},
113
+ } = {}) {
114
+ const exec = deps.run ?? run;
115
+ const service = deps.service ?? (await import('./service/index.js'));
116
+ const source = (deps.detectInstallSource ?? detectInstallSource)();
117
+ const from = deps.currentVersion ? deps.currentVersion() : currentVersion();
118
+
119
+ // 1. Can npm update THIS copy at all?
120
+ if (!source.npmUpdatable) {
121
+ return {
122
+ ok: false,
123
+ step: 'source',
124
+ source,
125
+ from,
126
+ // Not an error the caller has to phrase: the module that knows why also
127
+ // knows what to run instead.
128
+ lines: describeUnmanaged(source, 'update'),
129
+ reason: `this copy is ${source.reason}, which npm does not update in place`,
130
+ untouched: true,
131
+ };
132
+ }
133
+
134
+ // 2. What is installed, decided BEFORE the files are replaced.
135
+ let installed = null;
136
+ try {
137
+ installed = await service.status();
138
+ } catch { /* unreadable: treated as "no service" below */ }
139
+ const serviceInstalled = Boolean(installed?.service?.installed);
140
+ const serverMode = installed?.mode === MODES.SERVER;
141
+
142
+ // 3. Stop the service. Its files are the ones npm is about to replace, and
143
+ // on Windows an open handle makes the install fail rather than wait.
144
+ if (serviceInstalled) {
145
+ log('Stopping the background service...');
146
+ const stopped = await service.stop({ log });
147
+ if (!stopped.ok && !stopped.alreadyStopped) {
148
+ return {
149
+ ok: false,
150
+ step: 'stop',
151
+ from,
152
+ reason: stopped.reason,
153
+ needsElevation: stopped.needsElevation ?? false,
154
+ untouched: true,
155
+ };
156
+ }
157
+ } else {
158
+ log('No background service is installed; updating the package only.');
159
+ }
160
+
161
+ // 4. Replace the package. The SQLite handle is closed first: on Windows an
162
+ // open database file cannot be renamed, and npm renames the whole directory.
163
+ closeDb();
164
+
165
+ log(`Installing ${PACKAGE}@${version}...`);
166
+ const install = await exec(`npm install -g ${PACKAGE}@${version}`, { timeout: 600_000 });
167
+
168
+ initDb();
169
+
170
+ if (!install.success) {
171
+ const restarted = serviceInstalled ? await service.start({ log }) : { ok: true };
172
+ return {
173
+ ok: false,
174
+ step: 'install',
175
+ from,
176
+ reason: `npm install -g ${PACKAGE}@${version} failed:\n${trim(install)}`,
177
+ restarted: restarted.ok,
178
+ };
179
+ }
180
+
181
+ // 5. Windows Server mode runs from a COPY under ProgramData, so replacing
182
+ // the package has not changed what the service executes. Refresh it, or the
183
+ // update silently does nothing to the running service.
184
+ let copied = null;
185
+ if (isWindows && serverMode) {
186
+ log('Refreshing the service runtime copy...');
187
+ copied = (deps.copyRuntime ?? copyRuntime)({
188
+ dataDir,
189
+ appRoot: source.root,
190
+ nodeExe: path.join(runtimeDir(dataDir), 'node.exe'),
191
+ log: (line) => log(` ${line}`),
192
+ });
193
+ if (!copied.ok) {
194
+ const rolled = await rollback({ version: from, service, exec, serviceInstalled, log });
195
+ return {
196
+ ok: false,
197
+ step: 'copy',
198
+ from,
199
+ reason: `the package was updated but the service copy could not be refreshed: ${copied.reason}`,
200
+ rolledBack: rolled.ok,
201
+ restored: rolled.started,
202
+ };
203
+ }
204
+ }
205
+
206
+ // 6. Start, and wait for the daemon's own heartbeat.
207
+ if (!serviceInstalled) {
208
+ return { ok: true, from, to: version, serviceInstalled: false, source };
209
+ }
210
+
211
+ log('Starting the service on the new version...');
212
+ const started = await service.start({ log });
213
+ const alive = started.ok
214
+ ? await service.waitForDaemon({ dataDir, timeoutMs: deps.verifyMs ?? VERIFY_MS })
215
+ : { alive: false, waitedMs: 0 };
216
+
217
+ if (started.ok && alive.alive) {
218
+ // The lock carries the version the running daemon reports, which is the
219
+ // only proof that the NEW code is what came up.
220
+ const running = (deps.inspectLock ?? inspectLock)({ dataDir }).lock?.version ?? null;
221
+ return {
222
+ ok: true,
223
+ from,
224
+ to: version,
225
+ serviceInstalled: true,
226
+ runningVersion: running,
227
+ versionConfirmed: running ? compareVersions(running, version) === 0 : false,
228
+ waitedMs: alive.waitedMs,
229
+ refreshedCopy: Boolean(copied?.ok),
230
+ source,
231
+ };
232
+ }
233
+
234
+ // 7. It did not come up: put the old version back.
235
+ const why = started.ok
236
+ ? `it started but the daemon did not report in within ${Math.round((deps.verifyMs ?? VERIFY_MS) / 1000)}s`
237
+ : `the service could not be started: ${started.reason}`;
238
+
239
+ log(`The new version did not come up (${why}). Rolling back to v${from}...`);
240
+ const rolled = await rollback({ version: from, service, exec, serviceInstalled, log });
241
+
242
+ return {
243
+ ok: false,
244
+ step: 'verify',
245
+ from,
246
+ to: version,
247
+ reason: why,
248
+ rolledBack: rolled.ok,
249
+ restored: rolled.started,
250
+ rollbackReason: rolled.reason,
251
+ };
252
+ }
253
+
254
+ /** Reinstalls a known-good version and starts the service again. */
255
+ async function rollback({ version, service, exec, serviceInstalled, log }) {
256
+ closeDb();
257
+ const back = await exec(`npm install -g ${PACKAGE}@${version}`, { timeout: 600_000 });
258
+ initDb();
259
+
260
+ if (!back.success) {
261
+ return { ok: false, started: false, reason: `could not reinstall v${version}: ${trim(back)}` };
262
+ }
263
+
264
+ if (!serviceInstalled) return { ok: true, started: true };
265
+
266
+ const started = await service.start({ log });
267
+ return { ok: true, started: Boolean(started.ok) };
268
+ }
269
+
270
+ function trim(result) {
271
+ return `${result.stdout ?? ''}\n${result.stderr ?? ''}`.trim().slice(0, 500);
272
+ }
273
+
274
+ /**
275
+ * The lines explaining why this is a terminal-only operation.
276
+ *
277
+ * Printed by `ezz update` on every run, successful or not: the question "why
278
+ * can't I do this from the dashboard?" is asked once per user, and the answer
279
+ * belongs where they are already looking.
280
+ *
281
+ * @returns {string[]}
282
+ */
283
+ export function whyCliOnly() {
284
+ return [
285
+ 'Updating from the dashboard or the Hub is not possible yet:',
286
+ 'the process that performs the update is the one being replaced and',
287
+ 'stopped, so it could not report what happened. A terminal can.',
288
+ ];
289
+ }
290
+
291
+ /** Where the dashboard is reachable, for the "it is back" line. */
292
+ export function dashboardUrl() {
293
+ const config = loadConfig();
294
+ return `http://localhost:${config.dashboardPort}`;
295
+ }
@@ -94,3 +94,102 @@ export function validatePositiveInteger(val, field) {
94
94
  }
95
95
  return null;
96
96
  }
97
+
98
+ /**
99
+ * Validates the hub URL portion of a connection string.
100
+ * Accepts an http/https URL or a bare host, optionally with a port.
101
+ * @param {string} hubUrl
102
+ * @returns {string|null}
103
+ */
104
+ export function validateHubUrl(hubUrl) {
105
+ if (!hubUrl || typeof hubUrl !== 'string') {
106
+ return 'Hub URL is required';
107
+ }
108
+ if (hubUrl !== hubUrl.trim()) {
109
+ return 'Hub URL must not have leading or trailing spaces';
110
+ }
111
+
112
+ // Reject any other scheme explicitly. Without this, "file://x" is silently
113
+ // re-prefixed below and parses as the host "file", which hides the mistake.
114
+ if (hubUrl.includes('://') && !/^https?:\/\//i.test(hubUrl)) {
115
+ return 'Hub URL must use http or https';
116
+ }
117
+
118
+ // Bare hosts are allowed; the agent defaults them to https.
119
+ const candidate = /^https?:\/\//i.test(hubUrl) ? hubUrl : `https://${hubUrl}`;
120
+
121
+ let parsed;
122
+ try {
123
+ parsed = new URL(candidate);
124
+ } catch {
125
+ return 'Invalid hub URL';
126
+ }
127
+
128
+ if (parsed.protocol !== 'http:' && parsed.protocol !== 'https:') {
129
+ return 'Hub URL must use http or https';
130
+ }
131
+ if (!parsed.hostname) {
132
+ return 'Hub URL is missing a host';
133
+ }
134
+ // The hub never issues credentials inside the URL; refuse to store any.
135
+ if (parsed.username || parsed.password) {
136
+ return 'Hub URL must not contain credentials';
137
+ }
138
+
139
+ return null;
140
+ }
141
+
142
+ /** Upper bound on a pasted connection string (real ones are ~150 chars). */
143
+ const MAX_CONNECTION_STRING_LENGTH = 2048;
144
+
145
+ /** serverId / secret shape: URL-safe token, no whitespace or control chars. */
146
+ const SAFE_TOKEN = /^[A-Za-z0-9._~-]{1,200}$/;
147
+
148
+ /**
149
+ * Validates an Easy DevOps Hub connection string.
150
+ * Expected format: ezops://<hubUrl>|<serverId>|<secret>
151
+ *
152
+ * Note: this only checks shape. Whether the hub recognises the serverId and
153
+ * secret is decided by the hub during the agent handshake.
154
+ *
155
+ * @param {string} str
156
+ * @returns {string|null} Error message, or null if valid
157
+ */
158
+ export function validateConnectionString(str) {
159
+ if (!str || typeof str !== 'string') {
160
+ return 'Connection string is required';
161
+ }
162
+
163
+ const trimmed = str.trim();
164
+ const PREFIX = 'ezops://';
165
+
166
+ // A real string is ~150 characters. Cap it so a pasted blob is rejected
167
+ // before it is persisted to SQLite or sent as a handshake credential.
168
+ if (trimmed.length > MAX_CONNECTION_STRING_LENGTH) {
169
+ return 'Connection string is too long';
170
+ }
171
+
172
+ if (!trimmed.toLowerCase().startsWith(PREFIX)) {
173
+ return 'Connection string must start with ezops://';
174
+ }
175
+
176
+ const parts = trimmed.slice(PREFIX.length).split('|');
177
+ if (parts.length !== 3) {
178
+ return 'Connection string must have the form ezops://hubUrl|serverId|secret';
179
+ }
180
+
181
+ const [hubUrl, serverId, secret] = parts.map((part) => part.trim());
182
+
183
+ const hubUrlError = validateHubUrl(hubUrl);
184
+ if (hubUrlError) return hubUrlError;
185
+
186
+ if (!serverId) return 'Connection string is missing the server ID';
187
+ if (!secret) return 'Connection string is missing the secret';
188
+
189
+ // Identifiers the hub mints are URL-safe tokens (cuid / uuid / hex). Reject
190
+ // anything with whitespace or control characters rather than storing it.
191
+ if (!SAFE_TOKEN.test(serverId)) return 'Connection string has an invalid server ID';
192
+ if (!SAFE_TOKEN.test(secret)) return 'Connection string has an invalid secret';
193
+
194
+ return null;
195
+ }