@animastor/installer 0.1.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.
Files changed (37) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +66 -0
  3. package/ai/install-manifests/audio/qwen-tts.json +445 -0
  4. package/ai/install-manifests/image/qwen-image.json +465 -0
  5. package/ai/install-manifests/video/ltx-2.3.json +892 -0
  6. package/package.json +42 -0
  7. package/src/installer/cli.js +1067 -0
  8. package/src/installer/compatibility-resolver.js +1287 -0
  9. package/src/installer/download-planner.js +172 -0
  10. package/src/installer/engine/comfyui.js +508 -0
  11. package/src/installer/engine/downloader.js +514 -0
  12. package/src/installer/engine/engine.js +1180 -0
  13. package/src/installer/engine/io.js +437 -0
  14. package/src/installer/engine/logger.js +75 -0
  15. package/src/installer/engine/nodes.js +146 -0
  16. package/src/installer/engine/prereq.js +399 -0
  17. package/src/installer/engine/probe.js +349 -0
  18. package/src/installer/engine/progress.js +350 -0
  19. package/src/installer/engine/state.js +170 -0
  20. package/src/installer/engine/term.js +298 -0
  21. package/src/installer/engine/worker.js +430 -0
  22. package/src/installer/engine/workflows.js +153 -0
  23. package/src/installer/index.js +40 -0
  24. package/src/installer/install-manifest.js +376 -0
  25. package/src/installer/install-plan.js +741 -0
  26. package/src/installer/management.js +489 -0
  27. package/src/installer/platform/deployment/docker.js +78 -0
  28. package/src/installer/platform/deployment/native.js +36 -0
  29. package/src/installer/platform/index.js +165 -0
  30. package/src/installer/platform/linux.js +341 -0
  31. package/src/installer/platform/windows.js +360 -0
  32. package/src/installer/safety-rules.js +156 -0
  33. package/src/installer/setup-contract.js +1401 -0
  34. package/src/installer/uninstaller.js +572 -0
  35. package/src/installer/verification-report.js +220 -0
  36. package/src/installer/worker-bundle-source.js +64 -0
  37. package/src/installer/workflow-artifacts.js +152 -0
