easy-devops 1.3.4 → 1.3.6
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/dist/ui/assets/index-BUpPxWmp.css +1 -0
- package/dist/ui/assets/index-DD0JjK0Q.js +475 -0
- package/dist/ui/assets/{socket-K5ttLD2q.js → socket-4tfdap-e.js} +2 -2
- package/dist/ui/index.html +2 -2
- package/package.json +1 -1
- package/src/cli/menus/dashboard.js +7 -134
- package/src/cli/menus/update.js +81 -141
- package/src/core/daemon/run.js +7 -1
- package/src/core/dashboardProcess.js +158 -0
- package/src/core/hub/agent.js +84 -15
- package/src/core/hubPermissions.js +24 -9
- package/src/core/jobs/bus.js +5 -0
- package/src/core/updateStatus.js +115 -0
- package/src/core/updater.js +25 -22
- package/src/dashboard/controllers/hubController.js +60 -1
- package/src/dashboard/controllers/updateController.js +213 -0
- package/src/dashboard/lib/daemonBridge.js +22 -1
- package/src/dashboard/routes/update.js +18 -0
- package/src/dashboard/server.js +33 -1
- package/src/dashboard/updateWorker.js +163 -0
- package/dist/ui/assets/index-C2zXdVWq.js +0 -452
- package/dist/ui/assets/index-DxepaqNe.css +0 -1
|
@@ -17,6 +17,7 @@ import { PERMISSION_TREE, COMING_SOON_PERMISSION_IDS } from '../../core/hubPermi
|
|
|
17
17
|
import { getDataDir, isDevDataDir, isDefaultDataDir } from '../../core/platform.js';
|
|
18
18
|
import { inspect } from '../../core/daemon/lock.js';
|
|
19
19
|
import { stopHubAgent, initHubAgent, getHubAgentStatus, pushPermissions } from '../../core/hub/agent.js';
|
|
20
|
+
import { daemonStatus, daemonHubState } from '../lib/daemonBridge.js';
|
|
20
21
|
|
|
21
22
|
// POST /api/hub/connect
|
|
22
23
|
// Body: { connectionString: 'ezops://hubUrl|serverId|secret' }
|
|
@@ -101,10 +102,18 @@ export function updatePermissions(req, res) {
|
|
|
101
102
|
/**
|
|
102
103
|
* Builds the status response. Deliberately omits the secret — it is only ever
|
|
103
104
|
* shown once by the hub, and is never echoed back by this API.
|
|
105
|
+
*
|
|
106
|
+
* The agent lives in whichever process owns the background jobs. When a daemon
|
|
107
|
+
* is running, this process's own agent is not started at all, so asking it
|
|
108
|
+
* would report "connecting" for ever — it has no socket and never will. The
|
|
109
|
+
* answer therefore comes from the daemon (through the bridge), and `agentOwner`
|
|
110
|
+
* says which side reported it.
|
|
104
111
|
*/
|
|
105
112
|
function hubStatusPayload() {
|
|
106
113
|
const link = getLinkStatus();
|
|
107
|
-
const
|
|
114
|
+
const daemon = inspect();
|
|
115
|
+
const agentOwner = daemon.running ? 'daemon' : 'dashboard';
|
|
116
|
+
const agent = agentOwner === 'daemon' ? daemonAgentStatus(link.linked) : getHubAgentStatus();
|
|
108
117
|
|
|
109
118
|
return {
|
|
110
119
|
linked: link.linked,
|
|
@@ -118,6 +127,12 @@ function hubStatusPayload() {
|
|
|
118
127
|
status: agent.status,
|
|
119
128
|
lastError: agent.lastError,
|
|
120
129
|
attempts: agent.attempts,
|
|
130
|
+
// 'daemon' means the background service owns the Hub connection; the state
|
|
131
|
+
// above describes ITS agent, not this process's (which is not running).
|
|
132
|
+
agentOwner,
|
|
133
|
+
// False while the daemon is running but its event channel has not told us
|
|
134
|
+
// its state yet. The UI must say "waiting" rather than invent a status.
|
|
135
|
+
agentReported: agent.reported,
|
|
121
136
|
// Which database this dashboard — and therefore this agent — is actually
|
|
122
137
|
// using. A non-default directory means a CLI and a dashboard can be editing
|
|
123
138
|
// two separate domain lists with no visible sign of it, so the path is
|
|
@@ -133,3 +148,47 @@ function hubStatusPayload() {
|
|
|
133
148
|
devDataDir: isDevDataDir(),
|
|
134
149
|
};
|
|
135
150
|
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* The daemon's agent state, or an honest placeholder when it has not reported.
|
|
154
|
+
*
|
|
155
|
+
* `reported: false` is the important part: the daemon is running but its IPC
|
|
156
|
+
* channel is not up (or the hello frame has not arrived), so the real state is
|
|
157
|
+
* unknown. Claiming `connected` would be wrong and claiming `connecting` with
|
|
158
|
+
* no explanation looked like a stuck refresh — the UI renders the placeholder
|
|
159
|
+
* as "waiting for the background service".
|
|
160
|
+
*/
|
|
161
|
+
function daemonAgentStatus(dbLinked) {
|
|
162
|
+
const reported = daemonHubState();
|
|
163
|
+
|
|
164
|
+
if (!reported) {
|
|
165
|
+
return {
|
|
166
|
+
linked: dbLinked,
|
|
167
|
+
connected: false,
|
|
168
|
+
status: dbLinked ? 'connecting' : 'not_linked',
|
|
169
|
+
lastError: null,
|
|
170
|
+
attempts: 0,
|
|
171
|
+
reported: false,
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
const channelUp = daemonStatus().connected;
|
|
176
|
+
|
|
177
|
+
// A stale snapshot is only trusted while the channel that carried it is up.
|
|
178
|
+
// Once it drops, the bridge already clears its copy; this is the belt-and-
|
|
179
|
+
// braces case of a frame arriving mid-disconnect. A daemon that has not yet
|
|
180
|
+
// reconciled a link made a moment ago reports not_linked — the link is in
|
|
181
|
+
// the shared database, so that is a "starting up", not an "unlinked".
|
|
182
|
+
const linked = reported.linked || dbLinked;
|
|
183
|
+
let status = channelUp ? reported.status : 'connecting';
|
|
184
|
+
if (linked && status === 'not_linked') status = 'connecting';
|
|
185
|
+
|
|
186
|
+
return {
|
|
187
|
+
linked,
|
|
188
|
+
connected: Boolean(reported.connected) && channelUp,
|
|
189
|
+
status,
|
|
190
|
+
lastError: channelUp ? reported.lastError : null,
|
|
191
|
+
attempts: reported.attempts ?? 0,
|
|
192
|
+
reported: channelUp,
|
|
193
|
+
};
|
|
194
|
+
}
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* dashboard/controllers/updateController.js
|
|
3
|
+
*
|
|
4
|
+
* "Update" in Settings, for the dashboard.
|
|
5
|
+
*
|
|
6
|
+
* ── Why the request does not do the work ──────────────────────────────────
|
|
7
|
+
*
|
|
8
|
+
* npm replaces the files this server is running from, and the service it
|
|
9
|
+
* shares a lock with has to be stopped so those files can be replaced. A
|
|
10
|
+
* request cannot survive the server it arrived on, so it could report neither
|
|
11
|
+
* success nor failure. The route therefore only RECORDS the run and spawns a
|
|
12
|
+
* detached worker (dashboard/updateWorker.js) that outlives this process,
|
|
13
|
+
* performs the same core/updater.js flow as `ezz update`, and brings the
|
|
14
|
+
* dashboard back. The outcome lives in core/updateStatus.js, which the new
|
|
15
|
+
* dashboard reads on its first poll — the client discovers "I was restarted"
|
|
16
|
+
* from the status file, not from a response that never came.
|
|
17
|
+
*
|
|
18
|
+
* ── Why the install source gates everything ───────────────────────────────
|
|
19
|
+
*
|
|
20
|
+
* On a git clone `npm install -g` installs a second copy from the registry and
|
|
21
|
+
* leaves the service running the clone, so an update would report success and
|
|
22
|
+
* change nothing. The check endpoint reports the source so the UI can explain
|
|
23
|
+
* it, and start refuses with the commands to run instead.
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
import fs from 'fs';
|
|
27
|
+
import path from 'path';
|
|
28
|
+
import crypto from 'crypto';
|
|
29
|
+
import { spawn } from 'child_process';
|
|
30
|
+
import { fileURLToPath } from 'url';
|
|
31
|
+
|
|
32
|
+
import { getDataDir } from '../../core/platform.js';
|
|
33
|
+
import { loadConfig } from '../../core/config.js';
|
|
34
|
+
import { checkForUpdate, currentVersion } from '../../core/updater.js';
|
|
35
|
+
import { detectInstallSource } from '../../core/installSource.js';
|
|
36
|
+
import {
|
|
37
|
+
readStatus, writeStatus, describeRun, runIsActive,
|
|
38
|
+
} from '../../core/updateStatus.js';
|
|
39
|
+
|
|
40
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
41
|
+
const WORKER_PATH = path.resolve(__dirname, '..', 'updateWorker.js');
|
|
42
|
+
|
|
43
|
+
/** A release triple, the only shape `npm install -g easy-devops@…` gets. */
|
|
44
|
+
const VERSION_PATTERN = /^\d+\.\d+\.\d+$/;
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Starts the detached worker.
|
|
48
|
+
*
|
|
49
|
+
* Detached and unref'd so it survives this process being killed at the stop
|
|
50
|
+
* step; its output goes to a log file because a detached Windows process has
|
|
51
|
+
* no console to inherit.
|
|
52
|
+
*
|
|
53
|
+
* @returns {{ pid: number|undefined }}
|
|
54
|
+
*/
|
|
55
|
+
function spawnWorker({ args, logPath, dataDir }) {
|
|
56
|
+
fs.mkdirSync(path.dirname(logPath), { recursive: true });
|
|
57
|
+
const out = fs.openSync(logPath, 'a');
|
|
58
|
+
try {
|
|
59
|
+
const child = spawn(process.execPath, [WORKER_PATH, ...args], {
|
|
60
|
+
detached: true,
|
|
61
|
+
stdio: ['ignore', out, out],
|
|
62
|
+
env: { ...process.env, EASY_DEVOPS_DATA_DIR: dataDir },
|
|
63
|
+
windowsHide: true,
|
|
64
|
+
});
|
|
65
|
+
child.unref();
|
|
66
|
+
return child;
|
|
67
|
+
} finally {
|
|
68
|
+
fs.closeSync(out);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function describeSource(source) {
|
|
73
|
+
return {
|
|
74
|
+
kind: source.kind,
|
|
75
|
+
reason: source.reason,
|
|
76
|
+
npmUpdatable: source.npmUpdatable,
|
|
77
|
+
howToUpdate: source.howToUpdate,
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
class UpdateController {
|
|
82
|
+
constructor(deps = {}) {
|
|
83
|
+
this.getDataDir = deps.getDataDir ?? (() => getDataDir());
|
|
84
|
+
this.checkForUpdate = deps.checkForUpdate ?? checkForUpdate;
|
|
85
|
+
this.currentVersion = deps.currentVersion ?? currentVersion;
|
|
86
|
+
this.detectInstallSource = deps.detectInstallSource ?? detectInstallSource;
|
|
87
|
+
this.readStatus = deps.readStatus ?? ((dataDir) => readStatus(dataDir));
|
|
88
|
+
this.writeStatus = deps.writeStatus ?? ((patch, opts) => writeStatus(patch, opts));
|
|
89
|
+
this.spawnWorker = deps.spawnWorker ?? spawnWorker;
|
|
90
|
+
this.runId = deps.runId ?? (() => `${Date.now().toString(36)}-${crypto.randomBytes(3).toString('hex')}`);
|
|
91
|
+
this.now = deps.now ?? (() => Date.now());
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* GET /api/update — what the registry has, how this copy is managed, and
|
|
96
|
+
* the last run (if any) so a page that loads after a restart can report it.
|
|
97
|
+
*/
|
|
98
|
+
async check(req, res) {
|
|
99
|
+
try {
|
|
100
|
+
const dataDir = this.getDataDir();
|
|
101
|
+
const check = await this.checkForUpdate();
|
|
102
|
+
res.json({
|
|
103
|
+
current: check.current,
|
|
104
|
+
latest: check.latest,
|
|
105
|
+
reachable: check.reachable,
|
|
106
|
+
updateAvailable: check.updateAvailable,
|
|
107
|
+
source: describeSource(this.detectInstallSource()),
|
|
108
|
+
run: describeRun(this.readStatus(dataDir)),
|
|
109
|
+
});
|
|
110
|
+
} catch {
|
|
111
|
+
res.status(500).json({ error: 'Internal server error' });
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* GET /api/update/status — the run record alone, with this process's
|
|
117
|
+
* version. Polled while an update is in flight: it must not touch the
|
|
118
|
+
* registry, so it stays up while the network does not.
|
|
119
|
+
*/
|
|
120
|
+
async getStatus(req, res) {
|
|
121
|
+
try {
|
|
122
|
+
res.json({
|
|
123
|
+
current: this.currentVersion(),
|
|
124
|
+
run: describeRun(this.readStatus(this.getDataDir())),
|
|
125
|
+
});
|
|
126
|
+
} catch {
|
|
127
|
+
res.status(500).json({ error: 'Internal server error' });
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* POST /api/update — accepts the run and answers before it begins.
|
|
133
|
+
*
|
|
134
|
+
* Body: { version } (a release triple; a fresh registry check fills it in
|
|
135
|
+
* when omitted). 202 means "the worker was started", never "the update
|
|
136
|
+
* succeeded" — that is only ever known from the status record.
|
|
137
|
+
*/
|
|
138
|
+
async start(req, res) {
|
|
139
|
+
try {
|
|
140
|
+
const dataDir = this.getDataDir();
|
|
141
|
+
const source = this.detectInstallSource();
|
|
142
|
+
|
|
143
|
+
if (!source.npmUpdatable) {
|
|
144
|
+
res.status(409).json({
|
|
145
|
+
error: `This copy is ${source.reason}, so npm cannot update it in place.`,
|
|
146
|
+
source: describeSource(source),
|
|
147
|
+
});
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
const existing = describeRun(this.readStatus(dataDir));
|
|
152
|
+
if (runIsActive(existing)) {
|
|
153
|
+
res.status(409).json({
|
|
154
|
+
error: 'An update is already running.',
|
|
155
|
+
run: existing,
|
|
156
|
+
});
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
let target = String(req.body?.version ?? '').trim();
|
|
161
|
+
if (!VERSION_PATTERN.test(target)) {
|
|
162
|
+
const check = await this.checkForUpdate();
|
|
163
|
+
if (!check.reachable) {
|
|
164
|
+
res.status(502).json({
|
|
165
|
+
error: 'Could not reach the npm registry to find the latest version.',
|
|
166
|
+
});
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
if (!check.updateAvailable) {
|
|
170
|
+
res.status(409).json({
|
|
171
|
+
error: `Easy DevOps is already up to date (v${check.current}).`,
|
|
172
|
+
current: check.current,
|
|
173
|
+
latest: check.latest,
|
|
174
|
+
});
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
target = check.latest;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
const run = {
|
|
181
|
+
id: this.runId(),
|
|
182
|
+
phase: 'starting',
|
|
183
|
+
from: this.currentVersion(),
|
|
184
|
+
to: target,
|
|
185
|
+
step: null,
|
|
186
|
+
reason: null,
|
|
187
|
+
logTail: [],
|
|
188
|
+
startedAt: this.now(),
|
|
189
|
+
dashboard: {
|
|
190
|
+
pid: process.pid,
|
|
191
|
+
// The port the request actually arrived on: a dashboard moved
|
|
192
|
+
// with --port comes back where the user left it, not where the
|
|
193
|
+
// config says.
|
|
194
|
+
port: req.socket?.localPort ?? loadConfig().dashboardPort,
|
|
195
|
+
},
|
|
196
|
+
};
|
|
197
|
+
this.writeStatus(run, { dataDir, replace: true });
|
|
198
|
+
|
|
199
|
+
const child = this.spawnWorker({
|
|
200
|
+
args: ['--run-id', run.id],
|
|
201
|
+
logPath: path.join(dataDir, 'logs', 'update.log'),
|
|
202
|
+
dataDir,
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
const written = this.writeStatus({ workerPid: child?.pid ?? null }, { dataDir });
|
|
206
|
+
res.status(202).json({ started: true, run: written });
|
|
207
|
+
} catch {
|
|
208
|
+
res.status(500).json({ error: 'Internal server error' });
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
export default UpdateController;
|
|
@@ -32,6 +32,12 @@ let hello = null;
|
|
|
32
32
|
let lockWatch = null;
|
|
33
33
|
let lastAnnounced = null;
|
|
34
34
|
|
|
35
|
+
// The daemon's view of the Hub agent, kept current from two sources: the
|
|
36
|
+
// `hub` field of its hello frame (the state at connect time) and every
|
|
37
|
+
// `hub:agentState` event it publishes afterwards. Reset when the channel
|
|
38
|
+
// drops, because an unreachable daemon's last words are not its current state.
|
|
39
|
+
let hubState = null;
|
|
40
|
+
|
|
35
41
|
/**
|
|
36
42
|
* Starts the bridge. Safe to call when no daemon exists: the client retries
|
|
37
43
|
* quietly with backoff and connects if one appears later.
|
|
@@ -55,11 +61,13 @@ export function startDaemonBridge({ dataDir = getDataDir() } = {}) {
|
|
|
55
61
|
onEvent(event, payload) {
|
|
56
62
|
if (event === 'daemon:hello') {
|
|
57
63
|
// Identity, not an event the browser needs: the banner reads it from
|
|
58
|
-
// status() instead.
|
|
64
|
+
// status() instead. The Hub agent state travels with it and is kept.
|
|
59
65
|
hello = payload;
|
|
66
|
+
hubState = payload?.hub ?? null;
|
|
60
67
|
announce(dataDir);
|
|
61
68
|
return;
|
|
62
69
|
}
|
|
70
|
+
if (event === 'hub:agentState') hubState = payload;
|
|
63
71
|
publish(event, payload);
|
|
64
72
|
},
|
|
65
73
|
onStatus(connected, detail) {
|
|
@@ -67,6 +75,7 @@ export function startDaemonBridge({ dataDir = getDataDir() } = {}) {
|
|
|
67
75
|
console.log('[daemonBridge] connected to the background service');
|
|
68
76
|
} else {
|
|
69
77
|
hello = null;
|
|
78
|
+
hubState = null;
|
|
70
79
|
// A daemon that stopped is not the same as one we cannot reach, and
|
|
71
80
|
// saying "no event channel" for both was misleading during an ordinary
|
|
72
81
|
// restart. The lock tells us which this is.
|
|
@@ -133,9 +142,21 @@ export function stopDaemonBridge() {
|
|
|
133
142
|
lockWatch?.close();
|
|
134
143
|
lockWatch = null;
|
|
135
144
|
hello = null;
|
|
145
|
+
hubState = null;
|
|
136
146
|
lastAnnounced = null;
|
|
137
147
|
}
|
|
138
148
|
|
|
149
|
+
/**
|
|
150
|
+
* The Hub agent state as reported by the daemon, or null when this process has
|
|
151
|
+
* no live channel to it.
|
|
152
|
+
*
|
|
153
|
+
* A null does NOT mean "not connected": it means "the daemon has not told us".
|
|
154
|
+
* The caller must say so rather than guess.
|
|
155
|
+
*/
|
|
156
|
+
export function daemonHubState() {
|
|
157
|
+
return hubState;
|
|
158
|
+
}
|
|
159
|
+
|
|
139
160
|
/**
|
|
140
161
|
* What the dashboard should tell the user about the background service.
|
|
141
162
|
*
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import express from 'express';
|
|
2
|
+
import UpdateController from '../controllers/updateController.js';
|
|
3
|
+
import { requireAuth } from '../lib/requireAuth.js';
|
|
4
|
+
|
|
5
|
+
const router = express.Router();
|
|
6
|
+
const controller = new UpdateController();
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Updating the dashboard's own code is a local operator action, so it sits
|
|
10
|
+
* behind the same session gate as the rest of the dashboard.
|
|
11
|
+
*/
|
|
12
|
+
router.use(requireAuth);
|
|
13
|
+
|
|
14
|
+
router.get('/update', (req, res, next) => controller.check(req, res, next));
|
|
15
|
+
router.get('/update/status', (req, res, next) => controller.getStatus(req, res, next));
|
|
16
|
+
router.post('/update', (req, res, next) => controller.start(req, res, next));
|
|
17
|
+
|
|
18
|
+
export default router;
|
package/src/dashboard/server.js
CHANGED
|
@@ -17,6 +17,7 @@ import settingsRouter from './routes/settings.js';
|
|
|
17
17
|
import hubRouter from './routes/hub.js';
|
|
18
18
|
import processesRouter from './routes/processes.js';
|
|
19
19
|
import deploymentsRouter from './routes/deployments.js';
|
|
20
|
+
import updateRouter from './routes/update.js';
|
|
20
21
|
import { ensureCode as ensureSetupCode, describe as describeSetupCode } from '../core/setupCode.js';
|
|
21
22
|
import { warmProbes } from './controllers/processesController.js';
|
|
22
23
|
import { stopSslController } from './controllers/sslController.js';
|
|
@@ -174,6 +175,7 @@ app.use('/api', settingsRouter);
|
|
|
174
175
|
app.use('/api/hub', hubRouter);
|
|
175
176
|
app.use('/api', processesRouter);
|
|
176
177
|
app.use('/api/deployments', deploymentsRouter);
|
|
178
|
+
app.use('/api', updateRouter);
|
|
177
179
|
|
|
178
180
|
if (servingNewUi) {
|
|
179
181
|
// Hashed Vite assets are immutable; index.html is served with no-cache
|
|
@@ -184,9 +186,39 @@ if (servingNewUi) {
|
|
|
184
186
|
// handled by the static index. A real path (/domains/example.com) gets the
|
|
185
187
|
// same index.html so the app can resolve it. Static files above win first,
|
|
186
188
|
// and /api and /socket.io never reach this point with a routed request.
|
|
189
|
+
//
|
|
190
|
+
// `root` is load-bearing: with an absolute path, send() walks every segment
|
|
191
|
+
// of it as a potential dotfile and answers 404 for any that starts with a
|
|
192
|
+
// dot — a global Bun install lives under ~/.bun, so the built UI would
|
|
193
|
+
// never be served there. Root-relative parts contain no dot segments.
|
|
194
|
+
//
|
|
195
|
+
// A build that disappears after startup would otherwise reach Express's
|
|
196
|
+
// default error handler, which prints a stack trace in the browser. Fall
|
|
197
|
+
// back to the classic page instead, and report it once, not per request.
|
|
198
|
+
let indexErrorReported = false;
|
|
187
199
|
app.use((req, res, next) => {
|
|
188
200
|
if (req.method !== 'GET' && req.method !== 'HEAD') return next();
|
|
189
|
-
res.sendFile(
|
|
201
|
+
res.sendFile('index.html', {
|
|
202
|
+
root: uiDistDir,
|
|
203
|
+
headers: { 'Cache-Control': 'no-cache' },
|
|
204
|
+
}, (err) => {
|
|
205
|
+
if (!err) return;
|
|
206
|
+
// Client gone, or the body already started: nothing left to render.
|
|
207
|
+
if (res.headersSent || err.code === 'ECONNABORTED' || err.syscall === 'write') return;
|
|
208
|
+
if (!indexErrorReported) {
|
|
209
|
+
indexErrorReported = true;
|
|
210
|
+
process.stderr.write(
|
|
211
|
+
'\nThe dashboard UI could not be served:\n' +
|
|
212
|
+
` ${uiIndexFile}\n` +
|
|
213
|
+
` ${err.message}\n` +
|
|
214
|
+
' Serving the classic dashboard until it can.\n\n'
|
|
215
|
+
);
|
|
216
|
+
}
|
|
217
|
+
res.render('index', (renderErr, html) => {
|
|
218
|
+
if (renderErr) return next(renderErr);
|
|
219
|
+
res.send(html);
|
|
220
|
+
});
|
|
221
|
+
});
|
|
190
222
|
});
|
|
191
223
|
} else {
|
|
192
224
|
// Render EJS template for all other routes
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* dashboard/updateWorker.js
|
|
3
|
+
*
|
|
4
|
+
* The detached half of "Update" in Settings.
|
|
5
|
+
*
|
|
6
|
+
* The dashboard cannot update itself inside a request: npm replaces the very
|
|
7
|
+
* files the process is running, and the process is stopped mid-way, so the
|
|
8
|
+
* HTTP response could never report what actually happened. The route writes
|
|
9
|
+
* the run's first status record and spawns this worker detached. The worker
|
|
10
|
+
* outlives the dashboard (which it stops), runs the SAME core/updater.js flow
|
|
11
|
+
* as `ezz update`, starts the dashboard again, and leaves the outcome in the
|
|
12
|
+
* status file the new dashboard reads.
|
|
13
|
+
*
|
|
14
|
+
* Started only by the update controller — it is not a user-facing command and
|
|
15
|
+
* the CLI has no reason to use it, having its own terminal to report into.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import fs from 'fs';
|
|
19
|
+
import path from 'path';
|
|
20
|
+
import { getDataDir } from '../core/platform.js';
|
|
21
|
+
import { loadConfig } from '../core/config.js';
|
|
22
|
+
import { readStatus, writeStatus } from '../core/updateStatus.js';
|
|
23
|
+
import { startDashboard, stopDashboard, isPidAlive } from '../core/dashboardProcess.js';
|
|
24
|
+
|
|
25
|
+
const runId = parseRunId(process.argv.slice(2));
|
|
26
|
+
const dataDir = getDataDir();
|
|
27
|
+
const LOG_PATH = path.join(dataDir, 'logs', 'update.log');
|
|
28
|
+
|
|
29
|
+
/** Last lines, echoed into the status file for the result card. */
|
|
30
|
+
const logTail = [];
|
|
31
|
+
|
|
32
|
+
function log(line) {
|
|
33
|
+
const text = String(line ?? '');
|
|
34
|
+
try {
|
|
35
|
+
fs.appendFileSync(LOG_PATH, `${text}\n`);
|
|
36
|
+
} catch { /* the status file still carries the tail */ }
|
|
37
|
+
logTail.push(text);
|
|
38
|
+
if (logTail.length > 40) logTail.shift();
|
|
39
|
+
try {
|
|
40
|
+
writeStatus({ logTail: [...logTail] }, { dataDir });
|
|
41
|
+
} catch { /* a lost intermediate write is not fatal */ }
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function parseRunId(argv) {
|
|
45
|
+
const index = argv.indexOf('--run-id');
|
|
46
|
+
return index >= 0 ? argv[index + 1] ?? null : null;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function sleep(ms) {
|
|
50
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
async function main() {
|
|
54
|
+
const run = readStatus(dataDir);
|
|
55
|
+
if (!run) {
|
|
56
|
+
process.stderr.write('update worker: no status record to run\n');
|
|
57
|
+
return 1;
|
|
58
|
+
}
|
|
59
|
+
if (runId && run.id !== runId) {
|
|
60
|
+
process.stderr.write('update worker: this run has been superseded\n');
|
|
61
|
+
return 1;
|
|
62
|
+
}
|
|
63
|
+
if (run.phase !== 'starting') {
|
|
64
|
+
// A second worker for the same record would run npm a second time.
|
|
65
|
+
process.stderr.write(`update worker: run is already ${run.phase}\n`);
|
|
66
|
+
return 1;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// Claim the record with this pid before touching anything: from here on the
|
|
70
|
+
// route's double-start guard sees a live worker.
|
|
71
|
+
writeStatus({ phase: 'stopping', workerPid: process.pid, step: 'dashboard' }, { dataDir });
|
|
72
|
+
log(`Updating Easy DevOps v${run.from} -> v${run.to}`);
|
|
73
|
+
|
|
74
|
+
const heartbeat = setInterval(() => {
|
|
75
|
+
try {
|
|
76
|
+
writeStatus({}, { dataDir });
|
|
77
|
+
} catch { /* transient */ }
|
|
78
|
+
}, 5_000);
|
|
79
|
+
heartbeat.unref?.();
|
|
80
|
+
|
|
81
|
+
// The HTTP response that said "started" has to reach the browser before the
|
|
82
|
+
// server dies; stopping immediately would cut it off.
|
|
83
|
+
await sleep(1_500);
|
|
84
|
+
|
|
85
|
+
const dash = run.dashboard ?? {};
|
|
86
|
+
if (dash.pid) {
|
|
87
|
+
await stopDashboard(dash.pid);
|
|
88
|
+
log('Dashboard stopped');
|
|
89
|
+
// On POSIX the stop signal is asynchronous: the old process must be gone
|
|
90
|
+
// (and its port released) before the new one binds, or it fails to start.
|
|
91
|
+
const deadline = Date.now() + 5_000;
|
|
92
|
+
while (isPidAlive(dash.pid) && Date.now() < deadline) await sleep(200);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
let result;
|
|
96
|
+
try {
|
|
97
|
+
writeStatus({ phase: 'updating', step: 'install' }, { dataDir });
|
|
98
|
+
const updater = await import('../core/updater.js');
|
|
99
|
+
result = await updater.performUpdate({ version: run.to, dataDir, log });
|
|
100
|
+
} catch (error) {
|
|
101
|
+
result = { ok: false, step: 'exception', reason: error?.message ?? String(error) };
|
|
102
|
+
} finally {
|
|
103
|
+
clearInterval(heartbeat);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
writeStatus(
|
|
107
|
+
{
|
|
108
|
+
phase: 'restarting',
|
|
109
|
+
step: 'dashboard',
|
|
110
|
+
from: result.from ?? run.from,
|
|
111
|
+
to: result.to ?? run.to,
|
|
112
|
+
reason: result.reason ?? null,
|
|
113
|
+
rolledBack: Boolean(result.rolledBack),
|
|
114
|
+
restored: result.restored ?? null,
|
|
115
|
+
runningVersion: result.runningVersion ?? null,
|
|
116
|
+
versionConfirmed: Boolean(result.versionConfirmed),
|
|
117
|
+
serviceInstalled: Boolean(result.serviceInstalled),
|
|
118
|
+
refreshedCopy: Boolean(result.refreshedCopy),
|
|
119
|
+
},
|
|
120
|
+
{ dataDir }
|
|
121
|
+
);
|
|
122
|
+
|
|
123
|
+
// Back on the same port, on whatever code is on disk now — which is the new
|
|
124
|
+
// version when the update worked and the old one when it rolled back.
|
|
125
|
+
const port = dash.port || loadConfig().dashboardPort;
|
|
126
|
+
const back = await startDashboard(port, { env: { EASY_DEVOPS_DATA_DIR: dataDir } });
|
|
127
|
+
log(back.success
|
|
128
|
+
? `Dashboard restarted on port ${port}`
|
|
129
|
+
: `The dashboard did not come back on port ${port} - start it again from the Dashboard menu`);
|
|
130
|
+
|
|
131
|
+
writeStatus(
|
|
132
|
+
{
|
|
133
|
+
phase: result.ok ? 'done' : 'failed',
|
|
134
|
+
step: result.ok ? null : result.step ?? null,
|
|
135
|
+
dashboardRestarted: back.success,
|
|
136
|
+
dashboardPort: port,
|
|
137
|
+
finishedAt: Date.now(),
|
|
138
|
+
logTail: [...logTail],
|
|
139
|
+
},
|
|
140
|
+
{ dataDir }
|
|
141
|
+
);
|
|
142
|
+
|
|
143
|
+
return result.ok ? 0 : 1;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
main()
|
|
147
|
+
.then((code) => process.exit(code))
|
|
148
|
+
.catch((error) => {
|
|
149
|
+
try {
|
|
150
|
+
writeStatus(
|
|
151
|
+
{
|
|
152
|
+
phase: 'failed',
|
|
153
|
+
step: 'worker',
|
|
154
|
+
reason: error?.message ?? String(error),
|
|
155
|
+
finishedAt: Date.now(),
|
|
156
|
+
logTail: [...logTail],
|
|
157
|
+
},
|
|
158
|
+
{ dataDir }
|
|
159
|
+
);
|
|
160
|
+
} catch { /* the file may be unwritable; nothing else to do */ }
|
|
161
|
+
process.stderr.write(`update worker failed: ${error?.stack ?? error}\n`);
|
|
162
|
+
process.exit(1);
|
|
163
|
+
});
|