Sha256: 9cfa27a5018175b9f471339a5a7dac1f229d7f727fa921028f3916ec8993a6d5

Contents?: true

Size: 1.65 KB

Versions: 6

Compression:

Stored size: 1.65 KB

Contents

require 'aws-sdk'
require_relative '../util'

module Builderator
  module Model

    def self.instances
      @instances ||= Instances.new
    end
    ##
    # EC2 Instance resources
    ##
    class Instances < Model::Base
      PROPERTIES = %w(private_dns_name public_dns_name instance_type
                      subnet_id vpc_id private_ip_address public_ip_address
                      architecture root_device_type virtualization_type
                      hypervisor)

      def fetch
        @resources = {}.tap do |i|
          Util.ec2.describe_instances(:filters => [
            {
              :name => 'instance-state-name',
              :values => %w(pending running shutting-down stopping stopped)
            }
          ]).each do |page|
            page.reservations.each do |r|
              r.instances.each do |instance|
                properties = Util.from_tags(instance.tags)
                properties['availability_zone'] = instance.placement.availability_zone
                properties['creation_date'] = instance.launch_time.to_datetime
                PROPERTIES.each { |pp| properties[pp] = instance[pp.to_sym] }

                i[instance.instance_id] = {
                  :id => instance.instance_id,
                  :image => instance.image_id,
                  :volumes => instance.block_device_mappings.map { |b| b.ebs.volume_id },
                  :properties => properties
                }
              end
            end
          end
        end
      end

      def images
        resources.values.map { |i| i[:image] }
      end

      def volumes
        resources.values.map { |i| i[:volumes] }.flatten
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
builderator-0.3.15 lib/builderator/model/instances.rb
builderator-0.3.14 lib/builderator/model/instances.rb
builderator-0.3.13 lib/builderator/model/instances.rb
builderator-0.3.12 lib/builderator/model/instances.rb
builderator-0.3.11 lib/builderator/model/instances.rb
builderator-0.3.10 lib/builderator/model/instances.rb