Module: Facter

Includes:
ApiDebugger
Defined in:
lib/custom_facts/util/fact.rb,
lib/facter.rb,
lib/util/file_helper.rb,
lib/framework/cli/cli.rb,
lib/resolvers/aix/nim.rb,
lib/models/loaded_fact.rb,
lib/resolvers/utils/ssh.rb,
lib/models/resolved_fact.rb,
lib/models/searched_fact.rb,
lib/resolvers/docker_lxc.rb,
lib/resolvers/partitions.rb,
lib/framework/utils/utils.rb,
lib/resolvers/windows/ssh.rb,
lib/framework/core/options.rb,
lib/models/fact_collection.rb,
lib/resolvers/dmi_resolver.rb,
lib/resolvers/ssh_resolver.rb,
lib/resolvers/base_resolver.rb,
lib/resolvers/disk_resolver.rb,
lib/resolvers/lpar_resolver.rb,
lib/resolvers/path_resolver.rb,
lib/resolvers/ruby_resolver.rb,
lib/resolvers/wpar_resolver.rb,
lib/framework/logging/logger.rb,
lib/resolvers/agent_resolver.rb,
lib/resolvers/aix/partitions.rb,
lib/resolvers/aix/processors.rb,
lib/resolvers/debian_version.rb,
lib/resolvers/uname_resolver.rb,
lib/facts_utils/uptime_parser.rb,
lib/resolvers/aix/mountpoints.rb,
lib/resolvers/augeas_resolver.rb,
lib/resolvers/memory_resolver.rb,
lib/resolvers/uptime_resolver.rb,
lib/facts_utils/unit_converter.rb,
lib/framework/core/fact_filter.rb,
lib/resolvers/selinux_resolver.rb,
lib/resolvers/sw_vers_resolver.rb,
lib/custom_facts/core/aggregate.rb,
lib/custom_facts/core/execution.rb,
lib/framework/core/fact_manager.rb,
lib/resolvers/hostname_resolver.rb,
lib/resolvers/identity_resolver.rb,
lib/resolvers/os_level_resolver.rb,
lib/resolvers/timezone_resolver.rb,
lib/resolvers/utils/fingerprint.rb,
lib/custom_facts/util/resolution.rb,
lib/framework/config/fact_groups.rb,
lib/framework/core/cache_manager.rb,
lib/framework/core/session_cache.rb,
lib/resolvers/aix/ffi/ffi_helper.rb,
lib/resolvers/bsd/ffi/ffi_helper.rb,
lib/resolvers/solaris/processors.rb,
lib/framework/core/fact_augmenter.rb,
lib/resolvers/aix/utils/odm_query.rb,
lib/resolvers/macosx/dmi_resolver.rb,
lib/resolvers/os_release_resolver.rb,
lib/resolvers/processors_resolver.rb,
lib/framework/config/config_reader.rb,
lib/framework/parsers/query_parser.rb,
lib/resolvers/eos_release_resolver.rb,
lib/resolvers/filesystems_resolver.rb,
lib/resolvers/lsb_release_resolver.rb,
lib/resolvers/mountpoints_resolver.rb,
lib/resolvers/solaris/zfs_resolver.rb,
lib/framework/detector/os_hierarchy.rb,
lib/resolvers/aix/hardware_resolver.rb,
lib/resolvers/aix/os_level_resolver.rb,
lib/resolvers/fips_enabled_resolver.rb,
lib/resolvers/solaris/zone_resolver.rb,
lib/resolvers/suse_release_resolver.rb,
lib/resolvers/windows/fips_resolver.rb,
lib/custom_facts/core/execution/base.rb,
lib/resolvers/facterversion_resolver.rb,
lib/resolvers/load_averages_resolver.rb,
lib/resolvers/solaris/zpool_resolver.rb,
lib/custom_facts/core/execution/posix.rb,
lib/resolvers/aix/filesystem_resolver.rb,
lib/resolvers/aix/networking_resolver.rb,
lib/resolvers/puppet_version_resolver.rb,
lib/resolvers/redhat_release_resolver.rb,
lib/resolvers/utils/filesystem_helper.rb,
lib/resolvers/windows/kernel_resolver.rb,
lib/resolvers/windows/memory_resolver.rb,
lib/resolvers/windows/netkvm_resolver.rb,
lib/resolvers/windows/uptime_resolver.rb,
lib/facts_utils/windows_release_finder.rb,
lib/custom_facts/core/execution/windows.rb,
lib/framework/core/options/option_store.rb,
lib/resolvers/aix/architecture_resolver.rb,
lib/resolvers/macosx/ipaddress_resolver.rb,
lib/resolvers/macosx/processor_resolver.rb,
lib/resolvers/networking_linux_resolver.rb,
lib/resolvers/solaris/solaris_zone_name.rb,
lib/resolvers/windows/dmi_bios_resolver.rb,
lib/resolvers/windows/identity_resolver.rb,
lib/resolvers/windows/system32_resolver.rb,
lib/resolvers/aix/load_averages_resolver.rb,
lib/resolvers/bsd/load_averages_resolver.rb,
lib/resolvers/solaris/ipaddress_resolver.rb,
lib/framework/formatters/formatter_helper.rb,
lib/resolvers/macosx/filesystems_resolver.rb,
lib/resolvers/macosx/mountpoints_resolver.rb,
lib/resolvers/macosx/swap_memory_resolver.rb,
lib/resolvers/windows/networking_resolver.rb,
lib/resolvers/windows/processors_resolver.rb,
lib/framework/core/fact/internal/core_fact.rb,
lib/framework/formatters/formatter_factory.rb,
lib/resolvers/solaris/filesystems_resolver.rb,
lib/framework/core/fact_loaders/fact_loader.rb,
lib/resolvers/macosx/load_averages_resolver.rb,
lib/resolvers/macosx/system_memory_resolver.rb,
lib/framework/core/options/options_validator.rb,
lib/framework/formatters/json_fact_formatter.rb,
lib/framework/formatters/yaml_fact_formatter.rb,
lib/framework/formatters/hocon_fact_formatter.rb,
lib/resolvers/macosx/system_profiler_resolver.rb,
lib/resolvers/windows/virtualization_resolver.rb,
lib/framework/core/options/config_file_options.rb,
lib/framework/formatters/legacy_fact_formatter.rb,
lib/resolvers/freebsd/freebsd_version_resolver.rb,
lib/resolvers/windows/product_release_resolver.rb,
lib/framework/core/fact_loaders/class_discoverer.rb,
lib/resolvers/solaris/solaris_os_release_resolver.rb,
lib/resolvers/windows/dmi_computersystem_resolver.rb,
lib/resolvers/windows/win_os_description_resolver.rb,
lib/framework/core/fact_loaders/external_fact_loader.rb,
lib/framework/core/fact_loaders/internal_fact_loader.rb,
lib/resolvers/windows/hardware_architecture_resolver.rb,
lib/framework/core/fact/external/external_fact_manager.rb,
lib/framework/core/fact/internal/internal_fact_manager.rb

