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 type { Volume as FlyVolume } from "@distilled.cloud/fly-io/machines"; |
| 2 | import * as machines from "@distilled.cloud/fly-io/machines"; |
| 3 | import * as Data from "effect/Data"; |
| 4 | import * as Duration from "effect/Duration"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import * as Schedule from "effect/Schedule"; |
| 7 | import { listOwnedApps } from "./App.ts"; |
| 8 | import { |
| 9 | createFlyVolumeName, |
| 10 | matchesAlchemyPhysicalName, |
| 11 | sanitizeFlyVolumeName, |
| 12 | } from "./Metadata.ts"; |
| 13 | import type { DiskSpec } from "./MountVolume.ts"; |
| 14 | |
| 15 | export const DEFAULT_VOLUME_REGION = "iad"; |
| 16 | export const MIN_SIZE_GB = 1; |
| 17 | |
| 18 | export class VolumeNotCreated extends Data.TaggedError("Fly.VolumeNotCreated")<{ |
| 19 | resourceName: string; |
| 20 | appName: string; |
| 21 | }> {} |
| 22 | |
| 23 | class VolumePending extends Data.TaggedError("Fly.VolumePending")<{ |
| 24 | volumeId: string; |
| 25 | state: string; |
| 26 | }> {} |
| 27 | |
| 28 | const backoff = Schedule.min([ |
| 29 | Schedule.exponential(Duration.millis(500), 1.5), |
| 30 | Schedule.spaced(Duration.seconds(5)), |
| 31 | ]); |
| 32 | |
| 33 | export const destroying = (state: string | undefined) => |
| 34 | state === "destroyed" || state === "pending_destroy" || state === "scheduled_for_destruction"; |
| 35 | |
| 36 | const transientState = (state: string | undefined) => |
| 37 | state === "creating" || state === "pending" || state === "extending"; |
| 38 | |
| 39 | export const getVolumeById = (appName: string, volumeId: string) => |
| 40 | machines.getVolumeById({ app_name: appName, volume_id: volumeId }).pipe( |
| 41 | Effect.map((volume) => (destroying(volume.state) ? undefined : volume)), |
| 42 | Effect.catchTag("NotFound", () => Effect.undefined), |
| 43 | ); |
| 44 | |
| 45 | export const listVolumesByApp = (appName: string) => |
| 46 | machines.listVolumes({ app_name: appName }).pipe( |
| 47 | Effect.map((volumes) => volumes.filter((volume) => !destroying(volume.state))), |
| 48 | Effect.catchTag(["NotFound", "Forbidden"], () => Effect.succeed([])), |
| 49 | ); |
| 50 | |
| 51 | const newestFirst = (left: FlyVolume, right: FlyVolume) => { |
| 52 | const byCreated = Date.parse(left.created_at ?? "") - Date.parse(right.created_at ?? ""); |
| 53 | if (byCreated !== 0) return byCreated; |
| 54 | return (left.id ?? "").localeCompare(right.id ?? ""); |
| 55 | }; |
| 56 | |
| 57 | export const listVolumeGroup = (appName: string, name: string, region: string) => |
| 58 | listVolumesByApp(appName).pipe( |
| 59 | Effect.map((volumes) => |
| 60 | volumes |
| 61 | .filter((volume) => volume.name === name && volume.region === region) |
| 62 | .sort(newestFirst), |
| 63 | ), |
| 64 | ); |
| 65 | |
| 66 | export const waitUntilVolumeReady = (appName: string, volumeId: string) => |
| 67 | getVolumeById(appName, volumeId).pipe( |
| 68 | Effect.flatMap((volume) => { |
| 69 | if (volume === undefined) return Effect.undefined; |
| 70 | if (transientState(volume.state)) { |
| 71 | return Effect.fail( |
| 72 | new VolumePending({ |
| 73 | volumeId, |
| 74 | state: volume.state ?? "creating", |
| 75 | }), |
| 76 | ); |
| 77 | } |
| 78 | return Effect.succeed(volume); |
| 79 | }), |
| 80 | Effect.retry({ |
| 81 | while: (e) => e._tag === "Fly.VolumePending", |
| 82 | times: 8, |
| 83 | schedule: backoff, |
| 84 | }), |
| 85 | Effect.catchTag("Fly.VolumePending", () => getVolumeById(appName, volumeId)), |
| 86 | ); |
| 87 | |
| 88 | export const waitUntilVolumeGone = (appName: string, volumeId: string) => |
| 89 | getVolumeById(appName, volumeId).pipe( |
| 90 | Effect.map((volume) => volume === undefined), |
| 91 | Effect.repeat({ |
| 92 | schedule: Schedule.spaced("2 seconds"), |
| 93 | until: (gone) => gone, |
| 94 | times: 10, |
| 95 | }), |
| 96 | ); |
| 97 | |
| 98 | export const pathKey = (path: string): string => { |
| 99 | const key = path |
| 100 | .toLowerCase() |
| 101 | .replace(/[^a-z0-9]+/g, "_") |
| 102 | .replace(/^_+|_+$/g, ""); |
| 103 | return key.length === 0 ? "data" : key; |
| 104 | }; |
| 105 | |
| 106 | export const volumeGroupName = (id: string, disk: Pick<DiskSpec, "path" | "name">) => |
| 107 | Effect.gen(function* () { |
| 108 | if (disk.name !== undefined) return sanitizeFlyVolumeName(disk.name); |
| 109 | return yield* createFlyVolumeName(`${id}/${pathKey(disk.path)}`); |
| 110 | }); |
| 111 | |
| 112 | export const createVolume = Effect.fn(function* (input: { |
| 113 | appName: string; |
| 114 | name: string; |
| 115 | region: string; |
| 116 | disk: DiskSpec; |
| 117 | }) { |
| 118 | const sizeGb = Math.max(input.disk.sizeGb, MIN_SIZE_GB); |
| 119 | const created = yield* machines |
| 120 | .createVolume({ |
| 121 | app_name: input.appName, |
| 122 | name: input.name, |
| 123 | region: input.region, |
| 124 | size_gb: sizeGb, |
| 125 | encrypted: input.disk.encrypted, |
| 126 | fstype: input.disk.fstype, |
| 127 | auto_backup_enabled: input.disk.autoBackupEnabled, |
| 128 | snapshot_retention: input.disk.snapshotRetention, |
| 129 | snapshot_id: input.disk.snapshotId, |
| 130 | source_volume_id: input.disk.sourceVolumeId, |
| 131 | require_unique_zone: input.disk.requireUniqueZone, |
| 132 | }) |
| 133 | .pipe(Effect.catchTag("Conflict", () => Effect.undefined)); |
| 134 | if (created?.id !== undefined && created.id.length > 0) { |
| 135 | return (yield* waitUntilVolumeReady(input.appName, created.id)) ?? created; |
| 136 | } |
| 137 | const group = yield* listVolumeGroup(input.appName, input.name, input.region); |
| 138 | const hit = group.at(-1); |
| 139 | if (hit === undefined || hit.id === undefined) { |
| 140 | return yield* new VolumeNotCreated({ |
| 141 | resourceName: input.name, |
| 142 | appName: input.appName, |
| 143 | }); |
| 144 | } |
| 145 | return (yield* waitUntilVolumeReady(input.appName, hit.id)) ?? hit; |
| 146 | }); |
| 147 | |
| 148 | export const syncVolume = Effect.fn(function* (appName: string, volume: FlyVolume, disk: DiskSpec) { |
| 149 | const volumeId = volume.id; |
| 150 | if (volumeId === undefined || volumeId.length === 0) return volume; |
| 151 | let current = volume; |
| 152 | const sizeGb = Math.max(disk.sizeGb, MIN_SIZE_GB); |
| 153 | const observedSize = current.size_gb ?? 0; |
| 154 | if (sizeGb > observedSize) { |
| 155 | const extended = yield* machines.extendVolume({ |
| 156 | app_name: appName, |
| 157 | volume_id: volumeId, |
| 158 | size_gb: sizeGb, |
| 159 | }); |
| 160 | current = extended.volume ?? (yield* waitUntilVolumeReady(appName, volumeId)) ?? current; |
| 161 | } |
| 162 | |
| 163 | const backupChanged = |
| 164 | disk.autoBackupEnabled !== undefined && disk.autoBackupEnabled !== current.auto_backup_enabled; |
| 165 | const retentionChanged = |
| 166 | disk.snapshotRetention !== undefined && disk.snapshotRetention !== current.snapshot_retention; |
| 167 | if (backupChanged || retentionChanged) { |
| 168 | current = yield* machines.updateVolume({ |
| 169 | app_name: appName, |
| 170 | volume_id: volumeId, |
| 171 | auto_backup_enabled: disk.autoBackupEnabled, |
| 172 | snapshot_retention: disk.snapshotRetention, |
| 173 | }); |
| 174 | } |
| 175 | return current; |
| 176 | }); |
| 177 | |
| 178 | /** |
| 179 | * Observe-ensure-sync a Fly volume group (`name` shared, `count` |
| 180 | * independent volumes). Extras are left in place for the caller to |
| 181 | * delete after the Machines that mount them are gone. |
| 182 | */ |
| 183 | export const ensureVolumeGroup = Effect.fn(function* (input: { |
| 184 | appName: string; |
| 185 | name: string; |
| 186 | region: string; |
| 187 | count: number; |
| 188 | disk: DiskSpec; |
| 189 | preferIds?: readonly string[]; |
| 190 | }) { |
| 191 | const prefer = new Set((input.preferIds ?? []).filter((id) => id.length > 0)); |
| 192 | let group = yield* listVolumeGroup(input.appName, input.name, input.region); |
| 193 | const preferred = group.filter((volume) => volume.id !== undefined && prefer.has(volume.id)); |
| 194 | const rest = group.filter((volume) => volume.id === undefined || !prefer.has(volume.id)); |
| 195 | group = [...preferred, ...rest]; |
| 196 | |
| 197 | let attempts = 0; |
| 198 | while (group.length < input.count && attempts < input.count + 3) { |
| 199 | attempts += 1; |
| 200 | const created = yield* createVolume({ |
| 201 | appName: input.appName, |
| 202 | name: input.name, |
| 203 | region: input.region, |
| 204 | disk: input.disk, |
| 205 | }); |
| 206 | if (created.id !== undefined && !group.some((volume) => volume.id === created.id)) { |
| 207 | group.push(created); |
| 208 | } else { |
| 209 | group = yield* listVolumeGroup(input.appName, input.name, input.region); |
| 210 | } |
| 211 | } |
| 212 | if (group.length < input.count) { |
| 213 | return yield* new VolumeNotCreated({ |
| 214 | resourceName: input.name, |
| 215 | appName: input.appName, |
| 216 | }); |
| 217 | } |
| 218 | |
| 219 | const kept = group.slice(0, input.count); |
| 220 | const extras = group.slice(input.count); |
| 221 | const synced: FlyVolume[] = []; |
| 222 | for (const volume of kept) { |
| 223 | synced.push(yield* syncVolume(input.appName, volume, input.disk)); |
| 224 | } |
| 225 | return { volumes: synced, extras }; |
| 226 | }); |
| 227 | |
| 228 | export const deleteVolume = Effect.fn(function* (appName: string, volumeId: string) { |
| 229 | if (appName.length === 0 || volumeId.length === 0) return; |
| 230 | yield* machines |
| 231 | .deleteVolume({ |
| 232 | app_name: appName, |
| 233 | volume_id: volumeId, |
| 234 | }) |
| 235 | .pipe( |
| 236 | Effect.catchTag("NotFound", () => Effect.void), |
| 237 | Effect.retry({ |
| 238 | while: (e) => e._tag === "Conflict", |
| 239 | times: 8, |
| 240 | schedule: backoff, |
| 241 | }), |
| 242 | ); |
| 243 | yield* waitUntilVolumeGone(appName, volumeId); |
| 244 | }); |
| 245 | |
| 246 | export const listOwnedVolumes = Effect.fn(function* () { |
| 247 | const apps = yield* listOwnedApps(); |
| 248 | const groups = yield* Effect.forEach( |
| 249 | apps, |
| 250 | (app) => |
| 251 | listVolumesByApp(app.appName).pipe( |
| 252 | Effect.map((volumes) => |
| 253 | volumes.flatMap((volume) => { |
| 254 | if (!matchesAlchemyPhysicalName(volume.name)) return []; |
| 255 | const volumeId = volume.id; |
| 256 | if (volumeId === undefined || volumeId.length === 0) return []; |
| 257 | return [{ appName: app.appName, volumeId, volume }]; |
| 258 | }), |
| 259 | ), |
| 260 | ), |
| 261 | { concurrency: 8 }, |
| 262 | ); |
| 263 | return groups.flat(); |
| 264 | }); |