lib/poolparty/net/remoter_base.rb in auser-poolparty-1.2.2 vs lib/poolparty/net/remoter_base.rb in auser-poolparty-1.2.3

- old
+ new

@@ -14,28 +14,44 @@ After your remote base is written, make sure to register the base outside the context of the remote base, like so: register_remote_base :remote_base_name =end - +require "#{::File.dirname(__FILE__)}/../modules/pinger.rb" #FIXME should not need this here module PoolParty module Remote # This class is the base class for all remote types, such as ec2 # Everything remoting-wise is derived from this class class RemoterBase - include Dslify - include ::PoolParty::Remote + include Dslify + include ::PoolParty::Remote + include ::PoolParty::Pinger - attr_reader :cloud - - def initialize(prnt, opts={}, &block) - dsl_options prnt.options.merge(opts) if prnt && prnt.respond_to?(:options) + dsl_methods :cloud, # The cloud this remoter_base is a part of + :keypair, + :image_id + + def initialize(opts={}, &block) + opts.each {|k,v| opts[k] = v.call if v.respond_to?(:call) } + set_vars_from_options opts instance_eval &block if block - @cloud = prnt end + # def evaluate_proc_options(opts) + # if opts.respond_to?(:call) + # opts.call + # elsif opts.respond_to? :each + # if opts.respond_to?(:values) && opts.respond_to?(:keys) + # opts.each {|k,v| opts[k] = evaluate_proc_options v } + # elsif opts.respond_to?(:each_with_index) + # opts.each_with_index{|o,i| opts[i] = evaluate_proc_options o} + # end + # end + # opts + # end + def self.inherited(arg) base_name = "#{arg}".downcase.top_level_class.to_sym (remote_bases << base_name) unless remote_bases.include?(base_name) end @@ -52,79 +68,86 @@ # The next methods are required on all RemoteInstance types # If your RemoteInstance type does not overwrite the following methods # An exception will be raised and poolparty will explode into tiny little # pieces. Don't forget to overwrite these methods # Launch a new instance - def self.launch_new_instance!(cld, o={}) - new(cld, o).launch_new_instance!(o) + def self.launch_new_instance!(o={}) + new(o).launch_new_instance!(o) end def launch_new_instance!(o={}) raise RemoteException.new(:method_not_defined, "launch_new_instance!") end # Terminate an instance by id - def self.terminate_instance!(cld, o={}) - new(cld, o).terminate_instance!(o) + def self.terminate_instance!(o={}) + new(o).terminate_instance!(o) end def terminate_instance!(o={}) raise RemoteException.new(:method_not_defined, "terminate_instance!") end # Describe an instance's status - def self.describe_instance(cld, o={}) - new(cld, o).describe_instance(o) + def self.describe_instance(o={}) + new(o).describe_instance(o) end def describe_instance(o={}) raise RemoteException.new(:method_not_defined, "describe_instance") end # Get instances # The instances must have a status associated with them on the hash - def self.describe_instances(cld, o={}) - new(cld, o).describe_instances(o) + def self.describe_instances(o={}) + new(o).describe_instances(o) end def describe_instances(o={}) raise RemoteException.new(:method_not_defined, "describe_instances") end # TODO: Rename and modularize the @inst.status =~ /pending/ so that it works on all # remoter_bases def launch_instance!(o={}, &block) - @inst = launch_new_instance!( o ) + @cloud = clouds[o[:cloud_name]] + @inst = launch_new_instance!( dsl_options.merge(o) ) sleep(2) - cloud.dputs "#{cloud.name} launched instance checking for ip..." + @cloud.dputs "#{@cloud.name} launched instance checking for ip..." # Wait for 10 minutes for the instance to gain an ip if it doesn't already have one 500.times do |i| - break if @inst[:ip] =~ /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/ - sleep(2) - @inst = describe_instance(@inst) - cloud.dprint "." + if @inst + break if @inst[:ip] && @inst[:ip] =~ /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/ + break if @inst[:public_ip] && @inst[:public_ip] =~ /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/ + sleep(2) + @inst = describe_instance(@inst) + @cloud.dprint "." + else + @inst = describe_instances.last + @cloud.dprint "." + end end - cloud.dputs "Found an ip" - cloud.dputs "#{@cloud.name} Launched instance #{@inst[:ip]}" - cloud.dputs " waiting for it to respond" + dputs "Found an ip" + dputs "#{@cloud.name} Launched instance #{@inst[:ip]}" + dputs " waiting for it to respond" # Try for 10 minutes to pint port 22 500.times do |i| - cloud.dprint "." + @cloud.dprint "." if ping_port(@inst[:ip], 22) - cloud.dputs "" - cloud.started_instance = @inst + @cloud.dputs "" + @cloud.started_instance = @inst - cloud.call_after_launch_instance_callbacks(@inst) + @cloud.call_after_launch_instance_callbacks(@inst) block.call(@inst) if block return @inst end sleep(2) end raise "Instance not responding at #{inst.ip}" end - def self.launch_instance!(cld, o={}, &block) - new(cld, o, &block).launch_instance! + def self.launch_instance!(o={}, &block) + new(o, &block).launch_instance! end # Called after an instance is launched def after_launch_instance(instance=nil) puts "after_launch_instance in remoter_base" \ No newline at end of file