class Puppet::Util::Storage

a class for storing state

Public Class Methods

cache(object) click to toggle source

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.

   # File lib/puppet/util/storage.rb
24 def self.cache(object)
25   if object.is_a?(Symbol)
26     name = object
27   else
28     name = object.to_s
29   end
30 
31   @@state[name] ||= {}
32 end
clear() click to toggle source
   # File lib/puppet/util/storage.rb
34 def self.clear
35   @@state.clear
36 end
init() click to toggle source
   # File lib/puppet/util/storage.rb
38 def self.init
39   @@state = {}
40 end
load() click to toggle source
   # File lib/puppet/util/storage.rb
44 def self.load
45   Puppet.settings.use(:main) unless FileTest.directory?(Puppet[:statedir])
46   filename = Puppet[:statefile]
47 
48   unless Puppet::FileSystem.exist?(filename)
49     self.init if @@state.nil?
50     return
51   end
52   unless File.file?(filename)
53     Puppet.warning(_("Checksumfile %{filename} is not a file, ignoring") % { filename: filename })
54     return
55   end
56   Puppet::Util.benchmark(:debug, "Loaded state in %{seconds} seconds") do
57     begin
58       @@state = Puppet::Util::Yaml.safe_load_file(filename, [Symbol, Time])
59     rescue Puppet::Util::Yaml::YamlLoadError => detail
60       Puppet.err _("Checksumfile %{filename} is corrupt (%{detail}); replacing") % { filename: filename, detail: detail }
61 
62       begin
63         File.rename(filename, filename + ".bad")
64       rescue
65         raise Puppet::Error, _("Could not rename corrupt %{filename}; remove manually") % { filename: filename }, detail.backtrace
66       end
67     end
68   end
69 
70   unless @@state.is_a?(Hash)
71     Puppet.err _("State got corrupted")
72     self.init
73   end
74 end
new() click to toggle source
   # File lib/puppet/util/storage.rb
15 def initialize
16   self.class.load
17 end
state() click to toggle source
   # File lib/puppet/util/storage.rb
11 def self.state
12   @@state
13 end
stateinspect() click to toggle source
   # File lib/puppet/util/storage.rb
76 def self.stateinspect
77   @@state.inspect
78 end
store() click to toggle source
    # File lib/puppet/util/storage.rb
 80 def self.store
 81   Puppet.debug "Storing state"
 82 
 83   Puppet.info _("Creating state file %{file}") % { file: Puppet[:statefile] } unless Puppet::FileSystem.exist?(Puppet[:statefile])
 84 
 85   if Puppet[:statettl] == 0 || Puppet[:statettl] == Float::INFINITY
 86     Puppet.debug "Not pruning old state cache entries"
 87   else
 88     Puppet::Util.benchmark(:debug, "Pruned old state cache entries in %{seconds} seconds") do
 89       ttl_cutoff = Time.now - Puppet[:statettl]
 90 
 91       @@state.reject! do |k,v|
 92         @@state[k][:checked] && @@state[k][:checked] < ttl_cutoff
 93       end
 94     end
 95   end
 96 
 97   Puppet::Util.benchmark(:debug, "Stored state in %{seconds} seconds") do
 98     Puppet::Util::Yaml.dump(@@state, Puppet[:statefile])
 99   end
100 end