-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
executable file
·236 lines (202 loc) · 6.64 KB
/
index.js
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
#!/usr/bin/env node
"use strict";
import { pathToFileURL } from "url";
import { realpathSync } from "fs";
import readline from "readline";
import chalk from "chalk";
import analysis from "./lib/analysis.js";
import cfn from "./lib/cfn.js";
import helpers from "./lib/helpers.js";
import renderers from "./lib/renderers.js";
let chain = Promise.resolve(true);
function queueChangeSet(input) {
chain = chain.then(() => maybeReviewChangeSet(input));
}
/**
* Checks the given input for CloudFormation changeset ARN and
* starts review if one is found.
*
* @param {String} input the input to parse
* @param {Boolean} [skipExec=false] set to true to skip execution of change set (for tests)
*/
async function maybeReviewChangeSet(input, skipExec = false) {
const info = helpers.getChangeSetInfoFromInput(input);
if (!info) {
return;
}
const data = await cfn.describeChangeSet(info);
const stack = data.changeset.StackName;
const name = data.changeset.ChangeSetName;
console.log(chalk.bold("Summary"));
console.log(`- Stack Name: ${stack}`);
console.log(`- Change Set Name: ${name}`);
console.log();
if (!data.changeset.Changes || !data.changeset.Changes.length) {
if (data.changeset.ExecutionStatus === "UNAVAILABLE") {
console.log(chalk.bold("No changes"), data.changeset.StatusReason);
return;
} else {
console.log(
chalk.bold("No resource, parameter or tag changes."),
"Outputs may change but changes to outputs cannot be reviewed.",
);
}
}
const resourceChanges = analysis.analyzeResourceChanges(data.changeset);
printResourceChanges(resourceChanges);
const tagChanges = analysis.analyzeTagChanges(data.changeset, data.stack);
printKeyValueChanges(tagChanges, "Tag Changes");
const parameterChanges = analysis.analyzeParameterChanges(data.changeset, data.stack);
printKeyValueChanges(parameterChanges, "Parameter Changes");
if (!skipExec) {
return promptAndExecuteChanges(data.changeset);
}
}
/**
* @typedef KeyValueChange
* @property {String} Key
* @property {String} Value
* @property {String} [OldValue]
*/
/**
* @typedef KeyValueChanges
* @property {KeyValueChange[]} added
* @property {KeyValueChange[]} removed
* @property {KeyValueChange[]} modified
*/
/**
* @param {KeyValueChanges} changes
*/
function printKeyValueChanges(changes, title) {
if (!changes.added.length && !changes.removed.length && !changes.modified.length) {
// No tag changes!
return;
}
console.log(); // Print empty line to separate this from previous section
console.log(chalk.bold(title));
/**
* @param {String} action
* @param {KeyValueChange[]} changes
*/
function _printKeyValueChanges(action, changes) {
for (const change of changes) {
const type = renderers.renderAction({ Action: action });
const summary = renderers.renderKeyValueChange(change);
console.log(`${type} ${summary}`);
}
}
_printKeyValueChanges("Add", changes.added);
_printKeyValueChanges("Remove", changes.removed);
_printKeyValueChanges("Modify", changes.modified);
}
/**
* @param {Object} changes
* @param {import('@aws-sdk/client-cloudformation').ResourceChange[]} changes.added
* @param {import('@aws-sdk/client-cloudformation').ResourceChange[]} changes.removed
* @param {import('@aws-sdk/client-cloudformation').ResourceChange[]} changes.modified
*/
function printResourceChanges(changes) {
/**
* @param {import('@aws-sdk/client-cloudformation').ResourceChange[]} changes
*/
function _printResources(changes) {
for (const change of changes) {
const type = renderers.renderAction(change);
const resource = renderers.renderResourceSummary(change);
const replacement = renderers.renderReplacement(change);
console.log(`${type} ${resource}${replacement}`);
for (const detail of change.Details) {
const recreation = renderers.renderRecreation(detail);
console.log(` - ${detail.Summary}${recreation}`);
for (const cause of detail.Causes || []) {
console.log(` caused by ${cause}`);
}
}
}
}
if (!changes.added.length && !changes.removed.length && !changes.modified.length) {
// No resource changes!
return;
}
console.log(chalk.bold("Resource Changes"));
_printResources(changes.added);
_printResources(changes.removed);
_printResources(changes.modified);
}
/**
* @param {import('@aws-sdk/client-cloudformation').DescribeChangeSetOutput} changeset
* @param {Number} wait - wait time in seconds
*/
async function waitAndExecuteChanges(changeset, wait) {
while (wait > 0) {
process.stderr.write(wait + ". ");
await new Promise((resolve) => setTimeout(resolve, 1000));
wait -= 1;
}
console.log("Executing change set");
return cfn.executeChangeSet(changeset);
}
/**
* @param {import('@aws-sdk/client-cloudformation').DescribeChangeSetOutput} changeset
*/
async function promptAndExecuteChanges(changeset) {
if (process.env.PROMPT_ANSWER) {
return handleAnswer(changeset, process.env.PROMPT_ANSWER);
}
const ttys = await import("ttys").catch((_err) => null);
if (!ttys) {
console.log("Executing changeset in 10 seconds. Press CTRL+C to abort!");
return waitAndExecuteChanges(changeset, 10);
}
const i = readline.createInterface(ttys.stdin, ttys.stdout);
console.log(); // Empty line
const answer = await new Promise((resolve) => i.question("Execute change set [y/N]? ", resolve));
i.close();
return handleAnswer(changeset, answer);
}
/**
*
* Handle answer to prompt. y for yes, anything else for no.
*
* @param {import('@aws-sdk/client-cloudformation').DescribeChangeSetOutput} changeset
* @param {string} answer
* @returns
*/
async function handleAnswer(changeset, answer) {
if (answer === "y") {
console.log("Executing change set...");
return cfn.executeChangeSet(changeset);
} else {
console.log("Skipping change set execution");
return true;
}
}
function run() {
// See if we have a changeset ARN(s) given as command line argument(s)
if (process.argv.length > 2) {
for (const input of process.argv.slice(2)) {
queueChangeSet(input);
}
}
// Also check if we have been piped some input and detect changeset
// IDs from there
if (!process.stdin.isTTY) {
const rl = readline.createInterface({
input: process.stdin,
});
rl.on("line", (line) => {
// echo to stdout
console.log(line);
queueChangeSet(line);
});
rl.on("close", () => {
chain.then(() => process.exit(0));
});
}
}
/* istanbul ignore next */
if (import.meta.url === pathToFileURL(realpathSync(process.argv[1])).href) {
run();
}
// For testing
export { maybeReviewChangeSet };