@insforge/instabox-sdk 0.1.0 → 0.2.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.
- package/README.md +41 -14
- package/dist/index.cjs +89 -6
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +114 -7
- package/dist/index.d.ts +114 -7
- package/dist/index.js +89 -6
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/skills/instabox/SKILL.md +23 -10
package/dist/index.d.ts
CHANGED
|
@@ -100,11 +100,59 @@ interface Machine {
|
|
|
100
100
|
timeoutAt?: string | null;
|
|
101
101
|
archiveAt?: string | null;
|
|
102
102
|
onTimeout?: OnTimeout;
|
|
103
|
+
/** Idle seconds after which the server pauses the running machine; null when it never does. */
|
|
104
|
+
autoSuspendAfterSeconds?: number | null;
|
|
105
|
+
/** When the server paused it for idleness; cleared by the next explicit operation. */
|
|
106
|
+
autoSuspendedAt?: string | null;
|
|
103
107
|
/** String labels, stored by the server. */
|
|
104
108
|
metadata?: Record<string, string>;
|
|
109
|
+
/**
|
|
110
|
+
* The machine gets the account's Vault items at every boot and resume; false, once set, is for good.
|
|
111
|
+
* Absent on servers without the Vault: treat as true.
|
|
112
|
+
*/
|
|
113
|
+
vault?: boolean;
|
|
105
114
|
createdAt: string;
|
|
106
115
|
updatedAt: string;
|
|
107
116
|
}
|
|
117
|
+
/** One Vault item a machine gets (`GET /v1/machines/{id}/vault`). Values are never returned. */
|
|
118
|
+
interface MachineVaultItem {
|
|
119
|
+
name: string;
|
|
120
|
+
/** `env`: a variable exported to every login shell. `file`: a file at `path` under the home directory. */
|
|
121
|
+
kind: 'env' | 'file';
|
|
122
|
+
path?: string;
|
|
123
|
+
}
|
|
124
|
+
/** A file the Vault wrote: its home-relative path and the sha256 (hex) of its content. */
|
|
125
|
+
interface VaultFile {
|
|
126
|
+
path: string;
|
|
127
|
+
sha256: string;
|
|
128
|
+
}
|
|
129
|
+
/** A file that no longer applies but was left in place (changed in the sandbox since, or not writable). */
|
|
130
|
+
interface VaultKept {
|
|
131
|
+
path: string;
|
|
132
|
+
reason: string;
|
|
133
|
+
}
|
|
134
|
+
/** One write of the Vault into a machine. */
|
|
135
|
+
interface VaultApplied {
|
|
136
|
+
at: string;
|
|
137
|
+
/** The variables exported. */
|
|
138
|
+
env: string[];
|
|
139
|
+
/** The files written (or, when the write failed, those of the last write). */
|
|
140
|
+
files: VaultFile[];
|
|
141
|
+
/** Files of an earlier write removed because they no longer apply. */
|
|
142
|
+
removed?: string[];
|
|
143
|
+
kept?: VaultKept[];
|
|
144
|
+
/** Why the write failed, when it did. */
|
|
145
|
+
error?: string;
|
|
146
|
+
}
|
|
147
|
+
/** Response of `GET /v1/machines/{id}/vault`. */
|
|
148
|
+
interface MachineVault {
|
|
149
|
+
/** The machine takes the Vault. */
|
|
150
|
+
enabled: boolean;
|
|
151
|
+
/** The items that apply with the machine's current metadata (none when `enabled` is false). */
|
|
152
|
+
items: MachineVaultItem[];
|
|
153
|
+
/** The last write into the machine; null before one. */
|
|
154
|
+
applied: VaultApplied | null;
|
|
155
|
+
}
|
|
108
156
|
/** What happens at `timeoutAt`: `pause` suspends the machine, `kill` deletes it. */
|
|
109
157
|
type OnTimeout = 'pause' | 'kill';
|
|
110
158
|
interface MachineList {
|
|
@@ -141,6 +189,8 @@ interface Template {
|
|
|
141
189
|
digest: string;
|
|
142
190
|
arch: string;
|
|
143
191
|
defaultSpec: MachineSpec;
|
|
192
|
+
/** True when the server's release no longer builds it: listed, but new sandboxes are refused. */
|
|
193
|
+
hidden?: boolean;
|
|
144
194
|
}
|
|
145
195
|
interface SnapshotList {
|
|
146
196
|
snapshots: Snapshot[];
|
|
@@ -171,12 +221,12 @@ interface ExecResponse {
|
|
|
171
221
|
error?: string;
|
|
172
222
|
}
|
|
173
223
|
|
|
174
|
-
declare const DEFAULT_API_URL = "https://api.
|
|
224
|
+
declare const DEFAULT_API_URL = "https://api.instabox.sh";
|
|
175
225
|
/** Options shared by every call that talks to the API. */
|
|
176
226
|
interface ConnectionOptions {
|
|
177
227
|
/** API key from the instabox dashboard's API Keys page. Default: env `INSTABOX_API_KEY`. */
|
|
178
228
|
apiKey?: string;
|
|
179
|
-
/** API base URL. Default: env `INSTABOX_API_URL`, else `https://api.
|
|
229
|
+
/** API base URL. Default: env `INSTABOX_API_URL`, else `https://api.instabox.sh`. */
|
|
180
230
|
apiUrl?: string;
|
|
181
231
|
/** Timeout of a single HTTP request in ms. Default 60 000; exec defaults to its command timeout plus 30 000. */
|
|
182
232
|
requestTimeoutMs?: number;
|
|
@@ -204,7 +254,7 @@ declare class HttpClient {
|
|
|
204
254
|
private readonly apiKey;
|
|
205
255
|
private readonly requestTimeoutMs?;
|
|
206
256
|
private readonly fetchImpl;
|
|
207
|
-
|
|
257
|
+
readonly pollIntervalMs: number;
|
|
208
258
|
private readonly retryDelayMs;
|
|
209
259
|
constructor(opts?: ConnectionOptions);
|
|
210
260
|
request<T>(method: string, path: string, opts?: RequestOptions): Promise<T>;
|
|
@@ -296,7 +346,9 @@ declare class Files {
|
|
|
296
346
|
exists(path: string): Promise<boolean>;
|
|
297
347
|
}
|
|
298
348
|
|
|
299
|
-
declare const DEFAULT_TEMPLATE = "
|
|
349
|
+
declare const DEFAULT_TEMPLATE = "agent-browser";
|
|
350
|
+
/** The default when a server does not offer `DEFAULT_TEMPLATE` (an older or self-hosted one). */
|
|
351
|
+
declare const FALLBACK_TEMPLATE = "universal";
|
|
300
352
|
interface SandboxOpts extends ConnectionOptions {
|
|
301
353
|
/** Machine name. Default `sbx-<random>`. */
|
|
302
354
|
name?: string;
|
|
@@ -313,6 +365,13 @@ interface SandboxOpts extends ConnectionOptions {
|
|
|
313
365
|
onTimeout?: OnTimeout;
|
|
314
366
|
/** Stop and archive after this many milliseconds, independent of the pause/kill timeout. */
|
|
315
367
|
archiveMs?: number;
|
|
368
|
+
/**
|
|
369
|
+
* The server pauses the sandbox once it has been idle this long (no agent working, no terminal use,
|
|
370
|
+
* little CPU); the next command, file read or SSH sign-in resumes it. Sent as whole seconds, 2 min to
|
|
371
|
+
* 24 h. Default: 30 minutes. `0`: never, the sandbox stays on until paused or killed. Change it later
|
|
372
|
+
* with `setAutoSuspend()`.
|
|
373
|
+
*/
|
|
374
|
+
autoSuspendMs?: number;
|
|
316
375
|
/** vCPUs. Default: the template's. */
|
|
317
376
|
vcpus?: number;
|
|
318
377
|
/** Memory in MiB. Default: the template's. */
|
|
@@ -321,6 +380,13 @@ interface SandboxOpts extends ConnectionOptions {
|
|
|
321
380
|
diskGiB?: number;
|
|
322
381
|
/** Root disk size in GiB; may only grow. */
|
|
323
382
|
rootDiskGiB?: number;
|
|
383
|
+
/**
|
|
384
|
+
* `false`: the sandbox never gets the account's Vault items (the secrets the dashboard's Vault page or
|
|
385
|
+
* `instabox vault set` keeps, written into every sandbox of the account at boot and resume). Pass it for
|
|
386
|
+
* sandboxes made for other people: anything running in a sandbox can read what the Vault writes into it.
|
|
387
|
+
* Default: the sandbox gets them. Cannot be turned on again later.
|
|
388
|
+
*/
|
|
389
|
+
vault?: boolean;
|
|
324
390
|
}
|
|
325
391
|
interface SandboxConnectOpts extends ConnectionOptions {
|
|
326
392
|
/** Resume the sandbox if it is paused (suspended) or stopped. Default true. */
|
|
@@ -340,12 +406,16 @@ interface SandboxInfo {
|
|
|
340
406
|
name: string;
|
|
341
407
|
/** String labels stored with the sandbox. */
|
|
342
408
|
metadata: Record<string, string>;
|
|
409
|
+
/** The sandbox gets the account's Vault items at boot and resume (`vault: false` at creation, or `disableVault()`, turns this off for good). */
|
|
410
|
+
vault: boolean;
|
|
343
411
|
/** Creation time of the machine. */
|
|
344
412
|
startedAt: Date;
|
|
345
413
|
/** When the server pauses or kills the sandbox (see `onTimeout`); undefined for no timeout. */
|
|
346
414
|
endAt?: Date;
|
|
347
415
|
archiveAt?: Date;
|
|
348
416
|
onTimeout: OnTimeout;
|
|
417
|
+
/** Idle time after which the server pauses the sandbox; undefined when it never does. */
|
|
418
|
+
autoSuspendMs?: number;
|
|
349
419
|
state: MachineState;
|
|
350
420
|
/** The raw machine object from the API. */
|
|
351
421
|
machine: Machine;
|
|
@@ -362,8 +432,12 @@ interface ForkOpts {
|
|
|
362
432
|
/** Timeout of the copy, as in `SandboxOpts.timeoutMs`. Default: none (the source's is not copied). */
|
|
363
433
|
timeoutMs?: number;
|
|
364
434
|
onTimeout?: OnTimeout;
|
|
435
|
+
/** Idle pause of the copy, as in `SandboxOpts.autoSuspendMs`. Default: 30 minutes (the source's is not copied); `0`: never. */
|
|
436
|
+
autoSuspendMs?: number;
|
|
365
437
|
/** Metadata of the copy. Default: the source's; `{}` for none. */
|
|
366
438
|
metadata?: Record<string, string>;
|
|
439
|
+
/** `false`: the copy never gets the account's Vault items (a copy of a sandbox without the Vault never does). */
|
|
440
|
+
vault?: boolean;
|
|
367
441
|
/**
|
|
368
442
|
* A fork needs a stopped source. When true (default), `fork()` stops the source first if needed
|
|
369
443
|
* (running processes end, `/data` is kept) and leaves it stopped; call `resume()` to start it again.
|
|
@@ -403,7 +477,9 @@ declare class Sandbox {
|
|
|
403
477
|
/**
|
|
404
478
|
* Creates a sandbox and waits until it is running.
|
|
405
479
|
* `template` is a template ID, a private snapshot name or a snapshot ID (`sn_...`).
|
|
406
|
-
* Template IDs take precedence over snapshot names. Default template: `universal
|
|
480
|
+
* Template IDs take precedence over snapshot names. Default template: `agent-browser` (universal
|
|
481
|
+
* with a desktop and browser preinstalled, which the dashboard's Computer tab shows), or
|
|
482
|
+
* `universal` on a server that does not offer it.
|
|
407
483
|
*/
|
|
408
484
|
static create(opts?: SandboxOpts): Promise<Sandbox>;
|
|
409
485
|
static create(template: string, opts?: SandboxOpts): Promise<Sandbox>;
|
|
@@ -419,7 +495,10 @@ declare class Sandbox {
|
|
|
419
495
|
static getSnapshot(snapshotId: string, opts?: ConnectionOptions): Promise<Snapshot>;
|
|
420
496
|
/** Deletes a snapshot. Returns false if absent. Requires images:delete. */
|
|
421
497
|
static deleteSnapshot(snapshotId: string, opts?: ConnectionOptions): Promise<boolean>;
|
|
422
|
-
/**
|
|
498
|
+
/**
|
|
499
|
+
* Saves a stopped sandbox's disks as a private snapshot. Requires images:write. Waits first for a
|
|
500
|
+
* recent stop's upload to object storage (see `untilSynced`).
|
|
501
|
+
*/
|
|
423
502
|
createSnapshot(opts: CreateSnapshotOpts): Promise<Snapshot>;
|
|
424
503
|
/** Fetches the current state of the sandbox. */
|
|
425
504
|
getInfo(): Promise<SandboxInfo>;
|
|
@@ -430,8 +509,29 @@ declare class Sandbox {
|
|
|
430
509
|
setTimeout(timeoutMs: number, opts?: {
|
|
431
510
|
onTimeout?: OnTimeout;
|
|
432
511
|
}): Promise<void>;
|
|
512
|
+
/**
|
|
513
|
+
* Pauses the sandbox once it has been idle `idleMs` (whole seconds, 2 min to 24 h); 0 turns the idle
|
|
514
|
+
* pause off. Unlike a timeout the setting stays across pause and resume.
|
|
515
|
+
*/
|
|
516
|
+
setAutoSuspend(idleMs: number): Promise<void>;
|
|
433
517
|
/** Replaces the sandbox's metadata (`{}` clears it). */
|
|
434
518
|
setMetadata(metadata: Record<string, string>): Promise<void>;
|
|
519
|
+
/**
|
|
520
|
+
* What the Vault writes into this sandbox: whether it takes the Vault, the items that apply with its
|
|
521
|
+
* current metadata (names and paths, never values) and the last write. Requires machines:read.
|
|
522
|
+
* Throws `not_found` on a server without the Vault.
|
|
523
|
+
*/
|
|
524
|
+
vault(): Promise<MachineVault>;
|
|
525
|
+
/**
|
|
526
|
+
* Writes the account's current Vault items into the running sandbox now, the same write as after a boot,
|
|
527
|
+
* and returns its record (`error` set when the guest refused it). Requires machines:write.
|
|
528
|
+
*/
|
|
529
|
+
pushVault(): Promise<VaultApplied>;
|
|
530
|
+
/**
|
|
531
|
+
* Turns the Vault off for this sandbox, for good, in any state: its next boot, resume or push removes
|
|
532
|
+
* what the Vault wrote and nothing is written again. Requires machines:write.
|
|
533
|
+
*/
|
|
534
|
+
disableVault(): Promise<void>;
|
|
435
535
|
/** True if the sandbox is running (fetches fresh state). */
|
|
436
536
|
isRunning(): Promise<boolean>;
|
|
437
537
|
/** Snapshots memory and stops the sandbox (suspend). `resume()` or `Sandbox.connect()` brings it back. */
|
|
@@ -460,7 +560,14 @@ declare class Sandbox {
|
|
|
460
560
|
* Every state-dependent method calls this first, so it never decides from a stale cache.
|
|
461
561
|
*/
|
|
462
562
|
private settle;
|
|
563
|
+
/**
|
|
564
|
+
* Waits until a stopped sandbox's disks are in object storage (`storage.synced`). A stop returns
|
|
565
|
+
* once the host has the snapshot and uploads it after, so a fork or snapshot right after a stop
|
|
566
|
+
* was refused ("not uploaded to object storage"), which took long enough after a pause and resume
|
|
567
|
+
* to fail every time. Only a stopped sandbox is waited for; the server says what else is wrong.
|
|
568
|
+
*/
|
|
569
|
+
private untilSynced;
|
|
463
570
|
private mutate;
|
|
464
571
|
}
|
|
465
572
|
|
|
466
|
-
export { type ApiError, CommandExitError, type CommandHandle, type CommandOptions, type CommandResult, Commands, type Connection, type ConnectionOptions, type CreateSnapshotOpts, DEFAULT_API_URL, DEFAULT_TEMPLATE, type EntryInfo, type ExecResponse, Files, type ForkOpts, type HostTrust, InstaboxAuthError, InstaboxError, type InstaboxErrorInit, InstaboxQuotaError, InstaboxTimeoutError, MAX_FILE_BYTES, type Machine, type MachineList, type MachineSpec, type MachineState, type OnTimeout, type Operation, Sandbox, type SandboxConnectOpts, type SandboxInfo, type SandboxListOpts, type SandboxOpts, type Snapshot, type SnapshotList, type SshCredentials, type Template, type TemplateRef, type WriteInfo };
|
|
573
|
+
export { type ApiError, CommandExitError, type CommandHandle, type CommandOptions, type CommandResult, Commands, type Connection, type ConnectionOptions, type CreateSnapshotOpts, DEFAULT_API_URL, DEFAULT_TEMPLATE, type EntryInfo, type ExecResponse, FALLBACK_TEMPLATE, Files, type ForkOpts, type HostTrust, InstaboxAuthError, InstaboxError, type InstaboxErrorInit, InstaboxQuotaError, InstaboxTimeoutError, MAX_FILE_BYTES, type Machine, type MachineList, type MachineSpec, type MachineState, type MachineVault, type MachineVaultItem, type OnTimeout, type Operation, Sandbox, type SandboxConnectOpts, type SandboxInfo, type SandboxListOpts, type SandboxOpts, type Snapshot, type SnapshotList, type SshCredentials, type Template, type TemplateRef, type VaultApplied, type VaultFile, type VaultKept, type WriteInfo };
|
package/dist/index.js
CHANGED
|
@@ -241,12 +241,12 @@ var Files = class {
|
|
|
241
241
|
async list(path) {
|
|
242
242
|
const p = shellQuote(path);
|
|
243
243
|
const r = await this.commands.exec(
|
|
244
|
-
`[ -d ${p} ] || { echo "no such directory: "${p} >&2; exit 3; }; cd ${p} && for f in .* *; do case "$f" in .|..) continue ;; esac; [ -e "$f" ] || [ -L "$f" ] || continue; if [ -d "$f" ]; then printf 'd\\t%s\\
|
|
244
|
+
`[ -d ${p} ] || { echo "no such directory: "${p} >&2; exit 3; }; cd ${p} && for f in .* *; do case "$f" in .|..) continue ;; esac; [ -e "$f" ] || [ -L "$f" ] || continue; if [ -d "$f" ]; then printf 'd\\t%s\\0' "$f"; else printf 'f\\t%s\\0' "$f"; fi; done`
|
|
245
245
|
);
|
|
246
246
|
if (r.exitCode === 3) throw new InstaboxError(`directory not found: ${path}`, { status: 404, code: "not_found" });
|
|
247
247
|
ensureComplete(r);
|
|
248
248
|
const base = path.endsWith("/") ? path.slice(0, -1) : path;
|
|
249
|
-
return r.stdout.split("\
|
|
249
|
+
return r.stdout.split("\0").filter(Boolean).map((line) => {
|
|
250
250
|
const tab = line.indexOf(" ");
|
|
251
251
|
const name = line.slice(tab + 1);
|
|
252
252
|
return { name, path: `${base}/${name}`, type: line.slice(0, tab) === "d" ? "dir" : "file" };
|
|
@@ -269,7 +269,7 @@ function ensureComplete(result) {
|
|
|
269
269
|
}
|
|
270
270
|
|
|
271
271
|
// src/http.ts
|
|
272
|
-
var DEFAULT_API_URL = "https://api.
|
|
272
|
+
var DEFAULT_API_URL = "https://api.instabox.sh";
|
|
273
273
|
var MAX_RETRIES = 3;
|
|
274
274
|
var HttpClient = class {
|
|
275
275
|
apiUrl;
|
|
@@ -408,13 +408,15 @@ var HttpClient = class {
|
|
|
408
408
|
};
|
|
409
409
|
|
|
410
410
|
// src/sandbox.ts
|
|
411
|
-
var DEFAULT_TEMPLATE = "
|
|
411
|
+
var DEFAULT_TEMPLATE = "agent-browser";
|
|
412
|
+
var FALLBACK_TEMPLATE = "universal";
|
|
412
413
|
function toInfo(m) {
|
|
413
414
|
const info = {
|
|
414
415
|
sandboxId: m.id,
|
|
415
416
|
templateId: m.template?.id,
|
|
416
417
|
name: m.name,
|
|
417
418
|
metadata: { ...m.metadata },
|
|
419
|
+
vault: m.vault ?? true,
|
|
418
420
|
startedAt: new Date(m.createdAt),
|
|
419
421
|
onTimeout: m.onTimeout ?? "pause",
|
|
420
422
|
state: m.state,
|
|
@@ -423,8 +425,19 @@ function toInfo(m) {
|
|
|
423
425
|
if (m.snapshot?.id) info.snapshotId = m.snapshot.id;
|
|
424
426
|
if (m.timeoutAt) info.endAt = new Date(m.timeoutAt);
|
|
425
427
|
if (m.archiveAt) info.archiveAt = new Date(m.archiveAt);
|
|
428
|
+
if (m.autoSuspendAfterSeconds) info.autoSuspendMs = m.autoSuspendAfterSeconds * 1e3;
|
|
426
429
|
return info;
|
|
427
430
|
}
|
|
431
|
+
function toIdleSeconds(ms) {
|
|
432
|
+
if (ms === 0) return 0;
|
|
433
|
+
const s = Math.ceil(ms / 1e3);
|
|
434
|
+
if (!Number.isFinite(ms) || s < 120 || s > 86400) {
|
|
435
|
+
throw new InstaboxError(`autoSuspendMs must be 0 or between 120 000 (2 min) and 86 400 000 (24 h); got ${ms}`, {
|
|
436
|
+
code: "bad_request"
|
|
437
|
+
});
|
|
438
|
+
}
|
|
439
|
+
return s;
|
|
440
|
+
}
|
|
428
441
|
function toSeconds(ms, field = "timeoutMs") {
|
|
429
442
|
const s = Math.ceil(ms / 1e3);
|
|
430
443
|
if (!Number.isFinite(ms) || s < 60 || s > 86400) {
|
|
@@ -453,12 +466,13 @@ var Sandbox = class _Sandbox {
|
|
|
453
466
|
files;
|
|
454
467
|
machine;
|
|
455
468
|
static async create(a, b) {
|
|
456
|
-
const template = typeof a === "string" ? a : DEFAULT_TEMPLATE;
|
|
457
469
|
const opts = (typeof a === "string" ? b : a) ?? {};
|
|
458
470
|
const http = new HttpClient(opts);
|
|
459
471
|
const body = { name: opts.name ?? `sbx-${randomSuffix()}` };
|
|
460
472
|
if (opts.timeoutMs !== void 0) body.timeoutSeconds = toSeconds(opts.timeoutMs);
|
|
461
473
|
if (opts.archiveMs !== void 0) body.archiveSeconds = toSeconds(opts.archiveMs, "archiveMs");
|
|
474
|
+
if (opts.autoSuspendMs !== void 0) body.autoSuspendAfterSeconds = toIdleSeconds(opts.autoSuspendMs);
|
|
475
|
+
const template = typeof a === "string" ? a : await defaultTemplate(http);
|
|
462
476
|
Object.assign(body, await resolveSource(http, template));
|
|
463
477
|
if (opts.vcpus !== void 0) body.vcpus = opts.vcpus;
|
|
464
478
|
if (opts.memMiB !== void 0) body.memMiB = opts.memMiB;
|
|
@@ -466,6 +480,7 @@ var Sandbox = class _Sandbox {
|
|
|
466
480
|
if (opts.rootDiskGiB !== void 0) body.rootDiskGiB = opts.rootDiskGiB;
|
|
467
481
|
if (opts.onTimeout) body.onTimeout = opts.onTimeout;
|
|
468
482
|
if (opts.metadata) body.metadata = opts.metadata;
|
|
483
|
+
if (opts.vault === false) body.vault = false;
|
|
469
484
|
const op = await http.request("POST", "/v1/machines", { body });
|
|
470
485
|
const done = await http.waitForOperation(op);
|
|
471
486
|
const machine = await getMachine(http, done.machineId);
|
|
@@ -521,8 +536,13 @@ var Sandbox = class _Sandbox {
|
|
|
521
536
|
throw err;
|
|
522
537
|
}
|
|
523
538
|
}
|
|
524
|
-
/**
|
|
539
|
+
/**
|
|
540
|
+
* Saves a stopped sandbox's disks as a private snapshot. Requires images:write. Waits first for a
|
|
541
|
+
* recent stop's upload to object storage (see `untilSynced`).
|
|
542
|
+
*/
|
|
525
543
|
async createSnapshot(opts) {
|
|
544
|
+
await this.settle();
|
|
545
|
+
await this.untilSynced();
|
|
526
546
|
return this.http.request("POST", `/v1/machines/${enc(this.sandboxId)}/snapshots`, { body: opts });
|
|
527
547
|
}
|
|
528
548
|
/** Fetches the current state of the sandbox. */
|
|
@@ -539,12 +559,45 @@ var Sandbox = class _Sandbox {
|
|
|
539
559
|
if (opts.onTimeout) body.onTimeout = opts.onTimeout;
|
|
540
560
|
this.machine = await this.http.request("PATCH", `/v1/machines/${enc(this.sandboxId)}`, { body });
|
|
541
561
|
}
|
|
562
|
+
/**
|
|
563
|
+
* Pauses the sandbox once it has been idle `idleMs` (whole seconds, 2 min to 24 h); 0 turns the idle
|
|
564
|
+
* pause off. Unlike a timeout the setting stays across pause and resume.
|
|
565
|
+
*/
|
|
566
|
+
async setAutoSuspend(idleMs) {
|
|
567
|
+
this.machine = await this.http.request("PATCH", `/v1/machines/${enc(this.sandboxId)}`, {
|
|
568
|
+
body: { autoSuspendAfterSeconds: toIdleSeconds(idleMs) }
|
|
569
|
+
});
|
|
570
|
+
}
|
|
542
571
|
/** Replaces the sandbox's metadata (`{}` clears it). */
|
|
543
572
|
async setMetadata(metadata) {
|
|
544
573
|
this.machine = await this.http.request("PATCH", `/v1/machines/${enc(this.sandboxId)}`, {
|
|
545
574
|
body: { metadata }
|
|
546
575
|
});
|
|
547
576
|
}
|
|
577
|
+
/**
|
|
578
|
+
* What the Vault writes into this sandbox: whether it takes the Vault, the items that apply with its
|
|
579
|
+
* current metadata (names and paths, never values) and the last write. Requires machines:read.
|
|
580
|
+
* Throws `not_found` on a server without the Vault.
|
|
581
|
+
*/
|
|
582
|
+
async vault() {
|
|
583
|
+
return this.http.request("GET", `/v1/machines/${enc(this.sandboxId)}/vault`);
|
|
584
|
+
}
|
|
585
|
+
/**
|
|
586
|
+
* Writes the account's current Vault items into the running sandbox now, the same write as after a boot,
|
|
587
|
+
* and returns its record (`error` set when the guest refused it). Requires machines:write.
|
|
588
|
+
*/
|
|
589
|
+
async pushVault() {
|
|
590
|
+
return this.http.request("POST", `/v1/machines/${enc(this.sandboxId)}/vault`);
|
|
591
|
+
}
|
|
592
|
+
/**
|
|
593
|
+
* Turns the Vault off for this sandbox, for good, in any state: its next boot, resume or push removes
|
|
594
|
+
* what the Vault wrote and nothing is written again. Requires machines:write.
|
|
595
|
+
*/
|
|
596
|
+
async disableVault() {
|
|
597
|
+
this.machine = await this.http.request("PATCH", `/v1/machines/${enc(this.sandboxId)}`, {
|
|
598
|
+
body: { vault: false }
|
|
599
|
+
});
|
|
600
|
+
}
|
|
548
601
|
/** True if the sandbox is running (fetches fresh state). */
|
|
549
602
|
async isRunning() {
|
|
550
603
|
return (await this.getInfo()).state === "running";
|
|
@@ -572,6 +625,7 @@ var Sandbox = class _Sandbox {
|
|
|
572
625
|
/** Copies this sandbox (root disk and `/data`) into a new sandbox and waits until it runs. */
|
|
573
626
|
async fork(opts = {}) {
|
|
574
627
|
if (opts.timeoutMs !== void 0) toSeconds(opts.timeoutMs);
|
|
628
|
+
if (opts.autoSuspendMs !== void 0) toIdleSeconds(opts.autoSuspendMs);
|
|
575
629
|
await this.settle();
|
|
576
630
|
if (this.machine.state !== "stopped") {
|
|
577
631
|
if (opts.stopSource === false) {
|
|
@@ -581,6 +635,7 @@ var Sandbox = class _Sandbox {
|
|
|
581
635
|
}
|
|
582
636
|
await this.stop();
|
|
583
637
|
}
|
|
638
|
+
await this.untilSynced();
|
|
584
639
|
const body = { name: opts.name ?? `${this.machine.name}-fork-${randomSuffix(4)}` };
|
|
585
640
|
if (opts.vcpus !== void 0) body.vcpus = opts.vcpus;
|
|
586
641
|
if (opts.memMiB !== void 0) body.memMiB = opts.memMiB;
|
|
@@ -588,7 +643,9 @@ var Sandbox = class _Sandbox {
|
|
|
588
643
|
if (opts.rootDiskGiB !== void 0) body.rootDiskGiB = opts.rootDiskGiB;
|
|
589
644
|
if (opts.timeoutMs !== void 0) body.timeoutSeconds = toSeconds(opts.timeoutMs);
|
|
590
645
|
if (opts.onTimeout) body.onTimeout = opts.onTimeout;
|
|
646
|
+
if (opts.autoSuspendMs !== void 0) body.autoSuspendAfterSeconds = toIdleSeconds(opts.autoSuspendMs);
|
|
591
647
|
if (opts.metadata) body.metadata = opts.metadata;
|
|
648
|
+
if (opts.vault === false) body.vault = false;
|
|
592
649
|
const op = await this.http.request("POST", `/v1/machines/${enc(this.sandboxId)}/fork`, { body });
|
|
593
650
|
const done = await this.http.waitForOperation(op);
|
|
594
651
|
return new _Sandbox(this.http, await getMachine(this.http, done.machineId));
|
|
@@ -630,6 +687,27 @@ var Sandbox = class _Sandbox {
|
|
|
630
687
|
this.machine = await getMachine(this.http, this.sandboxId);
|
|
631
688
|
}
|
|
632
689
|
}
|
|
690
|
+
/**
|
|
691
|
+
* Waits until a stopped sandbox's disks are in object storage (`storage.synced`). A stop returns
|
|
692
|
+
* once the host has the snapshot and uploads it after, so a fork or snapshot right after a stop
|
|
693
|
+
* was refused ("not uploaded to object storage"), which took long enough after a pause and resume
|
|
694
|
+
* to fail every time. Only a stopped sandbox is waited for; the server says what else is wrong.
|
|
695
|
+
*/
|
|
696
|
+
async untilSynced() {
|
|
697
|
+
const deadline = Date.now() + this.http.operationTimeoutMs;
|
|
698
|
+
let delay = this.http.pollIntervalMs;
|
|
699
|
+
while (this.machine.state === "stopped" && !this.machine.storage.synced) {
|
|
700
|
+
if (Date.now() + delay > deadline) {
|
|
701
|
+
throw new InstaboxTimeoutError(
|
|
702
|
+
`${this.sandboxId}'s disks were not uploaded to object storage within ${this.http.operationTimeoutMs} ms`,
|
|
703
|
+
{ code: "operation_timeout" }
|
|
704
|
+
);
|
|
705
|
+
}
|
|
706
|
+
await new Promise((r) => setTimeout(r, delay));
|
|
707
|
+
delay = Math.min(delay * 2, 2e3);
|
|
708
|
+
this.machine = await getMachine(this.http, this.sandboxId);
|
|
709
|
+
}
|
|
710
|
+
}
|
|
633
711
|
async mutate(method, action) {
|
|
634
712
|
const op = await this.http.request(method, `/v1/machines/${enc(this.sandboxId)}/${action}`);
|
|
635
713
|
await this.http.waitForOperation(op);
|
|
@@ -649,6 +727,10 @@ async function killMachine(http, id) {
|
|
|
649
727
|
throw err;
|
|
650
728
|
}
|
|
651
729
|
}
|
|
730
|
+
async function defaultTemplate(http) {
|
|
731
|
+
const { templates } = await http.request("GET", "/v1/templates");
|
|
732
|
+
return templates.some((t) => t.id === DEFAULT_TEMPLATE && !t.hidden) ? DEFAULT_TEMPLATE : FALLBACK_TEMPLATE;
|
|
733
|
+
}
|
|
652
734
|
async function resolveSource(http, ref) {
|
|
653
735
|
if (/^sn_[a-z2-7]{26}$/.test(ref)) return { snapshotId: ref };
|
|
654
736
|
const { templates } = await http.request("GET", "/v1/templates");
|
|
@@ -666,6 +748,7 @@ export {
|
|
|
666
748
|
Commands,
|
|
667
749
|
DEFAULT_API_URL,
|
|
668
750
|
DEFAULT_TEMPLATE,
|
|
751
|
+
FALLBACK_TEMPLATE,
|
|
669
752
|
Files,
|
|
670
753
|
InstaboxAuthError,
|
|
671
754
|
InstaboxError,
|