Skip to content

Commit 52cfb0c

Browse files
authored
Merge pull request #11 from nib-health-funds/refactor
Refactor project
2 parents 225d21c + 2e40182 commit 52cfb0c

38 files changed

+850
-871
lines changed

hammertime.js

Lines changed: 5 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -1,126 +1,7 @@
1-
const instances = require('./src/instances');
2-
const asgs = require('./src/asgs');
1+
const start = require('./src/start');
2+
const stop = require('./src/stop');
33

4-
function stopInstances() {
5-
return new Promise((resolve, reject) => {
6-
instances.listInstancesToStop()
7-
.then((stoppableInstances) => {
8-
console.log('Found the following instances to shut down...');
9-
if (stoppableInstances.length === 0) {
10-
console.log('None! Moving on.');
11-
resolve('No instances to shut down');
12-
}
13-
14-
stoppableInstances.forEach((instance) => {
15-
console.log(instance);
16-
});
17-
return instances.tagInstances(stoppableInstances);
18-
})
19-
.then((taggedInstances) => {
20-
console.log('Finished tagging instances. Moving on to stop them.');
21-
return instances.stopInstances(taggedInstances);
22-
})
23-
.then(resolve)
24-
.catch(reject);
25-
});
26-
}
27-
28-
function startInstances() {
29-
return new Promise((resolve, reject) => {
30-
instances.listInstancesToStart()
31-
.then((startableInstances) => {
32-
console.log(`Found the following ${startableInstances.length} instances to start up...`);
33-
if (startableInstances.length === 0) {
34-
console.log('None! Moving on.');
35-
resolve('No instances to turn on');
36-
}
37-
38-
startableInstances.forEach((instance) => {
39-
console.log(instance);
40-
});
41-
return instances.startInstances(startableInstances);
42-
})
43-
.then((startedInstances) => {
44-
console.log('Finished starting instances. Moving on to untag them.');
45-
return instances.untagInstances(startedInstances);
46-
})
47-
.then(resolve)
48-
.catch(reject);
49-
});
50-
}
51-
52-
function stopASGs() {
53-
return new Promise((resolve, reject) => {
54-
asgs.listASGsToStop()
55-
.then((stoppableASGs) => {
56-
console.log(`Found the following ${stoppableASGs.length} instances to spin down...`);
57-
if (stoppableASGs.length === 0) {
58-
console.log('None! Moving on.');
59-
resolve('No ASGs to spin down');
60-
}
61-
62-
stoppableASGs.forEach((asg) => {
63-
console.log(asg.AutoScalingGroupName);
64-
});
65-
return asgs.tagASGs(stoppableASGs);
66-
})
67-
.then((taggedASGs) => {
68-
console.log(`Finished tagging ASGs. Moving on to spin down ${taggedASGs.length} of them.`);
69-
return asgs.stopASGs(taggedASGs);
70-
})
71-
.then(resolve)
72-
.catch(reject);
73-
});
74-
}
75-
76-
function startASGs() {
77-
return new Promise((resolve, reject) => {
78-
asgs.listASGsToStart()
79-
.then((startableASGs) => {
80-
console.log(`Found the following ${startableASGs.length} instances to start up...`);
81-
if (startableASGs.length === 0) {
82-
console.log('None! Moving on.');
83-
resolve('No ASGs to spin up');
84-
}
85-
86-
startableASGs.forEach((asg) => {
87-
console.log(asg.AutoScalingGroupName);
88-
});
89-
return asgs.startASGs(startableASGs);
90-
})
91-
.then((startedASGs) => {
92-
console.log(`Finished spinning up ASGs. Moving on to untag ${startedASGs.length} of them.`);
93-
return asgs.untagASGs(startedASGs);
94-
})
95-
.then(resolve)
96-
.catch(reject);
97-
});
98-
}
99-
100-
module.exports.start = (event, context, callback) => {
101-
console.log('Break it down!');
102-
Promise.all([
103-
startInstances(),
104-
startASGs(),
105-
]).then(() => {
106-
console.log('All instances and ASGs started successfully. Good morning!');
107-
callback(null, { message: 'Start: Hammertime successfully completed.' }, event);
108-
}).catch((err) => {
109-
console.error(err);
110-
callback(err);
111-
});
112-
};
113-
114-
module.exports.stop = (event, context, callback) => {
115-
console.log('Stop. Hammertime!');
116-
Promise.all([
117-
stopInstances(),
118-
stopASGs(),
119-
]).then(() => {
120-
console.log('All instances and ASGs stopped successfully. Good night!');
121-
callback(null, { message: 'Stop: Hammertime successfully completed.' }, event);
122-
}).catch((err) => {
123-
console.error(err);
124-
callback(err);
125-
});
4+
module.exports = {
5+
start,
6+
stop,
1267
};

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
"scripts": {
77
"deploy": "node_modules/serverless/bin/serverless deploy --verbose",
88
"destroy": "node_modules/serverless/bin/serverless remove --verbose",
9-
"lint": "eslint **/*.js",
9+
"lint": "eslint *.js",
1010
"test": "npm run lint && npm run test.unit",
11-
"test.unit": "mocha test/*.test.js"
11+
"test.unit": "mocha test/**/*.test.js"
1212
},
1313
"repository": {
1414
"type": "git",

src/asgs.js

Lines changed: 0 additions & 215 deletions
This file was deleted.

src/asgs/hasTag.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = function hasTag(asg, target) {
2+
return asg.Tags.some(tag => tag.Key === target);
3+
};

src/asgs/listASGsToStart.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const hasTag = require('./hasTag');
2+
const listTargetASGs = require('./listTargetASGs');
3+
4+
function startableASG(asg) {
5+
console.log(`Looking at ${asg.AutoScalingGroupName}, stop:hammertime tag is ${hasTag(asg, 'stop:hammertime')}, hammertime:canttouchthis is ${hasTag(asg, 'hammertime:canttouchthis')}`);
6+
return hasTag(asg, 'stop:hammertime') && !hasTag(asg, 'hammertime:canttouchthis');
7+
}
8+
9+
function listASGsToStart() {
10+
return listTargetASGs(startableASG);
11+
}
12+
13+
module.exports = listASGsToStart;

src/asgs/listASGsToStop.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const hasTag = require('./hasTag');
2+
const listTargetASGs = require('./listTargetASGs');
3+
4+
function stoppableASG(asg) {
5+
console.log(`Looking at ${asg.AutoScalingGroupName}, stop:hammertime tag is ${hasTag(asg, 'stop:hammertime')}, hammertime:canttouchthis is ${hasTag(asg, 'hammertime:canttouchthis')}`);
6+
return !hasTag(asg, 'stop:hammertime') && !hasTag(asg, 'hammertime:canttouchthis');
7+
}
8+
9+
function listASGsToStop() {
10+
return listTargetASGs(stoppableASG);
11+
}
12+
13+
module.exports = listASGsToStop;

0 commit comments

Comments
 (0)