Module: Facter

Includes:
ApiDebugger
Defined in:
lib/facter/util/aix/odm_query.rb,
lib/facter.rb,
lib/facter/config.rb,
lib/facter/version.rb,
lib/facter/util/utils.rb,
lib/facter/resolvers/dmi.rb,
lib/facter/resolvers/ec2.rb,
lib/facter/resolvers/gce.rb,
lib/facter/resolvers/ssh.rb,
lib/facter/resolvers/xen.rb,
lib/facter/resolvers/zfs.rb,
lib/facter/resolvers/disk.rb,
lib/facter/resolvers/lpar.rb,
lib/facter/resolvers/path.rb,
lib/facter/resolvers/ruby.rb,
lib/facter/resolvers/wpar.rb,
lib/facter/resolvers/cloud.rb,
lib/facter/resolvers/lspci.rb,
lib/facter/resolvers/uname.rb,
lib/facter/resolvers/zpool.rb,
lib/facter/resolvers/augeas.rb,
lib/facter/resolvers/memory.rb,
lib/facter/resolvers/uptime.rb,
lib/facter/resolvers/vmware.rb,
lib/facter/util/file_helper.rb,
lib/facter/framework/cli/cli.rb,
lib/facter/resolvers/aix/nim.rb,
lib/facter/resolvers/open_vz.rb,
lib/facter/resolvers/selinux.rb,
lib/facter/resolvers/sw_vers.rb,
lib/facter/util/api_debugger.rb,
lib/facter/models/loaded_fact.rb,
lib/facter/resolvers/hostname.rb,
lib/facter/resolvers/identity.rb,
lib/facter/resolvers/timezone.rb,
lib/facter/util/resolvers/ssh.rb,
lib/facter/resolvers/aix/disks.rb,
lib/facter/resolvers/virt_what.rb,
lib/facter/util/resolvers/http.rb,
lib/facter/models/resolved_fact.rb,
lib/facter/models/searched_fact.rb,
lib/facter/resolvers/aix/memory.rb,
lib/facter/resolvers/containers.rb,
lib/facter/resolvers/dmi_decode.rb,
lib/facter/resolvers/macosx/dmi.rb,
lib/facter/resolvers/networking.rb,
lib/facter/resolvers/os_release.rb,
lib/facter/resolvers/partitions.rb,
lib/facter/resolvers/processors.rb,
lib/facter/resolvers/eos_release.rb,
lib/facter/resolvers/filesystems.rb,
lib/facter/resolvers/freebsd/dmi.rb,
lib/facter/resolvers/lsb_release.rb,
lib/facter/resolvers/mountpoints.rb,
lib/facter/resolvers/solaris/dmi.rb,
lib/facter/resolvers/windows/ssh.rb,
lib/facter/util/windows/win32ole.rb,
lib/facter/custom_facts/util/fact.rb,
lib/facter/framework/core/options.rb,
lib/facter/models/fact_collection.rb,
lib/facter/resolvers/aix/hardware.rb,
lib/facter/resolvers/aix/os_level.rb,
lib/facter/resolvers/fips_enabled.rb,
lib/facter/resolvers/freebsd/geom.rb,
lib/facter/resolvers/solaris/ldom.rb,
lib/facter/resolvers/solaris/zone.rb,
lib/facter/resolvers/suse_release.rb,
lib/facter/resolvers/windows/fips.rb,
lib/facter/util/facts/facts_utils.rb,
lib/facter/resolvers/base_resolver.rb,
lib/facter/resolvers/facterversion.rb,
lib/facter/resolvers/load_averages.rb,
lib/facter/resolvers/solaris/disks.rb,
lib/facter/util/aix/info_extractor.rb,
lib/facter/framework/logging/logger.rb,
lib/facter/resolvers/aix/filesystem.rb,
lib/facter/resolvers/aix/networking.rb,
lib/facter/resolvers/aix/partitions.rb,
lib/facter/resolvers/aix/processors.rb,
lib/facter/resolvers/bsd/processors.rb,
lib/facter/resolvers/debian_version.rb,
lib/facter/resolvers/redhat_release.rb,
lib/facter/resolvers/solaris/memory.rb,
lib/facter/resolvers/windows/kernel.rb,
lib/facter/resolvers/windows/memory.rb,
lib/facter/resolvers/windows/netkvm.rb,
lib/facter/resolvers/windows/uptime.rb,
lib/facter/util/facts/uptime_parser.rb,
lib/facter/util/resolvers/aws_token.rb,
lib/facter/resolvers/aix/mountpoints.rb,
lib/facter/resolvers/freebsd/virtual.rb,
lib/facter/resolvers/solaris/ffi/ffi.rb,
lib/facter/util/facts/unit_converter.rb,
lib/facter/util/resolvers/ssh_helper.rb,
lib/facter/framework/core/fact_filter.rb,
lib/facter/resolvers/aix/architecture.rb,
lib/facter/resolvers/aix/serialnumber.rb,
lib/facter/resolvers/macosx/processor.rb,
lib/facter/resolvers/networking_linux.rb,
lib/facter/resolvers/windows/dmi_bios.rb,
lib/facter/resolvers/windows/identity.rb,
lib/facter/resolvers/windows/system32.rb,
lib/facter/util/resolvers/fingerprint.rb,
lib/facter/custom_facts/core/aggregate.rb,
lib/facter/custom_facts/core/execution.rb,
lib/facter/framework/core/fact_manager.rb,
lib/facter/resolvers/aio_agent_version.rb,
lib/facter/resolvers/aix/load_averages.rb,
lib/facter/resolvers/solaris/dmi_sparc.rb,
lib/facter/resolvers/solaris/ipaddress.rb,
lib/facter/resolvers/solaris/zone_name.rb,
lib/facter/util/facts/virtual_detector.rb,
lib/facter/custom_facts/util/resolution.rb,
lib/facter/framework/benchmarking/timer.rb,
lib/facter/framework/config/fact_groups.rb,
lib/facter/framework/core/cache_manager.rb,
lib/facter/framework/core/session_cache.rb,
lib/facter/resolvers/aix/ffi/ffi_helper.rb,
lib/facter/resolvers/bsd/ffi/ffi_helper.rb,
lib/facter/resolvers/freebsd/processors.rb,
lib/facter/resolvers/macosx/filesystems.rb,
lib/facter/resolvers/macosx/mountpoints.rb,
lib/facter/resolvers/macosx/swap_memory.rb,
lib/facter/resolvers/solaris/networking.rb,
lib/facter/resolvers/solaris/os_release.rb,
lib/facter/resolvers/solaris/processors.rb,
lib/facter/resolvers/windows/networking.rb,
lib/facter/resolvers/windows/processors.rb,
lib/facter/util/resolvers/uptime_helper.rb,
lib/facter/framework/core/fact_augmenter.rb,
lib/facter/resolvers/freebsd/swap_memory.rb,
lib/facter/resolvers/linux/docker_uptime.rb,
lib/facter/resolvers/linux/load_averages.rb,
lib/facter/resolvers/solaris/ffi/structs.rb,
lib/facter/resolvers/solaris/filesystems.rb,
lib/facter/resolvers/solaris/mountpoints.rb,
lib/facter/framework/config/config_reader.rb,
lib/facter/framework/parsers/query_parser.rb,
lib/facter/resolvers/macosx/load_averages.rb,
lib/facter/resolvers/macosx/system_memory.rb,
lib/facter/util/resolvers/networking/dhcp.rb,
lib/facter/framework/detector/os_hierarchy.rb,
lib/facter/resolvers/freebsd/system_memory.rb,
lib/facter/resolvers/solaris/ffi/functions.rb,
lib/facter/custom_facts/core/execution/base.rb,
lib/facter/resolvers/freebsd/ffi/ffi_helper.rb,
lib/facter/resolvers/macosx/system_profiler.rb,
lib/facter/resolvers/windows/virtualization.rb,
lib/facter/util/resolvers/ffi/load_averages.rb,
lib/facter/util/resolvers/filesystem_helper.rb,
lib/facter/custom_facts/core/execution/posix.rb,
lib/facter/resolvers/freebsd/freebsd_version.rb,
lib/facter/resolvers/windows/product_release.rb,
lib/facter/util/facts/windows_release_finder.rb,
lib/facter/custom_facts/core/execution/windows.rb,
lib/facter/framework/core/options/option_store.rb,
lib/facter/resolvers/windows/aio_agent_version.rb,
lib/facter/util/macosx/system_profile_executor.rb,
lib/facter/resolvers/windows/dmi_computersystem.rb,
lib/facter/resolvers/windows/win_os_description.rb,
lib/facter/util/resolvers/networking/networking.rb,
lib/facter/framework/formatters/formatter_helper.rb,
lib/facter/framework/core/fact/internal/core_fact.rb,
lib/facter/framework/formatters/formatter_factory.rb,
lib/facter/framework/core/fact_loaders/fact_loader.rb,
lib/facter/resolvers/windows/hardware_architecture.rb,
lib/facter/framework/core/options/options_validator.rb,
lib/facter/framework/formatters/json_fact_formatter.rb,
lib/facter/framework/formatters/yaml_fact_formatter.rb,
lib/facter/framework/formatters/hocon_fact_formatter.rb,
lib/facter/framework/core/options/config_file_options.rb,
lib/facter/framework/formatters/legacy_fact_formatter.rb,
lib/facter/util/resolvers/networking/primary_interface.rb,
lib/facter/framework/core/fact_loaders/class_discoverer.rb,
lib/facter/framework/core/fact_loaders/external_fact_loader.rb,
lib/facter/framework/core/fact_loaders/internal_fact_loader.rb,
lib/facter/framework/core/fact/external/external_fact_manager.rb,
lib/facter/framework/core/fact/internal/internal_fact_manager.rb

