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.



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

def cloud_provider
  @cloud_provider
end

Instance Method Details

#bootstrap!Object



184
185
186
# File 'lib/poolparty/cloud.rb', line 184

def bootstrap!
  cloud_provider.bootstrap_nodes!(tmp_path)
end

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

Raises:

  • (ArgumentError)


89
90
91
92
# File 'lib/poolparty/cloud.rb', line 89

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



214
215
216
217
218
219
220
221
222
223
# File 'lib/poolparty/cloud.rb', line 214

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



180
181
182
# File 'lib/poolparty/cloud.rb', line 180

def compile!
  @chef.compile! unless @chef.nil?
end

#configure!Object



188
189
190
191
# File 'lib/poolparty/cloud.rb', line 188

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

#describe_instance(o = {}) ⇒ Object



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

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

#describe_instances(o = {}) ⇒ Object



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

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



208
209
210
# File 'lib/poolparty/cloud.rb', line 208

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



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

def pool
  parent || pool
end

#proper_nameObject



231
232
233
# File 'lib/poolparty/cloud.rb', line 231

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

#reboot!Object



143
144
145
146
147
148
149
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
# File 'lib/poolparty/cloud.rb', line 143

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



193
194
195
# File 'lib/poolparty/cloud.rb', line 193

def reset!
  cloud_provider.reset!
end

#rsync(source, dest) ⇒ Object



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

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



94
95
96
97
98
99
100
101
# File 'lib/poolparty/cloud.rb', line 94

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



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

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

#ssh(num = 0) ⇒ Object



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

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



107
108
109
110
111
112
113
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
# File 'lib/poolparty/cloud.rb', line 107

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



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

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

#tmp_pathObject



65
66
67
# File 'lib/poolparty/cloud.rb', line 65

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

#using(provider_name, &block) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/poolparty/cloud.rb', line 72

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