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.
- package/README.md +97 -6
- package/dist/ui/assets/index-B6zdwEnG.css +1 -0
- package/dist/ui/assets/index-Cy0oNmul.js +442 -0
- package/dist/ui/assets/socket-DwLPrgsr.js +1 -0
- package/dist/ui/assets/socket-K5ttLD2q.js +65 -0
- package/dist/ui/brand/apple-touch-icon.png +0 -0
- package/dist/ui/brand/favicon-16.png +0 -0
- package/dist/ui/brand/favicon-32.png +0 -0
- package/dist/ui/brand/favicon-light-16.png +0 -0
- package/dist/ui/brand/favicon-light-32.png +0 -0
- package/dist/ui/brand/favicon.ico +0 -0
- package/dist/ui/brand/icon-192.png +0 -0
- package/dist/ui/brand/icon-48.png +0 -0
- package/dist/ui/brand/icon-512.png +0 -0
- package/dist/ui/brand/icon-64-light.png +0 -0
- package/dist/ui/brand/icon-64.png +0 -0
- package/dist/ui/brand/logo-dark.png +0 -0
- package/dist/ui/brand/logo-light.png +0 -0
- package/dist/ui/brand/mark-dark.png +0 -0
- package/dist/ui/brand/mark-light.png +0 -0
- package/dist/ui/index.html +71 -0
- package/install.ps1 +188 -1
- package/install.sh +254 -29
- package/package.json +15 -4
- package/src/cli/dash.js +110 -0
- package/src/cli/index.js +237 -106
- package/src/cli/managers/domain-manager.js +60 -40
- package/src/cli/managers/ssl-manager.js +47 -330
- package/src/cli/menu.js +156 -0
- package/src/cli/menus/cleanup.js +205 -0
- package/src/cli/menus/dashboard.js +92 -1
- package/src/cli/menus/runtime.js +236 -0
- package/src/cli/menus/service.js +764 -0
- package/src/cli/menus/settings.js +176 -8
- package/src/cli/menus/uninstall.js +167 -0
- package/src/cli/menus/update.js +221 -65
- package/src/core/acme-challenge.js +273 -0
- package/src/core/cert-operations.js +189 -0
- package/src/{dashboard/lib → core}/cert-reader.js +2 -2
- package/src/core/cert-renewal.js +494 -0
- package/src/core/cleanup.js +600 -0
- package/src/core/config.js +169 -2
- package/src/core/daemon/ipc.js +292 -0
- package/src/core/daemon/lock.js +294 -0
- package/src/core/daemon/run.js +241 -0
- package/src/core/daemon/stopRequest.js +162 -0
- package/src/core/db.js +37 -8
- package/src/core/deploy/apps.js +491 -0
- package/src/core/deploy/domainLink.js +200 -0
- package/src/core/deploy/engine.js +973 -0
- package/src/core/deploy/ezfile.js +361 -0
- package/src/core/deploy/github.js +239 -0
- package/src/core/deploy/githubApi.js +261 -0
- package/src/core/deploy/hookServer.js +325 -0
- package/src/core/deploy/hubCommands.js +267 -0
- package/src/core/deploy/launcher.js +57 -0
- package/src/core/deploy/status.js +91 -0
- package/src/core/deploy/store.js +398 -0
- package/src/core/domainNotifier.js +94 -28
- package/src/core/domains-db.js +234 -0
- package/src/core/events.js +94 -2
- package/src/core/hub/agent.js +842 -0
- package/src/core/hubLink.js +306 -0
- package/src/core/hubPermissions.js +430 -0
- package/src/core/installSource.js +162 -0
- package/src/core/jobs/bus.js +120 -0
- package/src/core/jobs/index.js +371 -0
- package/src/core/jobs/monitors.js +368 -0
- package/src/core/jobs/owner.js +217 -0
- package/src/core/jobs/revisionWatcher.js +106 -0
- package/src/core/nginx-apply.js +213 -0
- package/src/core/nginx-conf-generator.js +253 -17
- package/src/{dashboard/lib → core}/nginx-service.js +18 -9
- package/src/core/notifications/claim.js +214 -0
- package/src/core/notifications/dispatch.js +154 -0
- package/src/core/notifier.js +31 -1
- package/src/core/passwords.js +172 -0
- package/src/core/permissions.js +8 -4
- package/src/core/platform.js +709 -2
- package/src/core/probeCache.js +258 -0
- package/src/core/purge.js +372 -0
- package/src/core/runner.js +488 -0
- package/src/core/service/acl.js +226 -0
- package/src/core/service/dataDir.js +345 -0
- package/src/core/service/index.js +1128 -0
- package/src/core/service/legacy.js +218 -0
- package/src/core/service/runtime.js +446 -0
- package/src/core/service/runtimeUpdate.js +800 -0
- package/src/core/service/systemd.js +458 -0
- package/src/core/service/windowsPersonal.js +530 -0
- package/src/core/service/windowsService.js +576 -0
- package/src/core/service/winsw.js +260 -0
- package/src/core/setupCode.js +331 -0
- package/src/core/shell.js +70 -7
- package/src/core/ssl-issuer.js +627 -0
- package/src/core/updater.js +295 -0
- package/src/core/validators.js +99 -0
- package/src/dashboard/controllers/authController.js +302 -15
- package/src/dashboard/controllers/deploymentsController.js +479 -0
- package/src/dashboard/controllers/domainsController.js +39 -52
- package/src/dashboard/controllers/hubController.js +135 -0
- package/src/dashboard/controllers/nginxController.js +21 -3
- package/src/dashboard/controllers/processesController.js +368 -0
- package/src/dashboard/controllers/settingsController.js +180 -12
- package/src/dashboard/controllers/sslController.js +171 -10
- package/src/dashboard/lib/daemonBridge.js +163 -0
- package/src/dashboard/lib/localRequest.js +235 -0
- package/src/dashboard/lib/loginRateLimit.js +119 -0
- package/src/dashboard/lib/requireAuth.js +80 -0
- package/src/dashboard/public/js/app.js +489 -19
- package/src/dashboard/routes/auth.js +10 -10
- package/src/dashboard/routes/deployments.js +46 -0
- package/src/dashboard/routes/domains.js +9 -0
- package/src/dashboard/routes/hub.js +22 -0
- package/src/dashboard/routes/nginx.js +9 -0
- package/src/dashboard/routes/processes.js +29 -0
- package/src/dashboard/routes/settings.js +9 -0
- package/src/dashboard/routes/ssl.js +9 -0
- package/src/dashboard/server.js +288 -21
- package/src/dashboard/socketManager.js +171 -207
- package/src/dashboard/views/index.ejs +1 -0
- package/src/dashboard/views/partials/login.ejs +68 -7
- package/src/dashboard/views/partials/processes-panel.ejs +261 -0
- package/src/dashboard/views/partials/settings-panel.ejs +287 -0
- package/src/dashboard/views/partials/sidebar.ejs +2 -0
- package/src/dashboard/lib/domains-db.js +0 -51
|
@@ -0,0 +1,842 @@
|
|
|
1
|
+
// src/core/hub/agent.js
|
|
2
|
+
// Persistent outbound Socket.io agent that connects Easy DevOps to the Hub SaaS.
|
|
3
|
+
//
|
|
4
|
+
// Lives in core because it is background work, not dashboard work: the daemon
|
|
5
|
+
// runs it when a daemon is running, and an open dashboard runs it only while
|
|
6
|
+
// no daemon is (see core/jobs/index.js). It was in src/dashboard/lib/ when the
|
|
7
|
+
// dashboard was the only long-running process.
|
|
8
|
+
//
|
|
9
|
+
// Activated when hub-link exists in SQLite.
|
|
10
|
+
|
|
11
|
+
import { io } from 'socket.io-client';
|
|
12
|
+
import { createHash } from 'crypto';
|
|
13
|
+
import os from 'os';
|
|
14
|
+
import { dbGet } from '../db.js';
|
|
15
|
+
import { loadConfig } from '../config.js';
|
|
16
|
+
import { run } from '../shell.js';
|
|
17
|
+
import { getStatus, start as nginxStart, stop as nginxStop, restart as nginxRestart, test as nginxTest, reload as nginxReload, listConfigs, getConfig, saveConfig, getLogs } from '../nginx-service.js';
|
|
18
|
+
import { publish } from '../jobs/bus.js';
|
|
19
|
+
import { getLastDomainHealth, probeDomains, DOMAIN_HEALTH_POLL_MS } from '../jobs/monitors.js';
|
|
20
|
+
import { isWindows, platformName, isDevDataDir } from '../platform.js';
|
|
21
|
+
import { listAllCerts } from '../cert-reader.js';
|
|
22
|
+
import { backgroundServiceState } from '../service/index.js';
|
|
23
|
+
import * as runner from '../runner.js';
|
|
24
|
+
import { loadChannels } from '../notifier.js';
|
|
25
|
+
import {
|
|
26
|
+
getHubLink,
|
|
27
|
+
getHubPermissions,
|
|
28
|
+
getHubRevision,
|
|
29
|
+
resolveHubOrigin,
|
|
30
|
+
isAuthRejection,
|
|
31
|
+
describeRejection,
|
|
32
|
+
HUB_AGENT_STATUS,
|
|
33
|
+
} from '../hubLink.js';
|
|
34
|
+
import {
|
|
35
|
+
PERMISSION_TREE,
|
|
36
|
+
COMING_SOON_PERMISSION_IDS,
|
|
37
|
+
checkCommandAllowed,
|
|
38
|
+
} from '../hubPermissions.js';
|
|
39
|
+
|
|
40
|
+
/** How often the agent checks the shared revision counter. */
|
|
41
|
+
const REVISION_POLL_MS = 3_000;
|
|
42
|
+
|
|
43
|
+
// Network retries back off from 1s up to this cap.
|
|
44
|
+
const RECONNECT_DELAY_MIN = 1_000;
|
|
45
|
+
const RECONNECT_DELAY_MAX = 60_000;
|
|
46
|
+
|
|
47
|
+
let socket = null;
|
|
48
|
+
let intervals = [];
|
|
49
|
+
|
|
50
|
+
// socket.io-client does NOT auto-reconnect after a server-initiated
|
|
51
|
+
// disconnect ("io server disconnect"), which is exactly what the hub does when
|
|
52
|
+
// the shared secret is rotated. We re-dial manually on our own backoff so the
|
|
53
|
+
// agent learns whether its credentials are still valid instead of sitting idle.
|
|
54
|
+
let rejoinTimer = null;
|
|
55
|
+
let rejoinDelay = 0;
|
|
56
|
+
|
|
57
|
+
// ─── Connection status ─────────────────────────────────────────────────────────
|
|
58
|
+
// Mirrors the five states in core/hubLink.js. `lastError` is a human-readable
|
|
59
|
+
// explanation; it never contains the shared secret.
|
|
60
|
+
let agentState = {
|
|
61
|
+
status: HUB_AGENT_STATUS.NOT_LINKED,
|
|
62
|
+
lastError: null,
|
|
63
|
+
attempts: 0,
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
function setState(status, lastError = null) {
|
|
67
|
+
agentState = { ...agentState, status, lastError };
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// ─── CPU usage helper ──────────────────────────────────────────────────────────
|
|
71
|
+
|
|
72
|
+
function getCpuPercent() {
|
|
73
|
+
const cpus = os.cpus();
|
|
74
|
+
let totalIdle = 0;
|
|
75
|
+
let totalTick = 0;
|
|
76
|
+
for (const cpu of cpus) {
|
|
77
|
+
for (const type of Object.keys(cpu.times)) {
|
|
78
|
+
totalTick += cpu.times[type];
|
|
79
|
+
}
|
|
80
|
+
totalIdle += cpu.times.idle;
|
|
81
|
+
}
|
|
82
|
+
return Math.round((1 - totalIdle / totalTick) * 100);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// ─── Disk usage helper (cross-platform) ───────────────────────────────────────
|
|
86
|
+
|
|
87
|
+
async function getDiskPercent() {
|
|
88
|
+
try {
|
|
89
|
+
const cmd = isWindows
|
|
90
|
+
? `(Get-PSDrive C | Select-Object -ExpandProperty Used) / ((Get-PSDrive C | Select-Object -ExpandProperty Used) + (Get-PSDrive C | Select-Object -ExpandProperty Free)) * 100`
|
|
91
|
+
: `df / | awk 'NR==2 {print $5}' | tr -d '%'`;
|
|
92
|
+
const result = await run(cmd, { timeout: 5000 });
|
|
93
|
+
return Math.round(parseFloat(result.stdout) || 0);
|
|
94
|
+
} catch {
|
|
95
|
+
return 0;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// ─── Event pushers ─────────────────────────────────────────────────────────────
|
|
100
|
+
|
|
101
|
+
async function pushNginxStatus() {
|
|
102
|
+
if (!socket?.connected) return;
|
|
103
|
+
try {
|
|
104
|
+
const status = await getStatus();
|
|
105
|
+
socket.emit('nginx:status', { running: status.running, version: status.version ?? null });
|
|
106
|
+
} catch { /* non-fatal */ }
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
async function pushResources() {
|
|
110
|
+
if (!socket?.connected) return;
|
|
111
|
+
try {
|
|
112
|
+
const totalMem = os.totalmem();
|
|
113
|
+
const freeMem = os.freemem();
|
|
114
|
+
const disk = await getDiskPercent();
|
|
115
|
+
socket.emit('server:resources', {
|
|
116
|
+
cpu: getCpuPercent(),
|
|
117
|
+
ram: Math.round((1 - freeMem / totalMem) * 100),
|
|
118
|
+
disk,
|
|
119
|
+
});
|
|
120
|
+
} catch { /* non-fatal */ }
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
async function pushDomainHealth() {
|
|
124
|
+
if (!socket?.connected) return;
|
|
125
|
+
try {
|
|
126
|
+
const domains = dbGet('domains') || [];
|
|
127
|
+
|
|
128
|
+
// Real probe results, not the enabled flag. The monitors sweep every
|
|
129
|
+
// minute in this same process; reuse that sweep while it is fresh, and
|
|
130
|
+
// probe directly only when there is none yet (first push after a start).
|
|
131
|
+
const last = getLastDomainHealth();
|
|
132
|
+
const fresh = last && Date.now() - last.at < 3 * DOMAIN_HEALTH_POLL_MS;
|
|
133
|
+
const results = fresh ? last.results : await probeDomains(domains.filter(d => d.enabled !== false));
|
|
134
|
+
const upByName = new Map(results.map(r => [r.name, r.up]));
|
|
135
|
+
|
|
136
|
+
// `enabled` travels separately so the Hub never has to infer it from
|
|
137
|
+
// health. A disabled domain has no health to report: `up: null`.
|
|
138
|
+
socket.emit('domain:health', domains.map(d => {
|
|
139
|
+
const enabled = d.enabled !== false;
|
|
140
|
+
return { name: d.name, enabled, up: enabled ? (upByName.get(d.name) ?? null) : null };
|
|
141
|
+
}));
|
|
142
|
+
} catch { /* non-fatal */ }
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
async function pushDomainConfigs() {
|
|
146
|
+
if (!socket?.connected) return;
|
|
147
|
+
try {
|
|
148
|
+
const domains = dbGet('domains') || [];
|
|
149
|
+
socket.emit('domain:configs', domains);
|
|
150
|
+
} catch { /* non-fatal */ }
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
async function pushChannelsList() {
|
|
154
|
+
if (!socket?.connected) return;
|
|
155
|
+
try {
|
|
156
|
+
const channels = loadChannels();
|
|
157
|
+
socket.emit('channels:list', channels.map(ch => ({ id: ch.id, name: ch.name, type: ch.type })));
|
|
158
|
+
} catch { /* non-fatal */ }
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Reports whether the dashboard is running as a background service.
|
|
163
|
+
*
|
|
164
|
+
* This is what lets the Hub say "offline means really down": if the dashboard
|
|
165
|
+
* runs under PM2 with start-on-boot arranged, an agent that stops reporting is
|
|
166
|
+
* a genuine outage rather than someone having closed a terminal.
|
|
167
|
+
*
|
|
168
|
+
* Deliberately cheap and side-effect free: the state is a lock read and a few
|
|
169
|
+
* existsSync calls, and the PM2 probe never starts a PM2 daemon — so a server
|
|
170
|
+
* that does not use PM2 is not given one just because it is paired with a Hub.
|
|
171
|
+
*/
|
|
172
|
+
async function pushServiceState() {
|
|
173
|
+
if (!socket?.connected) return;
|
|
174
|
+
try {
|
|
175
|
+
// The NATIVE service, not the PM2 app. This used to report a PM2
|
|
176
|
+
// installation, which made "offline means really down" a much weaker
|
|
177
|
+
// signal than the Hub believed: the agent lived inside the dashboard, so
|
|
178
|
+
// there was nothing left to report when the dashboard closed.
|
|
179
|
+
const s = backgroundServiceState();
|
|
180
|
+
const pm2 = await runner.detect({ probeVersion: false });
|
|
181
|
+
|
|
182
|
+
socket.emit('service:state', {
|
|
183
|
+
installed: s.installed,
|
|
184
|
+
running: s.running,
|
|
185
|
+
status: s.detail,
|
|
186
|
+
mechanism: s.mechanism,
|
|
187
|
+
mode: s.mode,
|
|
188
|
+
startsAt: s.startsAt,
|
|
189
|
+
stoppedByUser: s.stoppedByUser,
|
|
190
|
+
jobsRunningHere: s.jobsRunningHere,
|
|
191
|
+
enabledOnBoot: s.installed,
|
|
192
|
+
dev: isDevDataDir(),
|
|
193
|
+
// The user's OWN PM2 installation, which is a different thing and the
|
|
194
|
+
// only reason these two fields still exist.
|
|
195
|
+
pm2Installed: pm2.installed,
|
|
196
|
+
pm2Version: pm2.version,
|
|
197
|
+
uptimeMs: s.startedAt ? Date.now() - s.startedAt : null,
|
|
198
|
+
});
|
|
199
|
+
} catch { /* non-fatal */ }
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Reports the PM2 process list, as read-only telemetry.
|
|
204
|
+
*
|
|
205
|
+
* Monitoring is never gated in this project's permission catalog — a paired
|
|
206
|
+
* server is a monitored server. Acting on a process from the Hub is a separate
|
|
207
|
+
* matter and is still `comingSoon` in the tree (process.start/stop/restart).
|
|
208
|
+
*
|
|
209
|
+
* Skipped entirely when the PM2 daemon is not running, so that reporting never
|
|
210
|
+
* starts one.
|
|
211
|
+
*/
|
|
212
|
+
async function pushProcesses() {
|
|
213
|
+
if (!socket?.connected) return;
|
|
214
|
+
try {
|
|
215
|
+
if (!runner.isDaemonRunning()) {
|
|
216
|
+
socket.emit('processes:list', { daemonRunning: false, apps: [] });
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
219
|
+
const pm2 = await runner.detect({ probeVersion: false });
|
|
220
|
+
if (!pm2.installed) {
|
|
221
|
+
socket.emit('processes:list', { daemonRunning: false, apps: [] });
|
|
222
|
+
return;
|
|
223
|
+
}
|
|
224
|
+
const listed = await runner.list({ pm2Path: pm2.path ?? 'pm2' });
|
|
225
|
+
socket.emit('processes:list', {
|
|
226
|
+
daemonRunning: true,
|
|
227
|
+
apps: listed.ok ? listed.apps : [],
|
|
228
|
+
});
|
|
229
|
+
} catch { /* non-fatal */ }
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
async function pushCertExpiry() {
|
|
233
|
+
if (!socket?.connected) return;
|
|
234
|
+
try {
|
|
235
|
+
const certs = await listAllCerts();
|
|
236
|
+
socket.emit('cert:expiry', certs.map(c => ({
|
|
237
|
+
domain: c.domain,
|
|
238
|
+
expiresAt: c.expiry ?? null,
|
|
239
|
+
daysLeft: c.daysLeft ?? null,
|
|
240
|
+
})));
|
|
241
|
+
} catch { /* non-fatal */ }
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
// ─── Command handlers ──────────────────────────────────────────────────────────
|
|
245
|
+
//
|
|
246
|
+
// Every command goes through runCommand(), which performs the local permission
|
|
247
|
+
// check against core/hubPermissions.js and always replies with command:result.
|
|
248
|
+
// The local check is the real security gate: the Hub's own copy of the
|
|
249
|
+
// permissions is only a UI hint and is never trusted here.
|
|
250
|
+
|
|
251
|
+
function getPermissions() {
|
|
252
|
+
return getHubPermissions();
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/** Emits the acknowledgement the Hub and its browser clients wait for. */
|
|
256
|
+
function sendResult(requestId, ok, { error = null, data = null } = {}) {
|
|
257
|
+
if (!requestId || !socket?.connected) return;
|
|
258
|
+
socket.emit('command:result', { requestId, ok, error, data });
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* Runs one hub command: permission check, execution, acknowledgement.
|
|
263
|
+
*
|
|
264
|
+
* @param {string} command
|
|
265
|
+
* @param {Object} payload May carry a requestId added by the Hub
|
|
266
|
+
* @param {(payload: Object) => Promise<Object|void>} fn
|
|
267
|
+
*/
|
|
268
|
+
async function runCommand(command, payload, fn) {
|
|
269
|
+
const requestId = payload?.requestId ?? null;
|
|
270
|
+
|
|
271
|
+
const verdict = checkCommandAllowed(command, payload, getPermissions());
|
|
272
|
+
if (!verdict.allowed) {
|
|
273
|
+
console.warn('[hubAgent] refused', command, '-', verdict.reason);
|
|
274
|
+
sendResult(requestId, false, { error: verdict.reason });
|
|
275
|
+
return;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
try {
|
|
279
|
+
const data = await fn(payload ?? {});
|
|
280
|
+
sendResult(requestId, true, { data: data ?? null });
|
|
281
|
+
} catch (err) {
|
|
282
|
+
console.error('[hubAgent]', command, 'failed:', err.message);
|
|
283
|
+
sendResult(requestId, false, { error: err.message });
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
async function handleReloadNginx(payload) {
|
|
288
|
+
return runCommand('hub:reloadNginx', payload, async () => {
|
|
289
|
+
// Reuse the dashboard's reload path instead of rebuilding the command
|
|
290
|
+
// here: it resolves the nginx.exe path on Windows and uses `sudo -n`.
|
|
291
|
+
const r = await nginxReload();
|
|
292
|
+
pushNginxStatus();
|
|
293
|
+
notifyLocalDashboard('nginx');
|
|
294
|
+
if (!r?.success) throw new Error(r?.output || 'nginx reload failed');
|
|
295
|
+
return { output: r.output ?? '' };
|
|
296
|
+
});
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
async function handleNginxAction(payload) {
|
|
300
|
+
return runCommand('hub:nginxAction', payload, async ({ action }) => {
|
|
301
|
+
const actions = {
|
|
302
|
+
start: nginxStart, stop: nginxStop, restart: nginxRestart, test: nginxTest,
|
|
303
|
+
};
|
|
304
|
+
const fn = actions[action];
|
|
305
|
+
if (!fn) throw new Error(`Unknown nginx action: ${action}`);
|
|
306
|
+
|
|
307
|
+
const r = await fn();
|
|
308
|
+
pushNginxStatus();
|
|
309
|
+
notifyLocalDashboard('nginx');
|
|
310
|
+
if (r && r.success === false) throw new Error(r.output || `nginx ${action} failed`);
|
|
311
|
+
return { action, output: r?.output ?? '' };
|
|
312
|
+
});
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
async function handleGetConfigs(payload) {
|
|
316
|
+
return runCommand('hub:getConfigs', payload, async () => {
|
|
317
|
+
const files = await listConfigs();
|
|
318
|
+
const configs = [];
|
|
319
|
+
for (const f of files) {
|
|
320
|
+
try {
|
|
321
|
+
const result = await getConfig(f);
|
|
322
|
+
configs.push({ name: f, content: result.content });
|
|
323
|
+
} catch { configs.push({ name: f, content: '' }); }
|
|
324
|
+
}
|
|
325
|
+
if (socket?.connected) socket.emit('nginx:configs', configs);
|
|
326
|
+
return { count: configs.length };
|
|
327
|
+
});
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/**
|
|
331
|
+
* Decides whether a `hub:saveConfig` should also reload nginx.
|
|
332
|
+
*
|
|
333
|
+
* Saving and reloading are two different capabilities, and a server may grant
|
|
334
|
+
* one without the other: `nginx.config.edit` lets the Hub change a file that
|
|
335
|
+
* `nginx -t` accepts, while `nginx.reload` lets it affect live traffic. An
|
|
336
|
+
* operator who allows editing but not reloading has said something specific,
|
|
337
|
+
* so a `reload: true` from the Hub must not quietly override it.
|
|
338
|
+
*
|
|
339
|
+
* The save therefore always stands on its own, and a refused reload is
|
|
340
|
+
* reported rather than treated as a failure — the file is written and valid
|
|
341
|
+
* either way.
|
|
342
|
+
*
|
|
343
|
+
* Exported so the decision can be tested without nginx: it is a pure function
|
|
344
|
+
* of the payload and the stored permission map.
|
|
345
|
+
*
|
|
346
|
+
* @param {Object} payload The command payload
|
|
347
|
+
* @param {Object} permissions The granted permission map
|
|
348
|
+
* @returns {{ requested: boolean, allowed: boolean, reason: string|null }}
|
|
349
|
+
*/
|
|
350
|
+
export function resolveSaveConfigReload(payload, permissions) {
|
|
351
|
+
// Only an explicit `true` asks for a reload. Anything else — absent, false,
|
|
352
|
+
// "true", 1 — is not a request, because a reload affects live traffic and
|
|
353
|
+
// should never happen through a loose coercion.
|
|
354
|
+
const requested = payload?.reload === true;
|
|
355
|
+
if (!requested) return { requested: false, allowed: false, reason: null };
|
|
356
|
+
|
|
357
|
+
const verdict = checkCommandAllowed('hub:reloadNginx', {}, permissions);
|
|
358
|
+
return {
|
|
359
|
+
requested: true,
|
|
360
|
+
allowed: verdict.allowed,
|
|
361
|
+
reason: verdict.allowed ? null : verdict.reason,
|
|
362
|
+
};
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
async function handleSaveConfig(payload) {
|
|
366
|
+
return runCommand('hub:saveConfig', payload, async (body) => {
|
|
367
|
+
const { filename, content } = body;
|
|
368
|
+
if (typeof filename !== 'string' || !filename.trim()) throw new Error('filename is required');
|
|
369
|
+
if (typeof content !== 'string') throw new Error('content must be a string');
|
|
370
|
+
|
|
371
|
+
// Decided BEFORE the write, so the answer cannot depend on anything the
|
|
372
|
+
// save itself changes.
|
|
373
|
+
const wanted = resolveSaveConfigReload(body, getPermissions());
|
|
374
|
+
|
|
375
|
+
// saveConfig() validates the filename, keeps the write inside the nginx
|
|
376
|
+
// config directory, tests with isNginxTestOk() and restores the previous
|
|
377
|
+
// bytes when nginx rejects the result.
|
|
378
|
+
const r = await saveConfig(filename, content);
|
|
379
|
+
if (!r?.success) throw new Error(r?.output || 'nginx rejected the config');
|
|
380
|
+
|
|
381
|
+
const result = {
|
|
382
|
+
filename,
|
|
383
|
+
output: r.output ?? '',
|
|
384
|
+
reloadRequested: wanted.requested,
|
|
385
|
+
reloaded: false,
|
|
386
|
+
reloadDetail: wanted.reason,
|
|
387
|
+
};
|
|
388
|
+
|
|
389
|
+
if (wanted.requested && wanted.allowed) {
|
|
390
|
+
// The config has already passed `nginx -t`, so this is the only step
|
|
391
|
+
// left. A reload that fails does NOT undo the save: the file on disk is
|
|
392
|
+
// valid, and rolling it back would discard a good edit over a transient
|
|
393
|
+
// reload problem. It is reported instead.
|
|
394
|
+
const reloaded = await nginxReload();
|
|
395
|
+
result.reloaded = Boolean(reloaded?.success);
|
|
396
|
+
result.reloadDetail = result.reloaded
|
|
397
|
+
? (reloaded.output ?? '').trim() || 'nginx reloaded'
|
|
398
|
+
: `the config was saved and is valid, but the reload failed: ${(reloaded?.output ?? '').trim() || 'no output'}`;
|
|
399
|
+
} else if (!wanted.requested) {
|
|
400
|
+
result.reloadDetail = 'no reload was requested; the new config takes effect on the next reload';
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
pushNginxStatus();
|
|
404
|
+
pushDomainConfigs();
|
|
405
|
+
notifyLocalDashboard('nginx');
|
|
406
|
+
return result;
|
|
407
|
+
});
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
async function handleGetLogs(payload) {
|
|
411
|
+
return runCommand('hub:getLogs', payload, async ({ lines }) => {
|
|
412
|
+
const result = await getLogs(lines ?? 200);
|
|
413
|
+
if (socket?.connected) socket.emit('nginx:logs', result);
|
|
414
|
+
return { lines: result?.lines?.length ?? 0 };
|
|
415
|
+
});
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
async function handleManageDomain(payload) {
|
|
419
|
+
return runCommand('hub:manageDomain', payload, async (p) => {
|
|
420
|
+
const { handleHubDomainCommand } = await import('../domains-db.js');
|
|
421
|
+
const result = await handleHubDomainCommand(p);
|
|
422
|
+
if (result && result.ok === false) throw new Error(result.error || 'domain command failed');
|
|
423
|
+
|
|
424
|
+
// Re-sync to the hub and refresh any open local dashboard tab.
|
|
425
|
+
pushDomainHealth();
|
|
426
|
+
pushDomainConfigs();
|
|
427
|
+
notifyLocalDashboard('domains');
|
|
428
|
+
return result ?? null;
|
|
429
|
+
});
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
async function handleManageSSL(payload) {
|
|
433
|
+
return runCommand('hub:manageSSL', payload, async (p) => {
|
|
434
|
+
const { handleHubSslCommand } = await import('../cert-operations.js');
|
|
435
|
+
const result = await handleHubSslCommand(p);
|
|
436
|
+
if (result && result.ok === false) throw new Error(result.error || 'ssl command failed');
|
|
437
|
+
|
|
438
|
+
pushCertExpiry();
|
|
439
|
+
pushDomainConfigs();
|
|
440
|
+
notifyLocalDashboard('ssl');
|
|
441
|
+
return result ?? null;
|
|
442
|
+
});
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
/**
|
|
446
|
+
* Reports the deployed apps and where each one stands.
|
|
447
|
+
*
|
|
448
|
+
* Monitoring, like the process list: a paired server is a monitored server.
|
|
449
|
+
* publicApp() is what makes that safe — environment variable NAMES, never
|
|
450
|
+
* values, and no credentials of any kind.
|
|
451
|
+
*/
|
|
452
|
+
async function pushDeployApps() {
|
|
453
|
+
if (!socket?.connected) return;
|
|
454
|
+
try {
|
|
455
|
+
const { describeApps } = await import('../deploy/status.js');
|
|
456
|
+
const { publicApp } = await import('../deploy/apps.js');
|
|
457
|
+
socket.emit('deploy:apps', await describeApps({ view: publicApp }));
|
|
458
|
+
} catch { /* non-fatal */ }
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
async function handleManageDeploy(payload) {
|
|
462
|
+
return runCommand('hub:manageDeploy', payload, async (p) => {
|
|
463
|
+
const { handleHubDeployCommand } = await import('../deploy/hubCommands.js');
|
|
464
|
+
try {
|
|
465
|
+
return await handleHubDeployCommand(p, { onFinished: () => { pushDeployApps(); } });
|
|
466
|
+
} finally {
|
|
467
|
+
// Whatever the action was, the list is the thing that may have changed.
|
|
468
|
+
if (!['list', 'detail', 'run', 'github', 'getEnv'].includes(p?.action)) pushDeployApps();
|
|
469
|
+
// Linking or deleting with a domain changes the domain list too.
|
|
470
|
+
if (['linkDomain', 'delete'].includes(p?.action)) { pushDomainConfigs(); pushDomainHealth(); notifyLocalDashboard('domains'); }
|
|
471
|
+
}
|
|
472
|
+
});
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
async function handleDeployHook(payload) {
|
|
476
|
+
return runCommand('hub:deployHook', payload, async (p) => {
|
|
477
|
+
const { handleHubDeployHook } = await import('../deploy/hubCommands.js');
|
|
478
|
+
const result = await handleHubDeployHook(p, { onFinished: () => { pushDeployApps(); } });
|
|
479
|
+
if (result?.started) pushDeployApps();
|
|
480
|
+
return result;
|
|
481
|
+
});
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
// ─── Permission + revision sync ───────────────────────────────────────────────
|
|
485
|
+
|
|
486
|
+
/**
|
|
487
|
+
* Sends the permission catalog and the current state to the Hub.
|
|
488
|
+
* Called on connect and whenever permissions change, so the Hub UI can grey
|
|
489
|
+
* out actions immediately instead of waiting for a poll.
|
|
490
|
+
*/
|
|
491
|
+
export function pushPermissions() {
|
|
492
|
+
if (!socket?.connected) return;
|
|
493
|
+
try {
|
|
494
|
+
socket.emit('permissions:state', {
|
|
495
|
+
tree: PERMISSION_TREE,
|
|
496
|
+
permissions: getHubPermissions(),
|
|
497
|
+
comingSoon: COMING_SOON_PERMISSION_IDS,
|
|
498
|
+
});
|
|
499
|
+
} catch { /* non-fatal */ }
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
/**
|
|
503
|
+
* Watches the shared revision counter.
|
|
504
|
+
*
|
|
505
|
+
* The CLI runs in a different process from the agent, so an in-process event
|
|
506
|
+
* cannot reach here. core/hubLink.js bumps a counter in SQLite on every change
|
|
507
|
+
* the Hub should learn about; polling it is a cheap read and only triggers a
|
|
508
|
+
* push when it actually moved — far better than the old 5-minute interval.
|
|
509
|
+
*/
|
|
510
|
+
let lastRevision = -1;
|
|
511
|
+
|
|
512
|
+
async function checkRevision() {
|
|
513
|
+
if (!socket?.connected) return;
|
|
514
|
+
try {
|
|
515
|
+
const rev = getHubRevision();
|
|
516
|
+
if (rev === lastRevision) return;
|
|
517
|
+
lastRevision = rev;
|
|
518
|
+
|
|
519
|
+
pushPermissions();
|
|
520
|
+
pushDomainConfigs();
|
|
521
|
+
pushDomainHealth();
|
|
522
|
+
pushCertExpiry();
|
|
523
|
+
pushServiceState();
|
|
524
|
+
} catch { /* non-fatal */ }
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
/**
|
|
528
|
+
* Tells any open dashboard that something changed, so the UI refreshes the
|
|
529
|
+
* affected section without a page reload.
|
|
530
|
+
*
|
|
531
|
+
* This used to call the socket layer directly, which is what tied the agent to
|
|
532
|
+
* a process that had a Socket.io server in it. It now publishes to the job bus,
|
|
533
|
+
* and the transport is someone else's problem: a dashboard running the jobs
|
|
534
|
+
* itself forwards it to its own browsers, and the daemon forwards it over IPC
|
|
535
|
+
* to a dashboard in another process. The payload is unchanged, so
|
|
536
|
+
* docs/api-contract.md still describes it exactly.
|
|
537
|
+
*
|
|
538
|
+
* @param {'domains'|'ssl'|'nginx'|'settings'} section
|
|
539
|
+
*/
|
|
540
|
+
function notifyLocalDashboard(section) {
|
|
541
|
+
try {
|
|
542
|
+
publish('local:changed', { section, source: 'hub', at: Date.now() });
|
|
543
|
+
} catch { /* never let a listener break a hub command */ }
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
// ─── Lifecycle ─────────────────────────────────────────────────────────────────
|
|
547
|
+
|
|
548
|
+
function startHeartbeats() {
|
|
549
|
+
stopHeartbeats(); // clear any existing intervals first
|
|
550
|
+
|
|
551
|
+
// Forget the revision we had last pushed. Anything could have changed while
|
|
552
|
+
// the socket was down, and the hub may have been restarted or re-seeded, so
|
|
553
|
+
// the next poll must be allowed to push even if the counter has not moved.
|
|
554
|
+
lastRevision = -1;
|
|
555
|
+
intervals.push(setInterval(pushNginxStatus, 5_000));
|
|
556
|
+
intervals.push(setInterval(pushResources, 30_000));
|
|
557
|
+
intervals.push(setInterval(pushDomainHealth, 60_000));
|
|
558
|
+
intervals.push(setInterval(pushDomainConfigs, 5 * 60_000));
|
|
559
|
+
intervals.push(setInterval(pushChannelsList, 5 * 60_000));
|
|
560
|
+
intervals.push(setInterval(pushCertExpiry, 12 * 60 * 60 * 1000));
|
|
561
|
+
// Both of these shell out to PM2, so they are deliberately infrequent.
|
|
562
|
+
intervals.push(setInterval(pushServiceState, 5 * 60_000));
|
|
563
|
+
intervals.push(setInterval(pushProcesses, 60_000));
|
|
564
|
+
// A deployment can be started from the dashboard or by CI, in another
|
|
565
|
+
// process, so the Hub learns about it from this rather than from an event.
|
|
566
|
+
intervals.push(setInterval(pushDeployApps, 20_000));
|
|
567
|
+
// Cheap shared signal so CLI-side changes reach the Hub in seconds.
|
|
568
|
+
intervals.push(setInterval(checkRevision, REVISION_POLL_MS));
|
|
569
|
+
// Push immediately on connect
|
|
570
|
+
pushNginxStatus();
|
|
571
|
+
pushResources();
|
|
572
|
+
pushDomainHealth();
|
|
573
|
+
pushDomainConfigs();
|
|
574
|
+
pushChannelsList();
|
|
575
|
+
pushCertExpiry();
|
|
576
|
+
pushPermissions();
|
|
577
|
+
pushServiceState();
|
|
578
|
+
pushProcesses();
|
|
579
|
+
pushDeployApps();
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
function stopHeartbeats() {
|
|
583
|
+
for (const id of intervals) clearInterval(id);
|
|
584
|
+
intervals = [];
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
function cancelRejoin() {
|
|
588
|
+
if (rejoinTimer) {
|
|
589
|
+
clearTimeout(rejoinTimer);
|
|
590
|
+
rejoinTimer = null;
|
|
591
|
+
}
|
|
592
|
+
rejoinDelay = 0;
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
export function initHubAgent() {
|
|
596
|
+
const link = getHubLink();
|
|
597
|
+
if (!link) {
|
|
598
|
+
setState(HUB_AGENT_STATUS.NOT_LINKED);
|
|
599
|
+
agentState.attempts = 0;
|
|
600
|
+
return;
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
const { hubUrl, serverId, secret } = link;
|
|
604
|
+
const url = resolveHubOrigin(hubUrl);
|
|
605
|
+
|
|
606
|
+
// Tear down any existing socket FIRST.
|
|
607
|
+
//
|
|
608
|
+
// This is the fix for a connection flap that looked like a hub problem.
|
|
609
|
+
// Reassigning `socket` below without disconnecting the old one left it
|
|
610
|
+
// CONNECTED but unreferenced — and the hub keeps one socket per serverId
|
|
611
|
+
// (agentServer.ts), evicting the older when a second arrives. The evicted
|
|
612
|
+
// client sees 'io server disconnect', re-dials, and evicts the other one:
|
|
613
|
+
// two sockets taking turns, "Connected"/"Disconnected" every few seconds,
|
|
614
|
+
// for ever. Reproduced against a test hub, which counted two live sockets
|
|
615
|
+
// from a single process.
|
|
616
|
+
//
|
|
617
|
+
// So this function is now idempotent: calling it twice leaves exactly one
|
|
618
|
+
// socket. That matters because it is reachable from more than one place —
|
|
619
|
+
// the job owner starts it, and re-pairing restarts it.
|
|
620
|
+
stopHubAgent();
|
|
621
|
+
|
|
622
|
+
// Recorded AFTER the teardown, because stopHubAgent() clears it. Setting it
|
|
623
|
+
// first left it null, so the next reconcile saw a mismatch and redialled —
|
|
624
|
+
// the very churn this signature exists to prevent.
|
|
625
|
+
activeLink = linkSignature(link);
|
|
626
|
+
|
|
627
|
+
cancelRejoin();
|
|
628
|
+
agentState = { status: HUB_AGENT_STATUS.CONNECTING, lastError: null, attempts: 0 };
|
|
629
|
+
|
|
630
|
+
// Bind handlers to this specific instance. Re-pairing replaces `socket`, and
|
|
631
|
+
// a late event from a discarded instance must not mutate the new one's state.
|
|
632
|
+
const self = io(`${url}/agent-socket`, {
|
|
633
|
+
auth: { serverId, secret },
|
|
634
|
+
reconnection: true,
|
|
635
|
+
reconnectionDelay: RECONNECT_DELAY_MIN,
|
|
636
|
+
reconnectionDelayMax: RECONNECT_DELAY_MAX,
|
|
637
|
+
transports: ['websocket'],
|
|
638
|
+
});
|
|
639
|
+
socket = self;
|
|
640
|
+
|
|
641
|
+
const isCurrent = () => socket === self;
|
|
642
|
+
|
|
643
|
+
self.on('connect', () => {
|
|
644
|
+
if (!isCurrent()) return;
|
|
645
|
+
console.log('[hubAgent] Connected to hub');
|
|
646
|
+
cancelRejoin();
|
|
647
|
+
agentState = { status: HUB_AGENT_STATUS.CONNECTED, lastError: null, attempts: 0 };
|
|
648
|
+
// Push server metadata so the hub can tailor the domain form (SSL paths, etc.)
|
|
649
|
+
const { sslDir } = loadConfig();
|
|
650
|
+
self.emit('server:info', { platform: platformName, sslDir: sslDir ?? '' });
|
|
651
|
+
startHeartbeats();
|
|
652
|
+
});
|
|
653
|
+
|
|
654
|
+
self.on('disconnect', (reason) => {
|
|
655
|
+
if (!isCurrent()) return;
|
|
656
|
+
console.log('[hubAgent] Disconnected:', reason);
|
|
657
|
+
stopHeartbeats();
|
|
658
|
+
|
|
659
|
+
if (agentState.status === HUB_AGENT_STATUS.CONNECTED) {
|
|
660
|
+
setState(HUB_AGENT_STATUS.CONNECTING, null);
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
// 'io server disconnect' means the hub closed us deliberately — the most
|
|
664
|
+
// common cause is a rotated secret. socket.io will not retry on its own
|
|
665
|
+
// for this reason, so re-dial here: the next handshake either succeeds or
|
|
666
|
+
// fails with INVALID_SECRET, which is how we reach the 'rejected' state.
|
|
667
|
+
if (reason === 'io server disconnect') scheduleRejoin();
|
|
668
|
+
});
|
|
669
|
+
|
|
670
|
+
/** Manual re-dial with its own capped backoff (socket.io's is not used here). */
|
|
671
|
+
function scheduleRejoin() {
|
|
672
|
+
if (rejoinTimer) return;
|
|
673
|
+
|
|
674
|
+
rejoinDelay = rejoinDelay
|
|
675
|
+
? Math.min(rejoinDelay * 2, RECONNECT_DELAY_MAX)
|
|
676
|
+
: RECONNECT_DELAY_MIN;
|
|
677
|
+
|
|
678
|
+
rejoinTimer = setTimeout(() => {
|
|
679
|
+
rejoinTimer = null;
|
|
680
|
+
if (!isCurrent()) return;
|
|
681
|
+
// A rejection already decided the outcome; do not fight it.
|
|
682
|
+
if (agentState.status === HUB_AGENT_STATUS.REJECTED) return;
|
|
683
|
+
try {
|
|
684
|
+
self.connect();
|
|
685
|
+
} catch { /* manager torn down */ }
|
|
686
|
+
}, rejoinDelay);
|
|
687
|
+
}
|
|
688
|
+
|
|
689
|
+
self.on('connect_error', (err) => {
|
|
690
|
+
const message = err?.message ?? 'unknown error';
|
|
691
|
+
|
|
692
|
+
if (isAuthRejection(message)) {
|
|
693
|
+
// Permanent until the user pastes a new string — retrying cannot help,
|
|
694
|
+
// so stop the manager's backoff instead of looping forever.
|
|
695
|
+
console.error('[hubAgent] Hub rejected the handshake:', message);
|
|
696
|
+
try {
|
|
697
|
+
self.io.reconnection(false);
|
|
698
|
+
self.disconnect();
|
|
699
|
+
} catch { /* already torn down */ }
|
|
700
|
+
|
|
701
|
+
if (!isCurrent()) return;
|
|
702
|
+
cancelRejoin();
|
|
703
|
+
setState(HUB_AGENT_STATUS.REJECTED, describeRejection(message));
|
|
704
|
+
stopHeartbeats();
|
|
705
|
+
return;
|
|
706
|
+
}
|
|
707
|
+
|
|
708
|
+
if (!isCurrent()) return;
|
|
709
|
+
|
|
710
|
+
// A manual re-dial that fails on the transport gets its own retry, since
|
|
711
|
+
// socket.io's backoff is not driving this attempt.
|
|
712
|
+
if (!self.active) scheduleRejoin();
|
|
713
|
+
|
|
714
|
+
// Transport/network failure — socket.io keeps retrying with backoff.
|
|
715
|
+
agentState = {
|
|
716
|
+
status: HUB_AGENT_STATUS.UNREACHABLE,
|
|
717
|
+
lastError: `Cannot reach the hub (${message}). Retrying...`,
|
|
718
|
+
attempts: agentState.attempts + 1,
|
|
719
|
+
};
|
|
720
|
+
console.error('[hubAgent] Connection error:', message);
|
|
721
|
+
});
|
|
722
|
+
|
|
723
|
+
self.on('hub:reloadNginx', handleReloadNginx);
|
|
724
|
+
self.on('hub:manageDomain', handleManageDomain);
|
|
725
|
+
self.on('hub:manageSSL', handleManageSSL);
|
|
726
|
+
self.on('hub:nginxAction', handleNginxAction);
|
|
727
|
+
self.on('hub:getConfigs', handleGetConfigs);
|
|
728
|
+
self.on('hub:saveConfig', handleSaveConfig);
|
|
729
|
+
self.on('hub:getLogs', handleGetLogs);
|
|
730
|
+
self.on('hub:manageDeploy', handleManageDeploy);
|
|
731
|
+
self.on('hub:deployHook', handleDeployHook);
|
|
732
|
+
}
|
|
733
|
+
|
|
734
|
+
/**
|
|
735
|
+
* Identity of the pairing a socket was started with.
|
|
736
|
+
*
|
|
737
|
+
* Used to decide whether a stored pairing CHANGED, which is a different
|
|
738
|
+
* question from whether the agent happens to be connected right now. The
|
|
739
|
+
* secret is included as a hash so a rotated secret counts as a change, and
|
|
740
|
+
* never as plain text — this value is compared, logged and kept in memory.
|
|
741
|
+
*/
|
|
742
|
+
function linkSignature(link) {
|
|
743
|
+
if (!link) return null;
|
|
744
|
+
const secretMark = createHash('sha256').update(String(link.secret ?? '')).digest('hex').slice(0, 12);
|
|
745
|
+
return `${resolveHubOrigin(link.hubUrl)}|${link.serverId}|${secretMark}`;
|
|
746
|
+
}
|
|
747
|
+
|
|
748
|
+
/** The signature the current socket was created for, or null when stopped. */
|
|
749
|
+
let activeLink = null;
|
|
750
|
+
|
|
751
|
+
/**
|
|
752
|
+
* Brings the agent into line with the stored pairing, and does nothing
|
|
753
|
+
* otherwise.
|
|
754
|
+
*
|
|
755
|
+
* Called by the job owner when the shared revision counter moves, which is how
|
|
756
|
+
* a pairing made in the dashboard or the CLI reaches the process that actually
|
|
757
|
+
* holds the agent.
|
|
758
|
+
*
|
|
759
|
+
* ── Why this compares identity, not connectedness ──────────────────────────
|
|
760
|
+
*
|
|
761
|
+
* The first version re-ran `initHubAgent()` whenever the agent was not
|
|
762
|
+
* CONNECTED. That looked harmless and was not: the revision counter also moves
|
|
763
|
+
* on every domain, certificate and permission change, so a busy server tore
|
|
764
|
+
* the socket down and redialled it repeatedly — including while it was still
|
|
765
|
+
* mid-connect, which is a perfectly normal state. It showed up as Hub updates
|
|
766
|
+
* arriving seconds late.
|
|
767
|
+
*
|
|
768
|
+
* So a socket that exists and was created for the pairing that is still stored
|
|
769
|
+
* is left strictly alone, connected or not: socket.io's own reconnection is
|
|
770
|
+
* what handles a transport problem, and this function must not fight it.
|
|
771
|
+
*
|
|
772
|
+
* @returns {{ action: 'none'|'started'|'restarted'|'stopped', reason: string }}
|
|
773
|
+
*/
|
|
774
|
+
export function reconcileHubAgent() {
|
|
775
|
+
const link = getHubLink();
|
|
776
|
+
|
|
777
|
+
if (!link) {
|
|
778
|
+
if (!socket && !activeLink) return { action: 'none', reason: 'not linked, no agent running' };
|
|
779
|
+
stopHubAgent();
|
|
780
|
+
return { action: 'stopped', reason: 'the pairing was removed' };
|
|
781
|
+
}
|
|
782
|
+
|
|
783
|
+
const wanted = linkSignature(link);
|
|
784
|
+
|
|
785
|
+
if (!socket) {
|
|
786
|
+
initHubAgent();
|
|
787
|
+
return { action: 'started', reason: 'a pairing exists but no agent was running' };
|
|
788
|
+
}
|
|
789
|
+
|
|
790
|
+
if (activeLink !== wanted) {
|
|
791
|
+
initHubAgent();
|
|
792
|
+
return { action: 'restarted', reason: 'the pairing changed' };
|
|
793
|
+
}
|
|
794
|
+
|
|
795
|
+
return { action: 'none', reason: 'the agent already matches the stored pairing' };
|
|
796
|
+
}
|
|
797
|
+
|
|
798
|
+
export function stopHubAgent() {
|
|
799
|
+
stopHeartbeats();
|
|
800
|
+
cancelRejoin();
|
|
801
|
+
activeLink = null;
|
|
802
|
+
if (socket) {
|
|
803
|
+
try {
|
|
804
|
+
socket.io.reconnection(false);
|
|
805
|
+
} catch { /* manager already gone */ }
|
|
806
|
+
socket.disconnect();
|
|
807
|
+
socket = null;
|
|
808
|
+
}
|
|
809
|
+
setState(HUB_AGENT_STATUS.NOT_LINKED);
|
|
810
|
+
agentState.attempts = 0;
|
|
811
|
+
}
|
|
812
|
+
|
|
813
|
+
/**
|
|
814
|
+
* Current agent state for the status API.
|
|
815
|
+
* `connected` is kept as a derived boolean for existing consumers.
|
|
816
|
+
* Never includes the shared secret.
|
|
817
|
+
*/
|
|
818
|
+
export function getHubAgentStatus() {
|
|
819
|
+
const linked = getHubLink() !== null;
|
|
820
|
+
const connected = socket?.connected ?? false;
|
|
821
|
+
|
|
822
|
+
let status = agentState.status;
|
|
823
|
+
|
|
824
|
+
if (!linked) {
|
|
825
|
+
// The link may have been cleared out of band; never report a stale state.
|
|
826
|
+
status = HUB_AGENT_STATUS.NOT_LINKED;
|
|
827
|
+
} else if (connected) {
|
|
828
|
+
status = HUB_AGENT_STATUS.CONNECTED;
|
|
829
|
+
} else if (status === HUB_AGENT_STATUS.NOT_LINKED) {
|
|
830
|
+
// Linked, but this process has not started the agent yet (initHubAgent
|
|
831
|
+
// runs at dashboard startup). Report it as on its way, not unlinked.
|
|
832
|
+
status = HUB_AGENT_STATUS.CONNECTING;
|
|
833
|
+
}
|
|
834
|
+
|
|
835
|
+
return {
|
|
836
|
+
linked,
|
|
837
|
+
connected,
|
|
838
|
+
status,
|
|
839
|
+
lastError: linked ? agentState.lastError : null,
|
|
840
|
+
attempts: linked ? agentState.attempts : 0,
|
|
841
|
+
};
|
|
842
|
+
}
|