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 compute from "@distilled.cloud/gcp/compute_v1"; |
| 2 | import { unownedUnlessPrevious } 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 { tagRecord } from "alchemy/Tags"; |
| 8 | import * as Data from "effect/Data"; |
| 9 | import * as Effect from "effect/Effect"; |
| 10 | import * as Schedule from "effect/Schedule"; |
| 11 | import * as Stream from "effect/Stream"; |
| 12 | import { GcpEnvironment } from "../Environment.ts"; |
| 13 | import { alchemyLabelKeys, createInternalLabels, hasAlchemyLabels } from "../Labels.ts"; |
| 14 | import type { Providers } from "../Providers.ts"; |
| 15 | import { waitRegionOperation } from "./operations.ts"; |
| 16 | |
| 17 | const MAX_NAME_LENGTH = 63; |
| 18 | |
| 19 | export type NodeTemplateCpuOvercommitType = |
| 20 | | compute.NodeTemplateCpuOvercommitTypeEnum |
| 21 | | (string & {}); |
| 22 | export type NodeTemplateLocalDisk = compute.LocalDisk; |
| 23 | export type NodeTemplateAccelerator = compute.AcceleratorConfig; |
| 24 | export type NodeTemplateNodeTypeFlexibility = compute.NodeTemplateNodeTypeFlexibility; |
| 25 | export type NodeTemplateServerBinding = compute.ServerBinding; |
| 26 | |
| 27 | export type NodeTemplateProps = { |
| 28 | /** |
| 29 | * Template name (RFC1035, 1-63 characters). If omitted, a unique name |
| 30 | * is generated from the stack, stage, and logical id. Immutable — |
| 31 | * changing it replaces the template. |
| 32 | */ |
| 33 | nodeTemplateName?: string; |
| 34 | /** |
| 35 | * Region the template lives in. Immutable — changing it replaces the |
| 36 | * template. `US-CENTRAL1` is accepted and normalized to `us-central1`. |
| 37 | * @default the stack's GCP region (`GCP.Region`, else the profile region, else `us-central1`) |
| 38 | */ |
| 39 | region?: string; |
| 40 | /** |
| 41 | * Optional description. Node templates have no labels field, so Alchemy |
| 42 | * ownership (`alchemy-stack` / `alchemy-stage` / `alchemy-id`) is stored |
| 43 | * in a `[alchemy …]` prefix for `list` / nuke. Description is set at |
| 44 | * create time — changing it replaces the template. |
| 45 | */ |
| 46 | description?: string; |
| 47 | /** |
| 48 | * Sole-tenant node type (for example `n2-node-80-640`). Mutually |
| 49 | * exclusive with `nodeTypeFlexibility`. Immutable — changing it |
| 50 | * replaces the template. |
| 51 | */ |
| 52 | nodeType?: string; |
| 53 | /** |
| 54 | * Flexible vCPU / memory / local-SSD requirements. Mutually exclusive |
| 55 | * with `nodeType`. Immutable — changing it replaces the template. |
| 56 | */ |
| 57 | nodeTypeFlexibility?: NodeTemplateNodeTypeFlexibility; |
| 58 | /** |
| 59 | * CPU overcommit (`NONE` or `ENABLED`). Immutable — changing it |
| 60 | * replaces the template. |
| 61 | */ |
| 62 | cpuOvercommitType?: NodeTemplateCpuOvercommitType; |
| 63 | /** |
| 64 | * Node affinity labels used when scheduling instances. Immutable — |
| 65 | * changing them replaces the template. |
| 66 | */ |
| 67 | nodeAffinityLabels?: Record<string, string>; |
| 68 | /** |
| 69 | * Local disks attached to each node. Immutable — changing them |
| 70 | * replaces the template. |
| 71 | */ |
| 72 | disks?: NodeTemplateLocalDisk[]; |
| 73 | /** |
| 74 | * Accelerators attached to each node. Immutable — changing them |
| 75 | * replaces the template. |
| 76 | */ |
| 77 | accelerators?: NodeTemplateAccelerator[]; |
| 78 | /** |
| 79 | * Physical-server binding. Immutable — changing it replaces the |
| 80 | * template. |
| 81 | */ |
| 82 | serverBinding?: NodeTemplateServerBinding; |
| 83 | }; |
| 84 | |
| 85 | export type NodeTemplate = Resource< |
| 86 | "GCP.Compute.NodeTemplate", |
| 87 | NodeTemplateProps, |
| 88 | { |
| 89 | /** Template name. */ |
| 90 | nodeTemplateName: string; |
| 91 | /** Project id. */ |
| 92 | project: string; |
| 93 | /** Region short name (`us-central1`). */ |
| 94 | region: string; |
| 95 | /** User description with the Alchemy ownership prefix stripped. */ |
| 96 | description: string | undefined; |
| 97 | /** Sole-tenant node type. */ |
| 98 | nodeType: string | undefined; |
| 99 | /** Flexible node-type requirements. */ |
| 100 | nodeTypeFlexibility: NodeTemplateNodeTypeFlexibility | undefined; |
| 101 | /** CPU overcommit. */ |
| 102 | cpuOvercommitType: string | undefined; |
| 103 | /** Node affinity labels. */ |
| 104 | nodeAffinityLabels: Record<string, string>; |
| 105 | /** Local disks. */ |
| 106 | disks: ReadonlyArray<NodeTemplateLocalDisk>; |
| 107 | /** Accelerators. */ |
| 108 | accelerators: ReadonlyArray<NodeTemplateAccelerator>; |
| 109 | /** Server binding. */ |
| 110 | serverBinding: NodeTemplateServerBinding | undefined; |
| 111 | /** Template status. */ |
| 112 | status: string | undefined; |
| 113 | /** Status message. */ |
| 114 | statusMessage: string | undefined; |
| 115 | /** Server-assigned numeric id. */ |
| 116 | nodeTemplateId: string | undefined; |
| 117 | /** Resource self-link. */ |
| 118 | selfLink: string | undefined; |
| 119 | /** RFC3339 creation timestamp. */ |
| 120 | creationTimestamp: string | undefined; |
| 121 | /** Resource kind. */ |
| 122 | kind: string | undefined; |
| 123 | }, |
| 124 | never, |
| 125 | Providers |
| 126 | >; |
| 127 | |
| 128 | /** |
| 129 | * A regional Compute Engine sole-tenant node template. |
| 130 | * |
| 131 | * Node templates define the properties of sole-tenant nodes created in a |
| 132 | * node group (node type, CPU overcommit, affinity labels, local disks). |
| 133 | * There is no in-place update API — changing any property replaces the |
| 134 | * template. Compute NodeTemplate has no labels field — Alchemy stamps |
| 135 | * ownership into the description so nuke can find leaked templates. |
| 136 | * |
| 137 | * ### Creating a Node Template |
| 138 | * **Example:** Generated name with a node type |
| 139 | * ```typescript |
| 140 | * const template = yield* GCP.Compute.NodeTemplate("SoleTenant", { |
| 141 | * region: "us-central1", |
| 142 | * nodeType: "n2-node-80-640", |
| 143 | * description: "prod sole tenant", |
| 144 | * }); |
| 145 | * ``` |
| 146 | * |
| 147 | * **Example:** Flexible node type |
| 148 | * ```typescript |
| 149 | * const template = yield* GCP.Compute.NodeTemplate("SoleTenant", { |
| 150 | * nodeTypeFlexibility: { cpus: "80", memory: "640GB" }, |
| 151 | * cpuOvercommitType: "ENABLED", |
| 152 | * }); |
| 153 | * ``` |
| 154 | * |
| 155 | * @resource |
| 156 | * @category Compute |
| 157 | */ |
| 158 | export const NodeTemplate = Resource<NodeTemplate>("GCP.Compute.NodeTemplate"); |
| 159 | |
| 160 | export class NodeTemplateNotResolved extends Data.TaggedError( |
| 161 | "GCP.Compute.NodeTemplateNotResolved", |
| 162 | )<{ |
| 163 | nodeTemplateName: string; |
| 164 | region: string; |
| 165 | }> {} |
| 166 | |
| 167 | export class NodeTemplateStillExists extends Data.TaggedError( |
| 168 | "GCP.Compute.NodeTemplateStillExists", |
| 169 | )<{ |
| 170 | nodeTemplateName: string; |
| 171 | }> {} |
| 172 | |
| 173 | const lastSegment = (value: string | undefined): string => { |
| 174 | if (value === undefined || value.length === 0) return ""; |
| 175 | const trimmed = value.replace(/\/+$/, ""); |
| 176 | const parts = trimmed.split("/"); |
| 177 | return parts[parts.length - 1] || trimmed; |
| 178 | }; |
| 179 | |
| 180 | const normalizeRegion = (region: string | undefined, defaultRegion: string) => |
| 181 | lastSegment(region ?? defaultRegion).toLowerCase(); |
| 182 | |
| 183 | const rfc1035 = (name: string): string => { |
| 184 | let next = name |
| 185 | .toLowerCase() |
| 186 | .replace(/[^a-z0-9-]/g, "-") |
| 187 | .replace(/-+/g, "-") |
| 188 | .replace(/^-+/, "") |
| 189 | .replace(/-+$/, ""); |
| 190 | if (!/^[a-z]/.test(next)) next = `t${next}`; |
| 191 | next = next.slice(0, MAX_NAME_LENGTH).replace(/-+$/, ""); |
| 192 | return next.length > 0 ? next : "template"; |
| 193 | }; |
| 194 | |
| 195 | const toName = (id: string, name: string | undefined, existing?: string) => |
| 196 | Effect.gen(function* () { |
| 197 | if (name !== undefined) return name; |
| 198 | if (existing !== undefined) return existing; |
| 199 | return rfc1035( |
| 200 | yield* createPhysicalName({ |
| 201 | id, |
| 202 | maxLength: MAX_NAME_LENGTH, |
| 203 | lowercase: true, |
| 204 | }), |
| 205 | ); |
| 206 | }); |
| 207 | |
| 208 | const encodeDescription = ( |
| 209 | labels: Record<string, string>, |
| 210 | description: string | undefined, |
| 211 | ): string => { |
| 212 | const marker = `[alchemy ${alchemyLabelKeys.stack}=${labels[alchemyLabelKeys.stack]} ${alchemyLabelKeys.stage}=${labels[alchemyLabelKeys.stage]} ${alchemyLabelKeys.id}=${labels[alchemyLabelKeys.id]}]`; |
| 213 | return description ? `${marker}\n${description}` : marker; |
| 214 | }; |
| 215 | |
| 216 | const parseDescription = ( |
| 217 | description: string | undefined, |
| 218 | ): { |
| 219 | labels: Record<string, string>; |
| 220 | description: string | undefined; |
| 221 | } => { |
| 222 | if (!description?.startsWith("[alchemy ")) { |
| 223 | return { labels: {}, description }; |
| 224 | } |
| 225 | const end = description.indexOf("]"); |
| 226 | if (end < 0) return { labels: {}, description }; |
| 227 | const labels: Record<string, string> = {}; |
| 228 | for (const part of description.slice("[alchemy ".length, end).split(/\s+/)) { |
| 229 | const eq = part.indexOf("="); |
| 230 | if (eq > 0) labels[part.slice(0, eq)] = part.slice(eq + 1); |
| 231 | } |
| 232 | const rest = description.slice(end + 1).replace(/^\n/, ""); |
| 233 | return { labels, description: rest.length > 0 ? rest : undefined }; |
| 234 | }; |
| 235 | |
| 236 | const hasOwnershipMarker = (description: string | undefined) => |
| 237 | Object.keys(parseDescription(description).labels).some((key) => key.startsWith("alchemy-")); |
| 238 | |
| 239 | const sameJson = (left: unknown, right: unknown) => |
| 240 | JSON.stringify(left ?? null) === JSON.stringify(right ?? null); |
| 241 | |
| 242 | const toAttrs = (template: compute.NodeTemplate, project: string): NodeTemplate["Attributes"] => { |
| 243 | const parsed = parseDescription(template.description); |
| 244 | return { |
| 245 | nodeTemplateName: template.name ?? "", |
| 246 | project, |
| 247 | region: lastSegment(template.region).toLowerCase(), |
| 248 | description: parsed.description, |
| 249 | nodeType: template.nodeType, |
| 250 | nodeTypeFlexibility: template.nodeTypeFlexibility, |
| 251 | cpuOvercommitType: template.cpuOvercommitType, |
| 252 | nodeAffinityLabels: tagRecord(template.nodeAffinityLabels), |
| 253 | disks: template.disks ?? [], |
| 254 | accelerators: template.accelerators ?? [], |
| 255 | serverBinding: template.serverBinding, |
| 256 | status: template.status, |
| 257 | statusMessage: template.statusMessage, |
| 258 | nodeTemplateId: template.id, |
| 259 | selfLink: template.selfLink, |
| 260 | creationTimestamp: template.creationTimestamp, |
| 261 | kind: template.kind, |
| 262 | }; |
| 263 | }; |
| 264 | |
| 265 | const getByName = (project: string, region: string, nodeTemplate: string) => |
| 266 | compute |
| 267 | .getNodeTemplates({ project, region, nodeTemplate }) |
| 268 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 269 | |
| 270 | const awaitResource = (project: string, region: string, nodeTemplateName: string) => |
| 271 | getByName(project, region, nodeTemplateName).pipe( |
| 272 | Effect.flatMap((template) => |
| 273 | template !== undefined |
| 274 | ? Effect.succeed(template) |
| 275 | : Effect.fail(new NodeTemplateNotResolved({ nodeTemplateName, region })), |
| 276 | ), |
| 277 | Effect.retry({ |
| 278 | while: (error) => error._tag === "GCP.Compute.NodeTemplateNotResolved", |
| 279 | times: 8, |
| 280 | schedule: Schedule.spaced("1 second"), |
| 281 | }), |
| 282 | ); |
| 283 | |
| 284 | const waitUntilGone = (project: string, region: string, nodeTemplateName: string) => |
| 285 | getByName(project, region, nodeTemplateName).pipe( |
| 286 | Effect.flatMap((template) => |
| 287 | template === undefined |
| 288 | ? Effect.void |
| 289 | : Effect.fail(new NodeTemplateStillExists({ nodeTemplateName })), |
| 290 | ), |
| 291 | Effect.retry({ |
| 292 | while: (error) => error._tag === "GCP.Compute.NodeTemplateStillExists", |
| 293 | times: 10, |
| 294 | schedule: Schedule.spaced("2 seconds"), |
| 295 | }), |
| 296 | Effect.catchTag("GCP.Compute.NodeTemplateStillExists", () => Effect.void), |
| 297 | ); |
| 298 | |
| 299 | export const NodeTemplateProvider = () => |
| 300 | Provider.succeed(NodeTemplate, { |
| 301 | stables: [ |
| 302 | "nodeTemplateName", |
| 303 | "project", |
| 304 | "region", |
| 305 | "nodeTemplateId", |
| 306 | "selfLink", |
| 307 | "nodeType", |
| 308 | "creationTimestamp", |
| 309 | ], |
| 310 | |
| 311 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 312 | if (!isResolved(news)) return undefined; |
| 313 | const env = yield* GcpEnvironment.current; |
| 314 | const previousName = olds?.nodeTemplateName ?? output?.nodeTemplateName; |
| 315 | const nextName = news.nodeTemplateName ?? previousName; |
| 316 | const nameChanged = |
| 317 | previousName !== undefined && nextName !== undefined && previousName !== nextName; |
| 318 | const previousRegion = normalizeRegion(olds?.region ?? output?.region, env.region); |
| 319 | const nextRegion = normalizeRegion(news.region ?? previousRegion, env.region); |
| 320 | |
| 321 | const previousType = olds?.nodeType ?? output?.nodeType ?? ""; |
| 322 | const nextType = news.nodeType ?? previousType; |
| 323 | const previousOvercommit = olds?.cpuOvercommitType ?? output?.cpuOvercommitType; |
| 324 | const nextOvercommit = news.cpuOvercommitType ?? previousOvercommit; |
| 325 | const previousBinding = olds?.serverBinding ?? output?.serverBinding; |
| 326 | const nextBinding = news.serverBinding ?? previousBinding; |
| 327 | const previousFlex = olds?.nodeTypeFlexibility ?? output?.nodeTypeFlexibility; |
| 328 | const nextFlex = news.nodeTypeFlexibility ?? previousFlex; |
| 329 | const previousAffinity = olds?.nodeAffinityLabels ?? output?.nodeAffinityLabels ?? {}; |
| 330 | const nextAffinity = news.nodeAffinityLabels ?? previousAffinity; |
| 331 | const previousDisks = olds?.disks ?? output?.disks ?? []; |
| 332 | const nextDisks = news.disks ?? previousDisks; |
| 333 | const previousAccel = olds?.accelerators ?? output?.accelerators ?? []; |
| 334 | const nextAccel = news.accelerators ?? previousAccel; |
| 335 | const previousDescription = olds?.description ?? output?.description ?? ""; |
| 336 | const nextDescription = news.description ?? previousDescription; |
| 337 | |
| 338 | const immutableChanged = |
| 339 | previousType !== nextType || |
| 340 | previousOvercommit !== nextOvercommit || |
| 341 | !sameJson(previousBinding, nextBinding) || |
| 342 | !sameJson(previousFlex, nextFlex) || |
| 343 | !sameJson(previousAffinity, nextAffinity) || |
| 344 | !sameJson(previousDisks, nextDisks) || |
| 345 | !sameJson(previousAccel, nextAccel) || |
| 346 | previousDescription !== nextDescription; |
| 347 | |
| 348 | if (nameChanged || previousRegion !== nextRegion) { |
| 349 | return { action: "replace" as const, deleteFirst: false }; |
| 350 | } |
| 351 | if (immutableChanged) { |
| 352 | return { action: "replace" as const, deleteFirst: true }; |
| 353 | } |
| 354 | return undefined; |
| 355 | }), |
| 356 | |
| 357 | read: Effect.fn(function* ({ id, olds, output, previous }) { |
| 358 | const env = yield* GcpEnvironment.current; |
| 359 | const nodeTemplateName = yield* toName(id, olds?.nodeTemplateName, output?.nodeTemplateName); |
| 360 | const region = normalizeRegion(olds?.region ?? output?.region, env.region); |
| 361 | const existing = yield* getByName(env.project, region, nodeTemplateName); |
| 362 | if (existing === undefined) return undefined; |
| 363 | const attrs = toAttrs(existing, env.project); |
| 364 | const { labels } = parseDescription(existing.description); |
| 365 | return (yield* hasAlchemyLabels(id, labels)) |
| 366 | ? attrs |
| 367 | : unownedUnlessPrevious(attrs, previous, "nodeTemplateName"); |
| 368 | }), |
| 369 | |
| 370 | list: () => |
| 371 | Effect.gen(function* () { |
| 372 | const env = yield* GcpEnvironment.current; |
| 373 | const pages = yield* compute.aggregatedListNodeTemplates |
| 374 | .pages({ |
| 375 | project: env.project, |
| 376 | returnPartialSuccess: true, |
| 377 | maxResults: 500, |
| 378 | }) |
| 379 | .pipe(Stream.runCollect); |
| 380 | return Array.from(pages).flatMap((page) => |
| 381 | Object.values(page.items ?? {}).flatMap((scoped) => |
| 382 | (scoped?.nodeTemplates ?? []) |
| 383 | .filter((item) => hasOwnershipMarker(item.description)) |
| 384 | .map((item) => toAttrs(item, env.project)), |
| 385 | ), |
| 386 | ); |
| 387 | }), |
| 388 | |
| 389 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 390 | const env = yield* GcpEnvironment.current; |
| 391 | const nodeTemplateName = yield* toName(id, news.nodeTemplateName, output?.nodeTemplateName); |
| 392 | const region = normalizeRegion(news.region ?? output?.region, env.region); |
| 393 | const ownership = yield* createInternalLabels(id); |
| 394 | const desiredDescription = encodeDescription(ownership, news.description); |
| 395 | |
| 396 | let current = yield* getByName(env.project, region, nodeTemplateName); |
| 397 | |
| 398 | if (current === undefined) { |
| 399 | yield* compute |
| 400 | .insertNodeTemplates({ |
| 401 | project: env.project, |
| 402 | region, |
| 403 | body: { |
| 404 | name: nodeTemplateName, |
| 405 | description: desiredDescription, |
| 406 | nodeType: news.nodeType, |
| 407 | nodeTypeFlexibility: news.nodeTypeFlexibility, |
| 408 | cpuOvercommitType: news.cpuOvercommitType, |
| 409 | nodeAffinityLabels: news.nodeAffinityLabels, |
| 410 | disks: news.disks, |
| 411 | accelerators: news.accelerators, |
| 412 | serverBinding: news.serverBinding, |
| 413 | }, |
| 414 | }) |
| 415 | .pipe( |
| 416 | Effect.flatMap((operation) => |
| 417 | waitRegionOperation(env.project, region, operation, { |
| 418 | ignore: ["RESOURCE_ALREADY_EXISTS"], |
| 419 | }), |
| 420 | ), |
| 421 | Effect.catchTag("Conflict", () => Effect.void), |
| 422 | ); |
| 423 | current = yield* awaitResource(env.project, region, nodeTemplateName); |
| 424 | } |
| 425 | |
| 426 | return toAttrs(current, env.project); |
| 427 | }), |
| 428 | |
| 429 | delete: Effect.fn(function* ({ output }) { |
| 430 | if (!output.nodeTemplateName) return; |
| 431 | const env = yield* GcpEnvironment.current; |
| 432 | const project = output.project || env.project; |
| 433 | const region = normalizeRegion(output.region, env.region); |
| 434 | yield* compute |
| 435 | .deleteNodeTemplates({ |
| 436 | project, |
| 437 | region, |
| 438 | nodeTemplate: output.nodeTemplateName, |
| 439 | }) |
| 440 | .pipe( |
| 441 | Effect.flatMap((operation) => |
| 442 | waitRegionOperation(project, region, operation, { |
| 443 | ignore: ["RESOURCE_NOT_FOUND"], |
| 444 | }), |
| 445 | ), |
| 446 | Effect.catchTag("NotFound", () => Effect.void), |
| 447 | Effect.retry({ |
| 448 | while: (error) => error._tag === "Conflict", |
| 449 | times: 8, |
| 450 | schedule: Schedule.spaced("2 seconds"), |
| 451 | }), |
| 452 | ); |
| 453 | yield* waitUntilGone(project, region, output.nodeTemplateName); |
| 454 | }), |
| 455 | }); |