1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
|
require 'yaml'
require 'sync'
#require 'puppet/lockfile'
module Puppet
# a class for storing state
class Storage
include Singleton
include Puppet::Util
def initialize
self.class.load
end
# Return a hash that will be stored to disk. It's worth noting
# here that we use the object's full path, not just the name/type
# combination. At the least, this is useful for those non-isomorphic
# types like exec, but it also means that if an object changes locations
# in the configuration it will lose its cache.
def self.cache(object)
unless object.is_a? Puppet::Type
raise Puppet::DevFail, "Must pass a Type instance to Storage.cache"
end
# We used to store things by path, now we store them by ref.
# In oscar(0.20.0) this changed to using the ref.
if @@state.include?(object.path)
@@state[object.ref] = @@state[object.path]
@@state.delete(object.path)
end
return @@state[object.ref] ||= {}
end
def self.clear
@@state.clear
Storage.init
end
def self.init
@@state = {}
@@splitchar = "\t"
end
self.init
def self.load
if Puppet[:statefile].nil?
raise Puppet::DevError, "Somehow the statefile is nil"
end
Puppet.config.use(:puppet)
unless File.exists?(Puppet[:statefile])
unless defined? @@state and ! @@state.nil?
self.init
end
return
end
Puppet::Util.benchmark(:debug, "Loaded state") do
Puppet::Util.readlock(Puppet[:statefile]) do |file|
begin
@@state = YAML.load(file)
rescue => detail
Puppet.err "Checksumfile %s is corrupt (%s); replacing" %
[Puppet[:statefile], detail]
begin
File.rename(Puppet[:statefile],
Puppet[:statefile] + ".bad")
rescue
raise Puppet::Error,
"Could not rename corrupt %s; remove manually" %
Puppet[:statefile]
end
end
end
end
unless @@state.is_a?(Hash)
Puppet.err "State got corrupted"
self.init
end
#Puppet.debug "Loaded state is %s" % @@state.inspect
end
def self.stateinspect
@@state.inspect
end
def self.store
Puppet.debug "Storing state"
unless FileTest.exist?(Puppet[:statefile])
Puppet.info "Creating state file %s" % Puppet[:statefile]
end
Puppet::Util.benchmark(:debug, "Stored state") do
Puppet::Util.writelock(Puppet[:statefile], 0660) do |file|
file.print YAML.dump(@@state)
end
end
end
end
end
# $Id: storage.rb 1835 2006-11-08 05:22:24Z luke $
|