summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorluke <luke@980ebf18-57e1-0310-9a29-db15c13687c0>2007-07-11 20:23:49 +0000
committerluke <luke@980ebf18-57e1-0310-9a29-db15c13687c0>2007-07-11 20:23:49 +0000
commitd104d4b32b34434e42a83078bccaa68890c1b373 (patch)
treee11ab0b246ed044c3fd61491e4b3501c9341366e
parent17a830d5421a0ae9d84914a4215010bf4b16253f (diff)
downloadpuppet-d104d4b32b34434e42a83078bccaa68890c1b373.tar.gz
puppet-d104d4b32b34434e42a83078bccaa68890c1b373.tar.xz
puppet-d104d4b32b34434e42a83078bccaa68890c1b373.zip
Having FileType instances automatically back their contents up to a filebucket, so it is much harder to lose content. This does not yet back up crontab contents, though.
git-svn-id: https://reductivelabs.com/svn/puppet/trunk@2679 980ebf18-57e1-0310-9a29-db15c13687c0
-rw-r--r--CHANGELOG4
-rw-r--r--lib/puppet/network/client/dipper.rb2
-rwxr-xr-xlib/puppet/provider/parsedfile.rb1
-rwxr-xr-xlib/puppet/type/pfilebucket.rb5
-rwxr-xr-xlib/puppet/util/filetype.rb11
-rwxr-xr-xtest/util/filetype.rb50
6 files changed, 67 insertions, 6 deletions
diff --git a/CHANGELOG b/CHANGELOG
index c37687faf..9e466e8bd 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,3 +1,7 @@
+ Files modified using a FileType instance, as ParsedFile
+ does, will now automatically get backed up to the filebucket
+ named "puppet".
+
Added a 'maillist' type for managing mailing lists.
Added a 'mailalias' type for managing mail aliases.
diff --git a/lib/puppet/network/client/dipper.rb b/lib/puppet/network/client/dipper.rb
index 5ddb67db1..c16e37725 100644
--- a/lib/puppet/network/client/dipper.rb
+++ b/lib/puppet/network/client/dipper.rb
@@ -19,7 +19,7 @@ class Puppet::Network::Client::Dipper < Puppet::Network::Client
# Back up a file to our bucket
def backup(file)
unless FileTest.exists?(file)
- raise(BucketError, "File %s does not exist" % file)
+ raise(ArgumentError, "File %s does not exist" % file)
end
contents = ::File.read(file)
unless local?
diff --git a/lib/puppet/provider/parsedfile.rb b/lib/puppet/provider/parsedfile.rb
index 4e3dac8f8..cdb770b8b 100755
--- a/lib/puppet/provider/parsedfile.rb
+++ b/lib/puppet/provider/parsedfile.rb
@@ -316,7 +316,6 @@ class Puppet::Provider::ParsedFile < Puppet::Provider
end
@property_hash[:name] ||= @resource.name
- warning "Flushing"
self.class.flush(@property_hash)
end
diff --git a/lib/puppet/type/pfilebucket.rb b/lib/puppet/type/pfilebucket.rb
index fd0f131a1..df30c1872 100755
--- a/lib/puppet/type/pfilebucket.rb
+++ b/lib/puppet/type/pfilebucket.rb
@@ -65,9 +65,10 @@ module Puppet
# Create a default filebucket.
def self.mkdefaultbucket
- unless self["puppet"]
- self.create :name => "puppet", :path => Puppet[:clientbucketdir]
+ unless default = self["puppet"]
+ default = self.create :name => "puppet", :path => Puppet[:clientbucketdir]
end
+ default
end
def self.instances
diff --git a/lib/puppet/util/filetype.rb b/lib/puppet/util/filetype.rb
index 8abe0cc00..7ce732b63 100755
--- a/lib/puppet/util/filetype.rb
+++ b/lib/puppet/util/filetype.rb
@@ -67,6 +67,16 @@ class Puppet::Util::FileType
@filetypes[type]
end
+ # Back the file up before replacing it.
+ def backup
+ bucket.backup(@path) if FileTest.exists?(@path)
+ end
+
+ # Pick or create a filebucket to use.
+ def bucket
+ Puppet::Type.type(:filebucket).mkdefaultbucket.bucket
+ end
+
def initialize(path)
@path = path
end
@@ -91,6 +101,7 @@ class Puppet::Util::FileType
# Overwrite the file.
def write(text)
+ backup()
File.open(@path, "w") { |f| f.print text; f.flush }
end
end
diff --git a/test/util/filetype.rb b/test/util/filetype.rb
index 6f026f9f1..2c4a16fc2 100755
--- a/test/util/filetype.rb
+++ b/test/util/filetype.rb
@@ -2,9 +2,9 @@
$:.unshift("../lib").unshift("../../lib") if __FILE__ =~ /\.rb$/
-require 'puppet'
-require 'puppet/util/filetype'
require 'puppettest'
+require 'puppet/util/filetype'
+require 'mocha'
class TestFileType < Test::Unit::TestCase
include PuppetTest
@@ -53,6 +53,52 @@ class TestFileType < Test::Unit::TestCase
assert_equal(text, newtext, "Text was changed somehow")
end
+ # Make sure that modified files are backed up before they're changed.
+ def test_backup_is_called
+ path = tempfile
+ File.open(path, "w") { |f| f.print 'yay' }
+
+ obj = Puppet::Util::FileType.filetype(:flat).new(path)
+
+ obj.expects(:backup)
+
+ obj.write("something")
+
+ assert_equal("something", File.read(path), "File did not get changed")
+ end
+
+ def test_backup
+ path = tempfile
+ type = Puppet::Type.type(:filebucket)
+
+ obj = Puppet::Util::FileType.filetype(:flat).new(path)
+
+ # First try it when the file does not yet exist.
+ assert_nothing_raised("Could not call backup when file does not exist") do
+ obj.backup
+ end
+
+ # Then create the file
+ File.open(path, "w") { |f| f.print 'one' }
+
+ # Then try it with no filebucket objects
+ assert_nothing_raised("Could not call backup with no buckets") do
+ obj.backup
+ end
+ puppet = type["puppet"]
+ assert(puppet, "Did not create default filebucket")
+
+ assert_equal("one", puppet.bucket.getfile(Digest::MD5.hexdigest(File.read(path))), "Could not get file from backup")
+
+ # Try it again when the default already exists
+ File.open(path, "w") { |f| f.print 'two' }
+ assert_nothing_raised("Could not call backup with no buckets") do
+ obj.backup
+ end
+
+ assert_equal("two", puppet.bucket.getfile(Digest::MD5.hexdigest(File.read(path))), "Could not get file from backup")
+ end
+
if Facter["operatingsystem"].value == "Darwin"
def test_ninfotoarray
obj = nil