mirror of
https://github.com/krislamo/puppet-rsnapshot
synced 2025-09-07 13:59:28 +00:00
random time feature with ranges (random between 1am and 5 am: 1..5)
This commit is contained in:
18
lib/puppet/parser/functions/assert_empty_hash.rb
Normal file
18
lib/puppet/parser/functions/assert_empty_hash.rb
Normal file
@@ -0,0 +1,18 @@
|
||||
module Puppet::Parser::Functions
|
||||
newfunction(:assert_empty_hash, :type => :rvalue, :doc => <<-EOS
|
||||
This function checks an input struct for undefined hashes in key => hash and assigns {}. This is only a helper function to make a hash.each work if one of the values is undefined
|
||||
EOS
|
||||
)do |args|
|
||||
fail "Must receive one argument." if args.empty?
|
||||
args.each do |arg|
|
||||
h = Hash.new
|
||||
arg.each_pair do |host, hash|
|
||||
unless hash.is_a? Hash
|
||||
hash = {}
|
||||
end
|
||||
h[host] = hash
|
||||
end
|
||||
return h
|
||||
end
|
||||
end
|
||||
end
|
37
lib/puppet/parser/functions/rand_from_array.rb
Normal file
37
lib/puppet/parser/functions/rand_from_array.rb
Normal file
@@ -0,0 +1,37 @@
|
||||
module Puppet::Parser::Functions
|
||||
newfunction(:rand_from_array, :type => :rvalue, :doc => <<-EOS
|
||||
This function takes either an int or an array as input and returns the int or a
|
||||
random element from the array
|
||||
EOS
|
||||
)do |args|
|
||||
fail "Must receive two argument." if args.empty?
|
||||
#++ this works if the input is an array
|
||||
# return args.sample
|
||||
# +++++++++++++++++++++++++++++++++++++
|
||||
# args.flatten!
|
||||
params = []
|
||||
params << args[0]
|
||||
params.flatten!
|
||||
arr = []
|
||||
salt = args[1].sum % 60
|
||||
rnd = Random.new(salt)
|
||||
# rnd = Random.new()
|
||||
params.each do |key|
|
||||
if key.is_a?(String) and key =~ /\d\.\.\d/
|
||||
k = key.split('..')
|
||||
r = Range.new(k[0],k[1]).to_a
|
||||
arr << r
|
||||
arr.flatten!
|
||||
elsif key.is_a?(String)
|
||||
arr << key
|
||||
arr.flatten!
|
||||
elsif key.is_a?(Integer)
|
||||
arr << key.to_s
|
||||
arr.flatten!
|
||||
end
|
||||
end
|
||||
last_i = arr.length - 1
|
||||
return arr[rnd.rand(0..last_i)]
|
||||
|
||||
end
|
||||
end
|
Reference in New Issue
Block a user