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