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

main
ts 1,221 lines 44.3 KB
Raw
1 import { randomBytes } from "node:crypto";
2 import { Query, type UnwrapPlan } from "@distilled.cloud/core/query";
3 import {
4 Railway,
5 type Service,
6 type ServiceCreateInput,
7 type ServiceInstance,
8 type ServiceInstanceUpdateInput,
9 type TCPProxy,
10 type TCPProxyCreateInput,
11 type VolumeCreateInput,
12 type VolumeInstance,
13 type VolumeInstanceUpdateInput,
14 type VolumeState,
15 } from "@distilled.cloud/railway";
16 import { Unowned } from "alchemy/AdoptPolicy";
17 import { isResolved } from "alchemy/Diff";
18 import * as Provider from "alchemy/Provider";
19 import { Resource } from "alchemy/Resource";
20 import * as Data from "effect/Data";
21 import * as Effect from "effect/Effect";
22 import * as Redacted from "effect/Redacted";
23 import * as Schedule from "effect/Schedule";
24 import * as Stream from "effect/Stream";
25 import {
26 environmentServiceInstances,
27 waitUntilDeleted,
28 projectServices,
29 environmentVolumes,
30 } from "./GraphQL.ts";
31 import { createRailwayName, matchesAlchemyPhysicalName } from "./Metadata.ts";
32 import { ownedProjects, type Project } from "./Project.ts";
33 import type { Providers } from "./Providers.ts";
34
35 const serviceFields = <E>(service: Query<Service, E>) => ({
36 id: service.id,
37 name: service.name,
38 deletedAt: service.deletedAt,
39 });
40 const attributeInstanceFields = <E>(instance: Query<ServiceInstance, E>) => ({
41 source: instance.source.pipe(Query.map((source) => ({ image: source.image }))),
42 region: instance.region,
43 latestDeployment: instance.latestDeployment.pipe(
44 Query.map((deployment) => ({ id: deployment.id, status: deployment.status })),
45 ),
46 });
47 const instanceFields = <E>(instance: Query<ServiceInstance, E>) => ({
48 ...attributeInstanceFields(instance),
49 deletedAt: instance.deletedAt,
50 sleepApplication: instance.sleepApplication,
51 startCommand: instance.startCommand,
52 });
53 const volumeFields = <E>(instance: Query<VolumeInstance, E>) => ({
54 id: instance.id,
55 volumeId: instance.volumeId,
56 environmentId: instance.environmentId,
57 serviceId: instance.serviceId,
58 deletedAt: instance.deletedAt,
59 isPendingDeletion: instance.isPendingDeletion,
60 state: instance.state,
61 mountPath: instance.mountPath,
62 volume: { id: instance.volume.id, name: instance.volume.name },
63 });
64 const proxyFields = <E>(proxy: Query<TCPProxy, E>) => ({
65 id: proxy.id,
66 applicationPort: proxy.applicationPort,
67 deletedAt: proxy.deletedAt,
68 syncStatus: proxy.syncStatus,
69 domain: proxy.domain,
70 proxyPort: proxy.proxyPort,
71 });
72 type CloudService = UnwrapPlan<ReturnType<typeof serviceFields>>;
73 type CloudAttributeInstance = UnwrapPlan<ReturnType<typeof attributeInstanceFields>>;
74 type ServiceInstanceResponse = UnwrapPlan<ReturnType<typeof instanceFields>>;
75 type CloudInstance = UnwrapPlan<ReturnType<typeof volumeFields>>;
76 type CloudProxy = UnwrapPlan<ReturnType<typeof proxyFields>>;
77
78 const readService = Query.fn((id: string) => serviceFields(Railway.service({ id })));
79 const readServiceInstance = Query.fn((environmentId: string, serviceId: string) =>
80 instanceFields(Railway.serviceInstance({ environmentId, serviceId })),
81 );
82 const readVolumeInstance = Query.fn((id: string) => volumeFields(Railway.volumeInstance({ id })));
83 const readProxies = Query.fn((environmentId: string, serviceId: string) =>
84 Railway.tcpProxies({ environmentId, serviceId }).pipe(Query.map(proxyFields)),
85 );
86 const readVariables = Query.fn((projectId: string, environmentId: string, serviceId: string) =>
87 Railway.variables({ projectId, environmentId, serviceId, unrendered: true }),
88 );
89 const liveEnvironments = (projectId: string) =>
90 Query.items(
91 Railway.environments({ projectId, first: 50 }).pipe(
92 Query.map((env) => ({ id: env.id, deletedAt: env.deletedAt })),
93 ),
94 );
95 const serviceCreate = Query.fn((args: { input: ServiceCreateInput }) =>
96 serviceFields(Railway.serviceCreate(args)),
97 );
98 const serviceUpdate = Query.fn((args: { id: string; input: { name: string } }) =>
99 serviceFields(Railway.serviceUpdate(args)),
100 );
101 const serviceInstanceUpdate = Query.fn(
102 (args: { environmentId: string; serviceId: string; input: ServiceInstanceUpdateInput }) =>
103 Railway.serviceInstanceUpdate(args),
104 );
105 const serviceInstanceDeployV2 = Query.fn((args: { environmentId: string; serviceId: string }) =>
106 Railway.serviceInstanceDeployV2(args),
107 );
108 const deploymentCancel = Query.fn((args: { id: string }) => Railway.deploymentCancel(args));
109 const serviceDelete = Query.fn((args: { id: string }) => Railway.serviceDelete(args));
110 const volumeCreate = Query.fn((args: { input: VolumeCreateInput }) => {
111 const volume = Railway.volumeCreate(args);
112 return { id: volume.id, name: volume.name };
113 });
114 const volumeUpdate = Query.fn((volumeId: string, name: string) => ({
115 id: Railway.volumeUpdate({ volumeId, input: { name } }).id,
116 }));
117 const volumeInstanceUpdate = Query.fn(
118 (args: { volumeId: string; environmentId: string; input: VolumeInstanceUpdateInput }) =>
119 Railway.volumeInstanceUpdate(args),
120 );
121 const volumeDelete = Query.fn((args: { volumeId: string }) => Railway.volumeDelete(args));
122 const tcpProxyCreate = Query.fn((args: { input: TCPProxyCreateInput }) =>
123 proxyFields(Railway.tcpProxyCreate(args)),
124 );
125 const tcpProxyDelete = Query.fn((id: string) => Railway.tcpProxyDelete({ id }));
126 const variableUpsert = Query.fn(
127 (input: {
128 projectId: string;
129 environmentId: string;
130 serviceId: string;
131 name: string;
132 value: string;
133 }) => Railway.variableUpsert({ input: { ...input, skipDeploys: true } }),
134 );
135
136 /**
137 * A resource-valued prop: the resource itself, or an Effect that produces
138 * it (so `yield* Project(...)` and `Project(...)` both type-check).
139 */
140 type Ref<T> = T | Effect.Effect<T, never, Providers>;
141
142 export const DEFAULT_MONGO_IMAGE = "mongo:8";
143 export const DEFAULT_MONGO_USER = "mongo";
144 export const DEFAULT_MONGO_DATABASE = "railway";
145 export const MONGO_PORT = 27017;
146 export const MONGO_MOUNT_PATH = "/data/db";
147 export const MONGO_URL_SECRET = "MONGO_URL";
148 export const MONGO_PUBLIC_URL_SECRET = "MONGO_PUBLIC_URL";
149 /**
150 * Official Mongo image must bind IPv6 so `{name}.railway.internal` works.
151 * `docker-entrypoint.sh` is required so `MONGO_INITDB_ROOT_*` still runs.
152 */
153 export const MONGO_START_COMMAND =
154 "docker-entrypoint.sh mongod --ipv6 --bind_ip ::,0.0.0.0 --setParameter diagnosticDataCollectionEnabled=false";
155
156 /**
157 * Environment identity Mongo is deployed into. Accepts a
158 * `Railway.Project` (its primary environment), a `Railway.Environment`,
159 * or an `{ environmentId }` stub.
160 */
161 export type MongoEnvironment = { readonly environmentId: string };
162
163 export interface MongoProps {
164 /**
165 * Parent Railway Project. Accepts a `Railway.Project` or an Effect
166 * that produces one. Changing the Project replaces Mongo.
167 */
168 project: Ref<Project>;
169 /**
170 * Environment to deploy into. Accepts a `Railway.Project` (primary
171 * environment), a `Railway.Environment`, or `{ environmentId }`.
172 * Defaults to the project's primary environment. Changing it replaces
173 * Mongo.
174 */
175 environment?: Ref<MongoEnvironment>;
176 /**
177 * Service name. Unique per Project. If omitted, a unique name is
178 * generated from the stack, stage and logical ID. Used as the private
179 * hostname `{name}.railway.internal`. Changing it updates in place.
180 */
181 name?: string;
182 /**
183 * MongoDB image. Default is the official `mongo:8` tag.
184 *
185 * @default "mongo:8"
186 */
187 image?: string;
188 /**
189 * Region for the service instance and volume (`us-west2`, `us-east4`,
190 * …). If omitted, Railway picks the default. Changing it replaces
191 * Mongo.
192 */
193 region?: string;
194 /**
195 * Root username. Create-only (used at `MONGO_INITDB_ROOT_USERNAME`).
196 *
197 * @default "mongo"
198 */
199 user?: string;
200 /**
201 * Root password. Wrap with `Redacted.make(...)`. If omitted, a
202 * password is generated on first create and stored as
203 * `MONGO_INITDB_ROOT_PASSWORD`. Create-only.
204 */
205 password?: Redacted.Redacted<string> | string;
206 /**
207 * Initial database name (`MONGO_INITDB_DATABASE`). Create-only.
208 *
209 * @default "railway"
210 */
211 database?: string;
212 /**
213 * Expose Mongo on a public TCP proxy (`*.proxy.rlwy.net`) for
214 * laptop access. In-service connections always use
215 * `{name}.railway.internal`.
216 *
217 * @default true
218 */
219 public?: boolean;
220 }
221
222 export type Mongo = Resource<
223 "Railway.Mongo",
224 MongoProps,
225 {
226 /** Railway service id for the Mongo container. */
227 serviceId: string;
228 /** Physical service name. Private hostname is `{name}.railway.internal`. */
229 name: string;
230 /** Parent Railway project id. */
231 projectId: string;
232 /** Environment the instance is deployed in. */
233 environmentId: string;
234 /** Observed `source.image`. */
235 image: string | undefined;
236 /** Observed region, if Railway reported one. */
237 region: string | undefined;
238 /** Volume id holding `/data/db`. */
239 volumeId: string;
240 /** Volume instance id in the target environment. */
241 volumeInstanceId: string;
242 /** TCP proxy id, when `public` is enabled. */
243 tcpProxyId: string | undefined;
244 /** Public proxy hostname (`*.proxy.rlwy.net`). */
245 tcpProxyDomain: string | undefined;
246 /** Public proxy port. Pair with `tcpProxyDomain`. */
247 tcpProxyPort: number | undefined;
248 /** Root username. */
249 user: string;
250 /** Database name. */
251 database: string;
252 /**
253 * Private Mongo URI (`{name}.railway.internal:27017`). Prefer
254 * {@link ConnectMongo} from a {@link Service}.
255 */
256 connectionUri: string;
257 /**
258 * Public TCP-proxy URI. Empty when `public` is false. Use this from
259 * the laptop.
260 */
261 publicConnectionUri: string;
262 /** Latest deployment id, if one exists. */
263 deploymentId: string | undefined;
264 /** Latest deployment status (`SUCCESS`, `DEPLOYING`, …). */
265 deploymentStatus: string | undefined;
266 },
267 never,
268 Providers
269 >;
270
271 const resolveMongoProps = (
272 props: MongoProps | Effect.Effect<MongoProps, never, Providers>,
273 ): Effect.Effect<MongoProps, never, Providers> =>
274 Effect.gen(function* () {
275 const resolved = Effect.isEffect(props) ? yield* props : props;
276 if (globalThis.__ALCHEMY_RUNTIME__) return resolved;
277 const project = Effect.isEffect(resolved.project)
278 ? yield* resolved.project as Effect.Effect<Project, never, Providers>
279 : resolved.project;
280 const environment =
281 resolved.environment === undefined
282 ? undefined
283 : Effect.isEffect(resolved.environment)
284 ? yield* resolved.environment as Effect.Effect<MongoEnvironment, never, Providers>
285 : resolved.environment;
286 return { ...resolved, project, environment };
287 });
288
289 const MongoResource = Resource<Mongo>("Railway.Mongo");
290
291 /**
292 * A Railway.Mongo is MongoDB-as-a-Service: the official `mongo` image, a
293 * Volume at `/data/db`, `MONGO_*` / `MONGO_URL` variables, IPv6 bind for
294 * the private network, and an optional TCP proxy for the public URL.
295 *
296 * Private hostname is `{name}.railway.internal`. From a
297 * {@link Service}, yield {@link ConnectMongo}. From a laptop, use
298 * `publicConnectionUri`. The IaC helper is {@link mongo}.
299 *
300 * @see https://docs.railway.com/databases/mongodb
301 *
302 * ### Create Mongo
303 * Pass a Project. Alchemy generates a unique name, password, volume,
304 * and a public TCP proxy.
305 *
306 * **Example:** Generated name
307 * ```typescript
308 * const site = yield* Railway.Project("Site");
309 * const db = yield* Railway.mongo("Db", { project: site });
310 * ```
311 *
312 * :::caution[Changing `project` replaces Mongo]
313 * A new service + volume are created in the new Project. The old
314 * service and volume are deleted. Data is not copied.
315 * :::
316 *
317 * ### Connect from a Service
318 * Yield `ConnectMongo` inside init. Provide
319 * {@link ConnectMongoHttp}. Pass `conn.connectionString` to the
320 * MongoDB driver (or {@link pingMongo}). The binding packs the private
321 * URI (`{name}.railway.internal`).
322 *
323 * **Example:** Bind and ping
324 * ```typescript
325 * import * as HttpServerResponse from "effect/http/HttpServerResponse";
326 * import * as Redacted from "effect/Redacted";
327 *
328 * export default class Api extends Railway.Service<Api>()(
329 * "Api",
330 * { project: Site, main: import.meta.url, build: { install: ["mongodb"] } },
331 * Effect.gen(function* () {
332 * const conn = yield* Railway.ConnectMongo(Db);
333 * return {
334 * fetch: Effect.gen(function* () {
335 * const url = yield* conn.connectionString;
336 * const ping = yield* Railway.pingMongo(Redacted.value(url));
337 * return HttpServerResponse.json(ping);
338 * }),
339 * };
340 * }).pipe(Effect.provide(Railway.ConnectMongoHttp)),
341 * ) {}
342 * ```
343 *
344 * ### Public TCP
345 * `public` (default `true`) creates a TCP proxy on 27017.
346 * `publicConnectionUri` is `{domain}:{proxyPort}` for laptop access.
347 *
348 * **Example:** Private only
349 * ```typescript
350 * const db = yield* Railway.mongo("Db", {
351 * project: site,
352 * public: false,
353 * });
354 * ```
355 *
356 * ### Image
357 * Default is official Mongo 8. Pass `image` to pin another tag.
358 *
359 * **Example:** Mongo 7
360 * ```typescript
361 * const db = yield* Railway.mongo("Db", {
362 * project: site,
363 * image: "mongo:7",
364 * });
365 * ```
366 *
367 * ### Module-scope declarations
368 * Resource-valued props accept the resource or an Effect producing it.
369 *
370 * **Example:** Module-scope Mongo
371 * ```typescript
372 * // src/db.ts
373 * import * as Railway from "@alchemy.run/railway";
374 *
375 * export const Site = Railway.Project("Site");
376 * export const Db = Railway.mongo("Db", { project: Site });
377 * ```
378 *
379 * @resource
380 * @product MongoDB
381 */
382 export const Mongo: typeof MongoResource = Object.assign(
383 (id: string, props: MongoProps | Effect.Effect<MongoProps, never, Providers>) =>
384 MongoResource(id, resolveMongoProps(props)),
385 MongoResource,
386 );
387
388 /**
389 * IaC helper matching Railway's `mongo()`. Same resource as {@link Mongo}.
390 */
391 export const mongo = Mongo;
392
393 export class MongoNotCreated extends Data.TaggedError("Railway.MongoNotCreated")<{
394 resourceName: string;
395 projectId: string;
396 }> {}
397
398 export class MongoProjectRequired extends Data.TaggedError("Railway.MongoProjectRequired")<{
399 message: string;
400 }> {}
401
402 export class MongoDeployFailed extends Data.TaggedError("Railway.MongoDeployFailed")<{
403 serviceId: string;
404 status: string;
405 deploymentId: string | undefined;
406 }> {}
407
408 export class MongoVolumeNotCreated extends Data.TaggedError("Railway.MongoVolumeNotCreated")<{
409 resourceName: string;
410 serviceId: string;
411 }> {}
412
413 export class MongoCommandError extends Data.TaggedError("Railway.MongoCommandError")<{
414 cause: unknown;
415 }> {
416 override get message() {
417 return String(this.cause);
418 }
419 }
420
421 class MongoPending extends Data.TaggedError("Railway.MongoPending")<{
422 serviceId: string;
423 status: string;
424 }> {}
425
426 class MongoDeployPending extends Data.TaggedError("Railway.MongoDeployPending")<{
427 serviceId: string;
428 status: string;
429 }> {}
430
431 class VolumePending extends Data.TaggedError("Railway.MongoVolumePending")<{
432 volumeId: string;
433 state: string;
434 }> {}
435
436 const projectIdOf = (value: unknown): string | undefined => {
437 if (value === null || typeof value !== "object") return undefined;
438 const rec = value as { projectId?: unknown };
439 return typeof rec.projectId === "string" && rec.projectId.length > 0 ? rec.projectId : undefined;
440 };
441
442 const environmentIdOf = (value: unknown): string | undefined => {
443 if (value === null || typeof value !== "object") return undefined;
444 const rec = value as { environmentId?: unknown };
445 return typeof rec.environmentId === "string" && rec.environmentId.length > 0
446 ? rec.environmentId
447 : undefined;
448 };
449
450 const unwrapSecret = (value: Redacted.Redacted<string> | string): string =>
451 Redacted.isRedacted(value) ? Redacted.value(value) : value;
452
453 const generatePassword = Effect.sync(() => {
454 const bytes = randomBytes(24);
455 return Array.from(bytes, (b) => b.toString(16).padStart(2, "0")).join("");
456 });
457
458 const isGoneService = (service: CloudService | undefined) =>
459 service === undefined || service.deletedAt != null;
460
461 const isGoneInstance = (instance: ServiceInstanceResponse | undefined) =>
462 instance === undefined || instance.deletedAt != null;
463
464 const goneVolumeState = (state: VolumeState | null | undefined) =>
465 state === "DELETED" || state === "DELETING";
466
467 const transientVolumeState = (state: VolumeState | null | undefined) =>
468 state === "UPDATING" ||
469 state === "MIGRATING" ||
470 state === "MIGRATION_PENDING" ||
471 state === "RESTORING";
472
473 const isGoneVolume = (instance: CloudInstance | undefined) =>
474 instance === undefined ||
475 instance.deletedAt != null ||
476 instance.isPendingDeletion ||
477 goneVolumeState(instance.state);
478
479 const isGoneProxy = (proxy: CloudProxy | undefined) =>
480 proxy === undefined || proxy.deletedAt != null || proxy.syncStatus === "DELETED";
481
482 const normalizeDomain = (domain: string) => domain.replace(/\.+$/, "");
483
484 const sameImage = (observed: string | null | undefined, desired: string) => {
485 if (observed == null || observed.length === 0) return false;
486 if (observed === desired) return true;
487 if (observed === `${desired}:latest` || desired === `${observed}:latest`) {
488 return true;
489 }
490 return observed.endsWith(`/${desired}`) || observed.endsWith(`/${desired}:latest`);
491 };
492
493 const normalizeStart = (value: string | null | undefined) =>
494 (value ?? "").replace(/\s+/g, " ").trim();
495
496 const isMongoImage = (image: string | null | undefined) =>
497 image != null && /(^|\/)mongo(:|$)/i.test(image);
498
499 const deployReady = (status: string | undefined) => status === "SUCCESS";
500
501 const deployFailed = (status: string | undefined) =>
502 status === "FAILED" || status === "CRASHED" || status === "REMOVED";
503
504 const resolveName = (id: string, name: string | undefined, existing?: string) =>
505 Effect.gen(function* () {
506 if (name !== undefined) return name;
507 if (existing !== undefined) return existing;
508 return yield* createRailwayName(id);
509 });
510
511 const encodePart = (value: string) => encodeURIComponent(value);
512
513 const privateConnectionUri = (input: {
514 user: string;
515 password: string;
516 name: string;
517 database: string;
518 }): string =>
519 `mongodb://${encodePart(input.user)}:${encodePart(input.password)}@${input.name}.railway.internal:${MONGO_PORT}/${encodePart(input.database)}?authSource=admin&directConnection=true`;
520
521 const publicConnectionUri = (input: {
522 user: string;
523 password: string;
524 domain: string;
525 port: number;
526 database: string;
527 }): string =>
528 `mongodb://${encodePart(input.user)}:${encodePart(input.password)}@${input.domain}:${input.port}/${encodePart(input.database)}?authSource=admin&directConnection=true`;
529
530 const desiredVariables = (input: {
531 user: string;
532 password: string;
533 database: string;
534 }): Record<string, string> => ({
535 MONGO_INITDB_ROOT_USERNAME: input.user,
536 MONGO_INITDB_ROOT_PASSWORD: input.password,
537 MONGO_INITDB_DATABASE: input.database,
538 MONGOUSER: "${{MONGO_INITDB_ROOT_USERNAME}}",
539 MONGOPASSWORD: "${{MONGO_INITDB_ROOT_PASSWORD}}",
540 MONGOHOST: "${{RAILWAY_PRIVATE_DOMAIN}}",
541 MONGOPORT: String(MONGO_PORT),
542 [MONGO_URL_SECRET]:
543 "mongodb://${{MONGO_INITDB_ROOT_USERNAME}}:${{MONGO_INITDB_ROOT_PASSWORD}}@${{RAILWAY_PRIVATE_DOMAIN}}:27017",
544 [MONGO_PUBLIC_URL_SECRET]:
545 "mongodb://${{MONGO_INITDB_ROOT_USERNAME}}:${{MONGO_INITDB_ROOT_PASSWORD}}@${{RAILWAY_TCP_PROXY_DOMAIN}}:${{RAILWAY_TCP_PROXY_PORT}}",
546 });
547
548 const toAttrs = (input: {
549 service: CloudService;
550 instance: CloudAttributeInstance | undefined;
551 volume: CloudInstance | undefined;
552 proxy: CloudProxy | undefined;
553 projectId: string;
554 environmentId: string;
555 user: string;
556 password: string;
557 database: string;
558 }): Mongo["Attributes"] => {
559 const name = input.service.name;
560 const domain = input.proxy !== undefined ? normalizeDomain(input.proxy.domain) : undefined;
561 const proxyPort = input.proxy?.proxyPort;
562 return {
563 serviceId: input.service.id,
564 name,
565 projectId: input.projectId,
566 environmentId: input.environmentId,
567 image: input.instance?.source?.image ?? undefined,
568 region: input.instance?.region ?? undefined,
569 volumeId: input.volume?.volumeId || input.volume?.volume.id || "",
570 volumeInstanceId: input.volume?.id ?? "",
571 tcpProxyId: input.proxy?.id,
572 tcpProxyDomain: domain,
573 tcpProxyPort: proxyPort,
574 user: input.user,
575 database: input.database,
576 connectionUri:
577 input.password.length > 0
578 ? privateConnectionUri({
579 user: input.user,
580 password: input.password,
581 name,
582 database: input.database,
583 })
584 : "",
585 publicConnectionUri:
586 input.password.length > 0 &&
587 domain !== undefined &&
588 domain.length > 0 &&
589 proxyPort !== undefined
590 ? publicConnectionUri({
591 user: input.user,
592 password: input.password,
593 domain,
594 port: proxyPort,
595 database: input.database,
596 })
597 : "",
598 deploymentId: input.instance?.latestDeployment?.id,
599 deploymentStatus: input.instance?.latestDeployment?.status,
600 };
601 };
602
603 const getById = (serviceId: string) =>
604 readService(serviceId).pipe(
605 Effect.map((service) => (isGoneService(service) ? undefined : service)),
606 Effect.catchTag("RailwayNotFound", () => Effect.undefined),
607 );
608
609 const getInstance = (environmentId: string, serviceId: string) =>
610 readServiceInstance(environmentId, serviceId).pipe(
611 Effect.map((instance) => (isGoneInstance(instance) ? undefined : instance)),
612 Effect.catchTag("RailwayNotFound", () => Effect.undefined),
613 );
614
615 const listProjectServices = (projectId: string) =>
616 projectServices(projectId, serviceFields).pipe(
617 Effect.map((services) => services.filter((node) => !isGoneService(node))),
618 Effect.catchTag("RailwayNotFound", () => Effect.succeed([] as CloudService[])),
619 );
620
621 const findByName = (projectId: string, name: string) =>
622 listProjectServices(projectId).pipe(
623 Effect.map((services) => services.find((service) => service.name === name)),
624 );
625
626 const getVolumeByInstanceId = (volumeInstanceId: string) =>
627 readVolumeInstance(volumeInstanceId).pipe(
628 Effect.map((instance) => (isGoneVolume(instance) ? undefined : instance)),
629 Effect.catchTag("RailwayNotFound", () => Effect.undefined),
630 );
631
632 const listVolumeInstances = (environmentId: string, projectId: string) =>
633 environmentVolumes(environmentId, projectId, volumeFields).pipe(
634 Effect.map((instances) => instances.filter((node) => !isGoneVolume(node))),
635 Effect.catchTag("RailwayNotFound", () => Effect.succeed([] as CloudInstance[])),
636 );
637
638 const findVolume = (
639 environmentId: string,
640 projectId: string,
641 match: (instance: CloudInstance) => boolean,
642 ) =>
643 listVolumeInstances(environmentId, projectId).pipe(
644 Effect.map((instances) => instances.find(match)),
645 );
646
647 const listProxies = (environmentId: string, serviceId: string) =>
648 readProxies(environmentId, serviceId).pipe(
649 Effect.map((items) => items.filter((proxy) => !isGoneProxy(proxy))),
650 Effect.catchTag("RailwayNotFound", () => Effect.succeed([] as CloudProxy[])),
651 );
652
653 const findProxy = (environmentId: string, serviceId: string, applicationPort: number) =>
654 listProxies(environmentId, serviceId).pipe(
655 Effect.map((items) => items.find((proxy) => proxy.applicationPort === applicationPort)),
656 );
657
658 /**
659 * Delete a TCP proxy, riding out Railway's per-environment mutation lock: a
660 * delete racing an in-flight deploy fails with "Cannot delete TCP proxy: an
661 * operation is already in progress". Already-gone proxies are a no-op.
662 */
663 const deleteProxy = (id: string) =>
664 tcpProxyDelete(id).pipe(
665 Effect.retry({
666 while: (e) => e._tag === "RailwayOperationInProgress",
667 schedule: Schedule.spaced("3 seconds"),
668 times: 10,
669 }),
670 Effect.catchTag("RailwayNotFound", () => Effect.void),
671 Effect.asVoid,
672 );
673
674 const asVariableMap = (value: unknown): Record<string, string> => {
675 if (value === null || typeof value !== "object" || Array.isArray(value)) {
676 return {};
677 }
678 const out: Record<string, string> = {};
679 for (const [key, item] of Object.entries(value)) {
680 if (typeof item === "string") {
681 out[key] = item;
682 }
683 }
684 return out;
685 };
686
687 const listVariableMap = (projectId: string, environmentId: string, serviceId: string) =>
688 readVariables(projectId, environmentId, serviceId).pipe(
689 Effect.map(asVariableMap),
690 Effect.catchTag("RailwayNotFound", () => Effect.succeed({} as Record<string, string>)),
691 );
692
693 const upsertVariable = (input: {
694 projectId: string;
695 environmentId: string;
696 serviceId: string;
697 name: string;
698 value: string;
699 }) => variableUpsert(input);
700
701 const syncEnv = Effect.fn(function* (input: {
702 projectId: string;
703 environmentId: string;
704 serviceId: string;
705 desired: Record<string, string>;
706 }) {
707 const observed = yield* listVariableMap(input.projectId, input.environmentId, input.serviceId);
708 let changed = false;
709 for (const [name, value] of Object.entries(input.desired)) {
710 if (observed[name] !== value) {
711 yield* upsertVariable({
712 projectId: input.projectId,
713 environmentId: input.environmentId,
714 serviceId: input.serviceId,
715 name,
716 value,
717 });
718 changed = true;
719 }
720 }
721 return changed;
722 });
723
724 const waitForInstance = (environmentId: string, serviceId: string) =>
725 getInstance(environmentId, serviceId).pipe(
726 Effect.flatMap((instance) => {
727 if (instance === undefined) {
728 return Effect.fail(new MongoPending({ serviceId, status: "creating" }));
729 }
730 return Effect.succeed(instance);
731 }),
732 Effect.retry({
733 while: (e) => e._tag === "Railway.MongoPending",
734 // serviceCreate fans the instance out to each environment
735 // asynchronously; wait a bounded interval for it to appear.
736 times: 10,
737 schedule: Schedule.spaced("2 seconds"),
738 }),
739 Effect.catchTag("Railway.MongoPending", () => getInstance(environmentId, serviceId)),
740 );
741
742 const waitForDeployment = (environmentId: string, serviceId: string) =>
743 getInstance(environmentId, serviceId).pipe(
744 Effect.flatMap((instance) => {
745 const latest = instance?.latestDeployment;
746 const status = latest?.status;
747 if (instance !== undefined && deployReady(status)) {
748 return Effect.succeed(instance);
749 }
750 return Effect.fail(new MongoDeployPending({ serviceId, status: status ?? "pending" }));
751 }),
752 Effect.retry({
753 while: (e) => e._tag === "Railway.MongoDeployPending",
754 times: 10,
755 schedule: Schedule.spaced("5 seconds"),
756 }),
757 Effect.catchTag("Railway.MongoDeployPending", () => getInstance(environmentId, serviceId)),
758 );
759
760 const waitForVolume = (
761 environmentId: string,
762 projectId: string,
763 volumeId: string,
764 volumeInstanceId?: string,
765 ) => {
766 const observe =
767 volumeInstanceId !== undefined && volumeInstanceId.length > 0
768 ? getVolumeByInstanceId(volumeInstanceId)
769 : findVolume(environmentId, projectId, (instance) => instance.volumeId === volumeId);
770 return observe.pipe(
771 Effect.flatMap((instance) => {
772 if (instance === undefined || transientVolumeState(instance.state)) {
773 return Effect.fail(new VolumePending({ volumeId, state: instance?.state ?? "creating" }));
774 }
775 return Effect.succeed(instance);
776 }),
777 Effect.retry({
778 while: (e) => e._tag === "Railway.MongoVolumePending",
779 times: 10,
780 schedule: Schedule.spaced("3 seconds"),
781 }),
782 Effect.catchTag("Railway.MongoVolumePending", () => observe),
783 );
784 };
785
786 const stampVolumeName = (volumeId: string, name: string) => volumeUpdate(volumeId, name);
787
788 /**
789 * Ping a MongoDB deployment. Used from tests over the public TCP proxy
790 * and from a {@link Service} with {@link ConnectMongo}.
791 */
792 export const pingMongo = (url: string): Effect.Effect<{ ok: number }, MongoCommandError> =>
793 Effect.tryPromise({
794 try: async () => {
795 const { MongoClient } = await import("mongodb");
796 const client = new MongoClient(url, {
797 directConnection: true,
798 serverSelectionTimeoutMS: 8000,
799 });
800 try {
801 await client.connect();
802 const result = await client.db("admin").command({ ping: 1 });
803 return { ok: Number(result.ok) };
804 } finally {
805 await client.close().catch(() => {});
806 }
807 },
808 catch: (cause) => new MongoCommandError({ cause }),
809 });
810
811 export const MongoProvider = () =>
812 Provider.succeed(Mongo, {
813 stables: ["serviceId", "projectId", "environmentId", "volumeId"],
814 nuke: { dependsOn: ["Railway.Project"] },
815
816 diff: Effect.fn(function* ({ news, output }) {
817 if (news === undefined || !isResolved(news)) return undefined;
818 if (output === undefined) return undefined;
819 const nextProject = projectIdOf(news.project);
820 const projectChanged = nextProject !== undefined && nextProject !== output.projectId;
821 const nextEnv = environmentIdOf(news.environment);
822 const environmentChanged = nextEnv !== undefined && nextEnv !== output.environmentId;
823 const regionChanged = news.region !== undefined && news.region !== output.region;
824 if (projectChanged || environmentChanged || regionChanged) {
825 return { action: "replace" as const };
826 }
827 return undefined;
828 }),
829
830 read: Effect.fn(function* ({ id, olds, output }) {
831 const projectId =
832 output?.projectId ?? (olds !== undefined ? projectIdOf(olds.project) : undefined);
833 const environmentId =
834 output?.environmentId ??
835 (olds !== undefined
836 ? (environmentIdOf(olds.environment) ?? environmentIdOf(olds.project))
837 : undefined);
838 const name = yield* resolveName(id, olds?.name, output?.name);
839 const byId =
840 output?.serviceId !== undefined && output.serviceId.length > 0
841 ? yield* getById(output.serviceId)
842 : undefined;
843 const found =
844 byId ?? (projectId !== undefined ? yield* findByName(projectId, name) : undefined);
845 if (found === undefined) return undefined;
846 const resolvedProjectId = projectIdOf(found) ?? projectId ?? "";
847 const resolvedEnvId =
848 environmentId ?? environmentIdOf(olds?.project) ?? output?.environmentId ?? "";
849 const instance =
850 resolvedEnvId.length > 0 ? yield* getInstance(resolvedEnvId, found.id) : undefined;
851 const volume =
852 output?.volumeInstanceId !== undefined && output.volumeInstanceId.length > 0
853 ? yield* getVolumeByInstanceId(output.volumeInstanceId)
854 : resolvedEnvId.length > 0 && resolvedProjectId.length > 0
855 ? yield* findVolume(
856 resolvedEnvId,
857 resolvedProjectId,
858 (row) =>
859 (row.serviceId ?? undefined) === found.id ||
860 (output?.volumeId !== undefined && row.volumeId === output.volumeId),
861 )
862 : undefined;
863 const proxy =
864 resolvedEnvId.length > 0
865 ? yield* findProxy(resolvedEnvId, found.id, MONGO_PORT)
866 : undefined;
867 const vars =
868 resolvedProjectId.length > 0 && resolvedEnvId.length > 0
869 ? yield* listVariableMap(resolvedProjectId, resolvedEnvId, found.id)
870 : {};
871 const attrs = toAttrs({
872 service: found,
873 instance,
874 volume,
875 proxy,
876 projectId: resolvedProjectId,
877 environmentId: resolvedEnvId,
878 user: vars.MONGO_INITDB_ROOT_USERNAME ?? output?.user ?? DEFAULT_MONGO_USER,
879 password: vars.MONGO_INITDB_ROOT_PASSWORD ?? "",
880 database: vars.MONGO_INITDB_DATABASE ?? output?.database ?? DEFAULT_MONGO_DATABASE,
881 });
882 if (output !== undefined) return attrs;
883 return matchesAlchemyPhysicalName(found.name) ? attrs : Unowned(attrs);
884 }),
885
886 list: Effect.fn(function* () {
887 const projects = yield* ownedProjects();
888 const rows = yield* Effect.forEach(projects, (project) =>
889 Effect.gen(function* () {
890 const services = new Map(
891 (yield* listProjectServices(project.projectId))
892 .filter((service) => matchesAlchemyPhysicalName(service.name))
893 .map((service) => [service.id, service]),
894 );
895 if (services.size === 0) return [];
896 const envIds = yield* liveEnvironments(project.projectId).pipe(
897 Stream.filter((env) => env.deletedAt == null),
898 Stream.map((env) => env.id),
899 Stream.runCollect,
900 Effect.catchTag("RailwayNotFound", () => Effect.succeed([])),
901 );
902 const items = yield* Effect.forEach(envIds, (environmentId) =>
903 Effect.gen(function* () {
904 const instances = (yield* environmentServiceInstances(
905 environmentId,
906 project.projectId,
907 (instance) => ({
908 ...attributeInstanceFields(instance),
909 serviceId: instance.serviceId,
910 deletedAt: instance.deletedAt,
911 }),
912 )).filter(
913 (instance) =>
914 instance.deletedAt == null &&
915 services.has(instance.serviceId) &&
916 isMongoImage(instance.source?.image),
917 );
918 if (instances.length === 0) return [];
919 const volumes = yield* listVolumeInstances(environmentId, project.projectId);
920 return instances.flatMap((instance) => {
921 const service = services.get(instance.serviceId);
922 return service === undefined
923 ? []
924 : [
925 toAttrs({
926 service,
927 instance,
928 projectId: project.projectId,
929 environmentId,
930 volume: volumes.find((row) => row.serviceId === service.id),
931 proxy: undefined,
932 user: DEFAULT_MONGO_USER,
933 password: "",
934 database: DEFAULT_MONGO_DATABASE,
935 }),
936 ];
937 });
938 }),
939 );
940 return items.flat();
941 }),
942 );
943 return rows.flat();
944 }),
945
946 reconcile: Effect.fn(function* ({ id, news, output }) {
947 const props = news ?? ({} as MongoProps);
948 const projectId = projectIdOf(props.project) ?? output?.projectId;
949 if (projectId === undefined) {
950 return yield* new MongoProjectRequired({
951 message: "Mongo requires a resolved Railway.Project",
952 });
953 }
954 const environmentId =
955 environmentIdOf(props.environment) ??
956 environmentIdOf(props.project) ??
957 output?.environmentId;
958 if (environmentId === undefined) {
959 return yield* new MongoProjectRequired({
960 message:
961 "Mongo requires a Railway environment (pass environment or a Project with environmentId)",
962 });
963 }
964 const name = yield* resolveName(id, props.name, output?.name);
965 const sourceImage = props.image ?? DEFAULT_MONGO_IMAGE;
966 const wantPublic = props.public !== false;
967 const volumeName = yield* createRailwayName(`${id}-data`);
968
969 let current: CloudService | undefined =
970 output?.serviceId !== undefined && output.serviceId.length > 0
971 ? yield* getById(output.serviceId)
972 : undefined;
973 if (current === undefined) {
974 current = yield* findByName(projectId, name);
975 }
976
977 const existingVars =
978 current !== undefined ? yield* listVariableMap(projectId, environmentId, current.id) : {};
979 const user = existingVars.MONGO_INITDB_ROOT_USERNAME ?? props.user ?? DEFAULT_MONGO_USER;
980 const database =
981 existingVars.MONGO_INITDB_DATABASE ?? props.database ?? DEFAULT_MONGO_DATABASE;
982 const password =
983 existingVars.MONGO_INITDB_ROOT_PASSWORD ??
984 (props.password !== undefined ? unwrapSecret(props.password) : undefined) ??
985 (yield* generatePassword);
986 const variables = desiredVariables({ user, password, database });
987
988 if (current === undefined) {
989 const created = yield* serviceCreate({
990 input: { projectId, environmentId, name, source: { image: sourceImage }, variables },
991 }).pipe(Effect.catchTag("RailwayValidationError", () => Effect.undefined));
992 current = created ?? (yield* findByName(projectId, name));
993 }
994
995 if (current === undefined || isGoneService(current)) {
996 return yield* new MongoNotCreated({ resourceName: name, projectId });
997 }
998
999 if (current.name !== name) {
1000 current = yield* serviceUpdate({ id: current.id, input: { name } });
1001 }
1002
1003 let instance = yield* waitForInstance(environmentId, current.id);
1004 let needsDeploy = false;
1005
1006 const observedImage = instance?.source?.image ?? undefined;
1007 const imageChanged = sourceImage !== undefined && !sameImage(observedImage, sourceImage);
1008 const observedRegion = instance?.region ?? undefined;
1009 const regionChanged = props.region !== undefined && props.region !== observedRegion;
1010 const sleepOn = instance?.sleepApplication !== false;
1011 const startChanged =
1012 normalizeStart(instance?.startCommand) !== normalizeStart(MONGO_START_COMMAND);
1013 if (imageChanged || regionChanged || sleepOn || startChanged) {
1014 yield* serviceInstanceUpdate({
1015 environmentId,
1016 serviceId: current.id,
1017 input: {
1018 ...(imageChanged ? { source: { image: sourceImage } } : {}),
1019 ...(regionChanged ? { region: props.region } : {}),
1020 ...(sleepOn ? { sleepApplication: false } : {}),
1021 ...(startChanged ? { startCommand: MONGO_START_COMMAND } : {}),
1022 },
1023 });
1024 needsDeploy = true;
1025 instance = (yield* getInstance(environmentId, current.id)) ?? instance;
1026 }
1027
1028 const envChanged = yield* syncEnv({
1029 projectId,
1030 environmentId,
1031 serviceId: current.id,
1032 desired: variables,
1033 });
1034 if (envChanged) needsDeploy = true;
1035
1036 let volume: CloudInstance | undefined =
1037 output?.volumeInstanceId !== undefined && output.volumeInstanceId.length > 0
1038 ? yield* getVolumeByInstanceId(output.volumeInstanceId)
1039 : undefined;
1040 if (volume === undefined && output?.volumeId !== undefined) {
1041 volume = yield* findVolume(
1042 environmentId,
1043 projectId,
1044 (row) => row.volumeId === output.volumeId,
1045 );
1046 }
1047 if (volume === undefined) {
1048 volume = yield* findVolume(
1049 environmentId,
1050 projectId,
1051 (row) => (row.serviceId ?? undefined) === current!.id || row.volume.name === volumeName,
1052 );
1053 }
1054 if (volume === undefined) {
1055 const created = yield* volumeCreate({
1056 input: {
1057 projectId,
1058 environmentId,
1059 mountPath: MONGO_MOUNT_PATH,
1060 serviceId: current.id,
1061 ...(props.region !== undefined ? { region: props.region } : {}),
1062 },
1063 });
1064 if (created.name !== volumeName) {
1065 yield* stampVolumeName(created.id, volumeName);
1066 }
1067 volume = yield* waitForVolume(environmentId, projectId, created.id);
1068 }
1069 if (volume === undefined || isGoneVolume(volume)) {
1070 return yield* new MongoVolumeNotCreated({
1071 resourceName: volumeName,
1072 serviceId: current.id,
1073 });
1074 }
1075 if (volume.volume.name !== volumeName) {
1076 yield* stampVolumeName(volume.volumeId, volumeName);
1077 }
1078 const observedMount = volume.mountPath;
1079 const observedServiceId = volume.serviceId ?? undefined;
1080 const mountChanged = observedMount !== MONGO_MOUNT_PATH;
1081 const attached = observedServiceId === current.id;
1082 if (mountChanged || !attached) {
1083 yield* volumeInstanceUpdate({
1084 volumeId: volume.volumeId,
1085 environmentId,
1086 input: {
1087 ...(mountChanged ? { mountPath: MONGO_MOUNT_PATH } : {}),
1088 ...(!attached ? { serviceId: current.id } : {}),
1089 },
1090 });
1091 volume = (yield* waitForVolume(environmentId, projectId, volume.volumeId)) ?? volume;
1092 needsDeploy = true;
1093 }
1094
1095 let proxy = yield* findProxy(environmentId, current.id, MONGO_PORT);
1096 if (wantPublic && proxy === undefined) {
1097 const created = yield* tcpProxyCreate({
1098 input: { applicationPort: MONGO_PORT, environmentId, serviceId: current.id },
1099 }).pipe(Effect.catchTag("RailwayValidationError", () => Effect.undefined));
1100 proxy =
1101 created !== undefined && !isGoneProxy(created)
1102 ? created
1103 : yield* findProxy(environmentId, current.id, MONGO_PORT);
1104 }
1105 if (!wantPublic && proxy !== undefined) {
1106 yield* deleteProxy(proxy.id);
1107 proxy = undefined;
1108 }
1109
1110 if (needsDeploy || instance?.latestDeployment == null) {
1111 yield* serviceInstanceDeployV2({ environmentId, serviceId: current.id }).pipe(
1112 Effect.catchTag("RailwayValidationError", () => Effect.void),
1113 );
1114 }
1115
1116 instance = (yield* waitForDeployment(environmentId, current.id)) ?? instance;
1117 let finalStatus = instance?.latestDeployment?.status;
1118 // A deployment can wedge in DEPLOYING and never reach SUCCESS — the
1119 // container may serve, but Railway keeps its per-environment operation
1120 // lock and the TCP proxy's routing is not committed. Converge: cancel
1121 // the wedged deployment, redeploy once, and insist on SUCCESS.
1122 if (!deployFailed(finalStatus) && !deployReady(finalStatus)) {
1123 const wedged = instance?.latestDeployment?.id;
1124 if (wedged != null && wedged.length > 0) {
1125 yield* deploymentCancel({ id: wedged }).pipe(
1126 Effect.catchTag("RailwayNotFound", () => Effect.void),
1127 );
1128 }
1129 yield* serviceInstanceDeployV2({ environmentId, serviceId: current.id }).pipe(
1130 Effect.catchTag("RailwayValidationError", () => Effect.void),
1131 );
1132 instance = (yield* waitForDeployment(environmentId, current.id)) ?? instance;
1133 finalStatus = instance?.latestDeployment?.status;
1134 }
1135 if (deployFailed(finalStatus) || !deployReady(finalStatus)) {
1136 return yield* new MongoDeployFailed({
1137 serviceId: current.id,
1138 status: finalStatus ?? "failed",
1139 deploymentId: instance?.latestDeployment?.id,
1140 });
1141 }
1142
1143 return toAttrs({
1144 service: current,
1145 instance,
1146 volume,
1147 proxy,
1148 projectId,
1149 environmentId,
1150 user,
1151 password,
1152 database,
1153 });
1154 }),
1155
1156 delete: Effect.fn(function* ({ output }) {
1157 const serviceId = output.serviceId;
1158 const environmentId = output.environmentId;
1159 // Cancel a still-running deployment first: it holds Railway's
1160 // per-environment operation lock ("Cannot delete TCP proxy: an
1161 // operation is already in progress") and can stall the service
1162 // teardown indefinitely. A finished deployment makes this a no-op.
1163 if (environmentId.length > 0 && serviceId.length > 0) {
1164 const instance = yield* getInstance(environmentId, serviceId);
1165 const latest = instance?.latestDeployment;
1166 if (
1167 latest?.id != null &&
1168 latest.id.length > 0 &&
1169 !deployReady(latest.status) &&
1170 !deployFailed(latest.status)
1171 ) {
1172 yield* deploymentCancel({ id: latest.id }).pipe(
1173 Effect.catchTag("RailwayNotFound", () => Effect.void),
1174 );
1175 }
1176 }
1177 // Delete the SERVICE next — its teardown cascades onto the proxies.
1178 if (serviceId.length > 0) {
1179 yield* serviceDelete({ id: serviceId }).pipe(
1180 Effect.catchTag("RailwayNotFound", () => Effect.void),
1181 );
1182 yield* waitUntilDeleted(
1183 "Service",
1184 serviceId,
1185 getById(serviceId).pipe(Effect.map((service) => service === undefined)),
1186 );
1187 }
1188 // Proxies usually disappear with the service; wait for the cascade
1189 // instead of fighting the teardown's lock, then force-delete any
1190 // survivor (which no-ops on NotFound).
1191 if (environmentId.length > 0 && serviceId.length > 0) {
1192 const leftover = yield* listProxies(environmentId, serviceId).pipe(
1193 Effect.repeat({
1194 schedule: Schedule.spaced("3 seconds"),
1195 until: (rows) => rows.length === 0,
1196 times: 10,
1197 }),
1198 );
1199 yield* Effect.forEach(leftover, (proxy) => deleteProxy(proxy.id), { concurrency: 4 });
1200 yield* waitUntilDeleted(
1201 "TcpProxy",
1202 serviceId,
1203 listProxies(environmentId, serviceId).pipe(Effect.map((proxies) => proxies.length === 0)),
1204 );
1205 } else if (output.tcpProxyId !== undefined && output.tcpProxyId.length > 0) {
1206 yield* deleteProxy(output.tcpProxyId);
1207 }
1208 if (output.volumeId.length > 0) {
1209 yield* volumeDelete({ volumeId: output.volumeId }).pipe(
1210 Effect.catchTag("RailwayNotFound", () => Effect.void),
1211 );
1212 const check =
1213 output.volumeInstanceId.length > 0
1214 ? getVolumeByInstanceId(output.volumeInstanceId).pipe(
1215 Effect.map((instance) => instance === undefined),
1216 )
1217 : Effect.succeed(true);
1218 yield* waitUntilDeleted("Volume", output.volumeId, check);
1219 }
1220 }),
1221 });