@sharedots/cli 0.1.0 → 0.1.2
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 +1 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +292 -189
- package/dist/index.js.map +1 -1
- package/dist/job-runner.d.ts +81 -0
- package/dist/job-runner.d.ts.map +1 -0
- package/dist/job-runner.js +544 -0
- package/dist/job-runner.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 +298 -0
- package/dist/up-wizard.js.map +1 -0
- package/package.json +4 -4
- package/src/cli.test.ts +50 -0
- package/src/daemon-runner.ts +165 -0
- package/src/index.ts +304 -193
- package/src/job-runner.test.ts +287 -0
- package/src/job-runner.ts +645 -0
- package/src/parser.ts +77 -0
- package/src/up-wizard.ts +339 -0
package/src/index.ts
CHANGED
|
@@ -1,116 +1,42 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
closeSync,
|
|
5
|
-
existsSync,
|
|
6
|
-
mkdirSync,
|
|
7
|
-
openSync,
|
|
8
|
-
readFileSync,
|
|
9
|
-
realpathSync,
|
|
10
|
-
rmSync,
|
|
11
|
-
writeFileSync,
|
|
12
|
-
} from "node:fs";
|
|
13
|
-
import { homedir } from "node:os";
|
|
2
|
+
import { spawnSync } from "node:child_process";
|
|
3
|
+
import { existsSync, readFileSync, realpathSync, rmSync } from "node:fs";
|
|
14
4
|
import { dirname, join, resolve } from "node:path";
|
|
15
5
|
import { fileURLToPath } from "node:url";
|
|
6
|
+
import { decodeInviteCode, encodeInviteCode } from "@sharedots/protocol";
|
|
16
7
|
import { Sharedots } from "sharedots";
|
|
8
|
+
import {
|
|
9
|
+
configPath,
|
|
10
|
+
getHomeDir,
|
|
11
|
+
isProcessRunning,
|
|
12
|
+
logPath,
|
|
13
|
+
pidPath,
|
|
14
|
+
readConfig,
|
|
15
|
+
startDaemonProcess,
|
|
16
|
+
stopDaemonProcess,
|
|
17
|
+
writeConfig,
|
|
18
|
+
} from "./daemon-runner.js";
|
|
19
|
+
import {
|
|
20
|
+
constructDashboardUrl,
|
|
21
|
+
openDashboardBridge,
|
|
22
|
+
runInteractiveJobRunner,
|
|
23
|
+
runScript,
|
|
24
|
+
} from "./job-runner.js";
|
|
25
|
+
import { runUpWizard } from "./up-wizard.js";
|
|
17
26
|
|
|
18
27
|
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
19
28
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
roomId: string;
|
|
23
|
-
token: string;
|
|
24
|
-
joinedAt: string;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
function getHomeDir(): string {
|
|
28
|
-
const dir = process.env.SHAREDOTS_HOME
|
|
29
|
-
? resolve(process.env.SHAREDOTS_HOME)
|
|
30
|
-
: join(homedir(), ".sharedots");
|
|
31
|
-
mkdirSync(dir, { recursive: true });
|
|
32
|
-
return dir;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
function configPath(): string {
|
|
36
|
-
return join(getHomeDir(), "worker-config.json");
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
function pidPath(): string {
|
|
40
|
-
return join(getHomeDir(), "worker.pid");
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
function logPath(): string {
|
|
44
|
-
return join(getHomeDir(), "worker.log");
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
function readConfig(): LocalWorkerConfig | null {
|
|
48
|
-
const p = configPath();
|
|
49
|
-
if (!existsSync(p)) return null;
|
|
50
|
-
return JSON.parse(readFileSync(p, "utf8")) as LocalWorkerConfig;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
function writeConfig(cfg: LocalWorkerConfig): void {
|
|
54
|
-
writeFileSync(configPath(), `${JSON.stringify(cfg, null, 2)}\n`, "utf8");
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
function resolveDaemonBinary(): string {
|
|
58
|
-
if (process.env.SHAREDOTS_DAEMON_BIN && existsSync(process.env.SHAREDOTS_DAEMON_BIN)) {
|
|
59
|
-
return process.env.SHAREDOTS_DAEMON_BIN;
|
|
60
|
-
}
|
|
61
|
-
const exe = process.platform === "win32" ? "sharedots-daemon.exe" : "sharedots-daemon";
|
|
62
|
-
const repoRoot = resolve(__dirname, "../../..");
|
|
63
|
-
const candidates = [
|
|
64
|
-
join(repoRoot, "target", "release", exe),
|
|
65
|
-
join(repoRoot, "target", "debug", exe),
|
|
66
|
-
join(process.cwd(), "target", "release", exe),
|
|
67
|
-
join(process.cwd(), "target", "debug", exe),
|
|
68
|
-
];
|
|
69
|
-
for (const c of candidates) {
|
|
70
|
-
if (existsSync(c)) return c;
|
|
71
|
-
}
|
|
72
|
-
return exe; // fallback to PATH
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
function isProcessRunning(pid: number): boolean {
|
|
76
|
-
try {
|
|
77
|
-
process.kill(pid, 0);
|
|
78
|
-
return true;
|
|
79
|
-
} catch {
|
|
80
|
-
return false;
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
export function parseFlags(argv: string[]): { positional: string[]; flags: Record<string, string | boolean> } {
|
|
85
|
-
const positional: string[] = [];
|
|
86
|
-
const flags: Record<string, string | boolean> = {};
|
|
87
|
-
for (let i = 0; i < argv.length; i++) {
|
|
88
|
-
const arg = argv[i]!;
|
|
89
|
-
if (arg.startsWith("--")) {
|
|
90
|
-
const key = arg.slice(2);
|
|
91
|
-
const eq = key.indexOf("=");
|
|
92
|
-
if (eq !== -1) {
|
|
93
|
-
flags[key.slice(0, eq)] = key.slice(eq + 1);
|
|
94
|
-
} else {
|
|
95
|
-
const next = argv[i + 1];
|
|
96
|
-
if (next && !next.startsWith("--")) {
|
|
97
|
-
flags[key] = next;
|
|
98
|
-
i++;
|
|
99
|
-
} else {
|
|
100
|
-
flags[key] = true;
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
} else {
|
|
104
|
-
positional.push(arg);
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
return { positional, flags };
|
|
108
|
-
}
|
|
29
|
+
export { parseFlags } from "./parser.js";
|
|
30
|
+
import { parseFlags } from "./parser.js";
|
|
109
31
|
|
|
110
32
|
function printHelp(): void {
|
|
111
33
|
console.log(`Sharedots CLI v0.1.0 — Universal Distributed Compute Network
|
|
112
34
|
|
|
113
35
|
Usage:
|
|
36
|
+
sharedots up [--join CODE] [--cluster] [--contributor] [--yes]
|
|
37
|
+
sharedots join <INVITE_CODE_OR_URL> [--foreground]
|
|
38
|
+
sharedots run [script.py] [--partitions N] [--coordinator URL] [--room ROOM_ID] [--token SECRET]
|
|
39
|
+
sharedots dashboard [--coordinator URL] [--port N] [--hosted]
|
|
114
40
|
sharedots tui [--coordinator URL]
|
|
115
41
|
sharedots room create [--name NAME] [--coordinator URL]
|
|
116
42
|
sharedots worker join <ROOM_ID> --token <SECRET> [--coordinator URL]
|
|
@@ -119,7 +45,7 @@ Usage:
|
|
|
119
45
|
sharedots worker stop
|
|
120
46
|
sharedots worker leave
|
|
121
47
|
sharedots worker logs [--lines N]
|
|
122
|
-
sharedots job submit <job.json> [--coordinator URL] [--room ROOM_ID] [--token SECRET]
|
|
48
|
+
sharedots job [submit <job.json> | run [script.py]] [--coordinator URL] [--room ROOM_ID] [--token SECRET]
|
|
123
49
|
sharedots bench [--size N] [--partitions P] [--coordinator URL] [--room ROOM_ID] [--token SECRET]
|
|
124
50
|
`);
|
|
125
51
|
}
|
|
@@ -136,7 +62,67 @@ async function main(): Promise<void> {
|
|
|
136
62
|
const defaultCoord =
|
|
137
63
|
(typeof flags.coordinator === "string" ? flags.coordinator : undefined) ??
|
|
138
64
|
process.env.SHAREDOTS_COORDINATOR ??
|
|
139
|
-
"
|
|
65
|
+
"https://sharedots.onrender.com";
|
|
66
|
+
|
|
67
|
+
if (group === "up") {
|
|
68
|
+
await runUpWizard({
|
|
69
|
+
cluster: Boolean(flags.cluster),
|
|
70
|
+
contributor: Boolean(flags.contributor),
|
|
71
|
+
joinCode: typeof flags.join === "string" ? flags.join : sub?.startsWith("sd_join_") ? sub : undefined,
|
|
72
|
+
coordinator: typeof flags.coordinator === "string" ? flags.coordinator : undefined,
|
|
73
|
+
yes: Boolean(flags.yes || flags.y),
|
|
74
|
+
foreground: Boolean(flags.foreground),
|
|
75
|
+
});
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (group === "join") {
|
|
80
|
+
const rawInput = sub ?? rest[0] ?? (typeof flags.code === "string" ? flags.code : undefined);
|
|
81
|
+
if (!rawInput) {
|
|
82
|
+
console.error("Usage: sharedots join <INVITE_CODE_OR_URL> [--foreground]");
|
|
83
|
+
process.exit(1);
|
|
84
|
+
}
|
|
85
|
+
const decoded = decodeInviteCode(rawInput);
|
|
86
|
+
if (!decoded) {
|
|
87
|
+
console.error("✖ Invalid invite code format. Expected 'sd_join_...' or URL containing it.");
|
|
88
|
+
process.exit(1);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
console.log(`Verifying connection to coordinator at ${decoded.coordinatorUrl}...`);
|
|
92
|
+
try {
|
|
93
|
+
const res = await fetch(`${decoded.coordinatorUrl}/api/overview`, { signal: AbortSignal.timeout(3000) });
|
|
94
|
+
if (!res.ok && res.status !== 401 && res.status !== 403) {
|
|
95
|
+
console.warn(`Warning: Coordinator returned status ${res.status}`);
|
|
96
|
+
}
|
|
97
|
+
} catch (err) {
|
|
98
|
+
console.warn(`Notice: Could not ping ${decoded.coordinatorUrl} (${err instanceof Error ? err.message : String(err)}). Continuing with configuration.`);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
writeConfig({
|
|
102
|
+
coordinator: decoded.coordinatorUrl,
|
|
103
|
+
roomId: decoded.roomId,
|
|
104
|
+
token: decoded.token,
|
|
105
|
+
joinedAt: new Date().toISOString(),
|
|
106
|
+
});
|
|
107
|
+
console.log(`Saved worker configuration for room ${decoded.roomId} (${decoded.coordinatorUrl})`);
|
|
108
|
+
|
|
109
|
+
const started = startDaemonProcess({
|
|
110
|
+
coordinator: decoded.coordinatorUrl,
|
|
111
|
+
roomId: decoded.roomId,
|
|
112
|
+
token: decoded.token,
|
|
113
|
+
cpuLimit: typeof flags["cpu-limit"] === "string" ? flags["cpu-limit"] : undefined,
|
|
114
|
+
memoryLimit: typeof flags["memory-limit"] === "string" ? flags["memory-limit"] : undefined,
|
|
115
|
+
gpu: flags.gpu === true,
|
|
116
|
+
foreground: flags.foreground === true,
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
if (started.pid) {
|
|
120
|
+
console.log(`Started sharedots-daemon (PID ${started.pid}) for room ${decoded.roomId}`);
|
|
121
|
+
console.log(`Logs: ${logPath()}`);
|
|
122
|
+
console.log(`Run 'sharedots tui' or 'sharedots worker status' to monitor.`);
|
|
123
|
+
}
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
140
126
|
|
|
141
127
|
if (group === "tui") {
|
|
142
128
|
try {
|
|
@@ -173,14 +159,19 @@ async function main(): Promise<void> {
|
|
|
173
159
|
const client = new Sharedots({ coordinator: defaultCoord });
|
|
174
160
|
const name = typeof flags.name === "string" ? flags.name : undefined;
|
|
175
161
|
const room = await client.createRoom(name);
|
|
162
|
+
const inviteCode = encodeInviteCode(defaultCoord, room.roomId, room.secret);
|
|
163
|
+
|
|
176
164
|
if (flags.json) {
|
|
177
|
-
console.log(JSON.stringify(room, null, 2));
|
|
165
|
+
console.log(JSON.stringify({ ...room, inviteCode }, null, 2));
|
|
178
166
|
} else {
|
|
179
167
|
console.log(`Room created successfully`);
|
|
180
168
|
console.log(` ROOM_ID: ${room.roomId}`);
|
|
181
169
|
console.log(` ROOM_SECRET: ${room.secret}`);
|
|
182
170
|
console.log(` COORDINATOR: ${defaultCoord}`);
|
|
183
|
-
console.log(
|
|
171
|
+
console.log(` INVITE_CODE: ${inviteCode}`);
|
|
172
|
+
console.log(`\nJoin a worker with 1-command:`);
|
|
173
|
+
console.log(` sharedots join ${inviteCode}`);
|
|
174
|
+
console.log(`Or classic style:`);
|
|
184
175
|
console.log(` sharedots worker join ${room.roomId} --coordinator ${defaultCoord} --token ${room.secret}`);
|
|
185
176
|
}
|
|
186
177
|
return;
|
|
@@ -188,11 +179,28 @@ async function main(): Promise<void> {
|
|
|
188
179
|
|
|
189
180
|
if (group === "worker") {
|
|
190
181
|
if (sub === "join") {
|
|
191
|
-
const
|
|
182
|
+
const firstArg = rest[0];
|
|
183
|
+
// If first argument is an invite code, decode it directly
|
|
184
|
+
if (firstArg && (firstArg.startsWith("sd_join_") || firstArg.includes("sd_join_"))) {
|
|
185
|
+
const decoded = decodeInviteCode(firstArg);
|
|
186
|
+
if (decoded) {
|
|
187
|
+
writeConfig({
|
|
188
|
+
coordinator: decoded.coordinatorUrl,
|
|
189
|
+
roomId: decoded.roomId,
|
|
190
|
+
token: decoded.token,
|
|
191
|
+
joinedAt: new Date().toISOString(),
|
|
192
|
+
});
|
|
193
|
+
console.log(`Saved worker configuration for room ${decoded.roomId} (${decoded.coordinatorUrl})`);
|
|
194
|
+
console.log(`Start the background daemon with: sharedots worker start`);
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
const roomId = firstArg;
|
|
192
200
|
const token =
|
|
193
201
|
(typeof flags.token === "string" ? flags.token : undefined) ?? process.env.SHAREDOTS_TOKEN;
|
|
194
202
|
if (!roomId || !token) {
|
|
195
|
-
console.error("Usage: sharedots worker join <ROOM_ID> --token <SECRET> [--coordinator URL]");
|
|
203
|
+
console.error("Usage: sharedots worker join <ROOM_ID> --token <SECRET> [--coordinator URL] OR sharedots join <CODE>");
|
|
196
204
|
process.exit(1);
|
|
197
205
|
}
|
|
198
206
|
writeConfig({
|
|
@@ -216,55 +224,23 @@ async function main(): Promise<void> {
|
|
|
216
224
|
defaultCoord;
|
|
217
225
|
|
|
218
226
|
if (!roomId || !token) {
|
|
219
|
-
console.error("Worker is not joined to a room. Run `sharedots worker join <ROOM_ID> --token <SECRET>` first.");
|
|
227
|
+
console.error("Worker is not joined to a room. Run `sharedots join <INVITE_CODE>` or `sharedots worker join <ROOM_ID> --token <SECRET>` first.");
|
|
220
228
|
process.exit(1);
|
|
221
229
|
}
|
|
222
230
|
|
|
223
|
-
const
|
|
224
|
-
const home = getHomeDir();
|
|
225
|
-
const args = [
|
|
226
|
-
"--coordinator",
|
|
231
|
+
const started = startDaemonProcess({
|
|
227
232
|
coordinator,
|
|
228
|
-
"--room-id",
|
|
229
233
|
roomId,
|
|
230
|
-
"--token",
|
|
231
234
|
token,
|
|
232
|
-
"
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
];
|
|
237
|
-
if (typeof flags["cpu-limit"] === "string") {
|
|
238
|
-
args.push("--cpu-limit", flags["cpu-limit"]);
|
|
239
|
-
}
|
|
240
|
-
if (typeof flags["memory-limit"] === "string") {
|
|
241
|
-
args.push("--memory-limit", flags["memory-limit"]);
|
|
242
|
-
}
|
|
243
|
-
if (flags.gpu === true) {
|
|
244
|
-
args.push("--gpu");
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
if (flags.foreground === true) {
|
|
248
|
-
const child = spawn(bin, args, { stdio: "inherit" });
|
|
249
|
-
child.on("exit", (code) => process.exit(code ?? 0));
|
|
250
|
-
return;
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
const logFd = openSync(logPath(), "a");
|
|
254
|
-
const child = spawn(bin, args, {
|
|
255
|
-
detached: true,
|
|
256
|
-
stdio: ["ignore", logFd, logFd],
|
|
235
|
+
cpuLimit: typeof flags["cpu-limit"] === "string" ? flags["cpu-limit"] : undefined,
|
|
236
|
+
memoryLimit: typeof flags["memory-limit"] === "string" ? flags["memory-limit"] : undefined,
|
|
237
|
+
gpu: flags.gpu === true,
|
|
238
|
+
foreground: flags.foreground === true,
|
|
257
239
|
});
|
|
258
|
-
child.unref();
|
|
259
|
-
closeSync(logFd);
|
|
260
240
|
|
|
261
|
-
if (
|
|
262
|
-
|
|
263
|
-
console.log(`Started sharedots-daemon (PID ${child.pid}) for room ${roomId}`);
|
|
241
|
+
if (started.pid) {
|
|
242
|
+
console.log(`Started sharedots-daemon (PID ${started.pid}) for room ${roomId}`);
|
|
264
243
|
console.log(`Logs: ${logPath()}`);
|
|
265
|
-
} else {
|
|
266
|
-
console.error(`Failed to start daemon binary: ${bin}`);
|
|
267
|
-
process.exit(1);
|
|
268
244
|
}
|
|
269
245
|
return;
|
|
270
246
|
}
|
|
@@ -288,39 +264,17 @@ async function main(): Promise<void> {
|
|
|
288
264
|
}
|
|
289
265
|
|
|
290
266
|
if (sub === "stop") {
|
|
291
|
-
const
|
|
292
|
-
if (
|
|
293
|
-
console.log(
|
|
294
|
-
return;
|
|
295
|
-
}
|
|
296
|
-
const pid = Number.parseInt(readFileSync(pidFile, "utf8").trim(), 10);
|
|
297
|
-
if (Number.isFinite(pid) && isProcessRunning(pid)) {
|
|
298
|
-
try {
|
|
299
|
-
process.kill(pid, "SIGTERM");
|
|
300
|
-
} catch {
|
|
301
|
-
// ignore
|
|
302
|
-
}
|
|
303
|
-
console.log(`Stopped worker daemon (PID ${pid})`);
|
|
267
|
+
const res = stopDaemonProcess();
|
|
268
|
+
if (res.stopped) {
|
|
269
|
+
console.log(`Stopped worker daemon (PID ${res.pid})`);
|
|
304
270
|
} else {
|
|
305
271
|
console.log("Worker daemon was already stopped.");
|
|
306
272
|
}
|
|
307
|
-
rmSync(pidFile, { force: true });
|
|
308
273
|
return;
|
|
309
274
|
}
|
|
310
275
|
|
|
311
276
|
if (sub === "leave") {
|
|
312
|
-
|
|
313
|
-
if (existsSync(pidFile)) {
|
|
314
|
-
const pid = Number.parseInt(readFileSync(pidFile, "utf8").trim(), 10);
|
|
315
|
-
if (Number.isFinite(pid) && isProcessRunning(pid)) {
|
|
316
|
-
try {
|
|
317
|
-
process.kill(pid, "SIGTERM");
|
|
318
|
-
} catch {
|
|
319
|
-
// ignore
|
|
320
|
-
}
|
|
321
|
-
}
|
|
322
|
-
rmSync(pidFile, { force: true });
|
|
323
|
-
}
|
|
277
|
+
stopDaemonProcess();
|
|
324
278
|
rmSync(configPath(), { force: true });
|
|
325
279
|
console.log("Worker left room and cleared local room configuration.");
|
|
326
280
|
return;
|
|
@@ -339,15 +293,77 @@ async function main(): Promise<void> {
|
|
|
339
293
|
}
|
|
340
294
|
}
|
|
341
295
|
|
|
342
|
-
if (group === "
|
|
343
|
-
const
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
296
|
+
if (group === "run") {
|
|
297
|
+
const cfg = readConfig();
|
|
298
|
+
const targetScript = sub ?? (typeof flags.file === "string" ? flags.file : undefined);
|
|
299
|
+
const partitions = typeof flags.partitions === "number"
|
|
300
|
+
? flags.partitions
|
|
301
|
+
: typeof flags.partitions === "string"
|
|
302
|
+
? parseInt(flags.partitions, 10)
|
|
303
|
+
: typeof flags.p === "string" || typeof flags.p === "number"
|
|
304
|
+
? Number(flags.p)
|
|
305
|
+
: undefined;
|
|
306
|
+
|
|
307
|
+
const roomId =
|
|
308
|
+
(typeof flags.room === "string" ? flags.room : undefined) ??
|
|
309
|
+
process.env.SHAREDOTS_ROOM_ID ??
|
|
310
|
+
cfg?.roomId;
|
|
311
|
+
const token =
|
|
312
|
+
(typeof flags.token === "string" ? flags.token : undefined) ??
|
|
313
|
+
process.env.SHAREDOTS_TOKEN ??
|
|
314
|
+
(flags.room && flags.room !== cfg?.roomId ? undefined : cfg?.token);
|
|
315
|
+
const coordinator =
|
|
316
|
+
(typeof flags.coordinator === "string" ? flags.coordinator : undefined) ??
|
|
317
|
+
process.env.SHAREDOTS_COORDINATOR ??
|
|
318
|
+
cfg?.coordinator ??
|
|
319
|
+
defaultCoord;
|
|
320
|
+
|
|
321
|
+
if (targetScript) {
|
|
322
|
+
const scriptArgs: string[] = [];
|
|
323
|
+
for (const r of rest) {
|
|
324
|
+
scriptArgs.push(r);
|
|
325
|
+
}
|
|
326
|
+
const reserved = new Set(["coordinator", "room", "token", "partitions", "p", "cpu", "memory", "gpu", "timeout", "json", "help", "h"]);
|
|
327
|
+
for (const [k, v] of Object.entries(flags)) {
|
|
328
|
+
if (reserved.has(k)) continue;
|
|
329
|
+
if (typeof v === "boolean" && v) {
|
|
330
|
+
scriptArgs.push(`--${k}`);
|
|
331
|
+
} else if (typeof v === "string" || typeof v === "number") {
|
|
332
|
+
scriptArgs.push(`--${k}`, String(v));
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
await runScript({
|
|
337
|
+
scriptPath: targetScript,
|
|
338
|
+
coordinator,
|
|
339
|
+
roomId,
|
|
340
|
+
token,
|
|
341
|
+
partitions,
|
|
342
|
+
args: scriptArgs,
|
|
343
|
+
cpu: typeof flags.cpu === "string" ? parseInt(flags.cpu, 10) : typeof flags.cpu === "number" ? flags.cpu : undefined,
|
|
344
|
+
memory: typeof flags.memory === "string" ? flags.memory : undefined,
|
|
345
|
+
gpu: flags.gpu === true,
|
|
346
|
+
timeout: typeof flags.timeout === "string" ? parseInt(flags.timeout, 10) : typeof flags.timeout === "number" ? flags.timeout : undefined,
|
|
347
|
+
});
|
|
348
|
+
return;
|
|
347
349
|
}
|
|
350
|
+
|
|
351
|
+
// Interactive job runner if no script file was passed
|
|
352
|
+
await runInteractiveJobRunner({
|
|
353
|
+
coordinator,
|
|
354
|
+
roomId,
|
|
355
|
+
token,
|
|
356
|
+
preset: typeof flags.preset === "string" ? flags.preset : undefined,
|
|
357
|
+
partitions,
|
|
358
|
+
});
|
|
359
|
+
return;
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
if (group === "dashboard") {
|
|
348
363
|
const cfg = readConfig();
|
|
349
364
|
const coordinator =
|
|
350
365
|
(typeof flags.coordinator === "string" ? flags.coordinator : undefined) ??
|
|
366
|
+
process.env.SHAREDOTS_COORDINATOR ??
|
|
351
367
|
cfg?.coordinator ??
|
|
352
368
|
defaultCoord;
|
|
353
369
|
const roomId =
|
|
@@ -357,24 +373,119 @@ async function main(): Promise<void> {
|
|
|
357
373
|
const token =
|
|
358
374
|
(typeof flags.token === "string" ? flags.token : undefined) ??
|
|
359
375
|
process.env.SHAREDOTS_TOKEN ??
|
|
360
|
-
cfg?.token;
|
|
376
|
+
(flags.room && flags.room !== cfg?.roomId ? undefined : cfg?.token);
|
|
377
|
+
const port = typeof flags.port === "string" ? flags.port : typeof flags.p === "string" ? flags.p : "3000";
|
|
378
|
+
|
|
379
|
+
await openDashboardBridge({
|
|
380
|
+
coordinator,
|
|
381
|
+
roomId,
|
|
382
|
+
token,
|
|
383
|
+
port,
|
|
384
|
+
hosted: flags.hosted === true,
|
|
385
|
+
openBrowser: flags["no-open"] !== true,
|
|
386
|
+
});
|
|
387
|
+
return;
|
|
388
|
+
}
|
|
361
389
|
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
390
|
+
if (group === "job") {
|
|
391
|
+
if (sub === "submit") {
|
|
392
|
+
const jobFile = rest[0];
|
|
393
|
+
if (!jobFile || !existsSync(resolve(jobFile))) {
|
|
394
|
+
console.error("Usage: sharedots job submit <job.json> [--coordinator URL] [--room ROOM_ID] [--token SECRET]");
|
|
395
|
+
process.exit(1);
|
|
396
|
+
}
|
|
397
|
+
const cfg = readConfig();
|
|
398
|
+
const coordinator =
|
|
399
|
+
(typeof flags.coordinator === "string" ? flags.coordinator : undefined) ??
|
|
400
|
+
cfg?.coordinator ??
|
|
401
|
+
defaultCoord;
|
|
402
|
+
const roomId =
|
|
403
|
+
(typeof flags.room === "string" ? flags.room : undefined) ??
|
|
404
|
+
process.env.SHAREDOTS_ROOM_ID ??
|
|
405
|
+
cfg?.roomId;
|
|
406
|
+
const token =
|
|
407
|
+
(typeof flags.token === "string" ? flags.token : undefined) ??
|
|
408
|
+
process.env.SHAREDOTS_TOKEN ??
|
|
409
|
+
cfg?.token;
|
|
410
|
+
|
|
411
|
+
if (!roomId || !token) {
|
|
412
|
+
console.error("Missing --room and --token (or join a room first).");
|
|
413
|
+
process.exit(1);
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
const spec = JSON.parse(readFileSync(resolve(jobFile), "utf8"));
|
|
417
|
+
const client = new Sharedots({ coordinator, token });
|
|
418
|
+
const room = await client.connect(roomId);
|
|
419
|
+
const job = await room.submit(spec);
|
|
420
|
+
console.log(`Submitted job ${job.jobId} to room ${roomId}`);
|
|
421
|
+
job.on("progress", (p) => {
|
|
422
|
+
console.log(` progress: ${p.percent}% (${p.completedTasks ?? 0}/${p.totalTasks ?? 0})`);
|
|
423
|
+
});
|
|
424
|
+
const result = await job.result();
|
|
425
|
+
console.log(JSON.stringify(result, null, 2));
|
|
426
|
+
return;
|
|
365
427
|
}
|
|
366
428
|
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
429
|
+
if (sub === "run" || !sub) {
|
|
430
|
+
const cfg = readConfig();
|
|
431
|
+
const targetScript = rest[0] ?? (typeof flags.file === "string" ? flags.file : undefined);
|
|
432
|
+
const partitions = typeof flags.partitions === "number"
|
|
433
|
+
? flags.partitions
|
|
434
|
+
: typeof flags.partitions === "string"
|
|
435
|
+
? parseInt(flags.partitions, 10)
|
|
436
|
+
: typeof flags.p === "string" || typeof flags.p === "number"
|
|
437
|
+
? Number(flags.p)
|
|
438
|
+
: undefined;
|
|
439
|
+
|
|
440
|
+
const roomId =
|
|
441
|
+
(typeof flags.room === "string" ? flags.room : undefined) ??
|
|
442
|
+
process.env.SHAREDOTS_ROOM_ID ??
|
|
443
|
+
cfg?.roomId;
|
|
444
|
+
const token =
|
|
445
|
+
(typeof flags.token === "string" ? flags.token : undefined) ??
|
|
446
|
+
process.env.SHAREDOTS_TOKEN ??
|
|
447
|
+
(flags.room && flags.room !== cfg?.roomId ? undefined : cfg?.token);
|
|
448
|
+
const coordinator =
|
|
449
|
+
(typeof flags.coordinator === "string" ? flags.coordinator : undefined) ??
|
|
450
|
+
process.env.SHAREDOTS_COORDINATOR ??
|
|
451
|
+
cfg?.coordinator ??
|
|
452
|
+
defaultCoord;
|
|
453
|
+
|
|
454
|
+
if (targetScript) {
|
|
455
|
+
const scriptArgs: string[] = [];
|
|
456
|
+
const reserved = new Set(["coordinator", "room", "token", "partitions", "p", "cpu", "memory", "gpu", "timeout", "json", "help", "h"]);
|
|
457
|
+
for (const [k, v] of Object.entries(flags)) {
|
|
458
|
+
if (reserved.has(k)) continue;
|
|
459
|
+
if (typeof v === "boolean" && v) {
|
|
460
|
+
scriptArgs.push(`--${k}`);
|
|
461
|
+
} else if (typeof v === "string" || typeof v === "number") {
|
|
462
|
+
scriptArgs.push(`--${k}`, String(v));
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
await runScript({
|
|
467
|
+
scriptPath: targetScript,
|
|
468
|
+
coordinator,
|
|
469
|
+
roomId,
|
|
470
|
+
token,
|
|
471
|
+
partitions,
|
|
472
|
+
args: scriptArgs,
|
|
473
|
+
cpu: typeof flags.cpu === "string" ? parseInt(flags.cpu, 10) : typeof flags.cpu === "number" ? flags.cpu : undefined,
|
|
474
|
+
memory: typeof flags.memory === "string" ? flags.memory : undefined,
|
|
475
|
+
gpu: flags.gpu === true,
|
|
476
|
+
timeout: typeof flags.timeout === "string" ? parseInt(flags.timeout, 10) : typeof flags.timeout === "number" ? flags.timeout : undefined,
|
|
477
|
+
});
|
|
478
|
+
} else {
|
|
479
|
+
await runInteractiveJobRunner({
|
|
480
|
+
coordinator,
|
|
481
|
+
roomId,
|
|
482
|
+
token,
|
|
483
|
+
preset: typeof flags.preset === "string" ? flags.preset : undefined,
|
|
484
|
+
partitions,
|
|
485
|
+
});
|
|
486
|
+
}
|
|
487
|
+
return;
|
|
488
|
+
}
|
|
378
489
|
}
|
|
379
490
|
|
|
380
491
|
if (group === "bench") {
|