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

main
ts 360 lines 12.8 KB
Raw
1 import * as dms from "@distilled.cloud/aws/database-migration-service";
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 /**
8 * Out-of-band orphan reaper for the DMS test suites.
9 *
10 * `test.provider` scratch stacks keep their state IN MEMORY, so a run that is
11 * hard-killed (vitest SIGKILL, outer `timeout`, OOM) between deploy and the
12 * `Effect.ensuring(scratch.destroy())` finalizer orphans the whole fixture —
13 * VPC, subnets, DMS subnet group, and (for the SLOW-gated test) a replication
14 * instance — with no state left to destroy from. A post-wave account nuke
15 * found exactly this: a leaked DMS test VPC stack.
16 *
17 * The reaper finds the fixtures by their DETERMINISTIC VPC CIDRs
18 * (`10.91.0.0/16` in ReplicationSubnetGroup.test.ts, `10.92.0.0/16` in
19 * ReplicationInstance.test.ts — unique across the test suite) and deletes
20 * everything inside dependency-first with typed, idempotent calls:
21 * replication instances → subnet groups → ENIs → IGWs → route tables →
22 * security groups → subnets → VPC.
23 *
24 * It runs as a delete-if-exists pre-clean at the top of each test body and as
25 * an `Effect.ensuring` finalizer around it, so any run converges the account
26 * to zero leftovers regardless of how the previous one ended. A clean account
27 * costs one `describeVpcs` call.
28 *
29 * NOTE: the account-wide `dms-vpc-role` IAM role is owned and cleaned by the
30 * ReplicationSubnetGroup provider when Alchemy created it and the last subnet
31 * group is gone. The network reaper does not delete foreign/pre-existing IAM
32 * roles with that AWS-mandated name.
33 */
34
35 /**
36 * A DMS test VPC, identified by its deterministic CIDR and its Alchemy logical
37 * ID. The reaper matches both: other suites reuse these CIDRs (the ElastiCache
38 * provisioned network is also 10.92.0.0/16), and the DMS files run
39 * concurrently, so a CIDR-only match deletes another test's live VPC.
40 */
41 export interface DmsTestVpc {
42 readonly cidr: string;
43 readonly logicalId: string;
44 }
45
46 export const DMS_SUBNET_GROUP_VPC: DmsTestVpc = {
47 cidr: "10.91.0.0/16",
48 logicalId: "DmsVpc",
49 };
50 export const DMS_REPLICATION_INSTANCE_VPC: DmsTestVpc = {
51 cidr: "10.92.0.0/16",
52 logicalId: "DmsInstVpc",
53 };
54
55 class OrphanStillPresent extends Data.TaggedError("OrphanStillPresent")<{
56 readonly kind: string;
57 }> {}
58
59 /** ~10 minute ceiling for slow asynchronous releases (DMS instance deletes, ENIs). */
60 const slowRelease = Schedule.max([Schedule.spaced("15 seconds"), Schedule.recurs(40)]);
61 /** ~2 minute ceiling for ordinary dependency-violation races. */
62 const dependencyRelease = Schedule.max([Schedule.spaced("10 seconds"), Schedule.recurs(12)]);
63
64 const findTestVpcIds = (vpc: DmsTestVpc) =>
65 ec2
66 .describeVpcs({
67 Filters: [
68 { Name: "cidr", Values: [vpc.cidr] },
69 { Name: "tag:alchemy::id", Values: [vpc.logicalId] },
70 ],
71 })
72 .pipe(
73 Effect.map((response) =>
74 (response.Vpcs ?? []).flatMap((vpc) => (vpc.VpcId ? [vpc.VpcId] : [])),
75 ),
76 );
77
78 /** Count replication instances still placed in the given VPCs. */
79 const countInstancesIn = (vpcIds: ReadonlySet<string>) =>
80 dms.describeReplicationInstances({}).pipe(
81 Effect.catchTag("ResourceNotFoundFault", () => Effect.succeed({ ReplicationInstances: [] })),
82 Effect.map(
83 (response) =>
84 (response.ReplicationInstances ?? []).filter(
85 (instance): boolean =>
86 !!instance.ReplicationSubnetGroup?.VpcId &&
87 vpcIds.has(instance.ReplicationSubnetGroup.VpcId),
88 ).length,
89 ),
90 );
91
92 /**
93 * Delete any replication instance placed in a test VPC and wait until it is
94 * fully gone — its ENIs block subnet/VPC deletion until then.
95 */
96 const reapReplicationInstances = Effect.fn(function* (vpcIds: ReadonlySet<string>) {
97 const response = yield* dms
98 .describeReplicationInstances({})
99 .pipe(
100 Effect.catchTag("ResourceNotFoundFault", () => Effect.succeed({ ReplicationInstances: [] })),
101 );
102 const arns = (response.ReplicationInstances ?? [])
103 .filter(
104 (instance): boolean =>
105 !!instance.ReplicationSubnetGroup?.VpcId &&
106 vpcIds.has(instance.ReplicationSubnetGroup.VpcId),
107 )
108 .flatMap((instance) =>
109 instance.ReplicationInstanceArn ? [instance.ReplicationInstanceArn] : [],
110 );
111 yield* Effect.forEach(
112 arns,
113 (ReplicationInstanceArn) =>
114 Effect.logInfo(`DMS reap: deleting replication instance ${ReplicationInstanceArn}`).pipe(
115 Effect.andThen(dms.deleteReplicationInstance({ ReplicationInstanceArn })),
116 // InvalidResourceStateFault: already deleting — the wait below
117 // confirms it actually goes away.
118 Effect.catchTag(["ResourceNotFoundFault", "InvalidResourceStateFault"], () => Effect.void),
119 ),
120 { discard: true },
121 );
122 if (arns.length > 0) {
123 yield* countInstancesIn(vpcIds).pipe(
124 Effect.repeat({ schedule: slowRelease, until: (n) => n === 0 }),
125 Effect.flatMap((n) =>
126 n === 0
127 ? Effect.void
128 : Effect.fail(new OrphanStillPresent({ kind: "DMS replication instances" })),
129 ),
130 );
131 }
132 });
133
134 /** Delete any replication subnet group whose subnets live in a test VPC. */
135 const reapSubnetGroups = Effect.fn(function* (vpcIds: ReadonlySet<string>) {
136 const response = yield* dms
137 .describeReplicationSubnetGroups({})
138 .pipe(
139 Effect.catchTag("ResourceNotFoundFault", () =>
140 Effect.succeed({ ReplicationSubnetGroups: [] }),
141 ),
142 );
143 const identifiers = (response.ReplicationSubnetGroups ?? [])
144 .filter((group): boolean => !!group.VpcId && vpcIds.has(group.VpcId))
145 .flatMap((group) =>
146 group.ReplicationSubnetGroupIdentifier ? [group.ReplicationSubnetGroupIdentifier] : [],
147 );
148 yield* Effect.forEach(
149 identifiers,
150 (ReplicationSubnetGroupIdentifier) =>
151 Effect.logInfo(`DMS reap: deleting subnet group ${ReplicationSubnetGroupIdentifier}`).pipe(
152 Effect.andThen(
153 dms.deleteReplicationSubnetGroup({
154 ReplicationSubnetGroupIdentifier,
155 }),
156 ),
157 // InvalidResourceStateFault: an instance is still detaching from the
158 // group (instance reap above waits, but deletion lag can linger).
159 Effect.retry({
160 while: (e): boolean => e._tag === "InvalidResourceStateFault",
161 schedule: dependencyRelease,
162 }),
163 Effect.catchTag("ResourceNotFoundFault", () => Effect.void),
164 ),
165 { discard: true },
166 );
167 });
168
169 /**
170 * ENIs (e.g. from a deleted replication instance) release asynchronously and
171 * block subnet/VPC deletion. Each pass deletes the ENIs that have reached
172 * `available`, then re-counts.
173 */
174 const sweepNetworkInterfaces = Effect.fn(function* (vpcId: string) {
175 const describe = ec2
176 .describeNetworkInterfaces({
177 Filters: [{ Name: "vpc-id", Values: [vpcId] }],
178 })
179 .pipe(Effect.map((response) => response.NetworkInterfaces ?? []));
180 yield* Effect.gen(function* () {
181 const enis = yield* describe;
182 yield* Effect.forEach(
183 enis.filter((eni): boolean => eni.Status === "available" && !!eni.NetworkInterfaceId),
184 (eni) =>
185 Effect.logInfo(`DMS reap: deleting ENI ${eni.NetworkInterfaceId}`).pipe(
186 Effect.andThen(
187 ec2.deleteNetworkInterface({
188 NetworkInterfaceId: eni.NetworkInterfaceId,
189 }),
190 ),
191 Effect.catchTag(
192 ["InvalidNetworkInterfaceID.NotFound", "InvalidNetworkInterface.InUse"],
193 () => Effect.void,
194 ),
195 ),
196 { discard: true },
197 );
198 return (yield* describe).length;
199 }).pipe(
200 Effect.repeat({ schedule: slowRelease, until: (n) => n === 0 }),
201 Effect.flatMap((n) =>
202 n === 0
203 ? Effect.void
204 : Effect.fail(new OrphanStillPresent({ kind: `network interfaces in ${vpcId}` })),
205 ),
206 );
207 });
208
209 const reapVpc = Effect.fn(function* (vpcId: string) {
210 yield* Effect.logInfo(`DMS reap: tearing down VPC ${vpcId}`);
211 yield* sweepNetworkInterfaces(vpcId);
212
213 // Internet gateways: detach, then delete.
214 const igws = yield* ec2
215 .describeInternetGateways({
216 Filters: [{ Name: "attachment.vpc-id", Values: [vpcId] }],
217 })
218 .pipe(
219 Effect.map((response) =>
220 (response.InternetGateways ?? []).flatMap((igw) =>
221 igw.InternetGatewayId ? [igw.InternetGatewayId] : [],
222 ),
223 ),
224 );
225 yield* Effect.forEach(
226 igws,
227 (InternetGatewayId) =>
228 Effect.logInfo(`DMS reap: detaching + deleting IGW ${InternetGatewayId}`).pipe(
229 Effect.andThen(
230 ec2
231 .detachInternetGateway({ InternetGatewayId, VpcId: vpcId })
232 .pipe(
233 Effect.catchTag(
234 ["Gateway.NotAttached", "InvalidInternetGatewayID.NotFound"],
235 () => Effect.void,
236 ),
237 ),
238 ),
239 Effect.andThen(ec2.deleteInternetGateway({ InternetGatewayId })),
240 Effect.retry({
241 while: (e): boolean => e._tag === "DependencyViolation",
242 schedule: dependencyRelease,
243 }),
244 Effect.catchTag("InvalidInternetGatewayID.NotFound", () => Effect.void),
245 ),
246 { discard: true },
247 );
248
249 // Non-main route tables: disassociate subnet associations, then delete.
250 const routeTables = yield* ec2
251 .describeRouteTables({
252 Filters: [{ Name: "vpc-id", Values: [vpcId] }],
253 })
254 .pipe(Effect.map((response) => response.RouteTables ?? []));
255 yield* Effect.forEach(
256 routeTables,
257 (table) =>
258 Effect.gen(function* () {
259 const associations = table.Associations ?? [];
260 if (associations.some((a): boolean => a.Main === true)) return;
261 yield* Effect.forEach(
262 associations.flatMap((a) =>
263 a.RouteTableAssociationId ? [a.RouteTableAssociationId] : [],
264 ),
265 (AssociationId) =>
266 ec2
267 .disassociateRouteTable({ AssociationId })
268 .pipe(Effect.catchTag("InvalidAssociationID.NotFound", () => Effect.void)),
269 { discard: true },
270 );
271 if (table.RouteTableId) {
272 yield* Effect.logInfo(`DMS reap: deleting route table ${table.RouteTableId}`).pipe(
273 Effect.andThen(ec2.deleteRouteTable({ RouteTableId: table.RouteTableId })),
274 Effect.retry({
275 while: (e): boolean => e._tag === "DependencyViolation",
276 schedule: dependencyRelease,
277 }),
278 Effect.catchTag("InvalidRouteTableID.NotFound", () => Effect.void),
279 );
280 }
281 }),
282 { discard: true },
283 );
284
285 // Non-default security groups.
286 const groups = yield* ec2
287 .describeSecurityGroups({
288 Filters: [{ Name: "vpc-id", Values: [vpcId] }],
289 })
290 .pipe(
291 Effect.map((response) =>
292 (response.SecurityGroups ?? []).filter(
293 (group): boolean => group.GroupName !== "default" && !!group.GroupId,
294 ),
295 ),
296 );
297 yield* Effect.forEach(
298 groups,
299 (group) =>
300 Effect.logInfo(
301 `DMS reap: deleting security group ${group.GroupId} (${group.GroupName})`,
302 ).pipe(
303 Effect.andThen(ec2.deleteSecurityGroup({ GroupId: group.GroupId })),
304 Effect.retry({
305 while: (e): boolean => e._tag === "DependencyViolation",
306 schedule: dependencyRelease,
307 }),
308 Effect.catchTag("InvalidGroup.NotFound", () => Effect.void),
309 ),
310 { discard: true },
311 );
312
313 // Subnets.
314 const subnets = yield* ec2
315 .describeSubnets({ Filters: [{ Name: "vpc-id", Values: [vpcId] }] })
316 .pipe(
317 Effect.map((response) =>
318 (response.Subnets ?? []).flatMap((subnet) => (subnet.SubnetId ? [subnet.SubnetId] : [])),
319 ),
320 );
321 yield* Effect.forEach(
322 subnets,
323 (SubnetId) =>
324 Effect.logInfo(`DMS reap: deleting subnet ${SubnetId}`).pipe(
325 Effect.andThen(ec2.deleteSubnet({ SubnetId })),
326 Effect.retry({
327 while: (e): boolean => e._tag === "DependencyViolation",
328 schedule: dependencyRelease,
329 }),
330 Effect.catchTag("InvalidSubnetID.NotFound", () => Effect.void),
331 ),
332 { discard: true },
333 );
334
335 yield* ec2.deleteVpc({ VpcId: vpcId }).pipe(
336 Effect.retry({
337 while: (e): boolean => e._tag === "DependencyViolation",
338 schedule: dependencyRelease,
339 }),
340 Effect.catchTag("InvalidVpcID.NotFound", () => Effect.void),
341 );
342 });
343
344 /**
345 * Delete every leftover DMS fixture resource in the given test VPC, in
346 * dependency order.
347 * Idempotent: a run against a clean account is a single `describeVpcs`.
348 *
349 * Requires the AWS provider environment (available inside `test.provider`
350 * bodies; wrap with `Core.withProviders` elsewhere).
351 */
352 export const reapDmsOrphans = (vpc: DmsTestVpc) =>
353 Effect.gen(function* () {
354 const vpcIdList = yield* findTestVpcIds(vpc);
355 if (vpcIdList.length === 0) return;
356 const vpcIds: ReadonlySet<string> = new Set(vpcIdList);
357 yield* reapReplicationInstances(vpcIds);
358 yield* reapSubnetGroups(vpcIds);
359 yield* Effect.forEach(vpcIdList, reapVpc, { discard: true });
360 });