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
# File lib/puppet/transaction/persistence.rb 11 def self.allowed_classes 12 @allowed_classes ||= [ 13 Symbol, 14 Time, 15 Regexp, 16 # URI is excluded, because it serializes all instance variables including the 17 # URI parser. Better to serialize the URL encoded representation. 18 SemanticPuppet::Version, 19 # SemanticPuppet::VersionRange has many nested classes and is unlikely to be 20 # used directly, so ignore it 21 Puppet::Pops::Time::Timestamp, 22 Puppet::Pops::Time::TimeData, 23 Puppet::Pops::Time::Timespan, 24 Puppet::Pops::Types::PBinaryType::Binary, 25 # Puppet::Pops::Types::PSensitiveType::Sensitive values are excluded from 26 # the persistence store, ignore it. 27 ].freeze 28 end
# File lib/puppet/transaction/persistence.rb 30 def initialize 31 @old_data = {} 32 @new_data = {"resources" => {}} 33 end
Public Instance Methods
# File lib/puppet/transaction/persistence.rb 64 def copy_skipped(resource_name) 65 @old_data["resources"] ||= {} 66 old_value = @old_data["resources"][resource_name] 67 if !old_value.nil? 68 @new_data["resources"][resource_name] = old_value 69 end 70 end
Obtain the full raw data from the persistence store. @return [Hash] hash of data stored in persistence store
# File lib/puppet/transaction/persistence.rb 37 def data 38 @old_data 39 end
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 117 def enabled?(catalog) 118 catalog.host_config? && Puppet.run_mode.name == :agent 119 end
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 45 def get_system_value(resource_name, param_name) 46 if !@old_data["resources"].nil? && 47 !@old_data["resources"][resource_name].nil? && 48 !@old_data["resources"][resource_name]["parameters"].nil? && 49 !@old_data["resources"][resource_name]["parameters"][param_name].nil? 50 @old_data["resources"][resource_name]["parameters"][param_name]["system_value"] 51 else 52 nil 53 end 54 end
Load data from the persistence store on disk.
# File lib/puppet/transaction/persistence.rb 73 def load 74 filename = Puppet[:transactionstorefile] 75 unless Puppet::FileSystem.exist?(filename) 76 return 77 end 78 unless File.file?(filename) 79 Puppet.warning(_("Transaction store file %{filename} is not a file, ignoring") % { filename: filename }) 80 return 81 end 82 83 result = nil 84 Puppet::Util.benchmark(:debug, _("Loaded transaction store file in %{seconds} seconds")) do 85 begin 86 result = Puppet::Util::Yaml.safe_load_file(filename, self.class.allowed_classes) 87 rescue Puppet::Util::Yaml::YamlLoadError => detail 88 Puppet.log_exception(detail, _("Transaction store file %{filename} is corrupt (%{detail}); replacing") % { filename: filename, detail: detail }) 89 90 begin 91 File.rename(filename, filename + ".bad") 92 rescue => detail 93 Puppet.log_exception(detail, _("Unable to rename corrupt transaction store file: %{detail}") % { detail: detail }) 94 raise Puppet::Error, _("Could not rename corrupt transaction store file %{filename}; remove manually") % { filename: filename }, detail.backtrace 95 end 96 97 result = {} 98 end 99 end 100 101 unless result.is_a?(Hash) 102 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 } 103 return 104 end 105 106 @old_data = result 107 end
Save data from internal class to persistence store on disk.
# File lib/puppet/transaction/persistence.rb 110 def save 111 Puppet::Util::Yaml.dump(@new_data, Puppet[:transactionstorefile]) 112 end
# File lib/puppet/transaction/persistence.rb 56 def set_system_value(resource_name, param_name, value) 57 @new_data["resources"] ||= {} 58 @new_data["resources"][resource_name] ||= {} 59 @new_data["resources"][resource_name]["parameters"] ||= {} 60 @new_data["resources"][resource_name]["parameters"][param_name] ||= {} 61 @new_data["resources"][resource_name]["parameters"][param_name]["system_value"] = value 62 end