Module: Puppet::Util::InstanceLoader

Includes:
Puppet::Util
Included in:
Indirector::Terminus, Reports, Reference
Defined in:
lib/puppet/util/instance_loader.rb

Overview

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

Constant Summary

Constants included from Puppet::Util

ALNUM, ALPHA, AbsolutePathPosix, AbsolutePathWindows, DEFAULT_POSIX_MODE, DEFAULT_WINDOWS_MODE, ESCAPED, HEX, HttpProxy, PUPPET_STACK_INSERTION_FRAME, RESERVED, RFC_3986_URI_REGEX, UNRESERVED, UNSAFE

Constants included from POSIX

POSIX::LOCALE_ENV_VARS, POSIX::USER_ENV_VARS

Constants included from SymbolicFileMode

SymbolicFileMode::SetGIDBit, SymbolicFileMode::SetUIDBit, SymbolicFileMode::StickyBit, SymbolicFileMode::SymbolicMode, SymbolicFileMode::SymbolicSpecialToBit

Instance Method Summary collapse

Methods included from Puppet::Util

absolute_path?, benchmark, chuser, clear_environment, create_erb, default_env, deterministic_rand, deterministic_rand_int, exit_on_fail, format_backtrace_array, format_puppetstack_frame, get_env, get_environment, logmethods, merge_environment, path_to_uri, pretty_backtrace, replace_file, resolve_stackframe, rfc2396_escape, safe_posix_fork, set_env, skip_external_facts, symbolizehash, thinmark, uri_encode, uri_query_encode, uri_to_path, uri_unescape, which, withenv, withumask

Methods included from POSIX

#get_posix_field, #gid, groups_of, #idfield, #methodbyid, #methodbyname, #search_posix_field, #uid

Methods included from SymbolicFileMode

#display_mode, #normalize_symbolic_mode, #symbolic_mode_to_int, #valid_symbolic_mode?

Instance Method Details

#instance_hash(type) ⇒ Object

Return the instance hash for our type.



40
41
42
# File 'lib/puppet/util/instance_loader.rb', line 40

def instance_hash(type)
  @instances[type.intern]
end

#instance_load(type, path) ⇒ Object

Define a new type of autoloading.



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

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

#instance_loader(type) ⇒ Object

Return the Autoload object for a given type.



45
46
47
# File 'lib/puppet/util/instance_loader.rb', line 45

def instance_loader(type)
  @autoloaders[type.intern]
end

#instance_loading?(type) ⇒ Boolean

Are we instance-loading this type?

Returns:

  • (Boolean)


13
14
15
# File 'lib/puppet/util/instance_loader.rb', line 13

def instance_loading?(type)
  defined?(@autoloaders) and @autoloaders.include?(type.intern)
end

#loaded_instance(type, name) ⇒ Object

Retrieve an already-loaded instance, or attempt to load our instance.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/puppet/util/instance_loader.rb', line 50

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

#loaded_instances(type) ⇒ Object

Return a list of the names of all instances



35
36
37
# File 'lib/puppet/util/instance_loader.rb', line 35

def loaded_instances(type)
  @instances[type].keys
end