Class: Ec2ssh::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/ec2ssh/builder.rb

Instance Method Summary collapse

Constructor Details

#initialize(container) ⇒ Builder

Returns a new instance of Builder.



7
8
9
10
# File 'lib/ec2ssh/builder.rb', line 7

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

Instance Method Details

#aws_keysObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/ec2ssh/builder.rb', line 30

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

#build_host_linesObject



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/ec2ssh/builder.rb', line 12

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

#ec2sObject



26
27
28
# File 'lib/ec2ssh/builder.rb', line 26

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

#filtersObject



45
46
47
48
49
50
# File 'lib/ec2ssh/builder.rb', line 45

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