Multiplayer agent harness for work.
porter: upload published app files as one bundle (#1724)
Jack Deckerfe383aae04a3
diff --git a/src/deploy/porter-deploy-provider.ts b/src/deploy/porter-deploy-provider.tsindex 0eed98fde..be0ade7fd 100644--- a/src/deploy/porter-deploy-provider.ts+++ b/src/deploy/porter-deploy-provider.ts@@ -3,7 +3,9 @@ import { LRUCache } from "lru-cache";import { NotFoundError } from "porter-sandbox";import type { Deployment, DeploymentVersion } from "./deploy-store.ts";import type { DeployEndpoint, DeployProvider } from "./deploy-provider.ts";-import { waitAppReady, writeTree } from "./shared-deploy-provider.ts";+import { waitAppReady } from "./shared-deploy-provider.ts";+import { normalizeRelPath, posixJoin, readTree } from "./deploy-fs.ts";+import { makeTar } from "../sandbox/tar.ts";import {createPorterClient,createPorterExec,@@ -20,7 +22,7 @@ import { createMemoryMap, type DurableMap } from "../persistence/durable-map.ts"import { createNoopAdvisoryLock, type AdvisoryLock } from "../persistence/advisory-lock.ts";import { createKeyedQueue } from "../util/async.ts";import { shq } from "../util/shell.ts";-import { errMessage, swallow } from "../util/errors.ts";+import { errMessage, swallow, swallowAs } from "../util/errors.ts";const APP_DIR = "/app";const HOME_DIR = "/root";@@ -31,6 +33,9 @@ const APP_PORT_DEFAULT = 8080;const ENDPOINT_PORT = 443;const APP_READY_WINDOW_SEC_DEFAULT = 60;const APP_START_EXEC_TIMEOUT_SEC = 60;+const BUNDLE_DIR = ".qm-bundle";+const BUNDLE_UPLOAD_TIMEOUT_MS = 600_000;+const BUNDLE_EXTRACT_TIMEOUT_SEC = 300;const RESOLVE_CACHE_MS_DEFAULT = 15_000;const RESOLVE_CACHE_MAX = 500;const KIND_TAG = "qm-kind";@@ -112,10 +117,32 @@ export function createPorterDeployProvider(opts: PorterDeployProviderOptions): Dreturn { ...declared, HOME: HOME_DIR, PORT: String(appPort), DATA_DIR };}- async function materialize(sandboxId: string, version: DeploymentVersion): Promise<void> {- const write = (abs: string, data: Uint8Array) => writeAbsBytes(sandboxId, abs, data);- await writeTree(write, APP_DIR, version.snapshotDir);- if (version.homeDir) await writeTree(write, HOME_DIR, version.homeDir);+ async function unpackTree(sandboxId: string, volumeId: string, guestDir: string, dir: string): Promise<void> {+ const files = await readTree(dir, { tolerateMissing: true });+ if (!files.length) return;+ const entries = files.map((f) => ({ path: normalizeRelPath(f.path), data: f.data }));+ const bundle = `${BUNDLE_DIR}/${randomUUID()}.tar`;+ const uploaded = await client.volumes.raw+ .writeFile(volumeId, await makeTar(entries), { path: `/${bundle}` }, { timeoutMs: BUNDLE_UPLOAD_TIMEOUT_MS })+ .then(() => true, swallowAs("porter-deploy: bundle upload failed, writing files one at a time", false));+ if (!uploaded) {+ for (const entry of entries) await writeAbsBytes(sandboxId, posixJoin(guestDir, entry.path), entry.data);+ return;+ }+ const guestBundle = posixJoin(DATA_DIR, bundle);+ const r = await execRaw(+ sandboxId,+ `mkdir -p ${shq(guestDir)} && tar -xmf ${shq(guestBundle)} -C ${shq(guestDir)}; rc=$?; ` ++ `rm -f ${shq(guestBundle)}; rmdir ${shq(posixJoin(DATA_DIR, BUNDLE_DIR))} 2>/dev/null; exit $rc`,+ BUNDLE_EXTRACT_TIMEOUT_SEC,+ );+ if (r.code !== 0)+ throw new Error(`porter deploy: unpacking into ${guestDir} failed: ${(r.stderr || r.stdout).slice(0, 300)}`);+ }++ async function materialize(sandboxId: string, volumeId: string, version: DeploymentVersion): Promise<void> {+ await unpackTree(sandboxId, volumeId, APP_DIR, version.snapshotDir);+ if (version.homeDir) await unpackTree(sandboxId, volumeId, HOME_DIR, version.homeDir);}async function startApp(sandboxId: string, version: DeploymentVersion): Promise<void> {@@ -206,7 +233,7 @@ export function createPorterDeployProvider(opts: PorterDeployProviderOptions): Dtry {await waitPorterRunning(name, sb);const address = await addressOf(d, sb);- await materialize(sb.id, version);+ await materialize(sb.id, volumeId, version);await startApp(sb.id, version);await store.put(d.id, { deploymentId: d.id, sandboxId: sb.id, name, ...address, createdAtMs: Date.now() });return endpointOf(address);diff --git a/src/deploy/shared-deploy-provider.ts b/src/deploy/shared-deploy-provider.tsindex 95ca4232f..12fd8681f 100644--- a/src/deploy/shared-deploy-provider.ts+++ b/src/deploy/shared-deploy-provider.ts@@ -1,6 +1,5 @@import type { ExecResult } from "../sandbox/sandbox.ts";import { shq } from "../util/shell.ts";-import { bytes, normalizeRelPath, posixJoin, readTree } from "./deploy-fs.ts";const EXIT_APP_EXITED = 98;const READY_EXEC_GRACE_SEC = 30;@@ -34,13 +33,3 @@ export async function waitAppReady(: `app never listened on port ${opts.appPort} within ${opts.windowSec}s`;throw new Error(why + (log ? `; last output from the entrypoint:\n${log}` : "; the entrypoint produced no output"));}--export async function writeTree(- write: (absPath: string, data: Uint8Array) => Promise<void>,- root: string,- dir: string,-): Promise<void> {- for (const f of await readTree(dir, { tolerateMissing: true })) {- await write(posixJoin(root, normalizeRelPath(f.path)), bytes(f.data));- }-}diff --git a/src/sandbox/porter-client.ts b/src/sandbox/porter-client.tsindex f3587a1bd..83da07711 100644--- a/src/sandbox/porter-client.ts+++ b/src/sandbox/porter-client.ts@@ -57,6 +57,14 @@ export interface PorterClientLike {create(body: { name?: string }): Promise<{ id: string }>;get(name: string): Promise<{ id: string }>;delete(name: string): Promise<void>;+ raw: {+ writeFile(+ id: string,+ body: Uint8Array,+ options: { path: string },+ requestOptions?: { timeoutMs?: number },+ ): Promise<void>;+ };};}diff --git a/test/porter-deploy-provider.test.ts b/test/porter-deploy-provider.test.tsindex 0e21634d5..c17c45239 100644--- a/test/porter-deploy-provider.test.ts+++ b/test/porter-deploy-provider.test.ts@@ -1,7 +1,7 @@import { test, after, beforeEach } from "node:test";import assert from "node:assert/strict";import { spawnSync } from "node:child_process";-import { mkdtempSync, writeFileSync, mkdirSync } from "node:fs";+import { mkdtempSync, writeFileSync, mkdirSync, readdirSync } from "node:fs";import { createServer } from "node:net";import { tmpdir } from "node:os";import { dirname, join } from "node:path";@@ -114,6 +114,39 @@ test("apply serves the app on a stable public domain", async () => {assert.equal(provider.profile.managedScaleToZero, false);});+const bulkFiles = (): Record<string, string> =>+ Object.fromEntries(Array.from({ length: 40 }, (_, i) => [`assets/file-${i}.txt`, `x${i}`.repeat(50_000)]));++test("apply uploads each tree as one bundle and removes it after unpacking", async () => {+ const execsBefore = fake.execScripts().length;+ await provider.apply(+ deployment("dep-bundle", "bundled"),+ version({ ...bulkFiles(), "server.js": SERVER }, "node server.js", {+ homeDir: mkdtempSync(join(tmpdir(), "porter-deploy-home-")),+ }),+ );+ const scripts = fake.execScripts().slice(execsBefore);+ assert.equal(scripts.filter((s) => s.includes("base64 -d")).length, 0);+ assert.equal(scripts.filter((s) => s.includes("tar -xmf")).length, 1);+ assert.match(await fetchText("/"), /^hello from .*-app v\?$/);+ assert.deepEqual(readdirSync(fake.volumeDir("qmt-app-dep-bundle-data")), []);+});++test("apply writes files one at a time when the bundle upload fails", async () => {+ fake.client.volumes.raw.writeFile = async () => {+ throw new Error("upload refused");+ };+ const execsBefore = fake.execScripts().length;+ await provider.apply(+ deployment("dep-fallback", "fallback"),+ version({ ...bulkFiles(), "server.js": SERVER }, "node server.js"),+ );+ const scripts = fake.execScripts().slice(execsBefore);+ assert.equal(scripts.filter((s) => s.includes("tar -xmf")).length, 0);+ assert.ok(scripts.some((s) => s.includes("base64 -d")));+ assert.match(await fetchText("/"), /^hello from .*-app v\?$/);+});+test("explicit public visibility opts the domain out of the private default", async () => {make({ visibility: "public" });await provider.apply(deployment("dep-1b", "shown"), version({ "server.js": SERVER }, "node server.js"));diff --git a/test/support/fake-porter.ts b/test/support/fake-porter.tsindex 987e848eb..218af864e 100644--- a/test/support/fake-porter.ts+++ b/test/support/fake-porter.ts@@ -1,7 +1,7 @@import { spawn, spawnSync } from "node:child_process";-import { mkdtempSync, mkdirSync, rmSync, existsSync, readFileSync } from "node:fs";+import { mkdtempSync, mkdirSync, rmSync, existsSync, readFileSync, writeFileSync } from "node:fs";import { tmpdir } from "node:os";-import { join } from "node:path";+import { dirname, join } from "node:path";import { NotFoundError } from "porter-sandbox";import type { PorterClientLike, PorterSandboxLike, PorterSandboxSpec } from "../../src/sandbox/porter-client.ts";@@ -253,6 +253,15 @@ export function installFakePorter(opts: FakePorterOptions = {}): FakePorter {rmSync(v.dir, { recursive: true, force: true });volumes.delete(name);},+ raw: {+ async writeFile(id, body, options) {+ const v = volumeById(id);+ if (!v) throw new NotFoundError(`fake porter: no volume ${id}`);+ const target = join(v.dir, options.path);+ mkdirSync(dirname(target), { recursive: true });+ writeFileSync(target, body);+ },+ },},};