From c5782dfb9aa6ecfab045d348a7d07e38bcb4a8e2 Mon Sep 17 00:00:00 2001 From: luke Date: Thu, 12 Jan 2006 16:57:34 +0000 Subject: Adding "content" state to files, and string interpolation handles escaped whitespace characters. git-svn-id: https://reductivelabs.com/svn/puppet/trunk@813 980ebf18-57e1-0310-9a29-db15c13687c0 --- lib/puppet/parser/scope.rb | 2 +- lib/puppet/type/pfile.rb | 7 ++++++ lib/puppet/type/pfile/content.rb | 46 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 1 deletion(-) create mode 100755 lib/puppet/type/pfile/content.rb (limited to 'lib') 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$ -- cgit