-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcdk-util.ts
More file actions
131 lines (112 loc) · 4.33 KB
/
Copy pathcdk-util.ts
File metadata and controls
131 lines (112 loc) · 4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
* Copyright (c) 2023-2025 - Restate Software, Inc., Restate GmbH
*
* This file is part of the Restate CDK Construct Library,
* which is released under the MIT license.
*
* You can find a copy of the license in file LICENSE in the root
* directory of this repository or package, or at
* https://github.com/restatedev/cdk/blob/main/LICENSE
*/
import { $, cd, path } from "zx";
export interface CdkStackProps {
stackName: string;
cdkAppPath: string;
context?: Record<string, string>;
}
interface StackOutput {
OutputKey: string;
OutputValue: string;
Description?: string;
}
export async function createStack(config: CdkStackProps): Promise<Record<string, string>> {
const noRollback = process.env["NO_ROLLBACK"] === "true";
const contextArgs = Object.entries(config.context ?? {}).flatMap(([key, value]) => ["--context", `${key}=${value}`]);
const extraArgs = noRollback ? ["--no-rollback"] : [];
cd(path.resolve(__dirname));
await $`npx cdk --app 'npx tsx ${config.cdkAppPath}' deploy ${config.stackName} \
--context stack_name=${config.stackName} \
${contextArgs} \
--output cdk.${config.stackName}.out \
--require-approval never \
${extraArgs}`.timeout("575s");
const result =
await $`aws cloudformation describe-stacks --stack-name "${config.stackName}" --query 'Stacks[0].Outputs'`;
const outputs: StackOutput[] = JSON.parse(result.stdout);
return outputs.reduce(
(acc, output) => ({
...acc,
[output.OutputKey]: output.OutputValue,
}),
{},
);
}
export async function destroyStackAsync(stackName: string) {
const retainStack = new Boolean(process.env["RETAIN_STACK"]).valueOf();
if (retainStack) {
console.log(`Retaining stack "${stackName}"`);
} else {
console.log(`Asynchronously deleting stack "${stackName}"...`);
await $`aws cloudformation delete-stack --stack-name "${stackName}"`;
}
}
export async function destroyStack(config: CdkStackProps) {
const retainStack = new Boolean(process.env["RETAIN_STACK"]).valueOf();
if (retainStack) {
console.log(`Retaining stack "${config.stackName}"`);
} else {
const contextArgs = Object.entries(config.context ?? {}).flatMap(([key, value]) => [
"--context",
`${key}=${value}`,
]);
await $`npx cdk --app 'npx tsx ${config.cdkAppPath}' destroy \
--context stack_name=${config.stackName} \
${contextArgs} \
--output "cdk.${config.stackName}.out" \
--force`.timeout("595s");
}
}
interface Deployment {
id: string;
endpoint?: string;
arn?: string;
created_at?: string;
}
async function runSsmCommand(instanceId: string, command: string): Promise<string> {
const result =
await $`aws ssm send-command --instance-ids ${instanceId} --document-name "AWS-RunShellScript" --parameters commands=${JSON.stringify([command])} --output json`;
const commandObj = JSON.parse(result.stdout);
const commandId = commandObj.Command.CommandId;
// Wait for command to complete (with timeout handling)
for (let i = 0; i < 30; i++) {
await new Promise((resolve) => setTimeout(resolve, 1000));
const statusResult =
await $`aws ssm get-command-invocation --command-id ${commandId} --instance-id ${instanceId} --output json`;
const status = JSON.parse(statusResult.stdout);
if (status.Status === "Success") {
return status.StandardOutputContent;
}
if (status.Status === "Failed") {
throw new Error(`SSM command failed: ${status.StandardErrorContent}`);
}
}
throw new Error("SSM command timed out");
}
export async function queryRestateDeployments(instanceId: string): Promise<Deployment[]> {
// Use SSM to query the admin API via curl from the instance
const output = await runSsmCommand(instanceId, "curl -s http://localhost:9070/deployments");
const response = JSON.parse(output) as { deployments: Deployment[] };
return response.deployments;
}
export async function queryRestateCloudDeployments(adminUrl: string, authToken: string): Promise<Deployment[]> {
const response = await fetch(`${adminUrl}/deployments`, {
headers: {
Authorization: `Bearer ${authToken}`,
},
});
if (!response.ok) {
throw new Error(`Failed to query deployments: ${response.status} ${response.statusText}`);
}
const data = (await response.json()) as { deployments: Deployment[] };
return data.deployments;
}