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
|
# File 'lib/gaff/ec2_api.rb', line 4
def self.exec(msg)
parser = Yajl::Parser.new
hash = parser.parse(msg)
Gaff::Log.debug(hash)
STDOUT.flush
begin
@ec2 = Fog::AWS::Compute.new(
:aws_access_key_id => hash["params"]["aws_key"],
:aws_secret_access_key => hash["params"]["aws_key_secret"],
:region => hash["params"]["region"])
Gaff::Log.debug(@ec2)
STDOUT.flush
case hash["method"]
when "attach_volume"
result = @ec2.attach_volume(hash["params"]["instance_id"], hash["params"]["volume_id"], hash["params"]["device"])
when "create_volume"
result = @ec2.create_volume(hash["params"]["availability_zone"], hash["params"]["size"].to_i, hash["params"]["snapshot_id"])
when "delete_volume"
result = @ec2.delete_volume(hash["params"]["volume_id"])
when "detach_volume"
result = @ec2.detach_volume(
hash["params"]["volume_id"],
{
"InstanceId" => hash["params"]["instance_id"],
"Device" => hash["params"]["device"],
"Force" => hash["params"]["force"]
})
when "launch_instances"
result = @ec2.run_instances(
hash["params"]["image_id"],
hash["params"]["count"],
hash["params"]["count"],
{
"SecurityGroup" => hash["params"]["group_ids"],
"KeyName" => hash["params"]["key_name"],
"Placement.AvailabilityZone" => hash["params"]["availability_zone"],
"InstanceType" => hash["params"]["instance_type"]
})
when "reboot_instances"
result = @ec2.reboot_instances(hash["params"]["instance_ids"])
when "terminate_instances"
result = @ec2.terminate_instances(hash["params"]["instance_ids"])
end
Gaff::Log.info(result)
STDOUT.flush
rescue Exception => e
Gaff::Log.error(e)
STDOUT.flush
end
end
|