Module: PoolParty::Ec2

Defined in:
lib/poolparty/net/remote_bases/ec2.rb

Instance Method Summary collapse

Instance Method Details

#create_keypair ⇒ Object

Help create a keypair for the cloud This is a helper to create the keypair and add them to the cloud for you



59
60
61
62
63
# File 'lib/poolparty/net/remote_bases/ec2.rb', line 59

def create_keypair
  return false unless keypair
  FileUtils.mkdir_p ::File.dirname(new_keypair_path) unless ::File.directory?(::File.dirname(new_keypair_path))
  Kernel.system "ec2-add-keypair #{keypair} > #{new_keypair_path} && chmod 600 #{new_keypair_path}"
end

#custom_configure_tasks_for(o) ⇒ Object



83
84
85
86
87
# File 'lib/poolparty/net/remote_bases/ec2.rb', line 83

def custom_configure_tasks_for(o)
  [
    "# ec2 configuration"
  ]
end

#custom_install_tasks_for(o) ⇒ Object

Callback



72
73
74
75
76
77
78
79
80
81
# File 'lib/poolparty/net/remote_bases/ec2.rb', line 72

def custom_install_tasks_for(o)
  [
    "# ec2 installation tasks",
    "# Set hostname",
    # "if [ -z $(grep -v '#' /etc/hosts | grep '#{o.name}') ]; then echo \"$(curl http://169.254.169.254/latest/meta-data/public-ipv4) #{o.name}\" >> /etc/hosts; fi",
    "if [ -z \"$(grep -v '#' /etc/hosts | grep '#{o.name}')\" ]; then echo '127.0.0.1 #{o.name}' >> /etc/hosts; fi",
    "hostname #{o.name}",
    "echo #{o.name} > /etc/hostname"
  ]
end

#describe_instance(id = nil) ⇒ Object

Describe an instance's status



29
30
31
# File 'lib/poolparty/net/remote_bases/ec2.rb', line 29

def describe_instance(id=nil)
  describe_instances.select {|a| a[:name] == id}[0] rescue nil
end

#describe_instances ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/poolparty/net/remote_bases/ec2.rb', line 32

def describe_instances
  unless @describe_instances && !@describe_instances.empty?
    @id = 0
    @describe_instances = get_instances_description.each_with_index do |h,i|
      if h[:status] == "running"
        @name = "node#{@id}"
        @id += 1
      else
        @name = "#{h[:status]}_node#{i}"
      end
      h.merge!({
        :name => @name,
        :hostname => h[:ip],
        :ip => h[:ip].convert_from_ec2_to_ip
      })
    end
    @describe_instances.first[:name] = "master" unless @describe_instances.empty?
  end
  @describe_instances
end

#ec2 ⇒ Object

EC2 connections



65
66
67
68
69
# File 'lib/poolparty/net/remote_bases/ec2.rb', line 65

def ec2
  @ec2 ||= EC2::Base.new( :access_key_id => (access_key || Base.access_key), 
                          :secret_access_key => (secret_access_key || Base.secret_access_key)
                        )
end

#get_instances_description ⇒ Object

Get the s3 description for the response in a hash format



53
54
55
# File 'lib/poolparty/net/remote_bases/ec2.rb', line 53

def get_instances_description
  @cached_descriptions ||= EC2ResponseObject.get_descriptions(ec2.describe_instances).sort_by {|a| a[:launching_time]}
end

#launch_new_instance! ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/poolparty/net/remote_bases/ec2.rb', line 9

def launch_new_instance!
  instance = ec2.run_instances(
    :image_id => self.respond_to?(:ami) ? ami : Base.ami,
    :user_data => "",
    :minCount => 1,
    :maxCount => 1,
    :key_name => "#{self.respond_to?(:keypair) ? keypair : Base.keypair}",
    :availability_zone => nil,
    :size => "#{self.respond_to?(:size) ? size : Base.size}")
  begin
    item = instance#.instancesSet.item
    EC2ResponseObject.get_hash_from_response(item)
  rescue Exception => e          
  end
end

#reset! ⇒ Object



89
90
91
# File 'lib/poolparty/net/remote_bases/ec2.rb', line 89

def reset!
  @describe_instances = @cached_descriptions = nil
end

#terminate_instance!(instance_id = nil) ⇒ Object

Terminate an instance by id



25
26
27
# File 'lib/poolparty/net/remote_bases/ec2.rb', line 25

def terminate_instance!(instance_id=nil)
  ec2.terminate_instances(:instance_id => instance_id)
end