# class Node::Environment

Model the environment that a node can operate in. This class just provides a simple wrapper for the functionality around environments.

### Attributes

name[R]

### Public Class Methods

clear() click to toggle source

    # File lib/puppet/node/environment.rb, line 58
    def self.clear
      @seen.clear
      Thread.current[:environment] = nil
    end

current() click to toggle source

    # File lib/puppet/node/environment.rb, line 46
    def self.current
      Thread.current[:environment] || root
    end

current=(env) click to toggle source

    # File lib/puppet/node/environment.rb, line 50
    def self.current=(env)
      Thread.current[:environment] = new(env)
    end

new(name = nil) click to toggle source

Return an existing environment instance, or create a new one.

    # File lib/puppet/node/environment.rb, line 31
    def self.new(name = nil)
      return name if name.is_a?(self)
      name ||= Puppet.settings.value(:environment)

      raise ArgumentError, "Environment name must be specified" unless name

      symbol = name.to_sym

      return @seen[symbol] if @seen[symbol]

      obj = self.allocate
      obj.send :initialize, symbol
      @seen[symbol] = obj
    end

new(name) click to toggle source

    # File lib/puppet/node/environment.rb, line 70
    def initialize(name)
      @name = name
      extend MonitorMixin
    end

root() click to toggle source

    # File lib/puppet/node/environment.rb, line 54
    def self.root
      @root
    end

### Public Instance Methods

[](param) click to toggle source

Return an environment-specific setting.

    # File lib/puppet/node/environment.rb, line 66
    def [](param)
      Puppet.settings.value(param, self.name)
    end

each_plugin_directory() { |lib| ... } click to toggle source

Yields each modules’ plugin directory.

@yield [String] Yields the plugin directory from each module to the block. @api public

    # File lib/puppet/node/environment.rb, line 95
    def each_plugin_directory(&block)
      modules.map(&:plugin_directory).each do |lib|
        lib = Puppet::Util::Autoload.cleanpath(lib)
        yield lib if File.directory?(lib)
      end
    end

known_resource_types() click to toggle source

    # File lib/puppet/node/environment.rb, line 75
    def known_resource_types
      # This makes use of short circuit evaluation to get the right thread-safe
      # per environment semantics with an efficient most common cases; we almost
      # always just return our thread's known-resource types.  Only at the start
      # of a compilation (after our thread var has been set to nil) or when the
      # environment has changed do we delve deeper.
      Thread.current[:known_resource_types] = nil if (krt = Thread.current[:known_resource_types]) && krt.environment != self
      Thread.current[:known_resource_types] ||= synchronize {
        if @known_resource_types.nil? or @known_resource_types.require_reparse?
          @known_resource_types = Puppet::Resource::TypeCollection.new(self)
          @known_resource_types.import_ast(perform_initial_import, '')
        end
        @known_resource_types
      }
    end

module(name) click to toggle source

    # File lib/puppet/node/environment.rb, line 102
    def module(name)
      modules.find {|mod| mod.name == name}
    end

module_by_forge_name(forge_name) click to toggle source

    # File lib/puppet/node/environment.rb, line 106
    def module_by_forge_name(forge_name)
      author, modname = forge_name.split('/')
      found_mod = self.module(modname)
      found_mod and found_mod.forge_name == forge_name ?
        found_mod :
        nil
    end

module_requirements() click to toggle source

    # File lib/puppet/node/environment.rb, line 172
    def module_requirements
      deps = {}
      modules.each do |mod|
        next unless mod.forge_name
        deps[mod.forge_name] ||= []
        mod.dependencies and mod.dependencies.each do |mod_dep|
          deps[mod_dep['name']] ||= []
          dep_details = {
            'name'                => mod.forge_name,
            'version'             => mod.version,
            'version_requirement' => mod_dep['version_requirement']
          }
          deps[mod_dep['name']] << dep_details
        end
      end
      deps.each do |mod, mod_deps|
        deps[mod] = mod_deps.sort_by {|d| d['name']}
      end
      deps
    end

modules_by_path() click to toggle source

Modules broken out by directory in the modulepath

    # File lib/puppet/node/environment.rb, line 157
    def modules_by_path
      modules_by_path = {}
      modulepath.each do |path|
        Dir.chdir(path) do
          module_names = Dir.glob('*').select do |d|
            FileTest.directory?(d) && (File.basename(d) =~ /\A\w+(-\w+)*\Z/)
          end
          modules_by_path[path] = module_names.sort.map do |name|
            Puppet::Module.new(name, File.join(path, name), self)
          end
        end
      end
      modules_by_path
    end

to_s() click to toggle source

    # File lib/puppet/node/environment.rb, line 193
    def to_s
      name.to_s
    end

to_sym() click to toggle source

    # File lib/puppet/node/environment.rb, line 197
    def to_sym
      to_s.to_sym
    end

to_zaml(z) click to toggle source

The only thing we care about when serializing an environment is its identity; everything else is ephemeral and should not be stored or transmitted.

    # File lib/puppet/node/environment.rb, line 204
    def to_zaml(z)
      self.to_s.to_zaml(z)
    end

validate_dirs(dirs) click to toggle source

    # File lib/puppet/node/environment.rb, line 208
    def validate_dirs(dirs)
      dirs.collect do |dir|
        File.expand_path(dir)
      end.find_all do |p|
        FileTest.directory?(p)
      end
    end

warn_about_mistaken_path(path, name) click to toggle source

    # File lib/puppet/node/environment.rb, line 148
    def warn_about_mistaken_path(path, name)
      if name == "lib"
        Puppet.debug("Warning: Found directory named 'lib' in module path ('#{path}/lib'); unless " +
            "you are expecting to load a module named 'lib', your module path may be set " +
            "incorrectly.")
      end
    end