diff options
author | luke <luke@980ebf18-57e1-0310-9a29-db15c13687c0> | 2006-07-03 16:22:42 +0000 |
---|---|---|
committer | luke <luke@980ebf18-57e1-0310-9a29-db15c13687c0> | 2006-07-03 16:22:42 +0000 |
commit | 25cf31b00057d552fcffde568049e37655c31fbc (patch) | |
tree | 15240e76c8e06a38141362a63b81793260884335 | |
parent | c899e2327b35f09f8061607bbb3b7c732500c5ba (diff) | |
download | puppet-25cf31b00057d552fcffde568049e37655c31fbc.tar.gz |
Adding minimal update checking for templates. It will only check the templates that have been parsed in this process, but it is better than nothing.
git-svn-id: https://reductivelabs.com/svn/puppet/trunk@1353 980ebf18-57e1-0310-9a29-db15c13687c0
-rw-r--r-- | lib/puppet/parser/interpreter.rb | 155 | ||||
-rw-r--r-- | lib/puppet/parser/scope.rb | 5 | ||||
-rwxr-xr-x | test/language/functions.rb | 46 |
3 files changed, 143 insertions, 63 deletions
diff --git a/lib/puppet/parser/interpreter.rb b/lib/puppet/parser/interpreter.rb index 9a0e47a52..3ccbc5c41 100644 --- a/lib/puppet/parser/interpreter.rb +++ b/lib/puppet/parser/interpreter.rb @@ -125,29 +125,12 @@ module Puppet Puppet::Rails.init end + @files = [] + # Create our parser object parsefiles end - # Connect to the LDAP Server - def setup_ldap - self.class.ldap = nil - begin - require 'ldap' - rescue LoadError - Puppet.notice( - "Could not set up LDAP Connection: Missing ruby/ldap libraries" - ) - @ldap = nil - return - end - begin - @ldap = self.class.ldap() - rescue => detail - raise Puppet::Error, "Could not connect to LDAP: %s" % detail - end - end - # Search for our node in the various locations. This only searches # locations external to the files; the scope is responsible for # searching the parse tree. @@ -259,6 +242,13 @@ module Puppet @parsedate end + # Add a new file to check for updateness. + def newfile(file) + unless @files.find { |f| f.file == file } + @files << Puppet::ParsedFile.new(file) + end + end + # evaluate our whole tree def run(client, facts) # We have to leave this for after initialization because there @@ -329,62 +319,59 @@ module Puppet end if Puppet[:storeconfigs] - unless defined? ActiveRecord - require 'puppet/rails' - unless defined? ActiveRecord - raise LoadError, - "storeconfigs is enabled but rails is unavailable" - end - end - - Puppet::Rails.init - - # Fork the storage, since we don't need the client waiting - # on that. How do I avoid this duplication? - if @forksave - fork { - # We store all of the objects, even the collectable ones - benchmark(:info, "Stored configuration for #{client}") do - # Try to batch things a bit, by putting them into - # a transaction - Puppet::Rails::Host.transaction do - Puppet::Rails::Host.store( - :objects => objects, - :host => client, - :facts => facts - ) - end - end - } - else - # We store all of the objects, even the collectable ones - benchmark(:info, "Stored configuration for #{client}") do - Puppet::Rails::Host.transaction do - Puppet::Rails::Host.store( - :objects => objects, - :host => client, - :facts => facts - ) - end - end - end - - # Now that we've stored everything, we need to strip out - # the collectable objects so that they are not sent on - # to the host - objects.collectstrip! + storeconfigs( + :objects => objects, + :host => client, + :facts => facts + ) end return objects end + # Connect to the LDAP Server + def setup_ldap + self.class.ldap = nil + begin + require 'ldap' + rescue LoadError + Puppet.notice( + "Could not set up LDAP Connection: Missing ruby/ldap libraries" + ) + @ldap = nil + return + end + begin + @ldap = self.class.ldap() + rescue => detail + raise Puppet::Error, "Could not connect to LDAP: %s" % detail + end + end + def scope return @scope end private + # Check whether any of our files have changed. + def checkfiles + if @files.find { |f| f.changed? } + @parsedate = Time.now.to_i + end + end + + # Parse the files, generating our parse tree. This automatically + # reparses only if files are updated, so it's safe to call multiple + # times. def parsefiles + # First check whether there are updates to any non-puppet files + # like templates. If we need to reparse, this will get quashed, + # but it needs to be done first in case there's no reparse + # but there are other file changes. + checkfiles() + + # Check if the parser should reparse. if @file if defined? @parser # Only check the files every 15 seconds or so, not on @@ -409,6 +396,8 @@ module Puppet end if defined? @parser + # If this isn't our first time parsing in this process, + # note that we're reparsing. Puppet.info "Reloading files" end # should i be creating a new parser each time...? @@ -431,6 +420,46 @@ module Puppet @parsedate = Time.now.to_i @lastchecked = Time.now end + + # Store the configs into the database. + def storeconfigs(hash) + unless defined? ActiveRecord + require 'puppet/rails' + unless defined? ActiveRecord + raise LoadError, + "storeconfigs is enabled but rails is unavailable" + end + end + + Puppet::Rails.init + + # Fork the storage, since we don't need the client waiting + # on that. How do I avoid this duplication? + if @forksave + fork { + # We store all of the objects, even the collectable ones + benchmark(:info, "Stored configuration for #{hash[:client]}") do + # Try to batch things a bit, by putting them into + # a transaction + Puppet::Rails::Host.transaction do + Puppet::Rails::Host.store(hash) + end + end + } + else + # We store all of the objects, even the collectable ones + benchmark(:info, "Stored configuration for #{hash[:client]}") do + Puppet::Rails::Host.transaction do + Puppet::Rails::Host.store(hash) + end + end + end + + # Now that we've stored everything, we need to strip out + # the collectable objects so that they are not sent on + # to the host + hash[:objects].collectstrip! + end end end end diff --git a/lib/puppet/parser/scope.rb b/lib/puppet/parser/scope.rb index 307545143..e9dabdcea 100644 --- a/lib/puppet/parser/scope.rb +++ b/lib/puppet/parser/scope.rb @@ -27,6 +27,11 @@ module Puppet::Parser raise Puppet::ParseError "Could not find template %s" % file end + + # We'll only ever not have an interpreter in testing, but, eh. + if @scope.interp + @scope.interp.newfile(@file) + end end def method_missing(name, *args) diff --git a/test/language/functions.rb b/test/language/functions.rb index ae4b115ea..1ee7ccf8e 100755 --- a/test/language/functions.rb +++ b/test/language/functions.rb @@ -193,6 +193,52 @@ class TestLangFunctions < Test::Unit::TestCase ast.evaluate(:scope => scope) end end + + def test_template_reparses + template = tempfile() + + File.open(template, "w") do |f| + f.puts "original text" + end + + manifest = tempfile() + file = tempfile() + File.open(manifest, "w") do |f| + f.puts %{file { "#{file}": content => template("#{template}") }} + end + + interpreter = Puppet::Parser::Interpreter.new( + :Manifest => manifest, + :UseNodes => false + ) + + parsedate = interpreter.parsedate() + + objects = nil + assert_nothing_raised { + objects = interpreter.run("myhost", {}) + } + + fileobj = objects[0] + + assert_equal("original text\n", fileobj["content"], + "Template did not work") + + # Have to sleep because one second is the fs's time granularity. + sleep(1) + + # Now modify the template + File.open(template, "w") do |f| + f.puts "new text" + end + + assert_nothing_raised { + objects = interpreter.run("myhost", {}) + } + newdate = interpreter.parsedate() + + assert(parsedate != newdate, "Parse date did not change") + end end # $Id$ |