Sha256: 69471fc513d9dd4d7aa3b4d2f372b053133bb6a006725890aaaea7fb05f6ed72

Contents?: true

Size: 1.17 KB

Versions: 2

Compression:

Stored size: 1.17 KB

Contents

module Ec2ssh
  class AwsEnvNotDefined < StandardError; end
  class Hosts
    def initialize
      @access_key_id = ENV["AMAZON_ACCESS_KEY_ID"].to_s
      @secret_access_key = ENV["AMAZON_SECRET_ACCESS_KEY"].to_s
      if @access_key_id.length == 0 || @secret_access_key.length == 0
        raise AwsEnvNotDefined
      end
    end

    def all
      ec2 = RightAws::Ec2.new(@access_key_id, @secret_access_key)
      regions = ec2.describe_regions
      regions.map do |region|
        process_region region
      end.flatten
    end

    private
    def process_region(region)
      ec2 = RightAws::Ec2.new(@access_key_id, @secret_access_key, :region => region)
      instance_names = {}
      ec2.describe_tags.each do |tag|
        next unless tag[:key]=='Name' && tag[:resource_type]=='instance'
        instance_names[tag[:resource_id]] = tag[:value]
      end
      ec2.describe_instances.map do |instance|
        instance_name = instance_names[instance[:aws_instance_id]] or next nil
        dns_name = instance[:dns_name] or next nil
        instance_name.empty? || dns_name.empty? ? nil : {:host => "#{instance_name}.#{region}", :dns_name => dns_name}
      end.compact
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ec2ssh-1.0.3 lib/ec2ssh/hosts.rb
ec2ssh-1.0.2 lib/ec2ssh/hosts.rb