easy-devops 1.2.0 → 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 +17 -5
- 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 +93 -2
- 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/channels/discord.js +77 -0
- package/src/core/channels/telegram.js +120 -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 +44 -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 +364 -0
- package/src/core/domains-db.js +234 -0
- package/src/core/events.js +222 -0
- 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 +103 -0
- 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 +128 -13
- 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 +316 -0
- package/src/dashboard/controllers/deploymentsController.js +479 -0
- package/src/dashboard/controllers/domainsController.js +403 -0
- package/src/dashboard/controllers/hubController.js +135 -0
- package/src/dashboard/controllers/nginxController.js +146 -0
- package/src/dashboard/controllers/processesController.js +368 -0
- package/src/dashboard/controllers/settingsController.js +503 -0
- package/src/dashboard/controllers/sslController.js +452 -0
- 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/img/icon.png +0 -0
- package/src/dashboard/public/img/icon_b.png +0 -0
- package/src/dashboard/public/js/app.js +595 -19
- package/src/dashboard/routes/auth.js +14 -33
- package/src/dashboard/routes/deployments.js +46 -0
- package/src/dashboard/routes/domains.js +20 -363
- package/src/dashboard/routes/hub.js +22 -0
- package/src/dashboard/routes/nginx.js +22 -146
- package/src/dashboard/routes/processes.js +29 -0
- package/src/dashboard/routes/settings.js +25 -232
- package/src/dashboard/routes/ssl.js +18 -276
- package/src/dashboard/server.js +279 -165
- package/src/dashboard/socketManager.js +246 -0
- package/src/dashboard/views/index.ejs +1 -0
- package/src/dashboard/views/partials/domain-form.ejs +64 -1
- package/src/dashboard/views/partials/login.ejs +69 -8
- package/src/dashboard/views/partials/processes-panel.ejs +261 -0
- package/src/dashboard/views/partials/settings-panel.ejs +549 -118
- package/src/dashboard/views/partials/sidebar.ejs +3 -1
- package/src/dashboard/lib/domains-db.js +0 -51
- /package/src/{dashboard/routes → core/channels}/.gitkeep +0 -0
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* core/deploy/hubCommands.js
|
|
3
|
+
*
|
|
4
|
+
* Deployment actions the Hub can request, and the CI trigger the Hub relays.
|
|
5
|
+
*
|
|
6
|
+
* Permission checking happens in core/hub/agent.js before any of this runs
|
|
7
|
+
* (core/hubPermissions.js maps each action to its permission). These
|
|
8
|
+
* functions assume they are allowed and concentrate on two things:
|
|
9
|
+
*
|
|
10
|
+
* 1. doing the work through the same core modules the dashboard uses, so a
|
|
11
|
+
* deployment started from the Hub is indistinguishable from one started
|
|
12
|
+
* locally;
|
|
13
|
+
* 2. deciding what may LEAVE the server. Everything returned here uses
|
|
14
|
+
* publicApp(): environment variable names without values, and no
|
|
15
|
+
* credentials. The two exceptions are explicit actions with their own
|
|
16
|
+
* permissions — `getEnv` (deploy.env) and `issueToken` (deploy.manage) —
|
|
17
|
+
* where the value passes through the Hub once and is not stored there.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import { loadConfig } from '../config.js';
|
|
21
|
+
import { getDomains } from '../domains-db.js';
|
|
22
|
+
import { getHubLink, getHubPermissions, resolveHubOrigin } from '../hubLink.js';
|
|
23
|
+
|
|
24
|
+
import {
|
|
25
|
+
getApp, createApp, updateApp, publicApp, normalizeEnv, rotateHookToken, verifyHookToken, validateAppId,
|
|
26
|
+
} from './apps.js';
|
|
27
|
+
import { startDeployment, startRollback, removeApp, moveApp } from './engine.js';
|
|
28
|
+
import { linkDomain, unlinkDomain } from './domainLink.js';
|
|
29
|
+
import { findDomain } from '../domains-db.js';
|
|
30
|
+
import { describeGithubSetup } from './github.js';
|
|
31
|
+
import { hookSettings } from './hookServer.js';
|
|
32
|
+
import { describeApps, describeAppDetail } from './status.js';
|
|
33
|
+
import { isId, readLogTail, readRun } from './store.js';
|
|
34
|
+
|
|
35
|
+
/** An error whose message is safe to show the Hub user as-is. */
|
|
36
|
+
function refuse(message) {
|
|
37
|
+
return Object.assign(new Error(message), { expected: true });
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function requireApp(id) {
|
|
41
|
+
const app = typeof id === 'string' ? getApp(id) : null;
|
|
42
|
+
if (!app) throw refuse(`No app named "${id}".`);
|
|
43
|
+
return app;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** The Hub pairing, in the form github.js wants. */
|
|
47
|
+
function hubTarget() {
|
|
48
|
+
const link = getHubLink();
|
|
49
|
+
return {
|
|
50
|
+
link: link ? { origin: resolveHubOrigin(link.hubUrl), serverId: link.serverId } : null,
|
|
51
|
+
ciAllowed: getHubPermissions()['deploy.ci'] === true,
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** The GitHub setup for an app, including the through-the-Hub URL. Shared with the dashboard. */
|
|
56
|
+
export function githubSetupFor(app, view) {
|
|
57
|
+
return describeGithubSetup(view(app), hookSettings(loadConfig()), getDomains(), hubTarget());
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Fields a Hub user may set on an app through create/update.
|
|
62
|
+
*
|
|
63
|
+
* `env` is deliberately absent: environment values have their own permission
|
|
64
|
+
* and their own action, so "may create an app" does not quietly include "may
|
|
65
|
+
* read or replace its secrets".
|
|
66
|
+
*/
|
|
67
|
+
function appInputFromHub(input) {
|
|
68
|
+
const src = input && typeof input === 'object' ? input : {};
|
|
69
|
+
const out = {};
|
|
70
|
+
for (const key of ['repoUrl', 'branch', 'authType', 'token', 'settings', 'keepReleases', 'autoRollback', 'notifyChannelIds']) {
|
|
71
|
+
if (src[key] !== undefined) out[key] = src[key];
|
|
72
|
+
}
|
|
73
|
+
return out;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Handles a hub:manageDeploy command.
|
|
78
|
+
*
|
|
79
|
+
* @param {Object} payload { action, id?, ... }
|
|
80
|
+
* @param {Object} [opts]
|
|
81
|
+
* @param {(run: Object) => void} [opts.onFinished] Called when a deployment or rollback started here ends
|
|
82
|
+
* @returns {Promise<Object>} The command's result data. Throws to refuse.
|
|
83
|
+
*/
|
|
84
|
+
export async function handleHubDeployCommand(payload, { onFinished = null } = {}) {
|
|
85
|
+
const { action, id } = payload ?? {};
|
|
86
|
+
const watch = (started) => {
|
|
87
|
+
if (onFinished) started.done.then((run) => { try { onFinished(run); } catch { /* ignore */ } });
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
switch (action) {
|
|
91
|
+
case 'list':
|
|
92
|
+
return { apps: await describeApps({ view: publicApp }) };
|
|
93
|
+
|
|
94
|
+
case 'detail': {
|
|
95
|
+
const detail = await describeAppDetail(String(id ?? ''), { view: publicApp });
|
|
96
|
+
if (!detail) throw refuse(`No app named "${id}".`);
|
|
97
|
+
return detail;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
case 'github': {
|
|
101
|
+
const app = requireApp(id);
|
|
102
|
+
return githubSetupFor(app, publicApp);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
case 'run': {
|
|
106
|
+
const app = requireApp(id);
|
|
107
|
+
const run = isId(payload.runId) ? readRun(app.id, payload.runId) : null;
|
|
108
|
+
if (!run) throw refuse('No such run.');
|
|
109
|
+
// A smaller tail than the dashboard's: this travels over the agent
|
|
110
|
+
// socket and through the Hub to a browser.
|
|
111
|
+
const log = readLogTail(app.id, run.id, { maxBytes: 32 * 1024 });
|
|
112
|
+
return { ...run, log: log.text, logTruncated: log.truncated };
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
case 'deploy': {
|
|
116
|
+
const app = requireApp(id);
|
|
117
|
+
const ref = typeof payload.ref === 'string' && payload.ref.trim() ? payload.ref.trim() : null;
|
|
118
|
+
const started = startDeployment(app.id, { trigger: 'hub', ref, actor: payload.actor ? `hub: ${String(payload.actor).slice(0, 80)}` : 'hub' });
|
|
119
|
+
if (!started.ok) throw refuse(started.error);
|
|
120
|
+
watch(started);
|
|
121
|
+
return { runId: started.runId, status: 'running' };
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
case 'rollback': {
|
|
125
|
+
const app = requireApp(id);
|
|
126
|
+
const releaseId = typeof payload.releaseId === 'string' && payload.releaseId ? payload.releaseId : null;
|
|
127
|
+
if (releaseId !== null && !isId(releaseId)) throw refuse('Invalid release id.');
|
|
128
|
+
const started = startRollback(app.id, { releaseId, actor: 'hub' });
|
|
129
|
+
if (!started.ok) throw refuse(started.error);
|
|
130
|
+
watch(started);
|
|
131
|
+
return { runId: started.runId, status: 'running' };
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
case 'create': {
|
|
135
|
+
const input = appInputFromHub(payload.app);
|
|
136
|
+
const result = createApp({ ...input, id: String(payload.app?.id ?? '').trim() });
|
|
137
|
+
if (!result.ok) throw refuse(result.errors[0]);
|
|
138
|
+
return publicApp(result.app);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
case 'update': {
|
|
142
|
+
requireApp(id);
|
|
143
|
+
const result = updateApp(id, appInputFromHub(payload.app));
|
|
144
|
+
if (!result.ok) throw refuse(result.errors[0]);
|
|
145
|
+
return publicApp(result.app);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
case 'delete': {
|
|
149
|
+
const app = requireApp(id);
|
|
150
|
+
const deleteDomain = payload.deleteDomain === true && Boolean(app.domain);
|
|
151
|
+
// Removing a domain is its own permission. Managing apps does not imply it.
|
|
152
|
+
if (deleteDomain && getHubPermissions()['domains.delete'] !== true) {
|
|
153
|
+
throw refuse("Deleting the linked domain needs 'domains.delete', which this server has switched off. Delete the app without its domain, or enable it in Settings → Hub.");
|
|
154
|
+
}
|
|
155
|
+
const result = await removeApp(id, { deleteDomain });
|
|
156
|
+
if (!result.ok) throw refuse(result.error);
|
|
157
|
+
return { deleted: id, domainDeleted: result.domainDeleted, warning: result.warning ?? null };
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
case 'linkDomain': {
|
|
161
|
+
requireApp(id);
|
|
162
|
+
const name = String(payload.domain ?? '').trim().toLowerCase();
|
|
163
|
+
// Linking writes a domain's nginx config, so it also needs the domain
|
|
164
|
+
// permission that action would need on its own.
|
|
165
|
+
const needed = findDomain(name) ? 'domains.edit' : 'domains.create';
|
|
166
|
+
if (getHubPermissions()[needed] !== true) {
|
|
167
|
+
throw refuse(`Linking this domain needs '${needed}', which this server has switched off (Settings → Hub).`);
|
|
168
|
+
}
|
|
169
|
+
// No certificate request from here: `ssl.issue` is its own permission
|
|
170
|
+
// and its own action, on the SSL section.
|
|
171
|
+
const result = await linkDomain(id, { domain: name, ssl: false });
|
|
172
|
+
if (!result.ok) throw refuse(result.error);
|
|
173
|
+
return { domain: result.domain, created: result.created, port: result.port, nginxReloaded: result.nginxReloaded };
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
case 'unlinkDomain': {
|
|
177
|
+
const result = unlinkDomain(id);
|
|
178
|
+
if (!result.ok) throw refuse(result.error);
|
|
179
|
+
return { unlinked: true };
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
case 'move': {
|
|
183
|
+
requireApp(id);
|
|
184
|
+
const result = await moveApp(id, payload.path);
|
|
185
|
+
if (!result.ok) throw refuse(result.error);
|
|
186
|
+
return { from: result.from, to: result.to, restarted: result.restarted, warning: result.warning ?? null };
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
case 'issueToken': {
|
|
190
|
+
requireApp(id);
|
|
191
|
+
const issued = rotateHookToken(id);
|
|
192
|
+
// The one moment the token exists in clear. It passes through the Hub to
|
|
193
|
+
// the person who asked for it; the Hub does not keep it.
|
|
194
|
+
return { token: issued.token, tokenHint: issued.hint };
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
case 'getEnv': {
|
|
198
|
+
const app = requireApp(id);
|
|
199
|
+
return { env: app.env ?? {} };
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
case 'setEnv': {
|
|
203
|
+
requireApp(id);
|
|
204
|
+
const env = normalizeEnv(payload.env);
|
|
205
|
+
if (!env.ok) throw refuse(env.error);
|
|
206
|
+
const result = updateApp(id, { env: env.value });
|
|
207
|
+
if (!result.ok) throw refuse(result.errors[0]);
|
|
208
|
+
return { envKeys: Object.keys(env.value).sort() };
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
default:
|
|
212
|
+
throw refuse(`Unknown deployment action: ${action}`);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Handles hub:deployHook — a CI job calling the Hub instead of this server.
|
|
218
|
+
*
|
|
219
|
+
* The Hub is a relay here, nothing more: it forwards the app id and the token
|
|
220
|
+
* the CI job presented, and THIS function verifies the token, exactly as the
|
|
221
|
+
* server's own deploy hook does. The Hub never learns whether a token is
|
|
222
|
+
* valid except by the answer, and stores nothing.
|
|
223
|
+
*
|
|
224
|
+
* One answer for an unknown app, an app without a token and a wrong token —
|
|
225
|
+
* `unauthorized: true`, which the Hub turns into a 401.
|
|
226
|
+
*
|
|
227
|
+
* @param {Object} payload { action: 'trigger'|'status', appId, token, ref?, runId? }
|
|
228
|
+
* @returns {Promise<Object>}
|
|
229
|
+
*/
|
|
230
|
+
export async function handleHubDeployHook(payload, { onFinished = null } = {}) {
|
|
231
|
+
const { action, appId, token } = payload ?? {};
|
|
232
|
+
|
|
233
|
+
const app = typeof appId === 'string' && validateAppId(appId) === null ? getApp(appId) : null;
|
|
234
|
+
if (!app || !verifyHookToken(app, typeof token === 'string' ? token : '')) {
|
|
235
|
+
return { unauthorized: true };
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
if (action === 'trigger') {
|
|
239
|
+
const ref = typeof payload.ref === 'string' && payload.ref.trim() ? payload.ref.trim() : null;
|
|
240
|
+
const started = startDeployment(app.id, { trigger: 'webhook', ref, actor: 'deploy hook (via Hub)' });
|
|
241
|
+
if (!started.ok) {
|
|
242
|
+
return { started: false, status: started.status ?? 500, error: started.error, runId: started.runId ?? null };
|
|
243
|
+
}
|
|
244
|
+
if (onFinished) started.done.then((run) => { try { onFinished(run); } catch { /* ignore */ } });
|
|
245
|
+
return { started: true, runId: started.runId };
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
if (action === 'status') {
|
|
249
|
+
const run = isId(payload.runId) ? readRun(app.id, payload.runId) : null;
|
|
250
|
+
if (!run) return { found: false };
|
|
251
|
+
return {
|
|
252
|
+
found: true,
|
|
253
|
+
run: {
|
|
254
|
+
runId: run.id,
|
|
255
|
+
status: run.status,
|
|
256
|
+
step: run.step,
|
|
257
|
+
sha: run.sha,
|
|
258
|
+
error: run.error,
|
|
259
|
+
rolledBackTo: run.rolledBackTo ?? null,
|
|
260
|
+
startedAt: run.startedAt,
|
|
261
|
+
finishedAt: run.finishedAt,
|
|
262
|
+
},
|
|
263
|
+
};
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
throw refuse(`Unknown deploy hook action: ${action}`);
|
|
267
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* core/deploy/launcher.js
|
|
3
|
+
*
|
|
4
|
+
* The small script PM2 runs when an app's start command is a command line
|
|
5
|
+
* (`npm start`, `python app.py`, `./server --port 3000`) rather than a plain
|
|
6
|
+
* `node file.js`.
|
|
7
|
+
*
|
|
8
|
+
* PM2 starts a script, not a command line. Asking it to run `npm` directly
|
|
9
|
+
* works on Linux and fails on Windows, where `npm` is `npm.cmd`; wrapping the
|
|
10
|
+
* command in a shell means choosing that shell per platform in a PM2 config.
|
|
11
|
+
* This file sidesteps both: PM2 always runs `node ez-launch.cjs`, and Node's
|
|
12
|
+
* own `shell: true` picks `/bin/sh` or `cmd.exe`.
|
|
13
|
+
*
|
|
14
|
+
* It is written into each release directory by the engine. It is CommonJS
|
|
15
|
+
* (.cjs) on purpose, so it runs the same whether or not the deployed project
|
|
16
|
+
* declares `"type": "module"`.
|
|
17
|
+
*
|
|
18
|
+
* Not project code in the sense of CLAUDE.md's "only core/shell.js spawns":
|
|
19
|
+
* this runs inside the DEPLOYED app's process tree, under PM2, and never
|
|
20
|
+
* inside Easy DevOps.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
export const LAUNCHER_FILE = 'ez-launch.cjs';
|
|
24
|
+
|
|
25
|
+
/** Environment variable the launcher reads its command from. */
|
|
26
|
+
export const LAUNCHER_COMMAND_ENV = 'EZ_START_COMMAND';
|
|
27
|
+
|
|
28
|
+
export const LAUNCHER_SOURCE = `// Generated by Easy DevOps. Starts the app's command line under PM2.
|
|
29
|
+
'use strict';
|
|
30
|
+
const { spawn } = require('child_process');
|
|
31
|
+
|
|
32
|
+
const command = process.env.${LAUNCHER_COMMAND_ENV};
|
|
33
|
+
if (!command) {
|
|
34
|
+
console.error('[ez-launch] ${LAUNCHER_COMMAND_ENV} is not set - nothing to start.');
|
|
35
|
+
process.exit(1);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const child = spawn(command, { shell: true, stdio: 'inherit', windowsHide: true });
|
|
39
|
+
|
|
40
|
+
// PM2 stops an app by signalling this process. Pass it on, so the real app
|
|
41
|
+
// gets the chance to shut down cleanly instead of being orphaned.
|
|
42
|
+
for (const signal of ['SIGINT', 'SIGTERM', 'SIGHUP']) {
|
|
43
|
+
process.on(signal, () => {
|
|
44
|
+
try { child.kill(signal); } catch (_) { /* already gone */ }
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
child.on('error', (err) => {
|
|
49
|
+
console.error('[ez-launch] could not start: ' + err.message);
|
|
50
|
+
process.exit(1);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
// Exit the way the app did, so PM2's restart policy sees the real outcome.
|
|
54
|
+
child.on('exit', (code, signal) => {
|
|
55
|
+
process.exit(signal ? 1 : (code === null ? 1 : code));
|
|
56
|
+
});
|
|
57
|
+
`;
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* core/deploy/status.js
|
|
3
|
+
*
|
|
4
|
+
* Where each deployed app stands right now: its record, the live release, the
|
|
5
|
+
* last run, whether a deployment is in progress, and its PM2 process.
|
|
6
|
+
*
|
|
7
|
+
* Shared by the dashboard's Deployments page and the Hub agent, so the two
|
|
8
|
+
* cannot describe the same app differently. The only thing that differs is
|
|
9
|
+
* the `view`: the dashboard gets dashboardApp() (environment values included,
|
|
10
|
+
* behind its own login), the Hub gets publicApp() (names only, no secrets).
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import * as defaultRunner from '../runner.js';
|
|
14
|
+
import { listApps, getApp, dashboardApp, pm2Name } from './apps.js';
|
|
15
|
+
import { describeRelease } from './engine.js';
|
|
16
|
+
import { listRuns, readState, listReleases, lockHolder } from './store.js';
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* PM2's view of every process, keyed by name — or an empty map.
|
|
20
|
+
*
|
|
21
|
+
* Read only when PM2's daemon is already running: a status read must never be
|
|
22
|
+
* the thing that starts a PM2 daemon on a machine that was not running one.
|
|
23
|
+
*
|
|
24
|
+
* @returns {Promise<Map<string, Object>>}
|
|
25
|
+
*/
|
|
26
|
+
export async function pm2Statuses({ runner = defaultRunner } = {}) {
|
|
27
|
+
const byName = new Map();
|
|
28
|
+
if (!runner.isDaemonRunning()) return byName;
|
|
29
|
+
try {
|
|
30
|
+
const pm2 = await runner.detect({ probeVersion: false });
|
|
31
|
+
if (!pm2.installed) return byName;
|
|
32
|
+
const listed = await runner.list({ pm2Path: pm2.path ?? 'pm2' });
|
|
33
|
+
if (listed.ok) for (const app of listed.apps) byName.set(app.name, app);
|
|
34
|
+
} catch { /* status is decoration; the list still renders */ }
|
|
35
|
+
return byName;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* One app with its current standing.
|
|
40
|
+
*
|
|
41
|
+
* @param {Object} app A registry record
|
|
42
|
+
* @param {Map<string, Object>} processes From pm2Statuses()
|
|
43
|
+
* @param {(app: Object) => Object} [view] dashboardApp (default) or publicApp
|
|
44
|
+
*/
|
|
45
|
+
export function describeApp(app, processes, view = dashboardApp) {
|
|
46
|
+
const state = readState(app.id);
|
|
47
|
+
const runs = listRuns(app.id, { limit: 1 });
|
|
48
|
+
const holder = lockHolder(app.id);
|
|
49
|
+
const proc = processes.get(pm2Name(app.id)) ?? null;
|
|
50
|
+
|
|
51
|
+
return {
|
|
52
|
+
...view(app),
|
|
53
|
+
currentRelease: state.current,
|
|
54
|
+
previousRelease: state.previous,
|
|
55
|
+
deploying: Boolean(holder),
|
|
56
|
+
activeRunId: holder?.runId ?? null,
|
|
57
|
+
lastRun: runs[0] ?? null,
|
|
58
|
+
process: proc
|
|
59
|
+
? {
|
|
60
|
+
status: proc.status, pid: proc.pid, cpu: proc.cpu, memoryBytes: proc.memoryBytes,
|
|
61
|
+
uptimeMs: proc.uptimeMs, restarts: proc.restarts,
|
|
62
|
+
}
|
|
63
|
+
: null,
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** Every app, described. */
|
|
68
|
+
export async function describeApps({ view = dashboardApp, runner = defaultRunner } = {}) {
|
|
69
|
+
const processes = await pm2Statuses({ runner });
|
|
70
|
+
return listApps().map((app) => describeApp(app, processes, view));
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* One app with its recent runs and the releases on disk, or null.
|
|
75
|
+
*/
|
|
76
|
+
export async function describeAppDetail(appId, { view = dashboardApp, runner = defaultRunner } = {}) {
|
|
77
|
+
const app = getApp(appId);
|
|
78
|
+
if (!app) return null;
|
|
79
|
+
|
|
80
|
+
const processes = await pm2Statuses({ runner });
|
|
81
|
+
const state = readState(app.id);
|
|
82
|
+
|
|
83
|
+
return {
|
|
84
|
+
...describeApp(app, processes, view),
|
|
85
|
+
runs: listRuns(app.id, { limit: 20 }),
|
|
86
|
+
releases: listReleases(app.id)
|
|
87
|
+
.map((id) => describeRelease(app.id, id))
|
|
88
|
+
.filter(Boolean)
|
|
89
|
+
.map((r) => ({ ...r, current: r.id === state.current, previous: r.id === state.previous })),
|
|
90
|
+
};
|
|
91
|
+
}
|