forked from serverless/serverless
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintegration-test-cleanup.js
97 lines (83 loc) · 2.51 KB
/
integration-test-cleanup.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
'use strict';
const BbPromise = require('bluebird');
const AWS = require('aws-sdk');
const CF = new AWS.CloudFormation({ region: 'us-east-1' });
const S3 = new AWS.S3({ region: 'us-east-1' });
BbPromise.promisifyAll(CF, { suffix: 'Promised' });
BbPromise.promisifyAll(S3, { suffix: 'Promised' });
const logger = console;
const emptyS3Bucket = (bucket) => (
S3.listObjectsPromised({ Bucket: bucket })
.then(data => {
logger.log('Bucket', bucket, 'has', data.Contents.length, 'items');
if (data.Contents.length) {
const keys = data.Contents.map(item => Object.assign({}, { Key: item.Key }));
return S3.deleteObjectsPromised({
Bucket: bucket,
Delete: {
Objects: keys,
},
});
}
return BbPromise.resolve();
})
);
const deleteS3Bucket = (bucket) => (
emptyS3Bucket(bucket)
.then(() => {
logger.log('Bucket', bucket, 'is now empty, deleting ...');
return S3.deleteBucketPromised({ Bucket: bucket });
})
);
const cleanupS3Buckets = (token) => {
logger.log('Looking through buckets ...');
const params = {};
if (token) {
params.NextToken = token;
}
return S3.listBucketsPromised()
.then(response =>
response.Buckets.reduce((memo, bucket) => memo
.then(() => deleteS3Bucket(bucket.Name)), BbPromise.resolve())
.then(() => {
if (response.NextToken) {
return cleanupS3Buckets(response.NextToken);
}
return BbPromise.resolve();
})
);
};
const cleanupCFStacks = (token) => {
const params = {
StackStatusFilter: [
'CREATE_FAILED',
'CREATE_COMPLETE',
'ROLLBACK_FAILED',
'ROLLBACK_COMPLETE',
'DELETE_FAILED',
'UPDATE_ROLLBACK_FAILED',
'UPDATE_ROLLBACK_COMPLETE',
],
};
if (token) {
params.NextToken = token;
}
logger.log('Looking through stacks ...');
return CF.listStacksPromised(params)
.then(response =>
response.StackSummaries.reduce((memo, stack) => {
if (['DELETE_COMPLETE', 'DELETE_IN_PROGRESS'].indexOf(stack.StackStatus) === -1) {
logger.log('Deleting stack', stack.StackName);
return memo.then(() => CF.deleteStackPromised({ StackName: stack.StackName }));
}
return memo;
}, BbPromise.resolve())
.then(() => {
if (response.NextToken) {
return cleanupCFStacks(response.NextToken);
}
return BbPromise.resolve();
})
);
};
cleanupS3Buckets().then(cleanupCFStacks);