Module: FacterDB

Defined in:
lib/facterdb.rb,
lib/facterdb/bin.rb,
lib/facterdb/version.rb

Defined Under Namespace

Modules: Version Classes: Bin

Class Method Summary collapse

Class Method Details

.databaseString

Returns - returns a giant incomprehensible string of concatenated json data.

Returns:

  • (String)
    • returns a giant incomprehensible string of concatenated json data



7
8
9
# File 'lib/facterdb.rb', line 7

def self.database
  @database ||= "[#{facterdb_fact_files.map { |f| read_json_file(f) }.join(',')}]\n"
end

.default_fact_filesArray[String]

Returns - list of all files found in the default facterdb facts path.

Returns:

  • (Array[String])
    • list of all files found in the default facterdb facts path



38
39
40
41
42
43
# File 'lib/facterdb.rb', line 38

def self.default_fact_files
  return [] unless use_defaultdb?
  proj_root = File.join(File.dirname(File.dirname(__FILE__)))
  facts_dir = File.expand_path(File.join(proj_root, 'facts'))
  Dir.glob(File.join(facts_dir, "**", '*.facts'))
end

.external_fact_files(fact_paths = ) ⇒ Array[String]

Returns - list of all files found in the user supplied facterdb facts path.

Parameters:

  • fact_paths (String) (defaults to: )
    • a comma separated list of paths to search for fact files

Returns:

  • (Array[String])
    • list of all files found in the user supplied facterdb facts path



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/facterdb.rb', line 47

def self.external_fact_files(fact_paths = ENV['FACTERDB_SEARCH_PATHS'])
  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_filesArray[String]

Note:

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.

Returns:

  • (Array[String])
    • list of all files found in the default facterdb facts path and user supplied path



63
64
65
# File 'lib/facterdb.rb', line 63

def self.facterdb_fact_files
  (external_fact_files + default_fact_files).uniq
end

.get_facts(filter = nil) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/facterdb.rb', line 95

def self.get_facts(filter=nil)
  if filter.is_a?(Array)
    filter_str = '(' + filter.map { |f| f.map { |k,v | "#{k}=#{v}" }.join(' and ') }.join(') or (') + ')'
  elsif filter.is_a?(Hash)
    filter_str = filter.map { |k,v | "#{k}=#{v}" }.join(' and ')
  elsif filter.is_a?(String)
    filter_str = filter
  elsif filter == nil
    filter_str = ''
  else
    raise 'filter must be either an Array a Hash or a String'
  end
  JGrep.jgrep(database, filter_str).map { |hash| Hash[hash.map{ |k, v| [k.to_sym, v] }] }
end

.get_os_facts(facter_version = '*', filter = []) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/facterdb.rb', line 67

def self.get_os_facts(facter_version='*', filter=[])
  if facter_version == '*'
    if filter.is_a?(Array)
      filter_str = filter.map { |f| f.map { |k,v | "#{k}=#{v}" }.join(' and ') }.join(' or ')
    elsif filter.is_a?(Hash)
      filter_str = filter.map { |k,v | "#{k}=#{v}" }.join(' and ')
    elsif filter.is_a?(String)
      filter_str = filter
    else
      raise 'filter must be either an Array a Hash or a String'
    end
  else
    if filter.is_a?(Array)
      filter_str = "facterversion=/^#{facter_version}/ and (#{filter.map { |f| f.map { |k,v | "#{k}=#{v}" }.join(' and ') }.join(' or ')})"
    elsif filter.is_a?(Hash)
      filter_str = "facterversion=/^#{facter_version}/ and (#{filter.map { |k,v | "#{k}=#{v}" }.join(' and ')})"
    elsif filter.is_a?(String)
      filter_str = "facterversion=/^#{facter_version}/ and (#{filter})"
    else
      raise 'filter must be either an Array a Hash or a String'
    end
  end

  warn "[DEPRECATION] `get_os_facts` is deprecated. Please use `get_facts(#{filter_str})` instead."

  get_facts(filter_str)
end

.inject_source?Boolean

The default is false.

Returns:

  • (Boolean)
    • returns true if we should inject the source file name and file path into the json factsets.



20
21
22
# File 'lib/facterdb.rb', line 20

def self.inject_source?
  !ENV['FACTERDB_INJECT_SOURCE'].nil?
end

.use_defaultdb?Boolean

Note:

If the user passes anything to the FACTERDB_SKIP_DEFAULTDB environment variable we assume

they want to skip the default db

Returns:

  • (Boolean)
    • returns true if we should use the default facterdb database, false otherwise



14
15
16
# File 'lib/facterdb.rb', line 14

def self.use_defaultdb?
  ENV['FACTERDB_SKIP_DEFAULTDB'].nil?
end