Skip to content

Commit 4eff90e

Browse files
authored
Don't write a beamignore all the time (#46)
* Do not write a .beamignore all the time * Add better error handling to surface actual error message * wrong constructor
1 parent c4282e1 commit 4eff90e

4 files changed

Lines changed: 107 additions & 49 deletions

File tree

lib/resources/abstraction/sandbox.ts

Lines changed: 53 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ export class Sandbox extends Pod {
9090
};
9191
if (!data.ok) {
9292
throw new SandboxConnectionError(
93-
data.errorMsg || "Failed to connect to sandbox"
93+
data.errorMsg || "Failed to connect to sandbox",
9494
);
9595
}
9696

@@ -103,7 +103,7 @@ export class Sandbox extends Pod {
103103
errorMsg: "",
104104
stubId: data.stubId || "",
105105
},
106-
sandbox
106+
sandbox,
107107
);
108108
}
109109

@@ -118,7 +118,7 @@ export class Sandbox extends Pod {
118118
* Throws: SandboxConnectionError if the sandbox creation fails.
119119
*/
120120
public static async createFromSnapshot(
121-
snapshotId: string
121+
snapshotId: string,
122122
): Promise<SandboxInstance> {
123123
// eslint-disable-next-line no-console
124124
console.log(`Creating sandbox from snapshot: ${snapshotId}`);
@@ -137,7 +137,7 @@ export class Sandbox extends Pod {
137137

138138
if (!body.ok) {
139139
throw new SandboxConnectionError(
140-
body.errorMsg || "Failed to create sandbox from snapshot"
140+
body.errorMsg || "Failed to create sandbox from snapshot",
141141
);
142142
}
143143

@@ -156,7 +156,7 @@ export class Sandbox extends Pod {
156156
};
157157
if (!connectData.ok) {
158158
throw new SandboxConnectionError(
159-
connectData.errorMsg || "Failed to connect to sandbox"
159+
connectData.errorMsg || "Failed to connect to sandbox",
160160
);
161161
}
162162

@@ -169,7 +169,7 @@ export class Sandbox extends Pod {
169169
ok: body.ok,
170170
errorMsg: body.errorMsg || "",
171171
},
172-
sandbox
172+
sandbox,
173173
);
174174
}
175175

@@ -195,10 +195,11 @@ export class Sandbox extends Pod {
195195
undefined,
196196
EStubType.Sandbox,
197197
true,
198-
ignorePatterns
198+
ignorePatterns,
199199
);
200200
if (!prepared) {
201-
throw new SandboxConnectionError("Failed to prepare runtime");
201+
const detail = this.stub.lastError?.message ?? "unknown reason";
202+
throw new SandboxConnectionError(`Failed to prepare runtime: ${detail}`);
202203
}
203204

204205
// eslint-disable-next-line no-console
@@ -217,7 +218,7 @@ export class Sandbox extends Pod {
217218

218219
if (!body.ok) {
219220
throw new SandboxConnectionError(
220-
body.errorMsg || "Failed to create sandbox"
221+
body.errorMsg || "Failed to create sandbox",
221222
);
222223
}
223224

@@ -236,19 +237,19 @@ export class Sandbox extends Pod {
236237
};
237238
if (!connectData.ok) {
238239
throw new SandboxConnectionError(
239-
connectData.errorMsg || "Failed to connect to sandbox"
240+
connectData.errorMsg || "Failed to connect to sandbox",
240241
);
241242
}
242243

243244
if ((this.stub.config.keepWarmSeconds as number) < 0) {
244245
// eslint-disable-next-line no-console
245246
console.log(
246-
"This sandbox has no timeout, it will run until it is shut down manually."
247+
"This sandbox has no timeout, it will run until it is shut down manually.",
247248
);
248249
} else {
249250
// eslint-disable-next-line no-console
250251
console.log(
251-
`This sandbox will timeout after ${this.stub.config.keepWarmSeconds} seconds.`
252+
`This sandbox will timeout after ${this.stub.config.keepWarmSeconds} seconds.`,
252253
);
253254
}
254255

@@ -260,7 +261,7 @@ export class Sandbox extends Pod {
260261
errorMsg: body.errorMsg || "",
261262
url: "",
262263
},
263-
this
264+
this,
264265
);
265266
}
266267
}
@@ -323,7 +324,7 @@ export class SandboxInstance extends PodInstance {
323324
*/
324325
public async createImageFromFilesystem(): Promise<string> {
325326
console.log(
326-
`Creating image from filesystem of: ${this.containerId}. This may take a few minutes...`
327+
`Creating image from filesystem of: ${this.containerId}. This may take a few minutes...`,
327328
);
328329

329330
const resp = await beamClient.request({
@@ -337,7 +338,7 @@ export class SandboxInstance extends PodInstance {
337338
console.log("data", data);
338339
if (!data.ok)
339340
throw new SandboxProcessError(
340-
data.errorMsg || "Failed to create image from filesystem"
341+
data.errorMsg || "Failed to create image from filesystem",
341342
);
342343
return data.imageId;
343344
}
@@ -388,11 +389,11 @@ export class SandboxInstance extends PodInstance {
388389
*/
389390
public async updateNetworkPermissions(
390391
blockNetwork: boolean = false,
391-
allowList?: string[]
392+
allowList?: string[],
392393
): Promise<void> {
393394
if (blockNetwork && allowList !== undefined) {
394395
throw new Error(
395-
"Cannot specify both 'blockNetwork=true' and 'allowList'. Use 'allowList' with CIDR notation to allow specific ranges, or use 'blockNetwork=true' to block all outbound traffic."
396+
"Cannot specify both 'blockNetwork=true' and 'allowList'. Use 'allowList' with CIDR notation to allow specific ranges, or use 'blockNetwork=true' to block all outbound traffic.",
396397
);
397398
}
398399

@@ -408,7 +409,7 @@ export class SandboxInstance extends PodInstance {
408409
const data = resp.data as PodSandboxUpdateNetworkPermissionsResponse;
409410
if (!data.ok) {
410411
throw new SandboxConnectionError(
411-
data.errorMsg || "Failed to update network permissions"
412+
data.errorMsg || "Failed to update network permissions",
412413
);
413414
}
414415
}
@@ -457,7 +458,7 @@ export class SandboxInstance extends PodInstance {
457458
code: string,
458459
blocking: boolean = true,
459460
cwd?: string,
460-
env?: Record<string, string>
461+
env?: Record<string, string>,
461462
): Promise<SandboxProcessResponse | SandboxProcess> {
462463
const process = await this._exec(["python3", "-c", code], { cwd, env });
463464
if (blocking) {
@@ -480,15 +481,15 @@ export class SandboxInstance extends PodInstance {
480481
/** Run an arbitrary command in the sandbox. */
481482
public async exec(
482483
command: string | string[],
483-
opts?: ExecOptions
484+
opts?: ExecOptions,
484485
): Promise<SandboxProcess> {
485486
const commandList = Array.isArray(command) ? command : [command];
486487
return this._exec(commandList, opts);
487488
}
488489

489490
private async _exec(
490491
command: string[] | string,
491-
opts?: { cwd?: string; env?: Record<string, string> }
492+
opts?: { cwd?: string; env?: Record<string, string> },
492493
): Promise<SandboxProcess> {
493494
const commandList = Array.isArray(command) ? command : [command];
494495
const shellCommand = commandList
@@ -563,7 +564,7 @@ export class SandboxProcessStream {
563564

564565
constructor(
565566
process: SandboxProcess,
566-
fetchFn: () => Promise<string> | string
567+
fetchFn: () => Promise<string> | string,
567568
) {
568569
this.process = process;
569570
this.fetch_fn = fetchFn;
@@ -873,22 +874,31 @@ export class SandboxFileInfo {
873874

874875
/** A position in a file. */
875876
export class SandboxFilePosition {
876-
constructor(public line: number, public column: number) {}
877+
constructor(
878+
public line: number,
879+
public column: number,
880+
) {}
877881
}
878882
/** A range in a file. */
879883
export class SandboxFileSearchRange {
880884
constructor(
881885
public start: SandboxFilePosition,
882-
public end: SandboxFilePosition
886+
public end: SandboxFilePosition,
883887
) {}
884888
}
885889
/** A match in a file. */
886890
export class SandboxFileSearchMatch {
887-
constructor(public range: SandboxFileSearchRange, public content: string) {}
891+
constructor(
892+
public range: SandboxFileSearchRange,
893+
public content: string,
894+
) {}
888895
}
889896
/** A search result in a file. */
890897
export class SandboxFileSearchResult {
891-
constructor(public path: string, public matches: SandboxFileSearchMatch[]) {}
898+
constructor(
899+
public path: string,
900+
public matches: SandboxFileSearchMatch[],
901+
) {}
892902
}
893903

894904
/**
@@ -905,7 +915,7 @@ export class SandboxFileSystem {
905915
/** Upload a local file to the sandbox. */
906916
public async uploadFile(
907917
localPath: string,
908-
sandboxPath: string
918+
sandboxPath: string,
909919
): Promise<void> {
910920
const content = fs.readFileSync(localPath);
911921
const resp = await beamClient.request({
@@ -920,14 +930,14 @@ export class SandboxFileSystem {
920930
const data = resp.data as { ok: boolean; errorMsg?: string };
921931
if (!data.ok)
922932
throw new SandboxFileSystemError(
923-
data.errorMsg || "Failed to upload file"
933+
data.errorMsg || "Failed to upload file",
924934
);
925935
}
926936

927937
/** Download a file from the sandbox to a local path. */
928938
public async downloadFile(
929939
sandboxPath: string,
930-
localPath: string
940+
localPath: string,
931941
): Promise<void> {
932942
const resp = await beamClient.request({
933943
method: "GET",
@@ -938,7 +948,7 @@ export class SandboxFileSystem {
938948
const data = resp.data as { ok: boolean; errorMsg?: string; data?: string };
939949
if (!data.ok || !data.data)
940950
throw new SandboxFileSystemError(
941-
data.errorMsg || "Failed to download file"
951+
data.errorMsg || "Failed to download file",
942952
);
943953
const buf = Buffer.from(data.data, "base64");
944954
fs.writeFileSync(localPath, buf);
@@ -991,7 +1001,7 @@ export class SandboxFileSystem {
9911001
owner: file.owner,
9921002
group: file.group,
9931003
permissions: Number(file.permissions),
994-
})
1004+
}),
9951005
);
9961006
}
9971007

@@ -1005,7 +1015,7 @@ export class SandboxFileSystem {
10051015
const data = resp.data as PodSandboxCreateDirectoryResponse;
10061016
if (!data.ok)
10071017
throw new SandboxFileSystemError(
1008-
data.errorMsg || "Failed to create directory"
1018+
data.errorMsg || "Failed to create directory",
10091019
);
10101020
}
10111021

@@ -1020,7 +1030,7 @@ export class SandboxFileSystem {
10201030
const data = resp.data as { ok: boolean; errorMsg?: string };
10211031
if (!data.ok)
10221032
throw new SandboxFileSystemError(
1023-
data.errorMsg || "Failed to delete directory"
1033+
data.errorMsg || "Failed to delete directory",
10241034
);
10251035
}
10261036

@@ -1035,15 +1045,15 @@ export class SandboxFileSystem {
10351045
const data = resp.data as { ok: boolean; errorMsg?: string };
10361046
if (!data.ok)
10371047
throw new SandboxFileSystemError(
1038-
data.errorMsg || "Failed to delete file"
1048+
data.errorMsg || "Failed to delete file",
10391049
);
10401050
}
10411051

10421052
/** Replace a string in all files in a directory. */
10431053
public async replaceInFiles(
10441054
sandboxPath: string,
10451055
oldString: string,
1046-
newString: string
1056+
newString: string,
10471057
): Promise<void> {
10481058
const resp = await beamClient.request({
10491059
method: "POST",
@@ -1057,14 +1067,14 @@ export class SandboxFileSystem {
10571067
const data = resp.data as { ok: boolean; errorMsg?: string };
10581068
if (!data.ok)
10591069
throw new SandboxFileSystemError(
1060-
data.errorMsg || "Failed to replace in files"
1070+
data.errorMsg || "Failed to replace in files",
10611071
);
10621072
}
10631073

10641074
/** Find files matching a pattern in the sandbox. */
10651075
public async findInFiles(
10661076
sandboxPath: string,
1067-
pattern: string
1077+
pattern: string,
10681078
): Promise<SandboxFileSearchResult[]> {
10691079
const resp = await beamClient.request({
10701080
method: "POST",
@@ -1078,7 +1088,7 @@ export class SandboxFileSystem {
10781088
};
10791089
if (!data.ok || !data.results)
10801090
throw new SandboxFileSystemError(
1081-
data.errorMsg || "Failed to find in files"
1091+
data.errorMsg || "Failed to find in files",
10821092
);
10831093

10841094
const results: SandboxFileSearchResult[] = [];
@@ -1090,15 +1100,15 @@ export class SandboxFileSystem {
10901100
new SandboxFileSearchRange(
10911101
new SandboxFilePosition(
10921102
match.range.start.line,
1093-
match.range.start.column
1103+
match.range.start.column,
10941104
),
10951105
new SandboxFilePosition(
10961106
match.range.end.line,
1097-
match.range.end.column
1098-
)
1107+
match.range.end.column,
1108+
),
10991109
),
1100-
match.content
1101-
)
1110+
match.content,
1111+
),
11021112
);
11031113
}
11041114
results.push(new SandboxFileSearchResult(result.path, matches));

0 commit comments

Comments
 (0)