diff options
author | luke <luke@980ebf18-57e1-0310-9a29-db15c13687c0> | 2006-01-03 00:35:31 +0000 |
---|---|---|
committer | luke <luke@980ebf18-57e1-0310-9a29-db15c13687c0> | 2006-01-03 00:35:31 +0000 |
commit | 4eaf13ad0bc3057fb439fbc0a7090caa6d8ebb0c (patch) | |
tree | 8543bb72bbb5931747693b79ea636820d4d6e6ab | |
parent | 3c14db187d68bcfebbd5cd017f1b38326645b07d (diff) | |
download | puppet-4eaf13ad0bc3057fb439fbc0a7090caa6d8ebb0c.tar.gz puppet-4eaf13ad0bc3057fb439fbc0a7090caa6d8ebb0c.tar.xz puppet-4eaf13ad0bc3057fb439fbc0a7090caa6d8ebb0c.zip |
converting storage from Marshal to YAML
git-svn-id: https://reductivelabs.com/svn/puppet/trunk@759 980ebf18-57e1-0310-9a29-db15c13687c0
-rw-r--r-- | lib/puppet/storage.rb | 67 | ||||
-rw-r--r-- | lib/puppet/util.rb | 15 | ||||
-rwxr-xr-x | test/other/storage.rb | 9 | ||||
-rw-r--r-- | test/puppettest.rb | 1 |
4 files changed, 54 insertions, 38 deletions
diff --git a/lib/puppet/storage.rb b/lib/puppet/storage.rb index da6061738..a1ca9e1cc 100644 --- a/lib/puppet/storage.rb +++ b/lib/puppet/storage.rb @@ -1,3 +1,5 @@ +require 'yaml' + module Puppet # a class for storing state class Storage @@ -31,28 +33,26 @@ module Puppet Puppet.info "Statefile %s does not exist" % Puppet[:checksumfile] return end - begin #Puppet.debug "Loading statefile %s" % Puppet[:checksumfile] - Puppet::Util.lock(Puppet[:checksumfile]) { - File.open(Puppet[:checksumfile]) { |file| - file.each { |line| - myclass, key, value = line.split(@@splitchar) - - begin - @@state[eval(myclass)][key] = Marshal::load(value) - rescue => detail - raise Puppet::Error, - "Failed to load value for %s::%s => %s" % [ - myclass,key,detail - ], caller - end - } - } + Puppet::Util.lock(Puppet[:checksumfile]) { |file| + #@@state = Marshal.load(file) + @@state = YAML.load(file) + #File.open(Puppet[:checksumfile]) { |file| + # file.each { |line| + # line.chomp! + # myclass, key, value = line.split(@@splitchar) +# +# begin +# @@state[eval(myclass)][key] = Marshal::load(value) +# rescue => detail +# raise Puppet::Error, +# "Failed to load value for %s::%s => %s" % [ +# myclass,key,detail +# ], caller +# end +# } + #} } - rescue => detail - Puppet.err "Could not read %s" % Puppet[:checksumfile] - raise - end #Puppet.debug "Loaded state is %s" % @@state.inspect end @@ -61,8 +61,7 @@ module Puppet unless myclass.is_a? Class myclass = myclass.class end - result = @@state[myclass] - return result + return @@state[myclass.to_s] end def self.store @@ -81,18 +80,20 @@ module Puppet Puppet.info "Creating state file %s" % Puppet[:checksumfile] end - Puppet::Util.lock(Puppet[:checksumfile]) { - File.open(Puppet[:checksumfile], File::CREAT|File::WRONLY, 0600) { |file| - @@state.each { |klass, thash| - thash.each { |key,value| - mvalue = Marshal::dump(value) - file.puts([klass,key,mvalue].join(@@splitchar)) - } - } - } + Puppet::Util.lock(Puppet[:checksumfile], File::CREAT|File::WRONLY, 0600) { |file| + file.print YAML.dump(@@state) + #file.puts(Marshal::dump(@@state)) + #File.open(Puppet[:checksumfile], File::CREAT|File::WRONLY, 0600) { |file| + # @@state.each { |klass, thash| + # thash.each { |key,value| + # Puppet.warning "Storing: %s %s %s" % + # [klass, key.inspect, value.inspect] + # mvalue = Marshal::dump(value) + # file.puts([klass,key,mvalue].join(@@splitchar)) + # } + # } + #} } - - #Puppet.debug "Stored state is %s" % @@state.inspect end end end diff --git a/lib/puppet/util.rb b/lib/puppet/util.rb index ec9b6e1fe..d1878f592 100644 --- a/lib/puppet/util.rb +++ b/lib/puppet/util.rb @@ -86,13 +86,20 @@ module Util end # Create a lock file while something is happening - def self.lock(file) - lock = file + ".lock" + def self.lock(*opts) + lock = opts[0] + ".lock" while File.exists?(lock) - Puppet.debug "%s is locked" % file + #Puppet.debug "%s is locked" % opts[0] sleep 0.1 end - yield + File.open(lock, "w") { |f| f.print " "; f.flush } + begin + File.open(*opts) { |file| yield file } + rescue + raise + ensure + File.delete(lock) + end end # Create instance methods for each of the log levels. This allows diff --git a/test/other/storage.rb b/test/other/storage.rb index 787fe9cdd..0a52196d6 100755 --- a/test/other/storage.rb +++ b/test/other/storage.rb @@ -29,6 +29,12 @@ class TestParsedFile < Test::Unit::TestCase Puppet::Storage.store } assert_nothing_raised { + Puppet::Storage.clear + } + assert_nothing_raised { + Puppet::Storage.load + } + assert_nothing_raised { state = Puppet::Storage.state(hash) } @@ -39,7 +45,7 @@ class TestParsedFile < Test::Unit::TestCase # are reading or writing the file at once # so we need to test that def test_multiwrite - value = {:a => :b, :c => :d} + value = {:a => :b} threads = [] 9.times { |a| threads << Thread.new { @@ -47,6 +53,7 @@ class TestParsedFile < Test::Unit::TestCase assert_nothing_raised { Puppet::Storage.load state = Puppet::Storage.state(value) + value.each { |k,v| state[k] = v } state[:e] = rand(100) Puppet::Storage.store } diff --git a/test/puppettest.rb b/test/puppettest.rb index be3ec8279..0871b5c13 100644 --- a/test/puppettest.rb +++ b/test/puppettest.rb @@ -98,6 +98,7 @@ module TestPuppet } @@tmppids.clear Puppet::Type.allclear + Puppet::Storage.clear Puppet.clear # reset all of the logs |