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 * as baremetalsolution from "@distilled.cloud/gcp/baremetalsolution_v2"; |
| 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 { tagRecord } from "alchemy/Tags"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as Schedule from "effect/Schedule"; |
| 9 | import { GcpEnvironment } from "../Environment.ts"; |
| 10 | import { createInternalLabels, diffLabels, hasAlchemyLabels, toLabels } from "../Labels.ts"; |
| 11 | import type { Providers } from "../Providers.ts"; |
| 12 | import { |
| 13 | DEFAULT_SIZE_GIB, |
| 14 | DEFAULT_STORAGE_TYPE, |
| 15 | differs, |
| 16 | fieldMask, |
| 17 | gibOf, |
| 18 | isDeletingState, |
| 19 | listAtLocation, |
| 20 | listLabeledPages, |
| 21 | networkOf, |
| 22 | normalizeLocation, |
| 23 | parentOf, |
| 24 | parseName, |
| 25 | replaceOnIdentity, |
| 26 | ResourceNotResolved, |
| 27 | sameText, |
| 28 | toPhysicalId, |
| 29 | userLabels, |
| 30 | waitForOperation, |
| 31 | waitUntilExists, |
| 32 | waitUntilGone, |
| 33 | waitUntilReady, |
| 34 | } from "./internal.ts"; |
| 35 | |
| 36 | export type AllowedClientProps = { |
| 37 | /** |
| 38 | * Bare Metal Solution network the access point sits on. Full name |
| 39 | * `projects/{project}/locations/{location}/networks/{network}` or the |
| 40 | * network id (combined with the share location). |
| 41 | */ |
| 42 | network: string; |
| 43 | /** |
| 44 | * Subnet of IP addresses permitted to mount the share (CIDR). |
| 45 | */ |
| 46 | allowedClientsCidr: string; |
| 47 | /** |
| 48 | * Mount permissions for the client. |
| 49 | * @default "READ_WRITE" |
| 50 | */ |
| 51 | mountPermissions?: baremetalsolution.AllowedClientMountPermissionsEnum | (string & {}); |
| 52 | /** |
| 53 | * Allow creation of devices (`allow_dev`). |
| 54 | */ |
| 55 | allowDev?: boolean; |
| 56 | /** |
| 57 | * Allow the setuid flag. |
| 58 | */ |
| 59 | allowSuid?: boolean; |
| 60 | /** |
| 61 | * Disable NFS root squashing for this client. |
| 62 | */ |
| 63 | noRootSquash?: boolean; |
| 64 | }; |
| 65 | |
| 66 | export type NfsShareProps = { |
| 67 | /** |
| 68 | * NFS share id (the `{nfsshare}` segment of |
| 69 | * `projects/{project}/locations/{location}/nfsShares/{nfsshare}`). |
| 70 | * If omitted, a unique RFC1035 name is generated from the stack, stage, |
| 71 | * and logical id. Immutable — changing it replaces the share. |
| 72 | */ |
| 73 | nfsShareId?: string; |
| 74 | /** |
| 75 | * Region (`us-central1`, `us-east1`, …). Immutable — changing it |
| 76 | * replaces the share. `US-CENTRAL1` is accepted and normalized to |
| 77 | * `us-central1`. |
| 78 | * @default the stack's GCP region (`GCP.Region`, else the profile region, else `us-central1`) |
| 79 | */ |
| 80 | location?: string; |
| 81 | /** |
| 82 | * Requested size of the underlying volume, in GiB. Minimum is 100. |
| 83 | * @default 100 |
| 84 | */ |
| 85 | requestedSizeGib?: number | string; |
| 86 | /** |
| 87 | * Storage type of the underlying volume. Immutable — changing it |
| 88 | * replaces the share. |
| 89 | * @default "SSD" |
| 90 | */ |
| 91 | storageType?: baremetalsolution.NfsShareStorageTypeEnum | (string & {}); |
| 92 | /** |
| 93 | * Independent infrastructure pod. The share can only attach to networks |
| 94 | * and instances in the same pod. Immutable — changing it replaces the |
| 95 | * share. |
| 96 | */ |
| 97 | pod?: string; |
| 98 | /** |
| 99 | * Access points allowed to mount the share. At least one client is |
| 100 | * required to create a share. |
| 101 | */ |
| 102 | allowedClients: AllowedClientProps[]; |
| 103 | /** |
| 104 | * User labels. Alchemy ownership labels are merged in automatically. |
| 105 | */ |
| 106 | labels?: Record<string, string>; |
| 107 | }; |
| 108 | |
| 109 | export type AllowedClient = { |
| 110 | /** Network the access point sits on. */ |
| 111 | network: string | undefined; |
| 112 | /** Client CIDR. */ |
| 113 | allowedClientsCidr: string | undefined; |
| 114 | /** Mount permissions. */ |
| 115 | mountPermissions: string | undefined; |
| 116 | /** Whether device creation is allowed. */ |
| 117 | allowDev: boolean | undefined; |
| 118 | /** Whether the setuid flag is allowed. */ |
| 119 | allowSuid: boolean | undefined; |
| 120 | /** Whether root squashing is disabled. */ |
| 121 | noRootSquash: boolean | undefined; |
| 122 | /** Share IP assigned on this network. */ |
| 123 | shareIp: string | undefined; |
| 124 | /** NFS path (`shareIp:/instanceId`). */ |
| 125 | nfsPath: string | undefined; |
| 126 | }; |
| 127 | |
| 128 | export type NfsShare = Resource< |
| 129 | "GCP.BareMetalSolution.NfsShare", |
| 130 | NfsShareProps, |
| 131 | { |
| 132 | /** Full resource name. */ |
| 133 | name: string; |
| 134 | /** NFS share id (last path segment). */ |
| 135 | nfsShareId: string; |
| 136 | /** Project id. */ |
| 137 | project: string; |
| 138 | /** Location id. */ |
| 139 | location: string; |
| 140 | /** Requested size in GiB. */ |
| 141 | requestedSizeGib: string | undefined; |
| 142 | /** Storage type of the underlying volume. */ |
| 143 | storageType: string | undefined; |
| 144 | /** Infrastructure pod. */ |
| 145 | pod: string | undefined; |
| 146 | /** Allowed access points. */ |
| 147 | allowedClients: AllowedClient[]; |
| 148 | /** User labels (Alchemy ownership labels stripped). */ |
| 149 | labels: Record<string, string>; |
| 150 | /** Backend-generated identifier. */ |
| 151 | id: string | undefined; |
| 152 | /** Underlying volume created for the share. */ |
| 153 | volume: string | undefined; |
| 154 | /** Server-reported state. */ |
| 155 | state: string | undefined; |
| 156 | }, |
| 157 | never, |
| 158 | Providers |
| 159 | >; |
| 160 | |
| 161 | /** |
| 162 | * A Bare Metal Solution NFS share — file storage attached to BMS |
| 163 | * networks in the same region and pod. |
| 164 | * |
| 165 | * Changing `nfsShareId`, `location`, `storageType`, or `pod` replaces the |
| 166 | * share. Labels and allowed clients update in place. |
| 167 | * |
| 168 | * Creating a share requires a Bare Metal Solution environment (physical |
| 169 | * hardware and a client network). Accounts without BMS entitlement are |
| 170 | * rejected by the API (`ServiceDisabled` when the API is off). |
| 171 | * |
| 172 | * ### Creating an NFS Share |
| 173 | * **Example:** Generated name |
| 174 | * ```typescript |
| 175 | * const share = yield* GCP.BareMetalSolution.NfsShare("Share", { |
| 176 | * allowedClients: [ |
| 177 | * { |
| 178 | * network: "client-net", |
| 179 | * allowedClientsCidr: "10.0.0.0/24", |
| 180 | * mountPermissions: "READ_WRITE", |
| 181 | * noRootSquash: true, |
| 182 | * }, |
| 183 | * ], |
| 184 | * }); |
| 185 | * ``` |
| 186 | * |
| 187 | * **Example:** Explicit id, size, and labels |
| 188 | * ```typescript |
| 189 | * const share = yield* GCP.BareMetalSolution.NfsShare("Share", { |
| 190 | * nfsShareId: "app-nfs", |
| 191 | * requestedSizeGib: 256, |
| 192 | * storageType: "SSD", |
| 193 | * allowedClients: [ |
| 194 | * { |
| 195 | * network: network.name, |
| 196 | * allowedClientsCidr: "10.200.0.0/28", |
| 197 | * mountPermissions: "READ_WRITE", |
| 198 | * allowDev: true, |
| 199 | * noRootSquash: true, |
| 200 | * }, |
| 201 | * ], |
| 202 | * labels: { env: "prod" }, |
| 203 | * }); |
| 204 | * ``` |
| 205 | * |
| 206 | * ### Updating an NFS Share |
| 207 | * **Example:** Labels and allowed clients |
| 208 | * ```typescript |
| 209 | * const share = yield* GCP.BareMetalSolution.NfsShare("Share", { |
| 210 | * allowedClients: [ |
| 211 | * { |
| 212 | * network: network.name, |
| 213 | * allowedClientsCidr: "10.200.0.0/28", |
| 214 | * mountPermissions: "READ_ONLY", |
| 215 | * }, |
| 216 | * ], |
| 217 | * labels: { env: "prod", team: "storage" }, |
| 218 | * }); |
| 219 | * ``` |
| 220 | * |
| 221 | * @resource |
| 222 | * @category BareMetalSolution |
| 223 | */ |
| 224 | export const NfsShare = Resource<NfsShare>("GCP.BareMetalSolution.NfsShare"); |
| 225 | |
| 226 | const resourceName = (project: string, location: string, nfsShareId: string) => |
| 227 | `${parentOf(project, location)}/nfsShares/${nfsShareId}`; |
| 228 | |
| 229 | const toAllowedClient = (client: baremetalsolution.AllowedClient): AllowedClient => ({ |
| 230 | network: client.network, |
| 231 | allowedClientsCidr: client.allowedClientsCidr, |
| 232 | mountPermissions: client.mountPermissions, |
| 233 | allowDev: client.allowDev, |
| 234 | allowSuid: client.allowSuid, |
| 235 | noRootSquash: client.noRootSquash, |
| 236 | shareIp: client.shareIp, |
| 237 | nfsPath: client.nfsPath, |
| 238 | }); |
| 239 | |
| 240 | const desiredClient = ( |
| 241 | client: AllowedClientProps, |
| 242 | project: string, |
| 243 | location: string, |
| 244 | ): baremetalsolution.AllowedClient => ({ |
| 245 | network: networkOf(client.network, project, location), |
| 246 | allowedClientsCidr: client.allowedClientsCidr, |
| 247 | mountPermissions: client.mountPermissions, |
| 248 | allowDev: client.allowDev, |
| 249 | allowSuid: client.allowSuid, |
| 250 | noRootSquash: client.noRootSquash, |
| 251 | }); |
| 252 | |
| 253 | const toAttrs = (item: baremetalsolution.NfsShare, project: string, region: string) => { |
| 254 | const name = item.name ?? ""; |
| 255 | const parsed = parseName(name, "nfsShares", region); |
| 256 | return { |
| 257 | name, |
| 258 | nfsShareId: parsed.id || item.nfsShareId || item.id || "", |
| 259 | project: parsed.project || project, |
| 260 | location: parsed.location, |
| 261 | requestedSizeGib: item.requestedSizeGib, |
| 262 | storageType: item.storageType, |
| 263 | pod: item.pod, |
| 264 | allowedClients: (item.allowedClients ?? []).map(toAllowedClient), |
| 265 | labels: userLabels(item.labels), |
| 266 | id: item.id ?? item.nfsShareId, |
| 267 | volume: item.volume, |
| 268 | state: item.state, |
| 269 | }; |
| 270 | }; |
| 271 | |
| 272 | const getByName = (name: string) => |
| 273 | Effect.gen(function* () { |
| 274 | if (name.length === 0 || name.includes("//")) { |
| 275 | return undefined; |
| 276 | } |
| 277 | return yield* baremetalsolution |
| 278 | .getProjectsLocationsNfsShares({ name }) |
| 279 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 280 | }); |
| 281 | |
| 282 | const listOwned = (project: string, region: string) => |
| 283 | listAtLocation(project, region, (parent) => |
| 284 | listLabeledPages( |
| 285 | baremetalsolution.listProjectsLocationsNfsShares.pages({ |
| 286 | parent, |
| 287 | pageSize: 1000, |
| 288 | }), |
| 289 | (page) => page.nfsShares, |
| 290 | (item) => item.labels, |
| 291 | ), |
| 292 | ); |
| 293 | |
| 294 | export const NfsShareProvider = () => |
| 295 | Provider.succeed(NfsShare, { |
| 296 | stables: ["name", "nfsShareId", "project", "location", "id", "volume"], |
| 297 | |
| 298 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 299 | if (!isResolved(news)) return undefined; |
| 300 | const env = yield* GcpEnvironment.current; |
| 301 | const previousType = olds?.storageType ?? output?.storageType; |
| 302 | const nextType = news.storageType ?? previousType; |
| 303 | const previousPod = olds?.pod ?? output?.pod; |
| 304 | const nextPod = news.pod ?? previousPod; |
| 305 | return replaceOnIdentity({ |
| 306 | previousId: olds?.nfsShareId ?? output?.nfsShareId, |
| 307 | nextId: news.nfsShareId ?? olds?.nfsShareId ?? output?.nfsShareId, |
| 308 | previousLocation: normalizeLocation(olds?.location ?? output?.location, env.region), |
| 309 | nextLocation: normalizeLocation( |
| 310 | news.location ?? olds?.location ?? output?.location, |
| 311 | env.region, |
| 312 | ), |
| 313 | extra: |
| 314 | (previousType !== undefined && nextType !== undefined && previousType !== nextType) || |
| 315 | (previousPod !== undefined && nextPod !== undefined && previousPod !== nextPod), |
| 316 | }); |
| 317 | }), |
| 318 | |
| 319 | read: Effect.fn(function* ({ id, olds, output }) { |
| 320 | const env = yield* GcpEnvironment.current; |
| 321 | const nfsShareId = yield* toPhysicalId(id, olds?.nfsShareId, output?.nfsShareId, "nfsshare"); |
| 322 | const location = normalizeLocation(olds?.location ?? output?.location, env.region); |
| 323 | const name = output?.name ?? resourceName(env.project, location, nfsShareId); |
| 324 | const existing = yield* getByName(name); |
| 325 | if (existing === undefined) return undefined; |
| 326 | const attrs = toAttrs(existing, env.project, env.region); |
| 327 | return (yield* hasAlchemyLabels(id, tagRecord(existing.labels))) ? attrs : Unowned(attrs); |
| 328 | }), |
| 329 | |
| 330 | list: () => |
| 331 | Effect.gen(function* () { |
| 332 | const env = yield* GcpEnvironment.current; |
| 333 | const items = yield* listOwned(env.project, env.region); |
| 334 | return items.map((item) => toAttrs(item, env.project, env.region)); |
| 335 | }), |
| 336 | |
| 337 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 338 | const env = yield* GcpEnvironment.current; |
| 339 | const nfsShareId = yield* toPhysicalId(id, news.nfsShareId, output?.nfsShareId, "nfsshare"); |
| 340 | const location = normalizeLocation(news.location ?? output?.location, env.region); |
| 341 | const name = resourceName(env.project, location, nfsShareId); |
| 342 | const desiredLabels = { |
| 343 | ...toLabels(news.labels), |
| 344 | ...(yield* createInternalLabels(id)), |
| 345 | }; |
| 346 | const size = gibOf(news.requestedSizeGib) ?? DEFAULT_SIZE_GIB; |
| 347 | const storageType = news.storageType ?? DEFAULT_STORAGE_TYPE; |
| 348 | const desiredClients = news.allowedClients.map((client) => |
| 349 | desiredClient(client, env.project, location), |
| 350 | ); |
| 351 | |
| 352 | let current = yield* getByName(output?.name ?? name); |
| 353 | |
| 354 | if (current !== undefined && isDeletingState(current.state)) { |
| 355 | yield* waitUntilGone(getByName(current.name ?? name), current.name ?? name); |
| 356 | current = undefined; |
| 357 | } |
| 358 | |
| 359 | if (current === undefined) { |
| 360 | const created = yield* baremetalsolution |
| 361 | .createProjectsLocationsNfsShares({ |
| 362 | parent: parentOf(env.project, location), |
| 363 | body: { |
| 364 | name, |
| 365 | requestedSizeGib: size, |
| 366 | storageType, |
| 367 | pod: news.pod, |
| 368 | allowedClients: desiredClients, |
| 369 | labels: desiredLabels, |
| 370 | }, |
| 371 | }) |
| 372 | .pipe(Effect.catchTag("Conflict", () => Effect.undefined)); |
| 373 | if (created !== undefined) { |
| 374 | yield* waitForOperation(created); |
| 375 | } |
| 376 | current = yield* waitUntilExists(getByName(name), name); |
| 377 | } |
| 378 | |
| 379 | if (current === undefined) { |
| 380 | return yield* new ResourceNotResolved({ resourceName: name }); |
| 381 | } |
| 382 | |
| 383 | const ready = yield* waitUntilReady( |
| 384 | getByName(current.name ?? name), |
| 385 | current.name ?? name, |
| 386 | (item) => item.state, |
| 387 | ); |
| 388 | |
| 389 | const observedLabels = tagRecord(ready.labels); |
| 390 | const { upsert, removed } = diffLabels(observedLabels, desiredLabels); |
| 391 | const labelsChanged = upsert.length > 0 || removed.length > 0; |
| 392 | const clientsChanged = differs( |
| 393 | (ready.allowedClients ?? []).map((client) => ({ |
| 394 | network: client.network, |
| 395 | allowedClientsCidr: client.allowedClientsCidr, |
| 396 | mountPermissions: client.mountPermissions, |
| 397 | allowDev: client.allowDev, |
| 398 | allowSuid: client.allowSuid, |
| 399 | noRootSquash: client.noRootSquash, |
| 400 | })), |
| 401 | desiredClients, |
| 402 | ); |
| 403 | const sizeChanged = !sameText(ready.requestedSizeGib, size); |
| 404 | const mask = fieldMask([ |
| 405 | labelsChanged && "labels", |
| 406 | clientsChanged && "allowedClients", |
| 407 | sizeChanged && "requestedSizeGib", |
| 408 | ]); |
| 409 | |
| 410 | if (mask.length > 0) { |
| 411 | const operation = yield* baremetalsolution.patchProjectsLocationsNfsShares({ |
| 412 | name: ready.name ?? name, |
| 413 | updateMask: mask, |
| 414 | body: { |
| 415 | name: ready.name ?? name, |
| 416 | labels: desiredLabels, |
| 417 | allowedClients: desiredClients, |
| 418 | requestedSizeGib: size, |
| 419 | }, |
| 420 | }); |
| 421 | yield* waitForOperation(operation); |
| 422 | const patched = yield* waitUntilReady( |
| 423 | getByName(ready.name ?? name), |
| 424 | ready.name ?? name, |
| 425 | (item) => item.state, |
| 426 | ); |
| 427 | return toAttrs(patched, env.project, env.region); |
| 428 | } |
| 429 | |
| 430 | return toAttrs(ready, env.project, env.region); |
| 431 | }), |
| 432 | |
| 433 | delete: Effect.fn(function* ({ output }) { |
| 434 | if (!output.name || output.name.includes("//")) return; |
| 435 | const operation = yield* baremetalsolution |
| 436 | .deleteProjectsLocationsNfsShares({ name: output.name }) |
| 437 | .pipe( |
| 438 | Effect.retry({ |
| 439 | while: (error) => error._tag === "Conflict", |
| 440 | times: 8, |
| 441 | schedule: Schedule.spaced("2 seconds"), |
| 442 | }), |
| 443 | Effect.catchTag("NotFound", () => Effect.undefined), |
| 444 | ); |
| 445 | if (operation !== undefined) { |
| 446 | yield* waitForOperation(operation, { notFoundOk: true }); |
| 447 | } |
| 448 | yield* waitUntilGone(getByName(output.name), output.name); |
| 449 | }), |
| 450 | }); |