-
Notifications
You must be signed in to change notification settings - Fork 2
/
removeDeleteMarkers.js
291 lines (267 loc) · 9.14 KB
/
removeDeleteMarkers.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/* eslint-disable max-len */
/* eslint-disable no-console */
const { http } = require('httpagent');
const async = require('async');
const AWS = require('aws-sdk');
const { Logger } = require('werelogs');
const log = new Logger('s3utils::removeDeleteMarkers');
// configurable params
const DEFAULT_WORKERS = 10;
const DEFAULT_LOG_PROGRESS_INTERVAL = 10;
const DEFAULT_LISTING_LIMIT = 1000;
const USAGE = `
removeDeleteMarkers.js
This script removes delete markers from one or more versioning-suspended bucket(s)
Usage:
node removeDeleteMarkers.js bucket1[,bucket2...]
Mandatory environment variables:
ENDPOINT: S3 endpoint
ACCESS_KEY: S3 account access key
SECRET_KEY: S3 account secret key
Optional environment variables:
TARGET_PREFIX: only process a specific prefix in the bucket(s)
WORKERS: concurrency value for listing / batch delete requests (default ${DEFAULT_WORKERS})
LOG_PROGRESS_INTERVAL: interval in seconds between progress update log lines (default ${DEFAULT_LOG_PROGRESS_INTERVAL})
LISTING_LIMIT: number of keys to list per listing request (default ${DEFAULT_LISTING_LIMIT})
KEY_MARKER: resume processing from a specific key
VERSION_ID_MARKER: resume processing from a specific version ID
`;
const BUCKETS = process.argv[2] ? process.argv[2].split(',') : null;
const { ACCESS_KEY } = process.env;
const { SECRET_KEY } = process.env;
const { ENDPOINT } = process.env;
const { TARGET_PREFIX } = process.env;
const WORKERS = (
process.env.WORKERS
&& Number.parseInt(process.env.WORKERS, 10)) || DEFAULT_WORKERS;
const { KEY_MARKER } = process.env;
const { VERSION_ID_MARKER } = process.env;
if (!BUCKETS || BUCKETS.length === 0) {
console.error('No buckets given as input, please provide '
+ 'a comma-separated list of buckets on the command line');
console.error(USAGE);
process.exit(1);
}
if (!ENDPOINT) {
console.error('ENDPOINT not defined');
console.error(USAGE);
process.exit(1);
}
if (!ACCESS_KEY) {
console.error('ACCESS_KEY not defined');
console.error(USAGE);
process.exit(1);
}
if (!SECRET_KEY) {
console.error('SECRET_KEY not defined');
console.error(USAGE);
process.exit(1);
}
const LISTING_LIMIT = (
process.env.LISTING_LIMIT
&& Number.parseInt(process.env.LISTING_LIMIT, 10))
|| DEFAULT_LISTING_LIMIT;
const LOG_PROGRESS_INTERVAL = (
process.env.LOG_PROGRESS_INTERVAL
&& Number.parseInt(process.env.LOG_PROGRESS_INTERVAL, 10))
|| DEFAULT_LOG_PROGRESS_INTERVAL;
AWS.config.update({
accessKeyId: ACCESS_KEY,
secretAccessKey: SECRET_KEY,
endpoint: ENDPOINT,
region: 'us-east-1',
sslEnabled: false,
s3ForcePathStyle: true,
apiVersions: { s3: '2006-03-01' },
signatureVersion: 'v4',
signatureCache: false,
});
const s3 = new AWS.S3({
httpOptions: {
agent: new http.Agent({ keepAlive: true }),
},
});
const status = {
objectsListed: 0,
deleteMarkersDeleted: 0,
deleteMarkersErrors: 0,
bucketInProgress: null,
keyMarker: KEY_MARKER,
versionIdMarker: VERSION_ID_MARKER,
};
function logProgress(message) {
log.info(message, {
objectsListed: status.objectsListed,
deleteMarkersDeleted: status.deleteMarkersDeleted,
deleteMarkersErrors: status.deleteMarkersErrors,
bucketInProgress: status.bucketInProgress,
keyMarker: status.keyMarker,
versionIdMarker: status.versionIdMarker,
});
}
const progressInterval = setInterval(
() => logProgress('progress update'),
LOG_PROGRESS_INTERVAL * 1000,
);
const taskQueue = async.queue((task, done) => {
const {
bucket, beginBucket, keyMarker, versionIdMarker,
} = task;
let bucketDone = false;
status.keyMarker = keyMarker;
status.versionIdMarker = versionIdMarker;
return async.waterfall([
next => {
if (!beginBucket) {
return next();
}
logProgress('start scanning bucket');
return s3.getBucketVersioning({
Bucket: bucket,
}, (err, data) => {
if (err) {
log.error('error getting bucket versioning', {
bucket,
error: err.message,
});
bucketDone = true;
return next(err);
}
if (data.Status !== 'Suspended') {
log.error('bucket versioning status is not "Suspended", skipping bucket', {
bucket,
versioningStatus: data.Status,
});
bucketDone = true;
return next(new Error('bucket not processed'));
}
return next();
});
},
next => s3.listObjectVersions({
Bucket: bucket,
MaxKeys: LISTING_LIMIT,
Prefix: TARGET_PREFIX,
KeyMarker: keyMarker,
VersionIdMarker: versionIdMarker,
}, (err, data) => {
if (err) {
log.error('error listing object versions', {
bucket,
keyMarker,
versionIdMarker,
error: err.message,
});
bucketDone = true;
return next(err);
}
status.objectsListed += data.Versions.length + data.DeleteMarkers.length;
if (data.NextKeyMarker || data.NextVersionIdMarker) {
taskQueue.push({
bucket,
keyMarker: data.NextKeyMarker,
versionIdMarker: data.NextVersionIdMarker,
});
} else {
bucketDone = true;
}
return next(null, data.DeleteMarkers);
}),
(deleteMarkers, next) => {
if (deleteMarkers.length === 0) {
return next();
}
return s3.deleteObjects({
Bucket: bucket,
Delete: {
Objects: deleteMarkers.map(item => ({
Key: item.Key,
VersionId: item.VersionId,
})),
},
}, (err, data) => {
if (err) {
log.error('batch delete request error', {
bucket,
keyMarker,
versionIdMarker,
error: err.message,
});
status.deleteMarkersErrors += deleteMarkers.length;
deleteMarkers.forEach(entry => {
log.error('error deleting delete marker', {
bucket,
objectKey: entry.Key,
versionId: entry.VersionId,
error: 'batch delete request failed',
});
});
return next();
}
if (data.Deleted) {
status.deleteMarkersDeleted += data.Deleted.length;
data.Deleted.forEach(entry => {
log.info('delete marker deleted', {
bucket,
objectKey: entry.Key,
versionId: entry.VersionId,
});
});
}
if (data.Errors) {
status.deleteMarkersErrors += data.Errors.length;
data.Errors.forEach(entry => {
log.error('error deleting delete marker', {
bucket,
objectKey: entry.Key,
versionId: entry.VersionId,
error: entry.Code,
errorDesc: entry.Message,
});
});
}
return next();
});
},
], err => {
if (bucketDone) {
status.keyMarker = null;
status.versionIdMarker = null;
if (!err) {
logProgress('completed listing of bucket');
}
if (BUCKETS.length > 0) {
status.bucketInProgress = BUCKETS.shift();
taskQueue.push({
bucket: status.bucketInProgress,
beginBucket: true,
});
}
}
return done();
});
}, WORKERS);
log.info('starting cleanup of delete markers in buckets', {
buckets: BUCKETS,
});
status.bucketInProgress = BUCKETS.shift();
taskQueue.push({
bucket: status.bucketInProgress,
beginBucket: true,
keyMarker: KEY_MARKER,
versionIdMarker: VERSION_ID_MARKER,
});
taskQueue.drain = () => {
status.bucketInProgress = null;
logProgress('completed cleanup of all buckets');
clearInterval(progressInterval);
};
function stop() {
log.info('stopping execution');
logProgress('last status');
process.exit(0);
}
process.on('SIGINT', stop);
process.on('SIGHUP', stop);
process.on('SIGQUIT', stop);
process.on('SIGTERM', stop);