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