Overview

This represents a fact resolution. A resolution is a concrete implementation of a fact. A single fact can have many resolutions and the correct resolution will be chosen at runtime. Each time Facter.add is called, a new resolution is created and added to the set of resolutions for the fact named in the call. Each resolution has a weight, which defines its priority over other resolutions, and a set of confinements, which defines the conditions under which it will be chosen. All confinements must be satisfied for a fact to be considered suitable.

Defined Under Namespace

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

Constant Summary collapse

VERSION =
'4.0.49'
RED =
"\e[31m"
GREEN =
"\e[32m"
YELLOW =
"\e[33m"
CYAN =
"\e[36m"
RESET =
"\e[0m"
DEFAULT_LOG_LEVEL =
:warn

Class Method Summary collapse

Class Method Details

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

Alias method for Facter.fact()

Parameters:

  • name (string)

    fact name

Returns:



54
55
56
# File 'lib/facter.rb', line 54

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



69
70
71
72
73
# File 'lib/facter.rb', line 69

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)


80
81
82
83
84
85
86
87
88
89
90
# File 'lib/facter.rb', line 80

def clear
  @already_searched = {}
  @debug_once = []
  @warn_once = []
  LegacyFacter.clear
  Options[:custom_dir] = []
  LegacyFacter.collection.invalidate_custom_facts
  LegacyFacter.collection.reload_custom_facts
  SessionCache.invalidate_all_caches
  nil