Overview

CuAt (Customized Attributes) non-default attribute values CuDv (Customized Devices) the devices present on this machine PdAt (Predefined Attributes) default values for all device attributes PdDv (Predefined Devices) the list of all devices supported by this release of AIX

Defined Under Namespace

Modules: Aix, Bsd, Core, FactAugmenter, FactsUtils, FilesystemHelper, OptionsValidator, Resolvers, Util, Utils Classes: CacheManager, ClassDiscoverer, Cli, ConfigFileOptions, ConfigReader, CoreFact, ExternalFactLoader, ExternalFactManager, FactCollection, FactFilter, FactGroups, FactLoader, FactManager, FingerPrint, FormatterFactory, FormatterHelper, HoconFactFormatter, InternalFactLoader, InternalFactManager, JsonFactFormatter, LegacyFactFormatter, LoadedFact, Log, ODMQuery, OptionStore, Options, OsHierarchy, QueryParser, ResolveCustomFactError, ResolvedFact, SearchedFact, SessionCache, Ssh, UptimeParser, WindowsReleaseFinder, YamlFactFormatter

Constant Summary collapse

RED =
31
GREEN =
32
YELLOW =
33
CYAN =
36
DEFAULT_LOG_LEVEL =
:warn

Class Method Summary collapse

Methods included from ApiDebugger

