@dibssoftware/dibs 0.0.0-stage → 0.0.178
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/CHANGELOG.md +577 -0
- package/LICENSE +670 -0
- package/README.md +234 -2
- package/bin/dibs-guard.mjs +7 -0
- package/bin/dibs.mjs +526 -0
- package/docs/benchmarks.md +193 -0
- package/docs/files.md +30 -0
- package/docs/guard.md +91 -0
- package/docs/later.md +43 -0
- package/docs/machines.md +147 -0
- package/docs/questions.md +78 -0
- package/docs/relay.md +196 -0
- package/docs/releasing.md +25 -0
- package/docs/scheduling.md +163 -0
- package/docs/simulators.md +17 -0
- package/package.json +44 -4
- package/skills/dibs/SKILL.md +162 -0
- package/src/acquire.mjs +697 -0
- package/src/admin.mjs +145 -0
- package/src/agents.mjs +145 -0
- package/src/alerts.mjs +437 -0
- package/src/answers.mjs +65 -0
- package/src/board.mjs +202 -0
- package/src/broker.mjs +369 -0
- package/src/commands.mjs +801 -0
- package/src/config.mjs +320 -0
- package/src/connect.mjs +106 -0
- package/src/cool.mjs +160 -0
- package/src/diskwarn.mjs +161 -0
- package/src/events.mjs +94 -0
- package/src/files.mjs +1100 -0
- package/src/guard.mjs +583 -0
- package/src/history.mjs +247 -0
- package/src/hogs.mjs +214 -0
- package/src/later.mjs +487 -0
- package/src/measure.mjs +262 -0
- package/src/nodecheck.mjs +17 -0
- package/src/notices.mjs +121 -0
- package/src/ondemand.mjs +572 -0
- package/src/overlap.mjs +151 -0
- package/src/override.mjs +351 -0
- package/src/owner.mjs +30 -0
- package/src/plain.mjs +467 -0
- package/src/platform.mjs +357 -0
- package/src/preempt.mjs +438 -0
- package/src/primer.mjs +61 -0
- package/src/proc.mjs +98 -0
- package/src/progress.mjs +158 -0
- package/src/qr.mjs +204 -0
- package/src/questions.mjs +389 -0
- package/src/quota.mjs +81 -0
- package/src/reads.mjs +54 -0
- package/src/relay.mjs +779 -0
- package/src/remote.mjs +584 -0
- package/src/reset.mjs +127 -0
- package/src/schedule.mjs +995 -0
- package/src/scratch.mjs +61 -0
- package/src/serve.mjs +736 -0
- package/src/service.mjs +221 -0
- package/src/shard.mjs +59 -0
- package/src/simctl.mjs +42 -0
- package/src/simpool.mjs +258 -0
- package/src/state.mjs +201 -0
- package/src/stats.mjs +328 -0
- package/src/style.mjs +44 -0
- package/src/talk.mjs +523 -0
- package/src/thin.sh +130 -0
- package/src/trust.mjs +152 -0
- package/src/util.mjs +184 -0
- package/src/version.mjs +2 -0
- package/src/web.mjs +1117 -0
- package/src/worker.mjs +318 -0
package/src/worker.mjs
ADDED
|
@@ -0,0 +1,318 @@
|
|
|
1
|
+
// dibs worker: take jobs from the coordinator and run them here, each through
|
|
2
|
+
// this machine's own dibs queue, so local agents and remote jobs take turns.
|
|
3
|
+
import fs from 'node:fs';
|
|
4
|
+
import os from 'node:os';
|
|
5
|
+
import path from 'node:path';
|
|
6
|
+
import { spawn } from 'node:child_process';
|
|
7
|
+
import { fileURLToPath } from 'node:url';
|
|
8
|
+
import { HOME, machine, resCfg } from './config.mjs';
|
|
9
|
+
import { withState, history } from './state.mjs';
|
|
10
|
+
import { used } from './schedule.mjs';
|
|
11
|
+
import { markIdle } from './acquire.mjs';
|
|
12
|
+
import { boardOf } from './board.mjs';
|
|
13
|
+
import { singleInstance } from './proc.mjs';
|
|
14
|
+
import { OS, stopTree } from './platform.mjs';
|
|
15
|
+
import { REMOTE, remoteConf, call, tarCreate, tarExtract, safeRel, copyKey, LOST } from './remote.mjs';
|
|
16
|
+
import { readRules, ruleAllows, isRuleFile } from './trust.mjs';
|
|
17
|
+
import { now, sleep, bytes, log, SIGNALS, signalExit, LEAN } from './util.mjs';
|
|
18
|
+
import { VERSION } from './version.mjs';
|
|
19
|
+
import { runLater } from './later.mjs';
|
|
20
|
+
import { unpatched } from './nodecheck.mjs';
|
|
21
|
+
|
|
22
|
+
const BIN = path.join(path.dirname(fileURLToPath(import.meta.url)), '..', 'bin', 'dibs.mjs');
|
|
23
|
+
const PIDFILE = path.join(HOME, 'worker.pid');
|
|
24
|
+
const WORK = path.join(HOME, 'work');
|
|
25
|
+
const FLUSH = 250;
|
|
26
|
+
const REJOIN_CHECK = 3000;
|
|
27
|
+
// --ephemeral, a worker on an on-demand VM (src/ondemand.mjs): with its
|
|
28
|
+
// coordinator out of reach this long (the forward dropped, the coordinator
|
|
29
|
+
// stopped), it leaves, since nobody is left to stop the VM's work or read it.
|
|
30
|
+
// DIBS_LOST_EXIT_MS lets a test see it leave in seconds.
|
|
31
|
+
const EPHEMERAL_LOST = Number(process.env.DIBS_LOST_EXIT_MS) || 60e3;
|
|
32
|
+
// With a job running it waits longer: a coordinator restart (every release does
|
|
33
|
+
// one) comes back and picks the job up again, and the job keeps the VM busy, so
|
|
34
|
+
// nothing stops it as idle meanwhile.
|
|
35
|
+
const EPHEMERAL_LOST_BUSY = 10 * 6e4;
|
|
36
|
+
|
|
37
|
+
// What the coordinator needs to pick a machine: size, load, each resource's
|
|
38
|
+
// line here and the copies kept here. Plus this machine's board, for other machines to show.
|
|
39
|
+
function report(cfg, name, running, inUse = new Set()) {
|
|
40
|
+
const m = machine();
|
|
41
|
+
const hist = history();
|
|
42
|
+
const { busy, board } = withState(s => {
|
|
43
|
+
markIdle(s);
|
|
44
|
+
const out = {};
|
|
45
|
+
const names = new Set([...Object.keys(cfg.resources), ...s.leases.flatMap(l => l.res), ...s.queue.flatMap(q => q.res)]);
|
|
46
|
+
for (const r of names) out[r] = { used: used(s, r), slots: resCfg(cfg, r).slots, waiting: s.queue.filter(q => q.pid && q.res.includes(r)).length };
|
|
47
|
+
return { busy: out, board: boardOf(s, cfg, hist) };
|
|
48
|
+
});
|
|
49
|
+
return { name, os: OS, cores: m.cores ?? os.cpus().length, memTotal: os.totalmem(), memFree: m.memFree, load: m.load,
|
|
50
|
+
diskFree: m.diskFree, version: VERSION, busy, running, copies: copies(inUse), board };
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// The copies kept here, by project: when one was last used and how many aren't
|
|
54
|
+
// in use by a job right now. Names and times only, nothing from inside them.
|
|
55
|
+
export function copies(inUse = new Set(), work = WORK) {
|
|
56
|
+
const out = {};
|
|
57
|
+
let names = [];
|
|
58
|
+
try { names = fs.readdirSync(work, { withFileTypes: true }).filter(e => e.isDirectory()).map(e => e.name); } catch { return out; }
|
|
59
|
+
for (const n of names) {
|
|
60
|
+
const key = n.match(/^(.+-[0-9a-f]{12})(-\d+)?$/)?.[1];
|
|
61
|
+
if (!key) continue;
|
|
62
|
+
const dir = path.join(work, n);
|
|
63
|
+
let at;
|
|
64
|
+
try { at = fs.statSync(`${dir}.files`).mtimeMs; } catch { continue; } // never finished unpacking
|
|
65
|
+
const c = out[key] ||= { used: 0, free: 0 };
|
|
66
|
+
c.used = Math.max(c.used, Math.round(at));
|
|
67
|
+
if (!inUse.has(dir)) c.free++;
|
|
68
|
+
}
|
|
69
|
+
return out;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// A folder per sender and working copy, reused between jobs so installed
|
|
73
|
+
// dependencies and build caches survive. Two jobs at once get two folders.
|
|
74
|
+
function workspace(job, inUse) {
|
|
75
|
+
const base = copyKey(job.from, job.root);
|
|
76
|
+
for (let n = 1; ; n++) {
|
|
77
|
+
const dir = path.join(WORK, n === 1 ? base : `${base}-${n}`);
|
|
78
|
+
if (!inUse.has(dir)) return dir;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// A job's copy, deleted, and its file list. A job can leave folders it can't
|
|
83
|
+
// be deleted from (a read-only node_modules/.bin): on 2026-10-20 one such copy
|
|
84
|
+
// threw EACCES, crashed boat-1's worker, and took every job running there with it.
|
|
85
|
+
// So what's read-only is made writable first, and a copy that still won't go
|
|
86
|
+
// is left for the next cleanup, said in the log.
|
|
87
|
+
function dropCopy(d) {
|
|
88
|
+
const rm = () => { fs.rmSync(d, { recursive: true, force: true }); fs.rmSync(`${d}.files`, { force: true }); };
|
|
89
|
+
try { rm(); return true; } catch {}
|
|
90
|
+
const open = p => {
|
|
91
|
+
try { fs.chmodSync(p, 0o700); } catch {}
|
|
92
|
+
let list = [];
|
|
93
|
+
try { list = fs.readdirSync(p, { withFileTypes: true }); } catch {}
|
|
94
|
+
for (const e of list) if (e.isDirectory() && !e.isSymbolicLink()) open(path.join(p, e.name));
|
|
95
|
+
};
|
|
96
|
+
open(d);
|
|
97
|
+
try { rm(); return true; } catch (e) { log(`couldn't delete the copy ${path.basename(d)}: ${e.message}`); return false; }
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// Copies nobody has sent work to for a week are deleted, and when disk runs
|
|
101
|
+
// low every copy not in use goes, oldest first.
|
|
102
|
+
const KEEP_WORK = 7 * 24 * 3600e3;
|
|
103
|
+
export function cleanWork(cfg, inUse, work = WORK) {
|
|
104
|
+
let dirs = [];
|
|
105
|
+
try { dirs = fs.readdirSync(work, { withFileTypes: true }).filter(e => e.isDirectory()).map(e => path.join(work, e.name)); } catch { return; }
|
|
106
|
+
const used = d => { try { return fs.statSync(`${d}.files`).mtimeMs; } catch { try { return fs.statSync(d).mtimeMs; } catch { return 0; } } };
|
|
107
|
+
const idle = dirs.filter(d => !inUse.has(d)).sort((a, b) => used(a) - used(b));
|
|
108
|
+
const low = () => { const free = machine().diskFree; return free != null && free < 2 * bytes(cfg.minDisk); };
|
|
109
|
+
for (const d of idle) {
|
|
110
|
+
if (now() - used(d) < KEEP_WORK && !low()) continue;
|
|
111
|
+
if (dropCopy(d)) log(`deleted the unused copy ${path.basename(d)}`);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// Files the last job had that this one doesn't were deleted by the sender.
|
|
116
|
+
function prune(dir, files) {
|
|
117
|
+
const list = `${dir}.files`;
|
|
118
|
+
let old = [];
|
|
119
|
+
try { old = JSON.parse(fs.readFileSync(list, 'utf8')); } catch {}
|
|
120
|
+
const keep = new Set(files);
|
|
121
|
+
for (const f of old) {
|
|
122
|
+
const rel = safeRel(f);
|
|
123
|
+
if (rel && !keep.has(rel)) fs.rmSync(path.join(dir, rel), { force: true });
|
|
124
|
+
}
|
|
125
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
126
|
+
fs.writeFileSync(list, JSON.stringify(files));
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// 404 too: a coordinator that just restarted doesn't know the job until this
|
|
130
|
+
// worker's next check-in tells it (src/serve.mjs adopt), so the output and the
|
|
131
|
+
// exit code wait for that rather than being lost.
|
|
132
|
+
async function retry(fn, what, ms = 60e3) {
|
|
133
|
+
const t = now();
|
|
134
|
+
for (;;) {
|
|
135
|
+
try { return await fn(); } catch (e) {
|
|
136
|
+
if (e.status && e.status !== 404 && e.status !== 502 && e.status !== 503) throw e;
|
|
137
|
+
if (now() - t > ms) throw new Error(`${what}: ${e.message}`);
|
|
138
|
+
await sleep(1000);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
export async function worker(o, cfg) {
|
|
144
|
+
{ const hole = unpatched(); if (hole) console.error(`warning: ${hole}. Then dibs service install worker on a fixed Node replaces this one.`); }
|
|
145
|
+
// Scheduled jobs (dibs later) are this machine's own, so they run whether or not it ever joins a coordinator.
|
|
146
|
+
runLater();
|
|
147
|
+
let rc = remoteConf();
|
|
148
|
+
if (!rc) {
|
|
149
|
+
// As a service it may start before dibs serve or dibs join has written the address.
|
|
150
|
+
console.log('not connected to a coordinator yet; waiting for dibs join (or dibs serve on this machine)');
|
|
151
|
+
while (!(rc = remoteConf())) await sleep(5000);
|
|
152
|
+
}
|
|
153
|
+
let name = o.name || rc.name;
|
|
154
|
+
await singleInstance(PIDFILE, 'a worker is already running here');
|
|
155
|
+
const running = new Map(); // job id -> { dir, stop() }
|
|
156
|
+
let quitting = false;
|
|
157
|
+
const quit = () => {
|
|
158
|
+
if (quitting) return;
|
|
159
|
+
quitting = true;
|
|
160
|
+
log(`stopping ${running.size} job${running.size === 1 ? '' : 's'} and leaving`);
|
|
161
|
+
for (const r of running.values()) r.stop(`${name} was shut down`);
|
|
162
|
+
const t = now();
|
|
163
|
+
const check = setInterval(() => {
|
|
164
|
+
if (!running.size || now() - t > 15e3) { try { if (Number(fs.readFileSync(PIDFILE, 'utf8')) === process.pid) fs.rmSync(PIDFILE); } catch {} process.exit(0); }
|
|
165
|
+
}, 200);
|
|
166
|
+
};
|
|
167
|
+
for (const sig of SIGNALS) process.on(sig, quit);
|
|
168
|
+
log(`taking jobs as ${name} from ${rc.url}`);
|
|
169
|
+
let down = 0;
|
|
170
|
+
while (!quitting) {
|
|
171
|
+
let r;
|
|
172
|
+
try {
|
|
173
|
+
// What each running job is, so a coordinator that restarted can pick it up again.
|
|
174
|
+
r = await call(rc, 'POST', '/worker/beat', { ...report(cfg, name, [...running.keys()], new Set([...running.values()].map(x => x.dir))),
|
|
175
|
+
jobs: [...running.values()].map(x => x.job) }, { timeout: 25e3 });
|
|
176
|
+
if (down) log(`back in touch with ${rc.url}`);
|
|
177
|
+
down = 0;
|
|
178
|
+
} catch (e) {
|
|
179
|
+
if (e.status === 401 || e.status === 403) {
|
|
180
|
+
// Exiting would only get us restarted by launchd or systemd, over and over.
|
|
181
|
+
log(`the coordinator at ${rc.url} turned this machine away: ${e.message}. Not taking jobs until dibs join is run here again`);
|
|
182
|
+
for (const r of running.values()) r.stop(`${name} was turned away by the coordinator`);
|
|
183
|
+
const old = JSON.stringify(rc);
|
|
184
|
+
while (!quitting) {
|
|
185
|
+
await sleep(REJOIN_CHECK);
|
|
186
|
+
const n = remoteConf();
|
|
187
|
+
if (n && JSON.stringify(n) !== old) { rc = n; name = o.name || rc.name; log(`${REMOTE} changed; taking jobs as ${name} from ${rc.url}`); break; }
|
|
188
|
+
}
|
|
189
|
+
continue;
|
|
190
|
+
}
|
|
191
|
+
if (!down) log(`can't reach ${rc.url} (${e.message}); retrying`);
|
|
192
|
+
down ||= now();
|
|
193
|
+
const limit = running.size ? EPHEMERAL_LOST_BUSY : EPHEMERAL_LOST;
|
|
194
|
+
if (o.ephemeral && now() - down > limit) { log(`no coordinator for ${Math.round(limit / 1000)}s on an on-demand machine: leaving`); quit(); break; }
|
|
195
|
+
await sleep(5000);
|
|
196
|
+
continue;
|
|
197
|
+
}
|
|
198
|
+
for (const id of r.cancel) running.get(id)?.stop();
|
|
199
|
+
for (const job of r.jobs) start(rc, job, running, cfg, name);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
// The `dibs run` a job becomes here: the sender's flags, in this machine's queue.
|
|
204
|
+
export function runArgs(job) {
|
|
205
|
+
const args = ['run', job.res.join(','), '--as', job.label || 'remote'];
|
|
206
|
+
if (job.purpose) args.push('-m', job.purpose);
|
|
207
|
+
if (job.prio) args.push('-p', job.prio);
|
|
208
|
+
if (job.by) args.push('--by', String(Math.max(1, Math.round((job.by - now()) / 1000))) + 's');
|
|
209
|
+
if (job.est) args.push('--est', `${Math.round(job.est / 1000)}s`);
|
|
210
|
+
// Only a --mem the sender gave: the resource default is this machine's own,
|
|
211
|
+
// and a pool resource adds its simulator on top of any --mem.
|
|
212
|
+
const mem = 'askedMem' in job ? job.askedMem : job.mem;
|
|
213
|
+
if (mem) args.push('--mem', String(mem));
|
|
214
|
+
if (job.cpu != null) args.push('--cpu', String(job.cpu));
|
|
215
|
+
if (job.disk) args.push('--disk', String(job.disk));
|
|
216
|
+
if (job.wait) args.push('--wait');
|
|
217
|
+
else if (job.maxWait != null) args.push(job.maxWait === 0 ? '--no-wait' : '--max-wait', ...(job.maxWait === 0 ? [] : [`${Math.round(job.maxWait / 1000)}s`]));
|
|
218
|
+
for (const p of job.ports || []) args.push('--port', p);
|
|
219
|
+
args.push('--', ...job.cmd);
|
|
220
|
+
return args;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
function start(rc, job, running, cfg, name) {
|
|
224
|
+
const inUse = new Set([...running.values()].map(r => r.dir));
|
|
225
|
+
const dir = workspace(job, inUse);
|
|
226
|
+
try { cleanWork(cfg, new Set([...inUse, dir])); } catch (e) { log(`cleaning ~/.dibs/work: ${e.message}`); }
|
|
227
|
+
let child = null, stopped = false, stopWhy = null, killTimer = null, ended = false;
|
|
228
|
+
// dibs run passes SIGTERM on to the job's process tree and releases its lease.
|
|
229
|
+
const kill = () => {
|
|
230
|
+
if (!child || killTimer) return;
|
|
231
|
+
try { child.kill('SIGTERM'); } catch {}
|
|
232
|
+
// If dibs run doesn't finish in time, kill it; the next state read then
|
|
233
|
+
// reaps its lease and kills what the job left running.
|
|
234
|
+
killTimer = setTimeout(() => { stopTree(child.pid, 'SIGKILL'); setTimeout(() => withState(() => {}), 500); }, 10e3);
|
|
235
|
+
};
|
|
236
|
+
const entry = {
|
|
237
|
+
dir,
|
|
238
|
+
job: { id: job.id, from: job.from, who: job.who, agent: job.agent, label: job.label, res: job.res, cmd: job.cmd, purpose: job.purpose, created: job.created, bring: job.bring },
|
|
239
|
+
// why is for the sender when someone else stopped it; a sender's own cancel needs none.
|
|
240
|
+
stop: why => {
|
|
241
|
+
if (stopped) return;
|
|
242
|
+
stopped = true;
|
|
243
|
+
stopWhy = why || null;
|
|
244
|
+
kill();
|
|
245
|
+
},
|
|
246
|
+
};
|
|
247
|
+
running.set(job.id, entry);
|
|
248
|
+
log(`job ${job.id} from ${job.agent || job.who}@${job.from}: ${job.cmd.join(' ').slice(0, 100)}`);
|
|
249
|
+
|
|
250
|
+
// Output goes up in order, a batch at a time.
|
|
251
|
+
let pending = [], sending = Promise.resolve();
|
|
252
|
+
const flush = () => {
|
|
253
|
+
if (!pending.length) return sending;
|
|
254
|
+
const chunks = pending.map(([s, d]) => [s, d.toString('base64')]);
|
|
255
|
+
pending = [];
|
|
256
|
+
sending = sending.then(() => retry(() => call(rc, 'POST', `/jobs/${job.id}/out`, { chunks }), 'sending output')
|
|
257
|
+
.then(a => { if (a.cancel) entry.stop(); }, e => log(`job ${job.id}: ${e.message}`)));
|
|
258
|
+
return sending;
|
|
259
|
+
};
|
|
260
|
+
const timer = setInterval(flush, FLUSH);
|
|
261
|
+
const tellSender = msg => pending.push([2, Buffer.from(`dibs@${rc.name || 'worker'}: ${msg}\n`)]);
|
|
262
|
+
|
|
263
|
+
const end = async (code, why) => {
|
|
264
|
+
if (ended) return;
|
|
265
|
+
ended = true;
|
|
266
|
+
clearInterval(timer);
|
|
267
|
+
clearTimeout(killTimer);
|
|
268
|
+
await flush();
|
|
269
|
+
const cwd = path.join(dir, job.cwdRel || '');
|
|
270
|
+
const want = (job.bring || []).map(safeRel).filter(Boolean);
|
|
271
|
+
const have = want.filter(p => fs.existsSync(path.join(cwd, p)));
|
|
272
|
+
if (want.length && !stopped) {
|
|
273
|
+
if (have.length < want.length) { tellSender(`nothing at ${want.filter(p => !have.includes(p)).join(', ')} to bring back`); await flush(); }
|
|
274
|
+
if (have.length) {
|
|
275
|
+
try { await retry(() => call(rc, 'PUT', `/jobs/${job.id}/artifacts`, tarCreate(cwd, have), { timeout: 300e3 }), 'sending files back'); }
|
|
276
|
+
catch (e) { tellSender(`couldn't send ${have.join(', ')} back: ${e.message}`); await flush(); }
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
try { await retry(() => call(rc, 'POST', `/jobs/${job.id}/done`, { code, why: stopWhy || why }), 'reporting the exit code'); }
|
|
280
|
+
catch (e) { log(`job ${job.id}: ${e.message}`); }
|
|
281
|
+
running.delete(job.id);
|
|
282
|
+
// The copy stays, on an on-demand VM too: its disk outlives each wake, and the
|
|
283
|
+
// next job from the project gets only the changed files, its node_modules and
|
|
284
|
+
// build output as the last job left them, and the browsers in ~/.cache. A copy
|
|
285
|
+
// nobody uses for a week, or any idle one when the disk runs low, goes (cleanWork).
|
|
286
|
+
log(`job ${job.id} ended: exit ${code}`);
|
|
287
|
+
};
|
|
288
|
+
|
|
289
|
+
(async () => {
|
|
290
|
+
// The sender checked its project's rule, and so did the coordinator; this is the last door.
|
|
291
|
+
if (job.stayOn && job.from !== name && !job.stayOn.includes(name)) return end(2, `${name} refused it: ${job.rule || 'the project rule'}`);
|
|
292
|
+
try {
|
|
293
|
+
const buf = await retry(() => call(rc, 'GET', `/jobs/${job.id}/bundle`, undefined, { raw: true, timeout: 300e3 }), 'fetching the working copy');
|
|
294
|
+
if (stopped) return end(130);
|
|
295
|
+
prune(dir, (job.files || []).map(safeRel).filter(Boolean));
|
|
296
|
+
tarExtract(dir, buf);
|
|
297
|
+
} catch (e) { return end(LOST, `${rc.name} couldn't unpack the working copy: ${e.message}`); }
|
|
298
|
+
// The copy's own .dibs.json files, read here rather than taken on the sender's word.
|
|
299
|
+
const rule = readRules((job.files || []).map(safeRel).filter(f => f && isRuleFile(f)).map(f => path.join(dir, f)));
|
|
300
|
+
if (!ruleAllows(rule, name, job.from)) {
|
|
301
|
+
dropCopy(dir);
|
|
302
|
+
return end(2, `${name} refused it and deleted the copy: ${rule.text}`);
|
|
303
|
+
}
|
|
304
|
+
if (job.cwdRel && !safeRel(job.cwdRel)) return end(2, `bad working folder ${job.cwdRel}`);
|
|
305
|
+
const cwd = path.join(dir, job.cwdRel || '');
|
|
306
|
+
fs.mkdirSync(cwd, { recursive: true });
|
|
307
|
+
// Through bin/dibs.mjs's sh lines, so the job waits thin here like a local one (Node on Windows).
|
|
308
|
+
const argv = OS === 'windows' ? [process.execPath, ...LEAN, BIN] : ['/bin/sh', BIN];
|
|
309
|
+
const env = { ...process.env, DIBS_AGENT: `${job.agent || job.who}@${job.from}`, DIBS_NO_SESSION: '1', DIBS_REMOTE_JOB: String(job.id), ...(typeof job.repo === 'string' && job.repo && { DIBS_REPO: job.repo }) };
|
|
310
|
+
delete env.DIBS_AS;
|
|
311
|
+
child = spawn(argv[0], [...argv.slice(1), ...runArgs(job)], { cwd, env, stdio: ['ignore', 'pipe', 'pipe'], detached: OS !== 'windows', windowsHide: true });
|
|
312
|
+
child.stdout.on('data', d => pending.push([1, d]));
|
|
313
|
+
child.stderr.on('data', d => pending.push([2, d]));
|
|
314
|
+
if (stopped) kill();
|
|
315
|
+
child.on('error', e => end(127, `couldn't start dibs: ${e.message}`));
|
|
316
|
+
child.on('exit', (code, sig) => end(code ?? signalExit(sig)));
|
|
317
|
+
})();
|
|
318
|
+
}
|