@notation/factory-cloudflare 0.0.0-stage → 0.1.1
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/image/Dockerfile +17 -0
- package/implementations/bindings.ts +8 -0
- package/implementations/code.ts +18 -0
- package/implementations/deployer.ts +212 -0
- package/implementations/index.ts +4 -0
- package/implementations/operator-auth.ts +47 -0
- package/implementations/router.ts +173 -0
- package/implementations/vault.ts +59 -0
- package/lib/access.ts +37 -0
- package/lib/artifacts.ts +74 -0
- package/lib/builds-token.ts +95 -0
- package/lib/cloudflare-api.ts +55 -0
- package/lib/outpost-workers.ts +41 -0
- package/lib/router-config.ts +27 -0
- package/package.json +39 -3
- package/resources/access.ts +74 -0
- package/resources/artifacts.ts +55 -0
- package/resources/builds-token.ts +55 -0
- package/resources/outpost-workers.ts +43 -0
- package/workers/build-watch.ts +128 -0
- package/workers/channel.ts +83 -0
- package/workers/cloudflare-workers.d.ts +34 -0
- package/workers/deployer.test.ts +97 -0
- package/workers/deployer.ts +106 -0
- package/workers/loader.ts +59 -0
- package/workers/outpost-api.ts +16 -0
- package/workers/outpost-settings.ts +23 -0
- package/workers/outpost-source.test.ts +44 -0
- package/workers/outpost-source.ts +59 -0
- package/workers/outpost.gen.ts +2 -0
- package/workers/outpost.ts +171 -0
- package/workers/outposts.ts +315 -0
- package/workers/router.ts +37 -0
- package/workers/test/.wrangler/state/v3/cache/miniflare-CacheObject/metadata.sqlite +0 -0
- package/workers/test/.wrangler/state/v3/cache/miniflare-CacheObject/metadata.sqlite-shm +0 -0
- package/workers/test/.wrangler/state/v3/cache/miniflare-CacheObject/metadata.sqlite-wal +0 -0
- package/workers/test/.wrangler/state/v3/d1/miniflare-D1DatabaseObject/metadata.sqlite +0 -0
- package/workers/test/.wrangler/state/v3/d1/miniflare-D1DatabaseObject/metadata.sqlite-shm +0 -0
- package/workers/test/.wrangler/state/v3/d1/miniflare-D1DatabaseObject/metadata.sqlite-wal +0 -0
- package/workers/test/.wrangler/state/v3/kv/miniflare-KVNamespaceObject/metadata.sqlite +0 -0
- package/workers/test/.wrangler/state/v3/kv/miniflare-KVNamespaceObject/metadata.sqlite-shm +0 -0
- package/workers/test/.wrangler/state/v3/kv/miniflare-KVNamespaceObject/metadata.sqlite-wal +0 -0
- package/workers/test/.wrangler/state/v3/observability/miniflare-wobs-trace-store/a590acd76969f996ec6e4b599c3c09f58c283a76f2d61392b5d3046caf557602.sqlite +0 -0
- package/workers/test/.wrangler/state/v3/observability/miniflare-wobs-trace-store/a590acd76969f996ec6e4b599c3c09f58c283a76f2d61392b5d3046caf557602.sqlite-shm +0 -0
- package/workers/test/.wrangler/state/v3/observability/miniflare-wobs-trace-store/a590acd76969f996ec6e4b599c3c09f58c283a76f2d61392b5d3046caf557602.sqlite-wal +0 -0
- package/workers/test/.wrangler/state/v3/observability/miniflare-wobs-trace-store/metadata.sqlite +0 -0
- package/workers/test/.wrangler/state/v3/observability/miniflare-wobs-trace-store/metadata.sqlite-shm +0 -0
- package/workers/test/.wrangler/state/v3/observability/miniflare-wobs-trace-store/metadata.sqlite-wal +0 -0
- package/workers/test/.wrangler/state/v3/r2/miniflare-R2BucketObject/metadata.sqlite +0 -0
- package/workers/test/.wrangler/state/v3/r2/miniflare-R2BucketObject/metadata.sqlite-shm +0 -0
- package/workers/test/.wrangler/state/v3/r2/miniflare-R2BucketObject/metadata.sqlite-wal +0 -0
- package/workers/test/evaluate-host.ts +22 -0
- package/workers/test/wrangler.jsonc +7 -0
- package/workers/types.ts +44 -0
- package/README.md +0 -3
|
@@ -0,0 +1,315 @@
|
|
|
1
|
+
import { deriveKey, hmacHex, type OutpostConfig, type Run, timingSafeEqual } from "@notation/factory/router";
|
|
2
|
+
import git from "isomorphic-git";
|
|
3
|
+
import http from "isomorphic-git/http/web";
|
|
4
|
+
import { Volume } from "memfs";
|
|
5
|
+
import * as v from "valibot";
|
|
6
|
+
import defaultDockerfile from "factory:dockerfile";
|
|
7
|
+
import { deleteRepo, ensureRepo, repoToken } from "../lib/artifacts.ts";
|
|
8
|
+
import { cloudflare, cloudflareOrNone } from "../lib/cloudflare-api.ts";
|
|
9
|
+
import { deleteOutpostWorker } from "../lib/outpost-workers.ts";
|
|
10
|
+
import type { StartedBuild } from "./build-watch.ts";
|
|
11
|
+
import { CONTAINER_CLASS, RUN_HEADERS } from "./outpost-api.ts";
|
|
12
|
+
import outpostBundle from "./outpost.gen.ts";
|
|
13
|
+
import { imageSource, type RuntimeAccount } from "./outpost-source.ts";
|
|
14
|
+
import { type OutpostSettings, OutpostSettingsSchema } from "./outpost-settings.ts";
|
|
15
|
+
import type { KVNamespace, SecretsStoreSecret } from "./types.ts";
|
|
16
|
+
|
|
17
|
+
export type OutpostsEnv = {
|
|
18
|
+
FACTORY_KV: KVNamespace;
|
|
19
|
+
FACTORY_ROOT_KEY: SecretsStoreSecret;
|
|
20
|
+
/** A user API token, since the Workers Builds API refuses account tokens. */
|
|
21
|
+
CLOUDFLARE_BUILDS_TOKEN: SecretsStoreSecret;
|
|
22
|
+
/** The KV key of the outpost settings. */
|
|
23
|
+
OUTPOST_SETTINGS_KEY: string;
|
|
24
|
+
/** Each runtime's account: its secret names, and its CLI's setup. */
|
|
25
|
+
ACCOUNTS: Record<string, { secrets: string[]; setup?: string }>;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export const settingsOf = async (env: OutpostsEnv): Promise<OutpostSettings> => {
|
|
29
|
+
const settings = await env.FACTORY_KV.get(env.OUTPOST_SETTINGS_KEY, "json");
|
|
30
|
+
if (!settings) throw new Error(`The deployer has no outpost settings in KV ${env.OUTPOST_SETTINGS_KEY}. Run \`factory infra up\`.`);
|
|
31
|
+
return v.parse(OutpostSettingsSchema, settings);
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
const encoder = new TextEncoder();
|
|
35
|
+
|
|
36
|
+
const sha256 = async (text: string) =>
|
|
37
|
+
Array.from(new Uint8Array(await crypto.subtle.digest("SHA-256", encoder.encode(text))), (b) =>
|
|
38
|
+
b.toString(16).padStart(2, "0"),
|
|
39
|
+
).join("");
|
|
40
|
+
|
|
41
|
+
/** The name of the outpost Worker of a repo's container, which its Artifacts repo shares. */
|
|
42
|
+
export const outpostWorkerName = async (settings: OutpostSettings, repo: string, container: string) =>
|
|
43
|
+
`${settings.prefix}-${(await sha256(`${repo.toLowerCase()}/${container}`)).slice(0, 12)}`;
|
|
44
|
+
|
|
45
|
+
/** The KV key of the names of a repo's outpost Workers, by container id. */
|
|
46
|
+
const workersKey = (repo: string) => `outpost-workers/${repo.toLowerCase()}`;
|
|
47
|
+
|
|
48
|
+
/** The KV key of the hash of the source that an outpost Worker was last built from. */
|
|
49
|
+
export const sourceKey = (worker: string) => `outpost-source/${worker}`;
|
|
50
|
+
|
|
51
|
+
const runKey = (rootKey: string, worker: string) => deriveKey(rootKey, `outpost:${worker}`);
|
|
52
|
+
|
|
53
|
+
const signed = (key: string, timestamp: string, body: string) => hmacHex(key, `${timestamp}.${body}`);
|
|
54
|
+
|
|
55
|
+
export const verifyRun = async (env: OutpostsEnv, worker: string, timestamp: string, body: string, signature: string) =>
|
|
56
|
+
timingSafeEqual(await signed(await runKey(await env.FACTORY_ROOT_KEY.get(), worker), timestamp, body), signature);
|
|
57
|
+
|
|
58
|
+
/** The answer of a placeholder outpost Worker, which stands until its first build deploys. */
|
|
59
|
+
const PLACEHOLDER = `export default { fetch: () => new Response("building", { status: 503 }) };`;
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* The files of a container's Artifacts repo:
|
|
63
|
+
* 1. `.factory/`, as the repo has it.
|
|
64
|
+
* 2. The image's files: `Dockerfile`, Factory's default, if the container
|
|
65
|
+
* names no image, and each of its runtimes' setups.
|
|
66
|
+
* 3. `wrangler.json` and `outpost.js`, the outpost Worker, which Workers
|
|
67
|
+
* Builds deploys as it is.
|
|
68
|
+
*/
|
|
69
|
+
const outpostSource = (
|
|
70
|
+
settings: OutpostSettings,
|
|
71
|
+
worker: string,
|
|
72
|
+
container: OutpostConfig["containers"][string],
|
|
73
|
+
files: Record<string, string>,
|
|
74
|
+
accounts: Record<string, RuntimeAccount>,
|
|
75
|
+
) => {
|
|
76
|
+
const image = imageSource(container, files, accounts, defaultDockerfile);
|
|
77
|
+
const source: Record<string, string> = {};
|
|
78
|
+
for (const [path, content] of Object.entries(files)) source[`.factory/${path}`] = content;
|
|
79
|
+
Object.assign(source, image.source);
|
|
80
|
+
const config = {
|
|
81
|
+
name: worker,
|
|
82
|
+
main: "outpost.js",
|
|
83
|
+
no_bundle: true,
|
|
84
|
+
compatibility_date: settings.compatibilityDate,
|
|
85
|
+
// Cloudflare refuses a Worker's fetch of another workers.dev URL on the
|
|
86
|
+
// same account without it, and containers reply through the router's.
|
|
87
|
+
compatibility_flags: ["global_fetch_strictly_public"],
|
|
88
|
+
workers_dev: true,
|
|
89
|
+
// Cloudflare refuses preview URLs for a Worker name over 54 characters.
|
|
90
|
+
preview_urls: false,
|
|
91
|
+
observability: { enabled: true },
|
|
92
|
+
containers: [
|
|
93
|
+
{
|
|
94
|
+
// Wrangler names the application `<worker>-<class>`, and Cloudflare
|
|
95
|
+
// refuses a name with `--`, which Notation's generated names have.
|
|
96
|
+
name: worker.replace(/-{2,}/g, "-"),
|
|
97
|
+
class_name: CONTAINER_CLASS,
|
|
98
|
+
image: image.image,
|
|
99
|
+
image_build_context: image.context,
|
|
100
|
+
max_instances: 1,
|
|
101
|
+
instance_type: "standard-1",
|
|
102
|
+
...(settings.regions && { constraints: { regions: settings.regions } }),
|
|
103
|
+
},
|
|
104
|
+
],
|
|
105
|
+
durable_objects: { bindings: [{ name: "OUTPOSTS", class_name: CONTAINER_CLASS }] },
|
|
106
|
+
migrations: [{ tag: "v1", new_sqlite_classes: [CONTAINER_CLASS] }],
|
|
107
|
+
services: [{ binding: "DEPLOYER", service: settings.deployer }],
|
|
108
|
+
vars: { WORKER_NAME: worker, SLEEP_AFTER: settings.sleepAfter },
|
|
109
|
+
};
|
|
110
|
+
source["wrangler.json"] = `${JSON.stringify(config, null, 2)}\n`;
|
|
111
|
+
source["outpost.js"] = outpostBundle;
|
|
112
|
+
return source;
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
/** Commits `files` as a root commit, force-pushes it to `main` of `remote`, and returns the commit. */
|
|
116
|
+
const pushSource = async (remote: string, token: string, files: Record<string, string>, message: string) => {
|
|
117
|
+
const fs = Volume.fromJSON({}) as unknown as Parameters<typeof git.init>[0]["fs"] & {
|
|
118
|
+
mkdirSync(path: string, options: { recursive: true }): void;
|
|
119
|
+
writeFileSync(path: string, content: string): void;
|
|
120
|
+
};
|
|
121
|
+
const dir = "/repo";
|
|
122
|
+
await git.init({ fs, dir, defaultBranch: "main" });
|
|
123
|
+
for (const [path, content] of Object.entries(files)) {
|
|
124
|
+
const full = `${dir}/${path}`;
|
|
125
|
+
fs.mkdirSync(full.slice(0, full.lastIndexOf("/")), { recursive: true });
|
|
126
|
+
fs.writeFileSync(full, content);
|
|
127
|
+
await git.add({ fs, dir, filepath: path });
|
|
128
|
+
}
|
|
129
|
+
const commit = await git.commit({ fs, dir, message, author: { name: "Notation Factory", email: "notation-factory@users.noreply.github.com" } });
|
|
130
|
+
const result = await git.push({
|
|
131
|
+
fs,
|
|
132
|
+
http,
|
|
133
|
+
dir,
|
|
134
|
+
url: remote,
|
|
135
|
+
ref: "main",
|
|
136
|
+
force: true,
|
|
137
|
+
headers: { authorization: `Bearer ${token}` },
|
|
138
|
+
});
|
|
139
|
+
if (!result.ok) throw new Error(`The push to the outpost's Artifacts repo failed: ${result.error ?? "no reason given"}`);
|
|
140
|
+
return commit;
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
/** The build token that Workers Builds deploys with: the builds token, which this registers once. */
|
|
144
|
+
const buildToken = async (token: string, accountId: string, kv: KVNamespace) => {
|
|
145
|
+
const { id } = await cloudflare<{ id: string }>(token, "GET", "/user/tokens/verify");
|
|
146
|
+
const key = `build-token/${id}`;
|
|
147
|
+
const cached = await kv.get(key);
|
|
148
|
+
if (cached) return cached;
|
|
149
|
+
const tokens = await cloudflare<{ build_token_uuid: string; cloudflare_token_id: string }[]>(
|
|
150
|
+
token,
|
|
151
|
+
"GET",
|
|
152
|
+
`/accounts/${accountId}/builds/tokens`,
|
|
153
|
+
);
|
|
154
|
+
const uuid =
|
|
155
|
+
tokens.find((t) => t.cloudflare_token_id === id)?.build_token_uuid ??
|
|
156
|
+
(
|
|
157
|
+
await cloudflare<{ build_token_uuid: string }>(token, "POST", `/accounts/${accountId}/builds/tokens`, {
|
|
158
|
+
build_token_name: "notation-factory-deployer",
|
|
159
|
+
build_token_secret: token,
|
|
160
|
+
cloudflare_token_id: id,
|
|
161
|
+
})
|
|
162
|
+
).build_token_uuid;
|
|
163
|
+
await kv.put(key, uuid);
|
|
164
|
+
return uuid;
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
/** The tag of the Worker `worker`, which this uploads as a placeholder if it doesn't exist. */
|
|
168
|
+
const ensureWorker = async (token: string, settings: OutpostSettings, worker: string) => {
|
|
169
|
+
const path = `/accounts/${settings.accountId}/workers/services/${worker}`;
|
|
170
|
+
const found = await cloudflareOrNone<{ default_environment: { script: { tag: string } } }>(token, "GET", path);
|
|
171
|
+
if (found) return found.default_environment.script.tag;
|
|
172
|
+
const form = new FormData();
|
|
173
|
+
form.append(
|
|
174
|
+
"metadata",
|
|
175
|
+
new Blob([JSON.stringify({ main_module: "placeholder.js", compatibility_date: settings.compatibilityDate })], {
|
|
176
|
+
type: "application/json",
|
|
177
|
+
}),
|
|
178
|
+
);
|
|
179
|
+
form.append("placeholder.js", new Blob([PLACEHOLDER], { type: "application/javascript+module" }), "placeholder.js");
|
|
180
|
+
const uploaded = await cloudflare<{ tag: string }>(
|
|
181
|
+
token,
|
|
182
|
+
"PUT",
|
|
183
|
+
`/accounts/${settings.accountId}/workers/scripts/${worker}`,
|
|
184
|
+
form,
|
|
185
|
+
);
|
|
186
|
+
return uploaded.tag;
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Creates the build config of the Worker `tag` from its Artifacts repo, and
|
|
191
|
+
* starts its first build, if it has no config. Resolves to the uuid of the
|
|
192
|
+
* build it started.
|
|
193
|
+
*/
|
|
194
|
+
const build = async (
|
|
195
|
+
token: string,
|
|
196
|
+
settings: OutpostSettings,
|
|
197
|
+
worker: string,
|
|
198
|
+
tag: string,
|
|
199
|
+
repo: { id: string },
|
|
200
|
+
buildTokenUuid: string,
|
|
201
|
+
) => {
|
|
202
|
+
const base = `/accounts/${settings.accountId}/builds`;
|
|
203
|
+
const triggers = await cloudflare<{ trigger_uuid: string }[]>(token, "GET", `${base}/workers/${tag}/triggers`);
|
|
204
|
+
// A push to a repo with a build config starts a build by itself.
|
|
205
|
+
if (triggers.length > 0) return undefined;
|
|
206
|
+
const build = { build_command: "", deploy_command: "npx wrangler deploy", build_token_uuid: buildTokenUuid, root_directory: "/" };
|
|
207
|
+
await cloudflare(token, "POST", `${base}/workers`, {
|
|
208
|
+
script_tag: tag,
|
|
209
|
+
previews_enabled: false,
|
|
210
|
+
previews_base_config: build,
|
|
211
|
+
production_settings: build,
|
|
212
|
+
git_repository: {
|
|
213
|
+
provider_type: "cloudflare_artifacts",
|
|
214
|
+
provider_account_id: settings.namespace,
|
|
215
|
+
provider_account_name: settings.namespace,
|
|
216
|
+
repo_id: repo.id,
|
|
217
|
+
repo_name: worker,
|
|
218
|
+
branch: "main",
|
|
219
|
+
},
|
|
220
|
+
});
|
|
221
|
+
const [trigger] = await cloudflare<{ trigger_uuid: string }[]>(token, "GET", `${base}/workers/${tag}/triggers`);
|
|
222
|
+
if (!trigger) throw new Error(`Workers Builds has no build trigger for ${worker}.`);
|
|
223
|
+
const started = await cloudflare<{ build_uuid?: string } | null>(
|
|
224
|
+
token,
|
|
225
|
+
"POST",
|
|
226
|
+
`${base}/triggers/${trigger.trigger_uuid}/builds`,
|
|
227
|
+
{ branch: "main" },
|
|
228
|
+
);
|
|
229
|
+
return started?.build_uuid;
|
|
230
|
+
};
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Builds the outpost Worker of each of a repo's containers, and deletes the
|
|
234
|
+
* Workers and container applications of containers the repo no longer has:
|
|
235
|
+
* 1. Commits the container's source to its Artifacts repo, unless it's the
|
|
236
|
+
* source that the Worker was last built from.
|
|
237
|
+
* 2. Creates the Worker as a placeholder, if it doesn't exist.
|
|
238
|
+
* 3. Creates the Worker's build config, and starts its first build, if it has
|
|
239
|
+
* none. Later pushes to the Artifacts repo start builds by themselves. A
|
|
240
|
+
* build builds the image and deploys the Worker.
|
|
241
|
+
* Resolves to the builds it started.
|
|
242
|
+
*/
|
|
243
|
+
export const deployOutposts = async (
|
|
244
|
+
env: OutpostsEnv,
|
|
245
|
+
repo: string,
|
|
246
|
+
outpost: OutpostConfig | undefined,
|
|
247
|
+
files: Record<string, string>,
|
|
248
|
+
): Promise<StartedBuild[]> => {
|
|
249
|
+
const settings = await settingsOf(env);
|
|
250
|
+
const token = await env.CLOUDFLARE_BUILDS_TOKEN.get();
|
|
251
|
+
const started: StartedBuild[] = [];
|
|
252
|
+
const kv = env.FACTORY_KV;
|
|
253
|
+
const previous = ((await kv.get(workersKey(repo), "json")) ?? {}) as Record<string, string>;
|
|
254
|
+
const current: Record<string, string> = {};
|
|
255
|
+
for (const [id, container] of Object.entries(outpost?.containers ?? {})) {
|
|
256
|
+
const worker = await outpostWorkerName(settings, repo, id);
|
|
257
|
+
current[id] = worker;
|
|
258
|
+
const source = outpostSource(settings, worker, container, files, env.ACCOUNTS);
|
|
259
|
+
const hash = await sha256(JSON.stringify(Object.entries(source).sort(([a], [b]) => a.localeCompare(b))));
|
|
260
|
+
if ((await kv.get(sourceKey(worker))) === hash) continue;
|
|
261
|
+
const artifact = await ensureRepo(token, settings.accountId, settings.namespace, worker);
|
|
262
|
+
const write = await repoToken(token, settings.accountId, settings.namespace, worker);
|
|
263
|
+
const commit = await pushSource(artifact.remote, write, source, `${repo}: container ${id}`);
|
|
264
|
+
const tag = await ensureWorker(token, settings, worker);
|
|
265
|
+
const uuid = await build(token, settings, worker, tag, artifact, await buildToken(token, settings.accountId, kv));
|
|
266
|
+
await kv.put(sourceKey(worker), hash);
|
|
267
|
+
started.push({ container: id, worker, tag, commit, hash, ...(uuid && { build: uuid }) });
|
|
268
|
+
console.log(JSON.stringify({ repo, container: id, worker, building: true }));
|
|
269
|
+
}
|
|
270
|
+
for (const [id, worker] of Object.entries(previous)) {
|
|
271
|
+
if (current[id] === worker) continue;
|
|
272
|
+
await deleteOutpostWorker(token, settings.accountId, worker);
|
|
273
|
+
await deleteRepo(token, settings.accountId, settings.namespace, worker);
|
|
274
|
+
await kv.delete(sourceKey(worker));
|
|
275
|
+
console.log(JSON.stringify({ repo, container: id, worker, deleted: true }));
|
|
276
|
+
}
|
|
277
|
+
if (Object.keys(current).length) await kv.put(workersKey(repo), JSON.stringify(current));
|
|
278
|
+
else await kv.delete(workersKey(repo));
|
|
279
|
+
return started;
|
|
280
|
+
};
|
|
281
|
+
|
|
282
|
+
/** The account's workers.dev subdomain, which outpost Workers' URLs are under. */
|
|
283
|
+
const subdomain = async (env: OutpostsEnv, settings: OutpostSettings) => {
|
|
284
|
+
const key = "workers-subdomain";
|
|
285
|
+
const cached = await env.FACTORY_KV.get(key);
|
|
286
|
+
if (cached) return cached;
|
|
287
|
+
const { subdomain } = await cloudflare<{ subdomain: string }>(
|
|
288
|
+
await env.CLOUDFLARE_BUILDS_TOKEN.get(),
|
|
289
|
+
"GET",
|
|
290
|
+
`/accounts/${settings.accountId}/workers/subdomain`,
|
|
291
|
+
);
|
|
292
|
+
await env.FACTORY_KV.put(key, subdomain);
|
|
293
|
+
return subdomain;
|
|
294
|
+
};
|
|
295
|
+
|
|
296
|
+
/** Posts `run` to the outpost Worker of its repo's container, signed with the Worker's key. */
|
|
297
|
+
export const postRun = async (env: OutpostsEnv, run: Run) => {
|
|
298
|
+
const settings = await settingsOf(env);
|
|
299
|
+
const worker = await outpostWorkerName(settings, run.repo, run.container);
|
|
300
|
+
const body = JSON.stringify(run);
|
|
301
|
+
const timestamp = String(Date.now());
|
|
302
|
+
const signature = await signed(await runKey(await env.FACTORY_ROOT_KEY.get(), worker), timestamp, body);
|
|
303
|
+
const response = await fetch(`https://${worker}.${await subdomain(env, settings)}.workers.dev/runs`, {
|
|
304
|
+
method: "POST",
|
|
305
|
+
headers: { "content-type": "application/json", [RUN_HEADERS.timestamp]: timestamp, [RUN_HEADERS.signature]: signature },
|
|
306
|
+
body,
|
|
307
|
+
});
|
|
308
|
+
const text = await response.text();
|
|
309
|
+
if (response.status === 503 && text === "building") {
|
|
310
|
+
throw new Error(`The container \`${run.container}\` has no image yet. Its first build hasn't finished.`);
|
|
311
|
+
}
|
|
312
|
+
if (!response.ok) {
|
|
313
|
+
throw new Error(`The container \`${run.container}\`'s Worker ${worker} refused the run with status ${response.status}: ${text.slice(0, 500)}`);
|
|
314
|
+
}
|
|
315
|
+
};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import {
|
|
2
|
+
handle,
|
|
3
|
+
type OutpostConfig,
|
|
4
|
+
outpostKey,
|
|
5
|
+
type RemoteChannel,
|
|
6
|
+
type RemoteDeployer,
|
|
7
|
+
type RouterConfig,
|
|
8
|
+
type RouterStore,
|
|
9
|
+
} from "@notation/factory/router";
|
|
10
|
+
import type { KVNamespace, SecretsStoreSecret } from "./types.ts";
|
|
11
|
+
|
|
12
|
+
type Env = {
|
|
13
|
+
FACTORY_KV: KVNamespace;
|
|
14
|
+
FACTORY_ROOT_KEY: SecretsStoreSecret;
|
|
15
|
+
FACTORY_DEPLOYER?: RemoteDeployer;
|
|
16
|
+
} & Record<string, unknown>;
|
|
17
|
+
|
|
18
|
+
type Context = { waitUntil(task: Promise<unknown>): void };
|
|
19
|
+
|
|
20
|
+
const store = async (env: Env, ctx: Context): Promise<RouterStore> => ({
|
|
21
|
+
config: ((await env.FACTORY_KV.get("config", "json")) ?? {}) as Partial<RouterConfig>,
|
|
22
|
+
// Each channel is a Worker that the router binds as a service.
|
|
23
|
+
channel: (binding) => env[binding] as RemoteChannel | undefined,
|
|
24
|
+
outpost: async (repo) =>
|
|
25
|
+
((await env.FACTORY_KV.get(outpostKey(repo), "json")) ?? undefined) as
|
|
26
|
+
| OutpostConfig
|
|
27
|
+
| undefined,
|
|
28
|
+
deployer: env.FACTORY_DEPLOYER,
|
|
29
|
+
rootKey: () => env.FACTORY_ROOT_KEY.get(),
|
|
30
|
+
background: (task) => ctx.waitUntil(task),
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
export default {
|
|
34
|
+
async fetch(request: Request, env: Env, ctx: Context) {
|
|
35
|
+
return handle(request, await store(env, ctx));
|
|
36
|
+
},
|
|
37
|
+
};
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
package/workers/test/.wrangler/state/v3/observability/miniflare-wobs-trace-store/metadata.sqlite
ADDED
|
Binary file
|
package/workers/test/.wrangler/state/v3/observability/miniflare-wobs-trace-store/metadata.sqlite-shm
ADDED
|
Binary file
|
package/workers/test/.wrangler/state/v3/observability/miniflare-wobs-trace-store/metadata.sqlite-wal
ADDED
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { evaluateOutpost, worktree } from "@notation/factory/deployer";
|
|
2
|
+
import { loaderContext } from "../loader.ts";
|
|
3
|
+
import type { WorkerLoader } from "../types.ts";
|
|
4
|
+
|
|
5
|
+
// Serves the deployer's evaluate and worktree over fetch, for deployer.test.ts.
|
|
6
|
+
export default {
|
|
7
|
+
async fetch(request: Request, env: { LOADER: WorkerLoader }) {
|
|
8
|
+
const { repo, files, trigger, payload } = (await request.json()) as {
|
|
9
|
+
repo: string;
|
|
10
|
+
files: Record<string, string>;
|
|
11
|
+
trigger?: string;
|
|
12
|
+
payload?: unknown;
|
|
13
|
+
};
|
|
14
|
+
const context = { ...loaderContext(env.LOADER), getFiles: async () => files };
|
|
15
|
+
try {
|
|
16
|
+
if (trigger) return Response.json({ branch: (await worktree(repo, trigger, payload, context)) ?? null });
|
|
17
|
+
return Response.json({ outpost: await evaluateOutpost(repo, files, context) });
|
|
18
|
+
} catch (error) {
|
|
19
|
+
return Response.json({ error: error instanceof Error ? error.message : String(error) });
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
};
|
package/workers/types.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
// The parts of the Workers runtime the hub's Workers use. The full types,
|
|
2
|
+
// @cloudflare/workers-types, replace Node's globals, which the rest of the
|
|
3
|
+
// repo type-checks against.
|
|
4
|
+
|
|
5
|
+
export type KVNamespace = {
|
|
6
|
+
get(key: string, type: "json"): Promise<unknown>;
|
|
7
|
+
get(key: string): Promise<string | null>;
|
|
8
|
+
put(key: string, value: string): Promise<void>;
|
|
9
|
+
delete(key: string): Promise<void>;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
/** A Durable Object namespace, whose stubs have the class's methods. */
|
|
13
|
+
export type DurableObjectNamespace<T> = {
|
|
14
|
+
idFromName(name: string): unknown;
|
|
15
|
+
get(id: unknown): T;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export type SecretsStoreSecret = { get(): Promise<string> };
|
|
19
|
+
|
|
20
|
+
export type R2Bucket = {
|
|
21
|
+
get(key: string): Promise<{ body: ReadableStream } | null>;
|
|
22
|
+
/** A stream must have a known length, such as a request's body with `content-length`. */
|
|
23
|
+
put(key: string, value: ReadableStream | null): Promise<unknown>;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
type LoadedWorker = {
|
|
27
|
+
getEntrypoint(): { fetch(request: Request): Promise<Response> };
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
/** The Worker Loader binding: runs code given at runtime in an isolate of its own. */
|
|
31
|
+
export type WorkerLoader = {
|
|
32
|
+
get(
|
|
33
|
+
id: string,
|
|
34
|
+
code: () => {
|
|
35
|
+
compatibilityDate: string;
|
|
36
|
+
compatibilityFlags?: string[];
|
|
37
|
+
mainModule: string;
|
|
38
|
+
modules: Record<string, string | { js: string }>;
|
|
39
|
+
/** `null` blocks the isolate's network access. */
|
|
40
|
+
globalOutbound: null;
|
|
41
|
+
env: Record<string, unknown>;
|
|
42
|
+
},
|
|
43
|
+
): LoadedWorker;
|
|
44
|
+
};
|
package/README.md
DELETED