Class: AmazonEC2

Inherits:
Object
  • Object
show all
Defined in:
lib/amazon-instance/amazon-ec2.rb

Instance Method Summary collapse

Constructor Details

#initialize(access_key, secret_key, ec2_server, elb_server) ⇒ AmazonEC2

Returns a new instance of AmazonEC2.



6
7
8
9
10
11
12
13
# File 'lib/amazon-instance/amazon-ec2.rb', line 6

def initialize(access_key, secret_key, ec2_server, elb_server)
  @ec2 = AWS::EC2::Base.new(:access_key_id     => access_key,
                            :secret_access_key => secret_key,
                            :server            => ec2_server)
  @elb = AWS::ELB::Base.new(:access_key_id     => access_key,
                            :secret_access_key => secret_key,
                            :server            => elb_server)
end

Instance Method Details

#attach_instance_to_load_balancer(instance_id, load_balancer_name) ⇒ Object



57
58
59
60
# File 'lib/amazon-instance/amazon-ec2.rb', line 57

def attach_instance_to_load_balancer(instance_id, load_balancer_name)
  @elb.register_instances_with_load_balancer(:load_balancer_name => load_balancer_name,
                                             :instances => [instance_id])
end

#host_by_instance(instance_id) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/amazon-instance/amazon-ec2.rb', line 45

def host_by_instance(instance_id)
  host = nil

  while host == nil do
    instances = @ec2.describe_instances({:instance_id => instance_id})
    host = instances['reservationSet']['item'].first['instancesSet']['item'].first['dnsName']
    sleep 2
  end
  
  host
end

#launch_instance(environment, config, config_dir) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/amazon-instance/amazon-ec2.rb', line 15

def launch_instance(environment, config, config_dir) 
  config[:base64_encoded] = true
  
  if !config[:on_boot].nil?
    File.open(config_dir+'on-boot/'+config[:on_boot]) do |content|
      config[:user_data] = content.read.gsub!('{environment}', environment)
    end
  end

  instance = @ec2.run_instances(config.to_hash)
  
  instance_id = instance['instancesSet']['item'].first['instanceId']

  if config[:load_balancer]
    attach_instance_to_load_balancer(instance_id, config[:load_balancer])
  end

  host_by_instance(instance_id)
end

#sleep_till_ssh_is_open(host) ⇒ Object



62
63
64
65
66
# File 'lib/amazon-instance/amazon-ec2.rb', line 62

def sleep_till_ssh_is_open(host)
  while ssh_open?(host) do
    sleep(3)
  end
end

#ssh_open?(host) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/amazon-instance/amazon-ec2.rb', line 68

def ssh_open?(host)
  begin
    Timeout::timeout(1) do
      begin
        s = TCPSocket.new(host, 22)
        s.close
        return true
      rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH
        return false
      end
    end
  rescue Timeout::Error
  end

  return false
end

#valid_group?(group_name) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
38
39
40
41
42
43
# File 'lib/amazon-instance/amazon-ec2.rb', line 35

def valid_group?(group_name)
  valid_group = false
  
  @ec2.describe_security_groups['securityGroupInfo']['item'].each do |group|
    valid_group = true if group['groupName'] == group_name
  end

  valid_group
end