Sha256: 2182366f1e0b48a620580672dedd551c5884478fb717652521b24d4464fbd898

Contents?: true

Size: 1.44 KB

Versions: 1

Compression:

Stored size: 1.44 KB

Contents

require 'ec2ssh/ec2_instances'
require 'erb'
require 'stringio'

module Ec2ssh
  class Builder
    def initialize(container)
      @container = container
      @host_lines_erb = ERB.new @container.host_line, trim_mode: '%-'
    end

    def build_host_lines
      out = StringIO.new
      aws_keys.each do |name, key|
        out.puts "# section: #{name}"
        ec2s.instances(name).each do |instance|
          bind = instance.instance_eval { binding }
          next if @container.reject && @container.reject.call(instance)
          line = @host_lines_erb.result(bind).rstrip
          out.puts line unless line.empty?
        end
      end
      out.string.rstrip
    end

    def ec2s
      @ec2s ||= Ec2Instances.new aws_keys, filters
    end

    def aws_keys
      @aws_keys ||= if @container.profiles
                      keys = {}
                      @container.profiles.each do |profile_name|
                        keys[profile_name] = {}
                        @container.regions.each do |region|
                          keys[profile_name][region] = Ec2Instances.expand_profile_name_to_credential profile_name, region
                        end
                      end
                      keys
                    else
                      @container.aws_keys
                    end
    end

    def filters
      @filters = @container.filters || [{
        name: 'instance-state-name',
        values: ['running']
      }]
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ec2ssh-5.1.0 lib/ec2ssh/builder.rb