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
11
12
# File 'lib/ec2ssh/builder.rb', line 7

def initialize(container)
  @container = container
  safe_level = nil
  erb_trim_mode = '%-'
  @host_lines_erb = ERB.new @container.host_line, safe_level, erb_trim_mode
end

Instance Method Details

#aws_keysObject



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

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



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

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



28
29
30
# File 'lib/ec2ssh/builder.rb', line 28

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

#filtersObject



47
48
49
50
51
52
# File 'lib/ec2ssh/builder.rb', line 47

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