Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add s3remove hooks to clean up #35

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 65 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const fs = require('fs');
const path = require('path');
const mime = require('mime-types');
const BbPromise = require('bluebird');
const messagePrefix = 'S3 Deploy: ';

const globOpts = {
nodir: true
Expand Down Expand Up @@ -45,12 +46,30 @@ class Assets {
shortcut: 'b'
}
}
},
s3remove: {
usage: 'Remove assets from S3 buckets',
lifecycleEvents: [
'remove'
],
options: {
verbose: {
usage: 'Increase verbosity',
shortcut: 'v'
},
bucket: {
usage: 'Limit the removal to a specific bucket',
shortcut: 'b'
}
}
}
};

this.hooks = {
's3deploy:deploy': () => Promise.resolve().then(this.deployS3.bind(this)),
'after:deploy:finalize': () => Promise.resolve().then(this.afterDeploy.bind(this))
'after:deploy:finalize': () => Promise.resolve().then(this.afterDeploy.bind(this)),
's3remove:remove': () => Promise.resolve().then(this.removeS3.bind(this)),
'before:remove:remove': () => Promise.resolve().then(this.beforeRemove.bind(this))
};
}

Expand All @@ -60,7 +79,7 @@ class Assets {
*/
log(message) {
if(this.options.verbose || process.env.SLS_DEBUG || this.config.verbose) {
this.serverless.cli.log(message);
this.serverless.cli.log(`${messagePrefix} ${message}`);
}
}

Expand All @@ -70,6 +89,12 @@ class Assets {
}
}

beforeRemove() {
if(this.config.auto) {
this.removeS3();
}
}

listStackResources(resources, nextToken) {
resources = resources || [];
if (!this.config.resolveReferences) {
Expand Down Expand Up @@ -136,6 +161,19 @@ class Assets {
});
}

removeBucket(bucket) {
console.log(`Removing bucket`, bucket);
const deleteParams = {
Bucket: bucket,
};

return this.provider.request('S3', 'deleteBucket', deleteParams)
.then(() => {
this.log(`Did it fail?`)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is a good way of checking for failure here, and capturing a 404 instead of reporting it to the stack?

return
});
}

deployS3() {
let assetSets = this.config.targets;

Expand Down Expand Up @@ -203,6 +241,31 @@ class Assets {
)
})
}

removeS3() {
let assetSets = this.config.targets;

// Read existing stack resources so we can resolve references if necessary
return this.listStackResources()
.then(resources => {
// Process asset sets in parallel (up to 3)
return BbPromise.map(assetSets, assets => {
const prefix = assets.prefix || '';
// Try to resolve the bucket name
return this.resolveBucket(resources, assets.bucket)
.then((bucket) => {
this.log(`Emptying bucket`)
return this.emptyBucket(bucket, prefix)
.then(() => {
this.log(`Removing Bucket: ${bucket}:${prefix}`)
return this.removeBucket(bucket);
});
})
},
{ concurrency: 3 }
)
})
}
}

module.exports = Assets;