class Transaction::Persistence

A persistence store implementation for storing information between transaction runs for the purposes of information inference (such as calculating corrective_change). @api private

Public Class Methods

new() click to toggle source
   # File lib/puppet/transaction/persistence.rb
 9 def initialize
10   @old_data = {}
11   @new_data = {"resources" => {}}
12 end

Public Instance Methods

copy_skipped(resource_name) click to toggle source
   # File lib/puppet/transaction/persistence.rb
43 def copy_skipped(resource_name)
44   @old_data["resources"] ||= {}
45   old_value = @old_data["resources"][resource_name]
46   if !old_value.nil?
47     @new_data["resources"][resource_name] = old_value
48   end
49 end
data() click to toggle source

Obtain the full raw data from the persistence store. @return [Hash] hash of data stored in persistence store

   # File lib/puppet/transaction/persistence.rb
16 def data
17   @old_data
18 end
enabled?(catalog) click to toggle source

Use the catalog and run_mode to determine if persistence should be enabled or not @param [Puppet::Resource::Catalog] catalog catalog being processed @return [boolean] true if persistence is enabled

    # File lib/puppet/transaction/persistence.rb
106 def enabled?(catalog)
107   catalog.host_config? && Puppet.run_mode.name == :agent
108 end
get_system_value(resource_name, param_name) click to toggle source

Retrieve the system value using the resource and parameter name @param [String] resource_name name of resource @param [String] param_name name of the parameter @return [Object,nil] the system_value

   # File lib/puppet/transaction/persistence.rb
24 def get_system_value(resource_name, param_name)
25   if !@old_data["resources"].nil? &&
26      !@old_data["resources"][resource_name].nil? &&
27      !@old_data["resources"][resource_name]["parameters"].nil? &&
28      !@old_data["resources"][resource_name]["parameters"][param_name].nil?
29     @old_data["resources"][resource_name]["parameters"][param_name]["system_value"]
30   else
31     nil
32   end
33 end
load() click to toggle source

Load data from the persistence store on disk.

   # File lib/puppet/transaction/persistence.rb
52 def load
53   filename = Puppet[:transactionstorefile]
54   unless Puppet::FileSystem.exist?(filename)
55     return
56   end
57   unless File.file?(filename)
58     Puppet.warning(_("Transaction store file %{filename} is not a file, ignoring") % { filename: filename })
59     return
60   end
61 
62   result = nil
63   Puppet::Util.benchmark(:debug, _("Loaded transaction store file in %{seconds} seconds")) do
64     begin
65       result = Puppet::Util::Yaml.safe_load_file(filename, [Symbol, Time])
66     rescue Puppet::Util::Yaml::YamlLoadError => detail
67       Puppet.log_exception(detail, _("Transaction store file %{filename} is corrupt (%{detail}); replacing") % { filename: filename, detail: detail })
68 
69       begin
70         File.rename(filename, filename + ".bad")
71       rescue => detail
72         Puppet.log_exception(detail, _("Unable to rename corrupt transaction store file: %{detail}") % { detail: detail })
73         raise Puppet::Error, _("Could not rename corrupt transaction store file %{filename}; remove manually") % { filename: filename }, detail.backtrace
74       end
75 
76       result = {}
77     end
78   end
79 
80   unless result.is_a?(Hash)
81     Puppet.err _("Transaction store file %{filename} is valid YAML but not returning a hash. Check the file for corruption, or remove it before continuing.") % { filename: filename }
82     return
83   end
84 
85   @old_data = result
86 end
save() click to toggle source

Save data from internal class to persistence store on disk.

    # File lib/puppet/transaction/persistence.rb
 89 def save
 90   converted_data = Puppet::Pops::Serialization::ToDataConverter.convert(
 91     @new_data, {
 92       symbol_as_string: false,
 93       local_reference: false,
 94       type_by_reference: true,
 95       force_symbol: true,
 96       silence_warnings: true,
 97       message_prefix: to_s
 98     }
 99   )
100   Puppet::Util::Yaml.dump(converted_data, Puppet[:transactionstorefile])
101 end
set_system_value(resource_name, param_name, value) click to toggle source
   # File lib/puppet/transaction/persistence.rb
35 def set_system_value(resource_name, param_name, value)
36   @new_data["resources"] ||= {}
37   @new_data["resources"][resource_name] ||= {}
38   @new_data["resources"][resource_name]["parameters"] ||= {}
39   @new_data["resources"][resource_name]["parameters"][param_name] ||= {}
40   @new_data["resources"][resource_name]["parameters"][param_name]["system_value"] = value
41 end