@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 410 lines 14.1 KB
Raw
1 import * as s3files from "@distilled.cloud/aws/s3files";
2 import { Unowned } from "alchemy/AdoptPolicy";
3 import { isResolved } from "alchemy/Diff";
4 import * as Provider from "alchemy/Provider";
5 import { Resource } from "alchemy/Resource";
6 import { createInternalTags, diffTags, hasAlchemyTags } from "alchemy/Tags";
7 import * as Effect from "effect/Effect";
8 import * as Stream from "effect/Stream";
9 import type { PolicyDocument } from "../IAM/Policy.ts";
10 import type { Providers } from "../Providers.ts";
11 import {
12 retryWhileConflict,
13 S3FilesNotConverged,
14 toTagList,
15 toTagRecord,
16 untilSettled,
17 } from "./internal.ts";
18
19 export interface FileSystemProps {
20 /**
21 * ARN of the S3 general purpose bucket the file system exposes as POSIX
22 * storage (`arn:aws:s3:::my-bucket`). The bucket must have versioning
23 * enabled. Changing it replaces the file system.
24 */
25 bucket: string;
26 /**
27 * Optional key prefix that scopes the file system to a portion of the
28 * bucket. Changing it replaces the file system.
29 */
30 prefix?: string;
31 /**
32 * ARN of the IAM role that grants the S3 Files service permission to read
33 * and write the bucket on the file system's behalf. S3 Files runs on EFS
34 * infrastructure, so the role must trust the
35 * `elasticfilesystem.amazonaws.com` service principal. Changing it
36 * replaces the file system.
37 */
38 roleArn: string;
39 /**
40 * KMS key used to encrypt file system data. Changing it replaces the file
41 * system.
42 * @default AWS-managed encryption
43 */
44 kmsKeyId?: string;
45 /**
46 * Acknowledge the service warning that applies when creating a file system
47 * over a bucket that already contains data.
48 * @default false
49 */
50 acceptBucketWarning?: boolean;
51 /**
52 * IAM resource policy controlling access to the file system. Omitting it
53 * removes any existing policy.
54 */
55 policy?: PolicyDocument;
56 /**
57 * When true, deletion force-deletes the file system even if it still has
58 * pending export data.
59 * @default false
60 */
61 forceDestroy?: boolean;
62 /**
63 * Tags to apply to the file system. Merged with internal Alchemy tags.
64 */
65 tags?: Record<string, string>;
66 }
67
68 export interface FileSystem extends Resource<
69 "AWS.S3Files.FileSystem",
70 FileSystemProps,
71 {
72 /**
73 * Unique ID of the file system.
74 */
75 fileSystemId: string;
76 /**
77 * ARN of the file system.
78 */
79 fileSystemArn: string;
80 /**
81 * Name of the S3 general purpose bucket backing the file system.
82 */
83 bucket: string;
84 /**
85 * ARN of the IAM role the file system uses to access the backing bucket.
86 */
87 roleArn: string;
88 /**
89 * Current lifecycle status of the file system (e.g. `AVAILABLE`).
90 */
91 status: string;
92 /**
93 * Key prefix within the backing bucket that scopes the file system, if
94 * one was configured.
95 */
96 prefix?: string;
97 },
98 never,
99 Providers
100 > {}
101
102 /**
103 * An Amazon S3 File System — POSIX-style file system access over an S3
104 * general purpose bucket (mountable from EC2/ECS via mount targets, with
105 * application-scoped access via {@link AccessPoint}s).
106 *
107 * S3 Files is a newer service; availability varies by region and account.
108 *
109 * ### Creating a File System
110 * **Example:** Basic File System
111 * ```typescript
112 * import * as AWS from "@alchemy.run/aws";
113 *
114 * // S3 Files requires versioning on the source bucket.
115 * const bucket = yield* AWS.S3.Bucket("Data", { versioning: "Enabled" });
116 * const role = yield* AWS.IAM.Role("FilesRole", {
117 * assumeRolePolicyDocument: {
118 * Version: "2012-10-17",
119 * Statement: [
120 * {
121 * Effect: "Allow",
122 * // S3 Files runs on EFS infrastructure and assumes the role as
123 * // the elasticfilesystem service principal.
124 * Principal: { Service: "elasticfilesystem.amazonaws.com" },
125 * Action: ["sts:AssumeRole"],
126 * },
127 * ],
128 * },
129 * inlinePolicies: {
130 * bucket: {
131 * Version: "2012-10-17",
132 * Statement: [
133 * {
134 * Effect: "Allow",
135 * Action: ["s3:ListBucket", "s3:ListBucketVersions"],
136 * Resource: [bucket.bucketArn],
137 * },
138 * {
139 * Effect: "Allow",
140 * Action: ["s3:GetObject*", "s3:PutObject*", "s3:DeleteObject*", "s3:List*", "s3:AbortMultipartUpload"],
141 * Resource: [AWS.Output.interpolate`${bucket.bucketArn}/*`],
142 * },
143 * ],
144 * },
145 * },
146 * });
147 *
148 * const fs = yield* AWS.S3Files.FileSystem("Files", {
149 * bucket: bucket.bucketArn,
150 * roleArn: role.roleArn,
151 * });
152 * ```
153 *
154 * **Example:** Prefix-Scoped File System
155 * ```typescript
156 * const fs = yield* AWS.S3Files.FileSystem("Files", {
157 * bucket: bucket.bucketArn,
158 * prefix: "shared/",
159 * roleArn: role.roleArn,
160 * });
161 * ```
162 *
163 * @resource
164 */
165 export const FileSystem = Resource<FileSystem>("AWS.S3Files.FileSystem");
166
167 const replacementKey = (props: Partial<FileSystemProps>) =>
168 JSON.stringify({
169 bucket: props.bucket,
170 prefix: props.prefix,
171 roleArn: props.roleArn,
172 kmsKeyId: props.kmsKeyId,
173 });
174
175 const normalizePolicy = (policy: string | undefined) => {
176 if (policy === undefined) return undefined;
177 try {
178 return JSON.stringify(JSON.parse(policy));
179 } catch {
180 return policy;
181 }
182 };
183
184 export const FileSystemProvider = () =>
185 Provider.effect(
186 FileSystem,
187 Effect.gen(function* () {
188 const observe = (fileSystemId: string) =>
189 s3files
190 .getFileSystem({ fileSystemId })
191 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined));
192
193 const observedTags = (fileSystemId: string) =>
194 s3files.listTagsForResource.items({ resourceId: fileSystemId }).pipe(
195 Stream.runCollect,
196 Effect.map((tags) => toTagRecord(Array.from(tags))),
197 Effect.catchTag("ResourceNotFoundException", () =>
198 Effect.succeed({} as Record<string, string>),
199 ),
200 );
201
202 const toAttributes = Effect.fn(function* (fs: s3files.GetFileSystemResponse) {
203 if (
204 fs.fileSystemId === undefined ||
205 fs.fileSystemArn === undefined ||
206 fs.bucket === undefined ||
207 fs.roleArn === undefined ||
208 fs.status === undefined
209 ) {
210 return yield* new S3FilesNotConverged({
211 resource: fs.fileSystemId ?? "unknown file system",
212 status: fs.status,
213 });
214 }
215 return {
216 fileSystemId: fs.fileSystemId,
217 fileSystemArn: fs.fileSystemArn,
218 bucket: fs.bucket,
219 roleArn: fs.roleArn,
220 status: fs.status,
221 ...(fs.prefix !== undefined ? { prefix: fs.prefix } : {}),
222 };
223 });
224
225 // State-loss / conflict fallback: find the file system carrying this
226 // logical id's Alchemy tags (optionally narrowed to a bucket).
227 const findByAlchemyTags = Effect.fn(function* (id: string, bucket: string | undefined) {
228 const fileSystems = yield* s3files.listFileSystems
229 .items(bucket !== undefined ? { bucket } : {})
230 .pipe(
231 Stream.runCollect,
232 Effect.map((c) => Array.from(c)),
233 );
234 for (const fs of fileSystems) {
235 const tags = yield* observedTags(fs.fileSystemId);
236 if (yield* hasAlchemyTags(id, tags)) {
237 return yield* observe(fs.fileSystemId);
238 }
239 }
240 return undefined;
241 });
242
243 const syncTags = Effect.fn(function* (fileSystemId: string, desired: Record<string, string>) {
244 const current = yield* observedTags(fileSystemId);
245 const { upsert, removed } = diffTags(current, desired);
246 if (upsert.length > 0) {
247 yield* s3files.tagResource({
248 resourceId: fileSystemId,
249 tags: upsert.map((t) => ({ key: t.Key, value: t.Value })),
250 });
251 }
252 if (removed.length > 0) {
253 yield* s3files.untagResource({
254 resourceId: fileSystemId,
255 tagKeys: removed,
256 });
257 }
258 });
259
260 const syncPolicy = Effect.fn(function* (
261 fileSystemId: string,
262 desired: PolicyDocument | undefined,
263 ) {
264 const current = yield* s3files.getFileSystemPolicy({ fileSystemId }).pipe(
265 Effect.map((r) => r.policy),
266 Effect.catchTag("ResourceNotFoundException", () => Effect.undefined),
267 );
268 const desiredJson = desired !== undefined ? JSON.stringify(desired) : undefined;
269 if (desiredJson !== undefined) {
270 if (normalizePolicy(current) !== normalizePolicy(desiredJson)) {
271 yield* s3files.putFileSystemPolicy({
272 fileSystemId,
273 policy: desiredJson,
274 });
275 }
276 } else if (current !== undefined) {
277 yield* s3files
278 .deleteFileSystemPolicy({ fileSystemId })
279 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.void));
280 }
281 });
282
283 return FileSystem.Provider.of({
284 stables: ["fileSystemId", "fileSystemArn", "bucket"],
285 list: () =>
286 Effect.gen(function* () {
287 const fileSystems = yield* s3files.listFileSystems.items({}).pipe(
288 Stream.runCollect,
289 Effect.map((c) => Array.from(c)),
290 );
291 return fileSystems.map((fs) => ({
292 fileSystemId: fs.fileSystemId,
293 fileSystemArn: fs.fileSystemArn,
294 bucket: fs.bucket,
295 roleArn: fs.roleArn,
296 status: fs.status,
297 }));
298 }),
299 read: Effect.fn(function* ({ id, olds, output }) {
300 const found =
301 output?.fileSystemId !== undefined
302 ? yield* observe(output.fileSystemId)
303 : yield* findByAlchemyTags(id, olds?.bucket);
304 if (found === undefined) return undefined;
305 const attrs = yield* toAttributes(found);
306 const tags = toTagRecord(found.tags);
307 return (yield* hasAlchemyTags(id, tags)) ? attrs : Unowned(attrs);
308 }),
309 diff: Effect.fn(function* ({ news, olds }) {
310 if (!isResolved(news)) return undefined;
311 // There is no UpdateFileSystem API — bucket, prefix, role, and
312 // encryption are all fixed at create time. Only tags and the
313 // resource policy are mutable in place.
314 if (replacementKey(olds ?? {}) !== replacementKey(news ?? {})) {
315 return { action: "replace" } as const;
316 }
317 // fall through: engine default update (tags + policy sync)
318 }),
319 reconcile: Effect.fn(function* ({ id, news, output, session }) {
320 const clientToken = yield* Effect.sync(() => crypto.randomUUID());
321 const internalTags = yield* createInternalTags(id);
322 const desiredTags = { ...news.tags, ...internalTags };
323
324 // 1. OBSERVE — cloud state is authoritative; output only caches
325 // the server-assigned file system id.
326 let live =
327 output?.fileSystemId !== undefined
328 ? yield* observe(output.fileSystemId)
329 : yield* findByAlchemyTags(id, news.bucket);
330
331 // 2. ENSURE — create when missing. A ConflictException means a
332 // file system already exists for this bucket/prefix (possibly a
333 // concurrent create) — re-find it by our tags and only fail if
334 // it is not ours.
335 if (live === undefined) {
336 const created = yield* s3files
337 .createFileSystem({
338 bucket: news.bucket,
339 prefix: news.prefix,
340 roleArn: news.roleArn,
341 kmsKeyId: news.kmsKeyId,
342 acceptBucketWarning: news.acceptBucketWarning,
343 clientToken,
344 tags: toTagList(desiredTags),
345 })
346 .pipe(Effect.catchTag("ConflictException", () => Effect.undefined));
347 live =
348 created?.fileSystemId !== undefined
349 ? yield* observe(created.fileSystemId)
350 : yield* findByAlchemyTags(id, news.bucket);
351 }
352 if (live?.fileSystemId === undefined) {
353 return yield* new S3FilesNotConverged({
354 resource: `file system for bucket ${news.bucket}`,
355 status: live?.status,
356 });
357 }
358 const fileSystemId = live.fileSystemId;
359
360 // Wait (bounded) for the file system to leave `creating`.
361 const settled = yield* untilSettled(
362 observe(fileSystemId),
363 (fs) => fs === undefined || fs.status !== "creating",
364 );
365 if (settled === undefined || settled.status !== "available") {
366 return yield* new S3FilesNotConverged({
367 resource: fileSystemId,
368 status: settled?.status,
369 });
370 }
371
372 // 3. SYNC — tags against observed cloud tags; policy against the
373 // observed resource policy.
374 yield* syncTags(fileSystemId, desiredTags);
375 yield* syncPolicy(fileSystemId, news.policy);
376
377 // 4. RETURN fresh attributes.
378 yield* session.note(fileSystemId);
379 return yield* toAttributes(settled);
380 }),
381 delete: Effect.fn(function* ({ olds, output }) {
382 // Idempotent: initiate deletion unless it is already underway.
383 const live = yield* observe(output.fileSystemId);
384 if (live === undefined) return;
385 if (live.status !== "deleting" && live.status !== "deleted") {
386 yield* retryWhileConflict(
387 s3files.deleteFileSystem({
388 fileSystemId: output.fileSystemId,
389 forceDelete: olds.forceDestroy,
390 }),
391 ).pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.void));
392 }
393 // Wait until the file system is fully gone: the source bucket
394 // rejects DeleteBucket with BucketHasS3FileSystemAttached while
395 // the file system still exists, and the engine deletes the bucket
396 // immediately after this resource.
397 const remaining = yield* untilSettled(
398 observe(output.fileSystemId),
399 (fs) => fs === undefined,
400 );
401 if (remaining !== undefined) {
402 return yield* new S3FilesNotConverged({
403 resource: output.fileSystemId,
404 status: remaining.status,
405 });
406 }
407 }),
408 });
409 }),
410 );