diff options
| author | luke <luke@980ebf18-57e1-0310-9a29-db15c13687c0> | 2006-06-08 17:28:28 +0000 |
|---|---|---|
| committer | luke <luke@980ebf18-57e1-0310-9a29-db15c13687c0> | 2006-06-08 17:28:28 +0000 |
| commit | e73f2d479f4d3f1ea84016ee554f729d78808620 (patch) | |
| tree | 09532f188435d624df810f57e3e9b9f18c3bf0b6 | |
| parent | 4266f64ed5eea79e939c39b18861685a5b5a8ec7 (diff) | |
| download | puppet-e73f2d479f4d3f1ea84016ee554f729d78808620.tar.gz puppet-e73f2d479f4d3f1ea84016ee554f729d78808620.tar.xz puppet-e73f2d479f4d3f1ea84016ee554f729d78808620.zip | |
Fixing #158. I did not add a force option, since I always back files up before I replace them.
git-svn-id: https://reductivelabs.com/svn/puppet/trunk@1244 980ebf18-57e1-0310-9a29-db15c13687c0
| -rw-r--r-- | lib/puppet/type/pfile.rb | 55 | ||||
| -rwxr-xr-x | lib/puppet/type/pfile/ensure.rb | 3 | ||||
| -rw-r--r-- | lib/puppet/type/pfile/target.rb | 19 | ||||
| -rw-r--r-- | test/types/file.rb | 45 |
4 files changed, 115 insertions, 7 deletions
diff --git a/lib/puppet/type/pfile.rb b/lib/puppet/type/pfile.rb index 9124c5c54..e464be572 100644 --- a/lib/puppet/type/pfile.rb +++ b/lib/puppet/type/pfile.rb @@ -256,9 +256,57 @@ module Puppet case File.stat(file).ftype when "directory": - self.info :bc - # we don't need to backup directories - return true + if self[:recurse] + # we don't need to backup directories when recurse is on + return true + else + backup = self[:backup] + case backup + when Puppet::Client::Dipper: + notice "Recursively backing up to filebucket" + require 'find' + Find.find(self[:path]) do |f| + if File.file?(f) + sum = backup.backup(f) + self.info "Filebucketed %s to %s with sum %s" % + [f, backup.name, sum] + end + end + + require 'fileutils' + FileUtils.rmtree(self[:path]) + return true + when String: + newfile = file + backup + # Just move it, since it's a directory. + if FileTest.exists?(newfile) + begin + File.unlink(newfile) + rescue => detail + puts detail.backtrace + self.err "Could not remove old backup: %s" % + detail + return false + end + end + begin + bfile = file + backup + + # Ruby 1.8.1 requires the 'preserve' addition, but + # later versions do not appear to require it. + FileUtils.cp_r(file, bfile, :preserve => true) + return true + rescue => detail + # since they said they want a backup, let's error out + # if we couldn't make one + self.fail "Could not back %s up: %s" % + [file, detail.message] + end + else + self.err "Invalid backup type %s" % backup + return false + end + end when "file": backup = self[:backup] case backup @@ -273,6 +321,7 @@ module Puppet begin File.unlink(newfile) rescue => detail + self.err "wtf?" self.err "Could not remove old backup: %s" % detail return false diff --git a/lib/puppet/type/pfile/ensure.rb b/lib/puppet/type/pfile/ensure.rb index cb3b8ae3e..033ce80e2 100755 --- a/lib/puppet/type/pfile/ensure.rb +++ b/lib/puppet/type/pfile/ensure.rb @@ -82,7 +82,8 @@ module Puppet self.set_directory return :directory_created else - return state.sync + state.mklink + return :link_created end else self.fail "Cannot create a symlink without a target" diff --git a/lib/puppet/type/pfile/target.rb b/lib/puppet/type/pfile/target.rb index b354e6650..dc1486560 100644 --- a/lib/puppet/type/pfile/target.rb +++ b/lib/puppet/type/pfile/target.rb @@ -12,13 +12,26 @@ module Puppet # Anything else, basically newvalue(/./) do + # Do nothing here, because sync is always called from the ensure state. + end + + # Create our link. + def mklink target = self.should if stat = @parent.stat - unless stat.ftype == "link" - self.fail "Not replacing non-symlink" + unless @parent.handlebackup + self.fail "Could not back up; will not replace with link" + end + + case stat.ftype + when "directory": + # uh, bad stuff + #self.fail "Not replacing directory" + FileUtils.rmtree(@parent[:path]) + else + File.unlink(@parent[:path]) end - File.unlink(@parent[:path]) end Dir.chdir(File.dirname(@parent[:path])) do unless FileTest.exists?(target) diff --git a/test/types/file.rb b/test/types/file.rb index e3f01d306..69ffad85e 100644 --- a/test/types/file.rb +++ b/test/types/file.rb @@ -1159,6 +1159,51 @@ class TestFile < Test::Unit::TestCase assert_events([:file_changed, :file_changed], obj) end + + def test_replacefilewithlink + path = tempfile() + link = tempfile() + + File.open(path, "w") { |f| f.puts "yay" } + File.open(link, "w") { |f| f.puts "a file" } + + file = nil + assert_nothing_raised { + file = Puppet.type(:file).create( + :ensure => path, + :path => link + ) + } + + assert_events([:link_created], file) + + assert(FileTest.symlink?(link), "Link was not created") + + assert_equal(path, File.readlink(link), "Link was created incorrectly") + end + + def test_replacedirwithlink + path = tempfile() + link = tempfile() + + File.open(path, "w") { |f| f.puts "yay" } + Dir.mkdir(link) + File.open(File.join(link, "yay"), "w") do |f| f.puts "boo" end + + file = nil + assert_nothing_raised { + file = Puppet.type(:file).create( + :ensure => path, + :path => link + ) + } + + assert_events([:link_created], file) + + assert(FileTest.symlink?(link), "Link was not created") + + assert_equal(path, File.readlink(link), "Link was created incorrectly") + end end # $Id$ |
