summaryrefslogtreecommitdiffstats
path: root/lib/puppet/node/configuration.rb
diff options
context:
space:
mode:
authorLuke Kanies <luke@madstop.com>2007-09-15 22:17:20 -0600
committerLuke Kanies <luke@madstop.com>2007-09-15 22:17:20 -0600
commitf17f19dae941b17a56c1fc83ed3a89712b98c427 (patch)
tree3615e9be9ed585511bbe0b85737208bbd85f00f0 /lib/puppet/node/configuration.rb
parent3ccf483f77b026dde8a53bd8e9dff6a5fd0f6722 (diff)
downloadpuppet-f17f19dae941b17a56c1fc83ed3a89712b98c427.tar.gz
puppet-f17f19dae941b17a56c1fc83ed3a89712b98c427.tar.xz
puppet-f17f19dae941b17a56c1fc83ed3a89712b98c427.zip
The whole system now uses Configuration objects instead of
ever converting the Transportable objects into a tree of components and then converting that into a graph. This is a significant step, and drastically simplifies the model of how to use a configuration. The old code might have looked something like this: file = Puppet::Type.create :path => "/whatever", ... comp = Puppet::Type.create :name => :whatever comp.push file transaction = comp.evaluate transaction.evaluate The new code looks like this: file = Puppet::Type.create :path => "/whatever", ... config = Puppet::Node::Configuration.new config.add_resource file config.apply I did not really intend to do this much refactoring, but I found I could not use a Configuration object to do work without refactoring a lot of the system. The primary problem was that the Client::Master and the Config classes determined how the transactions behaved; when I moved to using a Configuration, this distinction was lost, which meant that configurations were often needing to create other configurations, which resulted in a whole lot of infinite recursion (e.g., Config objects that create directories for Puppet use Configuration objects -- yes, I'm s/Config/Settings/g soon -- and these Configuration objects would need to create directories). Not everything is fixed, but it's very close. I am clearly over the hump, though, so I wanted to get a commit in.
Diffstat (limited to 'lib/puppet/node/configuration.rb')
-rw-r--r--lib/puppet/node/configuration.rb81
1 files changed, 76 insertions, 5 deletions
diff --git a/lib/puppet/node/configuration.rb b/lib/puppet/node/configuration.rb
index be30ddb68..8cb6ed1f9 100644
--- a/lib/puppet/node/configuration.rb
+++ b/lib/puppet/node/configuration.rb
@@ -5,9 +5,26 @@ require 'puppet/external/gratr/digraph'
# of the information in the configuration, including the resources
# and the relationships between them.
class Puppet::Node::Configuration < Puppet::PGraph
- attr_accessor :name, :version
+ # The host name this is a configuration for.
+ attr_accessor :name
+
+ # The configuration version. Used for testing whether a configuration
+ # is up to date.
+ attr_accessor :version
+
+ # How long this configuration took to retrieve. Used for reporting stats.
+ attr_accessor :retrieval_duration
+
+ # How we should extract the configuration for sending to the client.
attr_reader :extraction_format
+ # Whether this is a host configuration, which behaves very differently.
+ # In particular, reports are sent, graphs are made, and state is
+ # stored in the state database. If this is set incorrectly, then you often
+ # end up in infinite loops, because configurations are used to make things
+ # that the host configuration needs.
+ attr_accessor :host_config
+
# Add classes to our class list.
def add_class(*classes)
classes.each do |klass|
@@ -30,10 +47,50 @@ class Puppet::Node::Configuration < Puppet::PGraph
else
@resource_table[ref] = resource
end
+ add_vertex!(resource)
+ end
+
+ # Apply our configuration to the local host.
+ def apply
+ Puppet::Util::Storage.load if host_config?
+ transaction = Puppet::Transaction.new(self)
+
+ transaction.addtimes :config_retrieval => @retrieval_duration
+
+ begin
+ transaction.evaluate
+ rescue Puppet::Error => detail
+ Puppet.err "Could not apply complete configuration: %s" % detail
+ rescue => detail
+ if Puppet[:trace]
+ puts detail.backtrace
+ end
+ Puppet.err "Got an uncaught exception of type %s: %s" % [detail.class, detail]
+ ensure
+ # Don't try to store state unless we're a host config
+ # too recursive.
+ Puppet::Util::Storage.store if host_config?
+ end
+
+ if block_given?
+ yield transaction
+ end
+
+ if host_config and (Puppet[:report] or Puppet[:summarize])
+ transaction.send_report
+ end
+
+ return transaction
+ ensure
+ if defined? transaction and transaction
+ transaction.cleanup
+ end
end
- def clear
- super
+ def clear(remove_resources = true)
+ super()
+ # We have to do this so that the resources clean themselves up.
+ @resource_table.values.each { |resource| resource.remove } if remove_resources
@resource_table.clear
end
@@ -113,13 +170,23 @@ class Puppet::Node::Configuration < Puppet::PGraph
return result
end
- def initialize(name)
+ # Make sure all of our resources are "finished".
+ def finalize
+ @resource_table.values.each { |resource| resource.finish }
+ end
+
+ def initialize(name = nil)
super()
- @name = name
+ @name = name if name
@extraction_format ||= :transportable
@tags = []
@classes = []
@resource_table = {}
+
+ if block_given?
+ yield(self)
+ finalize()
+ end
end
# Look a resource up by its reference (e.g., File[/etc/passwd]).
@@ -127,6 +194,10 @@ class Puppet::Node::Configuration < Puppet::PGraph
@resource_table[ref]
end
+ def host_config?
+ host_config || false
+ end
+
# Add a tag.
def tag(*names)
names.each do |name|