@rcpooley/concord 0.1.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/LICENSE +21 -0
- package/README.md +318 -0
- package/bin/concord.service +69 -0
- package/dist/age.d.ts +20 -0
- package/dist/age.d.ts.map +1 -0
- package/dist/age.js +58 -0
- package/dist/age.js.map +1 -0
- package/dist/cli/main.d.ts +3 -0
- package/dist/cli/main.d.ts.map +1 -0
- package/dist/cli/main.js +301 -0
- package/dist/cli/main.js.map +1 -0
- package/dist/daemon.d.ts +4 -0
- package/dist/daemon.d.ts.map +1 -0
- package/dist/daemon.js +394 -0
- package/dist/daemon.js.map +1 -0
- package/dist/eval-config.d.ts +2 -0
- package/dist/eval-config.d.ts.map +1 -0
- package/dist/eval-config.js +45 -0
- package/dist/eval-config.js.map +1 -0
- package/dist/exec.d.ts +168 -0
- package/dist/exec.d.ts.map +1 -0
- package/dist/exec.js +313 -0
- package/dist/exec.js.map +1 -0
- package/dist/git.d.ts +59 -0
- package/dist/git.d.ts.map +1 -0
- package/dist/git.js +265 -0
- package/dist/git.js.map +1 -0
- package/dist/hook.d.ts +37 -0
- package/dist/hook.d.ts.map +1 -0
- package/dist/hook.js +89 -0
- package/dist/hook.js.map +1 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +15 -0
- package/dist/index.js.map +1 -0
- package/dist/init.d.ts +76 -0
- package/dist/init.d.ts.map +1 -0
- package/dist/init.js +236 -0
- package/dist/init.js.map +1 -0
- package/dist/loader.d.ts +35 -0
- package/dist/loader.d.ts.map +1 -0
- package/dist/loader.js +230 -0
- package/dist/loader.js.map +1 -0
- package/dist/module.d.ts +116 -0
- package/dist/module.d.ts.map +1 -0
- package/dist/module.js +100 -0
- package/dist/module.js.map +1 -0
- package/dist/modules/apt/index.d.ts +45 -0
- package/dist/modules/apt/index.d.ts.map +1 -0
- package/dist/modules/apt/index.js +249 -0
- package/dist/modules/apt/index.js.map +1 -0
- package/dist/modules/apt/schema.d.ts +63 -0
- package/dist/modules/apt/schema.d.ts.map +1 -0
- package/dist/modules/apt/schema.js +75 -0
- package/dist/modules/apt/schema.js.map +1 -0
- package/dist/modules/checkout/index.d.ts +88 -0
- package/dist/modules/checkout/index.d.ts.map +1 -0
- package/dist/modules/checkout/index.js +382 -0
- package/dist/modules/checkout/index.js.map +1 -0
- package/dist/modules/checkout/schema.d.ts +52 -0
- package/dist/modules/checkout/schema.d.ts.map +1 -0
- package/dist/modules/checkout/schema.js +132 -0
- package/dist/modules/checkout/schema.js.map +1 -0
- package/dist/modules/cloudflare/api.d.ts +64 -0
- package/dist/modules/cloudflare/api.d.ts.map +1 -0
- package/dist/modules/cloudflare/api.js +98 -0
- package/dist/modules/cloudflare/api.js.map +1 -0
- package/dist/modules/cloudflare/index.d.ts +74 -0
- package/dist/modules/cloudflare/index.d.ts.map +1 -0
- package/dist/modules/cloudflare/index.js +374 -0
- package/dist/modules/cloudflare/index.js.map +1 -0
- package/dist/modules/cloudflare/schema.d.ts +62 -0
- package/dist/modules/cloudflare/schema.d.ts.map +1 -0
- package/dist/modules/cloudflare/schema.js +85 -0
- package/dist/modules/cloudflare/schema.js.map +1 -0
- package/dist/modules/docker/index.d.ts +83 -0
- package/dist/modules/docker/index.d.ts.map +1 -0
- package/dist/modules/docker/index.js +470 -0
- package/dist/modules/docker/index.js.map +1 -0
- package/dist/modules/docker/registry.d.ts +35 -0
- package/dist/modules/docker/registry.d.ts.map +1 -0
- package/dist/modules/docker/registry.js +95 -0
- package/dist/modules/docker/registry.js.map +1 -0
- package/dist/modules/docker/render.d.ts +45 -0
- package/dist/modules/docker/render.d.ts.map +1 -0
- package/dist/modules/docker/render.js +162 -0
- package/dist/modules/docker/render.js.map +1 -0
- package/dist/modules/docker/schema.d.ts +231 -0
- package/dist/modules/docker/schema.d.ts.map +1 -0
- package/dist/modules/docker/schema.js +335 -0
- package/dist/modules/docker/schema.js.map +1 -0
- package/dist/modules/docker/sweeper.d.ts +64 -0
- package/dist/modules/docker/sweeper.d.ts.map +1 -0
- package/dist/modules/docker/sweeper.js +94 -0
- package/dist/modules/docker/sweeper.js.map +1 -0
- package/dist/modules/file/index.d.ts +79 -0
- package/dist/modules/file/index.d.ts.map +1 -0
- package/dist/modules/file/index.js +305 -0
- package/dist/modules/file/index.js.map +1 -0
- package/dist/modules/file/schema.d.ts +45 -0
- package/dist/modules/file/schema.d.ts.map +1 -0
- package/dist/modules/file/schema.js +114 -0
- package/dist/modules/file/schema.js.map +1 -0
- package/dist/modules/index.d.ts +20 -0
- package/dist/modules/index.d.ts.map +1 -0
- package/dist/modules/index.js +11 -0
- package/dist/modules/index.js.map +1 -0
- package/dist/modules/porkbun/api.d.ts +36 -0
- package/dist/modules/porkbun/api.d.ts.map +1 -0
- package/dist/modules/porkbun/api.js +62 -0
- package/dist/modules/porkbun/api.js.map +1 -0
- package/dist/modules/porkbun/index.d.ts +50 -0
- package/dist/modules/porkbun/index.d.ts.map +1 -0
- package/dist/modules/porkbun/index.js +236 -0
- package/dist/modules/porkbun/index.js.map +1 -0
- package/dist/modules/porkbun/schema.d.ts +38 -0
- package/dist/modules/porkbun/schema.d.ts.map +1 -0
- package/dist/modules/porkbun/schema.js +39 -0
- package/dist/modules/porkbun/schema.js.map +1 -0
- package/dist/modules/script/index.d.ts +43 -0
- package/dist/modules/script/index.d.ts.map +1 -0
- package/dist/modules/script/index.js +116 -0
- package/dist/modules/script/index.js.map +1 -0
- package/dist/modules/script/schema.d.ts +25 -0
- package/dist/modules/script/schema.d.ts.map +1 -0
- package/dist/modules/script/schema.js +63 -0
- package/dist/modules/script/schema.js.map +1 -0
- package/dist/modules/systemd/index.d.ts +93 -0
- package/dist/modules/systemd/index.d.ts.map +1 -0
- package/dist/modules/systemd/index.js +394 -0
- package/dist/modules/systemd/index.js.map +1 -0
- package/dist/modules/systemd/render.d.ts +41 -0
- package/dist/modules/systemd/render.d.ts.map +1 -0
- package/dist/modules/systemd/render.js +252 -0
- package/dist/modules/systemd/render.js.map +1 -0
- package/dist/modules/systemd/schema.d.ts +293 -0
- package/dist/modules/systemd/schema.d.ts.map +1 -0
- package/dist/modules/systemd/schema.js +302 -0
- package/dist/modules/systemd/schema.js.map +1 -0
- package/dist/modules/systemd/sweeper.d.ts +31 -0
- package/dist/modules/systemd/sweeper.d.ts.map +1 -0
- package/dist/modules/systemd/sweeper.js +77 -0
- package/dist/modules/systemd/sweeper.js.map +1 -0
- package/dist/plans.d.ts +132 -0
- package/dist/plans.d.ts.map +1 -0
- package/dist/plans.js +315 -0
- package/dist/plans.js.map +1 -0
- package/dist/records.d.ts +37 -0
- package/dist/records.d.ts.map +1 -0
- package/dist/records.js +100 -0
- package/dist/records.js.map +1 -0
- package/dist/registry.d.ts +38 -0
- package/dist/registry.d.ts.map +1 -0
- package/dist/registry.js +31 -0
- package/dist/registry.js.map +1 -0
- package/dist/runner.d.ts +48 -0
- package/dist/runner.d.ts.map +1 -0
- package/dist/runner.js +150 -0
- package/dist/runner.js.map +1 -0
- package/dist/scheduler.d.ts +36 -0
- package/dist/scheduler.d.ts.map +1 -0
- package/dist/scheduler.js +77 -0
- package/dist/scheduler.js.map +1 -0
- package/dist/schema.d.ts +762 -0
- package/dist/schema.d.ts.map +1 -0
- package/dist/schema.js +185 -0
- package/dist/schema.js.map +1 -0
- package/dist/secret.d.ts +24 -0
- package/dist/secret.d.ts.map +1 -0
- package/dist/secret.js +3 -0
- package/dist/secret.js.map +1 -0
- package/dist/ui.d.ts +62 -0
- package/dist/ui.d.ts.map +1 -0
- package/dist/ui.js +288 -0
- package/dist/ui.js.map +1 -0
- package/package.json +57 -0
package/dist/exec.js
ADDED
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
import { spawnSync } from 'node:child_process';
|
|
2
|
+
import { randomBytes } from 'node:crypto';
|
|
3
|
+
import { mkdtempSync, unlinkSync, writeFileSync } from 'node:fs';
|
|
4
|
+
import { constants, tmpdir } from 'node:os';
|
|
5
|
+
import { join } from 'node:path';
|
|
6
|
+
/** Ceiling on every host command unless the caller raises it. A stalled apt
|
|
7
|
+
* mirror or a hung ssh must fail the cycle, never hang the daemon. */
|
|
8
|
+
export const DEFAULT_TIMEOUT_MS = 600_000;
|
|
9
|
+
export const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
10
|
+
/**
|
|
11
|
+
* A script body, as `bash -s` receives it on stdin.
|
|
12
|
+
*
|
|
13
|
+
* `bash -s` reads its script from the same stdin that every command in it
|
|
14
|
+
* inherits, and it reads line by line as it goes. So a command that reads stdin
|
|
15
|
+
* (`ssh`, `docker exec -i`, `read`, `cat`) silently eats the rest of the script
|
|
16
|
+
* — and the script still exits 0, because the last command that DID run
|
|
17
|
+
* succeeded. A brace group is parsed in full before any of it runs, and
|
|
18
|
+
* `</dev/null` gives the commands inside it an empty stdin of their own. The
|
|
19
|
+
* closing brace sits on its own line so a body ending in a heredoc terminator
|
|
20
|
+
* or a comment still closes.
|
|
21
|
+
*/
|
|
22
|
+
export const scriptInput = (body) => `{\n${body}\n} </dev/null\n`;
|
|
23
|
+
/**
|
|
24
|
+
* Exit status + human note for a child that may not have exited on its own.
|
|
25
|
+
* Shell conventions, so the numbers read like `timeout(1)` and bash:
|
|
26
|
+
* 124 = timed out, 128+n = killed by signal n, 127 = could not be run.
|
|
27
|
+
*
|
|
28
|
+
* A signal-killed child has `status: null`. Mapping that to 1 with empty output
|
|
29
|
+
* is how an operator's `systemctl kill` read as a flaky upload.
|
|
30
|
+
*/
|
|
31
|
+
export function settle(r, timeoutMs) {
|
|
32
|
+
const code = r.error?.code;
|
|
33
|
+
if (code === 'ETIMEDOUT')
|
|
34
|
+
return { status: 124, note: `[timed out after ${timeoutMs / 1000}s]` };
|
|
35
|
+
// Before `signal`: when node itself stopped the child (ENOBUFS), the signal
|
|
36
|
+
// it sent is not what happened — the error is.
|
|
37
|
+
if (r.error)
|
|
38
|
+
return { status: 127, note: `[${r.error.message}]` };
|
|
39
|
+
if (r.signal) {
|
|
40
|
+
const n = constants.signals[r.signal] ?? 0;
|
|
41
|
+
return { status: 128 + n, note: `[killed by ${r.signal}]` };
|
|
42
|
+
}
|
|
43
|
+
return { status: r.status ?? 1, note: '' };
|
|
44
|
+
}
|
|
45
|
+
/** `exit N`, or the note unbracketed — `timed out after 600s`, `killed by SIGHUP`. */
|
|
46
|
+
export function describeExit(status, note) {
|
|
47
|
+
return note ? note.slice(1, -1) : `exit ${status}`;
|
|
48
|
+
}
|
|
49
|
+
/** The note on its own line, after whatever the child printed. */
|
|
50
|
+
function withNote(output, note) {
|
|
51
|
+
if (!note)
|
|
52
|
+
return output;
|
|
53
|
+
return output && !output.endsWith('\n') ? `${output}\n${note}` : `${output}${note}`;
|
|
54
|
+
}
|
|
55
|
+
function spawnSettled(cmd, args, opts = {}) {
|
|
56
|
+
const timeoutMs = opts.timeoutMs ?? DEFAULT_TIMEOUT_MS;
|
|
57
|
+
const r = spawnSync(cmd, args, {
|
|
58
|
+
encoding: 'utf8',
|
|
59
|
+
stdio: [opts.stdin ?? 'pipe', 'pipe', 'pipe'],
|
|
60
|
+
timeout: timeoutMs,
|
|
61
|
+
...(opts.input !== undefined ? { input: opts.input } : {}),
|
|
62
|
+
});
|
|
63
|
+
return { stdout: r.stdout ?? '', stderr: r.stderr ?? '', ...settle(r, timeoutMs) };
|
|
64
|
+
}
|
|
65
|
+
function sudoFailed(host, args, r) {
|
|
66
|
+
const detail = (r.stderr || r.stdout).trim().split('\n').slice(-6).join('\n');
|
|
67
|
+
return new Error(`[${host}] sudo ${args.join(' ')} failed (${describeExit(r.status, r.note)}):\n${detail}`);
|
|
68
|
+
}
|
|
69
|
+
/** `%F` renders with spaces ("regular file"), so it must stay last. */
|
|
70
|
+
const STAT_FMT = '%u %g %a %Y %F';
|
|
71
|
+
function parseStat(out) {
|
|
72
|
+
const [uid, gid, mode, mtime, ...rest] = out.trim().split(' ');
|
|
73
|
+
return {
|
|
74
|
+
uid: uid,
|
|
75
|
+
gid: gid,
|
|
76
|
+
mode: mode.padStart(4, '0'),
|
|
77
|
+
mtime: Number(mtime),
|
|
78
|
+
type: rest.join(' '),
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
// ----------------------------------------------------------------- local ----
|
|
82
|
+
export class LocalExec {
|
|
83
|
+
host;
|
|
84
|
+
constructor(host) {
|
|
85
|
+
this.host = host;
|
|
86
|
+
}
|
|
87
|
+
/** Always. If local exec is broken there is nothing left to reconcile with. */
|
|
88
|
+
reachable() {
|
|
89
|
+
return true;
|
|
90
|
+
}
|
|
91
|
+
run(cmd, args, opts = {}) {
|
|
92
|
+
const r = spawnSettled(cmd, args, { ...opts, stdin: 'ignore' });
|
|
93
|
+
return { stdout: r.stdout, status: r.status };
|
|
94
|
+
}
|
|
95
|
+
sudo(args, input, opts = {}) {
|
|
96
|
+
const r = spawnSettled('sudo', ['-n', ...args], { ...opts, input });
|
|
97
|
+
if (r.status !== 0)
|
|
98
|
+
throw sudoFailed(this.host, args, r);
|
|
99
|
+
return r.stdout;
|
|
100
|
+
}
|
|
101
|
+
sudoCombined(args, opts = {}) {
|
|
102
|
+
const r = spawnSettled('sudo', ['-n', ...args], opts);
|
|
103
|
+
return { output: withNote(`${r.stdout}${r.stderr}`, r.note), status: r.status };
|
|
104
|
+
}
|
|
105
|
+
runScript(script, opts = {}) {
|
|
106
|
+
const cmd = opts.sudo ? 'sudo' : 'bash';
|
|
107
|
+
const args = opts.sudo ? ['-n', 'bash', '-s'] : ['-s'];
|
|
108
|
+
const r = spawnSettled(cmd, args, { input: scriptInput(script), timeoutMs: opts.timeoutMs });
|
|
109
|
+
return { output: withNote(`${r.stdout}${r.stderr}`, r.note), status: r.status };
|
|
110
|
+
}
|
|
111
|
+
readFileMaybe(path) {
|
|
112
|
+
const r = this.run('cat', [path]);
|
|
113
|
+
return r.status === 0 ? r.stdout : '';
|
|
114
|
+
}
|
|
115
|
+
sudoReadFileMaybe(path) {
|
|
116
|
+
const r = this.run('sudo', ['-n', 'cat', path]);
|
|
117
|
+
return r.status === 0 ? r.stdout : '';
|
|
118
|
+
}
|
|
119
|
+
statMaybe(path) {
|
|
120
|
+
const r = this.run('sudo', ['-n', 'stat', '-c', STAT_FMT, path]);
|
|
121
|
+
return r.status === 0 ? parseStat(r.stdout) : null;
|
|
122
|
+
}
|
|
123
|
+
writeFileAsRoot(path, content, mode) {
|
|
124
|
+
const dir = path.slice(0, path.lastIndexOf('/'));
|
|
125
|
+
const tmp = join(tmpdir(), `concord-${randomBytes(8).toString('hex')}`);
|
|
126
|
+
try {
|
|
127
|
+
writeFileSync(tmp, content, { mode: 0o600 });
|
|
128
|
+
this.sudo(['mkdir', '-p', dir]);
|
|
129
|
+
this.sudo(['install', '-m', mode, '-o', 'root', '-g', 'root', tmp, path]);
|
|
130
|
+
}
|
|
131
|
+
finally {
|
|
132
|
+
try {
|
|
133
|
+
unlinkSync(tmp);
|
|
134
|
+
}
|
|
135
|
+
catch {
|
|
136
|
+
/* already gone */
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Remote execution over SSH.
|
|
143
|
+
*
|
|
144
|
+
* Connections are MULTIPLEXED (ControlMaster/ControlPersist): one TCP session
|
|
145
|
+
* per host, reused for every exec. Without it a module issuing dozens of execs
|
|
146
|
+
* pays a full handshake each time — roughly 100 ms versus 5 ms. This is the
|
|
147
|
+
* reason a worker process is unnecessary, since latency was its only real
|
|
148
|
+
* advantage.
|
|
149
|
+
*
|
|
150
|
+
* Commands are passed as ARGV to ssh, and script bodies over stdin, so nothing
|
|
151
|
+
* is spliced into a remote shell string. Shell quoting has been the single most
|
|
152
|
+
* common source of bugs in this project.
|
|
153
|
+
*/
|
|
154
|
+
export class SshExec {
|
|
155
|
+
target;
|
|
156
|
+
host;
|
|
157
|
+
#keyFile;
|
|
158
|
+
#ctlDir;
|
|
159
|
+
constructor(target) {
|
|
160
|
+
this.target = target;
|
|
161
|
+
this.host = target.host;
|
|
162
|
+
this.#ctlDir = mkdtempSync(join(tmpdir(), 'concord-ssh-'));
|
|
163
|
+
if (target.key) {
|
|
164
|
+
this.#keyFile = join(this.#ctlDir, 'id');
|
|
165
|
+
writeFileSync(this.#keyFile, target.key.trim() + '\n', { mode: 0o600 });
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
/** Release the multiplexed connection and remove the key file. */
|
|
169
|
+
close() {
|
|
170
|
+
try {
|
|
171
|
+
spawnSync('ssh', [...this.#opts(), '-O', 'exit', this.#dest()], { encoding: 'utf8', timeout: 10_000 });
|
|
172
|
+
}
|
|
173
|
+
catch {
|
|
174
|
+
/* nothing to close */
|
|
175
|
+
}
|
|
176
|
+
try {
|
|
177
|
+
if (this.#keyFile)
|
|
178
|
+
unlinkSync(this.#keyFile);
|
|
179
|
+
}
|
|
180
|
+
catch {
|
|
181
|
+
/* already gone */
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
reachable() {
|
|
185
|
+
return this.#read(['true'], 30_000, { throwOnTransport: false }).status === 0;
|
|
186
|
+
}
|
|
187
|
+
#dest = () => `${this.target.user}@${this.target.ip}`;
|
|
188
|
+
#opts() {
|
|
189
|
+
return [
|
|
190
|
+
'-o', 'BatchMode=yes',
|
|
191
|
+
'-o', 'StrictHostKeyChecking=accept-new',
|
|
192
|
+
'-o', 'ControlMaster=auto',
|
|
193
|
+
'-o', `ControlPath=${join(this.#ctlDir, 'cm-%r@%h:%p')}`,
|
|
194
|
+
'-o', 'ControlPersist=60s',
|
|
195
|
+
'-o', 'ConnectTimeout=10',
|
|
196
|
+
...(this.target.port ? ['-p', String(this.target.port)] : []),
|
|
197
|
+
...(this.#keyFile ? ['-i', this.#keyFile, '-o', 'IdentitiesOnly=yes'] : []),
|
|
198
|
+
];
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* argv straight through — ssh joins with spaces, so each arg is quoted.
|
|
202
|
+
*
|
|
203
|
+
* A timeout kills the ssh CLIENT; the remote command may outlive it, the same
|
|
204
|
+
* caveat `runScript` has always carried.
|
|
205
|
+
*/
|
|
206
|
+
#ssh(argv, input, timeoutMs = DEFAULT_TIMEOUT_MS) {
|
|
207
|
+
const r = spawnSettled('ssh', [...this.#opts(), this.#dest(), '--', ...argv.map(shellQuote)], {
|
|
208
|
+
input,
|
|
209
|
+
timeoutMs,
|
|
210
|
+
});
|
|
211
|
+
if (r.status === SSH_TRANSPORT_FAILURE)
|
|
212
|
+
throw transportFailed(this.host, argv, r);
|
|
213
|
+
return r;
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* A READ: retried when the transport fails, because answering "" or "absent"
|
|
217
|
+
* for a read that never happened is how a file gets rewritten for nothing.
|
|
218
|
+
*
|
|
219
|
+
* Exit 255 is ssh's own failure, not the command's. Tailscale SSH produces it
|
|
220
|
+
* spuriously: under concurrent sessions it sometimes closes a channel without
|
|
221
|
+
* sending the exit status, so a `cat` that printed the whole file still exits
|
|
222
|
+
* 255 (measured 2026-09-23: 6 of 300 parallel reads). Before this, every
|
|
223
|
+
* "maybe" read below mapped that to "missing", and a Concord cycle rewrote
|
|
224
|
+
* /etc/docker/daemon.json on a host and restarted Docker under a live
|
|
225
|
+
* session. Only reads retry: re-running a write that may already have
|
|
226
|
+
* happened is not this layer's call, so those throw instead (see #ssh).
|
|
227
|
+
*/
|
|
228
|
+
#read(argv, timeoutMs = DEFAULT_TIMEOUT_MS, { throwOnTransport = true } = {}) {
|
|
229
|
+
let r;
|
|
230
|
+
for (let attempt = 1; attempt <= READ_ATTEMPTS; attempt++) {
|
|
231
|
+
r = spawnSettled('ssh', [...this.#opts(), this.#dest(), '--', ...argv.map(shellQuote)], { timeoutMs });
|
|
232
|
+
if (r.status !== SSH_TRANSPORT_FAILURE)
|
|
233
|
+
return r;
|
|
234
|
+
}
|
|
235
|
+
if (throwOnTransport)
|
|
236
|
+
throw transportFailed(this.host, argv, r);
|
|
237
|
+
return r;
|
|
238
|
+
}
|
|
239
|
+
run(cmd, args, opts = {}) {
|
|
240
|
+
// Every caller of run() is reading state; see #read.
|
|
241
|
+
const r = this.#read([cmd, ...args], opts.timeoutMs);
|
|
242
|
+
return { stdout: r.stdout, status: r.status };
|
|
243
|
+
}
|
|
244
|
+
sudo(args, input, opts = {}) {
|
|
245
|
+
const r = this.#ssh(['sudo', '-n', ...args], input, opts.timeoutMs);
|
|
246
|
+
if (r.status !== 0)
|
|
247
|
+
throw sudoFailed(this.host, args, r);
|
|
248
|
+
return r.stdout;
|
|
249
|
+
}
|
|
250
|
+
sudoCombined(args, opts = {}) {
|
|
251
|
+
const r = this.#ssh(['sudo', '-n', ...args], undefined, opts.timeoutMs);
|
|
252
|
+
return { output: withNote(`${r.stdout}${r.stderr}`, r.note), status: r.status };
|
|
253
|
+
}
|
|
254
|
+
runScript(script, opts = {}) {
|
|
255
|
+
// Body over STDIN, never as an argument — no remote-shell quoting at all.
|
|
256
|
+
const argv = opts.sudo ? ['sudo', '-n', 'bash', '-s'] : ['bash', '-s'];
|
|
257
|
+
const r = this.#ssh(argv, scriptInput(script), opts.timeoutMs);
|
|
258
|
+
return { output: withNote(`${r.stdout}${r.stderr}`, r.note), status: r.status };
|
|
259
|
+
}
|
|
260
|
+
readFileMaybe(path) {
|
|
261
|
+
const r = this.#read(['cat', path]);
|
|
262
|
+
return r.status === 0 ? r.stdout : '';
|
|
263
|
+
}
|
|
264
|
+
sudoReadFileMaybe(path) {
|
|
265
|
+
const r = this.#read(['sudo', '-n', 'cat', path]);
|
|
266
|
+
return r.status === 0 ? r.stdout : '';
|
|
267
|
+
}
|
|
268
|
+
statMaybe(path) {
|
|
269
|
+
const r = this.#read(['sudo', '-n', 'stat', '-c', STAT_FMT, path]);
|
|
270
|
+
return r.status === 0 ? parseStat(r.stdout) : null;
|
|
271
|
+
}
|
|
272
|
+
writeFileAsRoot(path, content, mode) {
|
|
273
|
+
const dir = path.slice(0, path.lastIndexOf('/'));
|
|
274
|
+
// Content over stdin to a root-owned temp file, then `install` — the same
|
|
275
|
+
// shape as the local path, and content never touches a command line.
|
|
276
|
+
const tmp = `/tmp/concord-${randomBytes(8).toString('hex')}`;
|
|
277
|
+
this.sudo(['mkdir', '-p', dir]);
|
|
278
|
+
const w = this.#ssh(['sudo', '-n', 'tee', tmp], content);
|
|
279
|
+
if (w.status !== 0) {
|
|
280
|
+
throw new Error(`[${this.host}] writing ${path} failed: ${withNote((w.stderr || w.stdout).trim(), w.note)}`);
|
|
281
|
+
}
|
|
282
|
+
try {
|
|
283
|
+
this.sudo(['chmod', mode, tmp]);
|
|
284
|
+
this.sudo(['chown', 'root:root', tmp]);
|
|
285
|
+
this.sudo(['mv', tmp, path]);
|
|
286
|
+
}
|
|
287
|
+
finally {
|
|
288
|
+
try {
|
|
289
|
+
this.#ssh(['sudo', '-n', 'rm', '-f', tmp]);
|
|
290
|
+
}
|
|
291
|
+
catch {
|
|
292
|
+
/* a leftover temp file is not worth masking the real outcome */
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
/**
|
|
298
|
+
* ssh's own exit status for a connection or protocol failure. A remote command
|
|
299
|
+
* that itself exits 255 is indistinguishable, and is treated as a transport
|
|
300
|
+
* failure too — failing loudly is the safe reading either way.
|
|
301
|
+
*/
|
|
302
|
+
const SSH_TRANSPORT_FAILURE = 255;
|
|
303
|
+
const READ_ATTEMPTS = 3;
|
|
304
|
+
function transportFailed(host, argv, r) {
|
|
305
|
+
const detail = (r.stderr || '').trim().split('\n').slice(-3).join('\n');
|
|
306
|
+
return new Error(`[${host}] ssh transport failed (exit 255) running ${argv.slice(0, 4).join(' ')}` +
|
|
307
|
+
(detail ? `:\n${detail}` : ' — no error text; the channel closed without an exit status'));
|
|
308
|
+
}
|
|
309
|
+
/** ssh concatenates argv into one remote shell string, so each word is quoted. */
|
|
310
|
+
function shellQuote(s) {
|
|
311
|
+
return /^[A-Za-z0-9_@%+=:,./-]+$/.test(s) ? s : `'${s.replace(/'/g, `'\\''`)}'`;
|
|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,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,GAAG,CAAC,CAAC,CAAA;YAC5C,CAAC;YAAC,MAAM,CAAC;gBACP,gEAAgE;YAClE,CAAC;QACH,CAAC;IACH,CAAC;CACF;AAED;;;;GAIG;AACH,MAAM,qBAAqB,GAAG,GAAG,CAAA;AACjC,MAAM,aAAa,GAAG,CAAC,CAAA;AAEvB,SAAS,eAAe,CAAC,IAAY,EAAE,IAAc,EAAE,CAAU;IAC/D,MAAM,MAAM,GAAG,CAAC,CAAC,CAAC,MAAM,IAAI,EAAE,CAAC,CAAC,IAAI,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;IACvE,OAAO,IAAI,KAAK,CACd,IAAI,IAAI,6CAA6C,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE;QAC/E,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,MAAM,EAAE,CAAC,CAAC,CAAC,6DAA6D,CAAC,CAC5F,CAAA;AACH,CAAC;AAED,kFAAkF;AAClF,SAAS,UAAU,CAAC,CAAS;IAC3B,OAAO,0BAA0B,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,OAAO,CAAC,IAAI,EAAE,OAAO,CAAC,GAAG,CAAA;AACjF,CAAC"}
|
package/dist/git.d.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/** `owner/name` shorthand → the https URL git will actually be given. */
|
|
2
|
+
export declare function cloneUrl(repo: string): string;
|
|
3
|
+
export declare function githubSlug(repo: string): {
|
|
4
|
+
owner: string;
|
|
5
|
+
name: string;
|
|
6
|
+
} | null;
|
|
7
|
+
export interface RefRequest {
|
|
8
|
+
/** App name, for error messages. */
|
|
9
|
+
app: string;
|
|
10
|
+
repo: string;
|
|
11
|
+
branch: string;
|
|
12
|
+
/**
|
|
13
|
+
* Resolve the last commit that touched this subtree, rather than the branch
|
|
14
|
+
* tip.
|
|
15
|
+
*
|
|
16
|
+
* When several apps share one repo (a monorepo of small sites, say),
|
|
17
|
+
* tracking the tip would rebuild and restart every one of them whenever any
|
|
18
|
+
* one changed. Scoping to the build context makes the context its own watch
|
|
19
|
+
* path, which platforms usually need a separate `watch_paths` field for.
|
|
20
|
+
*
|
|
21
|
+
* '.' or undefined means the whole repo, where the tip IS the answer.
|
|
22
|
+
*/
|
|
23
|
+
path?: string;
|
|
24
|
+
}
|
|
25
|
+
/** Per app, because one deleted branch must not decide the fate of the rest. */
|
|
26
|
+
export type RefResult = {
|
|
27
|
+
commit: string;
|
|
28
|
+
} | {
|
|
29
|
+
error: string;
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* Resolve many branch tips in as few network round trips as possible.
|
|
33
|
+
*
|
|
34
|
+
* GitHub repos are batched into ONE GraphQL request using field aliases —
|
|
35
|
+
* `git ls-remote` can query many refs but only one repo, so batching has to
|
|
36
|
+
* move up to the API layer. Anything not on github.com, or any batch that
|
|
37
|
+
* cannot run because no token was supplied, falls back to per-repo
|
|
38
|
+
* `ls-remote`.
|
|
39
|
+
*
|
|
40
|
+
* Never throws. A failure is attributed to the narrowest set of apps it can
|
|
41
|
+
* honestly be blamed on: one app for a missing branch, every batched app for a
|
|
42
|
+
* transport or auth failure. The CALLER decides whether that is fatal, because
|
|
43
|
+
* only it knows which apps declared `failureIsWarning`.
|
|
44
|
+
*/
|
|
45
|
+
export declare function resolveRefs(requests: RefRequest[], token: string | undefined,
|
|
46
|
+
/** Supplies a local bare mirror, only needed for path-scoped non-GitHub repos. */
|
|
47
|
+
mirrorFor?: (repo: string) => string): Promise<Map<string, RefResult>>;
|
|
48
|
+
/**
|
|
49
|
+
* A bare mirror per repo, kept on the primary between runs.
|
|
50
|
+
*
|
|
51
|
+
* Bare because nothing is ever checked out here — `git archive` reads a tree
|
|
52
|
+
* straight out of the object database. Fetching into a warm mirror is what
|
|
53
|
+
* keeps the steady-state cost of a tracked app close to nothing.
|
|
54
|
+
*/
|
|
55
|
+
export declare function syncMirror(cacheRoot: string, repo: string, token: string | undefined): string;
|
|
56
|
+
/** True when the mirror already has this object — lets us skip a needless fetch. */
|
|
57
|
+
export declare function mirrorHas(mirror: string, commit: string): boolean;
|
|
58
|
+
export declare function archiveTree(mirror: string, commit: string, context: string): Buffer;
|
|
59
|
+
//# sourceMappingURL=git.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"git.d.ts","sourceRoot":"","sources":["../src/git.ts"],"names":[],"mappings":"AAuBA,yEAAyE;AACzE,wBAAgB,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAG7C;AAED,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,GAAG,IAAI,CAG/E;AA4CD,MAAM,WAAW,UAAU;IACzB,oCAAoC;IACpC,GAAG,EAAE,MAAM,CAAA;IACX,IAAI,EAAE,MAAM,CAAA;IACZ,MAAM,EAAE,MAAM,CAAA;IACd;;;;;;;;;;OAUG;IACH,IAAI,CAAC,EAAE,MAAM,CAAA;CACd;AAID,gFAAgF;AAChF,MAAM,MAAM,SAAS,GAAG;IAAE,MAAM,EAAE,MAAM,CAAA;CAAE,GAAG;IAAE,KAAK,EAAE,MAAM,CAAA;CAAE,CAAA;AAE9D;;;;;;;;;;;;;GAaG;AACH,wBAAsB,WAAW,CAC/B,QAAQ,EAAE,UAAU,EAAE,EACtB,KAAK,EAAE,MAAM,GAAG,SAAS;AACzB,kFAAkF;AAClF,SAAS,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,MAAM,GACnC,OAAO,CAAC,GAAG,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC,CAmBjC;AA+HD;;;;;;GAMG;AACH,wBAAgB,UAAU,CACxB,SAAS,EAAE,MAAM,EACjB,IAAI,EAAE,MAAM,EACZ,KAAK,EAAE,MAAM,GAAG,SAAS,GACxB,MAAM,CASR;AAED,oFAAoF;AACpF,wBAAgB,SAAS,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAOjE;AAWD,wBAAgB,WAAW,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,MAAM,CAiBnF"}
|
package/dist/git.js
ADDED
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
import { execFileSync, spawnSync } from 'node:child_process';
|
|
2
|
+
import { existsSync, mkdirSync } from 'node:fs';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
import { describeExit, settle } from './exec.js';
|
|
5
|
+
/**
|
|
6
|
+
* Git work that happens ON THE PRIMARY.
|
|
7
|
+
*
|
|
8
|
+
* Nothing here goes through `Exec`. Source is fetched centrally and the tree is
|
|
9
|
+
* streamed to the target, so a private repo's credentials never reach the
|
|
10
|
+
* fleet — the same reasoning that keeps the age key on the primary alone
|
|
11
|
+
* It also means a target host needs no git at all.
|
|
12
|
+
*
|
|
13
|
+
* Shared by two modules, which is why it lives here rather than under
|
|
14
|
+
* `modules/docker/`. They use different halves: `docker` uses all of it, while
|
|
15
|
+
* `checkout` uses only the RESOLUTION half — it drives git on the target
|
|
16
|
+
* (the deliverable is a working tree), but still resolves every branch tip
|
|
17
|
+
* here, once, in one batched request: desired state is resolved centrally,
|
|
18
|
+
* at plan time, so the resolved value appears in the plan.
|
|
19
|
+
*/
|
|
20
|
+
const GITHUB_SLUG = /^([\w.-]+)\/([\w.-]+)$/;
|
|
21
|
+
/** `owner/name` shorthand → the https URL git will actually be given. */
|
|
22
|
+
export function cloneUrl(repo) {
|
|
23
|
+
const m = GITHUB_SLUG.exec(repo);
|
|
24
|
+
return m ? `https://github.com/${m[1]}/${m[2]}.git` : repo;
|
|
25
|
+
}
|
|
26
|
+
export function githubSlug(repo) {
|
|
27
|
+
const m = GITHUB_SLUG.exec(repo);
|
|
28
|
+
return m ? { owner: m[1], name: m[2].replace(/\.git$/, '') } : null;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* A token is passed through a credential helper reading the ENVIRONMENT, never
|
|
32
|
+
* spliced into the URL or argv — argv is world-readable in /proc.
|
|
33
|
+
*/
|
|
34
|
+
function gitEnv(token) {
|
|
35
|
+
return {
|
|
36
|
+
...process.env,
|
|
37
|
+
GIT_TERMINAL_PROMPT: '0',
|
|
38
|
+
// Ignore the invoking user's git config and the system one. Without this,
|
|
39
|
+
// a private repo with NO declared token still cloned — git silently fell
|
|
40
|
+
// back to whatever credential helper the `ubuntu` user happened to have
|
|
41
|
+
// (`gh auth git-credential`). That made a working deploy depend on an
|
|
42
|
+
// undeclared, hand-placed login, so a rebuilt primary would have broken
|
|
43
|
+
// with a baffling "could not read Username". An absent credential must
|
|
44
|
+
// fail, not be quietly supplied from somewhere else.
|
|
45
|
+
GIT_CONFIG_GLOBAL: '/dev/null',
|
|
46
|
+
GIT_CONFIG_SYSTEM: '/dev/null',
|
|
47
|
+
...(token ? { CONCORD_GIT_TOKEN: token } : {}),
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
const CREDENTIAL_HELPER = '!f() { echo username=x-access-token; echo "password=$CONCORD_GIT_TOKEN"; }; f';
|
|
51
|
+
function gitArgs(token) {
|
|
52
|
+
return token ? ['-c', `credential.helper=${CREDENTIAL_HELPER}`] : [];
|
|
53
|
+
}
|
|
54
|
+
/** A stalled GitHub connection would hang the cycle just like a stalled ssh, so it gets the same ceiling. */
|
|
55
|
+
function git(args, opts = {}) {
|
|
56
|
+
return execFileSync('git', [...gitArgs(opts.token), ...args], {
|
|
57
|
+
encoding: 'utf8',
|
|
58
|
+
env: gitEnv(opts.token),
|
|
59
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
60
|
+
...(opts.cwd ? { cwd: opts.cwd } : {}),
|
|
61
|
+
maxBuffer: 256 * 1024 * 1024,
|
|
62
|
+
timeout: 600_000,
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
const wholeRepo = (p) => !p || p === '.';
|
|
66
|
+
/**
|
|
67
|
+
* Resolve many branch tips in as few network round trips as possible.
|
|
68
|
+
*
|
|
69
|
+
* GitHub repos are batched into ONE GraphQL request using field aliases —
|
|
70
|
+
* `git ls-remote` can query many refs but only one repo, so batching has to
|
|
71
|
+
* move up to the API layer. Anything not on github.com, or any batch that
|
|
72
|
+
* cannot run because no token was supplied, falls back to per-repo
|
|
73
|
+
* `ls-remote`.
|
|
74
|
+
*
|
|
75
|
+
* Never throws. A failure is attributed to the narrowest set of apps it can
|
|
76
|
+
* honestly be blamed on: one app for a missing branch, every batched app for a
|
|
77
|
+
* transport or auth failure. The CALLER decides whether that is fatal, because
|
|
78
|
+
* only it knows which apps declared `failureIsWarning`.
|
|
79
|
+
*/
|
|
80
|
+
export async function resolveRefs(requests, token,
|
|
81
|
+
/** Supplies a local bare mirror, only needed for path-scoped non-GitHub repos. */
|
|
82
|
+
mirrorFor) {
|
|
83
|
+
const out = new Map();
|
|
84
|
+
if (!requests.length)
|
|
85
|
+
return out;
|
|
86
|
+
const batchable = token ? requests.filter((r) => githubSlug(r.repo)) : [];
|
|
87
|
+
const rest = requests.filter((r) => !batchable.includes(r));
|
|
88
|
+
if (batchable.length) {
|
|
89
|
+
for (const [k, v] of await resolveViaGraphQL(batchable, token))
|
|
90
|
+
out.set(k, v);
|
|
91
|
+
}
|
|
92
|
+
for (const r of rest) {
|
|
93
|
+
try {
|
|
94
|
+
const mirror = !wholeRepo(r.path) && mirrorFor ? mirrorFor(r.repo) : undefined;
|
|
95
|
+
out.set(r.app, { commit: lsRemote(r, token, mirror) });
|
|
96
|
+
}
|
|
97
|
+
catch (err) {
|
|
98
|
+
out.set(r.app, { error: err instanceof Error ? err.message : String(err) });
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return out;
|
|
102
|
+
}
|
|
103
|
+
/** Aliases must match /^[_A-Za-z][_0-9A-Za-z]*$/, so they are index-based. */
|
|
104
|
+
async function resolveViaGraphQL(requests, token) {
|
|
105
|
+
const fields = requests.map((r, i) => {
|
|
106
|
+
const { owner, name } = githubSlug(r.repo);
|
|
107
|
+
// The path-scoped form costs nothing extra: it rides in the same request.
|
|
108
|
+
const target = wholeRepo(r.path)
|
|
109
|
+
? `target { oid }`
|
|
110
|
+
: `target { ... on Commit { history(first: 1, path: ${JSON.stringify(r.path)}) { nodes { oid } } } }`;
|
|
111
|
+
return (` a${i}: repository(owner: ${JSON.stringify(owner)}, name: ${JSON.stringify(name)}) {\n` +
|
|
112
|
+
` ref(qualifiedName: ${JSON.stringify(`refs/heads/${r.branch}`)}) { ${target} }\n` +
|
|
113
|
+
` }`);
|
|
114
|
+
});
|
|
115
|
+
const query = `query {\n${fields.join('\n')}\n}`;
|
|
116
|
+
const out = new Map();
|
|
117
|
+
/** A whole-batch failure is honestly attributable to every app in it. */
|
|
118
|
+
const blameAll = (why) => {
|
|
119
|
+
for (const r of requests)
|
|
120
|
+
out.set(r.app, { error: why });
|
|
121
|
+
return out;
|
|
122
|
+
};
|
|
123
|
+
let body;
|
|
124
|
+
try {
|
|
125
|
+
const res = await fetch('https://api.github.com/graphql', {
|
|
126
|
+
method: 'POST',
|
|
127
|
+
headers: {
|
|
128
|
+
authorization: `bearer ${token}`,
|
|
129
|
+
'content-type': 'application/json',
|
|
130
|
+
'user-agent': 'concord',
|
|
131
|
+
},
|
|
132
|
+
body: JSON.stringify({ query }),
|
|
133
|
+
});
|
|
134
|
+
if (!res.ok) {
|
|
135
|
+
return blameAll(`GitHub GraphQL returned ${res.status} ${res.statusText} resolving ` +
|
|
136
|
+
`${requests.length} branch tip(s)`);
|
|
137
|
+
}
|
|
138
|
+
body = (await res.json());
|
|
139
|
+
}
|
|
140
|
+
catch (err) {
|
|
141
|
+
return blameAll(`GitHub GraphQL request failed: ${err instanceof Error ? err.message : err}`);
|
|
142
|
+
}
|
|
143
|
+
// GraphQL answers PARTIALLY: one unresolvable repo returns 200 with its own
|
|
144
|
+
// alias null, an error carrying `path: ["a2"]`, and every other alias
|
|
145
|
+
// correctly resolved. Errors are therefore attributed by path — blaming the
|
|
146
|
+
// whole batch would let one deleted repo stop the entire fleet deploying,
|
|
147
|
+
// and ignoring `errors` entirely would read a null as "no change".
|
|
148
|
+
const byAlias = new Map();
|
|
149
|
+
const global = [];
|
|
150
|
+
for (const e of body.errors ?? []) {
|
|
151
|
+
const alias = e.path?.[0];
|
|
152
|
+
if (alias && /^a\d+$/.test(alias))
|
|
153
|
+
byAlias.set(alias, e.message);
|
|
154
|
+
else
|
|
155
|
+
global.push(e.message);
|
|
156
|
+
}
|
|
157
|
+
if (global.length)
|
|
158
|
+
return blameAll(`GitHub GraphQL errors: ${global.join('; ')}`);
|
|
159
|
+
if (!body.data)
|
|
160
|
+
return blameAll('GitHub GraphQL returned no data');
|
|
161
|
+
requests.forEach((r, i) => {
|
|
162
|
+
const alias = `a${i}`;
|
|
163
|
+
const named = byAlias.get(alias);
|
|
164
|
+
if (named) {
|
|
165
|
+
out.set(r.app, { error: named });
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
const target = body.data[alias]?.ref?.target;
|
|
169
|
+
const oid = wholeRepo(r.path) ? target?.oid : target?.history?.nodes?.[0]?.oid;
|
|
170
|
+
if (oid) {
|
|
171
|
+
out.set(r.app, { commit: oid });
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
// Never "no change": we do not know what should be running here, so nothing
|
|
175
|
+
// about this app may claim convergence. The two causes read differently.
|
|
176
|
+
out.set(r.app, {
|
|
177
|
+
error: target
|
|
178
|
+
? `${r.repo}@${r.branch} has no commit touching "${r.path}" — is the path right?`
|
|
179
|
+
: `${r.repo} has no branch "${r.branch}" (or the token cannot see the repo)`,
|
|
180
|
+
});
|
|
181
|
+
});
|
|
182
|
+
return out;
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* `ls-remote` cannot filter by path, so a path-scoped request needs the objects
|
|
186
|
+
* locally. The caller passes a mirror when it has one; without it, the branch
|
|
187
|
+
* tip is the honest answer and the caller narrows it afterwards.
|
|
188
|
+
*/
|
|
189
|
+
function lsRemote(r, token, mirror) {
|
|
190
|
+
if (!wholeRepo(r.path) && mirror) {
|
|
191
|
+
// A bare mirror fetched with +refs/heads/*:refs/heads/* has the branch at
|
|
192
|
+
// refs/heads/<branch>. There is no `origin/` remote-tracking namespace here.
|
|
193
|
+
let out;
|
|
194
|
+
try {
|
|
195
|
+
out = git(['log', '-1', '--format=%H', r.branch, '--', r.path], { cwd: mirror }).trim();
|
|
196
|
+
}
|
|
197
|
+
catch (err) {
|
|
198
|
+
throw new Error(`${r.repo} has no branch "${r.branch}": ${err.message.split('\n')[0]}`);
|
|
199
|
+
}
|
|
200
|
+
if (!/^[0-9a-f]{40}$/.test(out)) {
|
|
201
|
+
throw new Error(`${r.repo}@${r.branch} has no commit touching "${r.path}" — is the path right?`);
|
|
202
|
+
}
|
|
203
|
+
return out;
|
|
204
|
+
}
|
|
205
|
+
const out = git(['ls-remote', cloneUrl(r.repo), `refs/heads/${r.branch}`], { token });
|
|
206
|
+
const sha = out.trim().split(/\s+/)[0];
|
|
207
|
+
if (!sha || !/^[0-9a-f]{40}$/.test(sha)) {
|
|
208
|
+
throw new Error(`${r.repo} has no branch "${r.branch}"`);
|
|
209
|
+
}
|
|
210
|
+
return sha;
|
|
211
|
+
}
|
|
212
|
+
// -------------------------------------------------------- source fetching ----
|
|
213
|
+
/**
|
|
214
|
+
* A bare mirror per repo, kept on the primary between runs.
|
|
215
|
+
*
|
|
216
|
+
* Bare because nothing is ever checked out here — `git archive` reads a tree
|
|
217
|
+
* straight out of the object database. Fetching into a warm mirror is what
|
|
218
|
+
* keeps the steady-state cost of a tracked app close to nothing.
|
|
219
|
+
*/
|
|
220
|
+
export function syncMirror(cacheRoot, repo, token) {
|
|
221
|
+
const dir = join(cacheRoot, repo.replace(/[^\w.-]+/g, '_') + '.git');
|
|
222
|
+
if (!existsSync(dir)) {
|
|
223
|
+
mkdirSync(cacheRoot, { recursive: true });
|
|
224
|
+
git(['clone', '--bare', '--filter=blob:none', cloneUrl(repo), dir], { token });
|
|
225
|
+
}
|
|
226
|
+
else {
|
|
227
|
+
git(['fetch', '--prune', 'origin', '+refs/heads/*:refs/heads/*'], { cwd: dir, token });
|
|
228
|
+
}
|
|
229
|
+
return dir;
|
|
230
|
+
}
|
|
231
|
+
/** True when the mirror already has this object — lets us skip a needless fetch. */
|
|
232
|
+
export function mirrorHas(mirror, commit) {
|
|
233
|
+
const r = spawnSync('git', ['cat-file', '-e', `${commit}^{commit}`], {
|
|
234
|
+
cwd: mirror,
|
|
235
|
+
encoding: 'utf8',
|
|
236
|
+
timeout: 30_000,
|
|
237
|
+
});
|
|
238
|
+
return r.status === 0;
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* A tar of one subtree at one commit.
|
|
242
|
+
*
|
|
243
|
+
* `<sha>:<subdir>` archives that subtree with paths relative to it, which is
|
|
244
|
+
* exactly what a build context wants — and it means `context` costs nothing
|
|
245
|
+
* extra, unlike checking out the whole repo and building from a subdirectory.
|
|
246
|
+
*/
|
|
247
|
+
const ARCHIVE_TIMEOUT_MS = 300_000;
|
|
248
|
+
export function archiveTree(mirror, commit, context) {
|
|
249
|
+
const spec = context === '.' ? commit : `${commit}:${context.replace(/\/+$/, '')}`;
|
|
250
|
+
try {
|
|
251
|
+
return execFileSync('git', ['archive', '--format=tar', spec], {
|
|
252
|
+
cwd: mirror,
|
|
253
|
+
maxBuffer: 512 * 1024 * 1024,
|
|
254
|
+
timeout: ARCHIVE_TIMEOUT_MS,
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
catch (err) {
|
|
258
|
+
// execFileSync's error carries the spawnSync result, so it settles the same way.
|
|
259
|
+
const e = err;
|
|
260
|
+
const { status, note } = settle(e, ARCHIVE_TIMEOUT_MS);
|
|
261
|
+
throw new Error(`git archive ${spec} failed (${describeExit(status, note)}): ${String(e.stderr ?? '').trim()}\n` +
|
|
262
|
+
`Does the path "${context}" exist at that commit?`);
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
//# sourceMappingURL=git.js.map
|