summaryrefslogtreecommitdiffstats
path: root/lib/puppet/node
diff options
context:
space:
mode:
authorDominic Cleal <dcleal@redhat.com>2010-11-27 13:36:04 +0000
committerDominic Cleal <dcleal@redhat.com>2010-11-27 13:36:04 +0000
commitafe2d014feb2210a8666c93465d11e9c9d555f8b (patch)
tree208f5ac82b2c29610d2021821c8fca9b079e638b /lib/puppet/node
parent143fc744a839affd328234fca26246d49d15d3d8 (diff)
parent4b35402ba85d8842d757becec5c8a7bf4d6f6654 (diff)
Merge branch 'master' of github.com:domcleal/puppet into master-old
Diffstat (limited to 'lib/puppet/node')
-rw-r--r--lib/puppet/node/environment.rb50
-rwxr-xr-xlib/puppet/node/facts.rb37
-rw-r--r--lib/puppet/node/inventory.rb7
3 files changed, 85 insertions, 9 deletions
diff --git a/lib/puppet/node/environment.rb b/lib/puppet/node/environment.rb
index 762599cff..7877b7f73 100644
--- a/lib/puppet/node/environment.rb
+++ b/lib/puppet/node/environment.rb
@@ -1,4 +1,5 @@
require 'puppet/util/cacher'
+require 'monitor'
# Just define it, so this class has fewer load dependencies.
class Puppet::Node
@@ -67,14 +68,23 @@ class Puppet::Node::Environment
def initialize(name)
@name = name
+ extend MonitorMixin
end
def known_resource_types
- if @known_resource_types.nil? or @known_resource_types.stale?
- @known_resource_types = Puppet::Resource::TypeCollection.new(self)
- @known_resource_types.perform_initial_import
- end
- @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.stale?
+ @known_resource_types = Puppet::Resource::TypeCollection.new(self)
+ @known_resource_types.import_ast(perform_initial_import, '')
+ end
+ @known_resource_types
+ }
end
def module(name)
@@ -114,6 +124,10 @@ class Puppet::Node::Environment
name.to_s
end
+ def to_sym
+ to_s.to_sym
+ end
+
# The only thing we care about when serializing an environment is its
# identity; everything else is ephemeral and should not be stored or
# transmitted.
@@ -133,5 +147,31 @@ class Puppet::Node::Environment
end
end
+ private
+
+ def perform_initial_import
+ return empty_parse_result if Puppet.settings[:ignoreimport]
+ parser = Puppet::Parser::Parser.new(self)
+ if code = Puppet.settings.uninterpolated_value(:code, name.to_s) and code != ""
+ parser.string = code
+ else
+ file = Puppet.settings.value(:manifest, name.to_s)
+ return empty_parse_result unless File.exist?(file)
+ parser.file = file
+ end
+ parser.parse
+ rescue => detail
+ msg = "Could not parse for environment #{self}: #{detail}"
+ error = Puppet::Error.new(msg)
+ error.set_backtrace(detail.backtrace)
+ raise error
+ end
+
+ def empty_parse_result
+ # Return an empty toplevel hostclass to use as the result of
+ # perform_initial_import when no file was actually loaded.
+ return Puppet::Parser::AST::Hostclass.new('')
+ end
+
@root = new(:'*root*')
end
diff --git a/lib/puppet/node/facts.rb b/lib/puppet/node/facts.rb
index b77ad22d5..d84d54113 100755
--- a/lib/puppet/node/facts.rb
+++ b/lib/puppet/node/facts.rb
@@ -1,12 +1,17 @@
+require 'time'
+
require 'puppet/node'
require 'puppet/indirector'
+require 'puppet/util/pson'
+
# Manage a given node's facts. This either accepts facts and stores them, or
# returns facts for a given node.
class Puppet::Node::Facts
# Set up indirection, so that nodes can be looked for in
# the node sources.
extend Puppet::Indirector
+ extend Puppet::Util::Pson
# We want to expire any cached nodes if the facts are saved.
module NodeExpirer
@@ -30,7 +35,7 @@ class Puppet::Node::Facts
@name = name
@values = values
- add_internal
+ add_timestamp
end
def downcase_if_necessary
@@ -54,13 +59,37 @@ class Puppet::Node::Facts
strip_internal == other.send(:strip_internal)
end
- private
+ def self.from_pson(data)
+ result = new(data['name'], data['values'])
+ result.values[:_timestamp] = Time.parse(data['timestamp'])
+ result.expiration = Time.parse(data['expiration'])
+ result
+ end
+
+ def to_pson(*args)
+ {
+ 'expiration' => expiration,
+ 'name' => name,
+ 'timestamp' => values[:_timestamp],
+ 'values' => values.reject {|k,v| k == :_timestamp},
+ }.to_pson(*args)
+ end
# Add internal data to the facts for storage.
- def add_internal
- self.values[:_timestamp] = Time.now
+ def add_timestamp
+ self.timestamp = Time.now
end
+ def timestamp=(time)
+ self.values[:_timestamp] = time
+ end
+
+ def timestamp
+ self.values[:_timestamp]
+ end
+
+ private
+
# Strip out that internal data.
def strip_internal
newvals = values.dup
diff --git a/lib/puppet/node/inventory.rb b/lib/puppet/node/inventory.rb
new file mode 100644
index 000000000..fd99163b0
--- /dev/null
+++ b/lib/puppet/node/inventory.rb
@@ -0,0 +1,7 @@
+require 'puppet/node'
+require 'puppet/indirector'
+
+class Puppet::Node::Inventory
+ extend Puppet::Indirector
+ indirects :inventory, :terminus_setting => :inventory_terminus
+end