easy-devops 1.2.1 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +97 -6
- package/dist/ui/assets/index-B6zdwEnG.css +1 -0
- package/dist/ui/assets/index-Cy0oNmul.js +442 -0
- package/dist/ui/assets/socket-DwLPrgsr.js +1 -0
- package/dist/ui/assets/socket-K5ttLD2q.js +65 -0
- package/dist/ui/brand/apple-touch-icon.png +0 -0
- package/dist/ui/brand/favicon-16.png +0 -0
- package/dist/ui/brand/favicon-32.png +0 -0
- package/dist/ui/brand/favicon-light-16.png +0 -0
- package/dist/ui/brand/favicon-light-32.png +0 -0
- package/dist/ui/brand/favicon.ico +0 -0
- package/dist/ui/brand/icon-192.png +0 -0
- package/dist/ui/brand/icon-48.png +0 -0
- package/dist/ui/brand/icon-512.png +0 -0
- package/dist/ui/brand/icon-64-light.png +0 -0
- package/dist/ui/brand/icon-64.png +0 -0
- package/dist/ui/brand/logo-dark.png +0 -0
- package/dist/ui/brand/logo-light.png +0 -0
- package/dist/ui/brand/mark-dark.png +0 -0
- package/dist/ui/brand/mark-light.png +0 -0
- package/dist/ui/index.html +71 -0
- package/install.ps1 +188 -1
- package/install.sh +254 -29
- package/package.json +15 -4
- package/src/cli/dash.js +110 -0
- package/src/cli/index.js +237 -106
- package/src/cli/managers/domain-manager.js +60 -40
- package/src/cli/managers/ssl-manager.js +47 -330
- package/src/cli/menu.js +156 -0
- package/src/cli/menus/cleanup.js +205 -0
- package/src/cli/menus/dashboard.js +92 -1
- package/src/cli/menus/runtime.js +236 -0
- package/src/cli/menus/service.js +764 -0
- package/src/cli/menus/settings.js +176 -8
- package/src/cli/menus/uninstall.js +167 -0
- package/src/cli/menus/update.js +221 -65
- package/src/core/acme-challenge.js +273 -0
- package/src/core/cert-operations.js +189 -0
- package/src/{dashboard/lib → core}/cert-reader.js +2 -2
- package/src/core/cert-renewal.js +494 -0
- package/src/core/cleanup.js +600 -0
- package/src/core/config.js +169 -2
- package/src/core/daemon/ipc.js +292 -0
- package/src/core/daemon/lock.js +294 -0
- package/src/core/daemon/run.js +241 -0
- package/src/core/daemon/stopRequest.js +162 -0
- package/src/core/db.js +37 -8
- package/src/core/deploy/apps.js +491 -0
- package/src/core/deploy/domainLink.js +200 -0
- package/src/core/deploy/engine.js +973 -0
- package/src/core/deploy/ezfile.js +361 -0
- package/src/core/deploy/github.js +239 -0
- package/src/core/deploy/githubApi.js +261 -0
- package/src/core/deploy/hookServer.js +325 -0
- package/src/core/deploy/hubCommands.js +267 -0
- package/src/core/deploy/launcher.js +57 -0
- package/src/core/deploy/status.js +91 -0
- package/src/core/deploy/store.js +398 -0
- package/src/core/domainNotifier.js +94 -28
- package/src/core/domains-db.js +234 -0
- package/src/core/events.js +94 -2
- package/src/core/hub/agent.js +842 -0
- package/src/core/hubLink.js +306 -0
- package/src/core/hubPermissions.js +430 -0
- package/src/core/installSource.js +162 -0
- package/src/core/jobs/bus.js +120 -0
- package/src/core/jobs/index.js +371 -0
- package/src/core/jobs/monitors.js +368 -0
- package/src/core/jobs/owner.js +217 -0
- package/src/core/jobs/revisionWatcher.js +106 -0
- package/src/core/nginx-apply.js +213 -0
- package/src/core/nginx-conf-generator.js +253 -17
- package/src/{dashboard/lib → core}/nginx-service.js +18 -9
- package/src/core/notifications/claim.js +214 -0
- package/src/core/notifications/dispatch.js +154 -0
- package/src/core/notifier.js +31 -1
- package/src/core/passwords.js +172 -0
- package/src/core/permissions.js +8 -4
- package/src/core/platform.js +709 -2
- package/src/core/probeCache.js +258 -0
- package/src/core/purge.js +372 -0
- package/src/core/runner.js +488 -0
- package/src/core/service/acl.js +226 -0
- package/src/core/service/dataDir.js +345 -0
- package/src/core/service/index.js +1128 -0
- package/src/core/service/legacy.js +218 -0
- package/src/core/service/runtime.js +446 -0
- package/src/core/service/runtimeUpdate.js +800 -0
- package/src/core/service/systemd.js +458 -0
- package/src/core/service/windowsPersonal.js +530 -0
- package/src/core/service/windowsService.js +576 -0
- package/src/core/service/winsw.js +260 -0
- package/src/core/setupCode.js +331 -0
- package/src/core/shell.js +70 -7
- package/src/core/ssl-issuer.js +627 -0
- package/src/core/updater.js +295 -0
- package/src/core/validators.js +99 -0
- package/src/dashboard/controllers/authController.js +302 -15
- package/src/dashboard/controllers/deploymentsController.js +479 -0
- package/src/dashboard/controllers/domainsController.js +39 -52
- package/src/dashboard/controllers/hubController.js +135 -0
- package/src/dashboard/controllers/nginxController.js +21 -3
- package/src/dashboard/controllers/processesController.js +368 -0
- package/src/dashboard/controllers/settingsController.js +180 -12
- package/src/dashboard/controllers/sslController.js +171 -10
- package/src/dashboard/lib/daemonBridge.js +163 -0
- package/src/dashboard/lib/localRequest.js +235 -0
- package/src/dashboard/lib/loginRateLimit.js +119 -0
- package/src/dashboard/lib/requireAuth.js +80 -0
- package/src/dashboard/public/js/app.js +489 -19
- package/src/dashboard/routes/auth.js +10 -10
- package/src/dashboard/routes/deployments.js +46 -0
- package/src/dashboard/routes/domains.js +9 -0
- package/src/dashboard/routes/hub.js +22 -0
- package/src/dashboard/routes/nginx.js +9 -0
- package/src/dashboard/routes/processes.js +29 -0
- package/src/dashboard/routes/settings.js +9 -0
- package/src/dashboard/routes/ssl.js +9 -0
- package/src/dashboard/server.js +288 -21
- package/src/dashboard/socketManager.js +171 -207
- package/src/dashboard/views/index.ejs +1 -0
- package/src/dashboard/views/partials/login.ejs +68 -7
- package/src/dashboard/views/partials/processes-panel.ejs +261 -0
- package/src/dashboard/views/partials/settings-panel.ejs +287 -0
- package/src/dashboard/views/partials/sidebar.ejs +2 -0
- package/src/dashboard/lib/domains-db.js +0 -51
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* core/service/acl.js
|
|
3
|
+
*
|
|
4
|
+
* Granting the Windows service account the access it needs — and nothing else.
|
|
5
|
+
*
|
|
6
|
+
* The service runs as `NT SERVICE\EasyDevOps`, a virtual account with no
|
|
7
|
+
* password and, by default, no rights to anything outside its own service key.
|
|
8
|
+
* That is the right starting point, and it means every directory it genuinely
|
|
9
|
+
* needs has to be granted explicitly:
|
|
10
|
+
*
|
|
11
|
+
* - the data directory — Modify: it writes the database, the lock, logs
|
|
12
|
+
* - the pinned runtime — Read & execute: it runs node.exe from there
|
|
13
|
+
* - `nginxDir` from Settings — Modify: it writes and reloads domain configs
|
|
14
|
+
* - `sslDir` from Settings — Modify: it writes renewed certificates
|
|
15
|
+
*
|
|
16
|
+
* ── Why the paths come from Settings and are never hardcoded ───────────────
|
|
17
|
+
*
|
|
18
|
+
* `C:\nginx` and `C:\ssl` are only the defaults. A user who installed nginx
|
|
19
|
+
* somewhere else, or moved their certificates, would get a service that
|
|
20
|
+
* silently cannot read its own configuration — while the grant sat on two
|
|
21
|
+
* directories that may not even exist. Worse, granting a hardcoded path would
|
|
22
|
+
* hand a service account write access to a directory the user never pointed us
|
|
23
|
+
* at. So the grants are computed from the live config, recorded, and compared
|
|
24
|
+
* on every `status`: if the user changes a path in Settings afterwards, the
|
|
25
|
+
* service is told to run `ezz service repair` rather than quietly losing
|
|
26
|
+
* access.
|
|
27
|
+
*
|
|
28
|
+
* ── Why inheritance flags matter ───────────────────────────────────────────
|
|
29
|
+
*
|
|
30
|
+
* `(OI)(CI)` makes the grant apply to files and subdirectories created later.
|
|
31
|
+
* Without them, a certificate renewed next month lands in a directory the
|
|
32
|
+
* service can enter but whose new file it cannot read. `/T` applies the change
|
|
33
|
+
* to what is already there.
|
|
34
|
+
*
|
|
35
|
+
* Nothing in this module runs without an elevated terminal; `icacls` fails with
|
|
36
|
+
* "Access is denied" otherwise, which is reported as such rather than retried.
|
|
37
|
+
*/
|
|
38
|
+
|
|
39
|
+
import fs from 'fs';
|
|
40
|
+
import path from 'path';
|
|
41
|
+
|
|
42
|
+
import { run as shellRun } from '../shell.js';
|
|
43
|
+
import { SERVICE_ACCOUNT } from './winsw.js';
|
|
44
|
+
|
|
45
|
+
/** Access levels, in icacls's own notation. */
|
|
46
|
+
export const ACCESS = Object.freeze({
|
|
47
|
+
/** Read, write, delete within the tree. Not "take ownership" or "change permissions". */
|
|
48
|
+
MODIFY: 'M',
|
|
49
|
+
/** Read and execute only. */
|
|
50
|
+
READ_EXECUTE: 'RX',
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Which directories the service needs, and at what level.
|
|
55
|
+
*
|
|
56
|
+
* @param {Object} opts
|
|
57
|
+
* @param {string} opts.dataDir
|
|
58
|
+
* @param {string} [opts.runtimeDir]
|
|
59
|
+
* @param {string} [opts.nginxDir] From Settings — never a default
|
|
60
|
+
* @param {string} [opts.sslDir] From Settings — never a default
|
|
61
|
+
* @returns {Array<{ path: string, access: string, why: string }>}
|
|
62
|
+
*/
|
|
63
|
+
export function requiredGrants({ dataDir, runtimeDir = null, nginxDir = null, sslDir = null } = {}) {
|
|
64
|
+
const grants = [
|
|
65
|
+
{ path: dataDir, access: ACCESS.MODIFY, why: 'the database, the daemon lock and logs' },
|
|
66
|
+
];
|
|
67
|
+
|
|
68
|
+
if (runtimeDir) {
|
|
69
|
+
grants.push({ path: runtimeDir, access: ACCESS.READ_EXECUTE, why: 'running the pinned node.exe' });
|
|
70
|
+
}
|
|
71
|
+
if (nginxDir) {
|
|
72
|
+
grants.push({ path: nginxDir, access: ACCESS.MODIFY, why: 'writing and reloading domain configs' });
|
|
73
|
+
}
|
|
74
|
+
if (sslDir) {
|
|
75
|
+
grants.push({ path: sslDir, access: ACCESS.MODIFY, why: 'writing renewed certificates' });
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// A user can point nginxDir and sslDir at the same tree, or nest one inside
|
|
79
|
+
// the other. Granting twice is harmless but produces confusing output, so
|
|
80
|
+
// duplicates are collapsed — keeping the broader access where they differ.
|
|
81
|
+
const seen = new Map();
|
|
82
|
+
for (const grant of grants) {
|
|
83
|
+
if (!grant.path) continue;
|
|
84
|
+
const key = path.resolve(grant.path).toLowerCase();
|
|
85
|
+
const existing = seen.get(key);
|
|
86
|
+
if (!existing) {
|
|
87
|
+
seen.set(key, grant);
|
|
88
|
+
} else if (existing.access === ACCESS.READ_EXECUTE && grant.access === ACCESS.MODIFY) {
|
|
89
|
+
seen.set(key, grant);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return [...seen.values()];
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* The icacls command for one grant.
|
|
98
|
+
*
|
|
99
|
+
* The account name is quoted because it contains a backslash and a space;
|
|
100
|
+
* `icacls` is given the path quoted for the same reason.
|
|
101
|
+
*
|
|
102
|
+
* @param {string} dir
|
|
103
|
+
* @param {string} access
|
|
104
|
+
* @param {string} [account]
|
|
105
|
+
* @returns {string}
|
|
106
|
+
*/
|
|
107
|
+
export function buildGrantCommand(dir, access, account = SERVICE_ACCOUNT) {
|
|
108
|
+
return `icacls "${dir}" /grant "${account}:(OI)(CI)${access}" /T /C /Q`;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** The icacls command that removes every grant for the account. */
|
|
112
|
+
export function buildRevokeCommand(dir, account = SERVICE_ACCOUNT) {
|
|
113
|
+
return `icacls "${dir}" /remove:g "${account}" /T /C /Q`;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Applies every required grant.
|
|
118
|
+
*
|
|
119
|
+
* `run` is injectable so tests drive a stub and assert on the exact commands
|
|
120
|
+
* rather than touching real ACLs. This is not optional politeness: applying a
|
|
121
|
+
* real grant during a test run would change permissions on the developer's own
|
|
122
|
+
* nginx directory.
|
|
123
|
+
*
|
|
124
|
+
* @param {Object} opts
|
|
125
|
+
* @param {Array} opts.grants From requiredGrants()
|
|
126
|
+
* @param {Function} [opts.run]
|
|
127
|
+
* @param {(line: string) => void} [opts.log]
|
|
128
|
+
* @returns {Promise<{ ok: boolean, applied: Array, failed: Array, needsElevation: boolean }>}
|
|
129
|
+
*/
|
|
130
|
+
export async function applyGrants({ grants, run = shellRun, log = () => {} } = {}) {
|
|
131
|
+
const applied = [];
|
|
132
|
+
const failed = [];
|
|
133
|
+
let needsElevation = false;
|
|
134
|
+
|
|
135
|
+
for (const grant of grants) {
|
|
136
|
+
if (!grant.path) continue;
|
|
137
|
+
|
|
138
|
+
if (!fs.existsSync(grant.path)) {
|
|
139
|
+
// A missing directory is not a failure to grant: nginxDir can legitimately
|
|
140
|
+
// point somewhere not yet created, and `repair` will pick it up later.
|
|
141
|
+
// Granting on a path we then create would also be wrong, because we would
|
|
142
|
+
// be choosing its location rather than the user.
|
|
143
|
+
failed.push({ ...grant, reason: 'the directory does not exist yet' });
|
|
144
|
+
log(` ${grant.path}: skipped, does not exist`);
|
|
145
|
+
continue;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
const result = await run(buildGrantCommand(grant.path, grant.access), { timeout: 60_000 });
|
|
149
|
+
const output = `${result.stdout ?? ''}\n${result.stderr ?? ''}`;
|
|
150
|
+
|
|
151
|
+
if (result.success) {
|
|
152
|
+
applied.push(grant);
|
|
153
|
+
log(` ${grant.path}: ${grant.access} granted (${grant.why})`);
|
|
154
|
+
continue;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
if (/access is denied/i.test(output)) needsElevation = true;
|
|
158
|
+
failed.push({ ...grant, reason: output.trim().slice(0, 300) || 'icacls failed with no output' });
|
|
159
|
+
log(` ${grant.path}: FAILED`);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
return { ok: failed.length === 0, applied, failed, needsElevation };
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/** Removes the account's grants. Used by uninstall. */
|
|
166
|
+
export async function revokeGrants({ paths, run = shellRun, log = () => {} } = {}) {
|
|
167
|
+
const removed = [];
|
|
168
|
+
const failed = [];
|
|
169
|
+
|
|
170
|
+
for (const dir of paths) {
|
|
171
|
+
if (!dir || !fs.existsSync(dir)) continue;
|
|
172
|
+
|
|
173
|
+
const result = await run(buildRevokeCommand(dir), { timeout: 60_000 });
|
|
174
|
+
if (result.success) {
|
|
175
|
+
removed.push(dir);
|
|
176
|
+
log(` ${dir}: grants removed`);
|
|
177
|
+
} else {
|
|
178
|
+
failed.push({ path: dir, reason: `${result.stdout ?? ''}${result.stderr ?? ''}`.trim().slice(0, 300) });
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
return { ok: failed.length === 0, removed, failed };
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Whether the paths granted at install still match the live Settings.
|
|
187
|
+
*
|
|
188
|
+
* This is the check that catches the realistic failure: the user installs the
|
|
189
|
+
* service, then later changes `nginxDir` in Settings, and the service can no
|
|
190
|
+
* longer read the directory it is being asked to manage. Nothing detects that
|
|
191
|
+
* on its own, so it is compared explicitly and surfaced by `status`.
|
|
192
|
+
*
|
|
193
|
+
* @param {Object} opts
|
|
194
|
+
* @param {string[]} opts.granted Paths recorded at install time
|
|
195
|
+
* @param {Array} opts.required From requiredGrants() with the current config
|
|
196
|
+
* @returns {{ ok: boolean, missing: string[], stale: string[], reason: string|null }}
|
|
197
|
+
*/
|
|
198
|
+
export function compareGrants({ granted = [], required = [] } = {}) {
|
|
199
|
+
const norm = (p) => path.resolve(p).toLowerCase();
|
|
200
|
+
const grantedSet = new Set(granted.filter(Boolean).map(norm));
|
|
201
|
+
const requiredSet = new Set(required.map((g) => g.path).filter(Boolean).map(norm));
|
|
202
|
+
|
|
203
|
+
const missing = [...requiredSet].filter((p) => !grantedSet.has(p));
|
|
204
|
+
const stale = [...grantedSet].filter((p) => !requiredSet.has(p));
|
|
205
|
+
|
|
206
|
+
if (missing.length === 0 && stale.length === 0) {
|
|
207
|
+
return { ok: true, missing: [], stale: [], reason: null };
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
const parts = [];
|
|
211
|
+
if (missing.length) {
|
|
212
|
+
parts.push(
|
|
213
|
+
`the service has no access to ${missing.join(', ')} — a path in Settings changed since it was installed`,
|
|
214
|
+
);
|
|
215
|
+
}
|
|
216
|
+
if (stale.length) {
|
|
217
|
+
parts.push(`it still has access to ${stale.join(', ')}, which it no longer needs`);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
return {
|
|
221
|
+
ok: false,
|
|
222
|
+
missing,
|
|
223
|
+
stale,
|
|
224
|
+
reason: `${parts.join('; ')}. Run \`ezz service repair\` in an Administrator PowerShell to fix the permissions.`,
|
|
225
|
+
};
|
|
226
|
+
}
|
|
@@ -0,0 +1,345 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* core/service/dataDir.js
|
|
3
|
+
*
|
|
4
|
+
* Moving the data directory between `%APPDATA%\easy-devops` (per user) and
|
|
5
|
+
* `C:\ProgramData\easy-devops` (machine-wide, for the Windows service).
|
|
6
|
+
*
|
|
7
|
+
* ── Why the data has to move at all ────────────────────────────────────────
|
|
8
|
+
*
|
|
9
|
+
* The service runs as the virtual account `NT SERVICE\EasyDevOps`. That
|
|
10
|
+
* account has no access to the installing user's `%APPDATA%`, and its own
|
|
11
|
+
* profile is not that user's — so if the data stayed per-user, the service and
|
|
12
|
+
* the CLI would be looking at two different databases and neither would see
|
|
13
|
+
* the other's domains. ProgramData is the one location both can share.
|
|
14
|
+
*
|
|
15
|
+
* ── Why it is a copy-verify-then-remove, not a rename ──────────────────────
|
|
16
|
+
*
|
|
17
|
+
* `fs.renameSync` across volumes fails with EXDEV, and `%APPDATA%` and
|
|
18
|
+
* ProgramData can genuinely be on different volumes (a redirected profile, a
|
|
19
|
+
* small system disk). More importantly, a rename that fails halfway leaves the
|
|
20
|
+
* user with no complete copy anywhere. So: copy everything, verify it, keep a
|
|
21
|
+
* timestamped backup of the source, and only then remove the original.
|
|
22
|
+
*
|
|
23
|
+
* ── Why the database must not be open ──────────────────────────────────────
|
|
24
|
+
*
|
|
25
|
+
* SQLite keeps a WAL alongside the main file. Copying the main file while
|
|
26
|
+
* another process is mid-transaction can produce a database that opens but is
|
|
27
|
+
* missing the most recent writes. Every caller here is `ezz service
|
|
28
|
+
* install/uninstall`, which runs before the daemon is started or after it is
|
|
29
|
+
* stopped; `assertNotInUse()` makes that a checked precondition rather than an
|
|
30
|
+
* assumption.
|
|
31
|
+
*/
|
|
32
|
+
|
|
33
|
+
import fs from 'fs';
|
|
34
|
+
import path from 'path';
|
|
35
|
+
|
|
36
|
+
import {
|
|
37
|
+
isWindows,
|
|
38
|
+
programDataDir,
|
|
39
|
+
serverModeMarkerPath,
|
|
40
|
+
resetDataDirCache,
|
|
41
|
+
getDataDir,
|
|
42
|
+
} from '../platform.js';
|
|
43
|
+
import { inspect } from '../daemon/lock.js';
|
|
44
|
+
|
|
45
|
+
/** Per-user directory, computed without the Server-mode marker in the way. */
|
|
46
|
+
export function userDataDir() {
|
|
47
|
+
return isWindows
|
|
48
|
+
? path.join(process.env.APPDATA || '', 'easy-devops')
|
|
49
|
+
: path.join(process.env.HOME || '', '.config', 'easy-devops');
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Refuses to move the data while something is using it.
|
|
54
|
+
*
|
|
55
|
+
* @param {string} dir
|
|
56
|
+
* @returns {{ ok: true } | { ok: false, reason: string }}
|
|
57
|
+
*/
|
|
58
|
+
export function assertNotInUse(dir) {
|
|
59
|
+
const state = inspect({ dataDir: dir });
|
|
60
|
+
if (state.running) {
|
|
61
|
+
return {
|
|
62
|
+
ok: false,
|
|
63
|
+
reason: `a daemon is running against ${dir} (pid ${state.lock?.pid}). `
|
|
64
|
+
+ 'Stop the service first: ezz service uninstall, or stop it in services.msc.',
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
return { ok: true };
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Copies a directory tree.
|
|
72
|
+
*
|
|
73
|
+
* `fs.cpSync` with `recursive` exists in Node 16.7+, which is inside the
|
|
74
|
+
* supported floor, and it preserves timestamps. It is used rather than a
|
|
75
|
+
* hand-rolled walk so symlinks and empty directories behave predictably.
|
|
76
|
+
*/
|
|
77
|
+
function copyTree(from, to) {
|
|
78
|
+
fs.mkdirSync(to, { recursive: true });
|
|
79
|
+
fs.cpSync(from, to, { recursive: true, force: true, errorOnExist: false });
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** Every file in a tree, as paths relative to its root. */
|
|
83
|
+
function listFiles(dir, base = dir, out = []) {
|
|
84
|
+
let entries;
|
|
85
|
+
try {
|
|
86
|
+
entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
87
|
+
} catch {
|
|
88
|
+
return out;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
for (const entry of entries) {
|
|
92
|
+
const full = path.join(dir, entry.name);
|
|
93
|
+
if (entry.isDirectory()) listFiles(full, base, out);
|
|
94
|
+
else out.push(path.relative(base, full));
|
|
95
|
+
}
|
|
96
|
+
return out;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Checks that every file in `from` arrived in `to` with the same size.
|
|
101
|
+
*
|
|
102
|
+
* Size rather than a hash: these are SQLite files and session blobs that can
|
|
103
|
+
* be tens of megabytes, and the failure this guards against is a truncated or
|
|
104
|
+
* missing copy, which size catches. A silent bit flip is not the risk here.
|
|
105
|
+
*
|
|
106
|
+
* @returns {{ ok: boolean, missing: string[], differing: string[] }}
|
|
107
|
+
*/
|
|
108
|
+
export function verifyCopy(from, to) {
|
|
109
|
+
const missing = [];
|
|
110
|
+
const differing = [];
|
|
111
|
+
|
|
112
|
+
for (const rel of listFiles(from)) {
|
|
113
|
+
const src = path.join(from, rel);
|
|
114
|
+
const dst = path.join(to, rel);
|
|
115
|
+
|
|
116
|
+
if (!fs.existsSync(dst)) {
|
|
117
|
+
missing.push(rel);
|
|
118
|
+
continue;
|
|
119
|
+
}
|
|
120
|
+
try {
|
|
121
|
+
if (fs.statSync(src).size !== fs.statSync(dst).size) differing.push(rel);
|
|
122
|
+
} catch {
|
|
123
|
+
differing.push(rel);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
return { ok: missing.length === 0 && differing.length === 0, missing, differing };
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** `<dir>.backup-<timestamp>`, beside the original so it is on the same volume. */
|
|
131
|
+
export function backupPathFor(dir, now = new Date()) {
|
|
132
|
+
const stamp = now.toISOString().replace(/[:.]/g, '-').slice(0, 19);
|
|
133
|
+
return `${dir}.backup-${stamp}`;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Moves the data directory, keeping a backup of the source.
|
|
138
|
+
*
|
|
139
|
+
* Order: copy → verify → back up the source → remove the source. The backup is
|
|
140
|
+
* a RENAME of the original, which is why it is created last and placed beside
|
|
141
|
+
* it: renaming is atomic within a volume, so at no point does the only copy of
|
|
142
|
+
* the data not exist.
|
|
143
|
+
*
|
|
144
|
+
* @param {Object} opts
|
|
145
|
+
* @param {string} opts.from
|
|
146
|
+
* @param {string} opts.to
|
|
147
|
+
* @param {boolean} [opts.keepBackup] Leave the backup in place (default true)
|
|
148
|
+
* @param {(line: string) => void} [opts.log]
|
|
149
|
+
* @returns {{ ok: true, moved: boolean, backup: string|null, reason?: string }
|
|
150
|
+
* | { ok: false, reason: string }}
|
|
151
|
+
*/
|
|
152
|
+
export function moveDataDir({ from, to, keepBackup = true, log = () => {} }) {
|
|
153
|
+
if (!fs.existsSync(from)) {
|
|
154
|
+
// Nothing to move is a success: a fresh install has no per-user data, and
|
|
155
|
+
// the service simply starts with an empty ProgramData directory.
|
|
156
|
+
return { ok: true, moved: false, backup: null, reason: `${from} does not exist` };
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
if (path.resolve(from).toLowerCase() === path.resolve(to).toLowerCase()) {
|
|
160
|
+
return { ok: true, moved: false, backup: null, reason: 'source and destination are the same' };
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
const busy = assertNotInUse(from);
|
|
164
|
+
if (!busy.ok) return { ok: false, reason: busy.reason };
|
|
165
|
+
|
|
166
|
+
// A destination that already holds a database is not overwritten: that is
|
|
167
|
+
// either a previous install's data or a second machine's, and silently
|
|
168
|
+
// replacing it would destroy whichever one the user actually wanted.
|
|
169
|
+
const destDb = path.join(to, 'easy-devops.sqlite');
|
|
170
|
+
if (fs.existsSync(destDb)) {
|
|
171
|
+
return {
|
|
172
|
+
ok: false,
|
|
173
|
+
reason: `${to} already contains a database. Move or remove it first if you want to replace it; `
|
|
174
|
+
+ 'nothing has been changed.',
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
log(`Copying ${from} → ${to}`);
|
|
179
|
+
try {
|
|
180
|
+
copyTree(from, to);
|
|
181
|
+
} catch (err) {
|
|
182
|
+
return { ok: false, reason: `copy failed: ${err.message}. Nothing was removed.` };
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
const verified = verifyCopy(from, to);
|
|
186
|
+
if (!verified.ok) {
|
|
187
|
+
return {
|
|
188
|
+
ok: false,
|
|
189
|
+
reason: 'the copy is incomplete, so the original was left untouched: '
|
|
190
|
+
+ [
|
|
191
|
+
verified.missing.length ? `missing ${verified.missing.length} file(s): ${verified.missing.slice(0, 5).join(', ')}` : '',
|
|
192
|
+
verified.differing.length ? `${verified.differing.length} file(s) differ in size: ${verified.differing.slice(0, 5).join(', ')}` : '',
|
|
193
|
+
].filter(Boolean).join('; '),
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
log('Copy verified.');
|
|
197
|
+
|
|
198
|
+
const backup = backupPathFor(from);
|
|
199
|
+
try {
|
|
200
|
+
fs.renameSync(from, backup);
|
|
201
|
+
log(`Original kept at ${backup}`);
|
|
202
|
+
} catch (err) {
|
|
203
|
+
// The copy is good and verified, so the data is safe in two places. Not
|
|
204
|
+
// being able to rename the original is worth reporting but not worth
|
|
205
|
+
// undoing a verified migration over.
|
|
206
|
+
return {
|
|
207
|
+
ok: true,
|
|
208
|
+
moved: true,
|
|
209
|
+
backup: null,
|
|
210
|
+
reason: `the data was copied and verified, but the original could not be renamed: ${err.message}. `
|
|
211
|
+
+ `It is still at ${from} — remove it by hand once you are satisfied.`,
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
if (!keepBackup) {
|
|
216
|
+
try {
|
|
217
|
+
fs.rmSync(backup, { recursive: true, force: true });
|
|
218
|
+
log('Backup removed as requested.');
|
|
219
|
+
return { ok: true, moved: true, backup: null };
|
|
220
|
+
} catch (err) {
|
|
221
|
+
return { ok: true, moved: true, backup, reason: `the backup could not be removed: ${err.message}` };
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
return { ok: true, moved: true, backup };
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* Switches this machine into Server mode: data under ProgramData, marker
|
|
230
|
+
* written, per-user data migrated once.
|
|
231
|
+
*
|
|
232
|
+
* Idempotent. Running it again when the marker already exists re-checks the
|
|
233
|
+
* data rather than migrating a second time, because the per-user directory at
|
|
234
|
+
* that point is a backup, not the live data.
|
|
235
|
+
*
|
|
236
|
+
* @param {Object} [opts]
|
|
237
|
+
* @param {(line: string) => void} [opts.log]
|
|
238
|
+
* @returns {{ ok: boolean, dataDir: string, migrated: boolean, backup: string|null, reason?: string }}
|
|
239
|
+
*/
|
|
240
|
+
export function enableServerMode({ log = () => {} } = {}) {
|
|
241
|
+
if (!isWindows) {
|
|
242
|
+
return { ok: false, dataDir: getDataDir(), migrated: false, backup: null, reason: 'Server mode is Windows-only' };
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
const target = programDataDir();
|
|
246
|
+
const marker = serverModeMarkerPath();
|
|
247
|
+
|
|
248
|
+
try {
|
|
249
|
+
fs.mkdirSync(target, { recursive: true });
|
|
250
|
+
} catch (err) {
|
|
251
|
+
return {
|
|
252
|
+
ok: false,
|
|
253
|
+
dataDir: target,
|
|
254
|
+
migrated: false,
|
|
255
|
+
backup: null,
|
|
256
|
+
reason: `could not create ${target}: ${err.message}. `
|
|
257
|
+
+ 'Creating a directory under ProgramData needs an elevated terminal.',
|
|
258
|
+
};
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
if (fs.existsSync(marker)) {
|
|
262
|
+
log(`Already in Server mode: ${target}`);
|
|
263
|
+
return { ok: true, dataDir: target, migrated: false, backup: null, reason: 'the marker already exists' };
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
const moved = moveDataDir({ from: userDataDir(), to: target, log });
|
|
267
|
+
if (!moved.ok) {
|
|
268
|
+
// The marker is NOT written when the migration failed. Writing it would
|
|
269
|
+
// point every process at a directory that does not hold the user's data.
|
|
270
|
+
return { ok: false, dataDir: target, migrated: false, backup: null, reason: moved.reason };
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
try {
|
|
274
|
+
fs.writeFileSync(
|
|
275
|
+
marker,
|
|
276
|
+
`${JSON.stringify({ enabledAt: new Date().toISOString(), migratedFrom: userDataDir(), backup: moved.backup })}\n`,
|
|
277
|
+
'utf8',
|
|
278
|
+
);
|
|
279
|
+
} catch (err) {
|
|
280
|
+
return {
|
|
281
|
+
ok: false,
|
|
282
|
+
dataDir: target,
|
|
283
|
+
migrated: moved.moved,
|
|
284
|
+
backup: moved.backup,
|
|
285
|
+
reason: `the data was migrated but the marker could not be written: ${err.message}`,
|
|
286
|
+
};
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
resetDataDirCache();
|
|
290
|
+
log(`Server mode enabled. Data directory: ${target}`);
|
|
291
|
+
|
|
292
|
+
return { ok: true, dataDir: target, migrated: moved.moved, backup: moved.backup, reason: moved.reason };
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* Leaves Server mode, moving the data back to `%APPDATA%`.
|
|
297
|
+
*
|
|
298
|
+
* Only ever called when the user explicitly chooses it at uninstall —
|
|
299
|
+
* decision 5. The default on uninstall is to KEEP the server location, because
|
|
300
|
+
* the ProgramData directory holds the live data and silently moving it back
|
|
301
|
+
* would be a surprise.
|
|
302
|
+
*
|
|
303
|
+
* @param {Object} [opts]
|
|
304
|
+
* @param {(line: string) => void} [opts.log]
|
|
305
|
+
* @returns {{ ok: boolean, dataDir: string, moved: boolean, backup: string|null, reason?: string }}
|
|
306
|
+
*/
|
|
307
|
+
export function disableServerMode({ log = () => {} } = {}) {
|
|
308
|
+
if (!isWindows) {
|
|
309
|
+
return { ok: false, dataDir: getDataDir(), moved: false, backup: null, reason: 'Server mode is Windows-only' };
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
const source = programDataDir();
|
|
313
|
+
const target = userDataDir();
|
|
314
|
+
const marker = serverModeMarkerPath();
|
|
315
|
+
|
|
316
|
+
if (!fs.existsSync(marker)) {
|
|
317
|
+
return { ok: true, dataDir: target, moved: false, backup: null, reason: 'not in Server mode' };
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
// The marker is removed FIRST so that a failure halfway cannot leave every
|
|
321
|
+
// process pointed at a directory whose contents have already been moved
|
|
322
|
+
// away. If the move then fails, the data is still in ProgramData and the
|
|
323
|
+
// reason says so — recoverable by hand, and nothing is lost.
|
|
324
|
+
try {
|
|
325
|
+
fs.rmSync(marker, { force: true });
|
|
326
|
+
} catch (err) {
|
|
327
|
+
return { ok: false, dataDir: source, moved: false, backup: null, reason: `could not remove ${marker}: ${err.message}` };
|
|
328
|
+
}
|
|
329
|
+
resetDataDirCache();
|
|
330
|
+
|
|
331
|
+
const moved = moveDataDir({ from: source, to: target, log });
|
|
332
|
+
if (!moved.ok) {
|
|
333
|
+
return {
|
|
334
|
+
ok: false,
|
|
335
|
+
dataDir: source,
|
|
336
|
+
moved: false,
|
|
337
|
+
backup: null,
|
|
338
|
+
reason: `${moved.reason} Server mode is now off, so the data in ${source} is no longer being read — `
|
|
339
|
+
+ `move it to ${target} by hand.`,
|
|
340
|
+
};
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
log(`Server mode disabled. Data directory: ${target}`);
|
|
344
|
+
return { ok: true, dataDir: target, moved: moved.moved, backup: moved.backup, reason: moved.reason };
|
|
345
|
+
}
|