-
Notifications
You must be signed in to change notification settings - Fork 21
/
clear-account.js
217 lines (198 loc) · 4.95 KB
/
clear-account.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
const { getAWSSDK } = require("../lib/aws");
const { Command, flags } = require("@oclif/command");
const { checkVersion } = require("../lib/version-check");
const inquirer = require("inquirer");
const { getAllFunctionsCount, deleteAllFunctions } = require("../lib/lambda");
const { getAllRolesCount, deleteAllRoles } = require("../lib/iam");
const { getAllApiGwCount, deleteAllApiGw } = require("../lib/apigw");
const { getBucketCount, deleteAllBuckets } = require("../lib/s3");
const { deleteAllStacks, getAllStacksCount } = require("../lib/cloudformation");
const { track } = require("../lib/analytics");
require("colors");
const { getAllLogGroupsCount, deleteAllLogGroups } = require("../lib/cloudwatch-logs");
class ClearAccountCommand extends Command {
async run() {
const { flags } = this.parse(ClearAccountCommand);
const { force, profile, retries } = flags;
global.profile = profile;
this.retries = retries;
checkVersion();
track("clear-account", { force, retries });
const AWS = getAWSSDK();
if (force) {
this.log("Forcing account cleanup!".red.bold);
await this.clearEnvironment(AWS);
} else {
const sts = new AWS.STS();
const caller = await sts.getCallerIdentity().promise();
const message = `You are about clear account [${caller.Account}], are you sure?`;
const { clear } = await inquirer.prompt([
{
type: "confirm",
name: "clear",
message: message,
default: false
}
]);
if (clear) {
await this.clearEnvironment(AWS);
} else {
this.log("Not clearing environment");
}
}
}
summary(results, singularName, hasRegion) {
const failed = {};
results.forEach(val => {
const key = hasRegion ? `${val.name} [${val.region}]` : val.name;
if (val.status === "fail") {
failed[key] = val.reason;
}
});
console.info("");
if (Object.keys(failed).length > 0) {
this.log(
`Failed deleting ${Object.keys(failed).length} ${singularName}(s)`.red
.bold
);
for (const [key, value] of Object.entries(failed)) {
this.log(`${key} - ${value}`.red);
}
}
}
async resourceDeletion(countFunc, deleteAllFunc, singularName, hasRegion) {
const count = await countFunc();
if (count > 0) {
this.log(`Deleting ${count} ${singularName}(s)`);
let leftRetries = this.retries;
let results = await deleteAllFunc();
while (
leftRetries > 0 &&
results.filter(val => {
return val.status === "fail";
}).length > 0
) {
console.info("\nTrying again...");
results = await deleteAllFunc();
leftRetries--;
}
this.summary(results, singularName, hasRegion);
} else {
this.log(`No ${singularName}(s) to delete. Skipping...`);
}
}
async clearS3(AWS) {
this.log("S3".bold);
await this.resourceDeletion(
async () => {
return await getBucketCount(AWS);
},
async () => {
return await deleteAllBuckets(AWS);
},
"bucket",
false
);
}
async clearCF(AWS) {
this.log("CloudFormation".bold);
await this.resourceDeletion(
async () => {
return await getAllStacksCount(AWS);
},
async () => {
return await deleteAllStacks(AWS);
},
"CF stack",
true
);
}
async clearApiGw(AWS) {
this.log("API Gateway".bold);
await this.resourceDeletion(
async () => {
return await getAllApiGwCount(AWS);
},
async () => {
return await deleteAllApiGw(AWS);
},
"API Gateway",
true
);
}
async clearRoles(AWS) {
this.log("IAM Roles".bold);
await this.resourceDeletion(
async () => {
return await getAllRolesCount(AWS);
},
async () => {
return await deleteAllRoles(AWS);
},
"role",
true
);
}
async clearLambdas(AWS) {
this.log("Lambdas".bold);
await this.resourceDeletion(
async () => {
return await getAllFunctionsCount(AWS);
},
async () => {
return await deleteAllFunctions(AWS);
},
"lambda",
true
);
}
async clearLogGroups(AWS) {
this.log("Log Groups".bold);
await this.resourceDeletion(
async () => {
return await getAllLogGroupsCount(AWS);
},
async () => {
return await deleteAllLogGroups(AWS);
},
"log group",
true
);
}
async clearEnvironment(AWS) {
await this.clearS3(AWS);
console.info("");
await this.clearCF(AWS);
console.info("");
await this.clearApiGw(AWS);
console.info("");
await this.clearRoles(AWS);
console.info("");
await this.clearLambdas(AWS);
console.info("");
await this.clearLogGroups(AWS);
console.info("");
}
}
ClearAccountCommand.description =
"Clear your AWS account from all supported resources. Use with cautious!";
ClearAccountCommand.flags = {
force: flags.boolean({
char: "f",
description: "Skip prompt. Use mainly in CI/CD environments",
required: false,
default: false
}),
profile: flags.string({
char: "p",
description: "AWS CLI profile name",
required: false
}),
retries: flags.string({
char: "r",
description: "How many times to try to delete stubborn resource",
required: false,
default: 2
})
};
module.exports = ClearAccountCommand;