Class: Facter::Util::Loader

Inherits:
Object
  • Object
show all
Defined in:
lib/facter/util/loader.rb

Overview

Load facts on demand.

Instance Method Summary collapse

Constructor Details

#initialize(environment_vars = ENV) ⇒ Loader

Returns a new instance of Loader



8
9
10
11
# File 'lib/facter/util/loader.rb', line 8

def initialize(environment_vars = ENV)
  @loaded = []
  @environment_vars = environment_vars
end

Instance Method Details

#kernel_load(file) ⇒ Boolean (private)

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.

Load and execute the Ruby program specified in the file. This exists for testing purposes.

Returns:

  • (Boolean)


129
130
131
# File 'lib/facter/util/loader.rb', line 129

def kernel_load(file)
  Kernel.load(file)
end

#load(fact) ⇒ Object

Load all resolutions for a single fact.

Parameters:



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/facter/util/loader.rb', line 17

def load(fact)
  # Now load from the search path
  shortname = fact.to_s.downcase
  load_env(shortname)

  filename = shortname + ".rb"

  paths = search_path
  unless paths.nil?
    paths.each do |dir|
      # Load individual files
      file = File.join(dir, filename)

      load_file(file) if File.file?(file)
    end
  end
end

#load_allObject

Load all facts from all directories.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/facter/util/loader.rb', line 38

def load_all
  return if defined?(@loaded_all)

  load_env

  paths = search_path
  unless paths.nil?
    paths.each do |dir|
      # dir is already an absolute path
      Dir.glob(File.join(dir, '*.rb')).each do |path|
        # exclude dirs that end with .rb
        load_file(path) if File.file?(path)
      end
    end
  end

  @loaded_all = true
end

#load_env(fact = nil) ⇒ Object (private)

Load facts from the environment. If no name is provided, all will be loaded.



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/facter/util/loader.rb', line 135

def load_env(fact = nil)
  # Load from the environment, if possible
  @environment_vars.each do |name, value|
    # Skip anything that doesn't match our regex.
    next unless name =~ /^facter_?(\w+)$/i
    env_name = $1

    # If a fact name was specified, skip anything that doesn't
    # match it.
    next if fact and env_name != fact

    Facter.add($1) do
      has_weight 1_000_000
      setcode { value }
    end

    # Short-cut, if we are only looking for one value.
    break if fact
  end
end

#load_file(file) ⇒ Object (private)

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.

Load a file and record is paths to prevent duplicate loads.



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/facter/util/loader.rb', line 108

def load_file(file)
  return if @loaded.include? file

  # We have to specify Kernel.load, because we have a load method.
  begin
    # Store the file path so we don't try to reload it
    @loaded << file
    kernel_load(file)
  rescue ScriptError => detail
    # Don't store the path if the file can't be loaded
    # in case it's loadable later on.
    @loaded.delete(file)
    Facter.log_exception(detail, "Error loading fact #{file}: #{detail.message}")
  end
end

#search_pathArray<String>

List directories to search for fact files.

Search paths are gathered from the following sources:

  1. $LOAD_PATH entries are expanded to absolute paths
  2. ENV['FACTERLIB'] is split and used verbatim
  3. Entries from Facter.search_path are used verbatim

A warning will be generated for paths in Facter.search_path that are not absolute directories.

Returns:



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/facter/util/loader.rb', line 70

def search_path
  search_paths = []
  search_paths += $LOAD_PATH.map { |path| File.expand_path('facter', path) }

  if @environment_vars.include?("FACTERLIB")
    search_paths += @environment_vars["FACTERLIB"].split(File::PATH_SEPARATOR)
  end

  search_paths.delete_if { |path| ! valid_search_path?(path) }

  Facter.search_path.each do |path|
    if valid_search_path?(path)
      search_paths << path
    else
      Facter.warn "Excluding #{path} from search path. Fact file paths must be an absolute directory"
    end
  end

  search_paths.delete_if { |path| ! File.directory?(path) }

  search_paths.uniq
end

#valid_search_path?(path) ⇒ Boolean (private)

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.

Validate that the given path is valid, ie it is an absolute path.

Parameters:

  • path (String)

Returns:

  • (Boolean)


100
101
102
# File 'lib/facter/util/loader.rb', line 100

def valid_search_path?(path)
  Pathname.new(path).absolute?
end