-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjobs.ts
More file actions
179 lines (176 loc) · 5.96 KB
/
Copy pathjobs.ts
File metadata and controls
179 lines (176 loc) · 5.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import { mkdtemp, rm, mkdir, rename, lstat } from "node:fs/promises";
import { join, dirname, resolve, relative, isAbsolute } from "node:path";
import { CLI, quote } from "./cli.ts";
import type { Config } from "./config.ts";
import { stage } from "./sync.ts";
import { errorText, integer, text } from "./util.ts";
const LANGUAGES: Record<string, [string, string]> = {
py: ["main.py", "python3 main.py"],
js: ["main.js", "node main.js"],
ts: ["main.ts", "bun main.ts"],
sh: ["main.sh", "bash main.sh"],
go: ["main.go", "go run main.go"],
rb: ["main.rb", "ruby main.rb"],
c: ["main.c", "gcc -o main main.c && ./main"],
cpp: ["main.cpp", "g++ -o main main.cpp && ./main"],
rs: ["main.rs", "rustc -o main main.rs && ./main"],
mjs: ["main.mjs", "node main.mjs"],
cjs: ["main.cjs", "node main.cjs"],
};
/** Keep uncertain executions and failed downloads recoverable; report cleanup failure with the resource ID. */
export async function job(
cli: CLI,
config: Config,
source: string,
args: Record<string, unknown>,
signal?: AbortSignal,
) {
const code = args.code;
const language = text(args.lang, "lang", "py");
if (code !== undefined && !LANGUAGES[language])
throw new Error(`Unsupported language: ${language}`);
const timeout = integer(args.timeout, "timeout", config.timeout);
const id = await cli.create(
{
...config,
shape: text(args.shape, "shape", config.shape),
rootfs: text(args.rootfs, "rootfs", config.rootfs),
},
signal,
);
let keep = false,
started = false;
let failure: unknown;
let result: Awaited<ReturnType<CLI["execute"]>> | undefined;
try {
signal?.throwIfAborted();
await cli.ready(id, signal);
await cli.script(id, "mkdir -p /work", signal);
let command: string;
if (code !== undefined) {
const [file, run] = LANGUAGES[language];
await cli.push(id, "/work/" + file, text(code, "code"), signal);
command = run;
if (args.stdin !== undefined) {
await cli.push(id, "/work/.stdin", text(args.stdin, "stdin"), signal);
command = `(${run}) < /work/.stdin`;
}
} else {
command = text(args.command, "command");
await stage(cli, id, text(args.dir, "dir", source), "/work", signal);
}
started = true;
result = await cli.execute(id, command, "/work", timeout, signal);
started = false;
keep = args.keep_on_fail === true && result.code !== 0;
if (args.out !== undefined) {
const out = text(args.out, "out");
const parts = out.split("/");
if (!out || isAbsolute(out) || parts.includes("..")) {
keep = true;
throw new Error(`Invalid artifact path; sandbox ${id} retained`);
}
// Single-file retrieval avoids extracting a guest-controlled archive on the host.
const destinationRoot = resolve(text(args.dir, "dir", source));
const destination = resolve(destinationRoot, out);
const delta = relative(destinationRoot, destination);
if (delta.startsWith("..") || isAbsolute(delta))
throw new Error("Artifact escaped destination");
try {
await cli.script(
id,
`test -f ${quote("/work/" + out)} && test ! -L ${quote("/work/" + out)}`,
signal,
);
let parent = dirname(destination);
while (parent !== dirname(parent)) {
try {
if ((await lstat(parent)).isSymbolicLink())
throw new Error("Artifact destination contains a symlink");
} catch (error) {
if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error;
}
if (parent === destinationRoot) break;
parent = dirname(parent);
}
await mkdir(dirname(destination), { recursive: true });
const temp = await mkdtemp(join(dirname(destination), ".createos-artifact-"));
try {
await cli.checked(["sandbox", "pull", id, "/work/" + out, join(temp, "file")], {
signal,
timeout: 600_000,
});
await rename(join(temp, "file"), destination);
} finally {
await rm(temp, { recursive: true, force: true });
}
} catch (cause) {
keep = true;
throw new Error(`Artifact retrieval failed; sandbox ${id} retained`, { cause });
}
}
} catch (cause) {
keep ||= started && !signal?.aborted;
failure = new Error(
`Job failed; sandbox ${id}${keep ? " retained for recovery" : " will be destroyed"}: ${errorText(cause)}`,
{ cause },
);
}
if (!keep) {
try {
await cli.destroy(id);
} catch (cause) {
throw new AggregateError(
[...(failure ? [failure] : []), cause],
`Cleanup failed; sandbox ${id} is still allocated`,
);
}
}
if (failure) throw failure;
return { sandboxId: id, ...result!, kept: keep };
}
export async function fanout(
cli: CLI,
config: Config,
source: string,
args: Record<string, unknown>,
signal?: AbortSignal,
) {
if (
!Array.isArray(args.commands) ||
!args.commands.length ||
args.commands.length > 25 ||
args.commands.some((value) => typeof value !== "string")
)
throw new Error("commands must contain 1–25 strings");
const commands = args.commands as string[];
const results: unknown[] = Array.from({ length: commands.length });
let next = 0;
await Promise.all(
Array.from(
{ length: Math.min(commands.length, integer(args.jobs, "jobs", 2, 10)) },
async () => {
while (!signal?.aborted) {
const index = next++;
if (index >= commands.length) return;
try {
results[index] = await job(
cli,
config,
source,
{ ...args, command: commands[index], out: undefined },
signal,
);
} catch (error) {
results[index] = {
command: commands[index],
error: errorText(error),
};
}
}
},
),
);
signal?.throwIfAborted();
return results;
}