From ecf3a45cd1572e557dbfc232de058ddc4d4ee902 Mon Sep 17 00:00:00 2001 From: Nik Graf Date: Tue, 18 Oct 2016 13:31:27 +0200 Subject: [PATCH] fix cleaning up s3 buckets --- lib/plugins/aws/deploy/lib/cleanupS3Bucket.js | 48 +++++-------------- 1 file changed, 12 insertions(+), 36 deletions(-) diff --git a/lib/plugins/aws/deploy/lib/cleanupS3Bucket.js b/lib/plugins/aws/deploy/lib/cleanupS3Bucket.js index ad92b5a37..a245770aa 100644 --- a/lib/plugins/aws/deploy/lib/cleanupS3Bucket.js +++ b/lib/plugins/aws/deploy/lib/cleanupS3Bucket.js @@ -2,55 +2,31 @@ const BbPromise = require('bluebird'); const _ = require('lodash'); +const findAndGroupStacks = require('../utils/findAndGroupStacks'); +const s3ObjectsFromStacks = require('../utils/s3ObjectsFromStacks'); module.exports = { getObjectsToRemove() { // 4 old ones + the one which will be uploaded after the cleanup = 5 - const directoriesToKeepCount = 4; - const serviceStage = `${this.serverless.service.service}/${this.options.stage}`; + const stacksToKeepCount = 4; + const service = this.serverless.service.service; + const stage = this.options.stage; return this.sdk.request('S3', 'listObjectsV2', { Bucket: this.bucketName, - Prefix: `serverless/${serviceStage}`, + Prefix: `serverless/${service}/${stage}`, }, this.options.stage, this.options.region) - .then((result) => { - if (result.Contents.length) { - let directories = []; - const regex = new RegExp( - `serverless/${serviceStage}/(.+-.+-.+-.+)` - ); - - // get the unique directory names - result.Contents.forEach((obj) => { - const match = obj.Key.match(regex); - - if (match) { - const directoryName = match[1]; - directories = _.union(directories, [directoryName]); - } - }); - - // sort the directory names - directories = directories.sort(); - - const directoriesToKeep = _.takeRight(directories, directoriesToKeepCount); - const directoriesToRemove = _.pullAllWith(directories, directoriesToKeep, _.isEqual); - - const objectsToRemove = []; - - // get all the objects in the directories which should be removed - result.Contents.forEach((obj) => { - directoriesToRemove.some((element) => { - const match = obj.Key.match(element); - if (match) objectsToRemove.push({ Key: obj.Key }); - return obj; - }); - }); + .then((response) => { + const stacks = findAndGroupStacks(response, service, stage); + const stacksToKeep = _.takeRight(stacks, stacksToKeepCount); + const stacksToRemove = _.pullAllWith(stacks, stacksToKeep, _.isEqual); + const objectsToRemove = s3ObjectsFromStacks(stacksToRemove, service, stage); + if (objectsToRemove.length) { return BbPromise.resolve(objectsToRemove); }