@wervt/platform 0.0.6 → 0.0.8
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/dist/dev-services.js +138 -0
- package/dist/dev.d.ts +3 -1
- package/dist/dev.js +14 -5
- package/dist/provision.js +1 -2
- package/package.json +2 -2
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { join } from "node:path";
|
|
2
|
+
import { execFile, spawn } from "node:child_process";
|
|
3
|
+
import { createServer } from "node:net";
|
|
4
|
+
import { promisify } from "node:util";
|
|
5
|
+
import { serviceUrlVar } from "@wervt/core";
|
|
6
|
+
// src/dev-services.ts
|
|
7
|
+
const run = promisify(execFile);
|
|
8
|
+
const READY_TIMEOUT_MS = 6e5;
|
|
9
|
+
const stopping = /* @__PURE__ */ new Map();
|
|
10
|
+
/** The container's name: fixed per app and service, so a crashed dev server's is replaced. */
|
|
11
|
+
const devContainerName = (app, service) => `wervt-dev-${app}-${service}`;
|
|
12
|
+
/** `docker run`'s arguments for a service published on `hostPort`. */
|
|
13
|
+
function dockerRunArgs(name, image, service, hostPort) {
|
|
14
|
+
const args = [
|
|
15
|
+
"run",
|
|
16
|
+
"--rm",
|
|
17
|
+
"--name",
|
|
18
|
+
name,
|
|
19
|
+
"--init",
|
|
20
|
+
"--read-only",
|
|
21
|
+
"--tmpfs",
|
|
22
|
+
"/tmp"
|
|
23
|
+
];
|
|
24
|
+
args.push("--cap-drop", "ALL", "--security-opt", "no-new-privileges:true", "--pids-limit", "256");
|
|
25
|
+
for (const dir of service.tmpfs ?? []) args.push("--tmpfs", dir);
|
|
26
|
+
args.push("--memory", `${service.memory}b`, "--cpus", String(service.cpus));
|
|
27
|
+
args.push("-p", `127.0.0.1:${hostPort}:${service.port}`, "-e", `PORT=${service.port}`);
|
|
28
|
+
for (const [key, value] of Object.entries(service.env ?? {})) args.push("-e", `${key}=${value}`);
|
|
29
|
+
args.push(image);
|
|
30
|
+
return args;
|
|
31
|
+
}
|
|
32
|
+
function freePort() {
|
|
33
|
+
return new Promise((resolve, reject) => {
|
|
34
|
+
const server = createServer();
|
|
35
|
+
server.once("error", reject);
|
|
36
|
+
server.listen(0, "127.0.0.1", () => {
|
|
37
|
+
const address = server.address();
|
|
38
|
+
server.close(() => typeof address === "object" && address ? resolve(address.port) : reject(/* @__PURE__ */ new Error("no port")));
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
/** Without a health path, any answer will do, like the runner's open port. */
|
|
43
|
+
async function waitUntilReady(url, child, health) {
|
|
44
|
+
const deadline = Date.now() + READY_TIMEOUT_MS;
|
|
45
|
+
while (Date.now() < deadline && child.exitCode === null) {
|
|
46
|
+
if (await fetch(url, { signal: AbortSignal.timeout(2e3) }).then((res) => res.ok || !health, () => false)) return true;
|
|
47
|
+
await new Promise((done) => setTimeout(done, 500));
|
|
48
|
+
}
|
|
49
|
+
return false;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Starts the app's services in the background and returns their URL variables right away, so the
|
|
53
|
+
* dev server needn't wait for a build: until a service is up, its requests fail like a down one's.
|
|
54
|
+
* A variable that's already set wins: the developer runs that service themselves.
|
|
55
|
+
*/
|
|
56
|
+
async function startDevServices(host, app, services) {
|
|
57
|
+
const { logger } = host;
|
|
58
|
+
const wanted = Object.entries(services).filter(([name]) => {
|
|
59
|
+
if (!process.env[serviceUrlVar(name)]) return true;
|
|
60
|
+
logger.info(`Service \`${name}\` at ${process.env[serviceUrlVar(name)]} (${serviceUrlVar(name)})`);
|
|
61
|
+
return false;
|
|
62
|
+
});
|
|
63
|
+
if (!wanted.length) return {};
|
|
64
|
+
try {
|
|
65
|
+
await run("docker", [
|
|
66
|
+
"version",
|
|
67
|
+
"--format",
|
|
68
|
+
"{{.Server.Version}}"
|
|
69
|
+
]);
|
|
70
|
+
} catch {
|
|
71
|
+
logger.warn(`Docker isn't running, so the services ${wanted.map(([name]) => `\`${name}\``).join(", ")} aren't either. Start Docker, or set their ${serviceUrlVar(wanted[0][0])}.`);
|
|
72
|
+
return {};
|
|
73
|
+
}
|
|
74
|
+
const vars = {};
|
|
75
|
+
for (const [name, service] of wanted) {
|
|
76
|
+
const hostPort = await freePort();
|
|
77
|
+
const url = `http://127.0.0.1:${hostPort}`;
|
|
78
|
+
vars[serviceUrlVar(name)] = url;
|
|
79
|
+
const container = devContainerName(app, name);
|
|
80
|
+
let child;
|
|
81
|
+
let closed = false;
|
|
82
|
+
host.onClose(() => {
|
|
83
|
+
closed = true;
|
|
84
|
+
child?.kill("SIGTERM");
|
|
85
|
+
stopping.set(container, run("docker", [
|
|
86
|
+
"rm",
|
|
87
|
+
"-f",
|
|
88
|
+
container
|
|
89
|
+
]).catch(() => {}));
|
|
90
|
+
});
|
|
91
|
+
(async () => {
|
|
92
|
+
let image = service.image;
|
|
93
|
+
if (service.build) {
|
|
94
|
+
image = `wervt-dev/${app}/${name}`;
|
|
95
|
+
const context = join(host.rootDir, service.build.context);
|
|
96
|
+
const dockerfile = join(context, service.build.dockerfile ?? "Dockerfile");
|
|
97
|
+
logger.info(`Building service \`${name}\`…`);
|
|
98
|
+
try {
|
|
99
|
+
await run("docker", [
|
|
100
|
+
"build",
|
|
101
|
+
"-t",
|
|
102
|
+
image,
|
|
103
|
+
"-f",
|
|
104
|
+
dockerfile,
|
|
105
|
+
context
|
|
106
|
+
], { maxBuffer: 64 * 1024 ** 2 });
|
|
107
|
+
} catch (e) {
|
|
108
|
+
const output = String(e.stderr ?? e.message);
|
|
109
|
+
logger.warn(`Building service \`${name}\` failed:\n${output.trim().split("\n").slice(-20).join("\n")}`);
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
await stopping.get(container);
|
|
114
|
+
await run("docker", [
|
|
115
|
+
"rm",
|
|
116
|
+
"-f",
|
|
117
|
+
container
|
|
118
|
+
]).catch(() => {});
|
|
119
|
+
if (closed) return;
|
|
120
|
+
const started = performance.now();
|
|
121
|
+
child = spawn("docker", dockerRunArgs(container, image, service, hostPort), { stdio: [
|
|
122
|
+
"ignore",
|
|
123
|
+
"pipe",
|
|
124
|
+
"pipe"
|
|
125
|
+
] });
|
|
126
|
+
for (const stream of [child.stdout, child.stderr]) stream?.on("data", (chunk) => {
|
|
127
|
+
for (const line of chunk.toString().split("\n")) if (line.trim()) logger.info(`[${name}] ${line}`);
|
|
128
|
+
});
|
|
129
|
+
child.once("exit", (code) => {
|
|
130
|
+
if (!closed) logger.warn(`Service \`${name}\` stopped (exit ${code})`);
|
|
131
|
+
});
|
|
132
|
+
if (await waitUntilReady(`${url}${service.health ?? "/"}`, child, !!service.health)) logger.success(`Service \`${name}\` is up at ${url} (${Math.round((performance.now() - started) / 1e3)} s)`);
|
|
133
|
+
else if (!closed) logger.warn(`Service \`${name}\` didn't answer ${service.health ?? "/"}`);
|
|
134
|
+
})();
|
|
135
|
+
}
|
|
136
|
+
return vars;
|
|
137
|
+
}
|
|
138
|
+
export { devContainerName, dockerRunArgs, startDevServices };
|
package/dist/dev.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { SEED_TASK, SeedOutcome, runDevSeed, unscheduleSeed } from "./seed.js";
|
|
2
|
-
import { WervtRole } from "@wervt/core";
|
|
2
|
+
import { ServiceManifest, WervtRole } from "@wervt/core";
|
|
3
3
|
/** What the dev setup needs from the dev server it runs in. */
|
|
4
4
|
export interface DevHost {
|
|
5
5
|
/** The app's directory, with `drizzle/` and `.env`. */
|
|
@@ -66,6 +66,8 @@ export declare function resetDevDatabase(app: string, options?: DevOptions): Pro
|
|
|
66
66
|
*/
|
|
67
67
|
export declare function setupDev(host: DevHost, app: string, options: DevOptions, features?: {
|
|
68
68
|
storage?: boolean;
|
|
69
|
+
/** The app's service containers (its manifest's), run next to the dev server with Docker. */
|
|
70
|
+
services?: Record<string, ServiceManifest>;
|
|
69
71
|
}): Promise<void>;
|
|
70
72
|
/**
|
|
71
73
|
* App name, normalised to `[a-z0-9-]+`: `WERVT_APP` first, which `wervt deploy` sets for its
|
package/dist/dev.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { appDatabaseUrl, dbRole, dropDatabase, hmac, migrateAs, provisionDatabase, storageCredentials } from "./provision.js";
|
|
2
2
|
import { createStorage } from "./storage.js";
|
|
3
|
+
import { startDevServices } from "./dev-services.js";
|
|
3
4
|
import { SEED_TASK, runDevSeed, unscheduleSeed } from "./seed.js";
|
|
4
5
|
import { existsSync, watch } from "node:fs";
|
|
5
6
|
import { readFile } from "node:fs/promises";
|
|
@@ -57,6 +58,7 @@ async function setupDev(host, app, options, features = {}) {
|
|
|
57
58
|
accounts: { twitch: "dev" }
|
|
58
59
|
});
|
|
59
60
|
if (features.storage) await setupStorage(host, app, config.dbSecret, options);
|
|
61
|
+
if (features.services) setOwnEnv(await startDevServices(host, app, features.services));
|
|
60
62
|
const migrationsFolder = join(host.rootDir, "drizzle");
|
|
61
63
|
const hasMigrations = existsSync(migrationsFolder);
|
|
62
64
|
if (!hasMigrations) {
|
|
@@ -111,15 +113,12 @@ async function setupDev(host, app, options, features = {}) {
|
|
|
111
113
|
}
|
|
112
114
|
};
|
|
113
115
|
await migrate();
|
|
114
|
-
|
|
116
|
+
setOwnEnv({
|
|
115
117
|
DATABASE_URL: await appDatabaseUrl(provision),
|
|
116
118
|
WERVT_DB_SCHEMA: dbRole(app),
|
|
117
119
|
ELECTRIC_URL: config.electricUrl,
|
|
118
120
|
ELECTRIC_SECRET: config.electricSecret
|
|
119
|
-
};
|
|
120
|
-
const set = Object.fromEntries(Object.entries(own).filter(([key]) => process.env[key] === void 0));
|
|
121
|
-
Object.assign(process.env, set);
|
|
122
|
-
process.env[OWN_ENV] = JSON.stringify(set);
|
|
121
|
+
});
|
|
123
122
|
if (!hasMigrations) return;
|
|
124
123
|
let timer;
|
|
125
124
|
const watcher = watch(migrationsFolder, { recursive: true }, (_event, file) => {
|
|
@@ -133,6 +132,16 @@ async function setupDev(host, app, options, features = {}) {
|
|
|
133
132
|
host.onClose(() => watcher.close());
|
|
134
133
|
}
|
|
135
134
|
const OWN_ENV = "WERVT_DEV_OWN_ENV";
|
|
135
|
+
/** Sets what isn't set yet and remembers it as setupDev's own. */
|
|
136
|
+
function setOwnEnv(vars) {
|
|
137
|
+
const set = Object.fromEntries(Object.entries(vars).filter(([key]) => process.env[key] === void 0));
|
|
138
|
+
Object.assign(process.env, set);
|
|
139
|
+
const before = process.env[OWN_ENV] ? JSON.parse(process.env[OWN_ENV]) : {};
|
|
140
|
+
process.env[OWN_ENV] = JSON.stringify({
|
|
141
|
+
...before,
|
|
142
|
+
...set
|
|
143
|
+
});
|
|
144
|
+
}
|
|
136
145
|
function forgetOwnEnv(rootDir) {
|
|
137
146
|
const own = process.env[OWN_ENV];
|
|
138
147
|
if (!own) return;
|
package/dist/provision.js
CHANGED
|
@@ -244,7 +244,6 @@ async function setupPooler({ adminUrl, password }) {
|
|
|
244
244
|
/** Drops the app's schema (all its data) and role. */
|
|
245
245
|
async function dropDatabase({ app, adminUrl }) {
|
|
246
246
|
const role = dbRole(app);
|
|
247
|
-
const database = new URL(adminUrl).pathname.slice(1);
|
|
248
247
|
const admin = postgres(adminUrl, {
|
|
249
248
|
max: 1,
|
|
250
249
|
onnotice: () => {}
|
|
@@ -254,7 +253,7 @@ async function dropDatabase({ app, adminUrl }) {
|
|
|
254
253
|
await admin.unsafe(`drop schema if exists "${role}" cascade`);
|
|
255
254
|
const [row] = await admin`select exists(select 1 from pg_roles where rolname = ${role})`;
|
|
256
255
|
if (Boolean(row?.exists)) {
|
|
257
|
-
await admin.unsafe(`
|
|
256
|
+
await admin.unsafe(`drop owned by "${role}"`);
|
|
258
257
|
await admin.unsafe(`drop role "${role}"`);
|
|
259
258
|
}
|
|
260
259
|
} finally {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wervt/platform",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.8",
|
|
4
4
|
"description": "Provisioning for wervt apps: database roles, migrations, PgBouncer and object storage. Used by the control plane and local development.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"garage",
|
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
}
|
|
33
33
|
},
|
|
34
34
|
"dependencies": {
|
|
35
|
-
"@wervt/core": "^0.0.
|
|
35
|
+
"@wervt/core": "^0.0.7",
|
|
36
36
|
"aws4fetch": "^1.0.20",
|
|
37
37
|
"postgres": "^3.4.9"
|
|
38
38
|
},
|