Class: PoolParty::Cloud

Inherits:
Base show all
Defined in:
lib/poolparty/cloud.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#name

Instance Method Summary collapse

Methods inherited from Base

#initialize, #method_missing

Constructor Details

This class inherits a constructor from PoolParty::Base

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class PoolParty::Base

Instance Attribute Details

#cloud_providerObject (readonly)

Returns the value of attribute cloud_provider.



78
79
80
# File 'lib/poolparty/cloud.rb', line 78

def cloud_provider
  @cloud_provider
end

Instance Method Details

#bootstrap!Object



197
198
199
# File 'lib/poolparty/cloud.rb', line 197

def bootstrap!
  cloud_provider.bootstrap_nodes!(tmp_path)
end

#chef(chef_type = :solo, &block) ⇒ Object

Raises:

  • (ArgumentError)


96
97
98
99
# File 'lib/poolparty/cloud.rb', line 96

def chef(chef_type=:solo, &block)
  raise ArgumentError, "Chef type must be one of #{Chef.types.map{|v| ":" + v.to_s}.join(",")}." unless Chef.types.include?(chef_type)
  @chef||=Chef.get_chef(chef_type,self,&block)
end

#cmd(commands, opts = {}) ⇒ Object

Run command/s on all nodes in the cloud. Returns a hash of instance_id=>result pairs



227
228
229
230
231
232
233
234
235
236
# File 'lib/poolparty/cloud.rb', line 227

def cmd(commands, opts={})
  key_by = opts.delete(:key_by) || :instance_id
  results = {}
  threads = nodes.collect do |n|
    puts "result for #{n.instance_id} ==> n.ssh(#{commands.inspect}, #{opts.inspect})"
    Thread.new{ results[ n.send(key_by) ] = n.ssh(commands, opts) }
  end
  threads.each{ |aThread| aThread.join }
  results
end

#compile!Object



187
188
189
190
191
192
193
194
195
# File 'lib/poolparty/cloud.rb', line 187

def compile!
  unless @uploads.nil?
    puts "Uploading files via rsync"
    @uploads.each do |upload|
      rsync upload[:source], upload[:dest]
    end
  end
  @chef.compile! unless @chef.nil?
end

#configure!Object



201
202
203
204
# File 'lib/poolparty/cloud.rb', line 201

def configure!
  compile!
  cloud_provider.configure_nodes!(tmp_path)
end

#describe_instance(o = {}) ⇒ Object



242
# File 'lib/poolparty/cloud.rb', line 242

def describe_instance(o={}); cloud_provider.describe_instance(o);end

#describe_instances(o = {}) ⇒ Object



241
# File 'lib/poolparty/cloud.rb', line 241

def describe_instances(o={}); cloud_provider.describe_instances(o);end

#instances(arg) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/poolparty/cloud.rb', line 43

def instances(arg)
  case arg
  when Range
    minimum_instances arg.first
    maximum_instances arg.last
  when Fixnum
    minimum_instances arg
    maximum_instances arg
  when Hash
    nodes(arg)
  else
    raise PoolParty::PoolPartyError.create("DslMethodCall", "You must call instances with either a number, a range or a hash (for a list of nodes)")
  end
end

#keypair(n = nil, extra_paths = []) ⇒ Object

returns an instance of Keypair You can pass either a filename which will be searched for in ~/.ec2/ and ~/.ssh/ Or you can pass a full filepath



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/poolparty/cloud.rb', line 12

def keypair(n=nil, extra_paths=[])
  return @keypair if @keypair
  @keypair = case n
  when String
    Keypair.new(n, extra_paths)
  when nil
    fpath = CloudProviders::CloudProvider.default_keypair_path/"#{proper_name}"
    File.exists?(fpath) ? Keypair.new(fpath, extra_paths) : generate_keypair(extra_paths)
  else
    raise ArgumentError, "There was an error when defining the keypair"
  end
end

#nodesObject

TODO: list of nodes needs to be consistentley sorted



221
222
223
# File 'lib/poolparty/cloud.rb', line 221

def nodes
  cloud_provider.nodes.select {|a| a.in_service? }
end

#poolObject

The pool can either be the parent (the context where the object is declared) or the global pool object



68
69
70
# File 'lib/poolparty/cloud.rb', line 68

def pool
  parent || pool
end

#proper_nameObject



244
245
246
# File 'lib/poolparty/cloud.rb', line 244

def proper_name
  "#{parent.name}-#{name}"
end

#reboot!Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/poolparty/cloud.rb', line 150

def reboot!
  orig_nodes = nodes
  if autoscalers.empty?
    puts <<-EOE
