-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.tsx
249 lines (218 loc) · 6.92 KB
/
index.tsx
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
import * as inquirer from "inquirer";
import * as React from "react";
import { render, Text, Newline, Box } from "ink";
import Spinner from "ink-spinner";
import { s3 } from "@pulumi/aws";
import { PolicyDocument } from "@pulumi/aws/iam";
import {
EngineEvent,
InlineProgramArgs,
LocalWorkspace,
} from "@pulumi/pulumi/automation";
const green = "green";
const red = "red";
// This is our pulumi program in "inline function" form
const pulumiProgram = async () => {
// Create a bucket and expose a website index document
const siteBucket = new s3.Bucket("s3-website-bucket", {
website: {
indexDocument: "index.html",
},
});
const indexContent = `<html><head>
<title>Hello S3</title><meta charset="UTF-8">
</head>
<body><p>Hello, world!</p><p>Made with ❤️ with <a href="https://pulumi.com">Pulumi</a></p>
</body></html>
`;
// write our index.html into the site bucket
new s3.BucketObject("index", {
bucket: siteBucket,
content: indexContent,
contentType: "text/html; charset=utf-8",
key: "index.html",
});
// Create an S3 Bucket Policy to allow public read of all objects in bucket
function publicReadPolicyForBucket(bucketName: string): PolicyDocument {
return {
Version: "2012-10-17",
Statement: [
{
Effect: "Allow",
Principal: "*",
Action: ["s3:GetObject"],
Resource: [
`arn:aws:s3:::${bucketName}/*`, // policy refers to bucket name explicitly
],
},
],
};
}
// Set the access policy for the bucket so all objects are readable
new s3.BucketPolicy("bucketPolicy", {
bucket: siteBucket.bucket, // refer to the bucket created earlier
policy: siteBucket.bucket.apply(publicReadPolicyForBucket), // use output property `siteBucket.bucket`
});
return {
websiteUrl: siteBucket.websiteEndpoint,
};
};
const stackArgs: InlineProgramArgs = {
stackName: "dev",
projectName: "inlineNode",
program: pulumiProgram,
};
interface Answers {
destroy: boolean;
}
interface DoneProps {
error: boolean;
message: string;
}
const DoneMessage = (props: DoneProps) => {
if (props.error) {
return (
<Text color={red}>
<Newline />
{`❌ Failure! Error: ${props.message}`}
<Newline />
</Text>
);
}
return <Text color={green}>{`\n✅ ${props.message}\n`}</Text>;
};
interface ResourceUpdateListProps {
updatesInProgress: Record<string, string>;
updatesComplete: Record<string, string>;
}
const ResourceUpdateList = (props: ResourceUpdateListProps) => {
if (
Object.entries(props.updatesInProgress).length > 0 ||
Object.entries(props.updatesComplete).length > 0
) {
return (
<Box borderStyle="round" borderColor="green">
<Text bold={true}>
Updates in progress
<Newline />
</Text>
{Object.entries(props.updatesInProgress).map(([key, val]) => (
<Text
strikethrough={!!props.updatesComplete[key]}
key={key}
>
<Newline />
{val}
</Text>
))}
</Box>
);
}
return null;
};
interface InProgressProps {
message: string;
}
const InProgressMessage = (props: InProgressProps) => (
<Text>
<Newline />
<Text color={green}>
<Spinner type="dots" />
</Text>
{` Current step: ${props.message}`}
<Newline />
</Text>
);
interface UpdateProps {
destroy: boolean;
}
const Update = (props: UpdateProps) => {
const [message, setMessage] = React.useState("");
const [done, setDone] = React.useState(false);
const [hasError, setHasError] = React.useState(false);
const [updatesInProgress, setUpdatesInProgress] = React.useState({});
const [updatesComplete, setUpdatesComplete] = React.useState({});
const onEvent = (event: EngineEvent) => {
if (event.resourcePreEvent) {
const inProg = { ...updatesInProgress };
inProg[event.resourcePreEvent.metadata.urn] =
event.resourcePreEvent.metadata.type;
setUpdatesInProgress(inProg);
}
if (event.resOutputsEvent) {
const complete = { ...updatesComplete };
const { urn } = event.resOutputsEvent.metadata;
complete[urn] = event.resOutputsEvent.metadata.type;
setUpdatesComplete(complete);
}
};
const runPulumiUpdate = async () => {
try {
setMessage("Creating stack...");
const stack = await LocalWorkspace.createOrSelectStack(stackArgs);
setMessage("Ensuring plugins...");
await stack.workspace.installPlugin("aws", "v3.38.1");
setMessage("Setting configuration...");
await stack.setConfig("aws:region", {
value: "us-west-2",
});
setMessage("Running refresh...");
await stack.refresh();
if (props.destroy) {
setMessage("Running destroy...");
await stack.destroy();
setMessage("Deleting stack...");
await stack.workspace.removeStack(stack.name);
setMessage("Success!");
setDone(true);
return;
}
setMessage("Running update...");
await stack.up({ onEvent });
setMessage("Success!");
setDone(true);
} catch (error) {
setMessage(error.error());
setHasError(true);
setDone(true);
}
};
React.useEffect(() => {
runPulumiUpdate();
}, []);
if (done) {
return <DoneMessage error={hasError} message={message} />;
}
return (
<Box>
<InProgressMessage message={message} />
<ResourceUpdateList
updatesComplete={updatesComplete}
updatesInProgress={updatesInProgress}
/>
</Box>
);
};
inquirer
.prompt([
{
type: "list",
name: "destroy",
message: "What kind of update is this?",
default: false,
choices: [
{ name: "update", value: false },
{ name: "destroy", value: true },
],
},
])
.then((answers: Answers) => render(<Update destroy={answers.destroy} />))
.catch(error => {
if (error.isTtyError) {
console.error(
"Prompt couldn't be rendered in the current environment."
);
} else {
console.error(error);
}
});