briareus
Version:
Briareus assists with Feature Branch deploys to ECS
52 lines (45 loc) • 1.8 kB
JavaScript
const async = require('async');
const _ = require('lodash');
const CLI = require('../../cli');
var cmd = {
setup(yargs, output, cb) {
yargs.command('cleanup', 'Cleanup old variants', function (yargs) {
return yargs
.option('authtoken', {
required: true,
description: 'Authtoken'
})
.option('keep', {
default: 20,
description: 'The number of variants to keep around'
})
}, (argv) => cmd.run(new CLI(argv, output), cb));
},
run(cli, cb) {
cli.output.write("Progress rendering from this cleanup command is a TODO. Please be patient while Briareus does things in the background.\n")
async.waterfall([
(done) => cli.api.variant.list({}, (err, resp) => {
if (err) return done(err);
done(null, resp.data);
}),
(variants, done) => {
const totalVariants = variants.length;
const dynamicVariants = _.filter(variants, { 'cleanup': true });
const numStaticVariants = (totalVariants - dynamicVariants.length);
const numOfDynamicVariantsToKeep = cli.args.keep - numStaticVariants;
const numOfDynamicVariantsToDestroy = totalVariants - numOfDynamicVariantsToKeep
const variantsToDestroy = _.reverse(_.sortBy(variants, 'lastDeploymentAt')).slice(0, numOfDynamicVariantsToDestroy)
// Destroy in Series to help prevent the server from hitting request limits to AWS API's
async.mapSeries(variantsToDestroy, (variant, next) => {
cli.api.destroy({ slug: variant.slug }, (err) => {
if (err) return next(err);
cli.output.write(`Variant "${variant.slug}" Destroyed\n`);
next();
});
}, done);
}
], cb);
}
}
module.exports = cmd;