Class: OpsWalrus::Inventory
- Inherits:
-
Object
- Object
- OpsWalrus::Inventory
- Defined in:
- lib/opswalrus/inventory.rb
Instance Method Summary collapse
- #env ⇒ Object
- #hosts ⇒ Object
- #hosts_files ⇒ Object
-
#initialize(host_references = [HostsFile::DEFAULT_FILE_NAME], tags = []) ⇒ Inventory
constructor
host_references is an array of host names and path strings that reference hosts.yaml files tags is an array of strings.
- #read_secret(secret_name) ⇒ Object
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], = []) @host_references = host_references @tags = end |
Instance Method Details
#env ⇒ Object
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 |
#hosts ⇒ Object
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.) 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..include? t } end end.reject{|host| host.ignore? } selected_hosts.sort_by(&:to_s) end |
#hosts_files ⇒ Object
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 |