easy-devops 1.2.1 → 1.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +97 -6
- package/dist/ui/assets/index-CMRzkIKj.css +1 -0
- package/dist/ui/assets/index-DEouL3IK.js +452 -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 +222 -47
- 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 +80 -61
- 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 +288 -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 +10 -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,973 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* core/deploy/engine.js
|
|
3
|
+
*
|
|
4
|
+
* Puts a project on the server and keeps the previous version one step away.
|
|
5
|
+
*
|
|
6
|
+
* One deployment, in order:
|
|
7
|
+
*
|
|
8
|
+
* 1. fetch a fresh checkout into releases/<id> — never over the live one
|
|
9
|
+
* 2. settings ez.json + package.json + the app's overrides (ezfile.js)
|
|
10
|
+
* 3. pre hook → install → build all in the new directory
|
|
11
|
+
* 4. switch stop the old process, start the new one under PM2
|
|
12
|
+
* 5. health the app must answer (or stay up) before it counts
|
|
13
|
+
* 6. post hook, save the PM2 list, prune old releases
|
|
14
|
+
*
|
|
15
|
+
* Nothing before step 4 touches what is running, so a broken build costs
|
|
16
|
+
* nothing. If step 4 or 5 fails and there is a previous release, it is started
|
|
17
|
+
* again — the same files that were running before, not a rebuild.
|
|
18
|
+
*
|
|
19
|
+
* ── What this does not do ────────────────────────────────────────────────────
|
|
20
|
+
*
|
|
21
|
+
* The switch is a restart, not a zero-downtime handover: the old process is
|
|
22
|
+
* stopped before the new one starts, because they want the same port. The gap
|
|
23
|
+
* is the app's start-up time. Running both behind nginx and switching the
|
|
24
|
+
* upstream is the way to remove it, and is a later piece of work.
|
|
25
|
+
*
|
|
26
|
+
* ── Rules it keeps ───────────────────────────────────────────────────────────
|
|
27
|
+
*
|
|
28
|
+
* - Every subprocess goes through core/shell.js; every PM2 call through
|
|
29
|
+
* core/runner.js. Both are injectable, so the whole pipeline is tested
|
|
30
|
+
* without PM2 and without a network.
|
|
31
|
+
* - An app record is trusted to be shell-safe: core/deploy/apps.js validated
|
|
32
|
+
* the id, the URL and the branch on the way in. A ref supplied at
|
|
33
|
+
* deploy time (the commit a hook names) is validated here.
|
|
34
|
+
* - One deployment per app at a time, enforced by a lock file, because the
|
|
35
|
+
* dashboard and the background service are different processes.
|
|
36
|
+
*/
|
|
37
|
+
|
|
38
|
+
import fs from 'fs';
|
|
39
|
+
import http from 'http';
|
|
40
|
+
import path from 'path';
|
|
41
|
+
|
|
42
|
+
import { run as defaultRun, runCommandLine as defaultRunCommandLine } from '../shell.js';
|
|
43
|
+
import * as defaultRunner from '../runner.js';
|
|
44
|
+
import { createDeployEvent } from '../events.js';
|
|
45
|
+
import { sendNotification as defaultSendNotification } from '../notifier.js';
|
|
46
|
+
import { publish } from '../jobs/bus.js';
|
|
47
|
+
|
|
48
|
+
import { getApp, deleteApp, pm2Name, AUTH_TYPES, deployKeyPath, validateRef, setAppDir } from './apps.js';
|
|
49
|
+
import { deleteLinkedDomain, syncLinkedDomain } from './domainLink.js';
|
|
50
|
+
import {
|
|
51
|
+
EZ_FILE_LOCATIONS, parseEzFile, resolveSettings, parseStartCommand,
|
|
52
|
+
} from './ezfile.js';
|
|
53
|
+
import { LAUNCHER_FILE, LAUNCHER_SOURCE, LAUNCHER_COMMAND_ENV } from './launcher.js';
|
|
54
|
+
import {
|
|
55
|
+
acquireLock, appPaths, ensureAppDirs, listReleases, logFile, newReleaseId, pathsIn, pruneReleases, pruneRuns,
|
|
56
|
+
readRun, readState, releaseDir, validateDeployDir, writeRun, writeState,
|
|
57
|
+
} from './store.js';
|
|
58
|
+
|
|
59
|
+
/** The PM2 config written into every release. Its presence is what makes a release startable. */
|
|
60
|
+
export const PM2_CONFIG_FILE = 'ez.pm2.json';
|
|
61
|
+
|
|
62
|
+
const GIT_TIMEOUT_MS = 5 * 60 * 1000;
|
|
63
|
+
/** Installs and builds are slow on small servers; a tight limit only produces false failures. */
|
|
64
|
+
const STEP_TIMEOUT_MS = 20 * 60 * 1000;
|
|
65
|
+
const HEALTH_POLL_MS = 2000;
|
|
66
|
+
/** Without a port there is nothing to ask, so the app only has to stay up this long. */
|
|
67
|
+
const NO_PORT_SETTLE_MS = 8000;
|
|
68
|
+
const ROLLBACK_HEALTH_TIMEOUT_S = 30;
|
|
69
|
+
|
|
70
|
+
export const RUN_STATUS = Object.freeze({
|
|
71
|
+
RUNNING: 'running',
|
|
72
|
+
SUCCESS: 'success',
|
|
73
|
+
FAILED: 'failed',
|
|
74
|
+
ROLLED_BACK: 'rolled_back',
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
/** A failure with the step it happened in, so the run record can say where. */
|
|
78
|
+
class StepError extends Error {
|
|
79
|
+
constructor(step, message) {
|
|
80
|
+
super(message);
|
|
81
|
+
this.step = step;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const sleepFor = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
86
|
+
|
|
87
|
+
// ─── Health ───────────────────────────────────────────────────────────────────
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* One GET against the app on loopback.
|
|
91
|
+
* @returns {Promise<{ ok: boolean, status: number|null, error: string|null }>}
|
|
92
|
+
*/
|
|
93
|
+
export function probeHttp(port, healthPath, timeoutMs = 5000) {
|
|
94
|
+
return new Promise((resolve) => {
|
|
95
|
+
const req = http.request(
|
|
96
|
+
{ host: '127.0.0.1', port, path: healthPath, method: 'GET', timeout: timeoutMs },
|
|
97
|
+
(res) => {
|
|
98
|
+
res.resume();
|
|
99
|
+
resolve({ ok: true, status: res.statusCode ?? null, error: null });
|
|
100
|
+
},
|
|
101
|
+
);
|
|
102
|
+
req.on('timeout', () => { req.destroy(); resolve({ ok: false, status: null, error: 'timed out' }); });
|
|
103
|
+
req.on('error', (err) => resolve({ ok: false, status: null, error: err.code || err.message }));
|
|
104
|
+
req.end();
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Whether an HTTP status counts as healthy.
|
|
110
|
+
*
|
|
111
|
+
* With an explicit health path the app has said "this URL means I am well", so
|
|
112
|
+
* only 2xx/3xx will do. With the default `/`, plenty of healthy APIs answer 401
|
|
113
|
+
* or 404 there; any response that is not a server error shows the process is
|
|
114
|
+
* up and serving.
|
|
115
|
+
*/
|
|
116
|
+
export function isHealthyStatus(status, strict) {
|
|
117
|
+
if (typeof status !== 'number') return false;
|
|
118
|
+
return strict ? status >= 200 && status < 400 : status >= 100 && status < 500;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// ─── The engine ───────────────────────────────────────────────────────────────
|
|
122
|
+
|
|
123
|
+
function resolveDeps(deps = {}) {
|
|
124
|
+
return {
|
|
125
|
+
run: deps.run ?? defaultRun,
|
|
126
|
+
runCommandLine: deps.runCommandLine ?? defaultRunCommandLine,
|
|
127
|
+
runner: deps.runner ?? defaultRunner,
|
|
128
|
+
probe: deps.probe ?? probeHttp,
|
|
129
|
+
sleep: deps.sleep ?? sleepFor,
|
|
130
|
+
sendNotification: deps.sendNotification ?? defaultSendNotification,
|
|
131
|
+
syncDomain: deps.syncDomain ?? syncLinkedDomain,
|
|
132
|
+
deleteDomain: deps.deleteDomain ?? deleteLinkedDomain,
|
|
133
|
+
now: deps.now ?? (() => Date.now()),
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** Appends timestamped lines to a run's log. Never throws: a full disk must not mask the real error. */
|
|
138
|
+
function makeLogger(appId, runId) {
|
|
139
|
+
const file = logFile(appId, runId);
|
|
140
|
+
let partial = '';
|
|
141
|
+
|
|
142
|
+
const write = (text) => {
|
|
143
|
+
try { fs.appendFileSync(file, text); } catch { /* ignore */ }
|
|
144
|
+
};
|
|
145
|
+
const stamp = () => new Date().toISOString().slice(11, 19);
|
|
146
|
+
|
|
147
|
+
return {
|
|
148
|
+
file,
|
|
149
|
+
/** A line written by the engine itself. */
|
|
150
|
+
line(text = '') {
|
|
151
|
+
write(`${stamp()} ${text}\n`);
|
|
152
|
+
},
|
|
153
|
+
/** A heading for a step. */
|
|
154
|
+
step(title) {
|
|
155
|
+
write(`\n${stamp()} ── ${title}\n`);
|
|
156
|
+
},
|
|
157
|
+
/** Raw output from a command, line-buffered so chunks do not interleave mid-line. */
|
|
158
|
+
output(chunk) {
|
|
159
|
+
partial += String(chunk).replace(/\r\n/g, '\n').replace(/\r/g, '\n');
|
|
160
|
+
const lines = partial.split('\n');
|
|
161
|
+
partial = lines.pop() ?? '';
|
|
162
|
+
if (lines.length > 0) write(lines.map((l) => ` ${l}`).join('\n') + '\n');
|
|
163
|
+
},
|
|
164
|
+
flush() {
|
|
165
|
+
if (partial) { write(` ${partial}\n`); partial = ''; }
|
|
166
|
+
},
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** Double-quotes a path for a shell command. Paths come from the data directory, never from user input. */
|
|
171
|
+
function q(p) {
|
|
172
|
+
return `"${String(p)}"`;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* The git invocation prefix and environment for an app's credentials.
|
|
177
|
+
*
|
|
178
|
+
* A token is passed as a per-command HTTP header rather than embedded in the
|
|
179
|
+
* remote URL: in the URL it would be written into the checkout's .git/config
|
|
180
|
+
* and stay on disk in every release. `display` is what goes in the log.
|
|
181
|
+
*/
|
|
182
|
+
function gitAuth(app) {
|
|
183
|
+
const env = { GIT_TERMINAL_PROMPT: '0' };
|
|
184
|
+
|
|
185
|
+
if (app.auth?.type === AUTH_TYPES.TOKEN && app.auth.token) {
|
|
186
|
+
const basic = Buffer.from(`x-access-token:${app.auth.token}`).toString('base64');
|
|
187
|
+
return {
|
|
188
|
+
env,
|
|
189
|
+
flags: `-c "http.extraHeader=Authorization: Basic ${basic}"`,
|
|
190
|
+
display: '-c "http.extraHeader=Authorization: Basic ***"',
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
if (app.auth?.type === AUTH_TYPES.DEPLOY_KEY) {
|
|
195
|
+
const key = deployKeyPath(app.id);
|
|
196
|
+
if (!key) throw new StepError('fetch', 'This app uses a deploy key, but none has been generated yet.');
|
|
197
|
+
// Forward slashes: git runs this through its own sh, where a backslash in
|
|
198
|
+
// a Windows path would be read as an escape.
|
|
199
|
+
const keyArg = key.replace(/\\/g, '/');
|
|
200
|
+
env.GIT_SSH_COMMAND =
|
|
201
|
+
`ssh -i "${keyArg}" -o IdentitiesOnly=yes -o BatchMode=yes -o StrictHostKeyChecking=accept-new`;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
return { env, flags: '', display: '' };
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
function readTextIfExists(file) {
|
|
208
|
+
try { return fs.readFileSync(file, 'utf8'); } catch { return null; }
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/** Reads ez.json, package.json and the lockfiles out of a checkout and resolves the settings. */
|
|
212
|
+
export function loadProjectSettings(dir, overrides = {}) {
|
|
213
|
+
const warnings = [];
|
|
214
|
+
let ez = null;
|
|
215
|
+
let ezLocation = null;
|
|
216
|
+
|
|
217
|
+
for (const rel of EZ_FILE_LOCATIONS) {
|
|
218
|
+
const text = readTextIfExists(path.join(dir, rel));
|
|
219
|
+
if (text === null) continue;
|
|
220
|
+
const parsed = parseEzFile(text);
|
|
221
|
+
warnings.push(...parsed.warnings);
|
|
222
|
+
if (!parsed.ok) return { ok: false, error: parsed.errors.join(' '), warnings, ezLocation: rel };
|
|
223
|
+
ez = parsed.value;
|
|
224
|
+
ezLocation = rel;
|
|
225
|
+
break;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
let pkg = null;
|
|
229
|
+
const pkgText = readTextIfExists(path.join(dir, 'package.json'));
|
|
230
|
+
if (pkgText !== null) {
|
|
231
|
+
try { pkg = JSON.parse(pkgText); } catch { warnings.push('package.json is not valid JSON and was ignored'); }
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
const files = {
|
|
235
|
+
packageLock: fs.existsSync(path.join(dir, 'package-lock.json')),
|
|
236
|
+
yarnLock: fs.existsSync(path.join(dir, 'yarn.lock')),
|
|
237
|
+
pnpmLock: fs.existsSync(path.join(dir, 'pnpm-lock.yaml')),
|
|
238
|
+
};
|
|
239
|
+
|
|
240
|
+
const resolved = resolveSettings({ ez, pkg, files, overrides });
|
|
241
|
+
return { ...resolved, warnings, ezLocation };
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/** The environment a deployed app (and its build) runs with. */
|
|
245
|
+
function appEnvironment(app, settings, releaseId) {
|
|
246
|
+
const env = { ...(app.env ?? {}) };
|
|
247
|
+
// Most frameworks read PORT. Setting it from the declared port means the
|
|
248
|
+
// port in ez.json is the port the app actually listens on.
|
|
249
|
+
if (settings.port && env.PORT === undefined) env.PORT = String(settings.port);
|
|
250
|
+
env.EZ_APP = app.id;
|
|
251
|
+
env.EZ_RELEASE = releaseId;
|
|
252
|
+
return env;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/** Writes the PM2 config (and the launcher, when needed) into a release directory. */
|
|
256
|
+
function writePm2Config(dir, app, settings, env) {
|
|
257
|
+
const start = parseStartCommand(settings.start);
|
|
258
|
+
const entry = {
|
|
259
|
+
name: pm2Name(app.id),
|
|
260
|
+
cwd: dir,
|
|
261
|
+
autorestart: true,
|
|
262
|
+
max_restarts: 10,
|
|
263
|
+
min_uptime: 5000,
|
|
264
|
+
time: true,
|
|
265
|
+
env: { ...env },
|
|
266
|
+
};
|
|
267
|
+
|
|
268
|
+
if (start.kind === 'node') {
|
|
269
|
+
entry.script = start.script;
|
|
270
|
+
if (start.args.length > 0) entry.args = start.args;
|
|
271
|
+
} else {
|
|
272
|
+
fs.writeFileSync(path.join(dir, LAUNCHER_FILE), LAUNCHER_SOURCE);
|
|
273
|
+
entry.script = LAUNCHER_FILE;
|
|
274
|
+
entry.env[LAUNCHER_COMMAND_ENV] = start.command;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
const file = path.join(dir, PM2_CONFIG_FILE);
|
|
278
|
+
// Holds the app's environment, so owner-only like the deploy key.
|
|
279
|
+
fs.writeFileSync(file, JSON.stringify({ apps: [entry] }, null, 2), { mode: 0o600 });
|
|
280
|
+
try { fs.chmodSync(file, 0o600); } catch { /* not meaningful on Windows */ }
|
|
281
|
+
return file;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/** Reads back the health settings a release was started with, for a rollback. */
|
|
285
|
+
function releaseHealth(dir) {
|
|
286
|
+
try {
|
|
287
|
+
const meta = JSON.parse(fs.readFileSync(path.join(dir, 'ez.release.json'), 'utf8'));
|
|
288
|
+
return {
|
|
289
|
+
port: Number.isInteger(meta.port) ? meta.port : null,
|
|
290
|
+
healthPath: typeof meta.healthPath === 'string' ? meta.healthPath : '/',
|
|
291
|
+
strict: Boolean(meta.strict),
|
|
292
|
+
sha: typeof meta.sha === 'string' ? meta.sha : null,
|
|
293
|
+
ref: typeof meta.ref === 'string' ? meta.ref : null,
|
|
294
|
+
};
|
|
295
|
+
} catch {
|
|
296
|
+
return { port: null, healthPath: '/', strict: false, sha: null, ref: null };
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
/** What a release was built from, for the UI's release list. */
|
|
301
|
+
export function describeRelease(appId, releaseId) {
|
|
302
|
+
let dir;
|
|
303
|
+
try { dir = releaseDir(appId, releaseId); } catch { return null; }
|
|
304
|
+
const meta = releaseHealth(dir);
|
|
305
|
+
return {
|
|
306
|
+
id: releaseId,
|
|
307
|
+
sha: meta.sha,
|
|
308
|
+
ref: meta.ref,
|
|
309
|
+
startable: fs.existsSync(path.join(dir, PM2_CONFIG_FILE)),
|
|
310
|
+
};
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
/**
|
|
314
|
+
* Waits for a freshly started app to be healthy.
|
|
315
|
+
* @returns {Promise<{ ok: boolean, detail: string }>}
|
|
316
|
+
*/
|
|
317
|
+
async function waitHealthy({ name, port, healthPath, strict, timeoutS, pm2Path, d }) {
|
|
318
|
+
const status = async () => {
|
|
319
|
+
const listed = await d.runner.list({ run: d.run, pm2Path });
|
|
320
|
+
if (!listed.ok) return null;
|
|
321
|
+
return listed.apps.find((a) => a.name === name) ?? null;
|
|
322
|
+
};
|
|
323
|
+
|
|
324
|
+
if (!port) {
|
|
325
|
+
// Nothing to ask over HTTP: the app has to stay online, without PM2
|
|
326
|
+
// restarting it, for a few seconds.
|
|
327
|
+
await d.sleep(Math.min(2000, NO_PORT_SETTLE_MS));
|
|
328
|
+
const early = await status();
|
|
329
|
+
await d.sleep(NO_PORT_SETTLE_MS - 2000);
|
|
330
|
+
const late = await status();
|
|
331
|
+
if (!late) return { ok: false, detail: 'PM2 no longer lists the process.' };
|
|
332
|
+
if (late.status !== 'online') return { ok: false, detail: `The process is "${late.status}", not online.` };
|
|
333
|
+
if (early && late.restarts > early.restarts) {
|
|
334
|
+
return { ok: false, detail: 'The process keeps restarting — it starts and then exits.' };
|
|
335
|
+
}
|
|
336
|
+
return { ok: true, detail: 'The process stayed online (no port is configured, so no HTTP check was made).' };
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
const deadline = d.now() + timeoutS * 1000;
|
|
340
|
+
let last = 'no response yet';
|
|
341
|
+
|
|
342
|
+
for (;;) {
|
|
343
|
+
const res = await d.probe(port, healthPath);
|
|
344
|
+
if (res.ok && isHealthyStatus(res.status, strict)) {
|
|
345
|
+
return { ok: true, detail: `GET ${healthPath} on port ${port} answered ${res.status}.` };
|
|
346
|
+
}
|
|
347
|
+
last = res.ok ? `it answered ${res.status}` : `no answer (${res.error})`;
|
|
348
|
+
|
|
349
|
+
// A process PM2 has given up on will never become healthy; stop waiting.
|
|
350
|
+
const app = await status();
|
|
351
|
+
if (app && (app.status === 'errored' || app.status === 'stopped')) {
|
|
352
|
+
return { ok: false, detail: `The process is "${app.status}" — it exited instead of listening on port ${port}.` };
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
if (d.now() >= deadline) break;
|
|
356
|
+
await d.sleep(HEALTH_POLL_MS);
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
return {
|
|
360
|
+
ok: false,
|
|
361
|
+
detail: `No healthy response from GET ${healthPath} on port ${port} within ${timeoutS}s (${last}).`,
|
|
362
|
+
};
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
/** Stops whatever is running under the app's PM2 name and starts a release. */
|
|
366
|
+
async function switchTo(dir, name, pm2Path, d, log) {
|
|
367
|
+
const config = path.join(dir, PM2_CONFIG_FILE);
|
|
368
|
+
if (!fs.existsSync(config)) return { ok: false, output: `${PM2_CONFIG_FILE} is missing from ${dir}.` };
|
|
369
|
+
|
|
370
|
+
// `delete` rather than `restart`: the working directory and the environment
|
|
371
|
+
// belong to the release, and PM2 keeps the old ones across a restart.
|
|
372
|
+
// Deleting a name PM2 does not know fails harmlessly on a first deployment.
|
|
373
|
+
await d.runner.del(name, { run: d.run, pm2Path });
|
|
374
|
+
const started = await d.runner.startConfig(config, { run: d.run, pm2Path });
|
|
375
|
+
if (!started.ok) log.line(started.output || 'pm2 start failed with no output');
|
|
376
|
+
return started;
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
async function notify(app, run, d) {
|
|
380
|
+
const event = createDeployEvent(app.id, run.status, {
|
|
381
|
+
sha: run.sha,
|
|
382
|
+
reason: run.error,
|
|
383
|
+
rollback: run.trigger === 'rollback',
|
|
384
|
+
});
|
|
385
|
+
// The in-app bell, when this process is the one feeding an open dashboard.
|
|
386
|
+
publish('notification:new', event);
|
|
387
|
+
try {
|
|
388
|
+
await d.sendNotification(event, app.notifyChannelIds ?? []);
|
|
389
|
+
} catch (err) {
|
|
390
|
+
console.error('[deploy] notification failed:', err.message);
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
/**
|
|
395
|
+
* Starts a deployment and returns as soon as it is under way.
|
|
396
|
+
*
|
|
397
|
+
* The work continues in the background; `done` resolves with the finished run
|
|
398
|
+
* record (it never rejects), for callers that want to wait — tests, and the
|
|
399
|
+
* CLI.
|
|
400
|
+
*
|
|
401
|
+
* @param {string} appId
|
|
402
|
+
* @param {Object} [opts]
|
|
403
|
+
* @param {'manual'|'webhook'|'hub'} [opts.trigger]
|
|
404
|
+
* @param {string|null} [opts.ref] Commit, tag or branch to deploy instead of the app's branch
|
|
405
|
+
* @param {string|null} [opts.actor] Who asked, for the record
|
|
406
|
+
* @param {Object} [opts.deps] Injected collaborators (testing only)
|
|
407
|
+
* @returns {{ ok: true, runId: string, done: Promise<Object> }
|
|
408
|
+
* | { ok: false, status: number, error: string, runId?: string|null }}
|
|
409
|
+
*/
|
|
410
|
+
export function startDeployment(appId, { trigger = 'manual', ref = null, actor = null, deps = {} } = {}) {
|
|
411
|
+
const app = getApp(appId);
|
|
412
|
+
if (!app) return { ok: false, status: 404, error: `No app named "${appId}".` };
|
|
413
|
+
|
|
414
|
+
if (ref !== null && ref !== undefined && ref !== '') {
|
|
415
|
+
const refError = validateRef(String(ref));
|
|
416
|
+
if (refError) return { ok: false, status: 400, error: refError };
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
const d = resolveDeps(deps);
|
|
420
|
+
const runId = newReleaseId(new Date(d.now()));
|
|
421
|
+
|
|
422
|
+
const lock = acquireLock(appId, { runId });
|
|
423
|
+
if (!lock.ok) return { ok: false, status: 409, error: lock.reason, runId: lock.runId };
|
|
424
|
+
|
|
425
|
+
const run = {
|
|
426
|
+
id: runId,
|
|
427
|
+
appId,
|
|
428
|
+
trigger,
|
|
429
|
+
actor,
|
|
430
|
+
ref: ref ? String(ref) : app.branch,
|
|
431
|
+
sha: null,
|
|
432
|
+
releaseId: runId,
|
|
433
|
+
status: RUN_STATUS.RUNNING,
|
|
434
|
+
step: 'fetch',
|
|
435
|
+
startedAt: d.now(),
|
|
436
|
+
finishedAt: null,
|
|
437
|
+
error: null,
|
|
438
|
+
warnings: [],
|
|
439
|
+
rolledBackTo: null,
|
|
440
|
+
};
|
|
441
|
+
writeRun(run);
|
|
442
|
+
|
|
443
|
+
const done = executeDeployment(app, run, d)
|
|
444
|
+
.catch((err) => {
|
|
445
|
+
// executeDeployment handles its own failures; this is the net under it.
|
|
446
|
+
run.status = RUN_STATUS.FAILED;
|
|
447
|
+
run.error = err?.message ?? String(err);
|
|
448
|
+
run.finishedAt = d.now();
|
|
449
|
+
try { writeRun(run); } catch { /* ignore */ }
|
|
450
|
+
return run;
|
|
451
|
+
})
|
|
452
|
+
.finally(() => lock.release());
|
|
453
|
+
|
|
454
|
+
return { ok: true, runId, done };
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
async function executeDeployment(app, run, d) {
|
|
458
|
+
const log = makeLogger(app.id, run.id);
|
|
459
|
+
const name = pm2Name(app.id);
|
|
460
|
+
const dir = releaseDir(app.id, run.releaseId);
|
|
461
|
+
const before = readState(app.id);
|
|
462
|
+
|
|
463
|
+
let switched = false;
|
|
464
|
+
let pm2Path = 'pm2';
|
|
465
|
+
|
|
466
|
+
const setStep = (step, title) => {
|
|
467
|
+
run.step = step;
|
|
468
|
+
writeRun(run);
|
|
469
|
+
log.step(title);
|
|
470
|
+
};
|
|
471
|
+
|
|
472
|
+
/** Runs one of the engine's own commands (git) through the shared shell. */
|
|
473
|
+
const sh = async (step, command, display, options = {}) => {
|
|
474
|
+
log.line(`$ ${display ?? command}`);
|
|
475
|
+
const r = await d.run(command, { timeout: GIT_TIMEOUT_MS, onData: (c) => log.output(c), ...options });
|
|
476
|
+
log.flush();
|
|
477
|
+
if (!r.success) {
|
|
478
|
+
const tail = (r.stderr || r.stdout || '').split('\n').filter(Boolean).slice(-1)[0] ?? '';
|
|
479
|
+
throw new StepError(step, tail ? `${step} failed: ${tail}` : `${step} failed (exit ${r.exitCode ?? 'none'})`);
|
|
480
|
+
}
|
|
481
|
+
return r;
|
|
482
|
+
};
|
|
483
|
+
|
|
484
|
+
/** Runs the user's commands for one step, in the release directory. */
|
|
485
|
+
const userCommands = async (step, title, commands, env) => {
|
|
486
|
+
if (!commands || commands.length === 0) return;
|
|
487
|
+
setStep(step, title);
|
|
488
|
+
for (const command of commands) {
|
|
489
|
+
log.line(`$ ${command}`);
|
|
490
|
+
const r = await d.runCommandLine(command, {
|
|
491
|
+
cwd: dir, env, timeout: STEP_TIMEOUT_MS, onData: (c) => log.output(c),
|
|
492
|
+
});
|
|
493
|
+
log.flush();
|
|
494
|
+
if (!r.success) {
|
|
495
|
+
const why = r.exitCode === null ? (r.stderr || 'it was stopped') : `exit code ${r.exitCode}`;
|
|
496
|
+
throw new StepError(step, `${title} failed: "${command}" — ${why}`);
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
};
|
|
500
|
+
|
|
501
|
+
try {
|
|
502
|
+
log.line(`Deployment ${run.id} of ${app.id} (${run.trigger}${run.actor ? `, by ${run.actor}` : ''})`);
|
|
503
|
+
|
|
504
|
+
// ── 1. fetch ────────────────────────────────────────────────────────────
|
|
505
|
+
setStep('fetch', `Fetching ${app.repoUrl} @ ${run.ref}`);
|
|
506
|
+
ensureAppDirs(app.id);
|
|
507
|
+
|
|
508
|
+
const gitCheck = await d.run('git --version', { timeout: 15000 });
|
|
509
|
+
if (!gitCheck.success) {
|
|
510
|
+
throw new StepError('fetch', 'git is not installed on this server (or not on PATH for the Easy DevOps service).');
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
const auth = gitAuth(app);
|
|
514
|
+
const gitOpts = { env: auth.env };
|
|
515
|
+
const flags = auth.flags ? ` ${auth.flags}` : '';
|
|
516
|
+
const shown = auth.display ? ` ${auth.display}` : '';
|
|
517
|
+
|
|
518
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
519
|
+
await sh('fetch', `git init -q ${q(dir)}`, null, gitOpts);
|
|
520
|
+
await sh('fetch', `git -C ${q(dir)} remote add origin ${q(app.repoUrl)}`, null, gitOpts);
|
|
521
|
+
await sh(
|
|
522
|
+
'fetch',
|
|
523
|
+
`git -C ${q(dir)}${flags} fetch --depth 1 origin ${q(run.ref)}`,
|
|
524
|
+
`git -C ${q(dir)}${shown} fetch --depth 1 origin ${q(run.ref)}`,
|
|
525
|
+
gitOpts,
|
|
526
|
+
);
|
|
527
|
+
await sh('fetch', `git -C ${q(dir)} checkout -q FETCH_HEAD`, null, gitOpts);
|
|
528
|
+
const head = await sh('fetch', `git -C ${q(dir)} rev-parse HEAD`, null, gitOpts);
|
|
529
|
+
run.sha = head.stdout.trim().split(/\s+/).pop() || null;
|
|
530
|
+
writeRun(run);
|
|
531
|
+
|
|
532
|
+
// ── 2. settings ─────────────────────────────────────────────────────────
|
|
533
|
+
setStep('settings', 'Reading project settings');
|
|
534
|
+
const project = loadProjectSettings(dir, app.settings ?? {});
|
|
535
|
+
for (const w of project.warnings ?? []) { log.line(`warning: ${w}`); run.warnings.push(w); }
|
|
536
|
+
if (!project.ok) throw new StepError('settings', project.error);
|
|
537
|
+
|
|
538
|
+
const { settings, sources } = project;
|
|
539
|
+
log.line(project.ezLocation ? `ez.json found at ${project.ezLocation}` : 'no ez.json — using package.json defaults');
|
|
540
|
+
for (const field of ['install', 'build', 'start', 'port', 'healthPath']) {
|
|
541
|
+
const value = Array.isArray(settings[field]) ? (settings[field].join(' && ') || '(none)') : (settings[field] ?? '(none)');
|
|
542
|
+
log.line(` ${field.padEnd(10)} ${value} [${sources[field]}]`);
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
const env = appEnvironment(app, settings, run.releaseId);
|
|
546
|
+
const strict = sources.healthPath !== 'default';
|
|
547
|
+
|
|
548
|
+
// ── 3. pre → install → build ────────────────────────────────────────────
|
|
549
|
+
await userCommands('pre', 'Pre-deploy hook', settings.pre, env);
|
|
550
|
+
await userCommands('install', 'Install', settings.install, env);
|
|
551
|
+
await userCommands('build', 'Build', settings.build, env);
|
|
552
|
+
|
|
553
|
+
// ── 4. switch ───────────────────────────────────────────────────────────
|
|
554
|
+
setStep('start', 'Starting the new release');
|
|
555
|
+
|
|
556
|
+
const pm2 = await d.runner.detect({ run: d.run, probeVersion: false });
|
|
557
|
+
if (!pm2.installed) {
|
|
558
|
+
throw new StepError('start', 'PM2 is not installed. Install it from the Processes page, or run: npm install -g pm2');
|
|
559
|
+
}
|
|
560
|
+
pm2Path = pm2.path ?? 'pm2';
|
|
561
|
+
|
|
562
|
+
writePm2Config(dir, app, settings, env);
|
|
563
|
+
fs.writeFileSync(path.join(dir, 'ez.release.json'), JSON.stringify({
|
|
564
|
+
sha: run.sha, ref: run.ref, port: settings.port, healthPath: settings.healthPath, strict,
|
|
565
|
+
deployedAt: new Date(d.now()).toISOString(),
|
|
566
|
+
}, null, 2));
|
|
567
|
+
|
|
568
|
+
log.line(`$ pm2 delete ${name}; pm2 start ${PM2_CONFIG_FILE}`);
|
|
569
|
+
switched = true;
|
|
570
|
+
const started = await switchTo(dir, name, pm2Path, d, log);
|
|
571
|
+
if (!started.ok) throw new StepError('start', 'PM2 could not start the new release.');
|
|
572
|
+
|
|
573
|
+
// ── 5. health ───────────────────────────────────────────────────────────
|
|
574
|
+
setStep('health', 'Checking that the app is healthy');
|
|
575
|
+
const health = await waitHealthy({
|
|
576
|
+
name, port: settings.port, healthPath: settings.healthPath, strict,
|
|
577
|
+
timeoutS: settings.healthTimeout, pm2Path, d,
|
|
578
|
+
});
|
|
579
|
+
log.line(health.detail);
|
|
580
|
+
if (!health.ok) throw new StepError('health', health.detail);
|
|
581
|
+
|
|
582
|
+
// ── 6. it is live ───────────────────────────────────────────────────────
|
|
583
|
+
writeState(app.id, { current: run.releaseId, previous: before.current });
|
|
584
|
+
|
|
585
|
+
// A linked domain follows the app's port.
|
|
586
|
+
const domainNote = await d.syncDomain(app, settings.port);
|
|
587
|
+
if (domainNote) {
|
|
588
|
+
log.line(domainNote);
|
|
589
|
+
if (domainNote.startsWith('warning:')) run.warnings.push(domainNote.replace(/^warning:\s*/, ''));
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
try {
|
|
593
|
+
await userCommands('post', 'Post-deploy hook', settings.post, env);
|
|
594
|
+
} catch (err) {
|
|
595
|
+
// The new release is live and healthy. A failing post hook is worth
|
|
596
|
+
// saying loudly, but undoing a good deployment for it is not.
|
|
597
|
+
const warning = `The post-deploy hook failed, but the release is live: ${err.message}`;
|
|
598
|
+
log.line(`warning: ${warning}`);
|
|
599
|
+
run.warnings.push(warning);
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
setStep('finish', 'Finishing');
|
|
603
|
+
const saved = await d.runner.save({ run: d.run, pm2Path });
|
|
604
|
+
if (!saved.ok) log.line('warning: pm2 save failed — the app may not come back after a reboot.');
|
|
605
|
+
|
|
606
|
+
const removed = pruneReleases(app.id, {
|
|
607
|
+
keep: app.keepReleases, protect: [run.releaseId, before.current],
|
|
608
|
+
});
|
|
609
|
+
if (removed.length > 0) log.line(`Removed ${removed.length} old release(s).`);
|
|
610
|
+
|
|
611
|
+
run.status = RUN_STATUS.SUCCESS;
|
|
612
|
+
log.line('Deployment succeeded.');
|
|
613
|
+
} catch (err) {
|
|
614
|
+
log.flush();
|
|
615
|
+
run.error = err?.message ?? String(err);
|
|
616
|
+
run.step = err?.step ?? run.step;
|
|
617
|
+
log.line(`ERROR: ${run.error}`);
|
|
618
|
+
run.status = RUN_STATUS.FAILED;
|
|
619
|
+
|
|
620
|
+
if (!switched) {
|
|
621
|
+
// Nothing that was running has been touched. The half-built directory
|
|
622
|
+
// is of no use to a rollback and can be large; the log is what explains
|
|
623
|
+
// the failure.
|
|
624
|
+
try { fs.rmSync(dir, { recursive: true, force: true }); } catch { /* ignore */ }
|
|
625
|
+
log.line('The running release was not touched.');
|
|
626
|
+
} else if (before.current && app.autoRollback !== false) {
|
|
627
|
+
log.step(`Rolling back to ${before.current}`);
|
|
628
|
+
let restored = false;
|
|
629
|
+
try {
|
|
630
|
+
const previousDir = releaseDir(app.id, before.current);
|
|
631
|
+
const back = await switchTo(previousDir, name, pm2Path, d, log);
|
|
632
|
+
if (back.ok) {
|
|
633
|
+
const meta = releaseHealth(previousDir);
|
|
634
|
+
const health = await waitHealthy({
|
|
635
|
+
name, port: meta.port, healthPath: meta.healthPath, strict: meta.strict,
|
|
636
|
+
timeoutS: ROLLBACK_HEALTH_TIMEOUT_S, pm2Path, d,
|
|
637
|
+
});
|
|
638
|
+
log.line(health.detail);
|
|
639
|
+
restored = health.ok;
|
|
640
|
+
}
|
|
641
|
+
} catch (rollbackErr) {
|
|
642
|
+
log.line(`Rollback error: ${rollbackErr.message}`);
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
if (restored) {
|
|
646
|
+
run.status = RUN_STATUS.ROLLED_BACK;
|
|
647
|
+
run.rolledBackTo = before.current;
|
|
648
|
+
log.line(`The previous release (${before.current}) is running again.`);
|
|
649
|
+
} else {
|
|
650
|
+
log.line('The previous release could NOT be restored. The app may be down.');
|
|
651
|
+
run.error += ' The automatic rollback also failed — the app may be down.';
|
|
652
|
+
}
|
|
653
|
+
} else {
|
|
654
|
+
// No release to return to (a first deployment) or rollback is switched
|
|
655
|
+
// off. Remove the broken process so PM2 does not restart it in a loop.
|
|
656
|
+
try { await d.runner.del(name, { run: d.run, pm2Path }); } catch { /* ignore */ }
|
|
657
|
+
log.line(before.current
|
|
658
|
+
? 'Automatic rollback is switched off for this app; the process was stopped.'
|
|
659
|
+
: 'There is no earlier release to roll back to; the process was stopped.');
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
run.finishedAt = d.now();
|
|
664
|
+
if (run.status !== RUN_STATUS.RUNNING) run.step = run.status === RUN_STATUS.SUCCESS ? 'done' : run.step;
|
|
665
|
+
writeRun(run);
|
|
666
|
+
try { pruneRuns(app.id); } catch { /* ignore */ }
|
|
667
|
+
await notify(app, run, d);
|
|
668
|
+
return run;
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
/**
|
|
672
|
+
* Switches an app back to an earlier release, without rebuilding anything.
|
|
673
|
+
*
|
|
674
|
+
* @param {string} appId
|
|
675
|
+
* @param {Object} [opts]
|
|
676
|
+
* @param {string|null} [opts.releaseId] Defaults to the release before the live one
|
|
677
|
+
* @param {string|null} [opts.actor]
|
|
678
|
+
* @param {Object} [opts.deps]
|
|
679
|
+
* @returns {{ ok: true, runId: string, done: Promise<Object> }
|
|
680
|
+
* | { ok: false, status: number, error: string, runId?: string|null }}
|
|
681
|
+
*/
|
|
682
|
+
export function startRollback(appId, { releaseId = null, actor = null, deps = {} } = {}) {
|
|
683
|
+
const app = getApp(appId);
|
|
684
|
+
if (!app) return { ok: false, status: 404, error: `No app named "${appId}".` };
|
|
685
|
+
|
|
686
|
+
const before = readState(appId);
|
|
687
|
+
const target = releaseId ?? before.previous;
|
|
688
|
+
if (!target) return { ok: false, status: 409, error: 'There is no earlier release to roll back to.' };
|
|
689
|
+
if (target === before.current) return { ok: false, status: 409, error: 'That release is already the live one.' };
|
|
690
|
+
if (!listReleases(appId).includes(target)) {
|
|
691
|
+
return { ok: false, status: 404, error: `Release ${target} is no longer on this server.` };
|
|
692
|
+
}
|
|
693
|
+
|
|
694
|
+
let targetDir;
|
|
695
|
+
try { targetDir = releaseDir(appId, target); } catch { return { ok: false, status: 400, error: 'Invalid release id.' }; }
|
|
696
|
+
if (!fs.existsSync(path.join(targetDir, PM2_CONFIG_FILE))) {
|
|
697
|
+
return { ok: false, status: 409, error: `Release ${target} was never started, so it cannot be rolled back to.` };
|
|
698
|
+
}
|
|
699
|
+
|
|
700
|
+
const d = resolveDeps(deps);
|
|
701
|
+
const runId = newReleaseId(new Date(d.now()));
|
|
702
|
+
const lock = acquireLock(appId, { runId });
|
|
703
|
+
if (!lock.ok) return { ok: false, status: 409, error: lock.reason, runId: lock.runId };
|
|
704
|
+
|
|
705
|
+
const meta = releaseHealth(targetDir);
|
|
706
|
+
const run = {
|
|
707
|
+
id: runId,
|
|
708
|
+
appId,
|
|
709
|
+
trigger: 'rollback',
|
|
710
|
+
actor,
|
|
711
|
+
ref: meta.ref,
|
|
712
|
+
sha: meta.sha,
|
|
713
|
+
releaseId: target,
|
|
714
|
+
status: RUN_STATUS.RUNNING,
|
|
715
|
+
step: 'start',
|
|
716
|
+
startedAt: d.now(),
|
|
717
|
+
finishedAt: null,
|
|
718
|
+
error: null,
|
|
719
|
+
warnings: [],
|
|
720
|
+
rolledBackTo: null,
|
|
721
|
+
};
|
|
722
|
+
writeRun(run);
|
|
723
|
+
|
|
724
|
+
const done = (async () => {
|
|
725
|
+
const log = makeLogger(appId, runId);
|
|
726
|
+
const name = pm2Name(appId);
|
|
727
|
+
let pm2Path = 'pm2';
|
|
728
|
+
|
|
729
|
+
try {
|
|
730
|
+
log.line(`Rollback ${runId} of ${appId} to release ${target}${actor ? ` (by ${actor})` : ''}`);
|
|
731
|
+
const pm2 = await d.runner.detect({ run: d.run, probeVersion: false });
|
|
732
|
+
if (!pm2.installed) throw new StepError('start', 'PM2 is not installed.');
|
|
733
|
+
pm2Path = pm2.path ?? 'pm2';
|
|
734
|
+
|
|
735
|
+
log.step(`Starting release ${target}`);
|
|
736
|
+
const started = await switchTo(targetDir, name, pm2Path, d, log);
|
|
737
|
+
if (!started.ok) throw new StepError('start', 'PM2 could not start that release.');
|
|
738
|
+
|
|
739
|
+
run.step = 'health';
|
|
740
|
+
writeRun(run);
|
|
741
|
+
log.step('Checking that the app is healthy');
|
|
742
|
+
const health = await waitHealthy({
|
|
743
|
+
name, port: meta.port, healthPath: meta.healthPath, strict: meta.strict,
|
|
744
|
+
timeoutS: ROLLBACK_HEALTH_TIMEOUT_S, pm2Path, d,
|
|
745
|
+
});
|
|
746
|
+
log.line(health.detail);
|
|
747
|
+
if (!health.ok) throw new StepError('health', health.detail);
|
|
748
|
+
|
|
749
|
+
writeState(appId, { current: target, previous: before.current });
|
|
750
|
+
await d.runner.save({ run: d.run, pm2Path });
|
|
751
|
+
run.status = RUN_STATUS.SUCCESS;
|
|
752
|
+
run.step = 'done';
|
|
753
|
+
log.line('Rollback succeeded.');
|
|
754
|
+
} catch (err) {
|
|
755
|
+
run.status = RUN_STATUS.FAILED;
|
|
756
|
+
run.error = err?.message ?? String(err);
|
|
757
|
+
run.step = err?.step ?? run.step;
|
|
758
|
+
log.line(`ERROR: ${run.error}`);
|
|
759
|
+
|
|
760
|
+
// Put back what was running, so a failed rollback is not also an outage.
|
|
761
|
+
if (before.current) {
|
|
762
|
+
try {
|
|
763
|
+
const back = await switchTo(releaseDir(appId, before.current), name, pm2Path, d, log);
|
|
764
|
+
log.line(back.ok
|
|
765
|
+
? `Release ${before.current} was started again.`
|
|
766
|
+
: `Release ${before.current} could NOT be started again. The app may be down.`);
|
|
767
|
+
} catch (restoreErr) {
|
|
768
|
+
log.line(`Restore error: ${restoreErr.message}`);
|
|
769
|
+
}
|
|
770
|
+
}
|
|
771
|
+
}
|
|
772
|
+
|
|
773
|
+
run.finishedAt = d.now();
|
|
774
|
+
writeRun(run);
|
|
775
|
+
try { pruneRuns(appId); } catch { /* ignore */ }
|
|
776
|
+
await notify(app, run, d);
|
|
777
|
+
return run;
|
|
778
|
+
})()
|
|
779
|
+
.catch((err) => {
|
|
780
|
+
run.status = RUN_STATUS.FAILED;
|
|
781
|
+
run.error = err?.message ?? String(err);
|
|
782
|
+
run.finishedAt = d.now();
|
|
783
|
+
try { writeRun(run); } catch { /* ignore */ }
|
|
784
|
+
return run;
|
|
785
|
+
})
|
|
786
|
+
.finally(() => lock.release());
|
|
787
|
+
|
|
788
|
+
return { ok: true, runId, done };
|
|
789
|
+
}
|
|
790
|
+
|
|
791
|
+
/**
|
|
792
|
+
* Removes an app: stops its process, forgets it, deletes its files.
|
|
793
|
+
*
|
|
794
|
+
* @param {string} appId
|
|
795
|
+
* @param {Object} [opts]
|
|
796
|
+
* @param {boolean} [opts.deleteDomain] Also remove the domain linked to it
|
|
797
|
+
* @returns {Promise<{ ok: boolean, status: number, error: string|null, domainDeleted?: boolean, warning?: string|null }>}
|
|
798
|
+
*/
|
|
799
|
+
export async function removeApp(appId, { deleteDomain = false, deps = {} } = {}) {
|
|
800
|
+
const app = getApp(appId);
|
|
801
|
+
if (!app) return { ok: false, status: 404, error: `No app named "${appId}".` };
|
|
802
|
+
|
|
803
|
+
const d = resolveDeps(deps);
|
|
804
|
+
const lock = acquireLock(appId, { runId: null });
|
|
805
|
+
if (!lock.ok) return { ok: false, status: 409, error: 'A deployment of this app is running. Wait for it to finish.' };
|
|
806
|
+
|
|
807
|
+
try {
|
|
808
|
+
await stopProcess(appId, d, { save: true });
|
|
809
|
+
} catch { /* the registry entry is removed regardless */ } finally {
|
|
810
|
+
lock.release();
|
|
811
|
+
}
|
|
812
|
+
|
|
813
|
+
// The domain goes first, while the app record still says which one it is. A
|
|
814
|
+
// failure here does not keep the app: the person asked for the app to go.
|
|
815
|
+
let domainDeleted = false;
|
|
816
|
+
let warning = null;
|
|
817
|
+
if (deleteDomain && app.domain) {
|
|
818
|
+
const removed = await d.deleteDomain(app);
|
|
819
|
+
domainDeleted = removed.ok;
|
|
820
|
+
if (!removed.ok) warning = `The app was deleted, but the domain ${app.domain} was not: ${removed.error}`;
|
|
821
|
+
}
|
|
822
|
+
|
|
823
|
+
deleteApp(appId);
|
|
824
|
+
return { ok: true, status: 200, error: null, domainDeleted, warning };
|
|
825
|
+
}
|
|
826
|
+
|
|
827
|
+
/**
|
|
828
|
+
* Stops an app's PM2 process — only when PM2 exists and its daemon is up: a
|
|
829
|
+
* stop must never be the thing that starts a PM2 daemon on a machine that was
|
|
830
|
+
* not running one.
|
|
831
|
+
*
|
|
832
|
+
* @returns {Promise<{ pm2Path: string|null, wasRunning: boolean }>}
|
|
833
|
+
*/
|
|
834
|
+
async function stopProcess(appId, d, { save = false } = {}) {
|
|
835
|
+
const pm2 = await d.runner.detect({ run: d.run, probeVersion: false });
|
|
836
|
+
const daemonUp = typeof d.runner.isDaemonRunning === 'function' ? d.runner.isDaemonRunning() : true;
|
|
837
|
+
if (!pm2.installed || !daemonUp) return { pm2Path: pm2.installed ? (pm2.path ?? 'pm2') : null, wasRunning: false };
|
|
838
|
+
|
|
839
|
+
const pm2Path = pm2.path ?? 'pm2';
|
|
840
|
+
const name = pm2Name(appId);
|
|
841
|
+
let wasRunning = false;
|
|
842
|
+
try {
|
|
843
|
+
const listed = await d.runner.list({ run: d.run, pm2Path });
|
|
844
|
+
wasRunning = Boolean(listed.ok && listed.apps.some((a) => a.name === name));
|
|
845
|
+
} catch { /* treat as not running */ }
|
|
846
|
+
|
|
847
|
+
await d.runner.del(name, { run: d.run, pm2Path });
|
|
848
|
+
if (save) await d.runner.save({ run: d.run, pm2Path });
|
|
849
|
+
return { pm2Path, wasRunning };
|
|
850
|
+
}
|
|
851
|
+
|
|
852
|
+
/** True when `child` is `parent` or lies inside it. */
|
|
853
|
+
function isInside(child, parent) {
|
|
854
|
+
const rel = path.relative(path.resolve(parent), path.resolve(child));
|
|
855
|
+
return rel === '' || (!rel.startsWith('..') && !path.isAbsolute(rel));
|
|
856
|
+
}
|
|
857
|
+
|
|
858
|
+
/** Moves a directory: a rename when possible, a copy-then-delete across devices. */
|
|
859
|
+
function moveDirectory(from, to) {
|
|
860
|
+
fs.mkdirSync(path.dirname(to), { recursive: true });
|
|
861
|
+
try {
|
|
862
|
+
fs.renameSync(from, to);
|
|
863
|
+
} catch (err) {
|
|
864
|
+
// EXDEV: the destination is on another disk or mount. Copy, then remove.
|
|
865
|
+
if (err.code !== 'EXDEV') throw err;
|
|
866
|
+
fs.cpSync(from, to, { recursive: true, errorOnExist: true, force: false, preserveTimestamps: true });
|
|
867
|
+
fs.rmSync(from, { recursive: true, force: true });
|
|
868
|
+
}
|
|
869
|
+
}
|
|
870
|
+
|
|
871
|
+
/**
|
|
872
|
+
* Moves an app's folder to another place on the server.
|
|
873
|
+
*
|
|
874
|
+
* In order: stop its PM2 process (it holds the folder open, and on Windows a
|
|
875
|
+
* folder in use cannot be moved at all) → move the folder → record the new
|
|
876
|
+
* location → point each release's PM2 config at its new path → start the live
|
|
877
|
+
* release again. If the move itself fails nothing has changed, and the process
|
|
878
|
+
* is started again where it was.
|
|
879
|
+
*
|
|
880
|
+
* `targetDir` is the PARENT folder: the app ends up in `<targetDir>/<app id>`,
|
|
881
|
+
* so a folder can hold several apps without them colliding.
|
|
882
|
+
*
|
|
883
|
+
* @param {string} appId
|
|
884
|
+
* @param {string} targetDir
|
|
885
|
+
* @param {{ deps?: Object }} [opts]
|
|
886
|
+
* @returns {Promise<{ ok: boolean, status: number, error: string|null,
|
|
887
|
+
* from?: string, to?: string, restarted?: boolean, warning?: string|null }>}
|
|
888
|
+
*/
|
|
889
|
+
export async function moveApp(appId, targetDir, { deps = {} } = {}) {
|
|
890
|
+
const app = getApp(appId);
|
|
891
|
+
if (!app) return { ok: false, status: 404, error: `No app named "${appId}".` };
|
|
892
|
+
|
|
893
|
+
const dirError = validateDeployDir(targetDir);
|
|
894
|
+
if (dirError) return { ok: false, status: 400, error: dirError };
|
|
895
|
+
|
|
896
|
+
const from = appPaths(appId).root;
|
|
897
|
+
const to = path.join(path.resolve(String(targetDir).trim()), appId);
|
|
898
|
+
|
|
899
|
+
if (path.resolve(from) === path.resolve(to)) {
|
|
900
|
+
return { ok: false, status: 409, error: 'The app is already in that folder.' };
|
|
901
|
+
}
|
|
902
|
+
if (isInside(to, from)) return { ok: false, status: 400, error: 'The new folder cannot be inside the app’s current folder.' };
|
|
903
|
+
if (fs.existsSync(to) && fs.readdirSync(to).length > 0) {
|
|
904
|
+
return { ok: false, status: 409, error: `${to} already exists and is not empty.` };
|
|
905
|
+
}
|
|
906
|
+
|
|
907
|
+
const d = resolveDeps(deps);
|
|
908
|
+
const lock = acquireLock(appId, { runId: null });
|
|
909
|
+
if (!lock.ok) return { ok: false, status: 409, error: 'A deployment of this app is running. Wait for it to finish.' };
|
|
910
|
+
|
|
911
|
+
const state = readState(appId);
|
|
912
|
+
let pm2Path = null;
|
|
913
|
+
let wasRunning = false;
|
|
914
|
+
|
|
915
|
+
/** Starts the live release from wherever the app now is. */
|
|
916
|
+
const startCurrent = async () => {
|
|
917
|
+
if (!state.current || !pm2Path) return false;
|
|
918
|
+
const config = path.join(releaseDir(appId, state.current), PM2_CONFIG_FILE);
|
|
919
|
+
if (!fs.existsSync(config)) return false;
|
|
920
|
+
const started = await d.runner.startConfig(config, { run: d.run, pm2Path });
|
|
921
|
+
if (started.ok) await d.runner.save({ run: d.run, pm2Path });
|
|
922
|
+
return started.ok;
|
|
923
|
+
};
|
|
924
|
+
|
|
925
|
+
try {
|
|
926
|
+
({ pm2Path, wasRunning } = await stopProcess(appId, d));
|
|
927
|
+
|
|
928
|
+
// An empty folder left by an earlier attempt would make the rename fail.
|
|
929
|
+
if (fs.existsSync(to)) fs.rmdirSync(to);
|
|
930
|
+
|
|
931
|
+
try {
|
|
932
|
+
if (fs.existsSync(from)) moveDirectory(from, to);
|
|
933
|
+
else fs.mkdirSync(to, { recursive: true });
|
|
934
|
+
} catch (err) {
|
|
935
|
+
// Nothing was recorded, so the app is still where it was. Put it back up.
|
|
936
|
+
if (wasRunning) await startCurrent();
|
|
937
|
+
return { ok: false, status: 500, error: `The folder could not be moved: ${err.message}` };
|
|
938
|
+
}
|
|
939
|
+
|
|
940
|
+
setAppDir(appId, to);
|
|
941
|
+
|
|
942
|
+
// The lock file travelled with the folder; it is ours, and the old path it
|
|
943
|
+
// was taken at no longer exists for release() to clean up.
|
|
944
|
+
try { fs.rmSync(pathsIn(to).lock, { force: true }); } catch { /* ignore */ }
|
|
945
|
+
|
|
946
|
+
// Each release's PM2 config names its own absolute path.
|
|
947
|
+
for (const id of listReleases(appId)) {
|
|
948
|
+
const dir = releaseDir(appId, id);
|
|
949
|
+
const file = path.join(dir, PM2_CONFIG_FILE);
|
|
950
|
+
try {
|
|
951
|
+
const config = JSON.parse(fs.readFileSync(file, 'utf8'));
|
|
952
|
+
for (const entry of config.apps ?? []) entry.cwd = dir;
|
|
953
|
+
fs.writeFileSync(file, JSON.stringify(config, null, 2), { mode: 0o600 });
|
|
954
|
+
} catch { /* a release that never started has no config */ }
|
|
955
|
+
}
|
|
956
|
+
|
|
957
|
+
let restarted = false;
|
|
958
|
+
let warning = null;
|
|
959
|
+
if (wasRunning) {
|
|
960
|
+
restarted = await startCurrent();
|
|
961
|
+
if (!restarted) warning = 'The app was moved, but PM2 could not start it again. Deploy it, or start it from the Processes page.';
|
|
962
|
+
}
|
|
963
|
+
|
|
964
|
+
return { ok: true, status: 200, error: null, from, to, restarted, warning };
|
|
965
|
+
} finally {
|
|
966
|
+
lock.release();
|
|
967
|
+
}
|
|
968
|
+
}
|
|
969
|
+
|
|
970
|
+
/** The finished-or-running record of a run, or null. */
|
|
971
|
+
export function getRun(appId, runId) {
|
|
972
|
+
return readRun(appId, runId);
|
|
973
|
+
}
|