Sha256: 9900f34f569f0825c12d1fdea3ef10f6013a1292aa2524344d590d581aff8414

Contents?: true

Size: 1.68 KB

Versions: 15

Compression:

Stored size: 1.68 KB

Contents

module AwsEc2::Waiter
  class Ami < AwsEc2::Base
    include AwsEc2::AwsService

    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

15 entries across 15 versions & 1 rubygems

Version Path
aws-ec2-1.4.9 lib/aws_ec2/waiter/ami.rb
aws-ec2-1.4.8 lib/aws_ec2/waiter/ami.rb
aws-ec2-1.4.7 lib/aws_ec2/waiter/ami.rb
aws-ec2-1.4.6 lib/aws_ec2/waiter/ami.rb
aws-ec2-1.4.5 lib/aws_ec2/waiter/ami.rb
aws-ec2-1.4.4 lib/aws_ec2/waiter/ami.rb
aws-ec2-1.4.3 lib/aws_ec2/waiter/ami.rb
aws-ec2-1.4.2 lib/aws_ec2/waiter/ami.rb
aws-ec2-1.4.1 lib/aws_ec2/waiter/ami.rb
aws-ec2-1.4.0 lib/aws_ec2/waiter/ami.rb
aws-ec2-1.3.2 lib/aws_ec2/waiter/ami.rb
aws-ec2-1.3.1 lib/aws_ec2/waiter/ami.rb
aws-ec2-1.3.0 lib/aws_ec2/waiter/ami.rb
aws-ec2-1.2.2 lib/aws_ec2/waiter/ami.rb
aws-ec2-1.2.1 lib/aws_ec2/waiter/ami.rb