Puppet Function: extlib::cache_data
- Defined in:
- lib/puppet/functions/extlib/cache_data.rb
- Function type:
- Ruby 4.x API
Summary
Retrieves data from a cache file, or creates it with supplied data if the file doesn't existOverview
Retrieves data from a cache file, or creates it with supplied data if the file doesn’t exist
Useful for having data that’s randomly generated once on the master side (e.g. a password), but then stays the same on subsequent runs. Because it’s stored on the master on disk, it doesn’t work when you use mulitple Puppet masters that don’t share their vardir.
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 |
# File 'lib/puppet/functions/extlib/cache_data.rb', line 22 Puppet::Functions.create_function(:'extlib::cache_data') do # @param namespace Namespace for the cache # @param name Cache key within the namespace # @param initial_data The data for when there is no cache yet # @return The cached value when it exists. The initial data when no cache exists dispatch :cache_data do param 'String[1]', :namespace param 'String[1]', :name param 'Any', :initial_data return_type 'Any' end def cache_data(namespace, name, initial_data) cache_dir = File.join(Puppet[:vardir], namespace) cache = File.join(cache_dir, name) if File.exist? cache YAML.safe_load(File.read(cache)) else FileUtils.mkdir_p(cache_dir) File.open(cache, 'w', 0o600) do |c| c.write(YAML.dump(initial_data)) end File.chown(File.stat(Puppet[:vardir]).uid, nil, cache) File.chown(File.stat(Puppet[:vardir]).uid, nil, cache_dir) initial_data end end end |