| ... | ... | @@ -1,13 +1,15 @@ |
| 1 | 1 | import { execFile, spawn } from "node:child_process"; |
| 2 | 2 | import { createHash } from "node:crypto"; |
| 3 | | import { copyFileSync, createWriteStream, existsSync, mkdirSync, readdirSync, readFileSync, renameSync, rmSync, statSync, symlinkSync, writeFileSync } from "node:fs"; |
| 3 | import { resolve4 } from "node:dns/promises"; |
| 4 | import { once } from "node:events"; |
| 5 | import { copyFileSync, createWriteStream, existsSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, renameSync, rmSync, statSync, symlinkSync, writeFileSync } from "node:fs"; |
| 4 | 6 | import { connect, createServer } from "node:net"; |
| 5 | 7 | import { basename } from "node:path"; |
| 6 | 8 | import { createInterface } from "node:readline"; |
| 7 | 9 | import { setTimeout as sleep } from "node:timers/promises"; |
| 8 | 10 | import { promisify } from "node:util"; |
| 9 | 11 | |
| 10 | | const { BRANCH, PORT } = process.env; |
| 12 | const { BRANCH, PORT, VOICE_PUBLIC_HOST, VOICE_SIGNALING_PORT, VOICE_MEDIA_PORT } = process.env; |
| 11 | 13 | const WORK = "/work"; |
| 12 | 14 | const REPO = "/source.git"; |
| 13 | 15 | const POLL_MS = 250; |
| ... | ... | @@ -68,6 +70,9 @@ let wanted = null; |
| 68 | 70 | let recentModules = null; |
| 69 | 71 | const releases = new Set(); |
| 70 | 72 | let failed = { rev: null, at: 0 }; |
| 73 | let voice = null; |
| 74 | let retryVoiceAt = 0; |
| 75 | let stopping = false; |
| 71 | 76 | |
| 72 | 77 | createServer((client) => { |
| 73 | 78 | const target = live; |
| ... | ... | @@ -126,7 +131,7 @@ const freePort = () => |
| 126 | 131 | |
| 127 | 132 | function sweep() { |
| 128 | 133 | const used = { rel: new Set(), modules: new Set([recentModules]) }; |
| 129 | | for (const release of releases) { |
| 134 | for (const release of [...releases, ...(voice ? [voice.release] : [])]) { |
| 130 | 135 | used.rel.add(release.rev); |
| 131 | 136 | if (release.modules) used.modules.add(basename(release.modules)).add(`${basename(release.modules)}.partial`); |
| 132 | 137 | } |
| ... | ... | @@ -135,6 +140,97 @@ function sweep() { |
| 135 | 140 | } |
| 136 | 141 | } |
| 137 | 142 | |
| 143 | const running = (proc) => proc?.pid && proc.exitCode === null && proc.signalCode === null; |
| 144 | |
| 145 | async function stop(proc) { |
| 146 | if (!running(proc)) return; |
| 147 | const exited = once(proc, "exit"); |
| 148 | proc.kill("SIGTERM"); |
| 149 | await Promise.race([exited, sleep(3000, undefined, { ref: false })]); |
| 150 | if (running(proc)) { |
| 151 | proc.kill("SIGKILL"); |
| 152 | await exited; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | async function stopVoice() { |
| 157 | if (!voice) return; |
| 158 | await Promise.all([stop(voice.signaling), stop(voice.sfu)]); |
| 159 | rmSync("/tmp/sfu-ipc.sock", { force: true }); |
| 160 | voice = null; |
| 161 | } |
| 162 | |
| 163 | async function prepareSfu(release) { |
| 164 | const pin = JSON.parse(readFileSync(`${release.dir}/flake.lock`, "utf8")).nodes["pion-webrtc"].locked; |
| 165 | if (pin.owner !== "spacebarchat" || pin.repo !== "pion-webrtc" || !/^[a-f0-9]{40}$/.test(pin.rev)) { |
| 166 | throw new Error("invalid Snowcord Pion SFU pin"); |
| 167 | } |
| 168 | const binary = `${WORK}/pion-sfu-${pin.rev}`; |
| 169 | if (existsSync(binary)) return binary; |
| 170 | |
| 171 | const scratch = mkdtempSync(`${WORK}/build-sfu-`); |
| 172 | try { |
| 173 | const source = await fetch(`https://codeload.github.com/spacebarchat/pion-webrtc/tar.gz/${pin.rev}`, { signal: AbortSignal.timeout(60000) }); |
| 174 | if (!source.ok) throw new Error(`Pion SFU download failed with HTTP ${source.status}`); |
| 175 | writeFileSync(`${scratch}/source.tar.gz`, new Uint8Array(await source.arrayBuffer())); |
| 176 | await run("tar", ["-xzf", `${scratch}/source.tar.gz`, "-C", scratch]); |
| 177 | await run("/usr/local/go/bin/go", ["build", "-trimpath", "-o", `${scratch}/pion-sfu`, "."], { |
| 178 | cwd: `${scratch}/pion-webrtc-${pin.rev}/pion-sfu`, |
| 179 | env: { ...process.env, GOTOOLCHAIN: "local", CGO_ENABLED: "0" }, |
| 180 | }); |
| 181 | renameSync(`${scratch}/pion-sfu`, binary); |
| 182 | return binary; |
| 183 | } finally { |
| 184 | rmSync(scratch, { recursive: true, force: true }); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | async function waitForVoice(proc, ready) { |
| 189 | for (const deadline = Date.now() + 15000; Date.now() < deadline; await sleep(100)) { |
| 190 | if (!running(proc)) throw new Error("voice process exited during startup"); |
| 191 | if (await ready()) return; |
| 192 | } |
| 193 | throw new Error("voice process did not become ready"); |
| 194 | } |
| 195 | |
| 196 | async function startVoice(release) { |
| 197 | try { |
| 198 | const addresses = await resolve4(VOICE_PUBLIC_HOST); |
| 199 | if (addresses.length !== 1) throw new Error("voice hostname must resolve to one IPv4 address"); |
| 200 | const binary = await prepareSfu(release); |
| 201 | if (stopping) return; |
| 202 | await stopVoice(); |
| 203 | if (stopping || live !== release) return; |
| 204 | |
| 205 | const [ip] = addresses; |
| 206 | const start = (command, args, env = process.env) => capture(spawn(command, args, { |
| 207 | cwd: "/data", stdio: ["ignore", "pipe", "pipe"], env, |
| 208 | })).on("error", (error) => log(`voice process: ${error.message}`)); |
| 209 | voice = { release, sfu: start(binary, ["-ip", ip, "-port", VOICE_MEDIA_PORT]), signaling: null }; |
| 210 | await waitForVoice(voice.sfu, () => statSync("/tmp/sfu-ipc.sock", { throwIfNoEntry: false })?.isSocket()); |
| 211 | if (stopping) return; |
| 212 | |
| 213 | voice.signaling = start(process.execPath, ["--enable-source-maps", `${release.dir}/dist/webrtc/start.js`], { |
| 214 | ...process.env, |
| 215 | PORT: VOICE_SIGNALING_PORT, |
| 216 | APPLY_DB_MIGRATIONS: "false", |
| 217 | WRTC_LIBRARY: "@spacebarchat/pion-webrtc", |
| 218 | WRTC_PUBLIC_IP: ip, |
| 219 | WRTC_PORT_MIN: VOICE_MEDIA_PORT, |
| 220 | WRTC_PORT_MAX: VOICE_MEDIA_PORT, |
| 221 | }); |
| 222 | await waitForVoice(voice.signaling, () => fetch(`http://127.0.0.1:${VOICE_SIGNALING_PORT}/`, { |
| 223 | signal: AbortSignal.timeout(1000), |
| 224 | }).then((response) => response.ok, () => false)); |
| 225 | retryVoiceAt = 0; |
| 226 | log(`voice live ${release.rev.slice(0, 7)} with UDP ${VOICE_MEDIA_PORT}`); |
| 227 | } catch (error) { |
| 228 | if (voice?.release === release) await stopVoice(); |
| 229 | retryVoiceAt = Date.now() + RETRY_FAILED_MS; |
| 230 | log(`voice ${release.rev.slice(0, 7)} failed: ${error.message}`); |
| 231 | } |
| 232 | } |
| 233 | |
| 138 | 234 | function retire(old) { |
| 139 | 235 | setTimeout(() => { |
| 140 | 236 | for (const socket of old.sockets) socket.destroy(); |
| ... | ... | @@ -220,7 +316,11 @@ async function deploy() { |
| 220 | 316 | |
| 221 | 317 | mkdirSync(`${WORK}/rel`, { recursive: true }); |
| 222 | 318 | mkdirSync(`${WORK}/modules`, { recursive: true }); |
| 223 | | process.on("SIGTERM", () => process.exit(0)); |
| 319 | process.once("SIGTERM", async () => { |
| 320 | stopping = true; |
| 321 | await stopVoice(); |
| 322 | process.exit(0); |
| 323 | }); |
| 224 | 324 | |
| 225 | 325 | (async () => { |
| 226 | 326 | let problem = ""; |
| ... | ... | @@ -239,5 +339,7 @@ process.on("SIGTERM", () => process.exit(0)); |
| 239 | 339 | for (;;) { |
| 240 | 340 | const retry = wanted === failed.rev && !live && Date.now() - failed.at > RETRY_FAILED_MS; |
| 241 | 341 | if (wanted && wanted !== live?.rev && (wanted !== failed.rev || retry)) await deploy(); |
| 342 | else if (VOICE_SIGNALING_PORT && live && Date.now() >= retryVoiceAt |
| 343 | && (voice?.release !== live || !running(voice.sfu) || !running(voice.signaling))) await startVoice(live); |
| 242 | 344 | else await sleep(100); |
| 243 | 345 | } |