/
wakeup-ec2.coffee
85 lines (73 loc) · 2.34 KB
/
wakeup-ec2.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# Description
# Start your EC2 instance
#
# Configuration:
# LIST_OF_ENV_VARS_TO_SET
#
# Commands:
# hubot start instance TagKey=TagValue - <Start your instances where TagKey = TagValue>
# hubot stop instance TagKey=TagValue - <Stop your instances where TagKey = TagValue>
#
# Notes:
# <optional notes required for the script>
#
# Author:
# ntkm <aui.tkm@gmail.com>
REGION = 'ap-northeast-1'
async = require('async')
AWS = require('aws-sdk')
if process.env.AWS_ACCESS_KEY_ID? and process.env.AWS_SECRET_ACCESS_KEY?
AWS.config.update({
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY
})
else
AWS.config.loadFromPath(process.env.AWS_SECRET_PATH || "#{process.env.HOME}/secrets/credentials.json")
AWS.config.update({region: process.env.AWS_REGION || REGION})
handleInstance = (ec2, instance, toHandle) ->
params = { InstanceIds: [instance.InstanceId] }
if toHandle is 'start'
ec2.startInstances(params, (err, data) ->
console.log(err, err.stack) if err?
)
else if toHandle is 'stop'
ec2.stopInstances(params, (err, data) ->
console.log(err, err.stack) if err?
)
module.exports = (robot) ->
robot.respond /(start|stop) instance ([^ =]+)?=([^ =]+)?$/i, (res) ->
toHandle = res.match[1]
tagKey = res.match[2]
tagValue = res.match[3]
res.send "#{toHandle}ing your EC2 Instances."
ec2 = new AWS.EC2()
params = {
Filters: [
{
Name: 'tag-key',
Values: [tagKey]
},
{
Name: 'tag-value',
Values: [tagValue]
}
]
}
ec2.describeInstances(params, (err, data) ->
if err? res.send "#{err} with stacktrace: #{err.stack}"
else
async.forEach(data.Reservations, (reservation, callback)->
instance = reservation.Instances[0]
res.send "Instance: id = #{instance.InstanceId}"
if toHandle is 'start' and instance.State.Name is 'running'
res.send 'already running!'
else if toHandle is 'stop' and instance.State.Name isnt 'running'
res.send 'already stopping or stoped.'
else
handleInstance(ec2, instance, toHandle)
res.reply "#{toHandle}ed instance: id = #{instance.InstanceId}"
callback()
, () ->
res.send 'All Done!!'
)
)