@@ -0,0 +1,399 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * Host prerequisite checks — run BEFORE any heavy installer mutation
5
+ * (venv creation, ComfyUI clone, multi-GB model downloads).
6
+ *
7
+ * Detects the Debian/Ubuntu failure mode where `python3 -m venv` fails
8
+ * because `ensurepip` is unavailable (python3.X-venv not installed) and
9
+ * produces a precise remediation command for the detected Python version.
10
+ *
11
+ * Detection strategy: the RELIABLE check on Debian/Ubuntu is creating a
12
+ * throwaway venv — `python3 -m ensurepip` is deliberately disabled for the
13
+ * system python there, so a cheap module check would produce false failures
14
+ * on healthy hosts. The probe venv is created under a fixed temp root and
15
+ * always cleaned up.
16
+ *
17
+ * "Inconclusive" semantics: when the venv command reports success but the
18
+ * probe directory did not materialize (mocked io in tests), the check
19
+ * passes without a deep verdict — the runtime step itself re-validates the
20
+ * real venv afterwards.
21
+ */
22
+
23
+ const path = require('path');
24
+ const platforms = require('../platform');
25
+
26
+ /**
27
+ * Platform adapter for one call (explicit override wins over auto-detect).
28
+ * Package remediation commands and the venv layout are platform concerns.
29
+ */
30
+ function adapterOf(opts = {}) {
31
+ return (opts && opts.platformAdapter) || platforms.getPlatformAdapter();
32
+ }
33
+
34
+ /**
35
+ * Structured prerequisite failure. Carries the host package and the exact
36
+ * remediation command so CLI/engine can print a short, actionable message.
37
+ */
38
+ class PrerequisiteError extends Error {
39
+ /**
40
+ * @param {object} args { code, message, summary, hostPackage, remediationCommand }
41
+ */
42
+ constructor({ code, message, summary, hostPackage = null, remediationCommand = null }) {
43
+ super(message);
44
+ this.name = 'PrerequisiteError';
45
+ this.code = code;
46
+ this.summary = summary || message;
47
+ this.hostPackage = hostPackage;
48
+ this.remediationCommand = remediationCommand;
49
+ }
50
+
51
+ /** Render the short user-facing remediation block (requirement: 4 lines). */
52
+ remediationLines() {
53
+ return renderRemediation(this);
54
+ }
55
+ }
56
+
57
+ /** Debian/Ubuntu venv package name for a Python version, e.g. "3.10.12" → "python3.10-venv". */
58
+ function debianVenvPackage(pythonVersion) {
59
+ const m = /(\d+)\.(\d+)/.exec(String(pythonVersion || ''));
60
+ return m ? `python${m[1]}.${m[2]}-venv` : 'python3-venv';
61
+ }
62
+
63
+ /**
64
+ * Host package install command — delegated to the platform adapter
65
+ * (Debian/Ubuntu apt on Linux; winget/choco hints on Windows).
66
+ * The explicit `platform` argument keeps the historical apt behaviour for
67
+ * tests and Linux-only remediation classifiers.
68
+ */
69
+ function aptCommand(pkg, platform = 'linux') {
70
+ return platforms.getPlatformAdapter(platform).hostPackageCommand(pkg);
71
+ }
72
+
73
+ /**
74
+ * Classify a failed `python3 -m venv` run.
75
+ * The remediation command comes from the platform adapter when one is
76
+ * provided (apt on Linux, package-manager hints on Windows).
77
+ * @returns {{ code, summary, hostPackage, remediationCommand }}
78
+ */
79
+ function classifyVenvCreateFailure(output, pythonVersion = null, platformAdapter = null) {
80
+ const pkgCmd = (pkg) => (platformAdapter ? platformAdapter.hostPackageCommand(pkg) : aptCommand(pkg));
81
+ const text = String(output || '');
82
+ if (/ensurepip is not available|python3(\.\d+)?-venv|ensurepip/i.test(text)) {
83
+ const pkg = debianVenvPackage(pythonVersion);
84
+ return {
85
+ code: 'MISSING_VENV_PACKAGE',
86
+ summary: 'the Python venv prerequisite is missing',
87
+ hostPackage: pkg,
88
+ remediationCommand: pkgCmd(pkg),
89
+ };
90
+ }
91
+ const pkg = debianVenvPackage(pythonVersion);
92
+ return {
93
+ code: 'VENV_CREATE_FAILED',
94
+ summary: 'the Python venv could not be created on this host',
95
+ hostPackage: pkg,
96
+ remediationCommand: pkgCmd(pkg),
97
+ };
98
+ }
99
+
100
+ /** Build the remediation payload stored on the engine result. */
101
+ function remediationPayload(failure) {
102
+ return {
103
+ code: failure.code,
104
+ summary: failure.summary,
105
+ package: (failure.remediation && failure.remediation.package) || failure.hostPackage || null,
106
+ command: (failure.remediation && failure.remediation.command) || failure.remediationCommand || null,
107
+ };
108
+ }
109
+
110
+ /** Render the short user-facing remediation block for a failure object. */
111
+ function renderRemediation(failure) {
112
+ const lines = [];
113
+ lines.push(`Installation stopped because ${failure.summary || 'a host prerequisite is missing'}.`);
114
+ const pkg = (failure.remediation && failure.remediation.package) || failure.hostPackage;
115
+ const cmd = (failure.remediation && failure.remediation.command) || failure.remediationCommand;
116
+ if (pkg) lines.push(`Required host package: ${pkg}`);
117
+ if (cmd) lines.push(`Install it with: ${cmd}`);
118
+ lines.push('Then re-run the installer.');
119
+ return lines;
120
+ }
121
+
122
+ /**
123
+ * Classify an existing venv directory WITHOUT mutating it.
124
+ * The interpreter layout inside the venv (bin/python vs Scripts/python.exe)
125
+ * is a platform concern (platform adapter).
126
+ * @returns {{ state: 'missing'|'incomplete'|'has-python', reason?: string }}
127
+ */
128
+ function classifyVenvDir(io, venvDir, platformAdapter = null) {
129
+ if (!io.fs.existsSync(venvDir)) return { state: 'missing' };
130
+ const py = adapterOf({ platformAdapter }).venvPythonBin(venvDir);
131
+ if (!io.fs.existsSync(py)) {
132
+ return { state: 'incomplete', reason: `directory exists but ${py} is missing (broken/incomplete venv)` };
133
+ }
134
+ return { state: 'has-python' };
135
+ }
136
+
137
+ /**
138
+ * Probe python3 and (deep) the actual ability to create a working venv with
139
+ * a functioning pip — BEFORE any real mutation.
140
+ *
141
+ * @param {object} io io adapter
142
+ * @param {object} opts { python = 'python3', tmpRoot = null, deep = true,
143
+ * existingVenvDir = null }
144
+ * existingVenvDir: when a working venv already exists at the install
145
+ * target, nothing will need to CREATE a venv — its working pip satisfies
146
+ * the prerequisite without probing host venv creation.
147
+ * @returns {{ ok: true, python_version, deep_checked, existing_venv_usable? } |
148
+ * { ok: false, failure: { code, summary, message, remediation: { package, command } } }}
149
+ */
150
+ function checkPythonPrerequisites(io, opts = {}) {
151
+ const { python = 'python3', tmpRoot = null, deep = true, existingVenvDir = null, platformAdapter = null } = opts;
152
+ const adapter = adapterOf({ platformAdapter });
153
+ const os = require('os');
154
+
155
+ const versionRes = io.exec(python, ['--version']);
156
+ if (versionRes.code !== 0) {
157
+ return {
158
+ ok: false,
159
+ failure: {
160
+ code: 'NO_PYTHON',
161
+ summary: 'Python 3 is not available on this host',
162
+ message: `${python} is not usable (code ${versionRes.code})`,
163
+ remediation: { package: 'python3', command: adapter.hostPackageCommand('python3') },
164
+ },
165
+ };
166
+ }
167
+ const versionMatch = /Python\s+([0-9][0-9.]*)/.exec(String(versionRes.stdout) + String(versionRes.stderr));
168
+ const pythonVersion = versionMatch ? versionMatch[1] : null;
169
+
170
+ // A working existing venv at the target makes venv-creation capability
171
+ // irrelevant (the installer reuses it as-is).
172
+ if (existingVenvDir) {
173
+ const cls = classifyVenvDir(io, existingVenvDir, adapter);
174
+ if (cls.state === 'has-python') {
175
+ const existingPy = adapter.venvPythonBin(existingVenvDir);
176
+ const pipCheck = io.exec(existingPy, ['-m', 'pip', '--version']);
177
+ if (pipCheck.code === 0) {
178
+ return { ok: true, python_version: pythonVersion, deep_checked: false, existing_venv_usable: true };
179
+ }
180
+ }
181
+ }
182
+
183
+ if (!deep) {
184
+ return { ok: true, python_version: pythonVersion, deep_checked: false };
185
+ }
186
+
187
+ const root = tmpRoot || path.join(os.tmpdir(), 'animastor-installer-prereq');
188
+ const probeVenv = path.join(root, 'venv');
189
+
190
+ const cleanup = () => {
191
+ try { if (io.fs.existsSync(root)) io.fs.rmSync(root, { recursive: true, force: true }); } catch (_) { /* best effort */ }
192
+ };
193
+ cleanup();
194
+
195
+ const created = io.exec(python, ['-m', 'venv', probeVenv]);
196
+ if (created.code !== 0) {
197
+ cleanup();
198
+ const f = classifyVenvCreateFailure(String(created.stderr || created.stdout || created.error || ''), pythonVersion, adapter);
199
+ return {
200
+ ok: false,
201
+ failure: {
202
+ code: f.code,
203
+ summary: f.summary,
204
+ message: `python3 -m venv probe failed (code ${created.code}): ${String(created.stderr || created.stdout || '').slice(-400)}`,
205
+ remediation: { package: f.hostPackage, command: f.remediationCommand },
206
+ },
207
+ };
208
+ }
209
+
210
+ if (!io.fs.existsSync(probeVenv)) {
211
+ // Success reported but nothing materialized (mocked io) — inconclusive.
212
+ return { ok: true, python_version: pythonVersion, deep_checked: false };
213
+ }
214
+
215
+ const probePy = adapter.venvPythonBin(probeVenv);
216
+ if (!io.fs.existsSync(probePy)) {
217
+ cleanup();
218
+ const pkg = debianVenvPackage(pythonVersion);
219
+ return {
220
+ ok: false,
221
+ failure: {
222
+ code: 'VENV_INCOMPLETE',
223
+ summary: 'the Python venv prerequisite is missing (venv was created without a usable python)',
224
+ message: `venv probe produced no interpreter at ${probePy}`,
225
+ remediation: { package: pkg, command: adapter.hostPackageCommand(pkg) },
226
+ },
227
+ };
228
+ }
229
+
230
+ const pipCheck = io.exec(probePy, ['-m', 'pip', '--version']);
231
+ if (pipCheck.code !== 0) {
232
+ const boot = io.exec(probePy, ['-m', 'ensurepip', '--upgrade']);
233
+ cleanup();
234
+ if (boot.code !== 0) {
235
+ const pkg = debianVenvPackage(pythonVersion);
236
+ return {
237
+ ok: false,
238
+ failure: {
239
+ code: 'MISSING_ENSUREPIP',
240
+ summary: 'the Python venv prerequisite is missing (venv has neither pip nor ensurepip)',
241
+ message: `probe venv has no pip and ensurepip failed (code ${boot.code}): ${String(boot.stderr || boot.stdout || '').slice(-400)}`,
242
+ remediation: { package: pkg, command: adapter.hostPackageCommand(pkg) },
243
+ },
244
+ };
245
+ }
246
+ }
247
+
248
+ cleanup();
249
+ return { ok: true, python_version: pythonVersion, deep_checked: true };
250
+ }
251
+
252
+ // ---------------------------------------------------------------------------
253
+ // Ownership guard — sudo vs. normal user mixing
254
+ // ---------------------------------------------------------------------------
255
+
256
+ /** Current uid (null when unavailable, e.g. Windows). */
257
+ function currentUid() {
258
+ try {
259
+ return typeof process !== 'undefined' && typeof process.getuid === 'function' ? process.getuid() : null;
260
+ } catch (_) {
261
+ return null;
262
+ }
263
+ }
264
+
265
+ /** Current user's home directory. */
266
+ function currentHome() {
267
+ try {
268
+ return (process.env && process.env.HOME) || require('os').homedir();
269
+ } catch (_) {
270
+ return null;
271
+ }
272
+ }
273
+
274
+ function statUid(io, p) {
275
+ try {
276
+ const st = io.fs.statSync(p);
277
+ return typeof st.uid === 'number' ? st.uid : null;
278
+ } catch (_) {
279
+ return undefined; // absent
280
+ }
281
+ }
282
+
283
+ function pathInsideHome(p, home) {
284
+ if (!home) return false;
285
+ const h = path.resolve(home);
286
+ const r = path.resolve(p);
287
+ return r === h || r.startsWith(h + path.sep);
288
+ }
289
+
290
+ /**
291
+ * Detect ownership mixing that would lock a normal user out of their own
292
+ * home directory (the "ran it once, then re-ran with sudo" scenario).
293
+ *
294
+ * Violations:
295
+ * - root (uid 0) operating on paths (or their nearest existing ancestor)
296
+ * owned by a non-root user → would create root-owned files inside the
297
+ * user's home;
298
+ * - a non-root user operating on paths owned by a DIFFERENT uid;
299
+ * - the install state was created by another uid (resume after UID change).
300
+ *
301
+ * When uid information is unavailable (mocked/memory fs), the check passes —
302
+ * the runtime step re-validates the real venv afterwards.
303
+ *
304
+ * @returns {{ ok: boolean, violations: Array<{ path, kind, owner_uid?, message }> }}
305
+ */
306
+ function checkOwnership(io, { paths = [], home = null, currentUid: uidInput = null, stateUid = null } = {}) {
307
+ const uid = uidInput !== null && uidInput !== undefined ? uidInput : currentUid();
308
+ const violations = [];
309
+
310
+ if (uid === null || uid === undefined) return { ok: true, violations };
311
+
312
+ const describeOwner = (ownerUid) => (ownerUid === 0 ? 'root' : `uid ${ownerUid}`);
313
+
314
+ const checkPath = (p) => {
315
+ if (!p) return;
316
+ let abs = null;
317
+ try { abs = path.resolve(p); } catch (_) { return; }
318
+
319
+ const owner = statUid(io, abs);
320
+ if (owner === null) return; // uid info unavailable — cannot judge
321
+ if (owner !== undefined) {
322
+ if (uid === 0 && owner !== 0) {
323
+ violations.push({
324
+ path: abs,
325
+ kind: 'root-over-user-files',
326
+ owner_uid: owner,
327
+ message: `${abs} is owned by ${describeOwner(owner)} but the installer is running as root (sudo). `
328
+ + 'Continuing would create root-owned files inside the user\'s home and lock the normal user out of their installation. '
329
+ + 'Re-run as the owning user (e.g. `sudo -u $(stat -c %U ' + abs + ') ...`) or fix ownership first.',
330
+ });
331
+ } else if (uid !== 0 && owner !== uid) {
332
+ violations.push({
333
+ path: abs,
334
+ kind: 'foreign-user-files',
335
+ owner_uid: owner,
336
+ message: `${abs} is owned by ${describeOwner(owner)} but you are running as ${describeOwner(uid)} — the installer will not mix ownership in one installation.`,
337
+ });
338
+ }
339
+ return;
340
+ }
341
+ // path absent — check the nearest existing ancestor
342
+ let parent = path.dirname(abs);
343
+ while (parent && parent !== path.dirname(parent)) {
344
+ const parentOwner = statUid(io, parent);
345
+ if (parentOwner === null) return; // no uid info anywhere — pass
346
+ if (parentOwner !== undefined) {
347
+ if (uid === 0 && parentOwner !== 0) {
348
+ const where = pathInsideHome(abs, home) ? " (inside the user's home directory)" : '';
349
+ violations.push({
350
+ path: abs,
351
+ kind: 'root-into-user-home',
352
+ owner_uid: parentOwner,
353
+ message: `Running as root would create root-owned files under ${abs}, which lives inside ${describeOwner(parentOwner)}-owned ${parent}${where}. `
354
+ + 'Re-run the installer as the owning user instead of sudo.',
355
+ });
356
+ }
357
+ return;
358
+ }
359
+ parent = path.dirname(parent);
360
+ }
361
+ };
362
+
363
+ for (const p of paths) checkPath(p);
364
+
365
+ if (stateUid !== null && stateUid !== undefined && stateUid !== uid) {
366
+ violations.push({
367
+ path: null,
368
+ kind: 'state-owned-by-other-uid',
369
+ owner_uid: stateUid,
370
+ message: `The installation state was created by ${describeOwner(stateUid)} but the current process runs as ${describeOwner(uid)} — `
371
+ + 'resuming under a different uid would mix ownership. Run the installer as the original user (or clean up and reinstall).',
372
+ });
373
+ }
374
+
375
+ return { ok: violations.length === 0, violations };
376
+ }
377
+
378
+ /**
379
+ * Check for C build tools required by packages with native extensions
380
+ * (funasr, soundfile, etc.). Delegates to the platform adapter — the
381
+ * tool set and package names are OS-specific.
382
+ */
383
+ function checkBuildPrerequisites(io, { python = 'python3', log = null, platformAdapter = null } = {}) {
384
+ return adapterOf({ platformAdapter }).checkBuildPrerequisites(io, { python, log });
385
+ }
386
+
387
+ module.exports = {
388
+ PrerequisiteError,
389
+ debianVenvPackage,
390
+ classifyVenvCreateFailure,
391
+ classifyVenvDir,
392
+ remediationPayload,
393
+ renderRemediation,
394
+ checkPythonPrerequisites,
395
+ checkBuildPrerequisites,
396
+ checkOwnership,
397
+ currentUid,
398
+ currentHome,
399
+ };