Sha256: aad1817a53d1449ccf9043d008f37a702e49dbe6d8ac8d9d19133ae195368ef1

Contents?: true

Size: 785 Bytes

Versions: 3

Compression:

Stored size: 785 Bytes

Contents

require 'aws-sdk'

module Ec2ssh
  class Ec2Instances
    attr_reader :ec2s, :aws_keys

    def initialize(aws_keys, regions)
      @aws_keys = aws_keys
      @regions = regions
    end

    def make_ec2s
      AWS.start_memoizing
      _ec2s = {}
      aws_keys.each do |name, key|
        _ec2s[name] = {}
        @regions.each do |region|
          options = key.merge ec2_region: region
          _ec2s[name][region] = AWS::EC2.new options
        end
      end
      _ec2s
    end

    def ec2s
      @ec2s ||= make_ec2s
    end

    def instances(key_name)
      @regions.map {|region|
        ec2s[key_name][region].instances.
          filter('instance-state-name', 'running').
          to_a.
          sort_by {|ins| ins.tags['Name'].to_s }
      }.flatten
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ec2ssh-3.0.3 lib/ec2ssh/ec2_instances.rb
ec2ssh-3.0.2 lib/ec2ssh/ec2_instances.rb
ec2ssh-3.0.1 lib/ec2ssh/ec2_instances.rb