end

.core_value(user_query) ⇒ FactCollection

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 the value for a core fact, external or custom facts are

not returned with this call. Returns `nil` if no such fact exists.

Returns:



98
99
100
101
102
103
104
# File 'lib/facter.rb', line 98

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(message) ⇒ nil

Logs debug message when debug option is set to true

Parameters:

  • message (Object)

    Message object to be logged

Returns:

  • (nil)


112
113
114
115
116
117
# File 'lib/facter.rb', line 112

def debug(message)
  return unless debugging?

  logger.debug(message.to_s)
  nil
end

.debugging(debug_bool) ⇒ type

Enable or disable debugging

Parameters:

  • debug_bool (bool)

    State which debugging should have

Returns:

  • (type)
    description


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

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

.debugging?bool

Check whether debugging is enabled

Returns:

  • (bool)


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

def debugging?
  Options[:debug]
end

.debugonce(message) ⇒ nil

Logs the same debug message only once when debug option is set to true

Parameters:

  • message (Object)

    Message object to be logged

Returns:

  • (nil)


125
126
127
128
129
130
131
132
133
134
# File 'lib/facter.rb', line 125

def debugonce(message)
  return unless debugging?

  message_string = message.to_s
  return if @debug_once.include? message_string

  @debug_once << message_string
  logger.debug(message_string)
  nil
