Sha256: b9ab3717bfc3d867a5941dd0fc6536e49d26e730f4e69087f14c84a70e2d6b03

Contents?: true

Size: 1.39 KB

Versions: 11

Compression:

Stored size: 1.39 KB

Contents

require 'digest/sha2'

Puppet::Parser::Functions::newfunction(:fqdn_rand, :arity => -2, :type => :rvalue, :doc =>
  "Usage: `fqdn_rand(MAX, [SEED])`. MAX is required and must be a positive
  integer; SEED is optional and may be any number or string.

  Generates a random Integer number greater than or equal to 0 and less than MAX,
  combining the `$fqdn` fact and the value of SEED for repeatable randomness.
  (That is, each node will get a different random number from this function, but
  a given node's result will be the same every time unless its hostname changes.)

  This function is usually used for spacing out runs of resource-intensive cron
  tasks that run on many nodes, which could cause a thundering herd or degrade
  other services if they all fire at once. Adding a SEED can be useful when you
  have more than one such task and need several unrelated random numbers per
  node. (For example, `fqdn_rand(30)`, `fqdn_rand(30, 'expensive job 1')`, and
  `fqdn_rand(30, 'expensive job 2')` will produce totally different numbers.)") do |args|
    max = args.shift.to_i
 
    # We are consciously not using different hash algs based on fips mode here
    # since the randomness is not guaranteed to be predictable for a given node
    # It just needs to be unique for a given node
    seed = Digest::SHA256.hexdigest([self['::fqdn'],max,args].join(':')).hex
    Puppet::Util.deterministic_rand_int(seed,max)
end

Version data entries

11 entries across 11 versions & 2 rubygems

Version Path
bolt-0.17.1 vendored/puppet/lib/puppet/parser/functions/fqdn_rand.rb
bolt-0.17.0 vendored/puppet/lib/puppet/parser/functions/fqdn_rand.rb
bolt-0.16.4 vendored/puppet/lib/puppet/parser/functions/fqdn_rand.rb
bolt-0.16.3 vendored/puppet/lib/puppet/parser/functions/fqdn_rand.rb
bolt-0.16.2 vendored/puppet/lib/puppet/parser/functions/fqdn_rand.rb
puppet-5.4.0 lib/puppet/parser/functions/fqdn_rand.rb
puppet-5.4.0-x86-mingw32 lib/puppet/parser/functions/fqdn_rand.rb
puppet-5.4.0-x64-mingw32 lib/puppet/parser/functions/fqdn_rand.rb
puppet-5.4.0-universal-darwin lib/puppet/parser/functions/fqdn_rand.rb
bolt-0.16.1 vendored/puppet/lib/puppet/parser/functions/fqdn_rand.rb
bolt-0.16.0 vendored/puppet/lib/puppet/parser/functions/fqdn_rand.rb