-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathec2-start-stop.rb
42 lines (35 loc) · 1.1 KB
/
ec2-start-stop.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
39
#!/usr/bin/env ruby
require "aws-sdk"
@aws_access_key_id = ENV['AWS_ACCESS_KEY_ID']
@aws_secret_access_key = ENV['AWS_SECRET_ACCESS_KEY']
@ec2 = AWS::EC2.new(:access_key_id => @aws_access_key_id,:secret_access_key => @aws_secret_access_key)
action=ARGV[0]
def get_nodeStatus(instance_id)
begin
return @ec2.instances[instance_id].status
rescue
return "notrpresent"
end
end
def action_on_instances(instance_id,action)
running_state=get_nodeStatus(instance_id)
puts "Instance is in: #{running_state}"
if action == "start" and "#{running_state}" == "stopped" || "#{running_state}" == "notrunning"
begin
@ec2.instances[instance_id].start
puts "Starting the instance: #{instance_id}"
rescue Exception => e
puts e
end
elsif action == "stop" and "#{running_state}" == "running" || "#{running_state}" == "pending"
begin
@ec2.instances[instance_id].stop
puts "Stopping the instance: #{instance_id}"
rescue Exception => e
puts e
end
end
end
puts "Required Action: #{action}"
puts "Performing action: #{action} on instance: #{id}"
action_on_instances(instance_id,action)