end

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

Define a new fact or extend an existing fact.

Parameters:

  • name (Symbol)

    The name of the fact to define

  • options (Hash) (defaults to: {})

    A hash of options to set on the fact

Returns:



144
145
146
147
# File 'lib/facter.rb', line 144

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

.disable_sequentialObject



184
185
186
# File 'lib/facter.rb', line 184

def disable_sequential
  Facter::Options[:sequential] = false
end

.each {|name, value| ... } ⇒ Facter

Iterates over fact names and values

Yield Parameters:

  • name (String)

    the fact name

  • value (String)

    the current value of the fact

Returns:



200
201
202
203
204
205
206
207
208
209
# File 'lib/facter.rb', line 200

def each
  log_blocked_facts
  resolved_facts = Facter::FactManager.instance.resolve_facts

  resolved_facts.each do |fact|
    yield(fact.name, fact.value)
  end

  self
end

.enable_sequentialObject



180
181
182
# File 'lib/facter.rb', line 180

def enable_sequential
  Facter::Options[:sequential] = true
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:

  • user_query (String)

    the name of the fact

Returns:



221
222
223
224
225
226
# File 'lib/facter.rb', line 221

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

  @already_searched[user_query]
end

.flushvoid

This method returns an undefined value.

Flushes cached values for all facts. This does not cause code to be reloaded; it only clears the cached results.



248
249
250
251
252
# File 'lib/facter.rb', line 248

def flush
  LegacyFacter.flush
  SessionCache.invalidate_all_caches
  nil
end

.listArray

Returns a list with the names of all solved facts

Returns:

  • (Array)

    the list with all the fact names



440
441
442
# File 'lib/facter.rb', line 440

def list
  to_hash.keys.sort
end

.load_external(enable_external) ⇒ Object

Enables/Disables external facts.

Parameters:

  • enable_external (boolean)

    @return nil



270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/facter.rb', line 270

def load_external(enable_external)
  # enable_external param needs negation because behind the scene
  # no_external_facts= method is negating the parameter again.
  Options[:no_external_facts] = !enable_external

  if enable_external
    logger.debug('Facter.load_external(true) called. External facts will be loaded')
  else
    logger.debug('Facter.load_external(false) called. External facts will NOT be loaded')
  end

  nil
end

.loadfactsnil

Loads all facts

Returns:

  • (nil)


259
260
261
262
# File 'lib/facter.rb', line 259

def loadfacts
  LegacyFacter.loadfacts
  nil
end

.log_exception(exception, message = nil) ⇒ nil

Logs an exception and an optional message

Returns:

  • (nil)


426
427
428
429
430
431
432
433
434
# File 'lib/facter.rb', line 426

def log_exception(exception, message = nil)
  error_message = []

  error_message << message.to_s unless message.nil? || (message.is_a?(String) && message.empty?)

  parse_exception(exception, error_message)
  logger.error(error_message.flatten.join("\n"))
  nil
end

.on_message(&block) ⇒ nil

Stores a proc that will be used to output custom messages.

The proc must receive one parameter that will be the message to log.

Parameters:

  • block (Proc)

    a block defining messages handler

Returns:

  • (nil)


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

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

.resetnil

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

Returns:

  • (nil)


234
235
236
237
238
239
240
# File 'lib/facter.rb', line 234

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

.resolve(args_as_string) ⇒ Object

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.

Method used by puppet-agent to retrieve facts

Parameters:

  • args_as_string (string)

    facter cli arguments

Returns:

  • query result



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/facter.rb', line 26

def resolve(args_as_string)
  require 'facter/framework/cli/cli_launcher'

  args = args_as_string.split(' ')

  Facter::OptionsValidator.validate(args)
  processed_arguments = CliLauncher.prepare_arguments(args, nil)

  cli = Facter::Cli.new([], processed_arguments)

  if cli.args.include?(:version)
    cli.invoke(:version, [])
  elsif cli.args.include?('--list-cache-groups')
    cli.invoke(:list_cache_groups, [])
  elsif cli.args.include?('--list-block-groups')
    cli.invoke(:list_block_groups, [])
  else
    cli.invoke(:arg_parser)
  end