parse_options, prepended

Class Method Details

.[](name) ⇒ Facter::Util::Fact?

Alias method for Facter.fact()

Parameters:

  • name (string)

    fact name

Returns:



31
32
33
# File 'lib/facter.rb', line 31

def [](name)
  fact(name)
end

.add(name, options = {}, &block) ⇒ Facter::Util::Fact

Add custom facts to fact collection

Parameters:

  • name (String)

    Custom fact name

  • options (defaults to: {})

    {} [Hash] optional parameters for the fact - attributes

    of Facter::Util::Fact and Facter::Util::Resolution can be supplied here

  • block (Proc)

    a block defining a fact resolution

Returns:

  • (Facter::Util::Fact)

    the fact object, which includes any previously defined resolutions



46
47
48
49
50
# File 'lib/facter.rb', line 46

def add(name, options = {}, &block)
  options[:fact_type] = :custom
  LegacyFacter.add(name, options, &block)
  LegacyFacter.collection.invalidate_custom_facts
end

.clearnil

Clears all cached values and removes all facts from memory.

Returns:

  • (nil)


57
58
59
60
61
62
63
# File 'lib/facter.rb', line 57

def clear
  @already_searched = {}
  LegacyFacter.clear
  Options[:custom_dir] = []
  LegacyFacter.collection.invalidate_custom_facts
  LegacyFacter.collection.reload_custom_facts
end

.clear_messagesObject



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

def clear_messages
  logger.debug('clear_messages is not implemented')
end

.core_value(user_query) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/facter.rb', line 65

def core_value(user_query)
  user_query = user_query.to_s
  resolved_facts = Facter::FactManager.instance.resolve_core([user_query])
  fact_collection = FactCollection.new.build_fact_collection!(resolved_facts)
  splitted_user_query = Facter::Utils.split_user_query(user_query)
  fact_collection.dig(*splitted_user_query)
end

.debug(msg) ⇒ nil

Prints out a debug message when debug option is set to true

Parameters:

  • msg (String)

    Message to be printed out

Returns:

  • (nil)


79
80
81
82
83
84
# File 'lib/facter.rb', line 79

def debug(msg)
  return unless debugging?

  logger.debug(msg)
  nil
end

.debugging(debug_bool) ⇒ type

Enable or disable debugging

Parameters:

  • debug_bool (bool)

    State which debugging should have

Returns:

  • (type)
    description


105
106
107
# File 'lib/facter.rb', line 105

def debugging(debug_bool)
  Facter::Options[:debug] = debug_bool
end

.debugging?bool

Check whether debuging is enabled

Returns:

  • (bool)


95
96
97
# File 'lib/facter.rb', line 95

def debugging?
  Options[:debug]
end

.fact(user_query) ⇒ Facter::Util::Fact?

