Class: Puppet::Node::Environment

Inherits:
Object
  • Object
show all
Includes:
Util::Cacher
Defined in:
lib/vendor/puppet/node/environment.rb

Overview

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

Defined Under Namespace

Modules: Helper

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util::Cacher

extended, included

Constructor Details

#initialize(name) ⇒ Environment

Returns a new instance of Environment.



70
71
72
73
# File 'lib/vendor/puppet/node/environment.rb', line 70

def initialize(name)
  @name = name
  extend MonitorMixin
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



63
64
65
# File 'lib/vendor/puppet/node/environment.rb', line 63

def name
  @name
end

Class Method Details

.clearObject



58
59
60
61
# File 'lib/vendor/puppet/node/environment.rb', line 58

def self.clear
  @seen.clear
  Thread.current[:environment] = nil
end

.currentObject



46
47
48
# File 'lib/vendor/puppet/node/environment.rb', line 46

def self.current
  Thread.current[:environment] || root
end

.current=(env) ⇒ Object



50
51
52
# File 'lib/vendor/puppet/node/environment.rb', line 50

def self.current=(env)
  Thread.current[:environment] = new(env)
end

.new(name = nil) ⇒ Object

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

Raises:

  • (ArgumentError)


31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/vendor/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

.rootObject



54
55
56
# File 'lib/vendor/puppet/node/environment.rb', line 54

def self.root
  @root
end

Instance Method Details

#[](param) ⇒ Object

Return an environment-specific setting.



66
67
68
# File 'lib/vendor/puppet/node/environment.rb', line 66

def [](param)
  Puppet.settings.value(param, self.name)
end

#known_resource_typesObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/vendor/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) ⇒ Object



91
92
93
# File 'lib/vendor/puppet/node/environment.rb', line 91

def module(name)
  modules.find {|mod| mod.name == name}
end

#module_by_forge_name(forge_name) ⇒ Object



95
96
97
98
99
100
101
# File 'lib/vendor/puppet/node/environment.rb', line 95

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_requirementsObject



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/vendor/puppet/node/environment.rb', line 140

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_pathObject

Modules broken out by directory in the modulepath



125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/vendor/puppet/node/environment.rb', line 125

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) =~ /^[\w]+([-]{1}[\w]+)*$/)
      end
      modules_by_path[path] = module_names.sort.map do |name|
        Puppet::Module.new(name, :environment => self, :path => File.join(path, name))
      end
    end
  end
  modules_by_path
end

#to_sObject



161
162
163
# File 'lib/vendor/puppet/node/environment.rb', line 161

def to_s
  name.to_s
end

#to_symObject



165
166
167
# File 'lib/vendor/puppet/node/environment.rb', line 165

def to_sym
  to_s.to_sym
end

#to_zaml(z) ⇒ Object

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



172
173
174
# File 'lib/vendor/puppet/node/environment.rb', line 172

def to_zaml(z)
  self.to_s.to_zaml(z)
end

#validate_dirs(dirs) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
# File 'lib/vendor/puppet/node/environment.rb', line 176

def validate_dirs(dirs)
  dirs.collect do |dir|
    unless Puppet::Util.absolute_path?(dir)
      File.expand_path(File.join(Dir.getwd, dir))
    else
      dir
    end
  end.find_all do |p|
    Puppet::Util.absolute_path?(p) && FileTest.directory?(p)
  end
end