end

.search(*dirs) ⇒ nil

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

Returns:

  • (nil)


291
292
293
294
# File 'lib/facter.rb', line 291

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

.search_external(dirs) ⇒ nil

Registers directories to be searched for external facts.

Parameters:

  • dirs (Array<String>)

    An array of searched directories

Returns:

  • (nil)


302
303
304
305
# File 'lib/facter.rb', line 302

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

.search_external_pathArray<String>

Returns the registered search directories.for external facts.

Returns:

  • (Array<String>)

    An array of searched directories



312
313
314
# File 'lib/facter.rb', line 312

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



321
322
323
# File 'lib/facter.rb', line 321

def search_path
  Options.custom_dir
end

.sequential?Boolean

Returns:

  • (Boolean)


188
189
190
# File 'lib/facter.rb', line 188

def sequential?
  Facter::Options[:sequential]
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:



331
332
333
334
335
336
337
338
339
# File 'lib/facter.rb', line 331

def to_hash
  log_blocked_facts
  logger.debug("Facter version: #{Facter::VERSION}")

  resolved_facts = Facter::FactManager.instance.resolve_facts
  resolved_facts.reject! { |fact| fact.type == :custom && fact.value.nil? }
  collection = Facter::FactCollection.new.build_fact_collection!(resolved_facts)
  Hash[collection]
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



409
410
411
412
413
414
415
416
417
418
419
# File 'lib/facter.rb', line 409

def to_user_output(cli_options, *args)
  init_cli_options(cli_options, args)
  logger.info("executed with command line: #{ARGV.drop(1).join(' ')}")
  logger.debug("Facter version: #{Facter::VERSION}")
  log_blocked_facts
  resolved_facts = resolve_facts_for_user_query(args)
  fact_formatter = Facter::FormatterFactory.build(Facter::Options.get)
  status = error_check(resolved_facts)

  [fact_formatter.format(resolved_facts), status]
end

.trace(bool) ⇒ bool

Enable or disable trace

Parameters:

  • bool (bool)

    Set trace on debug state

Returns:

  • (bool)

    Value of trace debug state



356
357
358
# File 'lib/facter.rb', line 356

def trace(bool)
  Options[:trace] = bool
end

.trace?bool

Check whether printing stack trace is enabled

Returns:

  • (bool)


346
347
348
# File 'lib/facter.rb', line 346

def trace?
  Options[:trace]
end

.value(user_query) ⇒ String

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

Parameters:

  • user_query (String)

    the fact name

Returns:

  • (String)

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



366
367
368
369
370
# File 'lib/facter.rb', line 366

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

.values(options, user_queries) ⇒ FactCollection

Gets the values for multiple facts.

Parameters:

Returns:



382
383
384
385
386
387
388
389
390
391
392
393
# File 'lib/facter.rb', line 382

def values(options, user_queries)
  init_cli_options(options, user_queries)
  log_blocked_facts
  resolved_facts = Facter::FactManager.instance.resolve_facts(user_queries)
  resolved_facts.reject! { |fact| fact.type == :custom && fact.value.nil? }

  if user_queries.count.zero?
    Facter::FactCollection.new.build_fact_collection!(resolved_facts)
  else
    FormatterHelper.retrieve_facts_to_display_for_user_query(user_queries, resolved_facts)
  end
end

.versionString

Returns Facter version

Returns:

  • (String)

    Current version



400
401
402
# File 'lib/facter.rb', line 400

def version
  Facter::VERSION
end

.warn(message) ⇒ nil

Logs the message parameter as a warning.

Parameters:

  • message (Object)

    the warning object to be displayed

Returns:

  • (nil)


450
451
452
453
# File 'lib/facter.rb', line 450

def warn(message)
  logger.warn(message.to_s)
  nil
end

.warnonce(message) ⇒ nil

Logs only once the same warning message.

Parameters:

  • message (Object)

    the warning message object

Returns:

  • (nil)


461
462
463
464
465
466
467
468
# File 'lib/facter.rb', line 461

def warnonce(message)
  message_string = message.to_s
  return if @warn_once.include? message_string

  @warn_once << message_string
  logger.warn(message_string)
  nil
end