Module: FacterDB
- Defined in:
- lib/facterdb.rb,
lib/facterdb/bin.rb,
lib/facterdb/version.rb
Defined Under Namespace
Modules: Errors, Version Classes: Bin
Class Method Summary collapse
- .cleanup ⇒ Object
-
.database ⇒ String
Returns a giant incomprehensible string of concatenated json data.
-
.default_fact_files ⇒ Array[String]
List of all files found in the default facterdb facts path.
-
.external_fact_files(fact_paths = ENV.fetch('FACTERDB_SEARCH_PATHS', nil)) ⇒ Array[String]
List of all files found in the user supplied facterdb facts path.
-
.facterdb_fact_files ⇒ Array[String]
List of all files found in the default facterdb facts path and user supplied path.
-
.generate_filter_str(filter = nil) ⇒ String
The string filter.
-
.get_facts(filter = nil, cache = true, symbolize_keys: true) ⇒ Array[Hash[Symbol, Any]]
Array of hashes of facts.
-
.inject_source? ⇒ Boolean
The default is false.
-
.use_defaultdb? ⇒ Boolean
they want to skip the default db.
-
.valid_filters?(filters) ⇒ Boolean
True if the filter is valid against the jgrep filter validator.
Class Method Details
.cleanup ⇒ Object
Call this method at the end of test suite, for example via after(:suite), to reclaim back the memory required to hold json data and filter cache
14 15 16 17 18 |
# File 'lib/facterdb.rb', line 14 def self.cleanup @database = nil Thread.current[:facterdb_last_filter_seen] = nil Thread.current[:facterdb_last_facts_seen] = nil end |
.database ⇒ String
Returns a giant incomprehensible string of concatenated json data
9 10 11 |
# File 'lib/facterdb.rb', line 9 def self.database @database ||= "[#{facterdb_fact_files.map { |f| read_json_file(f) }.join(',')}]\n" end |
.default_fact_files ⇒ Array[String]
Returns list of all files found in the default facterdb facts path.
49 50 51 52 53 54 55 |
# File 'lib/facterdb.rb', line 49 def self.default_fact_files return [] unless use_defaultdb? proj_root = File.join(File.dirname(File.dirname(__FILE__))) facts_dir = File.(File.join(proj_root, 'facts')) Dir.glob(File.join(facts_dir, '**', '*.facts')) end |
.external_fact_files(fact_paths = ENV.fetch('FACTERDB_SEARCH_PATHS', nil)) ⇒ Array[String]
Returns list of all files found in the user supplied facterdb facts path.
59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/facterdb.rb', line 59 def self.external_fact_files(fact_paths = ENV.fetch('FACTERDB_SEARCH_PATHS', nil)) fact_paths ||= '' return [] if fact_paths.empty? paths = fact_paths.split(File::PATH_SEPARATOR).map do |fact_path| unless File.directory?(fact_path) warn("[FACTERDB] Ignoring external facts path #{fact_path} as it is not a directory") next nil end fact_path = fact_path.gsub(File::ALT_SEPARATOR, File::SEPARATOR) if File::ALT_SEPARATOR File.join(fact_path.strip, '**', '*.facts') end.compact Dir.glob(paths) end |
.facterdb_fact_files ⇒ Array[String]
external fact files supplied by the user will take precedence over default fact files found in this gem
Returns list of all files found in the default facterdb facts path and user supplied path.
76 77 78 |
# File 'lib/facterdb.rb', line 76 def self.facterdb_fact_files (external_fact_files + default_fact_files).uniq end |
.generate_filter_str(filter = nil) ⇒ String
Returns the string filter.
82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/facterdb.rb', line 82 def self.generate_filter_str(filter = nil) case filter when ::Array '(' + filter.map { |f| f.map { |k, v| "#{k}=#{v}" }.join(' and ') }.join(') or (') + ')' when ::Hash filter.map { |k, v| "#{k}=#{v}" }.join(' and ') when ::String filter when ::NilClass '' else raise Errors::InvalidFilter, "filter must be either an Array a Hash, String, or nil, received #{filter.class}" end end |
.get_facts(filter = nil, cache = true, symbolize_keys: true) ⇒ Array[Hash[Symbol, Any]]
Returns array of hashes of facts.
111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/facterdb.rb', line 111 def self.get_facts(filter = nil, cache = true, symbolize_keys: true) if cache && filter && filter == Thread.current[:facterdb_last_filter_seen] return Thread.current[:facterdb_last_facts_seen] end filter_str = generate_filter_str(filter) result = JGrep.jgrep(database, filter_str) result = result.map { |hash| hash.transform_keys(&:to_sym) } if symbolize_keys if cache Thread.current[:facterdb_last_filter_seen] = filter Thread.current[:facterdb_last_facts_seen] = result end result end |
.inject_source? ⇒ Boolean
The default is false.
29 30 31 |
# File 'lib/facterdb.rb', line 29 def self.inject_source? !ENV['FACTERDB_INJECT_SOURCE'].nil? end |
.use_defaultdb? ⇒ Boolean
If the user passes anything to the FACTERDB_SKIP_DEFAULTDB environment variable we assume
they want to skip the default db
23 24 25 |
# File 'lib/facterdb.rb', line 23 def self.use_defaultdb? ENV['FACTERDB_SKIP_DEFAULTDB'].nil? end |
.valid_filters?(filters) ⇒ Boolean
Returns true if the filter is valid against the jgrep filter validator.
99 100 101 102 103 104 |
# File 'lib/facterdb.rb', line 99 def self.valid_filters?(filters) filter_str = generate_filter_str(filters) JGrep.validate_filters(filter_str).nil? rescue RuntimeError false end |