Returns a fact object by name. If you use this, you still have to call ‘value` on it to retrieve the actual value.

Parameters:

  • name (String)

    the name of the fact

Returns:



119
120
121
122
123
124
# File 'lib/facter.rb', line 119

def fact(user_query)
  user_query = user_query.to_s
  resolve_fact(user_query)

  @already_searched[user_query]
end

.log_exception(exception, message = :default) ⇒ Object



253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/facter.rb', line 253

def log_exception(exception, message = :default)
  arr = []
  if message == :default
    arr << exception.message
  elsif message
    arr << message
  end
  if @trace
    arr << 'backtrace:'
    arr.concat(exception.backtrace)
  end

  logger.error(arr.flatten.join("\n"))
end

.on_message(&block) ⇒ Object



86
87
88
# File 'lib/facter.rb', line 86

def on_message(&block)
  Facter::Log.on_message(&block)
end

.resetnil

Reset search paths for custom and external facts If config file is set custom and external facts will be reloaded

Returns:

  • (nil)


132
133
134
135
136
137
# File 'lib/facter.rb', line 132

def reset
  LegacyFacter.reset
  Options[:custom_dir] = []
  Options[:external_dir] = []
  nil
end

.search(*dirs) ⇒ void

This method returns an undefined value.

Register directories to be searched for custom facts. The registered directories must be absolute paths or they will be ignored.

Parameters:

  • dirs (Array<String>)

    An array of searched directories



147
148
149
# File 'lib/facter.rb', line 147

def search(*dirs)
  Options[:custom_dir] += dirs
end

.search_external(dirs) ⇒ void

This method returns an undefined value.

Registers directories to be searched for external facts.

Parameters:

  • dirs (Array<String>)

    An array of searched directories



158
159
160
# File 'lib/facter.rb', line 158

def search_external(dirs)
  Options[:external_dir] += dirs
end

.search_external_pathArray<String>

Returns the registered search directories.for external facts.

Returns:

  • (Array<String>)

    An array of searched directories



167
168
169
# File 'lib/facter.rb', line 167

def search_external_path
  Options.external_dir
end

.search_pathArray<String>

Returns the registered search directories for custom facts.

Returns:

  • (Array<String>)

    An array of the directories searched



176
177
178
# File 'lib/facter.rb', line 176

def search_path
  Options.custom_dir
end

.to_hashFactCollection

Gets a hash mapping fact names to their values The hash contains core facts, legacy facts, custom facts and external facts (all facts that can be resolved).

Returns:



186
187
188
189
190
191
192
# File 'lib/facter.rb', line 186

def to_hash
  log_blocked_facts

  resolved_facts = Facter::FactManager.instance.resolve_facts
  Facter::SessionCache.invalidate_all_caches
  Facter::FactCollection.new.build_fact_collection!(resolved_facts)
end

.to_user_output(cli_options, *args) ⇒ Array

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Gets a hash mapping fact names to their values

Returns:

  • (Array)

    the hash of fact names and values



240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/facter.rb', line 240

def to_user_output(cli_options, *args)
  init_cli_options(cli_options, args)
  logger.info("executed with command line: #{ARGV.drop(1).join(' ')}")
  log_blocked_facts
  resolved_facts = Facter::FactManager.instance.resolve_facts(args)
  SessionCache.invalidate_all_caches
  fact_formatter = Facter::FormatterFactory.build(Facter::Options.get)

  status = error_check(args, resolved_facts)

  [fact_formatter.format(resolved_facts), status || 0]
end

.trace(bool) ⇒ type

Enable or disable trace

Parameters:

  • debug_bool (bool)

    Set trace on debug state

Returns:

  • (type)
    description


209
210
211
# File 'lib/facter.rb', line 209

def trace(bool)
  @trace = bool
end

.trace?bool

Check whether printing stack trace is enabled

Returns:

  • (bool)


199
200
201
# File 'lib/facter.rb', line 199

def trace?
  @trace
end

.value(user_query) ⇒ String

Gets the value for a fact. Returns ‘nil` if no such fact exists.

Parameters:

  • name (String)

    the fact name

Returns:

  • (String)

    the value of the fact, or nil if no fact is found



219
220
221
222
223
# File 'lib/facter.rb', line 219

def value(user_query)
  user_query = user_query.to_s
  resolve_fact(user_query)
  @already_searched[user_query]&.value
end

.versionString

Returns Facter version

Returns:

  • (String)

    Current version



230
231
232
233
# File 'lib/facter.rb', line 230

def version
  version_file = ::File.join(ROOT_DIR, 'VERSION')
  ::File.read(version_file).strip
end