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, 06:00 UTC
| 1 | import { VpcId } from "@alchemy.run/aws/EC2"; |
| 2 | import * as EC2 from "@distilled.cloud/aws/ec2"; |
| 3 | import * as Data from "effect/Data"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import * as Schedule from "effect/Schedule"; |
| 6 | |
| 7 | class DefaultVpcNotVisible extends Data.TaggedError("DefaultVpcNotVisible")<{}> {} |
| 8 | |
| 9 | class DefaultVpcNetworkNotVisible extends Data.TaggedError("DefaultVpcNetworkNotVisible")<{}> {} |
| 10 | |
| 11 | class UnsupportedDefaultVpcCidr extends Data.TaggedError("UnsupportedDefaultVpcCidr")<{ |
| 12 | readonly cidrBlock: string; |
| 13 | }> {} |
| 14 | |
| 15 | export const getDefaultVpc = Effect.gen(function* () { |
| 16 | const vpcs = yield* EC2.describeVpcs({}); |
| 17 | const vpc = (vpcs.Vpcs ?? []).find((v) => v.IsDefault); |
| 18 | if (!vpc?.VpcId || !vpc.CidrBlock) { |
| 19 | // The default VPC can be deleted out of band (e.g. by the account nuke |
| 20 | // script). Recreate it, then fail with the retryable marker so the retry |
| 21 | // loop below re-describes until it becomes visible. Concurrent test files |
| 22 | // racing on the same recreate surface DefaultVpcAlreadyExists — that just |
| 23 | // means someone else won the race, so fall through to the retry. |
| 24 | yield* EC2.createDefaultVpc({}).pipe( |
| 25 | Effect.catchTag("DefaultVpcAlreadyExists", () => Effect.void), |
| 26 | ); |
| 27 | return yield* Effect.fail(new DefaultVpcNotVisible()); |
| 28 | } |
| 29 | |
| 30 | const [baseAddress, prefixString] = vpc.CidrBlock.split("/"); |
| 31 | if (prefixString !== "16") { |
| 32 | return yield* Effect.fail(new UnsupportedDefaultVpcCidr({ cidrBlock: vpc.CidrBlock })); |
| 33 | } |
| 34 | |
| 35 | const [a, b] = baseAddress.split("."); |
| 36 | return { |
| 37 | vpcId: VpcId(vpc.VpcId), |
| 38 | cidrBlock: vpc.CidrBlock, |
| 39 | subnetCidrBlock: (thirdOctet: number) => `${a}.${b}.${thirdOctet}.0/24`, |
| 40 | }; |
| 41 | }).pipe( |
| 42 | Effect.retry({ |
| 43 | while: (e) => e._tag === "DefaultVpcNotVisible", |
| 44 | schedule: Schedule.max([Schedule.spaced("3 seconds"), Schedule.recurs(10)]), |
| 45 | }), |
| 46 | ); |
| 47 | |
| 48 | /** Wait for the default VPC's generated subnets and security group as well. */ |
| 49 | export const getDefaultVpcNetwork = Effect.gen(function* () { |
| 50 | const vpc = yield* getDefaultVpc; |
| 51 | const [subnets, groups] = yield* Effect.all( |
| 52 | [ |
| 53 | // Only the default-for-AZ subnets: other tests add subnets to the |
| 54 | // default VPC concurrently, and those can share one Availability Zone. |
| 55 | EC2.describeSubnets({ |
| 56 | Filters: [ |
| 57 | { Name: "vpc-id", Values: [vpc.vpcId] }, |
| 58 | { Name: "default-for-az", Values: ["true"] }, |
| 59 | ], |
| 60 | } as any), |
| 61 | EC2.describeSecurityGroups({ |
| 62 | Filters: [ |
| 63 | { Name: "vpc-id", Values: [vpc.vpcId] }, |
| 64 | { Name: "group-name", Values: ["default"] }, |
| 65 | ], |
| 66 | } as any), |
| 67 | ], |
| 68 | { concurrency: 2 }, |
| 69 | ); |
| 70 | const subnetIds = (subnets.Subnets ?? []) |
| 71 | .flatMap((subnet) => (subnet.SubnetId ? [subnet.SubnetId] : [])) |
| 72 | .sort(); |
| 73 | const defaultSecurityGroupId = groups.SecurityGroups?.[0]?.GroupId; |
| 74 | if (subnetIds.length === 0 || !defaultSecurityGroupId) { |
| 75 | return yield* Effect.fail(new DefaultVpcNetworkNotVisible()); |
| 76 | } |
| 77 | return { ...vpc, subnetIds, defaultSecurityGroupId }; |
| 78 | }).pipe( |
| 79 | Effect.retry({ |
| 80 | while: (error) => error._tag === "DefaultVpcNetworkNotVisible", |
| 81 | schedule: Schedule.max([Schedule.fixed("1 second"), Schedule.recurs(30)]), |
| 82 | }), |
| 83 | ); |