# File lib/boxgrinder-build/helpers/ec2-helper.rb, line 104 def self.availability_zone_to_region(availability_zone) availability_zone.scan(/((\w+)-(\w+)-(\d+))/).flatten.first end
# File lib/boxgrinder-build/helpers/ec2-helper.rb, line 96 def self.current_availability_zone get_meta_data('/latest/meta-data/placement/availability-zone/') end
# File lib/boxgrinder-build/helpers/ec2-helper.rb, line 100 def self.current_instance_id get_meta_data('/latest/meta-data/instance-id') end
EC2 Endpoints
# File lib/boxgrinder-build/helpers/ec2-helper.rb, line 131 def self.endpoints SERVICES end
EC2 meta-data queries
# File lib/boxgrinder-build/helpers/ec2-helper.rb, line 87 def self.get_meta_data(path) timeout(HTTP_TIMEOUT) do req = Net::HTTP::Get.new(path) res = Net::HTTP.start('169.254.169.254', 80) {|http| http.request(req)} return res.body if Net::HTTPSuccess res.error! end end
# File lib/boxgrinder-build/helpers/ec2-helper.rb, line 30 def initialize(ec2, opts={}) raise ArgumentError, "ec2 argument must not be nil" if ec2.nil? @ec2 = ec2 @log = opts[:log] || LogHelper.new end
EC2 queries
# File lib/boxgrinder-build/helpers/ec2-helper.rb, line 110 def ami_by_name(name, account_number) q = @ec2.images.with_owner(account_number). filter("name", name) return nil unless q.any? q.first end
# File lib/boxgrinder-build/helpers/ec2-helper.rb, line 123 def live_instances(ami) q = @ec2.instances.filter('image-id', ami.id) return q.select{|a| a.status != :terminated} if q.any? nil end
# File lib/boxgrinder-build/helpers/ec2-helper.rb, line 117 def snapshot_by_id(snapshot_id) q = @ec2.snapshots.filter('snapshot-id', snapshot_id) return nil unless q.any? q.first end
# File lib/boxgrinder-build/helpers/ec2-helper.rb, line 45 def wait_for_image_death(ami, opts={}) opts = parse_opts(opts, {:frequency => DEF_POLL_FREQ, :timeout => DEF_TIMEOUT}) wait_with_timeout(opts[:frequency], opts[:timeout]){ !ami.exists? } rescue AWS::EC2::Errors::InvalidImageID::NotFound end
Wait cycles
# File lib/boxgrinder-build/helpers/ec2-helper.rb, line 38 def wait_for_image_state(state, ami, opts={}) #First wait for the AMI to be confirmed to exist (after creating, an immediate query can cause an error) opts = parse_opts(opts, {:frequency => DEF_POLL_FREQ, :timeout => DEF_TIMEOUT}) wait_with_timeout(opts[:frequency], opts[:timeout]){ ami.exists? } wait_with_timeout(opts[:frequency], opts[:timeout]){ ami.state == state } end
Being serial shouldn't be much slower as we are blocked by the slowest stopper anyway
# File lib/boxgrinder-build/helpers/ec2-helper.rb, line 57 def wait_for_instance_death(instance, opts={}) wait_for_instance_status(:terminated, instance, opts) if instance.exists? rescue AWS::EC2::Errors::InvalidInstanceID::NotFound end
# File lib/boxgrinder-build/helpers/ec2-helper.rb, line 51 def wait_for_instance_status(status, instance, opts={}) opts = parse_opts(opts, {:frequency => DEF_POLL_FREQ, :timeout => DEF_TIMEOUT}) wait_with_timeout(opts[:frequency], opts[:timeout]){ instance.status == status } end
# File lib/boxgrinder-build/helpers/ec2-helper.rb, line 62 def wait_for_snapshot_status(status, snapshot, opts={}) opts = parse_opts(opts, {:frequency => DEF_POLL_FREQ, :timeout => DEF_TIMEOUT}) progress = -1 wait_with_timeout(opts[:frequency], opts[:timeout]) do current_progress = snapshot.progress || 0 unless progress == current_progress @log.info "Progress: #{current_progress}%" progress = current_progress end snapshot.status == status end rescue Exception @log.debug "Polling of snapshot #{snapshot.id} for status '#{status}' failed" unless snapshot.nil? raise end
# File lib/boxgrinder-build/helpers/ec2-helper.rb, line 78 def wait_for_volume_status(status, volume, opts={}) opts = parse_opts(opts, {:frequency => DEF_POLL_FREQ, :timeout => DEF_TIMEOUT}) wait_with_timeout(opts[:frequency], opts[:timeout]) do volume.status == status end end