Sha256: 07e4ecee0ca95ab0d1ffc72f45a2d5687bbe249902b0154ba4b63d0751cbcc05

Contents?: true

Size: 1.69 KB

Versions: 3

Compression:

Stored size: 1.69 KB

Contents

module Forger::Waiter
  class Ami < Forger::Base
    include Forger::AwsServices

    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
    # 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.
    def detect_ami(owners=["self"])
      images = ec2.describe_images(
        owners: owners,
        filters: filters
      ).images
      detected = images.first
      !!detected
    end

    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
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
forger-3.0.2 lib/forger/waiter/ami.rb
forger-3.0.1 lib/forger/waiter/ami.rb
forger-3.0.0 lib/forger/waiter/ami.rb