@sharedots/cli 0.0.0-stage → 0.1.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/dist/daemon-runner.d.ts +32 -0
- package/dist/daemon-runner.d.ts.map +1 -0
- package/dist/daemon-runner.js +132 -0
- package/dist/daemon-runner.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +322 -0
- package/dist/index.js.map +1 -0
- package/dist/parser.d.ts +5 -0
- package/dist/parser.d.ts.map +1 -0
- package/dist/parser.js +81 -0
- package/dist/parser.js.map +1 -0
- package/dist/up-wizard.d.ts +21 -0
- package/dist/up-wizard.d.ts.map +1 -0
- package/dist/up-wizard.js +297 -0
- package/dist/up-wizard.js.map +1 -0
- package/package.json +24 -4
- package/src/cli.test.ts +69 -0
- package/src/daemon-runner.ts +165 -0
- package/src/index.ts +361 -0
- package/src/parser.ts +77 -0
- package/src/up-wizard.ts +338 -0
- package/tsconfig.json +12 -0
- package/README.md +0 -3
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
import { execSync } from "node:child_process";
|
|
2
|
+
import os from "node:os";
|
|
3
|
+
import readline from "node:readline";
|
|
4
|
+
import { decodeInviteCode } from "@sharedots/protocol";
|
|
5
|
+
import { readConfig, startDaemonProcess, writeConfig } from "./daemon-runner.js";
|
|
6
|
+
export function probeSystemSpecs() {
|
|
7
|
+
const cpus = os.cpus();
|
|
8
|
+
const cpuCores = cpus.length;
|
|
9
|
+
const cpuModel = cpus[0]?.model || "Generic CPU";
|
|
10
|
+
const totalMemoryBytes = os.totalmem();
|
|
11
|
+
const totalMemoryGb = Math.round((totalMemoryBytes / (1024 ** 3)) * 10) / 10;
|
|
12
|
+
const osName = `${os.type()} ${os.release()} (${os.arch()})`;
|
|
13
|
+
let gpuDetected = false;
|
|
14
|
+
let gpuName;
|
|
15
|
+
// Check for nvidia-smi
|
|
16
|
+
try {
|
|
17
|
+
const smi = execSync("nvidia-smi --query-gpu=name --format=csv,noheader", {
|
|
18
|
+
encoding: "utf8",
|
|
19
|
+
stdio: ["pipe", "pipe", "ignore"],
|
|
20
|
+
timeout: 1500,
|
|
21
|
+
}).trim();
|
|
22
|
+
if (smi) {
|
|
23
|
+
gpuDetected = true;
|
|
24
|
+
gpuName = smi.split("\n")[0]?.trim();
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
catch {
|
|
28
|
+
// No nvidia-smi, check Windows WMIC or PowerShell if win32
|
|
29
|
+
if (process.platform === "win32") {
|
|
30
|
+
try {
|
|
31
|
+
const winGpu = execSync('powershell -NoProfile -Command "(Get-CimInstance Win32_VideoController | Select-Object -ExpandProperty Name)[0]"', { encoding: "utf8", stdio: ["pipe", "pipe", "ignore"], timeout: 1500 }).trim();
|
|
32
|
+
if (winGpu && !winGpu.toLowerCase().includes("basic display") && !winGpu.toLowerCase().includes("remote")) {
|
|
33
|
+
gpuDetected = true;
|
|
34
|
+
gpuName = winGpu;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
catch {
|
|
38
|
+
// ignore
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return {
|
|
43
|
+
cpuCores,
|
|
44
|
+
cpuModel,
|
|
45
|
+
totalMemoryBytes,
|
|
46
|
+
totalMemoryGb,
|
|
47
|
+
gpuDetected,
|
|
48
|
+
gpuName,
|
|
49
|
+
osName,
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
export async function detectCoordinator(customUrls = []) {
|
|
53
|
+
const candidateUrls = [
|
|
54
|
+
...customUrls,
|
|
55
|
+
"http://127.0.0.1:4000",
|
|
56
|
+
"http://localhost:4000",
|
|
57
|
+
];
|
|
58
|
+
// Try checking tailscale peers if tailscale is installed
|
|
59
|
+
try {
|
|
60
|
+
const tsOut = execSync("tailscale status --json", {
|
|
61
|
+
encoding: "utf8",
|
|
62
|
+
stdio: ["pipe", "pipe", "ignore"],
|
|
63
|
+
timeout: 1000,
|
|
64
|
+
});
|
|
65
|
+
const parsed = JSON.parse(tsOut);
|
|
66
|
+
if (parsed.Peer) {
|
|
67
|
+
for (const peer of Object.values(parsed.Peer)) {
|
|
68
|
+
if (peer.TailscaleIPs && peer.TailscaleIPs[0]) {
|
|
69
|
+
candidateUrls.push(`http://${peer.TailscaleIPs[0]}:4000`);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
catch {
|
|
75
|
+
// Tailscale not running or not installed
|
|
76
|
+
}
|
|
77
|
+
for (const url of candidateUrls) {
|
|
78
|
+
try {
|
|
79
|
+
const res = await fetch(`${url.replace(/\/+$/, "")}/api/overview`, {
|
|
80
|
+
signal: AbortSignal.timeout(1000),
|
|
81
|
+
});
|
|
82
|
+
if (res.ok) {
|
|
83
|
+
return url.replace(/\/+$/, "");
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
catch {
|
|
87
|
+
// transient unreachable
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return null;
|
|
91
|
+
}
|
|
92
|
+
function promptUser(query) {
|
|
93
|
+
const rl = readline.createInterface({
|
|
94
|
+
input: process.stdin,
|
|
95
|
+
output: process.stdout,
|
|
96
|
+
});
|
|
97
|
+
return new Promise((res) => {
|
|
98
|
+
rl.question(query, (ans) => {
|
|
99
|
+
rl.close();
|
|
100
|
+
res(ans.trim());
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
export async function runUpWizard(options = {}) {
|
|
105
|
+
console.log(`\n\x1b[1m\x1b[36m⚡ SHAREDOTS ZERO-CONFIG AUTO-DISCOVERY & LAUNCH WIZARD\x1b[0m\n`);
|
|
106
|
+
const specs = probeSystemSpecs();
|
|
107
|
+
console.log(`\x1b[32m✔ Host Hardware Detected:\x1b[0m`);
|
|
108
|
+
console.log(` • CPU: ${specs.cpuCores} cores (${specs.cpuModel})`);
|
|
109
|
+
console.log(` • Memory: ${specs.totalMemoryGb} GB`);
|
|
110
|
+
console.log(` • GPU: ${specs.gpuDetected ? (specs.gpuName ?? "Yes") : "None / Integrated"}`);
|
|
111
|
+
console.log(` • OS: ${specs.osName}\n`);
|
|
112
|
+
// Direct join code provided
|
|
113
|
+
if (options.joinCode) {
|
|
114
|
+
const decoded = decodeInviteCode(options.joinCode);
|
|
115
|
+
if (!decoded) {
|
|
116
|
+
console.error("\x1b[31m✖ Error: Invalid invite code provided.\x1b[0m");
|
|
117
|
+
process.exit(1);
|
|
118
|
+
}
|
|
119
|
+
console.log(`Configuring worker for room ${decoded.roomId} (${decoded.coordinatorUrl})...`);
|
|
120
|
+
writeConfig({
|
|
121
|
+
coordinator: decoded.coordinatorUrl,
|
|
122
|
+
roomId: decoded.roomId,
|
|
123
|
+
token: decoded.token,
|
|
124
|
+
joinedAt: new Date().toISOString(),
|
|
125
|
+
});
|
|
126
|
+
const started = startDaemonProcess({
|
|
127
|
+
coordinator: decoded.coordinatorUrl,
|
|
128
|
+
roomId: decoded.roomId,
|
|
129
|
+
token: decoded.token,
|
|
130
|
+
foreground: options.foreground,
|
|
131
|
+
});
|
|
132
|
+
if (started.pid) {
|
|
133
|
+
console.log(`\x1b[32m✔ Daemon running in background (PID ${started.pid}).\x1b[0m`);
|
|
134
|
+
console.log(`Run 'sharedots worker status' or 'sharedots tui' to monitor.\n`);
|
|
135
|
+
}
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
// Explicit cluster flag
|
|
139
|
+
if (options.cluster) {
|
|
140
|
+
console.log(`Starting new local Sharedots cluster...`);
|
|
141
|
+
console.log(`Run 'pnpm coordinator:start' or launch coordinator on port 4000.\n`);
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
// Explicit contributor mode flag
|
|
145
|
+
if (options.contributor) {
|
|
146
|
+
console.log(`Configuring auto-tuned contributor node...`);
|
|
147
|
+
const existing = readConfig();
|
|
148
|
+
const coord = options.coordinator ?? existing?.coordinator ?? "http://127.0.0.1:4000";
|
|
149
|
+
const room = existing?.roomId ?? "public-pool";
|
|
150
|
+
const token = existing?.token ?? "sec_public";
|
|
151
|
+
writeConfig({ coordinator: coord, roomId: room, token, joinedAt: new Date().toISOString() });
|
|
152
|
+
const started = startDaemonProcess({
|
|
153
|
+
coordinator: coord,
|
|
154
|
+
roomId: room,
|
|
155
|
+
token,
|
|
156
|
+
cpuLimit: 75,
|
|
157
|
+
foreground: options.foreground,
|
|
158
|
+
});
|
|
159
|
+
console.log(`\x1b[32m✔ Contributor node running (PID ${started.pid}). Contributing up to 75% CPU.\x1b[0m\n`);
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
// Probe for active coordinators
|
|
163
|
+
process.stdout.write("Scanning local network and Tailscale mesh for active coordinators... ");
|
|
164
|
+
const detectedCoord = await detectCoordinator(options.coordinator ? [options.coordinator] : []);
|
|
165
|
+
if (detectedCoord) {
|
|
166
|
+
console.log(`\x1b[32mFound!\x1b[0m`);
|
|
167
|
+
console.log(`\x1b[1m\x1b[35m● Active coordinator discovered at: ${detectedCoord}\x1b[0m\n`);
|
|
168
|
+
let joinNow = options.yes;
|
|
169
|
+
if (!joinNow && process.stdin.isTTY) {
|
|
170
|
+
const ans = await promptUser(`Join active coordinator at ${detectedCoord}? [Y/n]: `);
|
|
171
|
+
joinNow = ans === "" || ans.toLowerCase().startsWith("y");
|
|
172
|
+
}
|
|
173
|
+
if (joinNow) {
|
|
174
|
+
// Fetch or probe active rooms
|
|
175
|
+
let roomId = "mesh-default";
|
|
176
|
+
let token = "sec_auto";
|
|
177
|
+
try {
|
|
178
|
+
const roomsRes = await fetch(`${detectedCoord}/api/rooms`, {
|
|
179
|
+
signal: AbortSignal.timeout(2000),
|
|
180
|
+
});
|
|
181
|
+
if (roomsRes.ok) {
|
|
182
|
+
const body = (await roomsRes.json());
|
|
183
|
+
if (body.rooms && body.rooms.length > 0 && body.rooms[0]) {
|
|
184
|
+
roomId = body.rooms[0].roomId;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
catch {
|
|
189
|
+
// ignore
|
|
190
|
+
}
|
|
191
|
+
writeConfig({
|
|
192
|
+
coordinator: detectedCoord,
|
|
193
|
+
roomId,
|
|
194
|
+
token,
|
|
195
|
+
joinedAt: new Date().toISOString(),
|
|
196
|
+
});
|
|
197
|
+
const started = startDaemonProcess({
|
|
198
|
+
coordinator: detectedCoord,
|
|
199
|
+
roomId,
|
|
200
|
+
token,
|
|
201
|
+
foreground: options.foreground,
|
|
202
|
+
});
|
|
203
|
+
console.log(`\x1b[32m✔ Connected to cluster at ${detectedCoord} (room: ${roomId}).\x1b[0m`);
|
|
204
|
+
if (started.pid) {
|
|
205
|
+
console.log(`Worker daemon started (PID ${started.pid}). Run 'sharedots tui' to view dashboard.\n`);
|
|
206
|
+
}
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
else {
|
|
211
|
+
console.log(`None detected.`);
|
|
212
|
+
}
|
|
213
|
+
// Interactive selection if TTY
|
|
214
|
+
if (process.stdin.isTTY) {
|
|
215
|
+
console.log(`How do you want to run Sharedots?`);
|
|
216
|
+
console.log(` [1] Join existing mesh (paste invite code or enter URL)`);
|
|
217
|
+
console.log(` [2] Start new cluster (Coordinator + local Worker)`);
|
|
218
|
+
console.log(` [3] Contributor mode (Worker only, auto-tuned to 75% CPU)`);
|
|
219
|
+
const choice = await promptUser(`Select option [1-3] (default 1): `);
|
|
220
|
+
if (choice === "2") {
|
|
221
|
+
console.log(`Starting new local Sharedots cluster...`);
|
|
222
|
+
// Start cluster
|
|
223
|
+
console.log(`Run 'pnpm coordinator:start' or launch coordinator on port 4000.`);
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
if (choice === "3") {
|
|
227
|
+
console.log(`Configuring auto-tuned contributor node...`);
|
|
228
|
+
const existing = readConfig();
|
|
229
|
+
const coord = existing?.coordinator ?? detectedCoord ?? "http://127.0.0.1:4000";
|
|
230
|
+
const room = existing?.roomId ?? "public-pool";
|
|
231
|
+
const token = existing?.token ?? "sec_public";
|
|
232
|
+
writeConfig({ coordinator: coord, roomId: room, token, joinedAt: new Date().toISOString() });
|
|
233
|
+
const started = startDaemonProcess({
|
|
234
|
+
coordinator: coord,
|
|
235
|
+
roomId: room,
|
|
236
|
+
token,
|
|
237
|
+
cpuLimit: 75,
|
|
238
|
+
foreground: options.foreground,
|
|
239
|
+
});
|
|
240
|
+
console.log(`\x1b[32m✔ Contributor node running (PID ${started.pid}). Contributing up to 75% CPU.\x1b[0m\n`);
|
|
241
|
+
return;
|
|
242
|
+
}
|
|
243
|
+
// Default option 1: join
|
|
244
|
+
const rawInput = await promptUser(`Paste Unified Invite Code ('sd_join_...') or Coordinator URL: `);
|
|
245
|
+
if (!rawInput) {
|
|
246
|
+
console.log("No input received. Exiting.");
|
|
247
|
+
return;
|
|
248
|
+
}
|
|
249
|
+
const decoded = decodeInviteCode(rawInput);
|
|
250
|
+
if (decoded) {
|
|
251
|
+
writeConfig({
|
|
252
|
+
coordinator: decoded.coordinatorUrl,
|
|
253
|
+
roomId: decoded.roomId,
|
|
254
|
+
token: decoded.token,
|
|
255
|
+
joinedAt: new Date().toISOString(),
|
|
256
|
+
});
|
|
257
|
+
const started = startDaemonProcess({
|
|
258
|
+
coordinator: decoded.coordinatorUrl,
|
|
259
|
+
roomId: decoded.roomId,
|
|
260
|
+
token: decoded.token,
|
|
261
|
+
foreground: options.foreground,
|
|
262
|
+
});
|
|
263
|
+
console.log(`\x1b[32m✔ Worker successfully configured and launched (PID ${started.pid}).\x1b[0m\n`);
|
|
264
|
+
return;
|
|
265
|
+
}
|
|
266
|
+
console.log(`Connecting to ${rawInput}...`);
|
|
267
|
+
writeConfig({
|
|
268
|
+
coordinator: rawInput,
|
|
269
|
+
roomId: "default-room",
|
|
270
|
+
token: "sec_default",
|
|
271
|
+
joinedAt: new Date().toISOString(),
|
|
272
|
+
});
|
|
273
|
+
startDaemonProcess({
|
|
274
|
+
coordinator: rawInput,
|
|
275
|
+
roomId: "default-room",
|
|
276
|
+
token: "sec_default",
|
|
277
|
+
foreground: options.foreground,
|
|
278
|
+
});
|
|
279
|
+
console.log(`\x1b[32m✔ Worker started.\x1b[0m\n`);
|
|
280
|
+
}
|
|
281
|
+
else {
|
|
282
|
+
// Non-interactive fallback
|
|
283
|
+
const existing = readConfig();
|
|
284
|
+
const coord = existing?.coordinator ?? detectedCoord ?? "http://127.0.0.1:4000";
|
|
285
|
+
const room = existing?.roomId ?? "mesh-default";
|
|
286
|
+
const token = existing?.token ?? "sec_auto";
|
|
287
|
+
writeConfig({ coordinator: coord, roomId: room, token, joinedAt: new Date().toISOString() });
|
|
288
|
+
const started = startDaemonProcess({
|
|
289
|
+
coordinator: coord,
|
|
290
|
+
roomId: room,
|
|
291
|
+
token,
|
|
292
|
+
foreground: options.foreground,
|
|
293
|
+
});
|
|
294
|
+
console.log(`\x1b[32m✔ Sharedots started for room ${room} on ${coord} (PID ${started.pid}).\x1b[0m\n`);
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
//# sourceMappingURL=up-wizard.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"up-wizard.js","sourceRoot":"","sources":["../src/up-wizard.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAC;AAC9C,OAAO,EAAE,MAAM,SAAS,CAAC;AACzB,OAAO,QAAQ,MAAM,eAAe,CAAC;AACrC,OAAO,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AACvD,OAAO,EAAE,UAAU,EAAE,kBAAkB,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AAYjF,MAAM,UAAU,gBAAgB;IAC9B,MAAM,IAAI,GAAG,EAAE,CAAC,IAAI,EAAE,CAAC;IACvB,MAAM,QAAQ,GAAG,IAAI,CAAC,MAAM,CAAC;IAC7B,MAAM,QAAQ,GAAG,IAAI,CAAC,CAAC,CAAC,EAAE,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|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"types": "./dist/index.d.ts",
|
|
7
|
+
"bin": {
|
|
8
|
+
"sharedots": "./dist/index.js"
|
|
9
|
+
},
|
|
10
|
+
"scripts": {
|
|
11
|
+
"build": "tsc -p tsconfig.json",
|
|
12
|
+
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
13
|
+
"test": "vitest run",
|
|
14
|
+
"clean": "node -e \"require('fs').rmSync('dist', { recursive: true, force: true })\""
|
|
15
|
+
},
|
|
16
|
+
"dependencies": {
|
|
17
|
+
"@sharedots/protocol": "workspace:*",
|
|
18
|
+
"@sharedots/tui": "workspace:*",
|
|
19
|
+
"sharedots": "workspace:*"
|
|
20
|
+
},
|
|
21
|
+
"devDependencies": {
|
|
22
|
+
"@types/node": "^22.10.0",
|
|
23
|
+
"typescript": "^5.7.2",
|
|
24
|
+
"vitest": "^2.1.8"
|
|
25
|
+
}
|
|
26
|
+
}
|
package/src/cli.test.ts
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { parseFlags } from "./index.js";
|
|
3
|
+
import { probeSystemSpecs } from "./up-wizard.js";
|
|
4
|
+
|
|
5
|
+
describe("@sharedots/cli argument parser", () => {
|
|
6
|
+
it("parses positional commands and flag values cleanly", () => {
|
|
7
|
+
const parsed = parseFlags([
|
|
8
|
+
"worker",
|
|
9
|
+
"start",
|
|
10
|
+
"--cpu-limit",
|
|
11
|
+
"50",
|
|
12
|
+
"--memory-limit=4096",
|
|
13
|
+
"--gpu",
|
|
14
|
+
]);
|
|
15
|
+
expect(parsed.positional).toEqual(["worker", "start"]);
|
|
16
|
+
expect(parsed.flags["cpu-limit"]).toBe("50");
|
|
17
|
+
expect(parsed.flags["memory-limit"]).toBe("4096");
|
|
18
|
+
expect(parsed.flags.gpu).toBe(true);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it("parses sharedots join command with invite code", () => {
|
|
22
|
+
const parsed = parseFlags([
|
|
23
|
+
"join",
|
|
24
|
+
"sd_join_eyJjIjoiaHR0cDovL2xvY2FsaG9zdDo0MDAwIiwiciI6InJvb21fMSIsInQiOiJzZWNfMSJ9",
|
|
25
|
+
"--foreground",
|
|
26
|
+
]);
|
|
27
|
+
expect(parsed.positional[0]).toBe("join");
|
|
28
|
+
expect(parsed.positional[1]).toBe(
|
|
29
|
+
"sd_join_eyJjIjoiaHR0cDovL2xvY2FsaG9zdDo0MDAwIiwiciI6InJvb21fMSIsInQiOiJzZWNfMSJ9",
|
|
30
|
+
);
|
|
31
|
+
expect(parsed.flags.foreground).toBe(true);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it("parses sharedots up command with flags", () => {
|
|
35
|
+
const parsed = parseFlags(["up", "--cluster", "-y", "--coordinator", "http://100.84.12.9:4000"]);
|
|
36
|
+
expect(parsed.positional[0]).toBe("up");
|
|
37
|
+
expect(parsed.flags.cluster).toBe(true);
|
|
38
|
+
expect(parsed.flags.y).toBe(true);
|
|
39
|
+
expect(parsed.flags.coordinator).toBe("http://100.84.12.9:4000");
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it("handles boolean flags followed by positional arguments without swallowing them", () => {
|
|
43
|
+
const parsed = parseFlags(["up", "-y", "sd_join_eyJjIjoiaHR0cDovL2xvY2FsaG9zdDo0MDAwIn0"]);
|
|
44
|
+
expect(parsed.flags.y).toBe(true);
|
|
45
|
+
expect(parsed.positional).toEqual(["up", "sd_join_eyJjIjoiaHR0cDovL2xvY2FsaG9zdDo0MDAwIn0"]);
|
|
46
|
+
|
|
47
|
+
const parsedLong = parseFlags(["up", "--yes", "sd_join_test"]);
|
|
48
|
+
expect(parsedLong.flags.yes).toBe(true);
|
|
49
|
+
expect(parsedLong.positional).toEqual(["up", "sd_join_test"]);
|
|
50
|
+
|
|
51
|
+
const parsedGpu = parseFlags(["worker", "start", "--gpu", "my-room"]);
|
|
52
|
+
expect(parsedGpu.flags.gpu).toBe(true);
|
|
53
|
+
expect(parsedGpu.positional).toEqual(["worker", "start", "my-room"]);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it("respects double-dash end-of-flags delimiter", () => {
|
|
57
|
+
const parsed = parseFlags(["job", "submit", "--", "--some-positional-arg"]);
|
|
58
|
+
expect(parsed.positional).toEqual(["job", "submit", "--some-positional-arg"]);
|
|
59
|
+
expect(parsed.flags["some-positional-arg"]).toBeUndefined();
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it("probes system specs truthfully", () => {
|
|
63
|
+
const specs = probeSystemSpecs();
|
|
64
|
+
expect(specs.cpuCores).toBeGreaterThan(0);
|
|
65
|
+
expect(specs.totalMemoryBytes).toBeGreaterThan(0);
|
|
66
|
+
expect(specs.totalMemoryGb).toBeGreaterThan(0);
|
|
67
|
+
expect(typeof specs.osName).toBe("string");
|
|
68
|
+
});
|
|
69
|
+
});
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
import {
|
|
3
|
+
closeSync,
|
|
4
|
+
existsSync,
|
|
5
|
+
mkdirSync,
|
|
6
|
+
openSync,
|
|
7
|
+
readFileSync,
|
|
8
|
+
rmSync,
|
|
9
|
+
writeFileSync,
|
|
10
|
+
} from "node:fs";
|
|
11
|
+
import { homedir } from "node:os";
|
|
12
|
+
import { dirname, join, resolve } from "node:path";
|
|
13
|
+
import { fileURLToPath } from "node:url";
|
|
14
|
+
|
|
15
|
+
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
16
|
+
|
|
17
|
+
export interface LocalWorkerConfig {
|
|
18
|
+
coordinator: string;
|
|
19
|
+
roomId: string;
|
|
20
|
+
token: string;
|
|
21
|
+
joinedAt: string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function getHomeDir(): string {
|
|
25
|
+
const dir = process.env.SHAREDOTS_HOME
|
|
26
|
+
? resolve(process.env.SHAREDOTS_HOME)
|
|
27
|
+
: join(homedir(), ".sharedots");
|
|
28
|
+
mkdirSync(dir, { recursive: true });
|
|
29
|
+
return dir;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function configPath(): string {
|
|
33
|
+
return join(getHomeDir(), "worker-config.json");
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function pidPath(): string {
|
|
37
|
+
return join(getHomeDir(), "worker.pid");
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function logPath(): string {
|
|
41
|
+
return join(getHomeDir(), "worker.log");
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function readConfig(): LocalWorkerConfig | null {
|
|
45
|
+
const p = configPath();
|
|
46
|
+
if (!existsSync(p)) return null;
|
|
47
|
+
try {
|
|
48
|
+
return JSON.parse(readFileSync(p, "utf8")) as LocalWorkerConfig;
|
|
49
|
+
} catch {
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function writeConfig(cfg: LocalWorkerConfig): void {
|
|
55
|
+
writeFileSync(configPath(), `${JSON.stringify(cfg, null, 2)}\n`, "utf8");
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function resolveDaemonBinary(): string {
|
|
59
|
+
if (process.env.SHAREDOTS_DAEMON_BIN && existsSync(process.env.SHAREDOTS_DAEMON_BIN)) {
|
|
60
|
+
return process.env.SHAREDOTS_DAEMON_BIN;
|
|
61
|
+
}
|
|
62
|
+
const exe = process.platform === "win32" ? "sharedots-daemon.exe" : "sharedots-daemon";
|
|
63
|
+
const repoRoot = resolve(__dirname, "../../..");
|
|
64
|
+
const candidates = [
|
|
65
|
+
join(repoRoot, "target", "release", exe),
|
|
66
|
+
join(repoRoot, "target", "debug", exe),
|
|
67
|
+
join(process.cwd(), "target", "release", exe),
|
|
68
|
+
join(process.cwd(), "target", "debug", exe),
|
|
69
|
+
];
|
|
70
|
+
for (const c of candidates) {
|
|
71
|
+
if (existsSync(c)) return c;
|
|
72
|
+
}
|
|
73
|
+
return exe; // fallback to PATH
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export function isProcessRunning(pid: number): boolean {
|
|
77
|
+
try {
|
|
78
|
+
process.kill(pid, 0);
|
|
79
|
+
return true;
|
|
80
|
+
} catch {
|
|
81
|
+
return false;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export interface StartDaemonOptions {
|
|
86
|
+
coordinator: string;
|
|
87
|
+
roomId: string;
|
|
88
|
+
token: string;
|
|
89
|
+
cpuLimit?: string | number;
|
|
90
|
+
memoryLimit?: string | number;
|
|
91
|
+
gpu?: boolean;
|
|
92
|
+
foreground?: boolean;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export function startDaemonProcess(options: StartDaemonOptions): { pid?: number; foreground: boolean } {
|
|
96
|
+
const bin = resolveDaemonBinary();
|
|
97
|
+
const home = getHomeDir();
|
|
98
|
+
const args = [
|
|
99
|
+
"--coordinator",
|
|
100
|
+
options.coordinator,
|
|
101
|
+
"--room-id",
|
|
102
|
+
options.roomId,
|
|
103
|
+
"--token",
|
|
104
|
+
options.token,
|
|
105
|
+
"--state-dir",
|
|
106
|
+
join(home, "state"),
|
|
107
|
+
"--work-dir",
|
|
108
|
+
join(home, "work"),
|
|
109
|
+
];
|
|
110
|
+
|
|
111
|
+
if (options.cpuLimit !== undefined) {
|
|
112
|
+
args.push("--cpu-limit", String(options.cpuLimit));
|
|
113
|
+
}
|
|
114
|
+
if (options.memoryLimit !== undefined) {
|
|
115
|
+
args.push("--memory-limit", String(options.memoryLimit));
|
|
116
|
+
}
|
|
117
|
+
if (options.gpu === true) {
|
|
118
|
+
args.push("--gpu");
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
if (options.foreground === true) {
|
|
122
|
+
const child = spawn(bin, args, { stdio: "inherit" });
|
|
123
|
+
child.on("exit", (code) => process.exit(code ?? 0));
|
|
124
|
+
return { pid: child.pid, foreground: true };
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const logFd = openSync(logPath(), "a");
|
|
128
|
+
const child = spawn(bin, args, {
|
|
129
|
+
detached: true,
|
|
130
|
+
stdio: ["ignore", logFd, logFd],
|
|
131
|
+
});
|
|
132
|
+
child.on("error", (err) => {
|
|
133
|
+
console.error(`Failed to spawn daemon process (${bin}): ${err.message}`);
|
|
134
|
+
});
|
|
135
|
+
child.unref();
|
|
136
|
+
closeSync(logFd);
|
|
137
|
+
|
|
138
|
+
if (child.pid) {
|
|
139
|
+
writeFileSync(pidPath(), String(child.pid), "utf8");
|
|
140
|
+
}
|
|
141
|
+
return { pid: child.pid, foreground: false };
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export function stopDaemonProcess(): { stopped: boolean; pid?: number } {
|
|
145
|
+
const pPath = pidPath();
|
|
146
|
+
if (!existsSync(pPath)) {
|
|
147
|
+
return { stopped: false };
|
|
148
|
+
}
|
|
149
|
+
try {
|
|
150
|
+
const pid = Number.parseInt(readFileSync(pPath, "utf8").trim(), 10);
|
|
151
|
+
if (Number.isFinite(pid) && isProcessRunning(pid)) {
|
|
152
|
+
try {
|
|
153
|
+
process.kill(pid, "SIGTERM");
|
|
154
|
+
} catch {
|
|
155
|
+
// ignore
|
|
156
|
+
}
|
|
157
|
+
rmSync(pPath, { force: true });
|
|
158
|
+
return { stopped: true, pid };
|
|
159
|
+
}
|
|
160
|
+
} catch {
|
|
161
|
+
// ignore
|
|
162
|
+
}
|
|
163
|
+
rmSync(pPath, { force: true });
|
|
164
|
+
return { stopped: false };
|
|
165
|
+
}
|