diff options
-rw-r--r-- | lib/puppet/parser/scope.rb | 2 | ||||
-rw-r--r-- | lib/puppet/type/pfile.rb | 7 | ||||
-rwxr-xr-x | lib/puppet/type/pfile/content.rb | 46 | ||||
-rw-r--r-- | test/types/file.rb | 40 |
4 files changed, 94 insertions, 1 deletions
diff --git a/lib/puppet/parser/scope.rb b/lib/puppet/parser/scope.rb index c5ee0226a..a2d05e7d4 100644 --- a/lib/puppet/parser/scope.rb +++ b/lib/puppet/parser/scope.rb @@ -534,7 +534,7 @@ module Puppet end end #Puppet.debug("result is '%s'" % newstring) - return newstring + return newstring.gsub(/\\t/, "\t").gsub(/\\n/, "\n").gsub(/\\s/, "\s") end # This method will fail if the named object is already defined anywhere diff --git a/lib/puppet/type/pfile.rb b/lib/puppet/type/pfile.rb index c13e7038d..6a52d31e9 100644 --- a/lib/puppet/type/pfile.rb +++ b/lib/puppet/type/pfile.rb @@ -85,6 +85,12 @@ module Puppet } end + validate do + if self[:content] and self[:source] + raise Puppet::Error, "You cannot specify both content and a source" + end + end + @depthfirst = false @@ -540,6 +546,7 @@ end # the order they are in the state list. require 'puppet/type/pfile/create' require 'puppet/type/pfile/checksum' +require 'puppet/type/pfile/content' require 'puppet/type/pfile/source' require 'puppet/type/pfile/uid' require 'puppet/type/pfile/group' diff --git a/lib/puppet/type/pfile/content.rb b/lib/puppet/type/pfile/content.rb new file mode 100755 index 000000000..45aebdbda --- /dev/null +++ b/lib/puppet/type/pfile/content.rb @@ -0,0 +1,46 @@ +module Puppet + Puppet.type(:file).newstate(:content) do + desc "Specify the contents of a file as a string. Newlines, tabs, and spaces + can be specified using the escaped syntax (e.g., \\n for a newline). The + primary purpose of this parameter is to provide a kind of limited + templating." + + # We should probably take advantage of existing md5 sums if they're there, + # but I really don't feel like dealing with the complexity right now. + def retrieve + unless FileTest.exists?(@parent.name) + @is = :notfound + return + end + begin + @is = File.read(@parent.name) + rescue => detail + @is = nil + raise Puppet::Error, "Could not read %s: %s" % + [@parent.name, detail] + end + end + + + # Just write our content out to disk. + def sync + begin + File.open(@parent.name, "w") { |f| + f.print self.should + f.flush + } + rescue => detail + raise Puppet::Error, "Could not write content to %s: %s" % + [@parent.name, detail] + end + + if @is == :notfound + return :file_created + else + return :file_changed + end + end + end +end + +# $Id$ diff --git a/test/types/file.rb b/test/types/file.rb index 4aee2a4e9..f0bcf8ed6 100644 --- a/test/types/file.rb +++ b/test/types/file.rb @@ -587,6 +587,46 @@ class TestFile < Test::Unit::TestCase comp = newcomp(subobj, baseobj) assert_events([:directory_created, :file_created], comp) end + + def test_content + file = tempfile() + str = "This is some content" + + obj = nil + assert_nothing_raised { + obj = Puppet.type(:file).create( + :name => file, + :content => str + ) + } + + assert(!obj.insync?, "Object is incorrectly in sync") + + assert_events([:file_created], obj) + + obj.retrieve + + assert(obj.insync?, "Object is not in sync") + + text = File.read(file) + + assert_equal(str, text, "Content did not copy correctly") + + newstr = "Another string, yo" + + obj[:content] = newstr + + assert(!obj.insync?, "Object is incorrectly in sync") + + assert_events([:file_changed], obj) + + text = File.read(file) + + assert_equal(newstr, text, "Content did not copy correctly") + + obj.retrieve + assert(obj.insync?, "Object is not in sync") + end end # $Id$ |