File: instance_loader.rb

package info (click to toggle)
puppet-agent 7.23.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 19,092 kB
  • sloc: ruby: 245,074; sh: 456; makefile: 38; xml: 33
file content (66 lines) | stat: -rw-r--r-- 1,867 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
require_relative '../../puppet/util/autoload'
require_relative '../../puppet/util'
require_relative '../../puppet/concurrent/lock'

# A module that can easily autoload things for us.  Uses an instance
# of Puppet::Util::Autoload
module Puppet::Util::InstanceLoader
  include Puppet::Util

  # Are we instance-loading this type?
  def instance_loading?(type)
    defined?(@autoloaders) and @autoloaders.include?(type.intern)
  end

  # Define a new type of autoloading.
  def instance_load(type, path)
    @autoloaders ||= {}
    @instances ||= {}
    type = type.intern
    @instances[type] = {}
    @autoloaders[type] = Puppet::Util::Autoload.new(self, path)
    @instance_loader_lock = Puppet::Concurrent::Lock.new

    # Now define our new simple methods
    unless respond_to?(type)
      meta_def(type) do |name|
        loaded_instance(type, name)
      end
    end
  end

  # Return a list of the names of all instances
  def loaded_instances(type)
    @instances[type].keys
  end

  # Return the instance hash for our type.
  def instance_hash(type)
    @instances[type.intern]
  end

  # Return the Autoload object for a given type.
  def instance_loader(type)
    @autoloaders[type.intern]
  end

  # Retrieve an already-loaded instance, or attempt to load our instance.
  def loaded_instance(type, name)
    @instance_loader_lock.synchronize do
      name = name.intern
      instances = instance_hash(type)
      return nil unless instances
      unless instances.include? name
        if instance_loader(type).load(name, Puppet.lookup(:current_environment))
          unless instances.include? name
            Puppet.warning(_("Loaded %{type} file for %{name} but %{type} was not defined") % { type: type, name: name })
            return nil
          end
        else
          return nil
        end
      end
      instances[name]
    end
  end
end