Skip to content

Commit

Permalink
Merge pull request #11 from nib-health-funds/refactor
Browse files Browse the repository at this point in the history
Refactor project
  • Loading branch information
krutisfood authored May 4, 2017
2 parents 225d21c + 2e40182 commit 52cfb0c
Show file tree
Hide file tree
Showing 38 changed files with 850 additions and 871 deletions.
129 changes: 5 additions & 124 deletions hammertime.js
Original file line number Diff line number Diff line change
@@ -1,126 +1,7 @@
const instances = require('./src/instances');
const asgs = require('./src/asgs');
const start = require('./src/start');
const stop = require('./src/stop');

function stopInstances() {
return new Promise((resolve, reject) => {
instances.listInstancesToStop()
.then((stoppableInstances) => {
console.log('Found the following instances to shut down...');
if (stoppableInstances.length === 0) {
console.log('None! Moving on.');
resolve('No instances to shut down');
}

stoppableInstances.forEach((instance) => {
console.log(instance);
});
return instances.tagInstances(stoppableInstances);
})
.then((taggedInstances) => {
console.log('Finished tagging instances. Moving on to stop them.');
return instances.stopInstances(taggedInstances);
})
.then(resolve)
.catch(reject);
});
}

function startInstances() {
return new Promise((resolve, reject) => {
instances.listInstancesToStart()
.then((startableInstances) => {
console.log(`Found the following ${startableInstances.length} instances to start up...`);
if (startableInstances.length === 0) {
console.log('None! Moving on.');
resolve('No instances to turn on');
}

startableInstances.forEach((instance) => {
console.log(instance);
});
return instances.startInstances(startableInstances);
})
.then((startedInstances) => {
console.log('Finished starting instances. Moving on to untag them.');
return instances.untagInstances(startedInstances);
})
.then(resolve)
.catch(reject);
});
}

function stopASGs() {
return new Promise((resolve, reject) => {
asgs.listASGsToStop()
.then((stoppableASGs) => {
console.log(`Found the following ${stoppableASGs.length} instances to spin down...`);
if (stoppableASGs.length === 0) {
console.log('None! Moving on.');
resolve('No ASGs to spin down');
}

stoppableASGs.forEach((asg) => {
console.log(asg.AutoScalingGroupName);
});
return asgs.tagASGs(stoppableASGs);
})
.then((taggedASGs) => {
console.log(`Finished tagging ASGs. Moving on to spin down ${taggedASGs.length} of them.`);
return asgs.stopASGs(taggedASGs);
})
.then(resolve)
.catch(reject);
});
}

function startASGs() {
return new Promise((resolve, reject) => {
asgs.listASGsToStart()
.then((startableASGs) => {
console.log(`Found the following ${startableASGs.length} instances to start up...`);
if (startableASGs.length === 0) {
console.log('None! Moving on.');
resolve('No ASGs to spin up');
}

startableASGs.forEach((asg) => {
console.log(asg.AutoScalingGroupName);
});
return asgs.startASGs(startableASGs);
})
.then((startedASGs) => {
console.log(`Finished spinning up ASGs. Moving on to untag ${startedASGs.length} of them.`);
return asgs.untagASGs(startedASGs);
})
.then(resolve)
.catch(reject);
});
}

module.exports.start = (event, context, callback) => {
console.log('Break it down!');
Promise.all([
startInstances(),
startASGs(),
]).then(() => {
console.log('All instances and ASGs started successfully. Good morning!');
callback(null, { message: 'Start: Hammertime successfully completed.' }, event);
}).catch((err) => {
console.error(err);
callback(err);
});
};

module.exports.stop = (event, context, callback) => {
console.log('Stop. Hammertime!');
Promise.all([
stopInstances(),
stopASGs(),
]).then(() => {
console.log('All instances and ASGs stopped successfully. Good night!');
callback(null, { message: 'Stop: Hammertime successfully completed.' }, event);
}).catch((err) => {
console.error(err);
callback(err);
});
module.exports = {
start,
stop,
};
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
"scripts": {
"deploy": "node_modules/serverless/bin/serverless deploy --verbose",
"destroy": "node_modules/serverless/bin/serverless remove --verbose",
"lint": "eslint **/*.js",
"lint": "eslint *.js",
"test": "npm run lint && npm run test.unit",
"test.unit": "mocha test/*.test.js"
"test.unit": "mocha test/**/*.test.js"
},
"repository": {
"type": "git",
Expand Down
215 changes: 0 additions & 215 deletions src/asgs.js

This file was deleted.

3 changes: 3 additions & 0 deletions src/asgs/hasTag.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = function hasTag(asg, target) {
return asg.Tags.some(tag => tag.Key === target);
};
13 changes: 13 additions & 0 deletions src/asgs/listASGsToStart.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const hasTag = require('./hasTag');
const listTargetASGs = require('./listTargetASGs');

function startableASG(asg) {
console.log(`Looking at ${asg.AutoScalingGroupName}, stop:hammertime tag is ${hasTag(asg, 'stop:hammertime')}, hammertime:canttouchthis is ${hasTag(asg, 'hammertime:canttouchthis')}`);
return hasTag(asg, 'stop:hammertime') && !hasTag(asg, 'hammertime:canttouchthis');
}

function listASGsToStart() {
return listTargetASGs(startableASG);
}

module.exports = listASGsToStart;
13 changes: 13 additions & 0 deletions src/asgs/listASGsToStop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const hasTag = require('./hasTag');
const listTargetASGs = require('./listTargetASGs');

function stoppableASG(asg) {
console.log(`Looking at ${asg.AutoScalingGroupName}, stop:hammertime tag is ${hasTag(asg, 'stop:hammertime')}, hammertime:canttouchthis is ${hasTag(asg, 'hammertime:canttouchthis')}`);
return !hasTag(asg, 'stop:hammertime') && !hasTag(asg, 'hammertime:canttouchthis');
}

function listASGsToStop() {
return listTargetASGs(stoppableASG);
}

module.exports = listASGsToStop;
Loading

0 comments on commit 52cfb0c

Please sign in to comment.