forked from sinofseven/serverless-s3-remover
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
203 lines (193 loc) · 5.96 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
"use strict";
const chalk = require("chalk");
const prompt = require("prompt");
const messagePrefix = "S3 Remover: ";
class Remover {
constructor(serverless, options) {
this.serverless = serverless;
this.options = options;
this.provider = this.serverless.getProvider("aws");
this.commands = {
s3remove: {
usage: "Remove all files in S3 buckets",
lifecycleEvents: ["remove"],
options: {
verbose: {
usage: "Increase verbosity",
shortcut: "v"
}
}
}
};
this.hooks = {
"before:remove:remove": () =>
Promise.resolve().then(this.remove.bind(this)),
"s3remove:remove": () => Promise.resolve().then(this.remove.bind(this))
};
}
log(message) {
if (this.options.verbose) {
this.serverless.cli.log(message);
}
}
remove() {
const self = this;
const getAllKeys = bucket => {
const get = (src = {}) => {
const data = src.data;
let keys = src.keys || [];
const param = {
Bucket: bucket
};
if (data) {
param.VersionIdMarker = data.NextVersionIdMarker;
param.KeyMarker = data.NextKeyMarker;
}
return self.provider
.request("S3", "listObjectVersions", param)
.then(result => {
return new Promise(resolve => {
if (result.Versions && result.Versions.length) {
keys = keys.concat(
result.Versions.map(item => {
return { VersionId: item.VersionId, Key: item.Key };
})
);
}
if (result.DeleteMarkers && result.DeleteMarkers.length) {
keys = keys.concat(
result.DeleteMarkers.map(item => {
return { VersionId: item.VersionId, Key: item.Key };
})
);
}
resolve({
data: result,
keys
});
});
});
};
const list = (src = {}) => {
return get(src).then(result => {
if (result.data.IsTruncated) {
return list(result);
} else {
const keys = result.keys;
const batched = [];
for (let i = 0; i < keys.length; i += 1000) {
const objects = keys.slice(i, i + 1000);
batched.push({
Bucket: bucket,
Delete: {
Objects: objects
}
});
}
return new Promise(resolve => {
resolve(batched);
});
}
});
};
return list();
};
const executeRemove = params => {
return Promise.all(
params.map(param => {
return self.provider.request("S3", "deleteObjects", param);
})
);
};
const populateConfig = () => {
return this.serverless.variables
.populateObject(this.serverless.service.custom.remover)
.then(fileConfig => {
const defaultConfig = {
prompt: false,
buckets: []
};
return Object.assign({}, defaultConfig, fileConfig);
});
};
return new Promise(resolve => {
return populateConfig().then(config => {
if (!config.prompt) {
const promisses = [];
for (const b of config.buckets) {
promisses.push(
getAllKeys(b)
.then(executeRemove)
.then(() => {
const message = `Success: ${b} is empty.`;
self.log(message);
self.serverless.cli.consoleLog(
`${messagePrefix}${chalk.yellow(message)}`
);
})
.catch(err => {
console.log(err);
const message = `Faild: ${b} may not be empty.`;
self.log(message);
self.log(err);
self.serverless.cli.consoleLog(
`${messagePrefix}${chalk.yellow(message)}`
);
})
);
}
return Promise.all(promisses).then(resolve);
}
prompt.message = messagePrefix;
prompt.delimiter = "";
prompt.start();
const schema = { properties: {} };
config.buckets.forEach(b => {
schema.properties[b] = {
message: `Make ${b} empty. Are you sure? [yes/no]:`,
validator: /(yes|no)/,
required: true,
warning: "Must respond yes or no"
};
});
prompt.get(schema, (err, result) => {
const promisses = [];
for (const b of config.buckets) {
if (result[b].match(/^y/)) {
promisses.push(
getAllKeys(b)
.then(executeRemove)
.then(() => {
const message = `Success: ${b} is empty.`;
self.log(message);
self.serverless.cli.consoleLog(
`${messagePrefix}${chalk.yellow(message)}`
);
})
.catch(() => {
const message = `Faild: ${b} may not be empty.`;
self.log(message);
self.serverless.cli.consoleLog(
`${messagePrefix}${chalk.yellow(message)}`
);
})
);
} else {
promisses.push(
Promise.resolve().then(() => {
const message = `Remove cancelled: ${b}`;
self.log(message);
self.serverless.cli.consoleLog(
`${messagePrefix}${chalk.yellow(message)}`
);
})
);
}
}
Promise.all(promisses).then(resolve);
});
});
});
}
}
module.exports = Remover;