Sha256: a92f55d41ba82f1f9d165ea283c402747dab07f3297b8bda19707eded0a73cfa

Contents?: true

Size: 1.66 KB

Versions: 19

Compression:

Stored size: 1.66 KB

Contents

require 'fileutils'
require 'yaml'
require 'etc'

# @summary Retrieves data from a cache file, or creates it with supplied data if the file doesn't exist
#
# 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.
#
# @example Calling the function
#   $password = cache_data('mysql', 'mysql_password', 'this_is_my_password')
#
# @example With a random password
#   $password = cache_data('mysql', 'mysql_password', random_password())
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.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

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
puppet-debugger-1.4.0 spec/fixtures/modules/extlib/lib/puppet/functions/extlib/cache_data.rb
puppet-debugger-1.3.0 spec/fixtures/modules/extlib/lib/puppet/functions/extlib/cache_data.rb
puppet-debugger-1.2.0 spec/fixtures/modules/extlib/lib/puppet/functions/extlib/cache_data.rb
puppet-debugger-1.1.0 spec/fixtures/modules/extlib/lib/puppet/functions/extlib/cache_data.rb
puppet-debugger-1.0.0 spec/fixtures/modules/extlib/lib/puppet/functions/extlib/cache_data.rb
puppet-debugger-0.19.0 spec/fixtures/modules/extlib/lib/puppet/functions/extlib/cache_data.rb
puppet-debugger-0.18.0 spec/fixtures/modules/extlib/lib/puppet/functions/extlib/cache_data.rb
puppet-debugger-0.17.0 spec/fixtures/modules/extlib/lib/puppet/functions/extlib/cache_data.rb
puppet-debugger-0.16.0 spec/fixtures/modules/extlib/lib/puppet/functions/extlib/cache_data.rb
puppet-debugger-0.15.2 spec/fixtures/modules/extlib/lib/puppet/functions/extlib/cache_data.rb
puppet-debugger-0.15.1 spec/fixtures/modules/extlib/lib/puppet/functions/extlib/cache_data.rb
puppet-debugger-0.15.0 spec/fixtures/modules/extlib/lib/puppet/functions/extlib/cache_data.rb
puppet-debugger-0.14.0 spec/fixtures/modules/extlib/lib/puppet/functions/extlib/cache_data.rb
puppet-debugger-0.13.0 spec/fixtures/modules/extlib/lib/puppet/functions/extlib/cache_data.rb
puppet-debugger-0.12.3 spec/fixtures/modules/extlib/lib/puppet/functions/extlib/cache_data.rb
puppet-debugger-0.12.2 spec/fixtures/modules/extlib/lib/puppet/functions/extlib/cache_data.rb
puppet-debugger-0.12.1 spec/fixtures/modules/extlib/lib/puppet/functions/extlib/cache_data.rb
puppet-debugger-0.12.0 spec/fixtures/modules/extlib/lib/puppet/functions/extlib/cache_data.rb
puppet-debugger-0.11.0 spec/fixtures/modules/extlib/lib/puppet/functions/extlib/cache_data.rb