Class: Ciborg::Amazon

Inherits:
Object
  • Object
show all
Defined in:
lib/ciborg/amazon.rb

Constant Summary collapse

PORTS_TO_OPEN =
[22, 443] + (9000...9010).to_a

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, secret) ⇒ Amazon

Returns a new instance of Amazon.



9
10
11
12
# File 'lib/ciborg/amazon.rb', line 9

def initialize(key, secret)
  @key = key
  @secret = secret
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



7
8
9
# File 'lib/ciborg/amazon.rb', line 7

def key
  @key
end

#secretObject (readonly)

Returns the value of attribute secret.



7
8
9
# File 'lib/ciborg/amazon.rb', line 7

def secret
  @secret
end

Instance Method Details

#add_key_pair(key_pair_name, public_key) ⇒ Object



49
50
51
# File 'lib/ciborg/amazon.rb', line 49

def add_key_pair(key_pair_name, public_key)
  fog_key_pairs.create(:name => key_pair_name, :public_key => public_key) unless fog_key_pairs.get(key_pair_name)
end

#create_security_group(group_name) ⇒ Object



30
31
32
33
34
# File 'lib/ciborg/amazon.rb', line 30

def create_security_group(group_name)
  unless fog_security_groups.get(group_name)
    fog_security_groups.create(:name => group_name, :description => 'Ciborg-generated group')
  end
end

#delete_key_pair(key_pair_name) ⇒ Object



45
46
47
# File 'lib/ciborg/amazon.rb', line 45

def delete_key_pair(key_pair_name)
  fog_key_pairs.new(:name => key_pair_name).destroy
end

#destroy_ec2(confirm_proc, *args) ⇒ Object



76
77
78
79
80
81
82
83
84
85
# File 'lib/ciborg/amazon.rb', line 76

def destroy_ec2(confirm_proc, *args)
  servers.each do |server|
    next unless (args == [:all]) || args.include?(server.id)
    next unless confirm_proc.call(server)
    ip = server.public_ip_address
    server.destroy
    release_elastic_ip(ip)
    yield(server) if block_given?
  end
end

#elastic_ip_addressObject



26
27
28
# File 'lib/ciborg/amazon.rb', line 26

def elastic_ip_address
  @elastic_ip_address ||= fog.addresses.create
end

#fog_key_pairsObject



18
19
20
# File 'lib/ciborg/amazon.rb', line 18

def fog_key_pairs
  fog.key_pairs
end

#fog_security_groupsObject



14
15
16
# File 'lib/ciborg/amazon.rb', line 14

def fog_security_groups
  fog.security_groups
end

#fog_serversObject



22
23
24
# File 'lib/ciborg/amazon.rb', line 22

def fog_servers
  fog.servers
end

#launch_server(key_pair_name, security_group_name, instance_type = "m1.medium", availability_zone = "us-east-1b") ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/ciborg/amazon.rb', line 61

def launch_server(key_pair_name, security_group_name, instance_type = "m1.medium", availability_zone = "us-east-1b")
  fog_servers.create(
    :image_id => "ami-a29943cb",
    :flavor_id => instance_type,
    :availability_zone => availability_zone,
    :tags => {"Name" => "Ciborg", "ciborg" => Ciborg::VERSION},
    :key_name => key_pair_name,
    :groups => [security_group_name]
  ).tap do |server|
    server.wait_for { ready? }
    fog.associate_address(server.id, elastic_ip_address.public_ip) # needs to be running
    server.reload
  end
end

#open_port(group_name, *ports) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/ciborg/amazon.rb', line 36

def open_port(group_name, *ports)
  group = fog_security_groups.get(group_name)
  ports.each do |port|
    unless group.ip_permissions.any? { |p| (p["fromPort"]..p["toPort"]).include?(port) }
      group.authorize_port_range(port..port)
    end
  end
end

#release_elastic_ip(ip) ⇒ Object



91
92
93
# File 'lib/ciborg/amazon.rb', line 91

def release_elastic_ip(ip)
  fog.addresses.get(ip).destroy if fog.addresses.get(ip)
end

#serversObject



87
88
89
# File 'lib/ciborg/amazon.rb', line 87

def servers
  fog_servers.select { |s| s.tags.keys.include?("ciborg") && s.state == "running" }
end

#with_key_pair(pubkey) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/ciborg/amazon.rb', line 53

def with_key_pair(pubkey)
  unique_key_pair_name = "CIBORG-#{Time.now.to_i}"
  add_key_pair(unique_key_pair_name, pubkey)
  yield unique_key_pair_name if block_given?
ensure
  delete_key_pair(unique_key_pair_name)
end