class BoxGrinder::EC2Helper

Constants

DEF_POLL_FREQ
DEF_TIMEOUT
HTTP_TIMEOUT
SERVICES

Public Class Methods

availability_zone_to_region(availability_zone) click to toggle source
# 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
current_availability_zone() click to toggle source
# 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
current_instance_id() click to toggle source
# File lib/boxgrinder-build/helpers/ec2-helper.rb, line 100
def self.current_instance_id
  get_meta_data('/latest/meta-data/instance-id')
end
endpoints() click to toggle source

EC2 Endpoints

# File lib/boxgrinder-build/helpers/ec2-helper.rb, line 131
def self.endpoints
  SERVICES
end
get_meta_data(path) click to toggle source

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
new(ec2, opts={}) click to toggle source
# 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

Public Instance Methods

ami_by_name(name, account_number) click to toggle source

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
live_instances(ami) click to toggle source
# 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
snapshot_by_id(snapshot_id) click to toggle source
# 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
wait_for_image_death(ami, opts={}) click to toggle source
# 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_for_image_state(state, ami, opts={}) click to toggle source

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
wait_for_instance_death(instance, opts={}) click to toggle source

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
wait_for_instance_status(status, instance, opts={}) click to toggle source
# 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
wait_for_snapshot_status(status, snapshot, opts={}) click to toggle source
# 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
wait_for_volume_status(status, volume, opts={}) click to toggle source
# 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