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,430 @@
1
+ /**
2
+ * core/hubPermissions.js
3
+ *
4
+ * The catalog of things a paired Hub may ask this server to do.
5
+ *
6
+ * This file is the single source of truth. The dashboard renders the tree from
7
+ * it, the agent re-checks every incoming command against it, and the Hub only
8
+ * ever receives a copy. Nothing here is advisory: the local check in
9
+ * hubAgent.js is the real security gate, so a Hub that sends a command for a
10
+ * permission that is off gets nothing done.
11
+ *
12
+ * Monitoring is deliberately NOT in this catalog. Metrics, nginx status, the
13
+ * domain list, domain health and certificate expiry are always reported — a
14
+ * paired server is a monitored server, and gating read-only telemetry would
15
+ * make the Hub useless without protecting anything.
16
+ *
17
+ * Storage: a flat object of { 'group.child': boolean } under the
18
+ * 'hub-permissions' key. Group ids are never stored; a group is derived from
19
+ * its children so the two can never disagree.
20
+ */
21
+
22
+ /**
23
+ * @typedef {Object} PermissionLeaf
24
+ * @property {string} id Stored id, 'group.child'
25
+ * @property {string} label Short UI label
26
+ * @property {string} description One line explaining the effect
27
+ * @property {boolean} [comingSoon] Shown in the tree but cannot be enabled
28
+ */
29
+
30
+ /**
31
+ * @typedef {Object} PermissionGroup
32
+ * @property {string} id
33
+ * @property {string} label
34
+ * @property {string} description
35
+ * @property {PermissionLeaf[]} children
36
+ */
37
+
38
+ /** @type {PermissionGroup[]} */
39
+ export const PERMISSION_TREE = Object.freeze([
40
+ {
41
+ id: 'nginx',
42
+ label: 'Nginx',
43
+ description: 'Control the nginx service and its configuration files.',
44
+ children: [
45
+ { id: 'nginx.start', label: 'Start', description: 'Start nginx when it is stopped.' },
46
+ { id: 'nginx.stop', label: 'Stop', description: 'Stop nginx. Your sites go offline until it is started again.' },
47
+ { id: 'nginx.restart', label: 'Restart', description: 'Full restart. Briefly drops connections.' },
48
+ { id: 'nginx.reload', label: 'Reload', description: 'Graceful reload. Picks up config changes without dropping connections.' },
49
+ { id: 'nginx.test', label: 'Test config', description: 'Run nginx -t. Read-only, changes nothing.' },
50
+ { id: 'nginx.config.view', label: 'View configs', description: 'Read the generated nginx config files.' },
51
+ { id: 'nginx.config.edit', label: 'Edit configs', description: 'Edit a config file. Always tested before it is kept, and rolled back if nginx rejects it.' },
52
+ { id: 'nginx.logs.view', label: 'View logs', description: 'Read recent nginx access and error log lines.' },
53
+ ],
54
+ },
55
+ {
56
+ id: 'domains',
57
+ label: 'Domains',
58
+ description: 'Manage reverse proxy domains and their nginx configs.',
59
+ children: [
60
+ { id: 'domains.create', label: 'Create', description: 'Add a new domain and generate its nginx config.' },
61
+ { id: 'domains.edit', label: 'Edit', description: 'Change an existing domain and regenerate its config.' },
62
+ { id: 'domains.toggle', label: 'Enable / disable', description: 'Take a domain in or out of service without deleting it.' },
63
+ { id: 'domains.delete', label: 'Delete', description: 'Remove a domain and its nginx config.' },
64
+ ],
65
+ },
66
+ {
67
+ id: 'ssl',
68
+ label: 'SSL certificates',
69
+ description: 'Issue, renew and remove Let’s Encrypt certificates.',
70
+ children: [
71
+ { id: 'ssl.renew', label: 'Renew one', description: 'Renew a single certificate. nginx keeps running.' },
72
+ { id: 'ssl.renewAll', label: 'Renew all expiring', description: 'Renew every certificate expiring within 30 days.' },
73
+ { id: 'ssl.issue', label: 'Issue new', description: 'Request a new certificate over HTTP-01. nginx keeps running.' },
74
+ { id: 'ssl.delete', label: 'Delete', description: 'Delete certificate files. Refused while an enabled domain still uses them.' },
75
+ {
76
+ id: 'ssl.issueDns',
77
+ label: 'Issue wildcard (DNS-01)',
78
+ description: 'Wildcard certificates need a DNS TXT record you add by hand, so they cannot be driven from the Hub yet.',
79
+ comingSoon: true,
80
+ },
81
+ ],
82
+ },
83
+ {
84
+ id: 'deploy',
85
+ label: 'Deployments',
86
+ description: 'Deploy the apps on this server from the Hub. Fetching code and running its commands happens here, with this server’s own settings.',
87
+ children: [
88
+ { id: 'deploy.run', label: 'Deploy', description: 'Start a deployment of an existing app from its configured repository.' },
89
+ { id: 'deploy.rollback', label: 'Roll back', description: 'Switch an app back to an earlier release that is still on this server.' },
90
+ { id: 'deploy.logs', label: 'View deployment logs', description: 'Read the output of a deployment. Build output can contain whatever the build prints.' },
91
+ { id: 'deploy.manage', label: 'Create, edit and delete apps', description: 'Add an app from a repository, change its settings, issue its deploy token, or remove it. This decides which code runs here.' },
92
+ { id: 'deploy.env', label: 'Read and edit environment variables', description: 'See and change an app’s environment values. They pass through the Hub while being edited and are not stored there.' },
93
+ { id: 'deploy.ci', label: 'Deploy from CI through the Hub', description: 'Let a GitHub Action trigger a deployment by calling the Hub with the app’s own deploy token, so this server needs no inbound port.' },
94
+ ],
95
+ },
96
+ {
97
+ id: 'process',
98
+ label: 'Processes',
99
+ description: 'Control the PM2 processes on this server, including the Easy DevOps service itself.',
100
+ children: [
101
+ {
102
+ id: 'process.start',
103
+ label: 'Start',
104
+ description: 'Start a stopped PM2 process from the Hub.',
105
+ comingSoon: true,
106
+ },
107
+ {
108
+ id: 'process.stop',
109
+ label: 'Stop',
110
+ description: 'Stop a running PM2 process from the Hub. Stopping the dashboard would cut the Hub off from this server.',
111
+ comingSoon: true,
112
+ },
113
+ {
114
+ id: 'process.restart',
115
+ label: 'Restart',
116
+ description: 'Restart a PM2 process from the Hub.',
117
+ comingSoon: true,
118
+ },
119
+ ],
120
+ },
121
+ {
122
+ id: 'system',
123
+ label: 'System',
124
+ description: 'Server-level operations beyond nginx.',
125
+ children: [
126
+ {
127
+ id: 'system.nodeVersions',
128
+ label: 'Manage Node.js versions',
129
+ description: 'Install and switch Node.js versions through nvm.',
130
+ comingSoon: true,
131
+ },
132
+ {
133
+ id: 'system.restartServer',
134
+ label: 'Reboot the server',
135
+ description: 'Reboot the whole machine.',
136
+ comingSoon: true,
137
+ },
138
+ ],
139
+ },
140
+ ]);
141
+
142
+ /** Every leaf id, including the ones marked comingSoon. */
143
+ export const ALL_PERMISSION_IDS = Object.freeze(
144
+ PERMISSION_TREE.flatMap((g) => g.children.map((c) => c.id))
145
+ );
146
+
147
+ /** Leaf ids that can actually be enabled today. */
148
+ export const GRANTABLE_PERMISSION_IDS = Object.freeze(
149
+ PERMISSION_TREE.flatMap((g) => g.children.filter((c) => !c.comingSoon).map((c) => c.id))
150
+ );
151
+
152
+ /** Leaf ids that exist in the tree but cannot be enabled yet. */
153
+ export const COMING_SOON_PERMISSION_IDS = Object.freeze(
154
+ PERMISSION_TREE.flatMap((g) => g.children.filter((c) => c.comingSoon).map((c) => c.id))
155
+ );
156
+
157
+ /** @returns {boolean} True when `id` is a real leaf in the catalog. */
158
+ export function isKnownPermission(id) {
159
+ return ALL_PERMISSION_IDS.includes(id);
160
+ }
161
+
162
+ /** @returns {boolean} True when `id` exists but is not grantable yet. */
163
+ export function isComingSoon(id) {
164
+ return COMING_SOON_PERMISSION_IDS.includes(id);
165
+ }
166
+
167
+ /** A fully-denied permission map — the state every new link starts in. */
168
+ export function defaultPermissions() {
169
+ const out = {};
170
+ for (const id of ALL_PERMISSION_IDS) out[id] = false;
171
+ return out;
172
+ }
173
+
174
+ /**
175
+ * Normalises any stored shape into a complete, valid permission map.
176
+ *
177
+ * Unknown keys are dropped and comingSoon permissions are forced off, so a
178
+ * stale or hand-edited record can never grant something the catalog does not
179
+ * offer.
180
+ *
181
+ * @param {Object} stored
182
+ * @returns {Object<string, boolean>}
183
+ */
184
+ export function normalizePermissions(stored) {
185
+ const out = defaultPermissions();
186
+ if (!stored || typeof stored !== 'object') return out;
187
+
188
+ for (const id of ALL_PERMISSION_IDS) {
189
+ if (isComingSoon(id)) continue; // never grantable
190
+ if (stored[id] === true) out[id] = true;
191
+ }
192
+ return out;
193
+ }
194
+
195
+ /**
196
+ * Derives a group's checkbox state from its children.
197
+ * @param {string} groupId
198
+ * @param {Object<string, boolean>} permissions
199
+ * @returns {'on'|'off'|'partial'}
200
+ */
201
+ export function groupState(groupId, permissions) {
202
+ const group = PERMISSION_TREE.find((g) => g.id === groupId);
203
+ if (!group) return 'off';
204
+
205
+ // comingSoon children can never be on, so they would make a fully-granted
206
+ // group look partial forever. They are excluded from the calculation.
207
+ const relevant = group.children.filter((c) => !c.comingSoon);
208
+ if (relevant.length === 0) return 'off';
209
+
210
+ const on = relevant.filter((c) => permissions?.[c.id] === true).length;
211
+ if (on === 0) return 'off';
212
+ if (on === relevant.length) return 'on';
213
+ return 'partial';
214
+ }
215
+
216
+ /**
217
+ * Returns a copy with every grantable child of a group set on or off.
218
+ * @param {Object<string, boolean>} permissions
219
+ * @param {string} groupId
220
+ * @param {boolean} enabled
221
+ * @returns {Object<string, boolean>}
222
+ */
223
+ export function setGroup(permissions, groupId, enabled) {
224
+ const next = normalizePermissions(permissions);
225
+ const group = PERMISSION_TREE.find((g) => g.id === groupId);
226
+ if (!group) return next;
227
+
228
+ for (const child of group.children) {
229
+ if (child.comingSoon) continue;
230
+ next[child.id] = Boolean(enabled);
231
+ }
232
+ return next;
233
+ }
234
+
235
+ /**
236
+ * Returns a copy with one leaf set, ignoring unknown and comingSoon ids.
237
+ * @param {Object<string, boolean>} permissions
238
+ * @param {string} id
239
+ * @param {boolean} enabled
240
+ * @returns {Object<string, boolean>}
241
+ */
242
+ export function setPermission(permissions, id, enabled) {
243
+ const next = normalizePermissions(permissions);
244
+ if (!isKnownPermission(id) || isComingSoon(id)) return next;
245
+ next[id] = Boolean(enabled);
246
+ return next;
247
+ }
248
+
249
+ // ─── Legacy migration ─────────────────────────────────────────────────────────
250
+
251
+ /**
252
+ * The three booleans used before the tree existed.
253
+ * Kept only so an existing link can be migrated.
254
+ */
255
+ const LEGACY_KEYS = ['reloadNginx', 'manageDomains', 'manageSSL'];
256
+
257
+ /**
258
+ * Permissions that were never gated before the tree existed. Existing links
259
+ * already had these abilities in practice, so withholding them on upgrade
260
+ * would be a silent downgrade of what the operator had working.
261
+ */
262
+ const PREVIOUSLY_UNGATED = ['nginx.config.view', 'nginx.logs.view'];
263
+
264
+ /** @returns {boolean} True when `stored` is the pre-tree shape. */
265
+ export function isLegacyPermissions(stored) {
266
+ if (!stored || typeof stored !== 'object') return false;
267
+ // Already migrated if any catalog id is present.
268
+ if (ALL_PERMISSION_IDS.some((id) => id in stored)) return false;
269
+ return LEGACY_KEYS.some((k) => k in stored);
270
+ }
271
+
272
+ /**
273
+ * Converts the three legacy booleans into the tree, preserving exactly what
274
+ * the Hub could already do.
275
+ *
276
+ * reloadNginx -> nginx.start / stop / restart / reload / test
277
+ * (hub:nginxAction accepted all of these under one flag)
278
+ * manageDomains -> every domains.*
279
+ * manageSSL -> ssl.renew and ssl.renewAll only
280
+ * (issue and delete are new, so they stay off)
281
+ *
282
+ * Plus the two previously ungated read permissions.
283
+ *
284
+ * @param {Object} stored
285
+ * @returns {Object<string, boolean>}
286
+ */
287
+ export function migrateLegacyPermissions(stored) {
288
+ const next = defaultPermissions();
289
+ if (!stored || typeof stored !== 'object') return next;
290
+
291
+ if (stored.reloadNginx === true) {
292
+ for (const id of ['nginx.start', 'nginx.stop', 'nginx.restart', 'nginx.reload', 'nginx.test']) {
293
+ next[id] = true;
294
+ }
295
+ }
296
+ if (stored.manageDomains === true) {
297
+ for (const child of PERMISSION_TREE.find((g) => g.id === 'domains').children) {
298
+ next[child.id] = true;
299
+ }
300
+ }
301
+ if (stored.manageSSL === true) {
302
+ next['ssl.renew'] = true;
303
+ next['ssl.renewAll'] = true;
304
+ }
305
+
306
+ for (const id of PREVIOUSLY_UNGATED) next[id] = true;
307
+
308
+ return next;
309
+ }
310
+
311
+ // ─── Command → permission mapping ─────────────────────────────────────────────
312
+
313
+ /**
314
+ * Resolves the permission a hub command payload requires.
315
+ *
316
+ * Returns null when the command needs no permission (read-only telemetry).
317
+ * Returns undefined for an unknown command, so the agent can refuse it rather
318
+ * than treat it as free.
319
+ *
320
+ * @param {string} command
321
+ * @param {Object} [payload]
322
+ * @returns {string|null|undefined}
323
+ */
324
+ export function permissionForCommand(command, payload = {}) {
325
+ switch (command) {
326
+ case 'hub:reloadNginx':
327
+ return 'nginx.reload';
328
+
329
+ case 'hub:nginxAction': {
330
+ const map = {
331
+ start: 'nginx.start',
332
+ stop: 'nginx.stop',
333
+ restart: 'nginx.restart',
334
+ reload: 'nginx.reload',
335
+ test: 'nginx.test',
336
+ };
337
+ return map[payload?.action] ?? undefined;
338
+ }
339
+
340
+ case 'hub:manageDomain': {
341
+ const map = {
342
+ create: 'domains.create',
343
+ update: 'domains.edit',
344
+ toggle: 'domains.toggle',
345
+ delete: 'domains.delete',
346
+ reload: 'nginx.reload',
347
+ };
348
+ return map[payload?.action] ?? undefined;
349
+ }
350
+
351
+ case 'hub:manageSSL': {
352
+ const map = {
353
+ renew: 'ssl.renew',
354
+ renewAll: 'ssl.renewAll',
355
+ issue: 'ssl.issue',
356
+ delete: 'ssl.delete',
357
+ };
358
+ return map[payload?.action] ?? undefined;
359
+ }
360
+
361
+ case 'hub:manageDeploy': {
362
+ const map = {
363
+ // Listing the apps and their status is monitoring, like the process
364
+ // list: a paired server is a monitored server. No secrets are in it.
365
+ list: null,
366
+ detail: null,
367
+ github: null,
368
+ deploy: 'deploy.run',
369
+ rollback: 'deploy.rollback',
370
+ run: 'deploy.logs',
371
+ create: 'deploy.manage',
372
+ update: 'deploy.manage',
373
+ delete: 'deploy.manage',
374
+ issueToken: 'deploy.manage',
375
+ linkDomain: 'deploy.manage',
376
+ unlinkDomain: 'deploy.manage',
377
+ move: 'deploy.manage',
378
+ getEnv: 'deploy.env',
379
+ setEnv: 'deploy.env',
380
+ };
381
+ const action = payload?.action;
382
+ return Object.prototype.hasOwnProperty.call(map, action) ? map[action] : undefined;
383
+ }
384
+
385
+ // Not dispatchable by a Hub user: only the Hub's CI endpoint sends it, and
386
+ // the app's own deploy token is verified here, on the server.
387
+ case 'hub:deployHook':
388
+ return 'deploy.ci';
389
+
390
+ case 'hub:getConfigs':
391
+ return 'nginx.config.view';
392
+
393
+ case 'hub:saveConfig':
394
+ return 'nginx.config.edit';
395
+
396
+ case 'hub:getLogs':
397
+ return 'nginx.logs.view';
398
+
399
+ default:
400
+ return undefined;
401
+ }
402
+ }
403
+
404
+ /**
405
+ * Decides whether a command may run on this server.
406
+ * @param {string} command
407
+ * @param {Object} payload
408
+ * @param {Object<string, boolean>} permissions
409
+ * @returns {{ allowed: boolean, permission: string|null, reason: string|null }}
410
+ */
411
+ export function checkCommandAllowed(command, payload, permissions) {
412
+ const required = permissionForCommand(command, payload);
413
+
414
+ if (required === undefined) {
415
+ return { allowed: false, permission: null, reason: `Unknown command or action: ${command}` };
416
+ }
417
+ if (required === null) {
418
+ return { allowed: true, permission: null, reason: null };
419
+ }
420
+ if (isComingSoon(required)) {
421
+ return { allowed: false, permission: required, reason: `'${required}' is not available yet.` };
422
+ }
423
+
424
+ const granted = normalizePermissions(permissions)[required] === true;
425
+ return {
426
+ allowed: granted,
427
+ permission: required,
428
+ reason: granted ? null : `Permission '${required}' is disabled on this server.`,
429
+ };
430
+ }
@@ -0,0 +1,162 @@
1
+ /**
2
+ * core/installSource.js
3
+ *
4
+ * How this copy of Easy DevOps got onto the machine, and therefore how it is
5
+ * updated.
6
+ *
7
+ * ── Why this has to be detected rather than assumed ───────────────────────
8
+ *
9
+ * `ezz update` ran `npm install -g easy-devops`. For a globally installed
10
+ * package that is right. For a git clone it is actively wrong in two ways: it
11
+ * installs a SECOND copy from the registry, and the service keeps pointing at
12
+ * the clone — so the update reports success and changes nothing the service
13
+ * runs. Worse, the two copies then disagree about which is "the" version, and
14
+ * the clone, which is the one actually running, is the one that looks stale.
15
+ *
16
+ * A clone is updated with `git pull`. That is not something this tool should do
17
+ * on the user's behalf: a pull can conflict, can fast-forward past local
18
+ * commits, and the working tree may be dirty with work in progress. So the
19
+ * honest answer is to say what to run.
20
+ *
21
+ * ── How it is decided ─────────────────────────────────────────────────────
22
+ *
23
+ * By the package root on disk, not by anything recorded in the database:
24
+ *
25
+ * - a `.git` entry in the package root -> clone
26
+ * - the root sits inside a `node_modules` tree -> npm install
27
+ * - neither -> unknown
28
+ *
29
+ * `.git` can be a FILE rather than a directory (a worktree or a submodule
30
+ * records a gitdir pointer there), so both count.
31
+ *
32
+ * `npm link` makes a global symlink INTO a clone. That is still a clone — the
33
+ * files the service runs are the clone's, and `npm install -g` would replace
34
+ * the link rather than update them — so the `.git` test is deliberately first.
35
+ */
36
+
37
+ import fs from 'fs';
38
+ import path from 'path';
39
+ import { fileURLToPath } from 'url';
40
+
41
+ import { isWindows } from './platform.js';
42
+
43
+ const __dirname = path.dirname(fileURLToPath(import.meta.url));
44
+
45
+ /**
46
+ * @typedef {Object} InstallSource
47
+ * @property {'clone'|'npm'|'unknown'} kind
48
+ * @property {string} root The package root on disk
49
+ * @property {boolean} npmUpdatable Whether `npm install -g` updates THIS copy
50
+ * @property {string} reason One line, for a status display
51
+ * @property {string[]} howToUpdate The commands to run instead, if any
52
+ */
53
+
54
+ /**
55
+ * Where this copy lives and how it is updated.
56
+ *
57
+ * @param {Object} [opts]
58
+ * @param {string} [opts.root] Package root; defaults to this file's package
59
+ * @returns {InstallSource}
60
+ */
61
+ export function detectInstallSource({ root = packageRoot() } = {}) {
62
+ const resolved = path.resolve(root);
63
+
64
+ if (hasGitDir(resolved)) {
65
+ return {
66
+ kind: 'clone',
67
+ root: resolved,
68
+ npmUpdatable: false,
69
+ reason: `a git clone at ${resolved}`,
70
+ howToUpdate: [
71
+ `cd ${resolved}`,
72
+ 'git pull',
73
+ 'npm install',
74
+ 'ezz service restart',
75
+ ],
76
+ };
77
+ }
78
+
79
+ if (insideNodeModules(resolved)) {
80
+ return {
81
+ kind: 'npm',
82
+ root: resolved,
83
+ npmUpdatable: true,
84
+ reason: `an npm-installed package at ${resolved}`,
85
+ howToUpdate: [],
86
+ };
87
+ }
88
+
89
+ // An extracted tarball, or a copy someone placed by hand. `npm install -g`
90
+ // would install a separate copy from the registry and leave this one in
91
+ // place, so it is not treated as updatable.
92
+ return {
93
+ kind: 'unknown',
94
+ root: resolved,
95
+ npmUpdatable: false,
96
+ reason: `an unpackaged copy at ${resolved} (no .git, not under node_modules)`,
97
+ howToUpdate: [
98
+ 'Reinstall it the way you want it managed:',
99
+ ' npm install -g easy-devops (then remove this copy)',
100
+ ' git clone https://github.com/omar00050/Easy-DevOps.git',
101
+ ],
102
+ };
103
+ }
104
+
105
+ /** This package's root directory. */
106
+ export function packageRoot() {
107
+ // core/ -> src/ -> the package root.
108
+ return path.resolve(__dirname, '..', '..');
109
+ }
110
+
111
+ /** `.git` as either a directory or a pointer file. */
112
+ function hasGitDir(root) {
113
+ try {
114
+ fs.statSync(path.join(root, '.git'));
115
+ return true;
116
+ } catch {
117
+ return false;
118
+ }
119
+ }
120
+
121
+ /**
122
+ * Whether a path sits inside a node_modules tree.
123
+ *
124
+ * Compared segment by segment rather than with a substring test, so a project
125
+ * directory that merely contains the text "node_modules" in its name is not
126
+ * mistaken for an installed package. Case-insensitive on Windows, where paths
127
+ * are.
128
+ */
129
+ function insideNodeModules(root) {
130
+ const segments = root.split(/[\\/]+/).map((s) => (isWindows ? s.toLowerCase() : s));
131
+ return segments.includes('node_modules');
132
+ }
133
+
134
+ /**
135
+ * The lines to print when an operation cannot proceed because this copy is not
136
+ * npm-managed.
137
+ *
138
+ * @param {InstallSource} source
139
+ * @param {string} what What the caller was asked to do, e.g. 'update'
140
+ * @returns {string[]}
141
+ */
142
+ export function describeUnmanaged(source, what = 'update') {
143
+ const lines = [];
144
+
145
+ if (source.kind === 'clone') {
146
+ lines.push(`This is ${source.reason}, not an npm install.`);
147
+ lines.push('');
148
+ lines.push(`Running npm would install a second copy from the registry and`);
149
+ lines.push(`leave the service running this one, so the ${what} would report`);
150
+ lines.push('success and change nothing. Update the clone instead:');
151
+ } else {
152
+ lines.push(`This is ${source.reason}.`);
153
+ lines.push('');
154
+ lines.push(`npm cannot ${what} it in place.`);
155
+ }
156
+
157
+ lines.push('');
158
+ for (const line of source.howToUpdate) lines.push(` ${line}`);
159
+ lines.push('');
160
+
161
+ return lines;
162
+ }