class AwsEc2::Waiter::Ami

Public Instance Methods

wait() click to toggle source
# File lib/aws_ec2/waiter/ami.rb, line 5
def wait
  delay = 30
  timeout = @options[:timeout]
  max_attempts = timeout / delay
  current_time = 0

  puts "Waiting for #{@options[:name]} to be available. Delay: #{delay}s. Timeout: #{timeout}s"
  puts "Current time: #{Time.now}"
  return if ENV['TEST']

  # Using while loop because of issues with ruby's Timeout module
  # http://www.mikeperham.com/2015/05/08/timeout-rubys-most-dangerous-api/
  detected = detect_ami
  until detected || current_time > timeout
    print '.'
    sleep delay
    current_time += 30
    detected = detect_ami
  end
  puts

  if current_time > timeout
    puts "ERROR: Timeout. Unable to detect and available ami: #{@options[:name]}"
    exit 1
  else
    puts "Found available AMI: #{@options[:name]}"
  end
end

Private Instance Methods

detect_ami(owners=["self"]) click to toggle source

Using custom detect_ami instead of ec2.wait_until(:image_availalbe, …) because we start checking for the ami even before we've called create_ami. We start checking right after we launch the instance which will create the ami at the end.

# File lib/aws_ec2/waiter/ami.rb, line 39
def detect_ami(owners=["self"])
  images = ec2.describe_images(
    owners: owners,
    filters: filters
  ).images
  detected = images.first
  !!detected
end
filters() click to toggle source
# File lib/aws_ec2/waiter/ami.rb, line 48
def filters
  name_is_ami_id = @options[:name] =~ /^ami-/

  filters = [{name: "state", values: ["available"]}]
  filters << if name_is_ami_id
      {name: "image-id", values: [@options[:name]]}
    else
      {name: "name", values: [@options[:name]]}
    end

  filters
end