@elumixor/devkit 0.12.3 → 0.12.5
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/README.md +87 -0
- package/package.json +26 -2
- package/src/bin/devkit.ts +32 -1
- package/src/blob/index.ts +169 -0
- package/src/build-targets/web.ts +66 -20
- package/src/cloud/aws.ts +108 -0
- package/src/cloud/bootstrap.ts +114 -0
- package/src/cloud/cli.ts +56 -0
- package/src/cloud/commands/context.ts +42 -0
- package/src/cloud/commands/destroy.ts +43 -0
- package/src/cloud/commands/domains.ts +68 -0
- package/src/cloud/commands/env.ts +59 -0
- package/src/cloud/commands/init.ts +74 -0
- package/src/cloud/commands/logs.ts +35 -0
- package/src/cloud/commands/rollback.ts +54 -0
- package/src/cloud/commands/status.ts +50 -0
- package/src/cloud/edge/router.js +86 -0
- package/src/cloud/env.ts +93 -0
- package/src/cloud/github.ts +92 -0
- package/src/cloud/index.ts +9 -0
- package/src/cloud/log-events.ts +56 -0
- package/src/cloud/mcp.ts +150 -0
- package/src/cloud/names.ts +98 -0
- package/src/cloud/nitro-build.ts +42 -0
- package/src/cloud/overview.ts +105 -0
- package/src/cloud/project.ts +146 -0
- package/src/cloud/reconcile/acm.ts +97 -0
- package/src/cloud/reconcile/cloudflare.ts +103 -0
- package/src/cloud/reconcile/edge.ts +108 -0
- package/src/cloud/reconcile/guardrails.ts +112 -0
- package/src/cloud/reconcile/iam.ts +328 -0
- package/src/cloud/reconcile/kvs.ts +98 -0
- package/src/cloud/reconcile/lambda.ts +305 -0
- package/src/cloud/reconcile/logs.ts +24 -0
- package/src/cloud/reconcile/platform.ts +281 -0
- package/src/cloud/reconcile/s3.ts +155 -0
- package/src/cloud/reconcile/scheduler.ts +100 -0
- package/src/cloud/records.ts +129 -0
- package/src/cloud/release.ts +268 -0
- package/src/cloud/runtime-wrapper.ts +84 -0
- package/src/deploy-targets/aws.ts +26 -0
- package/src/run-target.ts +2 -0
- package/src/runtime/index.ts +50 -0
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { readdir, stat } from "node:fs/promises";
|
|
3
|
+
import { join, relative } from "node:path";
|
|
4
|
+
import {
|
|
5
|
+
CopyObjectCommand,
|
|
6
|
+
DeleteObjectsCommand,
|
|
7
|
+
GetObjectCommand,
|
|
8
|
+
ListObjectsV2Command,
|
|
9
|
+
PutObjectCommand,
|
|
10
|
+
} from "@aws-sdk/client-s3";
|
|
11
|
+
import { accountId, orUndefined, s3 } from "../aws.ts";
|
|
12
|
+
import { platform } from "../names.ts";
|
|
13
|
+
|
|
14
|
+
/** The two buckets, named after the account because bucket names are global. */
|
|
15
|
+
export async function buckets() {
|
|
16
|
+
const account = await accountId();
|
|
17
|
+
return { sites: platform.sitesBucket(account), blobs: platform.blobsBucket(account) };
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export async function getJson<T>(bucket: string, key: string): Promise<T | undefined> {
|
|
21
|
+
const result = await orUndefined(s3().send(new GetObjectCommand({ Bucket: bucket, Key: key })), "NoSuchKey");
|
|
22
|
+
if (!result?.Body) return undefined;
|
|
23
|
+
return JSON.parse(await result.Body.transformToString()) as T;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export async function putJson(bucket: string, key: string, value: unknown) {
|
|
27
|
+
await s3().send(
|
|
28
|
+
new PutObjectCommand({
|
|
29
|
+
Bucket: bucket,
|
|
30
|
+
Key: key,
|
|
31
|
+
Body: JSON.stringify(value, null, 2),
|
|
32
|
+
ContentType: "application/json",
|
|
33
|
+
}),
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Every key under a prefix with its ETag — an MD5 for anything uploaded in one part. */
|
|
38
|
+
export async function listKeys(bucket: string, prefix: string) {
|
|
39
|
+
const keys = new Map<string, string>();
|
|
40
|
+
let ContinuationToken: string | undefined;
|
|
41
|
+
do {
|
|
42
|
+
const page = await s3().send(new ListObjectsV2Command({ Bucket: bucket, Prefix: prefix, ContinuationToken }));
|
|
43
|
+
for (const item of page.Contents ?? []) if (item.Key) keys.set(item.Key, (item.ETag ?? "").replace(/"/g, ""));
|
|
44
|
+
ContinuationToken = page.NextContinuationToken;
|
|
45
|
+
} while (ContinuationToken);
|
|
46
|
+
return keys;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** The first-level "folders" under a prefix — deploy ids under a project, say. */
|
|
50
|
+
export async function listPrefixes(bucket: string, prefix: string) {
|
|
51
|
+
const found: string[] = [];
|
|
52
|
+
let ContinuationToken: string | undefined;
|
|
53
|
+
do {
|
|
54
|
+
const page = await s3().send(
|
|
55
|
+
new ListObjectsV2Command({ Bucket: bucket, Prefix: prefix, Delimiter: "/", ContinuationToken }),
|
|
56
|
+
);
|
|
57
|
+
for (const item of page.CommonPrefixes ?? []) if (item.Prefix) found.push(item.Prefix);
|
|
58
|
+
ContinuationToken = page.NextContinuationToken;
|
|
59
|
+
} while (ContinuationToken);
|
|
60
|
+
return found;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export async function deletePrefix(bucket: string, prefix: string) {
|
|
64
|
+
const keys = [...(await listKeys(bucket, prefix)).keys()];
|
|
65
|
+
for (let i = 0; i < keys.length; i += 1000) {
|
|
66
|
+
await s3().send(
|
|
67
|
+
new DeleteObjectsCommand({
|
|
68
|
+
Bucket: bucket,
|
|
69
|
+
Delete: { Objects: keys.slice(i, i + 1000).map((Key) => ({ Key })), Quiet: true },
|
|
70
|
+
}),
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
return keys.length;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** Fingerprinted asset paths get a year of immutable caching; everything else is revalidated on each visit. */
|
|
77
|
+
const IMMUTABLE_PATTERNS = [/^_app\/immutable\//, /^assets\//, /^_next\/static\//];
|
|
78
|
+
const CACHE_IMMUTABLE = "public, max-age=31536000, immutable";
|
|
79
|
+
const CACHE_REVALIDATE = "public, max-age=0, must-revalidate";
|
|
80
|
+
const UPLOAD_CONCURRENCY = 16;
|
|
81
|
+
|
|
82
|
+
export interface UploadSummary {
|
|
83
|
+
uploaded: number;
|
|
84
|
+
copied: number;
|
|
85
|
+
bytes: number;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Uploads a directory under `prefix`, copying any file whose MD5 matches the previous deploy's
|
|
90
|
+
* server-side rather than sending it again — most of a redeploy's files are unchanged.
|
|
91
|
+
*/
|
|
92
|
+
export async function uploadDirectory(
|
|
93
|
+
bucket: string,
|
|
94
|
+
dir: string,
|
|
95
|
+
prefix: string,
|
|
96
|
+
previousPrefix: string | undefined,
|
|
97
|
+
): Promise<UploadSummary> {
|
|
98
|
+
const files = await walk(dir);
|
|
99
|
+
const previous = previousPrefix ? await listKeys(bucket, previousPrefix) : new Map<string, string>();
|
|
100
|
+
const summary: UploadSummary = { uploaded: 0, copied: 0, bytes: 0 };
|
|
101
|
+
|
|
102
|
+
const queue = [...files];
|
|
103
|
+
const worker = async () => {
|
|
104
|
+
for (let file = queue.shift(); file; file = queue.shift()) {
|
|
105
|
+
const path = relative(dir, file).split("\\").join("/");
|
|
106
|
+
const key = prefix + path;
|
|
107
|
+
const body = await Bun.file(file).bytes();
|
|
108
|
+
const md5 = createHash("md5").update(body).digest("hex");
|
|
109
|
+
const twin = previousPrefix ? `${previousPrefix}${path}` : undefined;
|
|
110
|
+
if (twin && previous.get(twin) === md5) {
|
|
111
|
+
await s3().send(
|
|
112
|
+
new CopyObjectCommand({
|
|
113
|
+
Bucket: bucket,
|
|
114
|
+
Key: key,
|
|
115
|
+
CopySource: `/${bucket}/${encodeURIComponent(twin).replace(/%2F/g, "/")}`,
|
|
116
|
+
MetadataDirective: "COPY",
|
|
117
|
+
}),
|
|
118
|
+
);
|
|
119
|
+
summary.copied++;
|
|
120
|
+
continue;
|
|
121
|
+
}
|
|
122
|
+
await s3().send(
|
|
123
|
+
new PutObjectCommand({
|
|
124
|
+
Bucket: bucket,
|
|
125
|
+
Key: key,
|
|
126
|
+
Body: body,
|
|
127
|
+
ContentType: contentType(file),
|
|
128
|
+
CacheControl: IMMUTABLE_PATTERNS.some((pattern) => pattern.test(path)) ? CACHE_IMMUTABLE : CACHE_REVALIDATE,
|
|
129
|
+
}),
|
|
130
|
+
);
|
|
131
|
+
summary.uploaded++;
|
|
132
|
+
summary.bytes += body.byteLength;
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
await Promise.all(Array.from({ length: UPLOAD_CONCURRENCY }, worker));
|
|
136
|
+
return summary;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
async function walk(dir: string): Promise<string[]> {
|
|
140
|
+
const entries = await readdir(dir, { withFileTypes: true });
|
|
141
|
+
const nested = await Promise.all(
|
|
142
|
+
entries.map(async (entry) => {
|
|
143
|
+
const path = join(dir, entry.name);
|
|
144
|
+
if (entry.isDirectory()) return walk(path);
|
|
145
|
+
return (await stat(path)).isFile() ? [path] : [];
|
|
146
|
+
}),
|
|
147
|
+
);
|
|
148
|
+
return nested.flat();
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** Bun knows MIME types by extension; S3 would otherwise serve everything as octet-stream. */
|
|
152
|
+
function contentType(file: string) {
|
|
153
|
+
const type = Bun.file(file).type;
|
|
154
|
+
return type && type !== "application/octet-stream" ? type : "application/octet-stream";
|
|
155
|
+
}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CreateScheduleCommand,
|
|
3
|
+
CreateScheduleGroupCommand,
|
|
4
|
+
DeleteScheduleCommand,
|
|
5
|
+
GetScheduleCommand,
|
|
6
|
+
GetScheduleGroupCommand,
|
|
7
|
+
ListSchedulesCommand,
|
|
8
|
+
UpdateScheduleCommand,
|
|
9
|
+
} from "@aws-sdk/client-scheduler";
|
|
10
|
+
import { orUndefined, scheduler } from "../aws.ts";
|
|
11
|
+
import { cronSlug, stage as names, platform } from "../names.ts";
|
|
12
|
+
import type { Target } from "../project.ts";
|
|
13
|
+
|
|
14
|
+
/** What a schedule delivers to the function; the runtime wrapper turns it into a `GET` on the path. */
|
|
15
|
+
export const cronEvent = (path: string) => ({ dk: "cron", path });
|
|
16
|
+
|
|
17
|
+
export async function ensureScheduleGroup() {
|
|
18
|
+
const existing = await orUndefined(
|
|
19
|
+
scheduler().send(new GetScheduleGroupCommand({ Name: platform.scheduleGroup })),
|
|
20
|
+
"ResourceNotFoundException",
|
|
21
|
+
);
|
|
22
|
+
if (!existing) await scheduler().send(new CreateScheduleGroupCommand({ Name: platform.scheduleGroup }));
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Vercel's five-field cron in UTC → Scheduler's six-field `cron(...)`. EventBridge wants `?` in
|
|
27
|
+
* whichever day field is unconstrained, and numbers its weekdays 1–7 from Sunday where cron uses 0–6.
|
|
28
|
+
*/
|
|
29
|
+
export function toScheduleExpression(cron: string) {
|
|
30
|
+
const fields = cron.trim().split(/\s+/);
|
|
31
|
+
if (fields.length !== 5) throw new Error(`Cron "${cron}" must have five fields`);
|
|
32
|
+
const [minute, hour, dayOfMonth, month, dayOfWeek] = fields as [string, string, string, string, string];
|
|
33
|
+
if (dayOfWeek === "*") return `cron(${minute} ${hour} ${dayOfMonth} ${month} ? *)`;
|
|
34
|
+
if (dayOfMonth !== "*")
|
|
35
|
+
throw new Error(`Cron "${cron}" sets both a day of month and a day of week, which EventBridge cannot express`);
|
|
36
|
+
const weekday = dayOfWeek.replace(/\d/g, (digit) => String((Number(digit) % 7) + 1));
|
|
37
|
+
return `cron(${minute} ${hour} ? ${month} ${weekday} *)`;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Makes the stage's schedules match its config: creates, updates, and deletes the ones no longer listed. */
|
|
41
|
+
export async function reconcileSchedules(target: Target, aliasArn: string, roleArn: string) {
|
|
42
|
+
const wanted = new Map(
|
|
43
|
+
target.crons.map((cron) => [names.schedule(target.project, target.stage, cronSlug(cron.path)), cron]),
|
|
44
|
+
);
|
|
45
|
+
const existing = await listStageSchedules(target);
|
|
46
|
+
|
|
47
|
+
for (const [name, cron] of wanted) {
|
|
48
|
+
const input = {
|
|
49
|
+
Name: name,
|
|
50
|
+
GroupName: platform.scheduleGroup,
|
|
51
|
+
ScheduleExpression: toScheduleExpression(cron.schedule),
|
|
52
|
+
ScheduleExpressionTimezone: "UTC",
|
|
53
|
+
FlexibleTimeWindow: { Mode: "OFF" as const },
|
|
54
|
+
Target: {
|
|
55
|
+
Arn: aliasArn,
|
|
56
|
+
RoleArn: roleArn,
|
|
57
|
+
Input: JSON.stringify(cronEvent(cron.path)),
|
|
58
|
+
RetryPolicy: { MaximumRetryAttempts: 0 },
|
|
59
|
+
},
|
|
60
|
+
Description: `${target.project} ${target.stage}: ${cron.path}`,
|
|
61
|
+
};
|
|
62
|
+
const current = existing.has(name)
|
|
63
|
+
? await scheduler().send(new GetScheduleCommand({ Name: name, GroupName: platform.scheduleGroup }))
|
|
64
|
+
: undefined;
|
|
65
|
+
if (!current) await scheduler().send(new CreateScheduleCommand(input));
|
|
66
|
+
else if (
|
|
67
|
+
current.ScheduleExpression !== input.ScheduleExpression ||
|
|
68
|
+
current.Target?.Arn !== aliasArn ||
|
|
69
|
+
current.Target?.Input !== input.Target.Input
|
|
70
|
+
)
|
|
71
|
+
await scheduler().send(new UpdateScheduleCommand(input));
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const stale = [...existing].filter((name) => !wanted.has(name));
|
|
75
|
+
for (const name of stale)
|
|
76
|
+
await scheduler().send(new DeleteScheduleCommand({ Name: name, GroupName: platform.scheduleGroup }));
|
|
77
|
+
return { kept: wanted.size, removed: stale.length };
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export async function deleteStageSchedules(target: Target) {
|
|
81
|
+
for (const name of await listStageSchedules(target))
|
|
82
|
+
await scheduler().send(new DeleteScheduleCommand({ Name: name, GroupName: platform.scheduleGroup }));
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
async function listStageSchedules(target: Target) {
|
|
86
|
+
const found = new Set<string>();
|
|
87
|
+
let NextToken: string | undefined;
|
|
88
|
+
do {
|
|
89
|
+
const page = await scheduler().send(
|
|
90
|
+
new ListSchedulesCommand({
|
|
91
|
+
GroupName: platform.scheduleGroup,
|
|
92
|
+
NamePrefix: names.schedulePrefix(target.project, target.stage),
|
|
93
|
+
NextToken,
|
|
94
|
+
}),
|
|
95
|
+
);
|
|
96
|
+
for (const schedule of page.Schedules ?? []) if (schedule.Name) found.add(schedule.Name);
|
|
97
|
+
NextToken = page.NextToken;
|
|
98
|
+
} while (NextToken);
|
|
99
|
+
return found;
|
|
100
|
+
}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { stage as names, registryKey } from "./names.ts";
|
|
2
|
+
import type { Target } from "./project.ts";
|
|
3
|
+
import { buckets, getJson, listKeys, putJson } from "./reconcile/s3.ts";
|
|
4
|
+
|
|
5
|
+
/** What one deploy was and how it ended; the history `deploys`, `status` and `rollback` read. */
|
|
6
|
+
export interface DeployRecord {
|
|
7
|
+
id: string;
|
|
8
|
+
project: string;
|
|
9
|
+
stage: string;
|
|
10
|
+
version: string;
|
|
11
|
+
commit: string;
|
|
12
|
+
branch: string;
|
|
13
|
+
author: string;
|
|
14
|
+
message: string;
|
|
15
|
+
startedAt: string;
|
|
16
|
+
finishedAt?: string;
|
|
17
|
+
status: "live" | "superseded" | "failed" | "rolled-back";
|
|
18
|
+
hostnames: readonly string[];
|
|
19
|
+
error?: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** The pointer the live stage follows. */
|
|
23
|
+
export interface CurrentPointer {
|
|
24
|
+
deployId: string;
|
|
25
|
+
version: string;
|
|
26
|
+
since: string;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** One project's stages as last deployed — what `status` lists without reading every record. */
|
|
30
|
+
export interface ProjectRegistry {
|
|
31
|
+
project: string;
|
|
32
|
+
stages: Record<string, RegisteredStage>;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface RegisteredStage {
|
|
36
|
+
functionName: string;
|
|
37
|
+
functionUrl: string;
|
|
38
|
+
/** Every hostname the stage answers on, the primary one first. */
|
|
39
|
+
hostnames: readonly string[];
|
|
40
|
+
/** Hostnames added with `devkit cloud domains add` rather than the config; each deploy keeps them. */
|
|
41
|
+
extraHostnames?: readonly string[];
|
|
42
|
+
updatedAt: string;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export async function readCurrent(target: Target) {
|
|
46
|
+
return getJson<CurrentPointer>((await buckets()).sites, names.current(target.project, target.stage));
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export async function writeCurrent(target: Target, pointer: CurrentPointer) {
|
|
50
|
+
await putJson((await buckets()).sites, names.current(target.project, target.stage), pointer);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export async function readDeploy(target: Target, deployId: string) {
|
|
54
|
+
return getJson<DeployRecord>((await buckets()).sites, names.deployRecord(target.project, target.stage, deployId));
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export async function writeDeploy(record: DeployRecord) {
|
|
58
|
+
await putJson((await buckets()).sites, names.deployRecord(record.project, record.stage, record.id), record);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** Deploys newest first — ids start with a timestamp, so key order is time order. */
|
|
62
|
+
export async function listDeploys(target: Target, limit = 10): Promise<DeployRecord[]> {
|
|
63
|
+
const { sites } = await buckets();
|
|
64
|
+
const keys = [...(await listKeys(sites, names.deploysPrefix(target.project, target.stage))).keys()].sort().reverse();
|
|
65
|
+
const records = await Promise.all(keys.slice(0, limit).map((key) => getJson<DeployRecord>(sites, key)));
|
|
66
|
+
return records.filter((record): record is DeployRecord => record !== undefined);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export async function readRegistry(project: string) {
|
|
70
|
+
return getJson<ProjectRegistry>((await buckets()).sites, registryKey(project));
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** What a deploy serves: the config's hostnames, then any added from the CLI since. */
|
|
74
|
+
export async function deployHostnames(target: Target) {
|
|
75
|
+
const extra = (await readRegistry(target.project))?.stages[target.stage]?.extraHostnames ?? [];
|
|
76
|
+
return [...new Set([...target.hostnames, ...extra])];
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export async function updateRegistry(target: Target, functionUrl: string, hostnames: readonly string[]) {
|
|
80
|
+
const registry = (await readRegistry(target.project)) ?? { project: target.project, stages: {} };
|
|
81
|
+
const previous = registry.stages[target.stage];
|
|
82
|
+
await putJson((await buckets()).sites, registryKey(target.project), {
|
|
83
|
+
...registry,
|
|
84
|
+
stages: {
|
|
85
|
+
...registry.stages,
|
|
86
|
+
[target.stage]: {
|
|
87
|
+
functionName: target.functionName,
|
|
88
|
+
functionUrl,
|
|
89
|
+
hostnames,
|
|
90
|
+
extraHostnames: previous?.extraHostnames,
|
|
91
|
+
updatedAt: new Date().toISOString(),
|
|
92
|
+
},
|
|
93
|
+
},
|
|
94
|
+
} satisfies ProjectRegistry);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export async function removeRegistryStage(target: Target) {
|
|
98
|
+
const registry = await readRegistry(target.project);
|
|
99
|
+
if (!registry) return;
|
|
100
|
+
const { [target.stage]: _, ...stages } = registry.stages;
|
|
101
|
+
await putJson((await buckets()).sites, registryKey(target.project), { ...registry, stages });
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export async function listProjects() {
|
|
105
|
+
const { sites } = await buckets();
|
|
106
|
+
const keys = [...(await listKeys(sites, "_dk/projects/")).keys()];
|
|
107
|
+
const registries = await Promise.all(keys.map((key) => getJson<ProjectRegistry>(sites, key)));
|
|
108
|
+
return registries.filter((registry): registry is ProjectRegistry => registry !== undefined);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** A hostname added or removed outside a deploy, by `devkit cloud domains`. */
|
|
112
|
+
export async function updateRegistryHostname(target: Target, host: string, present: boolean) {
|
|
113
|
+
const registry = await readRegistry(target.project);
|
|
114
|
+
const entry = registry?.stages[target.stage];
|
|
115
|
+
if (!registry || !entry) return;
|
|
116
|
+
const without = (names: readonly string[]) => names.filter((name) => name !== host);
|
|
117
|
+
await putJson((await buckets()).sites, registryKey(target.project), {
|
|
118
|
+
...registry,
|
|
119
|
+
stages: {
|
|
120
|
+
...registry.stages,
|
|
121
|
+
[target.stage]: {
|
|
122
|
+
...entry,
|
|
123
|
+
hostnames: present ? [...without(entry.hostnames), host] : without(entry.hostnames),
|
|
124
|
+
extraHostnames: present ? [...without(entry.extraHostnames ?? []), host] : without(entry.extraHostnames ?? []),
|
|
125
|
+
updatedAt: new Date().toISOString(),
|
|
126
|
+
},
|
|
127
|
+
},
|
|
128
|
+
} satisfies ProjectRegistry);
|
|
129
|
+
}
|
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
2
|
+
import { rm } from "node:fs/promises";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { PutObjectCommand } from "@aws-sdk/client-s3";
|
|
5
|
+
import { $ } from "bun";
|
|
6
|
+
import { AWS_OUTPUT } from "../build-targets/web.ts";
|
|
7
|
+
import { CloudError, log, s3 } from "./aws.ts";
|
|
8
|
+
import { type Env, ensureSecret, readParameter, readStageEnv } from "./env.ts";
|
|
9
|
+
import { startGithubDeployment } from "./github.ts";
|
|
10
|
+
import { EDGE_HEADER, HOST_HEADER, stage as names, platform } from "./names.ts";
|
|
11
|
+
import type { Target } from "./project.ts";
|
|
12
|
+
import { reconcileAliases } from "./reconcile/edge.ts";
|
|
13
|
+
import { ensureFunctionRole, ensureSchedulerRole } from "./reconcile/iam.ts";
|
|
14
|
+
import { putRoutes, type RouteRecord } from "./reconcile/kvs.ts";
|
|
15
|
+
import {
|
|
16
|
+
deleteVersion,
|
|
17
|
+
ensureFunctionUrl,
|
|
18
|
+
invokeHttp,
|
|
19
|
+
listVersions,
|
|
20
|
+
liveVersion,
|
|
21
|
+
pointLiveAlias,
|
|
22
|
+
publishFunction,
|
|
23
|
+
urlHost,
|
|
24
|
+
} from "./reconcile/lambda.ts";
|
|
25
|
+
import { ensureLogGroup } from "./reconcile/logs.ts";
|
|
26
|
+
import { buckets, deletePrefix, listPrefixes, uploadDirectory } from "./reconcile/s3.ts";
|
|
27
|
+
import { ensureScheduleGroup, reconcileSchedules } from "./reconcile/scheduler.ts";
|
|
28
|
+
import {
|
|
29
|
+
type DeployRecord,
|
|
30
|
+
deployHostnames,
|
|
31
|
+
listDeploys,
|
|
32
|
+
readCurrent,
|
|
33
|
+
readDeploy,
|
|
34
|
+
updateRegistry,
|
|
35
|
+
writeCurrent,
|
|
36
|
+
writeDeploy,
|
|
37
|
+
} from "./records.ts";
|
|
38
|
+
|
|
39
|
+
/** How many deploys a stage keeps around for rollback. */
|
|
40
|
+
export const KEEP_DEPLOYS = 10;
|
|
41
|
+
|
|
42
|
+
export interface ReleaseOptions {
|
|
43
|
+
root: string;
|
|
44
|
+
/** Skip the health check — only for the first deploy of a project with no health route yet. */
|
|
45
|
+
skipHealthCheck?: boolean;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
interface GitInfo {
|
|
49
|
+
sha: string;
|
|
50
|
+
branch: string;
|
|
51
|
+
author: string;
|
|
52
|
+
message: string;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Takes a finished build live: reconcile the stage's resources, upload, publish a version, prove
|
|
57
|
+
* it answers, then flip the alias and the route records. Nothing points at the new code until the
|
|
58
|
+
* health check has passed, so a broken deploy leaves the old one serving.
|
|
59
|
+
*/
|
|
60
|
+
export async function release(target: Target, options: ReleaseOptions) {
|
|
61
|
+
const started = Date.now();
|
|
62
|
+
const git = await gitInfo(options.root);
|
|
63
|
+
const deployId = `${timestamp()}-${git.sha.slice(0, 7)}`;
|
|
64
|
+
const staticDir = join(options.root, AWS_OUTPUT, "static");
|
|
65
|
+
const serverDir = join(options.root, AWS_OUTPUT, "server");
|
|
66
|
+
if (!existsSync(serverDir)) throw new CloudError(`No build at ${serverDir} — the web build runs first`);
|
|
67
|
+
|
|
68
|
+
const { sites, blobs } = await buckets();
|
|
69
|
+
const hostnames = await deployHostnames(target);
|
|
70
|
+
const github = await startGithubDeployment(git.sha, target.stage, `${target.project} ${deployId}`);
|
|
71
|
+
const record: DeployRecord = {
|
|
72
|
+
id: deployId,
|
|
73
|
+
project: target.project,
|
|
74
|
+
stage: target.stage,
|
|
75
|
+
version: "",
|
|
76
|
+
commit: git.sha,
|
|
77
|
+
branch: git.branch,
|
|
78
|
+
author: git.author,
|
|
79
|
+
message: git.message,
|
|
80
|
+
startedAt: new Date(started).toISOString(),
|
|
81
|
+
status: "failed",
|
|
82
|
+
hostnames,
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
try {
|
|
86
|
+
log(`Deploying ${target.project} ${target.stage} as ${deployId}`);
|
|
87
|
+
|
|
88
|
+
step("reconcile");
|
|
89
|
+
const [role, schedulerRole] = await Promise.all([ensureFunctionRole(target), ensureSchedulerRole()]);
|
|
90
|
+
await Promise.all([ensureLogGroup(target.logGroup), ensureScheduleGroup()]);
|
|
91
|
+
if (target.domains.length) await reconcileAliases(target.domains);
|
|
92
|
+
const edgeSecret = await readParameter(platform.edgeSecretParameter);
|
|
93
|
+
if (!edgeSecret) throw new CloudError("No edge secret in SSM — run `devkit cloud bootstrap`");
|
|
94
|
+
|
|
95
|
+
step("env");
|
|
96
|
+
const stageEnv = await readStageEnv(target);
|
|
97
|
+
if (target.crons.length && !stageEnv.CRON_SECRET) {
|
|
98
|
+
stageEnv.CRON_SECRET = await ensureSecret(`${target.ssmPrefix}CRON_SECRET`);
|
|
99
|
+
log(" made a CRON_SECRET for the stage");
|
|
100
|
+
}
|
|
101
|
+
const env: Env = {
|
|
102
|
+
...stageEnv,
|
|
103
|
+
DK_PROJECT: target.project,
|
|
104
|
+
DK_STAGE: target.stage,
|
|
105
|
+
DK_DEPLOY_ID: deployId,
|
|
106
|
+
DK_HOST: target.primaryHost,
|
|
107
|
+
DK_BLOB_BUCKET: blobs,
|
|
108
|
+
DK_EDGE_SECRET: edgeSecret,
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
step("upload");
|
|
112
|
+
const previous = await readCurrent(target);
|
|
113
|
+
const prefix = names.deployPrefix(target.project, target.stage, deployId);
|
|
114
|
+
const previousPrefix = previous ? names.deployPrefix(target.project, target.stage, previous.deployId) : undefined;
|
|
115
|
+
const uploaded = existsSync(staticDir)
|
|
116
|
+
? await uploadDirectory(sites, staticDir, prefix, previousPrefix)
|
|
117
|
+
: { uploaded: 0, copied: 0, bytes: 0 };
|
|
118
|
+
log(` ${uploaded.uploaded} files uploaded (${formatBytes(uploaded.bytes)}), ${uploaded.copied} unchanged`);
|
|
119
|
+
|
|
120
|
+
step("publish");
|
|
121
|
+
const codeKey = names.codeKey(target.project, target.stage, deployId);
|
|
122
|
+
await s3().send(new PutObjectCommand({ Bucket: sites, Key: codeKey, Body: await zip(serverDir) }));
|
|
123
|
+
const { version, created } = await publishFunction(target, {
|
|
124
|
+
roleArn: role.arn,
|
|
125
|
+
code: { bucket: sites, key: codeKey },
|
|
126
|
+
env,
|
|
127
|
+
description: `${deployId} ${git.message.split("\n")[0]}`.slice(0, 256),
|
|
128
|
+
});
|
|
129
|
+
record.version = version;
|
|
130
|
+
log(` ${created ? "created" : "updated"} ${target.functionName}, version ${version}`);
|
|
131
|
+
|
|
132
|
+
// The URL hangs off the `live` alias, so a brand-new function gets its alias now; it has no
|
|
133
|
+
// traffic yet, so nothing is exposed before the health check.
|
|
134
|
+
const live = await liveVersion(target.functionName);
|
|
135
|
+
if (!live) await pointLiveAlias(target.functionName, version);
|
|
136
|
+
const functionUrl = await ensureFunctionUrl(target.functionName);
|
|
137
|
+
|
|
138
|
+
if (target.options.predeploy) {
|
|
139
|
+
step("predeploy");
|
|
140
|
+
log(` ${target.options.predeploy}`);
|
|
141
|
+
await $`sh -c ${target.options.predeploy}`.cwd(options.root).env({ ...process.env, ...stageEnv });
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
if (!options.skipHealthCheck) {
|
|
145
|
+
step("health check");
|
|
146
|
+
await healthCheck(target, version, edgeSecret);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
step("flip");
|
|
150
|
+
const alias = await pointLiveAlias(target.functionName, version);
|
|
151
|
+
const route: RouteRecord = {
|
|
152
|
+
p: target.project,
|
|
153
|
+
s: target.stage,
|
|
154
|
+
d: `${target.stage}/${deployId}`,
|
|
155
|
+
fn: urlHost(functionUrl),
|
|
156
|
+
api: target.apiPrefixes,
|
|
157
|
+
mode: target.routing,
|
|
158
|
+
};
|
|
159
|
+
await putRoutes(Object.fromEntries(hostnames.map((host) => [host, route])));
|
|
160
|
+
const schedules = await reconcileSchedules(target, alias.arn, schedulerRole.arn);
|
|
161
|
+
if (schedules.kept || schedules.removed) log(` ${schedules.kept} schedules, ${schedules.removed} removed`);
|
|
162
|
+
|
|
163
|
+
step("record");
|
|
164
|
+
record.status = "live";
|
|
165
|
+
record.finishedAt = new Date().toISOString();
|
|
166
|
+
await Promise.all([
|
|
167
|
+
writeDeploy(record),
|
|
168
|
+
writeCurrent(target, { deployId, version, since: record.finishedAt }),
|
|
169
|
+
updateRegistry(target, functionUrl, hostnames),
|
|
170
|
+
previous ? supersede(target, previous.deployId) : Promise.resolve(),
|
|
171
|
+
]);
|
|
172
|
+
await github.succeed(`https://${target.primaryHost}`);
|
|
173
|
+
await prune(target, sites);
|
|
174
|
+
|
|
175
|
+
const seconds = Math.round((Date.now() - started) / 1000);
|
|
176
|
+
log(`\nLive at https://${target.primaryHost} (${seconds}s)`);
|
|
177
|
+
for (const host of hostnames.slice(1)) log(` https://${host}`);
|
|
178
|
+
} catch (error) {
|
|
179
|
+
record.finishedAt = new Date().toISOString();
|
|
180
|
+
record.error = error instanceof Error ? error.message : String(error);
|
|
181
|
+
await writeDeploy(record).catch(() => undefined);
|
|
182
|
+
await github.fail();
|
|
183
|
+
throw error;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/** Calls the new version straight through Lambda with the request the edge would make. */
|
|
188
|
+
async function healthCheck(target: Target, version: string, edgeSecret: string) {
|
|
189
|
+
const path = target.healthPath;
|
|
190
|
+
const result = await invokeHttp(target.functionName, version, {
|
|
191
|
+
version: "2.0",
|
|
192
|
+
routeKey: "$default",
|
|
193
|
+
rawPath: path,
|
|
194
|
+
rawQueryString: "",
|
|
195
|
+
headers: { host: target.primaryHost, accept: "*/*", [EDGE_HEADER]: edgeSecret, [HOST_HEADER]: target.primaryHost },
|
|
196
|
+
requestContext: {
|
|
197
|
+
domainName: target.primaryHost,
|
|
198
|
+
http: { method: "GET", path, protocol: "HTTP/1.1", sourceIp: "127.0.0.1", userAgent: "devkit-cloud-health" },
|
|
199
|
+
requestId: "health",
|
|
200
|
+
time: new Date().toUTCString(),
|
|
201
|
+
timeEpoch: Date.now(),
|
|
202
|
+
},
|
|
203
|
+
isBase64Encoded: false,
|
|
204
|
+
});
|
|
205
|
+
if (result.status < 200 || result.status >= 300)
|
|
206
|
+
throw new CloudError(
|
|
207
|
+
`Health check GET ${path} on version ${version} answered ${result.status}: ${result.body.slice(0, 300)}`,
|
|
208
|
+
);
|
|
209
|
+
log(` GET ${path} → ${result.status}`);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
async function supersede(target: Target, deployId: string) {
|
|
213
|
+
const previous = await readDeploy(target, deployId);
|
|
214
|
+
if (previous?.status === "live") await writeDeploy({ ...previous, status: "superseded" });
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/** Keeps the last `KEEP_DEPLOYS` of everything: static prefixes, code zips, function versions and records. */
|
|
218
|
+
async function prune(target: Target, sites: string) {
|
|
219
|
+
const deploys = await listDeploys(target, 1000);
|
|
220
|
+
const stale = deploys.slice(KEEP_DEPLOYS);
|
|
221
|
+
const staleVersions = new Set(stale.map((deploy) => deploy.version).filter(Boolean));
|
|
222
|
+
for (const deploy of stale) {
|
|
223
|
+
await deletePrefix(sites, names.deployPrefix(target.project, target.stage, deploy.id));
|
|
224
|
+
await deletePrefix(sites, names.codeKey(target.project, target.stage, deploy.id));
|
|
225
|
+
await deletePrefix(sites, names.deployRecord(target.project, target.stage, deploy.id));
|
|
226
|
+
}
|
|
227
|
+
// Static prefixes with no record — a deploy that died before recording — go too.
|
|
228
|
+
const known = new Set(deploys.slice(0, KEEP_DEPLOYS).map((deploy) => deploy.id));
|
|
229
|
+
for (const prefix of await listPrefixes(sites, names.stagePrefix(target.project, target.stage))) {
|
|
230
|
+
const id = prefix.slice(names.stagePrefix(target.project, target.stage).length, -1);
|
|
231
|
+
if (!known.has(id)) await deletePrefix(sites, prefix);
|
|
232
|
+
}
|
|
233
|
+
const live = await liveVersion(target.functionName);
|
|
234
|
+
for (const version of await listVersions(target.functionName)) {
|
|
235
|
+
if (version !== live && staleVersions.has(version)) await deleteVersion(target.functionName, version);
|
|
236
|
+
}
|
|
237
|
+
if (stale.length) log(` pruned ${stale.length} old deploys`);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
async function zip(dir: string) {
|
|
241
|
+
const archive = join(dir, "..", "server.zip");
|
|
242
|
+
await rm(archive, { force: true });
|
|
243
|
+
await $`zip -qry -X ${archive} .`.cwd(dir);
|
|
244
|
+
return Bun.file(archive).bytes();
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
async function gitInfo(root: string): Promise<GitInfo> {
|
|
248
|
+
const read = async (...args: string[]) => (await $`git -C ${root} ${args}`.quiet().nothrow().text()).trim();
|
|
249
|
+
const sha = (await read("rev-parse", "HEAD")) || "0000000";
|
|
250
|
+
return {
|
|
251
|
+
sha,
|
|
252
|
+
branch: process.env.GITHUB_REF_NAME ?? (await read("rev-parse", "--abbrev-ref", "HEAD")) ?? "",
|
|
253
|
+
author: (await read("log", "-1", "--format=%an")) || process.env.GITHUB_ACTOR || "",
|
|
254
|
+
message: (await read("log", "-1", "--format=%s")) || "",
|
|
255
|
+
};
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
const timestamp = () => new Date().toISOString().replace(/[-:T]/g, "").slice(0, 12);
|
|
259
|
+
|
|
260
|
+
function step(name: string) {
|
|
261
|
+
log(`\n${name}`);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
export function formatBytes(bytes: number) {
|
|
265
|
+
if (bytes < 1024) return `${bytes} B`;
|
|
266
|
+
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`;
|
|
267
|
+
return `${(bytes / 1024 / 1024).toFixed(1)} MB`;
|
|
268
|
+
}
|