-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rb
executable file
·38 lines (31 loc) · 1.03 KB
/
main.rb
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
#!/usr/bin/env ruby
require 'aws-sdk'
if ENV.values_at('AWS_ACCESS_KEY_ID', 'AWS_SECRET_ACCESS_KEY').compact.size < 2
raise RuntimeError, "Please set both AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY first. See README.md for details."
end
client = Aws::EC2::Client.new(region: 'us-west-2')
instances = client.describe_instances(
filters: [
{ name: 'tag:autostopper', values: ['true'] }
]
)
StopCommand = Struct.new(:id, :launch_time, keyword_init: true)
NoopCommand = Class.new.new
instances_to_stop = instances.reservations.map do |reservation|
reservation.instances.map do |instance|
if "running" == instance.state.name
StopCommand.new(id: instance.instance_id, launch_time: instance.launch_time)
else
NoopCommand
end
end
end.flatten.reject { |cmd| NoopCommand == cmd }
if instances_to_stop.size > 0
puts "Stopping #{instances_to_stop.count} instances:"
puts instances_to_stop
client.stop_instances({
instance_ids: instances_to_stop.map(&:id)
})
else
puts "Couldn't find any running instances"
end