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

main
ts 234 lines 8.29 KB
Raw
1 import * as appconfig from "@distilled.cloud/aws/appconfig";
2 import { isResolved } from "alchemy/Diff";
3 import * as Provider from "alchemy/Provider";
4 import { Resource } from "alchemy/Resource";
5 import * as Data from "effect/Data";
6 import * as Effect from "effect/Effect";
7 import * as Schedule from "effect/Schedule";
8 import type { Providers } from "../Providers.ts";
9
10 export interface DeploymentProps {
11 /**
12 * ID of the application.
13 */
14 applicationId: string;
15 /**
16 * ID of the environment to deploy to.
17 */
18 environmentId: string;
19 /**
20 * ID of the deployment strategy that governs the rollout.
21 */
22 deploymentStrategyId: string;
23 /**
24 * ID of the configuration profile being deployed.
25 */
26 configurationProfileId: string;
27 /**
28 * The configuration version to deploy. For a hosted configuration profile
29 * this is the hosted version number (as a string); for other sources it is
30 * the version identifier defined by that source.
31 */
32 configurationVersion: string;
33 /**
34 * Description of the deployment.
35 */
36 description?: string;
37 }
38
39 export interface Deployment extends Resource<
40 "AWS.AppConfig.Deployment",
41 DeploymentProps,
42 {
43 applicationId: string;
44 environmentId: string;
45 deploymentNumber: number;
46 configurationProfileId: string;
47 configurationVersion: string;
48 state: string;
49 },
50 never,
51 Providers
52 > {}
53
54 /**
55 * An AWS AppConfig deployment — releases a configuration version to an
56 * environment following a deployment strategy. Deployments are immutable and
57 * asynchronous: the provider starts the deployment and waits (bounded) for it
58 * to reach a terminal state. Any change to the deployed version, strategy, or
59 * target creates a new deployment (a replacement). Use an all-at-once strategy
60 * (duration 0, bake 0) for a near-instant rollout.
61 *
62 * ### Deploying a Configuration
63 * **Example:** Deploy a Hosted Version
64 * ```typescript
65 * const deployment = yield* AppConfig.Deployment("Rollout", {
66 * applicationId: app.applicationId,
67 * environmentId: env.environmentId,
68 * deploymentStrategyId: strategy.deploymentStrategyId,
69 * configurationProfileId: profile.configurationProfileId,
70 * configurationVersion: String(version.versionNumber),
71 * });
72 * ```
73 *
74 * @resource
75 */
76 export const Deployment = Resource<Deployment>("AWS.AppConfig.Deployment");
77
78 class DeploymentNotSettled extends Data.TaggedError("DeploymentNotSettled")<{
79 readonly deploymentNumber: number;
80 readonly state: string;
81 }> {}
82
83 const TERMINAL_STATES = new Set(["COMPLETE", "ROLLED_BACK", "REVERTED"]);
84
85 export const DeploymentProvider = () =>
86 Provider.effect(
87 Deployment,
88 Effect.gen(function* () {
89 const readDeployment = Effect.fn(function* (
90 applicationId: string,
91 environmentId: string,
92 deploymentNumber: number,
93 ) {
94 return yield* appconfig
95 .getDeployment({
96 ApplicationId: applicationId,
97 EnvironmentId: environmentId,
98 DeploymentNumber: deploymentNumber,
99 })
100 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined));
101 });
102
103 // Deployments roll out asynchronously; wait (bounded) for a terminal
104 // state. With an all-at-once strategy this settles in seconds.
105 const waitForSettled = Effect.fn(function* (
106 applicationId: string,
107 environmentId: string,
108 deploymentNumber: number,
109 ) {
110 return yield* readDeployment(applicationId, environmentId, deploymentNumber).pipe(
111 Effect.flatMap((deployment) =>
112 deployment !== undefined && !TERMINAL_STATES.has(deployment.State ?? "")
113 ? Effect.fail(
114 new DeploymentNotSettled({
115 deploymentNumber,
116 state: deployment.State ?? "",
117 }),
118 )
119 : Effect.succeed(deployment),
120 ),
121 Effect.retry({
122 while: (e) => e instanceof DeploymentNotSettled,
123 schedule: Schedule.max([Schedule.fixed("3 seconds"), Schedule.recurs(40)]),
124 }),
125 );
126 });
127
128 return {
129 stables: ["applicationId", "environmentId", "deploymentNumber"],
130
131 // Deployments are immutable — any change starts a new deployment.
132 diff: Effect.fn(function* ({ olds, news }) {
133 if (!isResolved(news)) return undefined;
134 if (
135 olds !== undefined &&
136 (olds.applicationId !== news.applicationId ||
137 olds.environmentId !== news.environmentId ||
138 olds.deploymentStrategyId !== news.deploymentStrategyId ||
139 olds.configurationProfileId !== news.configurationProfileId ||
140 olds.configurationVersion !== news.configurationVersion)
141 ) {
142 return { action: "replace" } as const;
143 }
144 }),
145
146 read: Effect.fn(function* ({ output }) {
147 if (output?.deploymentNumber === undefined) return undefined;
148 const deployment = yield* readDeployment(
149 output.applicationId,
150 output.environmentId,
151 output.deploymentNumber,
152 );
153 if (deployment?.DeploymentNumber === undefined) return undefined;
154 return {
155 applicationId: output.applicationId,
156 environmentId: output.environmentId,
157 deploymentNumber: deployment.DeploymentNumber,
158 configurationProfileId:
159 deployment.ConfigurationProfileId ?? output.configurationProfileId,
160 configurationVersion: deployment.ConfigurationVersion ?? output.configurationVersion,
161 state: deployment.State ?? "",
162 };
163 }),
164
165 reconcile: Effect.fn(function* ({ news, output, session }) {
166 // Observe — an interrupted create may already have started the
167 // deployment.
168 let observed =
169 output?.deploymentNumber !== undefined
170 ? yield* readDeployment(
171 news.applicationId,
172 news.environmentId,
173 output.deploymentNumber,
174 )
175 : undefined;
176
177 // Ensure — start the deployment if it does not exist yet. A
178 // concurrent deployment on the same environment rejects with
179 // ConflictException; retry (bounded) until it clears.
180 if (observed?.DeploymentNumber === undefined) {
181 observed = yield* appconfig
182 .startDeployment({
183 ApplicationId: news.applicationId,
184 EnvironmentId: news.environmentId,
185 DeploymentStrategyId: news.deploymentStrategyId,
186 ConfigurationProfileId: news.configurationProfileId,
187 ConfigurationVersion: news.configurationVersion,
188 Description: news.description,
189 })
190 .pipe(
191 Effect.retry({
192 while: (e) => e._tag === "ConflictException",
193 schedule: Schedule.max([Schedule.fixed("3 seconds"), Schedule.recurs(20)]),
194 }),
195 );
196 }
197
198 const number = observed.DeploymentNumber!;
199 const settled = yield* waitForSettled(news.applicationId, news.environmentId, number);
200
201 yield* session.note(`deployment-${number}`);
202 return {
203 applicationId: news.applicationId,
204 environmentId: news.environmentId,
205 deploymentNumber: number,
206 configurationProfileId: news.configurationProfileId,
207 configurationVersion: news.configurationVersion,
208 state: settled?.State ?? observed.State ?? "",
209 };
210 }),
211
212 delete: Effect.fn(function* ({ output }) {
213 // A completed deployment is historical and cannot be deleted;
214 // stopDeployment only affects an in-progress rollout. Treat a
215 // "nothing to stop" rejection as success.
216 yield* appconfig
217 .stopDeployment({
218 ApplicationId: output.applicationId,
219 EnvironmentId: output.environmentId,
220 DeploymentNumber: output.deploymentNumber,
221 })
222 .pipe(
223 Effect.catchTag(
224 ["ResourceNotFoundException", "BadRequestException"],
225 () => Effect.void,
226 ),
227 );
228 }),
229
230 // Deployments are keyed under their parent environment.
231 list: () => Effect.succeed([]),
232 };
233 }),
234 );