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
| 1 | import * as fsx from "@distilled.cloud/aws/fsx"; |
| 2 | import { Unowned } from "alchemy/AdoptPolicy"; |
| 3 | import { isResolved } from "alchemy/Diff"; |
| 4 | import { createPhysicalName } from "alchemy/PhysicalName"; |
| 5 | import * as Provider from "alchemy/Provider"; |
| 6 | import { Resource } from "alchemy/Resource"; |
| 7 | import { createInternalTags, diffTags, hasAlchemyTags } from "alchemy/Tags"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import * as Stream from "effect/Stream"; |
| 10 | import { AWSEnvironment } from "../Environment.ts"; |
| 11 | import type { Providers } from "../Providers.ts"; |
| 12 | |
| 13 | export interface FileSystemProps { |
| 14 | /** |
| 15 | * The type of file system to create. Cannot be changed after creation |
| 16 | * (replacement). `LUSTRE` is the cheapest/fastest scratch storage; |
| 17 | * `WINDOWS`, `ONTAP`, and `OPENZFS` are the other managed engines. |
| 18 | */ |
| 19 | fileSystemType: fsx.FileSystemType; |
| 20 | /** |
| 21 | * Storage capacity in GiB. Minimums depend on the engine and deployment |
| 22 | * type (e.g. Lustre `SCRATCH_2` starts at 1200 GiB). Can be increased in |
| 23 | * place; decreasing requires replacement. |
| 24 | */ |
| 25 | storageCapacity?: number; |
| 26 | /** |
| 27 | * Storage type. `SSD` (default) or `HDD` (Lustre `PERSISTENT_1` / Windows |
| 28 | * only), or `INTELLIGENT_TIERING` (OpenZFS). Cannot be changed after |
| 29 | * creation (replacement). |
| 30 | */ |
| 31 | storageType?: fsx.StorageType; |
| 32 | /** |
| 33 | * Subnet IDs the file system is deployed into. Single-AZ engines take one |
| 34 | * subnet; multi-AZ ONTAP/OpenZFS take two. Cannot be changed after |
| 35 | * creation (replacement). |
| 36 | */ |
| 37 | subnetIds: string[]; |
| 38 | /** |
| 39 | * Security group IDs to associate with the file system's network |
| 40 | * interfaces. |
| 41 | */ |
| 42 | securityGroupIds?: string[]; |
| 43 | /** |
| 44 | * The KMS key used to encrypt the file system at rest. Cannot be changed |
| 45 | * after creation (replacement). |
| 46 | * @default the AWS-managed FSx key |
| 47 | */ |
| 48 | kmsKeyId?: string; |
| 49 | /** |
| 50 | * Engine version string (e.g. Lustre `"2.15"`, ONTAP `"9.13"`). Cannot be |
| 51 | * changed after creation (replacement). |
| 52 | */ |
| 53 | fileSystemTypeVersion?: string; |
| 54 | /** |
| 55 | * Lustre-specific configuration (deployment type, per-unit throughput, |
| 56 | * data-repository import/export, compression, etc.). Passed straight |
| 57 | * through to the FSx API. |
| 58 | */ |
| 59 | lustreConfiguration?: fsx.CreateFileSystemLustreConfiguration; |
| 60 | /** |
| 61 | * Windows-specific configuration (Active Directory, throughput capacity, |
| 62 | * deployment type). Passed straight through to the FSx API. |
| 63 | */ |
| 64 | windowsConfiguration?: fsx.CreateFileSystemWindowsConfiguration; |
| 65 | /** |
| 66 | * NetApp ONTAP-specific configuration. Passed straight through to the FSx |
| 67 | * API. |
| 68 | */ |
| 69 | ontapConfiguration?: fsx.CreateFileSystemOntapConfiguration; |
| 70 | /** |
| 71 | * OpenZFS-specific configuration. Passed straight through to the FSx API. |
| 72 | */ |
| 73 | openZFSConfiguration?: fsx.CreateFileSystemOpenZFSConfiguration; |
| 74 | /** |
| 75 | * Tags to apply to the file system. Merged with internal Alchemy tags. |
| 76 | */ |
| 77 | tags?: Record<string, string>; |
| 78 | } |
| 79 | |
| 80 | export interface FileSystem extends Resource< |
| 81 | "AWS.FSx.FileSystem", |
| 82 | FileSystemProps, |
| 83 | { |
| 84 | /** The generated ID of the file system, e.g. `fs-0123456789abcdef0`. */ |
| 85 | fileSystemId: string; |
| 86 | /** The ARN of the file system. */ |
| 87 | fileSystemArn: string; |
| 88 | /** The engine of the file system: `LUSTRE`, `WINDOWS`, `ONTAP`, or `OPENZFS`. */ |
| 89 | fileSystemType: fsx.FileSystemType; |
| 90 | /** The DNS name clients mount, e.g. `fs-....fsx.us-west-2.amazonaws.com`. */ |
| 91 | dnsName: string | undefined; |
| 92 | /** The VPC the file system's network interfaces live in. */ |
| 93 | vpcId: string | undefined; |
| 94 | }, |
| 95 | {}, |
| 96 | Providers |
| 97 | > {} |
| 98 | |
| 99 | /** |
| 100 | * An Amazon FSx file system — fully-managed shared storage for Lustre, |
| 101 | * Windows File Server, NetApp ONTAP, or OpenZFS. |
| 102 | * |
| 103 | * FSx file systems take several minutes to provision. The file system id, |
| 104 | * ARN, and DNS name are returned as soon as `CreateFileSystem` accepts the |
| 105 | * request; the file system then transitions `CREATING` → `AVAILABLE` |
| 106 | * asynchronously. Create is idempotent on a deterministic client request |
| 107 | * token derived from the app, stage, and logical id; if state is lost, the |
| 108 | * file system is re-discovered by its internal Alchemy tags. |
| 109 | * |
| 110 | * ### Creating File Systems |
| 111 | * **Example:** Lustre scratch file system (cheapest / fastest) |
| 112 | * ```typescript |
| 113 | * import * as AWS from "@alchemy.run/aws"; |
| 114 | * |
| 115 | * const scratch = yield* AWS.FSx.FileSystem("Scratch", { |
| 116 | * fileSystemType: "LUSTRE", |
| 117 | * storageCapacity: 1200, |
| 118 | * subnetIds: [subnetId], |
| 119 | * lustreConfiguration: { DeploymentType: "SCRATCH_2" }, |
| 120 | * }); |
| 121 | * ``` |
| 122 | * |
| 123 | * **Example:** Persistent Lustre with S3 data repository |
| 124 | * ```typescript |
| 125 | * const files = yield* AWS.FSx.FileSystem("Files", { |
| 126 | * fileSystemType: "LUSTRE", |
| 127 | * storageCapacity: 1200, |
| 128 | * subnetIds: [subnetId], |
| 129 | * lustreConfiguration: { |
| 130 | * DeploymentType: "PERSISTENT_2", |
| 131 | * PerUnitStorageThroughput: 125, |
| 132 | * DataCompressionType: "LZ4", |
| 133 | * }, |
| 134 | * }); |
| 135 | * ``` |
| 136 | * |
| 137 | * **Example:** OpenZFS file system |
| 138 | * ```typescript |
| 139 | * const zfs = yield* AWS.FSx.FileSystem("Zfs", { |
| 140 | * fileSystemType: "OPENZFS", |
| 141 | * storageCapacity: 64, |
| 142 | * subnetIds: [subnetId], |
| 143 | * openZFSConfiguration: { |
| 144 | * DeploymentType: "SINGLE_AZ_1", |
| 145 | * ThroughputCapacity: 64, |
| 146 | * }, |
| 147 | * }); |
| 148 | * ``` |
| 149 | * |
| 150 | * @resource |
| 151 | */ |
| 152 | export const FileSystem = Resource<FileSystem>("AWS.FSx.FileSystem"); |
| 153 | |
| 154 | const fsxTagsToRecord = (tags: readonly fsx.Tag[] | undefined): Record<string, string> => |
| 155 | Object.fromEntries((tags ?? []).map((t) => [t.Key, t.Value])); |
| 156 | |
| 157 | const deleteConfigFor = (fs: fsx.FileSystem): Partial<fsx.DeleteFileSystemRequest> => { |
| 158 | switch (fs.FileSystemType) { |
| 159 | case "LUSTRE": |
| 160 | // Scratch deployments have no backups at all — FSx rejects ANY |
| 161 | // LustreConfiguration on delete ("Final backup during delete is not |
| 162 | // supported on file systems using the Scratch deployment type"), even |
| 163 | // SkipFinalBackup: true. |
| 164 | return fs.LustreConfiguration?.DeploymentType?.startsWith("SCRATCH") |
| 165 | ? {} |
| 166 | : { LustreConfiguration: { SkipFinalBackup: true } }; |
| 167 | case "WINDOWS": |
| 168 | return { WindowsConfiguration: { SkipFinalBackup: true } }; |
| 169 | case "OPENZFS": |
| 170 | return { |
| 171 | OpenZFSConfiguration: { |
| 172 | SkipFinalBackup: true, |
| 173 | Options: ["DELETE_CHILD_VOLUMES_AND_SNAPSHOTS"], |
| 174 | }, |
| 175 | }; |
| 176 | default: |
| 177 | // ONTAP has no file-system-level skip-final-backup option; volumes and |
| 178 | // SVMs must be torn down first. |
| 179 | return {}; |
| 180 | } |
| 181 | }; |
| 182 | |
| 183 | export const FileSystemProvider = () => |
| 184 | Provider.effect( |
| 185 | FileSystem, |
| 186 | Effect.gen(function* () { |
| 187 | const createToken = Effect.fn(function* (id: string) { |
| 188 | return yield* createPhysicalName({ id, maxLength: 63 }); |
| 189 | }); |
| 190 | |
| 191 | const fileSystemArnOf = Effect.fn(function* (fileSystemId: string) { |
| 192 | const { accountId, region } = yield* AWSEnvironment.current; |
| 193 | return `arn:aws:fsx:${region}:${accountId}:file-system/${fileSystemId}`; |
| 194 | }); |
| 195 | |
| 196 | const findById = Effect.fn(function* (fileSystemId: string) { |
| 197 | return yield* fsx.describeFileSystems({ FileSystemIds: [fileSystemId] }).pipe( |
| 198 | Effect.map((r) => r.FileSystems?.[0]), |
| 199 | Effect.catchTag("FileSystemNotFound", () => Effect.undefined), |
| 200 | ); |
| 201 | }); |
| 202 | |
| 203 | const listAll = fsx.describeFileSystems.pages({}).pipe( |
| 204 | Stream.runCollect, |
| 205 | Effect.map((chunk) => Array.from(chunk).flatMap((page) => page.FileSystems ?? [])), |
| 206 | ); |
| 207 | |
| 208 | // FSx has no creation-token lookup, so recover lost state by scanning |
| 209 | // every file system and matching our internal Alchemy tags. |
| 210 | const findByTags = Effect.fn(function* (id: string) { |
| 211 | const internalTags = yield* createInternalTags(id); |
| 212 | const all = yield* listAll; |
| 213 | return all.find((fs) => { |
| 214 | const tags = fsxTagsToRecord(fs.Tags); |
| 215 | return Object.entries(internalTags).every(([k, v]) => tags[k] === v); |
| 216 | }); |
| 217 | }); |
| 218 | |
| 219 | return FileSystem.Provider.of({ |
| 220 | stables: ["fileSystemId", "fileSystemArn", "fileSystemType"], |
| 221 | |
| 222 | list: () => |
| 223 | Effect.gen(function* () { |
| 224 | const systems = yield* listAll; |
| 225 | return yield* Effect.forEach(systems, (fs) => |
| 226 | Effect.gen(function* () { |
| 227 | return { |
| 228 | fileSystemId: fs.FileSystemId!, |
| 229 | fileSystemArn: fs.ResourceARN ?? (yield* fileSystemArnOf(fs.FileSystemId!)), |
| 230 | fileSystemType: fs.FileSystemType ?? "LUSTRE", |
| 231 | dnsName: fs.DNSName, |
| 232 | vpcId: fs.VpcId, |
| 233 | }; |
| 234 | }), |
| 235 | ); |
| 236 | }), |
| 237 | |
| 238 | read: Effect.fn(function* ({ id, output }) { |
| 239 | const fs = output?.fileSystemId |
| 240 | ? yield* findById(output.fileSystemId) |
| 241 | : yield* findByTags(id); |
| 242 | if (fs === undefined || fs.Lifecycle === "DELETING") { |
| 243 | return undefined; |
| 244 | } |
| 245 | const attrs = { |
| 246 | fileSystemId: fs.FileSystemId!, |
| 247 | fileSystemArn: fs.ResourceARN ?? (yield* fileSystemArnOf(fs.FileSystemId!)), |
| 248 | fileSystemType: fs.FileSystemType ?? "LUSTRE", |
| 249 | dnsName: fs.DNSName, |
| 250 | vpcId: fs.VpcId, |
| 251 | }; |
| 252 | return (yield* hasAlchemyTags(id, fsxTagsToRecord(fs.Tags))) ? attrs : Unowned(attrs); |
| 253 | }), |
| 254 | |
| 255 | diff: Effect.fn(function* ({ news, olds }) { |
| 256 | if (!isResolved(news)) return undefined; |
| 257 | const replaced = |
| 258 | news.fileSystemType !== olds.fileSystemType || |
| 259 | news.kmsKeyId !== olds.kmsKeyId || |
| 260 | (news.storageType ?? "SSD") !== (olds.storageType ?? "SSD") || |
| 261 | news.fileSystemTypeVersion !== olds.fileSystemTypeVersion || |
| 262 | JSON.stringify(news.subnetIds) !== JSON.stringify(olds.subnetIds) || |
| 263 | // storage capacity can only be increased in place |
| 264 | (news.storageCapacity !== undefined && |
| 265 | olds.storageCapacity !== undefined && |
| 266 | news.storageCapacity < olds.storageCapacity); |
| 267 | if (replaced) { |
| 268 | return { action: "replace" } as const; |
| 269 | } |
| 270 | // undefined → default update path (storage-capacity increase, tags) |
| 271 | }), |
| 272 | |
| 273 | reconcile: Effect.fn(function* ({ id, news, output, session }) { |
| 274 | const internalTags = yield* createInternalTags(id); |
| 275 | |
| 276 | // 1. OBSERVE — cloud state is authoritative; output is only a cache. |
| 277 | let fs = output?.fileSystemId |
| 278 | ? yield* findById(output.fileSystemId) |
| 279 | : yield* findByTags(id); |
| 280 | if (fs?.Lifecycle === "DELETING") { |
| 281 | fs = undefined; |
| 282 | } |
| 283 | |
| 284 | // 2. ENSURE — create if missing. CreateFileSystem is idempotent on |
| 285 | // the deterministic client request token. |
| 286 | if (fs === undefined) { |
| 287 | const token = yield* createToken(id); |
| 288 | fs = yield* fsx |
| 289 | .createFileSystem({ |
| 290 | ClientRequestToken: token, |
| 291 | FileSystemType: news.fileSystemType, |
| 292 | StorageCapacity: news.storageCapacity, |
| 293 | StorageType: news.storageType, |
| 294 | SubnetIds: news.subnetIds, |
| 295 | SecurityGroupIds: news.securityGroupIds, |
| 296 | KmsKeyId: news.kmsKeyId, |
| 297 | FileSystemTypeVersion: news.fileSystemTypeVersion, |
| 298 | LustreConfiguration: news.lustreConfiguration, |
| 299 | WindowsConfiguration: news.windowsConfiguration, |
| 300 | OntapConfiguration: news.ontapConfiguration, |
| 301 | OpenZFSConfiguration: news.openZFSConfiguration, |
| 302 | Tags: Object.entries({ ...news.tags, ...internalTags }).map(([Key, Value]) => ({ |
| 303 | Key, |
| 304 | Value, |
| 305 | })), |
| 306 | }) |
| 307 | .pipe(Effect.map((r) => r.FileSystem!)); |
| 308 | } |
| 309 | |
| 310 | const fileSystemId = fs.FileSystemId!; |
| 311 | |
| 312 | // 3a. SYNC storage capacity — only an increase is valid, and only |
| 313 | // while the file system is settled. Skip during CREATING. |
| 314 | if ( |
| 315 | fs.Lifecycle === "AVAILABLE" && |
| 316 | news.storageCapacity !== undefined && |
| 317 | fs.StorageCapacity !== undefined && |
| 318 | news.storageCapacity > fs.StorageCapacity |
| 319 | ) { |
| 320 | yield* fsx.updateFileSystem({ |
| 321 | FileSystemId: fileSystemId, |
| 322 | StorageCapacity: news.storageCapacity, |
| 323 | }); |
| 324 | } |
| 325 | |
| 326 | // 3b. SYNC tags — diff against OBSERVED cloud tags so adoption |
| 327 | // converges. |
| 328 | const observedTags = fsxTagsToRecord(fs.Tags); |
| 329 | const { upsert, removed } = diffTags(observedTags, { |
| 330 | ...news.tags, |
| 331 | ...internalTags, |
| 332 | }); |
| 333 | if (upsert.length > 0) { |
| 334 | yield* fsx.tagResource({ |
| 335 | ResourceARN: fs.ResourceARN, |
| 336 | Tags: upsert, |
| 337 | }); |
| 338 | } |
| 339 | if (removed.length > 0) { |
| 340 | yield* fsx.untagResource({ |
| 341 | ResourceARN: fs.ResourceARN, |
| 342 | TagKeys: removed, |
| 343 | }); |
| 344 | } |
| 345 | |
| 346 | yield* session.note(fileSystemId); |
| 347 | return { |
| 348 | fileSystemId, |
| 349 | fileSystemArn: fs.ResourceARN ?? (yield* fileSystemArnOf(fileSystemId)), |
| 350 | fileSystemType: fs.FileSystemType ?? news.fileSystemType, |
| 351 | dnsName: fs.DNSName, |
| 352 | vpcId: fs.VpcId, |
| 353 | }; |
| 354 | }), |
| 355 | |
| 356 | delete: Effect.fn(function* ({ output }) { |
| 357 | // Observe first: the delete options depend on the deployment type. |
| 358 | const fs = yield* findById(output.fileSystemId); |
| 359 | if (fs === undefined || fs.Lifecycle === "DELETING") return; |
| 360 | yield* fsx |
| 361 | .deleteFileSystem({ |
| 362 | FileSystemId: output.fileSystemId, |
| 363 | ...deleteConfigFor(fs), |
| 364 | }) |
| 365 | .pipe(Effect.catchTag("FileSystemNotFound", () => Effect.void)); |
| 366 | }), |
| 367 | }); |
| 368 | }), |
| 369 | ); |