sandcovery 0.6.2 → 0.9.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 +45 -29
- package/dist/cli/index.mjs +14826 -2
- package/dist/cli/sandcovery-docker.mjs +8552 -4
- package/dist/cli/sandcovery-kubectl.mjs +9 -4
- package/dist/cli/sandcovery-t3.d.mts +1 -0
- package/dist/cli/sandcovery-t3.mjs +223 -0
- package/dist/index.d.mts +8737 -142
- package/dist/index.mjs +1 -1
- package/dist/kubectl-Ck04daor.mjs +472 -0
- package/dist/saved-sandboxes-BMjkAVGx.mjs +389 -0
- package/dist/src-DjOLw-ft.mjs +36591 -0
- package/dist/sync-rEFjHNC4.mjs +4171 -0
- package/dist/third-party-licenses.md +630 -0
- package/dist/types-DotsDqXt.d.mts +1264 -0
- package/dist/types.d.mts +2 -2
- package/dist/types.mjs +6 -1
- package/package.json +13 -10
- package/dist/Schema-CoX0a2_o.mjs +0 -18812
- package/dist/handlers-CpLa3dvg.mjs +0 -19715
- package/dist/port-forwarder-DvSPr-Tg.mjs +0 -5360
- package/dist/posthog-project-BpOd2VxW.mjs +0 -1
- package/dist/posthog-project-CdWw_ofg.mjs +0 -22
- package/dist/src-BqfeIyqj.mjs +0 -2047
- package/dist/start-D1LPu-aN.mjs +0 -1604
- package/dist/telemetry-DBPKT8Ap.mjs +0 -43
- package/dist/telemetry-delivery-BSGibWPJ.mjs +0 -8712
- package/dist/types-C4amb2QE.d.mts +0 -793
package/dist/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { t as Sandcovery } from "./src-
|
|
1
|
+
export { t as Sandcovery } from "./src-DjOLw-ft.mjs";
|
|
@@ -0,0 +1,472 @@
|
|
|
1
|
+
import { C as SandboxId, Ct as Number$1, Fa as isSuccess, Kt as fromJsonString, Mt as Struct, Ra as try_, Ut as decodeUnknownOption, Wt as decodeUnknownSync, Xa as exists, Za as filter, _o as some, ao as getOrElse, at as increment, ct as update, ho as none, io as fromUndefinedOr, jt as String, lo as isNone, mo as match, no as fromNullOr, nt as get, oo as getOrNull, ot as make, po as map, st as set, t as Sandcovery, tt as decrement, uo as isSome, vo as toArray } from "./src-DjOLw-ft.mjs";
|
|
2
|
+
import { C as clamp, _ as servedOrSpawned, b as KUBERNETES_GATEWAY_COMMAND, f as writePrivateFile, g as servedAt, h as isTerminal, i as launchedRelay, m as READY, n as savedSandboxes, o as DESTROY_TIMEOUT_MS, s as createdSandbox, t as runningSandbox, u as stopSignalOf, v as socketOf, w as noop, x as KUBERNETES_PORT, y as KUBERNETES_COMMAND } from "./saved-sandboxes-BMjkAVGx.mjs";
|
|
3
|
+
import os from "node:os";
|
|
4
|
+
import { spawn } from "node:child_process";
|
|
5
|
+
import net from "node:net";
|
|
6
|
+
import fs from "node:fs";
|
|
7
|
+
import path from "node:path";
|
|
8
|
+
import readline from "node:readline";
|
|
9
|
+
import { once } from "node:events";
|
|
10
|
+
import { setTimeout as setTimeout$1 } from "node:timers/promises";
|
|
11
|
+
// src/cli/kubectl-idle.ts
|
|
12
|
+
const IDLE_WATCHER_ENV = "__SANDCOVERY_KUBECTL_IDLE";
|
|
13
|
+
const IDLE_SOCKET_NAME = "kubectl-idle";
|
|
14
|
+
const IDLE_MS = 9e5;
|
|
15
|
+
const HEARTBEAT_MS = 6e4;
|
|
16
|
+
const REFUSED_STATUSES = /* @__PURE__ */ new Set([
|
|
17
|
+
400,
|
|
18
|
+
401,
|
|
19
|
+
403,
|
|
20
|
+
404,
|
|
21
|
+
409,
|
|
22
|
+
410,
|
|
23
|
+
422
|
|
24
|
+
]);
|
|
25
|
+
const ClusterLine = Struct({
|
|
26
|
+
expires_at: String,
|
|
27
|
+
key: String,
|
|
28
|
+
sandbox: String
|
|
29
|
+
});
|
|
30
|
+
const trackedOf = ({ expires_at, key, sandbox }) => ({
|
|
31
|
+
expiresAt: Date.parse(expires_at),
|
|
32
|
+
key,
|
|
33
|
+
sandbox
|
|
34
|
+
});
|
|
35
|
+
const UNWATCHED$1 = {
|
|
36
|
+
cluster: none(),
|
|
37
|
+
destroying: none(),
|
|
38
|
+
due: 0,
|
|
39
|
+
holders: 0
|
|
40
|
+
};
|
|
41
|
+
const decodeWatcher = decodeUnknownSync(fromJsonString(Struct({
|
|
42
|
+
idleMs: Number$1,
|
|
43
|
+
socketPath: String
|
|
44
|
+
})));
|
|
45
|
+
const decodeHold = decodeUnknownOption(fromJsonString(Struct({ origin: String })));
|
|
46
|
+
const decodeAnswered = decodeUnknownOption(Struct({ cause: Struct({ status: Number$1 }) }));
|
|
47
|
+
const decodeCluster = decodeUnknownOption(fromJsonString(ClusterLine));
|
|
48
|
+
const ended = async ({ cluster, origin }) => {
|
|
49
|
+
try {
|
|
50
|
+
await new Sandcovery({
|
|
51
|
+
apiKey: cluster.key,
|
|
52
|
+
baseUrl: origin
|
|
53
|
+
}).sandboxes.destroy(cluster.sandbox, { signal: AbortSignal.timeout(DESTROY_TIMEOUT_MS) });
|
|
54
|
+
return true;
|
|
55
|
+
} catch (error) {
|
|
56
|
+
if (!(error instanceof Error)) throw error;
|
|
57
|
+
return isTerminal(error) || exists(decodeAnswered(error), ({ cause }) => REFUSED_STATUSES.has(cause.status));
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
const destroyed = async ({ entry, origin, settle }) => {
|
|
61
|
+
const { cluster } = get(entry);
|
|
62
|
+
if (isSome(cluster) && await ended({
|
|
63
|
+
cluster: cluster.value,
|
|
64
|
+
origin
|
|
65
|
+
})) update(entry, (watching) => ({
|
|
66
|
+
...watching,
|
|
67
|
+
cluster: watching.cluster.pipe(filter(({ sandbox }) => sandbox !== cluster.value.sandbox)),
|
|
68
|
+
destroying: none()
|
|
69
|
+
}));
|
|
70
|
+
else update(entry, (watching) => ({
|
|
71
|
+
...watching,
|
|
72
|
+
destroying: none(),
|
|
73
|
+
due: Date.now() + HEARTBEAT_MS
|
|
74
|
+
}));
|
|
75
|
+
settle();
|
|
76
|
+
};
|
|
77
|
+
const watchIdle = async (config) => {
|
|
78
|
+
const { idleMs, socketPath } = decodeWatcher(config);
|
|
79
|
+
const watched = /* @__PURE__ */ new Map();
|
|
80
|
+
const listener = net.createServer();
|
|
81
|
+
const connections = make(0);
|
|
82
|
+
const timer = make(null);
|
|
83
|
+
const check = () => {
|
|
84
|
+
const now = Date.now();
|
|
85
|
+
const waiting = get(timer);
|
|
86
|
+
if (waiting !== null) clearTimeout(waiting);
|
|
87
|
+
for (const [origin, entry] of watched) {
|
|
88
|
+
const current = get(entry);
|
|
89
|
+
const live = {
|
|
90
|
+
...current,
|
|
91
|
+
cluster: current.cluster.pipe(filter(({ expiresAt }) => now < expiresAt))
|
|
92
|
+
};
|
|
93
|
+
set(entry, live);
|
|
94
|
+
if (isSome(live.cluster) && live.holders === 0 && isNone(live.destroying) && now >= live.due) set(entry, {
|
|
95
|
+
...live,
|
|
96
|
+
destroying: some(destroyed({
|
|
97
|
+
entry,
|
|
98
|
+
origin,
|
|
99
|
+
settle: check
|
|
100
|
+
}))
|
|
101
|
+
});
|
|
102
|
+
const settled = get(entry);
|
|
103
|
+
if (isNone(settled.cluster) && settled.holders === 0 && isNone(settled.destroying)) watched.delete(origin);
|
|
104
|
+
}
|
|
105
|
+
if (get(connections) === 0 && watched.size === 0) {
|
|
106
|
+
listener.close();
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
const next = Math.min(now + HEARTBEAT_MS, ...[...watched.values()].flatMap((entry) => {
|
|
110
|
+
const { cluster, destroying, due, holders } = get(entry);
|
|
111
|
+
const dueAt = holders === 0 ? match(destroying, {
|
|
112
|
+
onNone: () => [due],
|
|
113
|
+
onSome: () => []
|
|
114
|
+
}) : [];
|
|
115
|
+
return toArray(cluster).flatMap(({ expiresAt }) => [expiresAt, ...dueAt]);
|
|
116
|
+
}));
|
|
117
|
+
set(timer, setTimeout(check, clamp(next - now, 0, HEARTBEAT_MS)));
|
|
118
|
+
};
|
|
119
|
+
const held = async (connection, origin) => {
|
|
120
|
+
const entry = watched.get(origin) ?? make({
|
|
121
|
+
...UNWATCHED$1,
|
|
122
|
+
due: Date.now() + idleMs
|
|
123
|
+
});
|
|
124
|
+
update(entry, (watching) => ({
|
|
125
|
+
...watching,
|
|
126
|
+
holders: watching.holders + 1
|
|
127
|
+
}));
|
|
128
|
+
watched.set(origin, entry);
|
|
129
|
+
await getOrNull(get(entry).destroying);
|
|
130
|
+
connection.write(READY);
|
|
131
|
+
return entry;
|
|
132
|
+
};
|
|
133
|
+
listener.on("connection", (connection) => {
|
|
134
|
+
const hold = make(null);
|
|
135
|
+
const lines = readline.createInterface({ input: connection });
|
|
136
|
+
increment(connections);
|
|
137
|
+
connection.on("error", noop);
|
|
138
|
+
const received = async (line) => {
|
|
139
|
+
const holding = get(hold);
|
|
140
|
+
if (holding !== null) {
|
|
141
|
+
const entry = await holding;
|
|
142
|
+
const cluster = decodeCluster(line).pipe(map(trackedOf), filter(({ expiresAt }) => !Number.isNaN(expiresAt)));
|
|
143
|
+
if (isNone(cluster)) {
|
|
144
|
+
connection.destroy();
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
update(entry, (watching) => ({
|
|
148
|
+
...watching,
|
|
149
|
+
cluster
|
|
150
|
+
}));
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
const asked = decodeHold(line);
|
|
154
|
+
if (isNone(asked)) {
|
|
155
|
+
connection.destroy();
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
set(hold, held(connection, asked.value.origin));
|
|
159
|
+
};
|
|
160
|
+
const released = async () => {
|
|
161
|
+
const entry = await get(hold);
|
|
162
|
+
decrement(connections);
|
|
163
|
+
lines.close();
|
|
164
|
+
if (entry !== null) update(entry, (watching) => ({
|
|
165
|
+
...watching,
|
|
166
|
+
due: Date.now() + idleMs,
|
|
167
|
+
holders: watching.holders - 1
|
|
168
|
+
}));
|
|
169
|
+
check();
|
|
170
|
+
};
|
|
171
|
+
lines.on("line", (line) => {
|
|
172
|
+
received(line);
|
|
173
|
+
});
|
|
174
|
+
connection.once("close", () => {
|
|
175
|
+
released();
|
|
176
|
+
});
|
|
177
|
+
});
|
|
178
|
+
const serving = await servedAt(listener, socketPath);
|
|
179
|
+
process.stdout.write(READY);
|
|
180
|
+
if (serving) set(timer, setTimeout(check, HEARTBEAT_MS));
|
|
181
|
+
};
|
|
182
|
+
const holdCluster = async ({ entry, folder, origin }) => {
|
|
183
|
+
const socketPath = socketOf({
|
|
184
|
+
folder,
|
|
185
|
+
name: IDLE_SOCKET_NAME
|
|
186
|
+
});
|
|
187
|
+
const socket = await servedOrSpawned({
|
|
188
|
+
attempt: async () => {
|
|
189
|
+
const connection = net.connect(socketPath);
|
|
190
|
+
await once(connection, "connect");
|
|
191
|
+
return connection;
|
|
192
|
+
},
|
|
193
|
+
env: { [IDLE_WATCHER_ENV]: JSON.stringify({
|
|
194
|
+
idleMs: IDLE_MS,
|
|
195
|
+
socketPath
|
|
196
|
+
}) },
|
|
197
|
+
entry
|
|
198
|
+
});
|
|
199
|
+
socket.on("error", noop);
|
|
200
|
+
const { promise: ready, reject, resolve } = Promise.withResolvers();
|
|
201
|
+
socket.once("data", () => {
|
|
202
|
+
resolve(null);
|
|
203
|
+
});
|
|
204
|
+
socket.once("close", () => {
|
|
205
|
+
reject(/* @__PURE__ */ new Error("The idle watcher stopped before it answered."));
|
|
206
|
+
});
|
|
207
|
+
socket.write(`${JSON.stringify({ origin })}\n`);
|
|
208
|
+
try {
|
|
209
|
+
await ready;
|
|
210
|
+
} catch (error) {
|
|
211
|
+
socket.destroy();
|
|
212
|
+
throw error;
|
|
213
|
+
}
|
|
214
|
+
return {
|
|
215
|
+
attach: (cluster) => {
|
|
216
|
+
socket.write(`${JSON.stringify(cluster)}\n`);
|
|
217
|
+
},
|
|
218
|
+
release: () => {
|
|
219
|
+
socket.end();
|
|
220
|
+
socket.unref();
|
|
221
|
+
}
|
|
222
|
+
};
|
|
223
|
+
};
|
|
224
|
+
// src/cli/commands/kubectl.ts
|
|
225
|
+
const SANDBOXES_FILE = "kubectl.json";
|
|
226
|
+
const KUBECONFIG_FOLDER = "kubeconfig";
|
|
227
|
+
const CONTEXT_NAME = "sandcovery";
|
|
228
|
+
const SLUGS = ["freestyle/2x4", "boxd/2x8"];
|
|
229
|
+
const START_TIMEOUT_SECONDS = 300;
|
|
230
|
+
const ANSWER_WAIT_MS = 12e4;
|
|
231
|
+
const ANSWER_POLL_MS = 1e3;
|
|
232
|
+
const ANSWER_REQUEST_MS = 1e4;
|
|
233
|
+
const UNWATCHED = "Can't stop the cluster after 15 idle minutes, so it runs until its TTL.";
|
|
234
|
+
const KUBECTL = process.platform === "win32" ? "kubectl.exe" : "kubectl";
|
|
235
|
+
const LOCAL_COMMANDS = /* @__PURE__ */ new Set([
|
|
236
|
+
"completion",
|
|
237
|
+
"config",
|
|
238
|
+
"help",
|
|
239
|
+
"kustomize",
|
|
240
|
+
"options",
|
|
241
|
+
"plugin"
|
|
242
|
+
]);
|
|
243
|
+
const HELP_FLAGS = /* @__PURE__ */ new Set(["--help", "-h"]);
|
|
244
|
+
const CLIENT_FLAGS = /* @__PURE__ */ new Set(["--client", "--client=true"]);
|
|
245
|
+
const GLOBAL_VALUE_FLAGS = /* @__PURE__ */ new Set([
|
|
246
|
+
"--as",
|
|
247
|
+
"--as-group",
|
|
248
|
+
"--as-uid",
|
|
249
|
+
"--cache-dir",
|
|
250
|
+
"--certificate-authority",
|
|
251
|
+
"--client-certificate",
|
|
252
|
+
"--client-key",
|
|
253
|
+
"--cluster",
|
|
254
|
+
"--context",
|
|
255
|
+
"--kubeconfig",
|
|
256
|
+
"--log-flush-frequency",
|
|
257
|
+
"--namespace",
|
|
258
|
+
"--password",
|
|
259
|
+
"--profile",
|
|
260
|
+
"--profile-output",
|
|
261
|
+
"--request-timeout",
|
|
262
|
+
"--server",
|
|
263
|
+
"--tls-server-name",
|
|
264
|
+
"--token",
|
|
265
|
+
"--user",
|
|
266
|
+
"--username",
|
|
267
|
+
"--v",
|
|
268
|
+
"--vmodule",
|
|
269
|
+
"-n",
|
|
270
|
+
"-s",
|
|
271
|
+
"-v"
|
|
272
|
+
]);
|
|
273
|
+
const READY_STATUSES = /* @__PURE__ */ new Set([200, 401]);
|
|
274
|
+
const answers = async ({ deadline, tool, url }) => {
|
|
275
|
+
try {
|
|
276
|
+
const response = await fetch(url, {
|
|
277
|
+
redirect: "manual",
|
|
278
|
+
signal: AbortSignal.any([tool.signal, AbortSignal.timeout(ANSWER_REQUEST_MS)])
|
|
279
|
+
});
|
|
280
|
+
await response.body?.cancel();
|
|
281
|
+
if (READY_STATUSES.has(response.status)) return true;
|
|
282
|
+
} catch (error) {
|
|
283
|
+
if (tool.signal.aborted) throw error;
|
|
284
|
+
}
|
|
285
|
+
if (Date.now() + ANSWER_POLL_MS > deadline) return false;
|
|
286
|
+
await setTimeout$1(ANSWER_POLL_MS, null, { signal: tool.signal });
|
|
287
|
+
return await answers({
|
|
288
|
+
deadline,
|
|
289
|
+
tool,
|
|
290
|
+
url
|
|
291
|
+
});
|
|
292
|
+
};
|
|
293
|
+
const startCluster = async ({ kubeconfig, tool }) => {
|
|
294
|
+
const state = {
|
|
295
|
+
file: path.join(tool.folder, SANDBOXES_FILE),
|
|
296
|
+
schema: SandboxId
|
|
297
|
+
};
|
|
298
|
+
const saved = fromUndefinedOr(savedSandboxes(state)[tool.origin]);
|
|
299
|
+
const running = await runningSandbox({
|
|
300
|
+
id: saved,
|
|
301
|
+
tool
|
|
302
|
+
});
|
|
303
|
+
if (isSome(running) && fs.existsSync(kubeconfig)) return running.value;
|
|
304
|
+
if (isSome(running)) await tool.client.sandboxes.destroy(running.value.id, { signal: tool.signal });
|
|
305
|
+
const sandbox = await createdSandbox({
|
|
306
|
+
request: {
|
|
307
|
+
api_key_id: null,
|
|
308
|
+
disk_mib: null,
|
|
309
|
+
egress: null,
|
|
310
|
+
env: null,
|
|
311
|
+
gpu: null,
|
|
312
|
+
idle_timeout_seconds: null,
|
|
313
|
+
image: null,
|
|
314
|
+
os: null,
|
|
315
|
+
ports: [KUBERNETES_PORT],
|
|
316
|
+
provider: null,
|
|
317
|
+
region: null,
|
|
318
|
+
slug: null,
|
|
319
|
+
slugs: SLUGS,
|
|
320
|
+
snapshot_id: null,
|
|
321
|
+
ttl_seconds: null
|
|
322
|
+
},
|
|
323
|
+
tool
|
|
324
|
+
});
|
|
325
|
+
const provider = sandbox.provider ?? "its provider";
|
|
326
|
+
try {
|
|
327
|
+
const start = await tool.client.sandboxes.exec(sandbox.id, {
|
|
328
|
+
command: KUBERNETES_COMMAND,
|
|
329
|
+
cwd: null,
|
|
330
|
+
env: null,
|
|
331
|
+
timeout_seconds: START_TIMEOUT_SECONDS
|
|
332
|
+
}, { signal: tool.signal });
|
|
333
|
+
const token = start.stdout.trim();
|
|
334
|
+
if (start.exit_code !== 0 || token === "" || token.includes("\n") || token.includes(" ")) throw new Error(`Can't run kubectl. Kubernetes didn't start in sandbox ${sandbox.id} on ${provider}, so Sandcovery destroyed it.`, { cause: start });
|
|
335
|
+
const gateway = await launchedRelay({
|
|
336
|
+
command: KUBERNETES_GATEWAY_COMMAND,
|
|
337
|
+
id: sandbox.id,
|
|
338
|
+
tool
|
|
339
|
+
});
|
|
340
|
+
if (gateway.code !== 0) throw new Error(`Can't run kubectl. The Sandcovery relay for the Kubernetes API didn't start in sandbox ${sandbox.id} on ${provider}, so Sandcovery destroyed it.`, { cause: gateway });
|
|
341
|
+
const preview = await tool.client.sandboxes.portPreview(sandbox.id, KUBERNETES_PORT, { signal: tool.signal });
|
|
342
|
+
const server = preview.url.endsWith("/") ? preview.url.slice(0, -1) : preview.url;
|
|
343
|
+
if (!await answers({
|
|
344
|
+
deadline: Date.now() + ANSWER_WAIT_MS,
|
|
345
|
+
url: `${server}/version`,
|
|
346
|
+
tool
|
|
347
|
+
})) throw new Error(`Can't run kubectl. The cluster in sandbox ${sandbox.id} on ${provider} didn't answer at ${server} within 2 minutes, so Sandcovery destroyed it.`);
|
|
348
|
+
writePrivateFile({
|
|
349
|
+
file: kubeconfig,
|
|
350
|
+
text: `${JSON.stringify({
|
|
351
|
+
apiVersion: "v1",
|
|
352
|
+
clusters: [{
|
|
353
|
+
cluster: { server },
|
|
354
|
+
name: CONTEXT_NAME
|
|
355
|
+
}],
|
|
356
|
+
contexts: [{
|
|
357
|
+
context: {
|
|
358
|
+
cluster: CONTEXT_NAME,
|
|
359
|
+
user: CONTEXT_NAME
|
|
360
|
+
},
|
|
361
|
+
name: CONTEXT_NAME
|
|
362
|
+
}],
|
|
363
|
+
"current-context": CONTEXT_NAME,
|
|
364
|
+
kind: "Config",
|
|
365
|
+
users: [{
|
|
366
|
+
name: CONTEXT_NAME,
|
|
367
|
+
user: { token }
|
|
368
|
+
}]
|
|
369
|
+
}, null, 2)}\n`
|
|
370
|
+
});
|
|
371
|
+
} catch (error) {
|
|
372
|
+
await tool.client.sandboxes.destroy(sandbox.id, { signal: AbortSignal.timeout(DESTROY_TIMEOUT_MS) });
|
|
373
|
+
throw error;
|
|
374
|
+
}
|
|
375
|
+
writePrivateFile({
|
|
376
|
+
file: state.file,
|
|
377
|
+
text: `${JSON.stringify({
|
|
378
|
+
...savedSandboxes(state),
|
|
379
|
+
[tool.origin]: sandbox.id
|
|
380
|
+
}, null, 2)}\n`
|
|
381
|
+
});
|
|
382
|
+
return sandbox;
|
|
383
|
+
};
|
|
384
|
+
const held = async (tool) => {
|
|
385
|
+
const { entry } = tool;
|
|
386
|
+
if (isNone(entry)) return none();
|
|
387
|
+
try {
|
|
388
|
+
return some(await holdCluster({
|
|
389
|
+
entry: entry.value,
|
|
390
|
+
folder: tool.folder,
|
|
391
|
+
origin: tool.origin
|
|
392
|
+
}));
|
|
393
|
+
} catch (error) {
|
|
394
|
+
if (!(error instanceof Error)) throw error;
|
|
395
|
+
tool.io.stderr.write(`${UNWATCHED} ${error.message}\n`);
|
|
396
|
+
return none();
|
|
397
|
+
}
|
|
398
|
+
};
|
|
399
|
+
const ran = async ({ kubeconfig, kubectl, tool }) => {
|
|
400
|
+
const { promise, reject, resolve } = Promise.withResolvers();
|
|
401
|
+
const child = spawn(kubectl, tool.args, {
|
|
402
|
+
env: {
|
|
403
|
+
...process.env,
|
|
404
|
+
KUBECONFIG: kubeconfig
|
|
405
|
+
},
|
|
406
|
+
stdio: "inherit"
|
|
407
|
+
});
|
|
408
|
+
const forwarding = new AbortController();
|
|
409
|
+
child.once("error", reject);
|
|
410
|
+
child.once("close", (code, signal) => {
|
|
411
|
+
const signalled = match(fromNullOr(signal), {
|
|
412
|
+
onNone: () => 0,
|
|
413
|
+
onSome: (name) => os.constants.signals[name]
|
|
414
|
+
});
|
|
415
|
+
resolve(code ?? 128 + signalled);
|
|
416
|
+
});
|
|
417
|
+
tool.signal.addEventListener("abort", () => {
|
|
418
|
+
child.kill(getOrElse(stopSignalOf(tool.signal), () => "SIGTERM"));
|
|
419
|
+
}, {
|
|
420
|
+
once: true,
|
|
421
|
+
signal: forwarding.signal
|
|
422
|
+
});
|
|
423
|
+
try {
|
|
424
|
+
return await promise;
|
|
425
|
+
} finally {
|
|
426
|
+
forwarding.abort();
|
|
427
|
+
}
|
|
428
|
+
};
|
|
429
|
+
const onPath = (name) => fromUndefinedOr((process.env.PATH ?? "").split(path.delimiter).flatMap((folder) => path.isAbsolute(folder) ? [path.join(folder, name)] : []).find((file) => {
|
|
430
|
+
const found = try_(() => {
|
|
431
|
+
fs.accessSync(file, fs.constants.X_OK);
|
|
432
|
+
return fs.statSync(file).isFile();
|
|
433
|
+
});
|
|
434
|
+
return isSuccess(found) && found.success;
|
|
435
|
+
}));
|
|
436
|
+
const kubectlRun = async (tool) => {
|
|
437
|
+
const kubectl = onPath(KUBECTL);
|
|
438
|
+
if (isNone(kubectl)) throw new Error("Can't run kubectl. It isn't on PATH.");
|
|
439
|
+
if (process.argv.slice(1, 2).some((own) => fs.realpathSync(kubectl.value) === fs.realpathSync(own))) throw new Error(`Can't run kubectl. ${kubectl.value} on PATH is sandcovery-kubectl itself.`);
|
|
440
|
+
const kubeconfig = path.join(tool.folder, KUBECONFIG_FOLDER, encodeURIComponent(tool.origin));
|
|
441
|
+
const end = tool.args.indexOf("--");
|
|
442
|
+
const words = end === -1 ? tool.args : tool.args.slice(0, end);
|
|
443
|
+
if (match(fromUndefinedOr(words.find((word, index) => !word.startsWith("-") && !GLOBAL_VALUE_FLAGS.has(words[index - 1] ?? ""))), {
|
|
444
|
+
onNone: () => true,
|
|
445
|
+
onSome: (command) => LOCAL_COMMANDS.has(command) || command === "version" && !CLIENT_FLAGS.isDisjointFrom(new Set(words))
|
|
446
|
+
}) || !HELP_FLAGS.isDisjointFrom(new Set(words))) return await ran({
|
|
447
|
+
kubeconfig,
|
|
448
|
+
tool,
|
|
449
|
+
kubectl: kubectl.value
|
|
450
|
+
});
|
|
451
|
+
const hold = await held(tool);
|
|
452
|
+
try {
|
|
453
|
+
const sandbox = await startCluster({
|
|
454
|
+
kubeconfig,
|
|
455
|
+
tool
|
|
456
|
+
});
|
|
457
|
+
const { key } = tool;
|
|
458
|
+
if (isSome(hold) && isSome(key)) hold.value.attach({
|
|
459
|
+
expires_at: sandbox.expires_at,
|
|
460
|
+
key: key.value,
|
|
461
|
+
sandbox: sandbox.id
|
|
462
|
+
});
|
|
463
|
+
return await ran({
|
|
464
|
+
kubeconfig,
|
|
465
|
+
tool,
|
|
466
|
+
kubectl: kubectl.value
|
|
467
|
+
});
|
|
468
|
+
} finally {
|
|
469
|
+
if (isSome(hold)) hold.value.release();
|
|
470
|
+
}
|
|
471
|
+
};
|
|
472
|
+
export { watchIdle as a, IDLE_WATCHER_ENV as i, kubectlRun as n, onPath as r, answers as t };
|