Class: OpsWalrus::Inventory

Inherits:
Object
  • Object
show all
Defined in:
lib/opswalrus/inventory.rb

Instance Method Summary collapse

Constructor Details

#initialize(host_references = [HostsFile::DEFAULT_FILE_NAME], tags = []) ⇒ Inventory

host_references is an array of host names and path strings that reference hosts.yaml files tags is an array of strings



10
11
12
13
# File 'lib/opswalrus/inventory.rb', line 10

def initialize(host_references = [HostsFile::DEFAULT_FILE_NAME], tags = [])
  @host_references = host_references
  @tags = tags
end

Instance Method Details

#envObject



15
16
17
# File 'lib/opswalrus/inventory.rb', line 15

def env
  @env ||= hosts_files.reduce({}) {|memo, hosts_file| memo.deep_merge(hosts_file.env) }
end

#hostsObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/opswalrus/inventory.rb', line 27

def hosts()
  hosts_files, host_strings = @host_references.partition {|ref| File.exist?(ref) }
  inventory_file_hosts = hosts_files.
                            map {|file_path| HostsFile.new(file_path) }.
                            reduce({}) do |host_map, hosts_file|
                              hosts_file.hosts.each do |host|
                                (host_map[host] ||= host).tag!(host.tags)
                              end

                              host_map
                            end.
                            keys
  untagged_hosts = host_strings.map(&:strip).uniq.map {|host| Host.new(host) }
  all_hosts = untagged_hosts + inventory_file_hosts

  selected_hosts = if @tags.empty?
    all_hosts
  else
    all_hosts.select do |host|
      @tags.all? {|t| host.tags.include? t }
    end
  end.reject{|host| host.ignore? }

  selected_hosts.sort_by(&:to_s)
end

#hosts_filesObject



23
24
25
# File 'lib/opswalrus/inventory.rb', line 23

def hosts_files
  @hosts_files ||= @host_references.select {|ref| File.exist?(ref) }.map {|file_path| HostsFile.new(file_path) }
end

#read_secret(secret_name) ⇒ Object



19
20
21
# File 'lib/opswalrus/inventory.rb', line 19

def read_secret(secret_name)
  hosts_files.find {|hosts_file| hosts_file.has_secret?(secret_name) }&.read_secret(secret_name)
end