|
1 |
| -const instances = require('./src/instances'); |
2 |
| -const asgs = require('./src/asgs'); |
| 1 | +const start = require('./src/start'); |
| 2 | +const stop = require('./src/stop'); |
3 | 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 |
| - }); |
| 4 | +module.exports = { |
| 5 | + start, |
| 6 | + stop, |
126 | 7 | };
|
0 commit comments