@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

main
ts 422 lines 14.5 KB
Raw
1 import * as compute from "@distilled.cloud/gcp/compute_v1";
2 import { Unowned } 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 * as Data from "effect/Data";
8 import * as Effect from "effect/Effect";
9 import * as Schedule from "effect/Schedule";
10 import * as Stream from "effect/Stream";
11 import { GcpEnvironment } from "../Environment.ts";
12 import { alchemyLabelKeys, createInternalLabels, hasAlchemyLabels } from "../Labels.ts";
13 import type { Providers } from "../Providers.ts";
14 import { waitGlobalOperation } from "./operations.ts";
15
16 export type GlobalVmExtensionPolicyExtensionPolicy = compute.GlobalVmExtensionPolicyExtensionPolicy;
17 export type GlobalVmExtensionPolicyInstanceSelector =
18 compute.GlobalVmExtensionPolicyInstanceSelector;
19 export type GlobalVmExtensionPolicyRolloutOperation =
20 compute.GlobalVmExtensionPolicyRolloutOperation;
21
22 export type GlobalVmExtensionPolicyProps = {
23 /**
24 * Policy name (RFC1035, 1-63 chars). If omitted, a unique name is
25 * generated from the stack, stage, and logical id. Changing it replaces
26 * the policy.
27 */
28 policyName?: string;
29 /**
30 * Optional description. Global VM extension policies have no labels
31 * field — Alchemy ownership is stored in a `[alchemy …]` prefix for
32 * `list` / nuke.
33 */
34 description?: string;
35 /**
36 * Map of extension name (e.g. `"ops-agent"`) to policy configuration.
37 * Required by the API.
38 */
39 extensionPolicies?: compute.GlobalVmExtensionPolicyExtensionPolicyMap;
40 /**
41 * Rollout plan. Defaults to a fast project-wide rollout.
42 */
43 rolloutOperation?: GlobalVmExtensionPolicyRolloutOperation;
44 /**
45 * Conflict-resolution priority. Larger numbers win.
46 * @default 0
47 */
48 priority?: number;
49 /**
50 * VM selectors. Empty applies the policy to every VM. Selectors are
51 * AND'd together.
52 */
53 instanceSelectors?: GlobalVmExtensionPolicyInstanceSelector[];
54 };
55
56 export type GlobalVmExtensionPolicy = Resource<
57 "GCP.Compute.GlobalVmExtensionPolicy",
58 GlobalVmExtensionPolicyProps,
59 {
60 /** Policy name. */
61 policyName: string;
62 /** Project id. */
63 project: string;
64 /** User description with the Alchemy ownership prefix stripped. */
65 description: string | undefined;
66 /** Extension policy map. */
67 extensionPolicies: compute.GlobalVmExtensionPolicyExtensionPolicyMap;
68 /** Rollout operation. */
69 rolloutOperation: GlobalVmExtensionPolicyRolloutOperation | undefined;
70 /** Priority. */
71 priority: number | undefined;
72 /** VM selectors. */
73 instanceSelectors: GlobalVmExtensionPolicyInstanceSelector[];
74 /** Server-assigned numeric id. */
75 policyId: string | undefined;
76 /** Server-defined URL. */
77 selfLink: string | undefined;
78 /** RFC3339 creation timestamp. */
79 creationTimestamp: string | undefined;
80 /** RFC3339 update timestamp. */
81 updateTimestamp: string | undefined;
82 /** Scoped resource purge status. */
83 scopedResourceStatus: string | undefined;
84 },
85 never,
86 Providers
87 >;
88
89 /**
90 * A project-level Compute Engine VM extension policy.
91 *
92 * Global policies roll out guest extensions (Ops Agent, and similar) to
93 * matching VMs. Name replaces the resource; description, priority,
94 * selectors, extensions, and the rollout plan update in place. Compute
95 * has no labels field, so Alchemy stamps ownership into the description.
96 *
97 * ### Creating a Global VM Extension Policy
98 * **Example:** Ops Agent, fast rollout
99 * ```typescript
100 * const policy = yield* GCP.Compute.GlobalVmExtensionPolicy("ops", {
101 * extensionPolicies: { "ops-agent": {} },
102 * rolloutOperation: {
103 * rolloutInput: { predefinedRolloutPlan: "FAST_ROLLOUT" },
104 * },
105 * });
106 * ```
107 *
108 * **Example:** Label-selected VMs
109 * ```typescript
110 * const policy = yield* GCP.Compute.GlobalVmExtensionPolicy("ops", {
111 * extensionPolicies: { "ops-agent": { pinnedVersion: "2.53.0" } },
112 * instanceSelectors: [
113 * { labelSelector: { inclusionLabels: { role: "web" } } },
114 * ],
115 * priority: 100,
116 * });
117 * ```
118 *
119 * @resource
120 * @category Compute
121 */
122 export const GlobalVmExtensionPolicy = Resource<GlobalVmExtensionPolicy>(
123 "GCP.Compute.GlobalVmExtensionPolicy",
124 );
125
126 export class GlobalVmExtensionPolicyNotResolved extends Data.TaggedError(
127 "GCP.Compute.GlobalVmExtensionPolicyNotResolved",
128 )<{
129 policyName: string;
130 }> {}
131
132 export class GlobalVmExtensionPolicyStillExists extends Data.TaggedError(
133 "GCP.Compute.GlobalVmExtensionPolicyStillExists",
134 )<{
135 policyName: string;
136 }> {}
137
138 const DEFAULT_ROLLOUT: GlobalVmExtensionPolicyRolloutOperation = {
139 rolloutInput: { predefinedRolloutPlan: "FAST_ROLLOUT" },
140 };
141
142 const rfc1035 = (name: string): string => {
143 let next = name
144 .toLowerCase()
145 .replace(/[^a-z0-9-]/g, "-")
146 .replace(/-+/g, "-")
147 .replace(/^-+/, "")
148 .replace(/-+$/, "");
149 if (!/^[a-z]/.test(next)) {
150 next = `v${next}`;
151 }
152 next = next.slice(0, 63).replace(/-+$/, "");
153 return next.length > 0 ? next : "vmextensionpolicy";
154 };
155
156 const toName = (id: string, name: string | undefined, existing?: string) =>
157 Effect.gen(function* () {
158 if (name !== undefined) return name;
159 if (existing !== undefined) return existing;
160 return rfc1035(
161 yield* createPhysicalName({
162 id,
163 maxLength: 63,
164 lowercase: true,
165 }),
166 );
167 });
168
169 const encodeDescription = (
170 labels: Record<string, string>,
171 description: string | undefined,
172 ): string => {
173 const marker = `[alchemy ${alchemyLabelKeys.stack}=${labels[alchemyLabelKeys.stack]} ${alchemyLabelKeys.stage}=${labels[alchemyLabelKeys.stage]} ${alchemyLabelKeys.id}=${labels[alchemyLabelKeys.id]}]`;
174 return description ? `${marker}\n${description}` : marker;
175 };
176
177 const parseDescription = (
178 description: string | undefined,
179 ): {
180 labels: Record<string, string>;
181 description: string | undefined;
182 } => {
183 if (!description?.startsWith("[alchemy ")) {
184 return { labels: {}, description };
185 }
186 const end = description.indexOf("]");
187 if (end < 0) return { labels: {}, description };
188 const labels: Record<string, string> = {};
189 for (const part of description.slice("[alchemy ".length, end).split(/\s+/)) {
190 const eq = part.indexOf("=");
191 if (eq > 0) {
192 labels[part.slice(0, eq)] = part.slice(eq + 1);
193 }
194 }
195 const rest = description.slice(end + 1).replace(/^\n/, "");
196 return { labels, description: rest.length > 0 ? rest : undefined };
197 };
198
199 const jsonOf = (value: unknown) => JSON.stringify(value ?? null);
200
201 // Compute echoes unset extension fields back as empty strings
202 // (`{ pinnedVersion: "", stringConfig: "" }`), so compare only set fields.
203 const extensionPoliciesKey = (policies: compute.GlobalVmExtensionPolicy["extensionPolicies"]) =>
204 jsonOf(
205 Object.entries(policies ?? {})
206 .sort(([a], [b]) => a.localeCompare(b))
207 .map(([name, policy]) => [
208 name,
209 Object.entries(policy ?? {})
210 .filter(([, value]) => value !== "" && value != null)
211 .sort(([a], [b]) => a.localeCompare(b)),
212 ]),
213 );
214
215 const toBody = (
216 policyName: string,
217 props: GlobalVmExtensionPolicyProps,
218 ownership: Record<string, string>,
219 ): compute.GlobalVmExtensionPolicy => ({
220 name: policyName,
221 description: encodeDescription(ownership, props.description),
222 extensionPolicies: props.extensionPolicies ?? { "ops-agent": {} },
223 rolloutOperation: props.rolloutOperation ?? DEFAULT_ROLLOUT,
224 priority: props.priority,
225 instanceSelectors: props.instanceSelectors,
226 });
227
228 const toAttrs = (
229 policy: compute.GlobalVmExtensionPolicy,
230 project: string,
231 ): GlobalVmExtensionPolicy["Attributes"] => {
232 const parsed = parseDescription(policy.description);
233 return {
234 policyName: policy.name ?? policy.id ?? "",
235 project,
236 description: parsed.description,
237 extensionPolicies: policy.extensionPolicies ?? {},
238 rolloutOperation: policy.rolloutOperation,
239 priority: policy.priority,
240 instanceSelectors: policy.instanceSelectors ?? [],
241 policyId: policy.id,
242 selfLink: policy.selfLink,
243 creationTimestamp: policy.creationTimestamp,
244 updateTimestamp: policy.updateTimestamp,
245 scopedResourceStatus: policy.scopedResourceStatus,
246 };
247 };
248
249 const needsUpdate = (
250 current: compute.GlobalVmExtensionPolicy,
251 desired: compute.GlobalVmExtensionPolicy,
252 ) => {
253 if ((current.description ?? "") !== (desired.description ?? "")) return true;
254 if ((current.priority ?? 0) !== (desired.priority ?? 0)) return true;
255 if (
256 extensionPoliciesKey(current.extensionPolicies) !==
257 extensionPoliciesKey(desired.extensionPolicies)
258 ) {
259 return true;
260 }
261 if (jsonOf(current.instanceSelectors ?? []) !== jsonOf(desired.instanceSelectors ?? [])) {
262 return true;
263 }
264 const currentPlan = current.rolloutOperation?.rolloutInput?.predefinedRolloutPlan;
265 const desiredPlan = desired.rolloutOperation?.rolloutInput?.predefinedRolloutPlan;
266 return (currentPlan ?? "") !== (desiredPlan ?? "");
267 };
268
269 const getByName = (project: string, globalVmExtensionPolicy: string) =>
270 compute
271 .getGlobalVmExtensionPolicies({ project, globalVmExtensionPolicy })
272 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
273
274 const awaitResource = (project: string, policyName: string) =>
275 getByName(project, policyName).pipe(
276 Effect.repeat({
277 schedule: Schedule.spaced("1 second"),
278 until: (item) => item !== undefined,
279 times: 8,
280 }),
281 );
282
283 export const GlobalVmExtensionPolicyProvider = () =>
284 Provider.succeed(GlobalVmExtensionPolicy, {
285 stables: ["policyName", "project", "policyId", "selfLink", "creationTimestamp"],
286
287 diff: Effect.fn(function* ({ news, olds, output }) {
288 if (!isResolved(news)) return undefined;
289 const previousName = olds?.policyName ?? output?.policyName;
290 const nextName = news.policyName;
291 if (previousName !== undefined && nextName !== undefined && previousName !== nextName) {
292 return { action: "replace" as const };
293 }
294 return undefined;
295 }),
296
297 read: Effect.fn(function* ({ id, olds, output }) {
298 const env = yield* GcpEnvironment.current;
299 const policyName = yield* toName(id, olds?.policyName, output?.policyName);
300 const existing = yield* getByName(env.project, policyName);
301 if (existing === undefined) return undefined;
302 const attrs = toAttrs(existing, env.project);
303 const { labels } = parseDescription(existing.description);
304 return (yield* hasAlchemyLabels(id, labels)) ? attrs : Unowned(attrs);
305 }),
306
307 list: () =>
308 Effect.gen(function* () {
309 const env = yield* GcpEnvironment.current;
310 return yield* compute.listGlobalVmExtensionPolicies
311 .items({ project: env.project, maxResults: 500 })
312 .pipe(
313 Stream.filter((policy) => {
314 const { labels } = parseDescription(policy.description);
315 return Object.keys(labels).some((key) => key.startsWith("alchemy-"));
316 }),
317 Stream.map((policy) => toAttrs(policy, env.project)),
318 Stream.runCollect,
319 Effect.map((chunk) => Array.from(chunk)),
320 );
321 }),
322
323 reconcile: Effect.fn(function* ({ id, news, output }) {
324 const env = yield* GcpEnvironment.current;
325 const policyName = yield* toName(id, news.policyName, output?.policyName);
326 const ownership = yield* createInternalLabels(id);
327 const desired = toBody(policyName, news, ownership);
328
329 let current = yield* getByName(env.project, policyName);
330
331 if (current === undefined) {
332 yield* compute
333 .insertGlobalVmExtensionPolicies({
334 project: env.project,
335 body: desired,
336 })
337 .pipe(
338 Effect.flatMap((operation) =>
339 waitGlobalOperation(env.project, operation, {
340 ignore: ["RESOURCE_ALREADY_EXISTS"],
341 }),
342 ),
343 Effect.catchTag("Conflict", () => Effect.undefined),
344 );
345 current = yield* awaitResource(env.project, policyName);
346 }
347
348 if (current === undefined) {
349 return yield* new GlobalVmExtensionPolicyNotResolved({ policyName });
350 }
351
352 if (needsUpdate(current, desired)) {
353 yield* compute
354 .updateGlobalVmExtensionPolicies({
355 project: env.project,
356 globalVmExtensionPolicy: policyName,
357 body: desired,
358 })
359 .pipe(Effect.flatMap((operation) => waitGlobalOperation(env.project, operation)));
360 current = yield* getByName(env.project, policyName);
361 if (current === undefined) {
362 return yield* new GlobalVmExtensionPolicyNotResolved({
363 policyName,
364 });
365 }
366 }
367
368 return toAttrs(current, env.project);
369 }),
370
371 delete: Effect.fn(function* ({ output }) {
372 const env = yield* GcpEnvironment.current;
373 const requestDelete = (retryUuid?: string) =>
374 compute
375 .deleteGlobalVmExtensionPolicies({
376 project: env.project,
377 globalVmExtensionPolicy: output.policyName,
378 body: { predefinedRolloutPlan: "FAST_ROLLOUT", retryUuid },
379 })
380 .pipe(
381 Effect.retry({
382 while: (error) => error._tag === "Conflict",
383 schedule: Schedule.spaced("2 seconds"),
384 times: 8,
385 }),
386 Effect.catchTag("NotFound", () => Effect.undefined),
387 Effect.flatMap((operation) =>
388 operation === undefined
389 ? Effect.void
390 : waitGlobalOperation(env.project, operation).pipe(
391 Effect.catchTag("NotFound", () => Effect.void),
392 Effect.asVoid,
393 ),
394 ),
395 );
396 // A policy left in DELETING by an earlier (possibly failed) delete
397 // rollout only accepts another delete with a fresh `retryUuid`.
398 const observed = yield* getByName(env.project, output.policyName);
399 if (observed === undefined) return;
400 const retryUuid =
401 observed.scopedResourceStatus === "SCOPED_RESOURCE_STATUS_DELETING"
402 ? yield* Effect.sync(() => crypto.randomUUID())
403 : undefined;
404 yield* requestDelete(retryUuid);
405 // Deletion is itself a rollout; the policy stays readable until the
406 // rollout finishes.
407 yield* getByName(env.project, output.policyName).pipe(
408 Effect.filterOrFail(
409 (policy) => policy === undefined,
410 () =>
411 new GlobalVmExtensionPolicyStillExists({
412 policyName: output.policyName,
413 }),
414 ),
415 Effect.retry({
416 while: (error) => error._tag === "GCP.Compute.GlobalVmExtensionPolicyStillExists",
417 schedule: Schedule.spaced("5 seconds"),
418 times: 60,
419 }),
420 );
421 }),
422 });