No autoscalers defined
  Launching new nodes and then shutting down original nodes
    EOE
    # Terminate the nodes
    orig_nodes.each_with_index do |node, i|
      # Start new nodes
      print "Starting node: #{i}...\n"
      expand_by(1)
      print "Terminating node: #{i}...\n"
      node.terminate!
      puts ""
    end
  else
    # Terminate the nodes
    @num_nodes = orig_nodes.size
    orig_nodes.each do |node|
      node.terminate!
      puts "----> Terminated node: #{node.instance_id}"
      # Wait for the autoscaler to boot the next node
      puts "----> Waiting for new node to boot via the autoscaler"
      loop do
        reset!
        break if nodes.size == @num_nodes
        $stdout.print "."
        $stdout.flush
        sleep 1
      end
    end
  end
  run
  puts ""
end

#reset!Object



206
207
208
# File 'lib/poolparty/cloud.rb', line 206

def reset!
  cloud_provider.reset!
end

#rsync(source, dest) ⇒ Object



214
215
216
217
218
# File 'lib/poolparty/cloud.rb', line 214

def rsync(source, dest)
  nodes.each do |node|
    node.rsync(:source => source, :destination => dest)
  end
end

#runObject

compile the cloud spec and execute the compiled system and remote calls



101
102
103
104
105
106
107
108
# File 'lib/poolparty/cloud.rb', line 101

def run
  puts "  running on #{cloud_provider.class}"
  cloud_provider.run
  unless @chef.nil?
    compile!
    bootstrap!
  end
end

#run_instance(o = {}) ⇒ Object

Explicit proxies to cloud_provider methods



239
# File 'lib/poolparty/cloud.rb', line 239

def run_instance(o={}); cloud_provider.run_instance(o);end

#ssh(num = 0) ⇒ Object



210
211
212
# File 'lib/poolparty/cloud.rb', line 210

def ssh(num=0)
  nodes[num].ssh
end

#teardownObject

TODO: Incomplete and needs testing Shutdown and delete the load_balancers, auto_scaling_groups, launch_configurations, security_groups, triggers and instances defined by this cloud



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/poolparty/cloud.rb', line 114

def teardown
  raise "Only Ec2 teardown supported" unless cloud_provider.name.to_s == 'ec2'
  puts "! Tearing down cloud #{name}"
  # load_balancers.each do |name, lb|
  #   puts "! Deleting load_balaner #{lb_name}"
  #   lb.teardown
  # end
  load_balancers.each do |lb|
    puts "-----> Tearing down load balancer: #{lb.name}"
    lb.teardown
  end

  rds_instances.each do |rds|
    puts "-----> Tearing down RDS Instance: #{rds.name}"
    rds.teardown
  end
  # instances belonging to an auto_scaling group must be deleted before the auto_scaling group
  #THIS SCARES ME! nodes.each{|n| n.terminate_instance!}
  # loop {nodes.size>0 ? sleep(4) : break }
  if autoscalers.empty?
    nodes.each do |node|
      node.terminate!
    end
  else
    autoscalers.each do |a|
      puts "-----> Tearing down autoscaler #{a.name}"
      a.teardown
    end
  end
  # autoscalers.keys.each do |as_name|
  #   puts "! Deleting auto_scaling_group #{as_name}"
  #   cloud_provider.as.delete_autoscaling_group('AutoScalingGroupName' => as_name)
  # end
  #TODO: keypair.delete # Do we want to delete the keypair?  probably, but not certain
end

#terminate_instance!(o = {}) ⇒ Object



240
# File 'lib/poolparty/cloud.rb', line 240

def terminate_instance!(o={}); cloud_provider.terminate_instance!(o);end

#tmp_pathObject



72
73
74
# File 'lib/poolparty/cloud.rb', line 72

def tmp_path
  "/tmp/poolparty" / pool.name / name
end

#upload(source, dest) ⇒ Object

Upload the source to dest ( using rsync )



60
61
62
63
# File 'lib/poolparty/cloud.rb', line 60

def upload source, dest
  @uploads ||= []
  @uploads << { :source => source, :dest => dest }
end

#using(provider_name, &block) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/poolparty/cloud.rb', line 79

def using(provider_name, &block)
  return @cloud_provider if @cloud_provider
  @cloud_provider = "#{provider_name}".constantize(CloudProviders).send(:new, provider_name, :cloud => self, &block)
  # Decorate the cloud with the cloud_provider methods
  (class << self; self; end).instance_variable_set('@cloud_provider', @cloud_provider)
    (class << self; self; end).class_eval do
      @cloud_provider.public_methods(false).each do |meth|
        next if respond_to?(meth) || method_defined?(meth) || private_method_defined?(meth)
        eval <<-EOE
          def #{meth}(*args, &block)
            @cloud_provider.send(:#{meth}, *args, &block)
          end
        EOE
    end
  end
end